| 1 | import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import * as Cause from "effect/Cause"; |
| 3 | import { AsyncResult } from "effect/reactivity"; |
| 4 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 5 | |
| 6 | import { |
| 7 | createProviderCatalogRefreshRunner, |
| 8 | providerCatalogRefreshError, |
| 9 | } from "./provider-catalog-refresh"; |
| 10 | |
| 11 | describe("mobile provider catalog refresh", () => { |
| 12 | it("requests model discovery for the selected environment and deduplicates pending taps", async () => { |
| 13 | let resolveRefresh: ((value: "refreshed") => void) | undefined; |
| 14 | const refreshProviders = vi.fn( |
| 15 | () => |
| 16 | new Promise<"refreshed">((resolve) => { |
| 17 | resolveRefresh = resolve; |
| 18 | }), |
| 19 | ); |
| 20 | const refresh = createProviderCatalogRefreshRunner(refreshProviders); |
| 21 | const environmentId = EnvironmentId.make("environment-mobile"); |
| 22 | |
| 23 | const first = refresh(environmentId); |
| 24 | const second = refresh(environmentId); |
| 25 | |
| 26 | expect(second).toBe(first); |
| 27 | expect(refreshProviders).toHaveBeenCalledOnce(); |
| 28 | expect(refreshProviders).toHaveBeenCalledWith({ |
| 29 | environmentId, |
| 30 | input: { refreshModels: true }, |
| 31 | }); |
| 32 | |
| 33 | resolveRefresh?.("refreshed"); |
| 34 | await expect(first).resolves.toBe("refreshed"); |
| 35 | }); |
| 36 | |
| 37 | it("reports a discovery error and allows retry after the failed command settles", async () => { |
| 38 | const failure = AsyncResult.failure<never, Error>(Cause.fail(new Error("discovery failed"))); |
| 39 | const success = AsyncResult.success("refreshed"); |
| 40 | let callCount = 0; |
| 41 | const refreshProviders = vi.fn(async () => (callCount++ === 0 ? failure : success)); |
| 42 | const refresh = createProviderCatalogRefreshRunner(refreshProviders); |
| 43 | const environmentId = EnvironmentId.make("environment-mobile"); |
| 44 | |
| 45 | expect(providerCatalogRefreshError(await refresh(environmentId))).toBe("discovery failed"); |
| 46 | expect(providerCatalogRefreshError(await refresh(environmentId))).toBeNull(); |
| 47 | expect(refreshProviders).toHaveBeenCalledTimes(2); |
| 48 | }); |
| 49 | }); |