feat: message-per-row transcript, session resume, drop the composer (#4)
This commit was merged in pull request #4.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { MESSAGE_PREVIEW_LENGTH, buildThread, type LiveState } from "../lib/thread";
|
||||
import type { ChatTurn } from "../lib/types";
|
||||
|
||||
const idle: LiveState = { isStreaming: false, response: "", thinking: "" };
|
||||
|
||||
function turn(id: string, prompt: string, response: string, overrides: Partial<ChatTurn> = {}): ChatTurn {
|
||||
return {
|
||||
id,
|
||||
prompt,
|
||||
response,
|
||||
thinking: "",
|
||||
model: "m",
|
||||
createdAt: 100,
|
||||
toolCalls: [],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("buildThread", () => {
|
||||
it("returns nothing for an empty conversation", () => {
|
||||
expect(buildThread([], idle)).toEqual([]);
|
||||
});
|
||||
|
||||
it("flattens a turn into a user message followed by an assistant message", () => {
|
||||
const thread = buildThread([turn("t1", "hello", "hi there")], idle);
|
||||
|
||||
expect(thread.map((message) => message.role)).toEqual(["user", "assistant"]);
|
||||
expect(thread[0].text).toBe("hello");
|
||||
expect(thread[1].text).toBe("hi there");
|
||||
});
|
||||
|
||||
it("keeps messages in chronological order across turns", () => {
|
||||
const thread = buildThread(
|
||||
[turn("t1", "first question", "first answer"), turn("t2", "second question", "second answer")],
|
||||
idle,
|
||||
);
|
||||
|
||||
expect(thread.map((message) => message.text)).toEqual([
|
||||
"first question",
|
||||
"first answer",
|
||||
"second question",
|
||||
"second answer",
|
||||
]);
|
||||
});
|
||||
|
||||
it("gives every message a unique id", () => {
|
||||
const thread = buildThread([turn("t1", "a", "b"), turn("t2", "c", "d")], idle);
|
||||
const ids = thread.map((message) => message.id);
|
||||
|
||||
expect(new Set(ids).size).toBe(ids.length);
|
||||
});
|
||||
|
||||
it("builds one-line previews", () => {
|
||||
const thread = buildThread([turn("t1", "hello\n\nworld", "a\nlong\n\nanswer")], idle);
|
||||
|
||||
expect(thread[0].preview).toBe("hello world");
|
||||
expect(thread[1].preview).toBe("a long answer");
|
||||
});
|
||||
|
||||
it("clips a preview longer than the row can show", () => {
|
||||
const long = "x".repeat(MESSAGE_PREVIEW_LENGTH + 50);
|
||||
const thread = buildThread([turn("t1", long, long)], idle);
|
||||
|
||||
expect(thread[0].preview).toHaveLength(MESSAGE_PREVIEW_LENGTH);
|
||||
});
|
||||
|
||||
it("streams the newest turn only", () => {
|
||||
const live: LiveState = { isStreaming: true, response: "streaming now", thinking: "hmm" };
|
||||
const thread = buildThread([turn("t1", "old", "old answer"), turn("t2", "new", "")], live);
|
||||
|
||||
// The older turn stays put...
|
||||
expect(thread[1].isStreaming).toBe(false);
|
||||
expect(thread[1].text).toBe("old answer");
|
||||
|
||||
// ...and the newest assistant message reads from the live buffer.
|
||||
expect(thread[3].isStreaming).toBe(true);
|
||||
expect(thread[3].text).toBe("streaming now");
|
||||
expect(thread[3].thinking).toBe("hmm");
|
||||
|
||||
// The user's line is never "streaming".
|
||||
expect(thread[2].isStreaming).toBe(false);
|
||||
});
|
||||
|
||||
it("falls back to the activity line while an answer has no text yet", () => {
|
||||
const live: LiveState = { isStreaming: true, response: "", thinking: "", status: "Searching the web" };
|
||||
const thread = buildThread([turn("t1", "q", "")], live);
|
||||
|
||||
expect(thread[1].preview).toBe("Searching the web");
|
||||
});
|
||||
|
||||
it("falls back to a generic label while thinking", () => {
|
||||
const live: LiveState = { isStreaming: true, response: "", thinking: "" };
|
||||
const thread = buildThread([turn("t1", "q", "")], live);
|
||||
|
||||
expect(thread[1].preview).toBe("Thinking…");
|
||||
});
|
||||
|
||||
it("previews a stored error when a finished turn has no answer", () => {
|
||||
const thread = buildThread([turn("t1", "q", "", { error: "Request failed" })], idle);
|
||||
|
||||
expect(thread[1].preview).toBe("Request failed");
|
||||
expect(thread[1].error).toBe("Request failed");
|
||||
});
|
||||
|
||||
it("carries tool calls onto the assistant message", () => {
|
||||
const withTools = turn("t1", "q", "a", {
|
||||
toolCalls: [{ name: "web_search", arguments: {}, result: "ok", isError: false }],
|
||||
});
|
||||
const thread = buildThread([withTools], idle);
|
||||
|
||||
expect(thread[0].toolCalls).toEqual([]);
|
||||
expect(thread[1].toolCalls).toHaveLength(1);
|
||||
expect(thread[1].toolCalls[0].name).toBe("web_search");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user