Open History to see the last commit
1import { createMemoryHistory } from "@tanstack/react-router";
2import {
3 ProviderDriverKind,
4 ProviderInstanceId,
5 ProviderThreadId,
6 type OrchestrationV2ProviderThread,
7} from "@t3tools/contracts";
8import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
9import type * as Context from "effect/Context";
10import * as DateTime from "effect/DateTime";
11import { expect, it, vi } from "vite-plus/test";
12import { page, userEvent } from "vite-plus/test/browser";
13
14import { AppRoot } from "~/AppRoot";
15// The route loads ChatView lazily, and its first transform (React Compiler over ~4k lines)
16// takes 6-11s; importing it here keeps that out of the test's timeout.
17import "~/components/ChatView";
18import { __setClientSettingsForTests } from "~/hooks/useSettings";
19import { getRouter } from "~/router";
20import {
21 CLAUDE_PROVIDER,
22 CODEX_PROVIDER,
23 ENVIRONMENT_ID,
24 dispatchedCommands,
25 emitThreadEvent,
26 installFakeEnvironment,
27 threadWithHistory,
28 type FakeDriver,
29} from "../../test/fakeEnvironment";
30import { act, render } from "../../test/render";
31
32// The socket is the only fake layer.
33vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
34 const [actual, { fakeDriver }, Layer] = await Promise.all([
35 importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
36 import("../../test/fakeEnvironment"),
37 import("effect/Layer"),
38 ]);
39 return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
40});
41
42vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
43 ...(await original()),
44 trackRpcRequestSent: () => {},
45}));
46
47const CLAUDE = ProviderInstanceId.make("claudeAgent");
48const CLAUDE_DRIVER = ProviderDriverKind.make("claudeAgent");
49
50/** A Claude thread whose 250k-token context went cold long ago, with its native thread kept for handoff. */
51function coldClaudeThread(threadId = "thread-claude") {
52 const thread = threadWithHistory(threadId, 3, {
53 instanceId: CLAUDE,
54 model: "claude-opus-4-6",
55 });
56 const at = DateTime.makeUnsafe("2026-03-17T19:12:28.000Z");
57 const providerThread: OrchestrationV2ProviderThread = {
58 id: ProviderThreadId.make("provider-thread-claude"),
59 driver: CLAUDE_DRIVER,
60 providerInstanceId: CLAUDE,
61 providerSessionId: null,
62 appThreadId: thread.id,
63 ownerNodeId: null,
64 nativeThreadRef: { driver: CLAUDE_DRIVER, nativeId: "native-claude", strength: "strong" },
65 nativeConversationHeadRef: null,
66 status: "idle",
67 firstRunOrdinal: 1,
68 lastRunOrdinal: 3,
69 handoffIds: [],
70 forkedFrom: null,
71 pendingBackgroundTasks: [],
72 contextUsage: { usedTokens: 250_000, maxTokens: 1_000_000 },
73 nativeMetadata: null,
74 goal: null,
75 createdAt: at,
76 updatedAt: at,
77 };
78 return { ...thread, providerThreads: [providerThread] };
79}
80
81it("sends a Codex follow-up on a cold Claude thread without compacting first", async () => {
82 const thread = coldClaudeThread();
83 using _environment = installFakeEnvironment([thread], {
84 acceptCommands: true,
85 providers: [CLAUDE_PROVIDER, CODEX_PROVIDER],
86 });
87 __setClientSettingsForTests({
88 ...DEFAULT_CLIENT_SETTINGS,
89 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
90 });
91 const router = getRouter(
92 createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }),
93 );
94 await router.load();
95 await using _app = await render(<AppRoot router={router} />, {
96 // AppRoot brings its own router.
97 router: false,
98 });
99 await expect.element(page.getByText(`Answer 2 for ${thread.id}.`)).toBeVisible();
100 const compactAndSend = page.getByRole("button", { name: "Compact and send" });
101 await expect.element(compactAndSend).toBeVisible();
102
103 await page.getByRole("button", { name: "Claude Opus 4.6", exact: true }).click();
104 // The picker lists only the active provider's models until you search.
105 await userEvent.type(page.getByPlaceholder("Search models..."), "GPT-5.4");
106 await page.getByRole("option", { name: "GPT-5.4", exact: false }).click();
107 await expect.element(page.getByRole("button", { name: "GPT-5.4", exact: true })).toBeVisible();
108 await expect.element(compactAndSend).not.toBeInTheDocument();
109 // Picking a model leaves the composer's settings menu open.
110 await userEvent.keyboard("{Escape}");
111
112 const editor = page.getByTestId("composer-editor");
113 await userEvent.click(editor);
114 await userEvent.type(editor, "keep going");
115 await userEvent.keyboard("{Enter}");
116
117 const messages = () =>
118 dispatchedCommands().flatMap((command) =>
119 command.type === "message.dispatch"
120 ? [{ text: command.text, instanceId: command.modelSelection?.instanceId }]
121 : [],
122 );
123 const sent = { text: "keep going", instanceId: CODEX_PROVIDER.instanceId };
124 await vi.waitFor(() => expect(messages()).toContainEqual(sent));
125 expect(messages()).toEqual([sent]);
126});
127
128it("drops the compact offer on a cold Claude thread while /clear is armed", async () => {
129 // At phone width, dismissing the fresh-session notice collapses the composer and hides the chip.
130 const viewport = { width: window.innerWidth, height: window.innerHeight };
131 await page.viewport(1280, 800);
132 await using _viewport = {
133 [Symbol.asyncDispose]: () => page.viewport(viewport.width, viewport.height),
134 };
135 // The first test's model pick stays on thread-claude's draft, so this one uses its own thread.
136 const thread = coldClaudeThread("thread-claude-cleared");
137 using _environment = installFakeEnvironment([thread], { providers: [CLAUDE_PROVIDER] });
138 __setClientSettingsForTests({
139 ...DEFAULT_CLIENT_SETTINGS,
140 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
141 });
142 const router = getRouter(
143 createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }),
144 );
145 await router.load();
146 await using _app = await render(<AppRoot router={router} />, {
147 // AppRoot brings its own router.
148 router: false,
149 });
150 await expect.element(page.getByText(`Answer 2 for ${thread.id}.`)).toBeVisible();
151 const chip = page.getByRole("button", { name: "Compact 250k tokens of history before sending" });
152 const compactAndSend = page.getByRole("button", { name: "Compact and send" });
153 await expect.element(chip).toBeVisible();
154 await expect.element(compactAndSend).toBeVisible();
155
156 const editor = page.getByTestId("composer-editor");
157 await userEvent.click(editor);
158 await userEvent.type(editor, "/clear");
159 await userEvent.keyboard("{Enter}");
160 await expect.element(page.getByText("Your next message starts a fresh session")).toBeVisible();
161 await expect.element(chip).not.toBeInTheDocument();
162 await expect.element(compactAndSend).not.toBeInTheDocument();
163 await expect.element(page.getByRole("button", { name: "Submit message" })).toBeVisible();
164
165 await page.getByRole("button", { name: "Keep the current session" }).click();
166 await expect.element(chip).toBeVisible();
167 await expect.element(compactAndSend).toBeVisible();
168});
169
170it("/compact stays in the slash menu while a turn runs, since a typed command queues", async () => {
171 const history = threadWithHistory("compact-mid-turn", 2, {
172 instanceId: CLAUDE,
173 model: "claude-opus-4-6",
174 });
175 using _environment = installFakeEnvironment([history], { providers: [CLAUDE_PROVIDER] });
176 __setClientSettingsForTests({
177 ...DEFAULT_CLIENT_SETTINGS,
178 onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
179 });
180 const router = getRouter(
181 createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${history.id}`] }),
182 );
183 await router.load();
184 await using _app = await render(<AppRoot router={router} />, { router: false });
185 await expect.element(page.getByText(`Answer 1 for ${history.id}.`)).toBeVisible();
186 const compact = page.getByRole("option", { name: /^\/compact/ });
187
188 await userEvent.type(page.getByTestId("composer-editor"), "/compact");
189 await expect.element(compact).toBeVisible();
190
191 const running = { ...history.runs[1]!, status: "running" as const, completedAt: null };
192 await act(() => emitThreadEvent(history.id, { type: "run.updated", payload: running }));
193 await expect.element(page.getByRole("button", { name: "Steer message" })).toBeVisible();
194 await expect.element(compact).toBeVisible();
195});