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

vcsStatusRevalidation.test.ts

54 lines2.1 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5import { createVcsStatusRevalidationAtomFamily } from "./vcsStatusRevalidation.ts";
6​
7const environmentId = EnvironmentId.make("environment-1");
8​
9function setup(options: { readonly connected: boolean }) {
10 const connectedAtom = Atom.make(options.connected);
11 const run = vi.fn(async () => AsyncResult.success(null));
12 const family = createVcsStatusRevalidationAtomFamily({
13 refreshStatus: { label: "refresh-status", run },
14 connected: () => connectedAtom,
15 });
16 const registry = AtomRegistry.make();
17 return { registry, family, connectedAtom, run };
19​
20describe("createVcsStatusRevalidationAtomFamily", () => {
21 it("refreshes once per connection, shared by every subscriber, and again on reconnect", () => {
22 const { registry, family, connectedAtom, run } = setup({ connected: true });
23 const target = { environmentId, cwd: "/repo" };
24 const first = registry.mount(family(target));
25 const second = registry.mount(family({ environmentId, cwd: "/repo" }));
26 expect(run).toHaveBeenCalledTimes(1);
27 expect(run).toHaveBeenCalledWith(expect.anything(), { environmentId, input: { cwd: "/repo" } });
28​
29 registry.set(connectedAtom, false);
30 expect(run).toHaveBeenCalledTimes(1);
31 registry.set(connectedAtom, true);
32 expect(run).toHaveBeenCalledTimes(2);
33​
34 registry.mount(family({ environmentId, cwd: "/other" }))();
35 expect(run).toHaveBeenCalledTimes(3);
36 first();
37 second();
38 });
39​
40 it("waits for the connection before the first refresh", () => {
41 const { registry, family, connectedAtom, run } = setup({ connected: false });
42 const unmount = registry.mount(family({ environmentId, cwd: "/repo" }));
43 expect(run).not.toHaveBeenCalled();
44 registry.set(connectedAtom, true);
45 expect(run).toHaveBeenCalledTimes(1);
46 unmount();
47 });
48​
49 it("does nothing for an idle target", () => {
50 const { registry, family, run } = setup({ connected: true });
51 registry.mount(family(null))();
52 expect(run).not.toHaveBeenCalled();
53 });
54});