@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 94 lines 3.32 KB
Raw
1 import { EnvironmentId } from "@t3tools/contracts";
2 import * as Effect from "effect/Effect";
3 import { Atom, AtomRegistry } from "effect/reactivity";
4 import { describe, expect, it, vi } from "vite-plus/test";
5
6 import { createVcsStatusRevalidationAtomFamily } from "./vcsStatusRevalidation.ts";
7
8 const environmentId = EnvironmentId.make("environment-1");
9
10 function setup(options: { readonly connected?: boolean; readonly wake?: boolean }) {
11 const connectedAtom = Atom.make(options.connected ?? false);
12 const wakes = new Set<() => void>();
13 const run = vi.fn(() => Effect.succeed(null));
14 const family = createVcsStatusRevalidationAtomFamily({
15 refreshStatus: { label: "refresh-status", run },
16 ...(options.connected === undefined ? {} : { connected: () => connectedAtom }),
17 ...(options.wake
18 ? {
19 subscribeWake: (wake: () => void) => {
20 wakes.add(wake);
21 return () => wakes.delete(wake);
22 },
23 wakeDebounceMs: 250,
24 }
25 : {}),
26 });
27 const registry = AtomRegistry.make();
28 const wake = () => {
29 for (const listener of wakes) listener();
30 };
31 return { registry, family, connectedAtom, run, wakes, wake };
32 }
33
34 describe("createVcsStatusRevalidationAtomFamily", () => {
35 it("refreshes once per connection, shared by every subscriber, and again on reconnect", () => {
36 const { registry, family, connectedAtom, run } = setup({ connected: true });
37 const target = { environmentId, cwd: "/repo" };
38 const first = registry.mount(family(target));
39 const second = registry.mount(family({ environmentId, cwd: "/repo" }));
40 expect(run).toHaveBeenCalledTimes(1);
41 expect(run).toHaveBeenCalledWith(expect.anything(), { environmentId, input: { cwd: "/repo" } });
42
43 registry.set(connectedAtom, false);
44 expect(run).toHaveBeenCalledTimes(1);
45 registry.set(connectedAtom, true);
46 expect(run).toHaveBeenCalledTimes(2);
47
48 registry.mount(family({ environmentId, cwd: "/other" }))();
49 expect(run).toHaveBeenCalledTimes(3);
50 first();
51 second();
52 });
53
54 it("waits for the connection before the first refresh", () => {
55 const { registry, family, connectedAtom, run } = setup({ connected: false });
56 const unmount = registry.mount(family({ environmentId, cwd: "/repo" }));
57 expect(run).not.toHaveBeenCalled();
58 registry.set(connectedAtom, true);
59 expect(run).toHaveBeenCalledTimes(1);
60 unmount();
61 });
62
63 it("debounces wake signals and stops listening with the last subscriber", () => {
64 vi.useFakeTimers();
65 try {
66 const { registry, family, run, wakes, wake } = setup({ wake: true });
67 const unmount = registry.mount(family({ environmentId, cwd: "/repo" }));
68 expect(run).not.toHaveBeenCalled();
69
70 wake();
71 vi.advanceTimersByTime(100);
72 wake();
73 vi.advanceTimersByTime(249);
74 expect(run).not.toHaveBeenCalled();
75 vi.advanceTimersByTime(1);
76 expect(run).toHaveBeenCalledTimes(1);
77
78 wake();
79 unmount();
80 vi.runAllTimers();
81 expect(run).toHaveBeenCalledTimes(1);
82 expect(wakes.size).toBe(0);
83 } finally {
84 vi.useRealTimers();
85 }
86 });
87
88 it("does nothing for an idle target", () => {
89 const { registry, family, run, wakes } = setup({ connected: true, wake: true });
90 registry.mount(family(null))();
91 expect(run).not.toHaveBeenCalled();
92 expect(wakes.size).toBe(0);
93 });
94 });