Open History to see the last commit
1import { createMemoryHistory } from "@tanstack/react-router";
2import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { ThreadId } from "@t3tools/contracts";
4import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
5import type * as Context from "effect/Context";
6import { expect, it, vi } from "vite-plus/test";
7import { page } from "vite-plus/test/browser";
8
9import { AppRoot } from "~/AppRoot";
10// The route loads ChatView lazily, and its first transform (React Compiler over ~4k lines)
11// takes 6-11s; importing it here keeps that out of the test's timeout.
12import "~/components/ChatView";
13import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
14import { selectThreadDiffPanelSelection, useDiffPanelStore } from "~/diffPanelStore";
15import { __setClientSettingsForTests } from "~/hooks/useSettings";
16import { useRightPanelStore } from "~/rightPanelStore";
17import { getRouter } from "~/router";
18import {
19 ENVIRONMENT_ID,
20 PROJECT_ID,
21 installFakeEnvironment,
22 threadWithHistory,
23 type FakeDriver,
24} from "../../test/fakeEnvironment";
25import { act, render } from "../../test/render";
26
27// The socket is the only fake layer; see test/appScenarios.renders.tsx.
28vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
29 const [actual, { fakeDriver }, Layer] = await Promise.all([
30 importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
31 import("../../test/fakeEnvironment"),
32 import("effect/Layer"),
33 ]);
34 return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
35});
36
37vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
38 ...(await original()),
39 trackRpcRequestSent: () => {},
40}));
41
42it("a new draft does not load its reserved thread before the server lists it", async () => {
43 using _environment = installFakeEnvironment([]);
44 using fetchSpy = vi.spyOn(globalThis, "fetch");
45 __setClientSettingsForTests({
46 ...DEFAULT_CLIENT_SETTINGS,
47 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
48 });
49 const draftId = DraftId.make("draft-27443");
50 const threadId = ThreadId.make("thread-27443");
51 useComposerDraftStore
52 .getState()
53 .setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, { threadId });
54
55 const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] }));
56 await router.load();
57 await using _app = await render(<AppRoot router={router} />, {
58 // AppRoot brings its own router.
59 router: false,
60 });
61 await expect.element(page.getByTestId("composer-editor")).toBeVisible();
62 // The shell snapshot has arrived once the sidebar lists the project, so the connection is up.
63 await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible();
64
65 const threadRequests = fetchSpy.mock.calls
66 .map(([input]) => String(input instanceof Request ? input.url : input))
67 .filter((url) => url.includes(`/api/orchestration/threads/${threadId}/`));
68 // A 404 here marks the thread deleted, and the view never loads it once the send creates it.
69 expect(threadRequests).toEqual([]);
70});
71
72it("opens the thread actions menu for an older settled thread the shell leaves out", async () => {
73 const thread = { ...threadWithHistory("older-settled", 1), olderSettled: true as const };
74 using _environment = installFakeEnvironment([thread]);
75 __setClientSettingsForTests({
76 ...DEFAULT_CLIENT_SETTINGS,
77 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
78 });
79
80 const router = getRouter(
81 createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }),
82 );
83 await router.load();
84 await using _app = await render(<AppRoot router={router} />, {
85 // AppRoot brings its own router.
86 router: false,
87 });
88 await expect.element(page.getByText("older-settled: question 0")).toBeVisible();
89
90 await page.getByRole("button", { name: `Thread actions for ${thread.title}` }).click();
91 // Without a bound the poll runs to the test deadline and reports a bare timeout.
92 await expect
93 .element(page.getByRole("menuitem", { name: "Rename thread" }), { timeout: 5_000 })
94 .toBeVisible();
95});
96
97it("retains each thread's diff selection when returning from another thread or Settings", async () => {
98 const a = threadWithHistory("diff-selection-a", 1);
99 const b = threadWithHistory("diff-selection-b", 1);
100 using _environment = installFakeEnvironment([a, b]);
101 __setClientSettingsForTests({
102 ...DEFAULT_CLIENT_SETTINGS,
103 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
104 });
105 const aRef = scopeThreadRef(ENVIRONMENT_ID, a.id);
106 const bRef = scopeThreadRef(ENVIRONMENT_ID, b.id);
107 useRightPanelStore.getState().open(aRef, "diff");
108 useRightPanelStore.getState().open(bRef, "diff");
109 useDiffPanelStore.getState().selectBranchBaseRef(bRef, "origin/release");
110 const router = getRouter(createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${a.id}`] }));
111 await router.load();
112 await using _app = await render(<AppRoot router={router} />, {
113 // AppRoot brings its own router.
114 router: false,
115 });
116 await expect.element(page.getByText(`Answer 0 for ${a.id}.`)).toBeVisible();
117 await act(() => useDiffPanelStore.getState().selectGitScope(aRef, "unstaged"));
118
119 await act(() =>
120 router.navigate({
121 to: "/$environmentId/$threadId",
122 params: { environmentId: ENVIRONMENT_ID, threadId: b.id },
123 }),
124 );
125 await expect.element(page.getByText(`Answer 0 for ${b.id}.`)).toBeVisible();
126 expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, bRef)).toEqual({
127 kind: "branch",
128 baseRef: "origin/release",
129 });
130 await act(() =>
131 router.navigate({
132 to: "/$environmentId/$threadId",
133 params: { environmentId: ENVIRONMENT_ID, threadId: a.id },
134 }),
135 );
136 await expect.element(page.getByText(`Answer 0 for ${a.id}.`)).toBeVisible();
137 expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, aRef)).toEqual({
138 kind: "unstaged",
139 });
140
141 await act(() => useDiffPanelStore.getState().selectTurn(aRef, a.runs[0]!.id));
142 await act(() => router.navigate({ to: "/settings/general" }));
143 await expect
144 .element(page.getByRole("heading", { name: "Time format", exact: true }))
145 .toBeVisible();
146 await act(() =>
147 router.navigate({
148 to: "/$environmentId/$threadId",
149 params: { environmentId: ENVIRONMENT_ID, threadId: a.id },
150 }),
151 );
152 await expect.element(page.getByText(`Answer 0 for ${a.id}.`)).toBeVisible();
153 expect(
154 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, aRef),
155 ).toMatchObject({
156 kind: "turn",
157 turnId: a.runs[0]!.id,
158 });
159});