|
| 1 | +import { EnvironmentId, type ResourceTelemetrySnapshot } from "@t3tools/contracts"; |
| 2 | +import { AsyncResult } from "effect/reactivity"; |
| 3 | +import { useState } from "react"; |
| 4 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 5 | + |
| 6 | +import { act, render } from "../../test/render"; |
| 7 | + |
| 8 | +const environmentId = EnvironmentId.make("remote"); |
| 9 | + |
| 10 | +const monitor = vi.hoisted(() => ({ |
| 11 | + error: null as string | null, |
| 12 | + retry: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock(import("../state/environments"), async (original) => ({ |
| 16 | + ...(await original()), |
| 17 | + usePrimaryEnvironment: (() => ({ environmentId })) as never, |
| 18 | +})); |
| 19 | +vi.mock(import("../state/session"), async (original) => ({ |
| 20 | + ...(await original()), |
| 21 | + readEnvironmentScope: () => true, |
| 22 | + useEnvironmentScope: () => true, |
| 23 | +})); |
| 24 | +vi.mock(import("../state/server"), async (original) => { |
| 25 | + const actual = await original(); |
| 26 | + return { |
| 27 | + ...actual, |
| 28 | + serverEnvironment: { |
| 29 | + ...actual.serverEnvironment, |
| 30 | + resourceTelemetry: () => null, |
| 31 | + } as unknown as typeof actual.serverEnvironment, |
| 32 | + }; |
| 33 | +}); |
| 34 | +vi.mock(import("../state/use-atom-command"), async (original) => ({ |
| 35 | + ...(await original()), |
| 36 | + useAtomCommand: (() => monitor.retry) as never, |
| 37 | +})); |
| 38 | +vi.mock(import("../state/query"), async (original) => ({ |
| 39 | + ...(await original()), |
| 40 | + useEnvironmentQuery: (() => { |
| 41 | + const [error, setError] = useState(monitor.error); |
| 42 | + return { |
| 43 | + data: null, |
| 44 | + dataUpdatedAt: 0, |
| 45 | + error, |
| 46 | + failure: null, |
| 47 | + isPending: false, |
| 48 | + isSuccess: error === null, |
| 49 | + refresh: () => setError(monitor.error), |
| 50 | + }; |
| 51 | + }) as never, |
| 52 | +})); |
| 53 | + |
| 54 | +import { useResourceTelemetry, type ResourceTelemetryState } from "./resourceTelemetryState"; |
| 55 | + |
| 56 | +function Probe(props: { onResult: (state: ResourceTelemetryState) => void }) { |
| 57 | + props.onResult(useResourceTelemetry()); |
| 58 | + return null; |
| 59 | +} |
| 60 | + |
| 61 | +describe("useResourceTelemetry", () => { |
| 62 | + it("clears the stale error once a retry succeeds", async () => { |
| 63 | + monitor.error = "The resource monitor is not available."; |
| 64 | + monitor.retry.mockImplementation(async () => { |
| 65 | + monitor.error = null; |
| 66 | + return AsyncResult.success({ snapshot: {} as ResourceTelemetrySnapshot }); |
| 67 | + }); |
| 68 | + let state: ResourceTelemetryState | undefined; |
| 69 | + await using _screen = await render(<Probe onResult={(next) => (state = next)} />); |
| 70 | + expect(state!.error).toBe("The resource monitor is not available."); |
| 71 | + |
| 72 | + await act(async () => { |
| 73 | + await state!.retry(); |
| 74 | + }); |
| 75 | + |
| 76 | + expect(state!.error).toBeNull(); |
| 77 | + }); |
| 78 | +}); |
0 commit comments