Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadVisitWatermark.browser.test.tsx

82 lines2.9 KB

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