Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { beforeEach, expect, it, vi } from "vite-plus/test";
3
4const state = vi.hoisted(() => ({
5 values: [] as unknown[],
6 cursor: 0,
7 presentations: new Map(),
8 refreshProviders: vi.fn(),
9 autoRefresh: async () => {},
10 refreshingRef: { current: false },
11}));
12vi.mock("react", () => ({
13 useState: (initial: unknown) => {
14 const index = state.cursor++;
15 if (!(index in state.values)) {
16 state.values[index] = typeof initial === "function" ? initial() : initial;
17 }
18 return [
19 state.values[index],
20 (next: unknown) => {
21 state.values[index] = typeof next === "function" ? next(state.values[index]) : next;
22 },
23 ];
24 },
25 useRef: () => state.refreshingRef,
26 useEffect: () => {},
27 useEffectEvent: (callback: () => Promise<void>) => {
28 state.autoRefresh = callback;
29 return callback;
30 },
31}));
32vi.mock("@effect/atom-react", () => ({ useAtomValue: () => state.presentations }));
33vi.mock("react-native", () => ({ Alert: {}, Pressable: "button", View: "div" }));
34vi.mock("../../components/AppText", () => ({ AppText: "span" }));
35vi.mock("../../components/ProviderIcon", () => ({ ProviderIcon: () => null }));
36vi.mock("./usageProviders", () => ({ useProviderColors: () => ({}) }));
37vi.mock("../../state/presentation", () => ({
38 environmentPresentations: { presentationsAtom: null },
39}));
40vi.mock("../../state/server", () => ({ serverEnvironment: { refreshProviders: null } }));
41vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => state.refreshProviders }));
42
43import { useRefreshLimits } from "./UsageLimitsSection";
44import { refreshUsageLimits } from "@t3tools/client-runtime/state/usage";
45
46beforeEach(() => {
47 state.values = [];
48 state.cursor = 0;
49 state.refreshingRef.current = false;
50 state.refreshProviders.mockReset();
51});
52
53it("keeps a newer environment failure when an older refresh finishes", async () => {
54 const a = EnvironmentId.make("mobile-limits-a");
55 const b = EnvironmentId.make("mobile-limits-b");
56 const pending = Promise.withResolvers<{ _tag: string }>();
57 const read = () => {
58 state.cursor = 0;
59 return useRefreshLimits();
60 };
61 const presentation = (label: string) => ({
62 connection: { phase: "connected" },
63 entry: { target: { label } },
64 });
65 state.presentations = new Map([[a, presentation("A")]]);
66 state.refreshProviders.mockImplementation(({ environmentId }) =>
67 environmentId === a ? pending.promise : Promise.resolve({ _tag: "Failure" }),
68 );
69 const first = read().refresh();
70 state.presentations = new Map([
71 [a, presentation("A")],
72 [b, presentation("B")],
73 ]);
74 read();
75 await state.autoRefresh();
76 expect(read().failedLabels).toEqual(["B"]);
77 pending.resolve({ _tag: "Success" });
78 await first;
79 expect(read().failedLabels).toEqual(["B"]);
80});
81
82it("does not let an older multi-environment batch clear a newer failure for the same environment", async () => {
83 const a = EnvironmentId.make("mobile-limits-race-a");
84 const b = EnvironmentId.make("mobile-limits-race-b");
85 const aFirst = Promise.withResolvers<{ _tag: string }>();
86 const bFirst = Promise.withResolvers<{ _tag: string }>();
87 const read = (selected: ReadonlySet<EnvironmentId> | null = null) => {
88 state.cursor = 0;
89 return useRefreshLimits(selected);
90 };
91 const presentation = (label: string) => ({
92 connection: { phase: "connected" },
93 entry: { target: { label } },
94 });
95 state.presentations = new Map([
96 [a, presentation("A")],
97 [b, presentation("B")],
98 ]);
99 let aCalls = 0;
100 state.refreshProviders.mockImplementation(({ environmentId }) =>
101 environmentId === b
102 ? bFirst.promise
103 : ++aCalls === 1
104 ? aFirst.promise
105 : Promise.resolve({ _tag: "Failure" }),
106 );
107 read();
108 const older = state.autoRefresh();
109 aFirst.resolve({ _tag: "Success" });
110 await refreshUsageLimits(a, () => aFirst.promise);
111 const selected = new Set([a]);
112 await read(selected).refresh();
113 expect(read(selected).failedLabels).toEqual(["A"]);
114 bFirst.resolve({ _tag: "Success" });
115 await older;
116 expect(read(selected).failedLabels).toEqual(["A"]);
117});