Files
pi2d/web/tests/chat.migration.test.tsx
yuez 3789394c85 refactor(web): remove the dead right-panel and subagent-preview code
v0.1.1 replaced both surfaces: the session tray took the right panel's
todo/tasks, and the in-place `?sub=` child view took the subagent preview.

- delete `features/chat/side-panel.tsx` (nothing imported it since the tray
  landed)
- delete `SubagentModal` and its private helpers (`SUB_BADGE`, `SUB_DOT`,
  `SubagentEntry`, `parseSubagentLines`, `SUB_ROLES`); the child view builds its
  timeline from the parent's entries instead of `subagent.read`
- drop `chatCommands.readSubagent` (its only caller is gone)
- drop the deprecated store slice `PreviewTarget` / `preview` / `setPreview`,
  including the session-migration re-key and the `PanelTab` alias
- drop their exclusive CSS (`.sub-item*`, `.sub-preview*`, `.sub-steer`) and the
  6 orphan catalog keys (chat.subagentTitle, chat.preview, chat.previewNote,
  chat.task, chat.currentSubagents, chat.steerRequired)
- tests: remove the two SubagentModal cases and the preview/readSubagent
  expectations (295 tests, down from 297 — exactly the deleted cases)

Verification: typecheck clean, vitest 295/295, vite build green, visual matrix
24/24 clean.
2026-10-27 09:26:36 +08:00

83 lines
3.9 KiB
TypeScript

import { beforeEach, describe, expect, it } from "vitest";
import { sessionMigration } from "@/features/chat/data";
import { chatSessionPath, migrateSessionId, onSessionMigration, replaceChatUrl } from "@/features/chat/migration";
import { useChatStore } from "@/features/chat/store";
import { useSessionsStore } from "@/stores/sessions";
import { entryFixture, sessionFixture } from "./chat-fixtures";
beforeEach(() => {
useSessionsStore.getState().reset();
useChatStore.setState({ history: {}, sending: null });
window.history.replaceState(null, "", "/chat");
});
describe("session id migration (`tmp-N` → real id)", () => {
it("re-keys the session row, history, in-flight prompt and the URL", () => {
useSessionsStore.getState().setSessions("w1", [sessionFixture({ sessionId: "tmp-1", name: "New session" })]);
useChatStore.getState().receiveInitial("tmp-1", [entryFixture({ id: "u1", role: "user" })], {
before: "u1",
hasMore: true,
});
useChatStore.getState().setSending({ sessionId: "tmp-1", text: "hello" });
window.history.replaceState(null, "", "/chat/tmp-1?workspace=w1");
const seen: string[] = [];
const off = onSessionMigration((migration) => seen.push(`${migration.from}->${migration.to}`));
const migration = migrateSessionId("tmp-1", "01a1-real", "w1");
off();
expect(seen).toEqual(["tmp-1->01a1-real"]);
expect(migration?.path).toBe("/chat/01a1-real?workspace=w1");
// The tree row, the history page and the optimistic chip all followed.
expect(useSessionsStore.getState().listFor("w1").map((session) => session.sessionId)).toEqual([
"01a1-real",
]);
expect(useSessionsStore.getState().getSession("tmp-1")).toBeUndefined();
expect(useChatStore.getState().history["tmp-1"]).toBeUndefined();
expect(useChatStore.getState().history["01a1-real"]?.entries).toHaveLength(1);
expect(useChatStore.getState().history["01a1-real"]?.cursor).toEqual({ before: "u1", hasMore: true });
expect(useChatStore.getState().sending).toEqual({ sessionId: "01a1-real", text: "hello" });
expect(replaceChatUrl("tmp-1", "01a1-real", "w1")).toBe(true);
expect(window.location.pathname + window.location.search).toBe("/chat/01a1-real?workspace=w1");
});
it("leaves the URL alone when the route addressed another session", () => {
window.history.replaceState(null, "", "/chat/other?workspace=w1");
expect(replaceChatUrl("tmp-1", "01a1-real", "w1")).toBe(false);
expect(window.location.pathname).toBe("/chat/other");
expect(chatSessionPath("01a1-real")).toBe("/chat/01a1-real");
});
it("is a no-op for the same id and never invents a history slice", () => {
expect(migrateSessionId("s1", "s1", "w1")).toBeNull();
expect(useChatStore.getState().history["s1"]).toBeUndefined();
});
it("reads the migration announcement off the old-id `pi2:session` event", () => {
expect(
sessionMigration("tmp-1", {
type: "pi2:session",
session: { sessionId: "01a1-real", previousSessionId: "tmp-1" },
}),
).toEqual({ from: "tmp-1", to: "01a1-real" });
// A plain patch on the same id is not a migration.
expect(sessionMigration("s1", { type: "pi2:session", session: { sessionId: "s1" } })).toBeUndefined();
// A patch about a different session must not steal this subscription.
expect(
sessionMigration("tmp-1", {
type: "pi2:session",
session: { sessionId: "01a1-real", previousSessionId: "tmp-9" },
}),
).toBeUndefined();
// Only `pi2:session` announces migrations.
expect(
sessionMigration("tmp-1", { type: "pi2:todo", session: { sessionId: "01a1-real" } }),
).toBeUndefined();
});
});