fix(web): make the web console work end-to-end
- Pair through the public POST /api/v1/pair and persist the device scopes; keep the allow-listed next deep link. Rename the manual field to pairing code. - Authenticate the WebSocket with the raw device token (strip Bearer) and recover from a 401 by clearing the connection once. - Fix chat payloads: setModel/setThinking, attention/diff responses, subagent dispatch items, queue ids/mode, task output and WS resume since. - Render exactly one assistant message per turn: one-to-one snapshot/history merge plus a stale-delta guard after message_end (fixes PONGONG). - Fix dialogs (content above the overlay), unknown-session deep links (error + retry, no silent fallback), device list/revoke, status identity and live pairing code, reconnect banner and Playwright selectors.
This commit is contained in:
1 parent
5572ac0d84
commit
0ef019f58d
57 files changed
+3549
-684
No files matched your search
@@ -1,10 +1,15 @@
|
||||
import { fireEvent, render, screen, within } from "@testing-library/react";
|
||||
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import type { ComponentProps } from "react";
|
||||
import { beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { setLanguage } from "@/lib/i18n";
|
||||
import { SessionTree } from "@/features/chat/session-tree";
|
||||
import { useChatStore } from "@/features/chat/store";
|
||||
import { ChatPage } from "@/pages/chat";
|
||||
import { useSessionsStore } from "@/stores/sessions";
|
||||
import { sessionFixture, subagentFixture } from "./chat-fixtures";
|
||||
import { mockApi } from "./helpers/api-mock";
|
||||
import { renderPage } from "./helpers/render-page";
|
||||
|
||||
beforeAll(async () => {
|
||||
await setLanguage("en");
|
||||
@@ -80,3 +85,198 @@ describe("session tree", () => {
|
||||
expect(onToggleAll).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Deep links: an unknown session must never borrow another session. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
const BOGUS = "ffffffffffffffffffffffff";
|
||||
const TS = 1_733_234_400_000;
|
||||
|
||||
/** The chat page opens a socket on mount; a silent socket keeps it inert. */
|
||||
function silentSocket(): void {
|
||||
class SilentSocket {
|
||||
readyState = 0;
|
||||
onopen: ((event: unknown) => void) | null = null;
|
||||
onmessage: ((event: { data: unknown }) => void) | null = null;
|
||||
onclose: ((event: { code?: number }) => void) | null = null;
|
||||
onerror: ((event: unknown) => void) | null = null;
|
||||
send(): void {}
|
||||
close(): void {
|
||||
this.readyState = 3;
|
||||
}
|
||||
}
|
||||
vi.stubGlobal("WebSocket", SilentSocket);
|
||||
}
|
||||
|
||||
function workspaceBody() {
|
||||
return [
|
||||
{
|
||||
workspaceId: "W1",
|
||||
cwd: "/work/w1",
|
||||
name: "W1",
|
||||
trusted: true,
|
||||
exists: true,
|
||||
sessionCount: 2,
|
||||
liveCount: 0,
|
||||
lastActiveAt: 0,
|
||||
instance: null,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function sessionsBody() {
|
||||
return {
|
||||
sessions: [
|
||||
{ sessionId: "s1", name: "Alpha", workspaceId: "W1", cwd: "/work/w1", status: "idle" },
|
||||
{ sessionId: "s2", name: "Beta", workspaceId: "W1", cwd: "/work/w1", status: "busy" },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
function entriesBody(text: string) {
|
||||
return {
|
||||
entries: [
|
||||
{
|
||||
type: "message",
|
||||
id: "u1",
|
||||
timestamp: new Date(TS).toISOString(),
|
||||
message: { role: "user", content: text, timestamp: TS },
|
||||
},
|
||||
],
|
||||
cursor: { hasMore: false },
|
||||
};
|
||||
}
|
||||
|
||||
function chatRoutes() {
|
||||
return [
|
||||
{ path: "/chat/$sessionId", component: ChatPage },
|
||||
{ path: "/chat", component: ChatPage },
|
||||
{ path: "/workspaces", component: () => <div>Workspaces destination</div> },
|
||||
];
|
||||
}
|
||||
|
||||
function streamPane(): HTMLElement {
|
||||
const pane = document.querySelector(".stream-pane");
|
||||
if (!pane) throw new Error("the chat layout did not render a stream pane");
|
||||
return pane as HTMLElement;
|
||||
}
|
||||
|
||||
function requestedUrls(fetchMock: ReturnType<typeof mockApi>): string[] {
|
||||
return fetchMock.mock.calls.map(([input]) => String(input));
|
||||
}
|
||||
|
||||
describe("chat session deep link", () => {
|
||||
beforeEach(() => {
|
||||
useSessionsStore.getState().reset();
|
||||
useChatStore.setState({ history: {}, sending: null, preview: null });
|
||||
silentSocket();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("shows an error + retry for an unknown session instead of another session", async () => {
|
||||
const fetchMock = mockApi([
|
||||
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
|
||||
{ path: "/api/v1/workspaces/W1/sessions", handler: () => ({ body: sessionsBody() }) },
|
||||
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
|
||||
{ path: /^\/api\/v1\/sessions\/s1\/entries/, handler: () => ({ body: entriesBody("Alpha first message") }) },
|
||||
]);
|
||||
|
||||
await renderPage(chatRoutes(), `/chat/${BOGUS}?workspace=W1`);
|
||||
|
||||
const error = await screen.findByTestId("session-not-found");
|
||||
expect(within(error).getByText("Session not found")).toBeInTheDocument();
|
||||
expect(within(error).getByRole("button", { name: /retry/i })).toBeInTheDocument();
|
||||
// A way back to the workspace list, so the dead end is not a dead end.
|
||||
expect(within(error).getByRole("link", { name: /back to workspaces/i })).toHaveAttribute(
|
||||
"href",
|
||||
"/workspaces",
|
||||
);
|
||||
|
||||
// The main area never borrows the first session of the workspace.
|
||||
expect(within(streamPane()).queryByText("Alpha")).toBeNull();
|
||||
expect(within(streamPane()).queryByText("Alpha first message")).toBeNull();
|
||||
expect(streamPane().querySelector(".stream-name")?.textContent ?? "").not.toContain("Alpha");
|
||||
// ... and no composer for a session that does not exist.
|
||||
expect(within(streamPane()).queryByLabelText("Message input")).toBeNull();
|
||||
// No tree row claims to be the current session either.
|
||||
expect(document.querySelector('[data-session-id][aria-current="true"]')).toBeNull();
|
||||
// The unknown id is never used to fetch entries.
|
||||
expect(requestedUrls(fetchMock).some((url) => url.includes(BOGUS))).toBe(false);
|
||||
|
||||
// Retry refetches the session list.
|
||||
const listCalls = () =>
|
||||
requestedUrls(fetchMock).filter((url) => url.includes("/api/v1/workspaces/W1/sessions")).length;
|
||||
const before = listCalls();
|
||||
fireEvent.click(within(error).getByTestId("session-not-found-retry"));
|
||||
await waitFor(() => expect(listCalls()).toBeGreaterThan(before));
|
||||
});
|
||||
|
||||
it("selects the deep-linked session and renders its own history", async () => {
|
||||
const fetchMock = mockApi([
|
||||
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
|
||||
{ path: "/api/v1/workspaces/W1/sessions", handler: () => ({ body: sessionsBody() }) },
|
||||
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
|
||||
{ path: /^\/api\/v1\/sessions\/s2\/entries/, handler: () => ({ body: entriesBody("Beta first message") }) },
|
||||
]);
|
||||
|
||||
await renderPage(chatRoutes(), "/chat/s2?workspace=W1");
|
||||
|
||||
await waitFor(() => expect(document.querySelector('[data-session-id="s2"]')).not.toBeNull());
|
||||
expect(document.querySelector('[data-session-id="s2"]')).toHaveAttribute("aria-current", "true");
|
||||
expect(document.querySelector('[data-session-id="s1"]')).not.toHaveAttribute("aria-current", "true");
|
||||
|
||||
expect(await screen.findByText("Beta first message")).toBeInTheDocument();
|
||||
expect(screen.queryByTestId("session-not-found")).toBeNull();
|
||||
expect(streamPane().querySelector(".stream-name")).toHaveTextContent("Beta");
|
||||
expect(requestedUrls(fetchMock)).toContainEqual(
|
||||
expect.stringContaining("/api/v1/sessions/s2/entries"),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps the first-session fallback for the /chat index", async () => {
|
||||
mockApi([
|
||||
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
|
||||
{ path: "/api/v1/workspaces/W1/sessions", handler: () => ({ body: sessionsBody() }) },
|
||||
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
|
||||
{ path: /^\/api\/v1\/sessions\/s1\/entries/, handler: () => ({ body: entriesBody("Alpha first message") }) },
|
||||
]);
|
||||
|
||||
await renderPage(chatRoutes(), "/chat?workspace=W1");
|
||||
|
||||
expect(await screen.findByText("Alpha first message")).toBeInTheDocument();
|
||||
expect(screen.queryByTestId("session-not-found")).toBeNull();
|
||||
expect(streamPane().querySelector(".stream-name")).toHaveTextContent("Alpha");
|
||||
});
|
||||
|
||||
it("does not flash the error state while the first session fetch is in flight", async () => {
|
||||
let release = () => {};
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
mockApi([
|
||||
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
|
||||
{
|
||||
path: "/api/v1/workspaces/W1/sessions",
|
||||
handler: async () => {
|
||||
await gate;
|
||||
return { body: sessionsBody() };
|
||||
},
|
||||
},
|
||||
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
|
||||
{ path: /^\/api\/v1\/sessions\/s1\/entries/, handler: () => ({ body: entriesBody("Alpha first message") }) },
|
||||
]);
|
||||
|
||||
await renderPage(chatRoutes(), `/chat/${BOGUS}?workspace=W1`);
|
||||
|
||||
// Still loading: no error surface and no borrowed session.
|
||||
expect(screen.queryByTestId("session-not-found")).toBeNull();
|
||||
expect(streamPane().querySelector(".stream-name")?.textContent ?? "").not.toContain("Alpha");
|
||||
|
||||
release();
|
||||
expect(await screen.findByTestId("session-not-found")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user