* feat(webui): inspect file context in a live graph sidebar * refactor(webui): place preview controls in the shared sidebar header * fix(webui): synchronize npm lockfile for graph dependencies * fix(webui): preserve compacted context and refine graph inspection * fix(context): harden provenance, request usage, and drawer lifecycle * fix(webui): align source skeleton and browser fixtures * perf(context): reuse source analysis and capture request graphs once * perf(webui): compact graph history and render graph motion on canvas * fix(webui): remove redundant context sidebar header * style(webui): clarify context replay icon * perf(webui): pace context replay on an adaptive timeline * feat(webui): briefly reveal labels for new context nodes * fix(webui): reveal new labels only for loaded context files * fix(webui): restart graph animation clock after idle * fix(webui): reveal labels when replay reverses node exit * refactor(context): project file-use facts without retention tracking * refactor(context): observe file reads outside agent execution * fix(webui): animate context sidebar closing * refactor(context): remove unused graph and usage panel code * refactor(context): isolate prompt observation from execution metadata * fix(webui): clarify cache rate and show context token usage * fix(webui): abbreviate context token counts in thousands * fix(webui): omit token unit from context usage details * fix(webui): restore large source preview highlighting and panel elevation * fix(webui): distinguish modifier scrolling from keyboard focus * fix(webui): align preview tab and panel corner radii * fix(webui): attach file tabs to the header divider * fix(webui): use one action to toggle file preview mode * fix(webui): use hover hint for context replay * fix(webui): share composer layout between graph and file sidebars * fix(webui): align sidebar bottom with composer surface * fix(webui): restore mobile graph drawing and touch file details * test(webui): align legacy cache assertions with current label * perf(context): retain source analysis and bound graph animation work * style(webui): add light elevation to user messages * test(webui): await lazy graph scene before replay assertions * fix(webui): align preview skeleton and browser contracts
142 lines
6.8 KiB
TypeScript
142 lines
6.8 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { fetchLatestWebuiThread } from "@/lib/latest-thread-request";
|
|
import { NanobotClient } from "@/lib/nanobot-client";
|
|
import { webuiThreadCache } from "@/lib/webui-thread-cache";
|
|
import type { WebuiThreadPersistedPayload } from "@/lib/types";
|
|
|
|
const KEY = "websocket:shared";
|
|
|
|
function connectedClient(url: string): NanobotClient {
|
|
const socket = { readyState: 1, onopen: null as (() => void) | null, send() {}, close() {} };
|
|
const client = new NanobotClient({ url, reconnect: false, socketFactory: () => socket as unknown as WebSocket });
|
|
client.connect();
|
|
socket.onopen?.();
|
|
return client;
|
|
}
|
|
|
|
function cachedThread(revision = "cached"): WebuiThreadPersistedPayload {
|
|
return { schemaVersion: 1, projection: "events", events: [], revision };
|
|
}
|
|
|
|
function deferredResponse() {
|
|
let resolve!: (response: Response) => void;
|
|
const promise = new Promise<Response>((finish) => { resolve = finish; });
|
|
return { promise, resolve };
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
webuiThreadCache.clear();
|
|
});
|
|
|
|
describe("shared latest thread requests", () => {
|
|
it("shares a conditional read and lets one consumer cancel while another receives the owning 304 body", async () => {
|
|
const client = connectedClient("ws://host");
|
|
const body = cachedThread();
|
|
webuiThreadCache.set(KEY, body);
|
|
const read = deferredResponse();
|
|
const fetchMock = vi.fn().mockReturnValue(read.promise);
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
const mounted = new AbortController();
|
|
const first = fetchLatestWebuiThread(client, "token", KEY, mounted.signal);
|
|
const surviving = fetchLatestWebuiThread(client, "token", KEY);
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
"/api/sessions/websocket%3Ashared/webui-thread?limit=40&direction=latest&include_model_requests=1&context_graph_encoding=references-v1",
|
|
expect.objectContaining({ headers: { Authorization: "Bearer token", "If-None-Match": '"cached"' } }),
|
|
);
|
|
const rejected = expect(first).rejects.toMatchObject({ name: "AbortError" });
|
|
mounted.abort();
|
|
await rejected;
|
|
expect(fetchMock.mock.calls[0][1].signal.aborted).toBe(false);
|
|
read.resolve(new Response(null, { status: 304 }));
|
|
await expect(surviving).resolves.toBe(body);
|
|
});
|
|
|
|
it("aborts the network after its final consumer cancels and starts a new read for recovery", async () => {
|
|
const client = connectedClient("ws://host");
|
|
const reads = [deferredResponse(), deferredResponse()];
|
|
const fetchMock = vi.fn().mockReturnValueOnce(reads[0].promise).mockReturnValueOnce(reads[1].promise);
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
const controller = new AbortController();
|
|
const cancelled = fetchLatestWebuiThread(client, "token", KEY, controller.signal);
|
|
const rejected = expect(cancelled).rejects.toMatchObject({ name: "AbortError" });
|
|
controller.abort();
|
|
await rejected;
|
|
expect(fetchMock.mock.calls[0][1].signal.aborted).toBe(true);
|
|
const recovered = fetchLatestWebuiThread(client, "token", KEY);
|
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
const body = cachedThread("new");
|
|
for (const read of reads) read.resolve(new Response(JSON.stringify(body), { status: 200 }));
|
|
await expect(recovered).resolves.toEqual(body);
|
|
});
|
|
|
|
it.each(["thread update", "turn end"] as const)("replaces surviving reads only for their own chat's %s", async (invalidation) => {
|
|
const socket = {
|
|
readyState: 1, onopen: null as (() => void) | null,
|
|
onmessage: null as ((event: MessageEvent) => void) | null, send() {}, close() {},
|
|
};
|
|
const client = new NanobotClient({ url: "ws://host", reconnect: false, socketFactory: () => socket as unknown as WebSocket });
|
|
client.connect();
|
|
socket.onopen?.();
|
|
const reads = [deferredResponse(), deferredResponse()];
|
|
const fetchMock = vi.fn().mockReturnValueOnce(reads[0].promise).mockReturnValueOnce(reads[1].promise);
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
const first = fetchLatestWebuiThread(client, "token", KEY);
|
|
const surviving = fetchLatestWebuiThread(client, "token", KEY);
|
|
const receive = (chatId: string, scope: string) => socket.onmessage?.({
|
|
data: JSON.stringify({ event: "session_updated", chat_id: chatId, scope }),
|
|
} as MessageEvent);
|
|
receive("shared", "metadata");
|
|
receive("another-chat", "thread");
|
|
expect(fetchMock.mock.calls[0][1].signal.aborted).toBe(false);
|
|
if (invalidation === "thread update") receive("shared", "thread");
|
|
else socket.onmessage?.({
|
|
data: JSON.stringify({ event: "turn_end", chat_id: "shared", turn_id: "turn-shared" }),
|
|
} as MessageEvent);
|
|
const fresh = fetchLatestWebuiThread(client, "token", KEY);
|
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
const body = cachedThread("complete");
|
|
reads[0].resolve(new Response(JSON.stringify(cachedThread("stale")), { status: 200 }));
|
|
reads[1].resolve(new Response(JSON.stringify(body), { status: 200 }));
|
|
await Promise.all([first, surviving, fresh].map((read) => expect(read).resolves.toEqual(body)));
|
|
});
|
|
|
|
it("keeps host clients and renewed credentials in separate reads", async () => {
|
|
const firstHost = connectedClient("ws://first-host");
|
|
const secondHost = connectedClient("ws://second-host");
|
|
const reads = [deferredResponse(), deferredResponse(), deferredResponse()];
|
|
const fetchMock = vi.fn()
|
|
.mockReturnValueOnce(reads[0].promise)
|
|
.mockReturnValueOnce(reads[1].promise)
|
|
.mockReturnValueOnce(reads[2].promise);
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
const requests = [
|
|
fetchLatestWebuiThread(firstHost, "old-token", KEY),
|
|
fetchLatestWebuiThread(firstHost, "new-token", KEY),
|
|
fetchLatestWebuiThread(secondHost, "old-token", KEY),
|
|
];
|
|
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
for (const read of reads) read.resolve(new Response(JSON.stringify(cachedThread()), { status: 200 }));
|
|
await Promise.all(requests);
|
|
});
|
|
|
|
it("does not join a pending read after a newer canonical page changes its cached baseline", async () => {
|
|
const client = connectedClient("ws://host");
|
|
const oldBody = cachedThread("old");
|
|
const newBody = cachedThread("new");
|
|
webuiThreadCache.set(KEY, oldBody);
|
|
const reads = [deferredResponse(), deferredResponse()];
|
|
const fetchMock = vi.fn().mockReturnValueOnce(reads[0].promise).mockReturnValueOnce(reads[1].promise);
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
const oldRead = fetchLatestWebuiThread(client, "token", KEY);
|
|
webuiThreadCache.set(KEY, newBody);
|
|
const newRead = fetchLatestWebuiThread(client, "token", KEY);
|
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
for (const read of reads) read.resolve(new Response(null, { status: 304 }));
|
|
await expect(oldRead).resolves.toBe(oldBody);
|
|
await expect(newRead).resolves.toBe(newBody);
|
|
});
|
|
});
|