Open History to see the last commit
1import { claimThreadOpen } from "@t3tools/client-runtime/state/thread-search";
2import { EnvironmentId, type ScopedThreadRef, ThreadId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import { Atom } from "effect/reactivity";
5import { expect, it, vi } from "vite-plus/test";
6
7import { fakeTimers, render } from "../../../test/render";
8import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry";
9
10const mocks = vi.hoisted(() => ({
11 visitRun: vi.fn(),
12}));
13
14const shellKey = (ref: ScopedThreadRef) => `${ref.environmentId}:${ref.threadId}`;
15
16vi.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
31import { ThreadVisitWatermark } from "./ThreadVisitWatermark";
32
33// Kept alive so a shell set before its tracker mounts is still there when it does.
34const shellAtom = Atom.family((_key: string) => Atom.make<unknown>(null).pipe(Atom.keepAlive));
35
36const NOW = Date.parse("2026-01-04T00:00:00.000Z");
37const ENVIRONMENT = EnvironmentId.make("environment-visit-watermark");
38const threadA: ScopedThreadRef = { environmentId: ENVIRONMENT, threadId: ThreadId.make("a") };
39const threadB: ScopedThreadRef = { environmentId: ENVIRONMENT, threadId: ThreadId.make("b") };
40
41function 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
51function visitedThreadIds() {
52 return mocks.visitRun.mock.calls.map(
53 ([, input]) => (input as { input: { threadId: string } }).input.threadId,
54 );
55}
56
57it("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 _focus = vi.spyOn(document, "hasFocus").mockReturnValue(true);
63 using _timers = fakeTimers();
64 vi.setSystemTime(NOW);
65 // A palette pick already sent B's open with its query, so B's visit is a plain watermark advance.
66 claimThreadOpen(threadB.environmentId, threadB.threadId, NOW);
67
68 await using screen = await render(
69 <AppAtomRegistryProvider>
70 <ThreadVisitWatermark threadRef={threadA} />
71 </AppAtomRegistryProvider>,
72 );
73 expect(visitedThreadIds()).toEqual(["a"]);
74
75 await screen.rerender(
76 <AppAtomRegistryProvider>
77 <ThreadVisitWatermark threadRef={threadB} />
78 </AppAtomRegistryProvider>,
79 );
80
81 // Thread A's throttle must not hold back B's first watermark: leaving B
82 // within the throttle window would otherwise keep it unread.
83 expect(visitedThreadIds()).toEqual(["a", "b"]);
84});
85
86it("does not mark a run that finishes in a background window as seen", async () => {
87 mocks.visitRun.mockReset();
88 mocks.visitRun.mockReturnValue(Effect.void);
89 const threadC: ScopedThreadRef = { environmentId: ENVIRONMENT, threadId: ThreadId.make("c") };
90 setShell(threadC, "2026-01-02T00:00:00.000Z");
91 let focused = true;
92 let visibility: DocumentVisibilityState = "visible";
93 using _focus = vi.spyOn(document, "hasFocus").mockImplementation(() => focused);
94 using _visibility = vi
95 .spyOn(document, "visibilityState", "get")
96 .mockImplementation(() => visibility);
97 using _timers = fakeTimers();
98 vi.setSystemTime(NOW);
99
100 await using _screen = await render(
101 <AppAtomRegistryProvider>
102 <ThreadVisitWatermark threadRef={threadC} />
103 </AppAtomRegistryProvider>,
104 );
105 expect(visitedThreadIds()).toEqual(["c"]);
106
107 // The user switches to another app while the run is still working.
108 focused = false;
109 visibility = "hidden";
110 window.dispatchEvent(new Event("blur"));
111 document.dispatchEvent(new Event("visibilitychange"));
112
113 const completedAt = "2026-01-03T00:00:00.000Z";
114 appAtomRegistry.set(shellAtom(shellKey(threadC)), {
115 environmentId: threadC.environmentId,
116 id: threadC.threadId,
117 updatedAt: completedAt,
118 lastVisitedAt: "2026-01-02T00:00:00.000Z",
119 latestRun: { completedAt },
120 });
121 vi.advanceTimersByTime(60_000);
122
123 expect(visitedThreadIds()).toEqual(["c"]);
124
125 focused = true;
126 visibility = "visible";
127 document.dispatchEvent(new Event("visibilitychange"));
128 window.dispatchEvent(new Event("focus"));
129
130 expect(visitedThreadIds()).toEqual(["c", "c"]);
131});