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

threadVisits.test.ts

174 lines6.1 KB

1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
3import { expect, it, vi } from "vite-plus/test";
4​
5import type { VisitThreadInput } from "../operations/commands.ts";
6import { claimThreadOpen } from "./threadSearch.ts";
7import { createThreadVisitTracker, VISIT_DISPATCH_THROTTLE_MS } from "./threadVisits.ts";
8​
9const environmentId = EnvironmentId.make("env-visits");
10​
11interface Shell {
12 readonly environmentId: EnvironmentId;
13 readonly id: ThreadId;
14 readonly updatedAt: string;
15 readonly lastVisitedAt?: string | null;
16 readonly latestRun: { readonly completedAt: string | null } | null;
17 readonly wokeAt?: string | null;
19​
20function at(seconds: number) {
21 return `2026-01-01T00:00:${String(seconds).padStart(2, "0")}.000Z`;
23​
24// claimThreadOpen dedupes per thread in module state, so each test opens its own thread.
25function setup(threadName: string, initial: Omit<Shell, "environmentId" | "id"> | null) {
26 vi.useFakeTimers({ now: Date.UTC(2026, 0, 1) });
27 const threadId = ThreadId.make(threadName);
28 const ref = { environmentId, threadId };
29 const shellAtom = Atom.make<Shell | null>(
30 initial === null ? null : { environmentId, id: threadId, ...initial },
31 );
32 const visits: Array<VisitThreadInput> = [];
33 const localVisits: Array<string> = [];
34 const tracker = createThreadVisitTracker({
35 threadShellAtom: () => shellAtom,
36 visit: {
37 label: "test-visit",
38 run: async (_registry, value) => {
39 visits.push(value.input);
40 return AsyncResult.success(undefined);
41 },
42 },
43 markLocalVisit: (_ref, visitedAt) => localVisits.push(visitedAt),
44 });
45 const registry = AtomRegistry.make();
46 return {
47 ref,
48 visits,
49 localVisits,
50 mount: () => registry.mount(tracker(ref)),
51 update: (patch: Partial<Shell>) => {
52 const current = registry.get(shellAtom);
53 registry.set(shellAtom, {
54 environmentId,
55 id: threadId,
56 updatedAt: at(0),
57 latestRun: null,
58 ...current,
59 ...patch,
60 });
61 },
62 [Symbol.dispose]: () => vi.useRealTimers(),
63 };
65​
66it("records the open once, then throttles streaming visits", () => {
67 using t = setup("thread-throttle", {
68 updatedAt: at(10),
69 lastVisitedAt: at(0),
70 latestRun: null,
71 });
72 t.mount();
73 expect(t.visits).toEqual([{ threadId: t.ref.threadId, visitedAt: at(10), opened: true }]);
74​
75 vi.advanceTimersByTime(1_000);
76 t.update({ updatedAt: at(11) });
77 t.update({ updatedAt: at(12) });
78 expect(t.visits).toHaveLength(1);
79​
80 vi.advanceTimersByTime(VISIT_DISPATCH_THROTTLE_MS - 1_000);
81 expect(t.visits.slice(1)).toEqual([{ threadId: t.ref.threadId, visitedAt: at(12) }]);
82});
83​
84it("sends an unseen completion without waiting for the throttle", () => {
85 using t = setup("thread-completion", {
86 updatedAt: at(10),
87 lastVisitedAt: at(0),
88 latestRun: null,
89 });
90 t.mount();
91 t.update({ updatedAt: at(20), latestRun: { completedAt: at(20) } });
92 expect(t.visits.slice(1)).toEqual([{ threadId: t.ref.threadId, visitedAt: at(20) }]);
93});
94​
95it("does not resend the open a palette pick already sent", () => {
96 using t = setup("thread-palette", { updatedAt: at(10), lastVisitedAt: at(0), latestRun: null });
97 claimThreadOpen(environmentId, t.ref.threadId, Date.UTC(2026, 0, 1));
98 t.mount();
99 expect(t.visits).toEqual([{ threadId: t.ref.threadId, visitedAt: at(10) }]);
100});
101​
102it("keeps a mark-unread on the open thread until new activity lands", () => {
103 using t = setup("thread-unread", { updatedAt: at(10), lastVisitedAt: at(0), latestRun: null });
104 t.mount();
105 t.update({ lastVisitedAt: at(10) });
106 t.update({ lastVisitedAt: at(5) });
107 vi.advanceTimersByTime(VISIT_DISPATCH_THROTTLE_MS);
108 expect(t.visits).toHaveLength(1);
109​
110 t.update({ updatedAt: at(30) });
111 expect(t.visits.slice(1)).toEqual([{ threadId: t.ref.threadId, visitedAt: at(30) }]);
112});
113​
114it("keeps mark unread sticky after an expired wake is retired", () => {
115 using t = setup("thread-unread-retired-wake", {
116 updatedAt: at(10),
117 lastVisitedAt: at(0),
118 latestRun: { completedAt: at(9) },
119 wokeAt: at(15),
120 });
121 t.mount();
122 expect(t.visits).toEqual([{ threadId: t.ref.threadId, visitedAt: at(15), opened: true }]);
123 t.update({ lastVisitedAt: at(15) });
124 t.update({ lastVisitedAt: at(8), wokeAt: null });
125 vi.advanceTimersByTime(VISIT_DISPATCH_THROTTLE_MS);
126 expect(t.visits).toHaveLength(1);
127​
128 t.update({ updatedAt: at(30) });
129 expect(t.visits.slice(1)).toEqual([{ threadId: t.ref.threadId, visitedAt: at(30) }]);
130});
131​
132it("waits for the shell and records local visits for servers without tracking", () => {
133 using t = setup("thread-local", null);
134 t.mount();
135 expect(t.visits).toEqual([]);
136 t.update({ updatedAt: at(10), latestRun: null });
137 t.update({ updatedAt: at(11) });
138 expect(t.visits).toEqual([]);
139 expect(t.localVisits).toEqual([at(10), at(11)]);
140});
141​
142it("drops a pending visit when the thread is no longer viewed", () => {
143 using t = setup("thread-unmount", { updatedAt: at(10), lastVisitedAt: at(0), latestRun: null });
144 const unmount = t.mount();
145 t.update({ updatedAt: at(11) });
146 unmount();
147 vi.advanceTimersByTime(VISIT_DISPATCH_THROTTLE_MS);
148 expect(t.visits).toHaveLength(1);
149});
150​
151it("sends the open with the first visit when the shell arrives after the thread is shown", () => {
152 using t = setup("thread-shell-missing", null);
153 t.mount();
154 expect(claimThreadOpen(environmentId, t.ref.threadId, Date.UTC(2026, 0, 1))).toBe(false);
155 expect(t.visits).toEqual([]);
156​
157 t.update({ updatedAt: at(10), lastVisitedAt: at(0), latestRun: null });
158 expect(t.visits).toEqual([{ threadId: t.ref.threadId, visitedAt: at(10), opened: true }]);
159​
160 t.update({ updatedAt: at(10), lastVisitedAt: at(10) });
161 vi.advanceTimersByTime(VISIT_DISPATCH_THROTTLE_MS);
162 expect(t.visits).toHaveLength(1);
163});
164​
165it("advances the watermark to a wake without waiting for the throttle", () => {
166 using t = setup("thread-wake", { updatedAt: at(10), lastVisitedAt: at(0), latestRun: null });
167 t.mount();
168 t.update({ wokeAt: at(15) });
169 expect(t.visits.slice(1)).toEqual([{ threadId: t.ref.threadId, visitedAt: at(15) }]);
170​
171 vi.advanceTimersByTime(1_000);
172 t.update({ updatedAt: at(16) });
173 expect(t.visits).toHaveLength(2);
174});