The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { createMemoryHistory } from "@tanstack/react-router"; |
| 2 | import { ProviderInstanceId } from "@t3tools/contracts"; |
| 3 | import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; |
| 4 | import type * as Context from "effect/Context"; |
| 5 | import { expect, it, vi } from "vite-plus/test"; |
| 6 | import { page, userEvent } from "vite-plus/test/browser"; |
| 7 | |
| 8 | import { AppRoot } from "~/AppRoot"; |
| 9 | // The route loads ChatView lazily, and its first transform (React Compiler over ~4k lines) |
| 10 | // takes 6-11s; importing it here keeps that out of the test's timeout. |
| 11 | import "~/components/ChatView"; |
| 12 | import { __setClientSettingsForTests } from "~/hooks/useSettings"; |
| 13 | import { getRouter } from "~/router"; |
| 14 | import { |
| 15 | ENVIRONMENT_ID, |
| 16 | dispatchedCommands, |
| 17 | installFakeEnvironment, |
| 18 | threadWithHistory, |
| 19 | type FakeDriver, |
| 20 | } from "../../test/fakeEnvironment"; |
| 21 | import { render } from "../../test/render"; |
| 22 | |
| 23 | // The socket is the only fake layer; see test/appScenarios.renders.tsx. |
| 24 | vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => { |
| 25 | const [actual, { fakeDriver }, Layer] = await Promise.all([ |
| 26 | importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(), |
| 27 | import("../../test/fakeEnvironment"), |
| 28 | import("effect/Layer"), |
| 29 | ]); |
| 30 | return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) }; |
| 31 | }); |
| 32 | |
| 33 | vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({ |
| 34 | ...(await original()), |
| 35 | trackRpcRequestSent: () => {}, |
| 36 | })); |
| 37 | |
| 38 | // Its own file: the app's shell subscription outlives a test, and it must list this thread. |
| 39 | it("a model picked for an existing thread reaches the server before any message is sent", async () => { |
| 40 | const stored = { |
| 41 | instanceId: ProviderInstanceId.make("codex"), |
| 42 | model: "gpt-5.4", |
| 43 | options: [{ id: "reasoningEffort", value: "high" }], |
| 44 | }; |
| 45 | const thread = { ...threadWithHistory("thread-model-pick", 1), modelSelection: stored }; |
| 46 | const aliasThread = { |
| 47 | ...threadWithHistory("thread-model-alias", 1), |
| 48 | modelSelection: { ...stored, model: "gpt-5-codex" }, |
| 49 | }; |
| 50 | using _environment = installFakeEnvironment([thread, aliasThread], { acceptCommands: true }); |
| 51 | __setClientSettingsForTests({ |
| 52 | ...DEFAULT_CLIENT_SETTINGS, |
| 53 | onboardingCompletedAt: "2026-01-01T00:00:00.000Z", |
| 54 | }); |
| 55 | const router = getRouter( |
| 56 | createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }), |
| 57 | ); |
| 58 | await router.load(); |
| 59 | await using _app = await render(<AppRoot router={router} />, { |
| 60 | // AppRoot brings its own router. |
| 61 | router: false, |
| 62 | }); |
| 63 | await expect.element(page.getByText("Answer 0 for thread-model-pick.")).toBeVisible(); |
| 64 | |
| 65 | const modelSets = () => |
| 66 | dispatchedCommands().filter((command) => command.type === "thread.model-selection.set"); |
| 67 | |
| 68 | await page.getByRole("button", { name: "GPT-5.4", exact: true }).click(); |
| 69 | await page.getByRole("option", { name: /GPT-5\.5/ }).click(); |
| 70 | await vi.waitFor(() => |
| 71 | expect(modelSets()).toEqual([ |
| 72 | expect.objectContaining({ |
| 73 | threadId: thread.id, |
| 74 | modelSelection: { instanceId: "codex", model: "gpt-5.5" }, |
| 75 | }), |
| 76 | ]), |
| 77 | ); |
| 78 | |
| 79 | // A thread stored under an alias keeps its options when its canonical model is picked. |
| 80 | await userEvent.keyboard("{Escape}"); |
| 81 | await router.navigate({ href: `/${ENVIRONMENT_ID}/${aliasThread.id}` }); |
| 82 | await expect.element(page.getByText("Answer 0 for thread-model-alias.")).toBeVisible(); |
| 83 | await page.getByRole("button", { name: "GPT-5.4", exact: true }).click(); |
| 84 | await page.getByRole("option", { name: /GPT-5\.4/ }).click(); |
| 85 | await vi.waitFor(() => |
| 86 | expect(modelSets().at(-1)).toEqual( |
| 87 | expect.objectContaining({ threadId: aliasThread.id, modelSelection: stored }), |
| 88 | ), |
| 89 | ); |
| 90 | }); |