The best way to control Claude Code, Codex and more from any computer or phone

main
ts 144 lines 5.38 KB
Raw
1 /// <reference lib="dom" />
2 import type { EnvironmentId, ProjectSearchEntriesInput } from "@t3tools/contracts";
3 import { expect } from "e2e";
4 import type { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
5
6 import { test, WEB } from "./t3.ts";
7
8 const CITED = "cited-note.txt";
9 const PICKED = "picker-note.txt";
10
11 test(
12 "A slow citation leaves the newer file picker choice active",
13 WEB,
14 async ({ t3, app, browser, screen }) => {
15 const project = await t3.project("demo", {
16 [`notes/${CITED}`]: "Text from the earlier citation",
17 [PICKED]: "Text from the newer choice",
18 });
19 const thread = await t3.thread({
20 project,
21 title: "File preview order",
22 messages: [`See :codex-file-citation{path="${CITED}" line_range_start="1"}`],
23 });
24
25 await browser.addInitScript((citation) => {
26 const { promise: requested, resolve: markRequested } = Promise.withResolvers<void>();
27 const { promise: released, resolve: release } = Promise.withResolvers<void>();
28 const { promise: replied, resolve: markReplied } = Promise.withResolvers<void>();
29 Object.assign(window, {
30 citationRequested: requested,
31 releaseCitation: release,
32 citationReplied: replied,
33 });
34
35 // Delay only the citation's real search, so the file picker can search in the meantime.
36 const send = WebSocket.prototype.send;
37 WebSocket.prototype.send = function (data) {
38 const request =
39 typeof data === "string"
40 ? (JSON.parse(data) as {
41 _tag?: string;
42 id?: string;
43 tag?: string;
44 payload?: { query?: string };
45 })
46 : null;
47 if (
48 request?._tag !== "Request" ||
49 request.tag !== "projects.searchEntries" ||
50 request.payload?.query !== citation
51 ) {
52 return send.call(this, data);
53 }
54
55 const onReply = (event: MessageEvent) => {
56 if (typeof event.data !== "string") return;
57 const messages = [JSON.parse(event.data)].flat();
58 if (
59 !messages.some((message) => message._tag === "Exit" && message.requestId === request.id)
60 )
61 return;
62 this.removeEventListener("message", onReply);
63 markReplied();
64 };
65 this.addEventListener("message", onReply);
66 Object.assign(window, { citationInput: request.payload });
67 markRequested();
68 void released.then(() => send.call(this, data));
69 };
70 }, CITED);
71
72 const activeTab = browser.locator('[data-active-tab="true"]');
73 await t3.open(thread);
74 await screen.getByRole("button", /^Agent See/).tap();
75 await screen.getByRole("link", /^cited-note\.txt/).tap();
76 await browser.evaluate(async () => {
77 await (window as unknown as { citationRequested: Promise<void> }).citationRequested;
78 return null;
79 });
80
81 await browser.keyboard.press("Control+p");
82 await screen.getByPlaceholder("Search files…").fill(PICKED);
83 await screen.getByRole("option", PICKED, { exact: false }).tap();
84 await activeTab.getByText(PICKED, { exact: true }).waitFor();
85
86 await browser.evaluate(async () => {
87 const test = window as unknown as {
88 releaseCitation: () => void;
89 citationReplied: Promise<void>;
90 citationInput: ProjectSearchEntriesInput;
91 };
92 const registryModule = "/src/rpc/atomRegistry.ts";
93 const projectsModule = "/src/state/projects.ts";
94 const reactModule = performance
95 .getEntriesByType("resource")
96 .find(({ name }) => /\/\.vite\/deps\/react\.js(?:\?|$)/.test(name))?.name;
97 if (!reactModule) throw new Error("The app's React module has not loaded");
98 const [{ appAtomRegistry }, { projectEnvironment }, { default: react }] = await Promise.all([
99 import(registryModule) as Promise<{
100 appAtomRegistry: Pick<AtomRegistry.AtomRegistry, "subscribe">;
101 }>,
102 import(projectsModule) as Promise<{
103 projectEnvironment: {
104 searchEntries: (target: {
105 environmentId: EnvironmentId;
106 input: ProjectSearchEntriesInput;
107 }) => Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>>;
108 };
109 }>,
110 import(reactModule) as Promise<{
111 default: { act: (run: () => Promise<void>) => PromiseLike<void> };
112 }>,
113 ]);
114 const atom = projectEnvironment.searchEntries({
115 environmentId: location.pathname.split("/")[1] as EnvironmentId,
116 input: test.citationInput,
117 });
118 // Wait for the query used by the citation and flush React before reading the selected tab.
119 const settled = Promise.resolve(
120 react.act(async () => {
121 const { promise, resolve } = Promise.withResolvers<void>();
122 const unsubscribe = appAtomRegistry.subscribe(atom, (result) => {
123 if (result._tag !== "Initial" && !result.waiting) resolve();
124 });
125 try {
126 await promise;
127 } finally {
128 unsubscribe();
129 }
130 }),
131 );
132 test.releaseCitation();
133 await test.citationReplied;
134 await settled;
135 return null;
136 });
137 await app.screenshot("active-file-after-citation-search");
138 const activeFile = await browser.evaluate(
139 () =>
140 document.querySelector('[data-active-tab="true"] button:last-of-type')?.textContent ?? null,
141 );
142 expect(activeFile).toBe(PICKED);
143 },
144 );