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

main
tsx 83 lines 2.94 KB
Raw
1 import { claimThreadOpen } from "@t3tools/client-runtime/state/thread-search";
2 import { EnvironmentId, type ScopedThreadRef, ThreadId } from "@t3tools/contracts";
3 import * as Effect from "effect/Effect";
4 import { Atom } from "effect/reactivity";
5 import { expect, it, vi } from "vite-plus/test";
6
7 import { fakeTimers, render } from "../../../test/render";
8 import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry";
9
10 const mocks = vi.hoisted(() => ({
11 visitRun: vi.fn(),
12 }));
13
14 const shellKey = (ref: ScopedThreadRef) => `${ref.environmentId}:${ref.threadId}`;
15
16 vi.mock(import("../../state/threads"), async (original) => {
17 const actual = await original();
18 return {
19 ...actual,
20 environmentThreadShells: {
21 ...actual.environmentThreadShells,
22 threadShellAtom: ((ref: ScopedThreadRef) => shellAtom(shellKey(ref))) as never,
23 },
24 threadEnvironment: {
25 ...actual.threadEnvironment,
26 visit: { label: "visit", run: mocks.visitRun } as never,
27 },
28 };
29 });
30
31 import { ThreadVisitWatermark } from "./ThreadVisitWatermark";
32
33 // Kept alive so a shell set before its tracker mounts is still there when it does.
34 const shellAtom = Atom.family((_key: string) => Atom.make<unknown>(null).pipe(Atom.keepAlive));
35
36 const NOW = Date.parse("2026-01-04T00:00:00.000Z");
37 const ENVIRONMENT = EnvironmentId.make("environment-visit-watermark");
38 const threadA: ScopedThreadRef = { environmentId: ENVIRONMENT, threadId: ThreadId.make("a") };
39 const threadB: ScopedThreadRef = { environmentId: ENVIRONMENT, threadId: ThreadId.make("b") };
40
41 function setShell(ref: ScopedThreadRef, updatedAt: string) {
42 appAtomRegistry.set(shellAtom(shellKey(ref)), {
43 environmentId: ref.environmentId,
44 id: ref.threadId,
45 updatedAt,
46 lastVisitedAt: "2026-01-01T00:00:00.000Z",
47 latestRun: null,
48 });
49 }
50
51 function visitedThreadIds() {
52 return mocks.visitRun.mock.calls.map(
53 ([, input]) => (input as { input: { threadId: string } }).input.threadId,
54 );
55 }
56
57 it("records the visit of the next thread at once after switching threads", async () => {
58 mocks.visitRun.mockReset();
59 mocks.visitRun.mockReturnValue(Effect.void);
60 setShell(threadA, "2026-01-02T00:00:00.000Z");
61 setShell(threadB, "2026-01-03T00:00:00.000Z");
62 using _timers = fakeTimers();
63 vi.setSystemTime(NOW);
64 // A palette pick already sent B's open with its query, so B's visit is a plain watermark advance.
65 claimThreadOpen(threadB.environmentId, threadB.threadId, NOW);
66
67 await using screen = await render(
68 <AppAtomRegistryProvider>
69 <ThreadVisitWatermark threadRef={threadA} />
70 </AppAtomRegistryProvider>,
71 );
72 expect(visitedThreadIds()).toEqual(["a"]);
73
74 await screen.rerender(
75 <AppAtomRegistryProvider>
76 <ThreadVisitWatermark threadRef={threadB} />
77 </AppAtomRegistryProvider>,
78 );
79
80 // Thread A's throttle must not hold back B's first watermark: leaving B
81 // within the throttle window would otherwise keep it unread.
82 expect(visitedThreadIds()).toEqual(["a", "b"]);
83 });