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