Fit streamed markdown tables to their content, and test that replies don't jump

Replies already stream through <markdown streaming>; tables now size to their
cells with the theme's border colour. Tests cover tables mid-stream and settled,
the newest line staying in view, and a scrolled-up view staying still.
This commit is contained in:
Callum Alpass committed 2026-11-03 03:08:26 +11:00
1 parent d87026a66b
commit a9a4b47566
5 files changed
+78 -4

No files matched your search

+8
View File
@@ -166,3 +166,11 @@ export function withOmittedOutput(): OrchestrationV2ThreadProjection {
visibleTurnItems: [{ position: 0, visibility: "local", sourceThreadId: omittedCommand.threadId, sourceItemId: omittedCommand.id, item: omittedCommand }],
} as unknown as OrchestrationV2ThreadProjection;
}
/** The finished turn, its reply replaced by `text`, still streaming in if `streaming`. */
export function withReply(text: string, streaming: boolean): OrchestrationV2ThreadProjection {
const turn = structuredClone(withCompletedTurn()) as unknown as { turnItems: Record<string, unknown>[]; visibleTurnItems: { item: Record<string, unknown> }[] };
for (const item of turn.turnItems) if (item.type === "assistant_message") Object.assign(item, { text, streaming, status: streaming ? "running" : "completed" });
for (const visible of turn.visibleTurnItems) visible.item = turn.turnItems.find((item) => item.id === visible.item.id)!;
return turn as unknown as OrchestrationV2ThreadProjection;
}
+11 -2
View File
@@ -51,6 +51,7 @@ export function fakeSession(
} = {},
) {
const calls: Call[] = [];
const threadListeners = new Set<(value: unknown) => void>();
const snapshot = state.snapshot ?? v2ShellSnapshot;
const projection = state.projection ?? v2Projection;
const live = <T,>(value: T) => (listener: (value: T) => void) => {
@@ -60,7 +61,11 @@ export function fakeSession(
const known: Record<string, unknown> = {
getShell: () => ({ status: state.status ?? { type: "live" }, snapshot, serverUrl: "ws://test" }),
onShell: () => () => {},
watchThread: (_id: ThreadId, listener: (value: unknown) => void) => live({ status: "live", projection, history: { cursor: null, hasMore: false, loading: false } })(listener),
watchThread: (_id: ThreadId, listener: (value: unknown) => void) => {
threadListeners.add(listener);
listener({ status: "live", projection, history: { cursor: null, hasMore: false, loading: false } });
return () => threadListeners.delete(listener);
},
watchServerConfig: live(state.serverConfig ? { status: "live", config: state.serverConfig } : { status: "loading", config: null }),
watchServers: live({ status: "live", items: [] }),
watchAutomations: live({ status: "live", items: [] }),
@@ -85,7 +90,11 @@ export function fakeSession(
};
},
}) as unknown as T3Session;
return { session, calls };
/** Sends watchers of the thread a new projection, as the server does when it changes. */
const updateThread = (next: OrchestrationV2ThreadProjection) => {
for (const listener of threadListeners) listener({ status: "live", projection: next, history: { cursor: null, hasMore: false, loading: false } });
};
return { session, calls, updateThread };
}
/** Renders the whole app against a fake session, with helpers to drive it by key and mouse. */
+57
View File
@@ -0,0 +1,57 @@
import { afterEach, describe, expect, test } from "bun:test";
import { withReply } from "./fixtures.ts";
import { fakeSession, renderApp } from "./harness.tsx";
let app: Awaited<ReturnType<typeof renderApp>> | null = null;
const open = async (...args: Parameters<typeof renderApp>) => (app = await renderApp(...args));
afterEach(() => {
app?.destroy();
app = null;
});
const table = "| Name | Value |\n| --- | --- |\n| alpha | 1 |\n| beta | a longer cell |\n";
const paragraphs = (from: number, to: number) => Array.from({ length: to - from }, (_, index) => `Paragraph ${from + index} of the answer.\n\n`).join("");
/** The frame without its scrollbar column, which moves as the content grows. */
const content = (frame: string) => frame.split("\n").map((line) => line.slice(0, -3)).join("\n");
describe("streaming replies", () => {
test("tables render as tables, while streaming and once settled", async () => {
const fake = fakeSession({ projection: withReply(`Here:\n\n${table.slice(0, 40)}`, true) });
const app = await open({ session: fake });
expect(app.frame()).toContain("│ Name │ Value │");
fake.updateThread(withReply(`Here:\n\n${table}\nAfter.`, false));
await app.settle(50);
expect(app.frame()).toContain("│ beta │ a longer cell │");
expect(app.frame()).not.toContain("| --- |");
});
test("at the bottom, a long answer keeps its newest line in view", async () => {
const fake = fakeSession({ projection: withReply("", true) });
const app = await open({ session: fake, height: 30 });
for (let count = 10; count <= 60; count += 10) {
fake.updateThread(withReply(paragraphs(0, count), true));
await app.settle();
expect(app.frame()).toContain(`Paragraph ${count - 1} of the answer.`);
}
});
test("scrolled up, the text in view stays put as the answer grows and settles", async () => {
let text = paragraphs(0, 40);
const fake = fakeSession({ projection: withReply(text, true) });
const app = await open({ session: fake, height: 30 });
await app.mockMouse.scroll(60, 10, "up");
await app.mockMouse.scroll(60, 10, "up");
await app.settle(30);
const before = content(app.frame());
for (let more = 40; more < 50; more++) {
text += `Paragraph ${more} of the answer.\n\n`;
fake.updateThread(withReply(text, true));
await app.settle();
expect(content(app.frame())).toBe(before);
}
fake.updateThread(withReply(`${text}${table}`, false));
await app.settle(30);
expect(content(app.frame())).toBe(before);
});
});