Open History to see the last commit
1import { createMemoryHistory } from "@tanstack/react-router";
2import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { MessageId, ThreadId } from "@t3tools/contracts";
4import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
5import {
6 serializeAssistantCitation,
7 withAssistantCitationComment,
8} from "@t3tools/shared/assistantCitations";
9import type * as Context from "effect/Context";
10import { expect, it, vi } from "vite-plus/test";
11import { page, userEvent } from "vite-plus/test/browser";
12
13import { AppRoot } from "~/AppRoot";
14// The route loads ChatView lazily; importing it here keeps its first transform out of the timeout.
15import "~/components/ChatView";
16import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
17import { __setClientSettingsForTests } from "~/hooks/useSettings";
18import { getRouter } from "~/router";
19import { appAtomRegistry } from "~/rpc/atomRegistry";
20import { environmentThreadShells } from "~/state/threads";
21import {
22 ENVIRONMENT_ID,
23 PROJECT_ID,
24 installFakeEnvironment,
25 threadWithHistory,
26 type FakeDriver,
27} from "../../test/fakeEnvironment";
28import { act, render } from "../../test/render";
29
30// The socket is the only fake layer.
31vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
32 const [actual, { fakeDriver }, Layer] = await Promise.all([
33 importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
34 import("../../test/fakeEnvironment"),
35 import("effect/Layer"),
36 ]);
37 return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
38});
39
40// Its own file: after other app tests, the shell never lists the second thread, so the switch would remount the view.
41it("an unsaved citation comment survives leaving the thread and coming back", async () => {
42 const cited = threadWithHistory("thread-citation-cited", 1);
43 const empty = threadWithHistory("thread-citation-empty", 1);
44 using _environment = installFakeEnvironment([cited, empty]);
45 __setClientSettingsForTests({
46 ...DEFAULT_CLIENT_SETTINGS,
47 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
48 });
49 const citation = {
50 version: 1 as const,
51 environmentId: ENVIRONMENT_ID,
52 threadId: cited.id,
53 messageId: MessageId.make("thread-citation-cited-assistant-0"),
54 text: "Answer 0",
55 start: 0,
56 end: 8,
57 prefix: "",
58 suffix: "",
59 };
60 const citedRef = scopeThreadRef(ENVIRONMENT_ID, cited.id);
61 useComposerDraftStore
62 .getState()
63 .setPrompt(citedRef, `See ${serializeAssistantCitation(citation)} `);
64 const router = getRouter(
65 createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${cited.id}`] }),
66 );
67 await router.load();
68 await using _app = await render(<AppRoot router={router} />, {
69 // AppRoot brings its own router.
70 router: false,
71 });
72 // Before the shell lists the next thread, its route remounts the view.
73 await vi.waitFor(() =>
74 expect(
75 appAtomRegistry.get(
76 environmentThreadShells.quietThreadShellAtom(scopeThreadRef(ENVIRONMENT_ID, empty.id)),
77 ),
78 ).not.toBeNull(),
79 );
80 const comment = page.getByLabelText("Comment on selected text");
81 await userEvent.click(page.getByRole("button", { name: "Add comment to citation" }));
82 await expect.element(comment).toHaveFocus();
83 await userEvent.keyboard("check this");
84
85 await act(() => router.navigate({ to: `/${ENVIRONMENT_ID}/${empty.id}` }));
86 await expect.element(page.getByText("Answer 0 for thread-citation-empty.")).toBeVisible();
87 await expect.element(page.getByText("Ask anything. @ files, $ skills, / commands")).toBeVisible();
88 await act(() => router.navigate({ to: `/${ENVIRONMENT_ID}/${cited.id}` }));
89
90 expect(useComposerDraftStore.getState().getComposerDraft(citedRef)?.prompt).toBe(
91 `See ${serializeAssistantCitation(withAssistantCitationComment(citation, "check this"))} `,
92 );
93 await userEvent.click(page.getByRole("button", { name: "Edit citation comment" }));
94 await expect.element(comment).toHaveValue("check this");
95});
96
97it("an unsaved citation comment survives going back to a new-thread draft", async () => {
98 const cited = threadWithHistory("thread-citation-remount", 1);
99 using _environment = installFakeEnvironment([cited]);
100 __setClientSettingsForTests({
101 ...DEFAULT_CLIENT_SETTINGS,
102 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
103 });
104 const citation = {
105 version: 1 as const,
106 environmentId: ENVIRONMENT_ID,
107 threadId: cited.id,
108 messageId: MessageId.make("thread-citation-remount-assistant-0"),
109 text: "Answer 0",
110 start: 0,
111 end: 8,
112 prefix: "",
113 suffix: "",
114 };
115 const citedRef = scopeThreadRef(ENVIRONMENT_ID, cited.id);
116 const drafts = useComposerDraftStore.getState();
117 drafts.setPrompt(citedRef, `See ${serializeAssistantCitation(citation)} `);
118 const draftId = DraftId.make("draft-citation-remount");
119 drafts.setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, {
120 threadId: ThreadId.make("thread-citation-remount-draft"),
121 });
122 const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] }));
123 await router.load();
124 await using _app = await render(<AppRoot router={router} />, {
125 // AppRoot brings its own router.
126 router: false,
127 });
128 await act(() => router.navigate({ to: `/${ENVIRONMENT_ID}/${cited.id}` }));
129 await expect.element(page.getByText("Answer 0 for thread-citation-remount.")).toBeVisible();
130 const comment = page.getByLabelText("Comment on selected text");
131 await userEvent.click(page.getByRole("button", { name: "Add comment to citation" }));
132 await expect.element(comment).toHaveFocus();
133 await userEvent.keyboard("check this");
134
135 // The draft route remounts the composer instead of switching the editor's draft.
136 await act(() => router.history.back());
137 await expect
138 .element(page.getByText("Answer 0 for thread-citation-remount."))
139 .not.toBeInTheDocument();
140 await act(() => router.history.forward());
141
142 expect(useComposerDraftStore.getState().getComposerDraft(citedRef)?.prompt).toBe(
143 `See ${serializeAssistantCitation(withAssistantCitationComment(citation, "check this"))} `,
144 );
145 await userEvent.click(page.getByRole("button", { name: "Edit citation comment" }));
146 await expect.element(comment).toHaveValue("check this");
147});