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

ChatView.browser.test.tsx

67 lines2.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 type FakeDriver,
21} from "../../test/fakeEnvironment";
22import { render } from "../../test/render";
23​
24// The socket is the only fake layer; see test/appScenarios.renders.tsx.
25vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
26 const [actual, { fakeDriver }, Layer] = await Promise.all([
27 importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
28 import("../../test/fakeEnvironment"),
29 import("effect/Layer"),
30 ]);
31 return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
32});
33​
34vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
35 ...(await original()),
36 trackRpcRequestSent: () => {},
37}));
38​
39it("a new draft does not load its reserved thread before the server lists it", async () => {
40 using _environment = installFakeEnvironment([]);
41 using fetchSpy = vi.spyOn(globalThis, "fetch");
42 __setClientSettingsForTests({
43 ...DEFAULT_CLIENT_SETTINGS,
44 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
45 });
46 const draftId = DraftId.make("draft-27443");
47 const threadId = ThreadId.make("thread-27443");
48 useComposerDraftStore
49 .getState()
50 .setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, { threadId });
51​
52 const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] }));
53 await router.load();
54 await using _app = await render(<AppRoot router={router} />, {
55 // AppRoot brings its own router.
56 router: false,
57 });
58 await expect.element(page.getByTestId("composer-editor")).toBeVisible();
59 // The shell snapshot has arrived once the sidebar lists the project, so the connection is up.
60 await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible();
61​
62 const threadRequests = fetchSpy.mock.calls
63 .map(([input]) => String(input instanceof Request ? input.url : input))
64 .filter((url) => url.includes(`/api/orchestration/threads/${threadId}/`));
65 // A 404 here marks the thread deleted, and the view never loads it once the send creates it.
66 expect(threadRequests).toEqual([]);
67});