Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ChatView.browser.test.tsx

93 lines3.9 KB

1import { createMemoryHistory } from "@tanstack/react-router";
2import { scopeProjectRef } 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 { __setClientSettingsForTests } from "~/hooks/useSettings";
15import { getRouter } from "~/router";
16import {
17 ENVIRONMENT_ID,
18 PROJECT_ID,
19 installFakeEnvironment,
20 threadWithHistory,
21 type FakeDriver,
22} from "../../test/fakeEnvironment";
23import { render } from "../../test/render";
24​
25// The socket is the only fake layer; see test/appScenarios.renders.tsx.
26vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
27 const [actual, { fakeDriver }, Layer] = await Promise.all([
28 importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
29 import("../../test/fakeEnvironment"),
30 import("effect/Layer"),
31 ]);
32 return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
33});
34​
35vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
36 ...(await original()),
37 trackRpcRequestSent: () => {},
38}));
39​
40it("a new draft does not load its reserved thread before the server lists it", async () => {
41 using _environment = installFakeEnvironment([]);
42 using fetchSpy = vi.spyOn(globalThis, "fetch");
43 __setClientSettingsForTests({
44 ...DEFAULT_CLIENT_SETTINGS,
45 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
46 });
47 const draftId = DraftId.make("draft-27443");
48 const threadId = ThreadId.make("thread-27443");
49 useComposerDraftStore
50 .getState()
51 .setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, { threadId });
52​
53 const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] }));
54 await router.load();
55 await using _app = await render(<AppRoot router={router} />, {
56 // AppRoot brings its own router.
57 router: false,
58 });
59 await expect.element(page.getByTestId("composer-editor")).toBeVisible();
60 // The shell snapshot has arrived once the sidebar lists the project, so the connection is up.
61 await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible();
62​
63 const threadRequests = fetchSpy.mock.calls
64 .map(([input]) => String(input instanceof Request ? input.url : input))
65 .filter((url) => url.includes(`/api/orchestration/threads/${threadId}/`));
66 // A 404 here marks the thread deleted, and the view never loads it once the send creates it.
67 expect(threadRequests).toEqual([]);
68});
69​
70it("opens the thread actions menu for an older settled thread the shell leaves out", async () => {
71 const thread = { ...threadWithHistory("older-settled", 1), olderSettled: true as const };
72 using _environment = installFakeEnvironment([thread]);
73 __setClientSettingsForTests({
74 ...DEFAULT_CLIENT_SETTINGS,
75 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
76 });
77​
78 const router = getRouter(
79 createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }),
80 );
81 await router.load();
82 await using _app = await render(<AppRoot router={router} />, {
83 // AppRoot brings its own router.
84 router: false,
85 });
86 await expect.element(page.getByText("older-settled: question 0")).toBeVisible();
87​
88 await page.getByRole("button", { name: `Thread actions for ${thread.title}` }).click();
89 // Without a bound the poll runs to the test deadline and reports a bare timeout.
90 await expect
91 .element(page.getByRole("menuitem", { name: "Rename thread" }), { timeout: 5_000 })
92 .toBeVisible();
93});