Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import { Atom, AtomRegistry } from "effect/reactivity";
4import { describe, expect, it, vi } from "vite-plus/test";
5
6const fixture = vi.hoisted(() => ({
7 appState: "active",
8 appStateListeners: new Set<() => void>(),
9 refreshed: [] as string[],
10}));
11
12vi.mock("react-native", () => ({
13 AppState: {
14 get currentState() {
15 return fixture.appState;
16 },
17 addEventListener: (_event: string, listener: () => void) => {
18 fixture.appStateListeners.add(listener);
19 return { remove: () => fixture.appStateListeners.delete(listener) };
20 },
21 },
22 AccessibilityInfo: { addEventListener: vi.fn(), isReduceMotionEnabled: vi.fn() },
23}));
24
25vi.mock("../state/atom-registry", async () => {
26 const { AtomRegistry } = await import("effect/reactivity");
27 return { appAtomRegistry: AtomRegistry.make() };
28});
29
30vi.mock("../connection/catalog", async () => {
31 const { Atom } = await import("effect/reactivity");
32 return { environmentCatalog: { catalogValueAtom: Atom.make({ isReady: false }) } };
33});
34
35vi.mock("../state/presentation", async () => {
36 const { Atom } = await import("effect/reactivity");
37 return {
38 environmentPresentations: { presentationsAtom: Atom.make(new Map<string, unknown>()) },
39 };
40});
41
42vi.mock("../state/shell", async () => {
43 const { Atom } = await import("effect/reactivity");
44 const Option = await import("effect/Option");
45 return {
46 environmentShell: {
47 stateValueAtom: Atom.family((_id: string) =>
48 Atom.make({ status: "synchronizing", error: Option.none<string>() }),
49 ),
50 },
51 };
52});
53
54vi.mock("../state/server", () => ({
55 serverEnvironment: { refreshProviders: { label: "Refresh providers" } },
56}));
57
58vi.mock("@t3tools/client-runtime/state/runtime", () => ({
59 runAtomCommand: (
60 _registry: unknown,
61 _command: unknown,
62 input: { readonly environmentId: string; readonly input?: { readonly limitsOnly?: boolean } },
63 ) => {
64 expect(input.input).toEqual({ limitsOnly: true });
65 fixture.refreshed.push(input.environmentId);
66 return Promise.resolve();
67 },
68}));
69
70import { environmentCatalog } from "../connection/catalog";
71import { appAtomRegistry } from "../state/atom-registry";
72import { environmentPresentations } from "../state/presentation";
73import { environmentShell } from "../state/shell";
74import { installSubscriptionUsageRefresh } from "./useSubscriptionUsage";
75
76const registry = appAtomRegistry as AtomRegistry.AtomRegistry;
77const catalogAtom = environmentCatalog.catalogValueAtom as unknown as Atom.Writable<{
78 isReady: boolean;
79}>;
80const presentationsAtom = environmentPresentations.presentationsAtom as unknown as Atom.Writable<
81 Map<string, { connection: { phase: string } }>
82>;
83
84const shellAtom = (environmentId: EnvironmentId) =>
85 environmentShell.stateValueAtom(environmentId) as unknown as Atom.Writable<{
86 status: string;
87 error: Option.Option<string>;
88 }>;
89
90const connected = EnvironmentId.make("env-connected");
91const connecting = EnvironmentId.make("env-connecting");
92const syncing = EnvironmentId.make("env-syncing");
93const failed = EnvironmentId.make("env-failed");
94
95describe("installSubscriptionUsageRefresh", () => {
96 it("refreshes connected environments once the catalog is ready and the app is active", () => {
97 fixture.appState = "background";
98 fixture.refreshed = [];
99 registry.set(catalogAtom, { isReady: false });
100 registry.set(
101 presentationsAtom,
102 new Map([
103 [connected, { connection: { phase: "connected" } }],
104 [connecting, { connection: { phase: "connecting" } }],
105 [syncing, { connection: { phase: "connected" } }],
106 [failed, { connection: { phase: "connected" } }],
107 ]),
108 );
109 registry.set(shellAtom(connected), { status: "live", error: Option.none() });
110 // A shell that failed never goes live; its quotas still refresh.
111 registry.set(shellAtom(failed), { status: "cached", error: Option.some("Shell failed") });
112
113 const dispose = installSubscriptionUsageRefresh();
114 registry.set(catalogAtom, { isReady: true });
115 // Backgrounded: nothing refreshes yet.
116 expect(fixture.refreshed).toEqual([]);
117
118 fixture.appState = "active";
119 for (const listener of fixture.appStateListeners) listener();
120 expect(fixture.refreshed).toEqual([connected, failed]);
121
122 // The refresh waits for the thread list, so it never competes with it on a cold start.
123 registry.set(shellAtom(syncing), { status: "live", error: Option.none() });
124 expect(fixture.refreshed).toEqual([connected, failed, syncing]);
125
126 dispose();
127 expect(fixture.appStateListeners.size).toBe(0);
128 });
129});