The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { refreshUsageLimits } from "@t3tools/client-runtime/state/usage"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import { AsyncResult, AtomRegistry } from "effect/reactivity"; |
| 5 | import { expect, it, vi } from "vite-plus/test"; |
| 6 | |
| 7 | type RefreshResult = AsyncResult.AsyncResult<void, string>; |
| 8 | |
| 9 | const state = vi.hoisted(() => ({ |
| 10 | refresh: (_environmentId: string): Promise<RefreshResult> => { |
| 11 | throw new Error("refresh not set up"); |
| 12 | }, |
| 13 | })); |
| 14 | vi.mock("../../state/server", async () => { |
| 15 | const Effect = await import("effect/Effect"); |
| 16 | return { |
| 17 | serverEnvironment: { |
| 18 | refreshProviders: { |
| 19 | label: "refresh providers", |
| 20 | run: (_registry: unknown, input: { readonly environmentId: string }) => |
| 21 | Effect.promise(() => state.refresh(input.environmentId)).pipe( |
| 22 | Effect.flatMap((result) => |
| 23 | result._tag === "Failure" ? Effect.failCause(result.cause) : Effect.void, |
| 24 | ), |
| 25 | ), |
| 26 | }, |
| 27 | }, |
| 28 | }; |
| 29 | }); |
| 30 | |
| 31 | import { |
| 32 | environmentIdsKey, |
| 33 | limitsRefreshFailuresAtom, |
| 34 | refreshEnvironmentLimits, |
| 35 | } from "./usageLimitsRefresh"; |
| 36 | |
| 37 | const success: RefreshResult = AsyncResult.success(undefined); |
| 38 | const failure: RefreshResult = AsyncResult.failure(Cause.fail("offline")); |
| 39 | |
| 40 | function setup(refresh: (environmentId: string) => Promise<RefreshResult>) { |
| 41 | state.refresh = refresh; |
| 42 | const registry = AtomRegistry.make(); |
| 43 | return { |
| 44 | registry, |
| 45 | failures: (ids: readonly EnvironmentId[]) => { |
| 46 | const atom = limitsRefreshFailuresAtom(environmentIdsKey(ids)); |
| 47 | registry.mount(atom); |
| 48 | return registry.get(atom); |
| 49 | }, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | it("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<RefreshResult>(); |
| 57 | const { registry, failures } = setup((environmentId) => |
| 58 | environmentId === a ? pending.promise : Promise.resolve(failure), |
| 59 | ); |
| 60 | failures([a, b]); |
| 61 | const first = refreshEnvironmentLimits(registry, a); |
| 62 | await refreshEnvironmentLimits(registry, b, { automatic: true }); |
| 63 | expect(failures([a, b])).toEqual([b]); |
| 64 | pending.resolve(success); |
| 65 | await first; |
| 66 | expect(failures([a, b])).toEqual([b]); |
| 67 | }); |
| 68 | |
| 69 | it("does not let an older refresh clear a newer failure for the same environment", async () => { |
| 70 | const a = EnvironmentId.make("mobile-limits-race-a"); |
| 71 | const b = EnvironmentId.make("mobile-limits-race-b"); |
| 72 | const aFirst = Promise.withResolvers<RefreshResult>(); |
| 73 | const bFirst = Promise.withResolvers<RefreshResult>(); |
| 74 | let aCalls = 0; |
| 75 | const { registry, failures } = setup((environmentId) => |
| 76 | environmentId === b |
| 77 | ? bFirst.promise |
| 78 | : ++aCalls === 1 |
| 79 | ? aFirst.promise |
| 80 | : Promise.resolve(failure), |
| 81 | ); |
| 82 | failures([a, b]); |
| 83 | const older = Promise.all([ |
| 84 | refreshEnvironmentLimits(registry, a, { automatic: true }), |
| 85 | refreshEnvironmentLimits(registry, b, { automatic: true }), |
| 86 | ]); |
| 87 | aFirst.resolve(success); |
| 88 | await refreshUsageLimits(a, () => aFirst.promise); |
| 89 | await refreshEnvironmentLimits(registry, a); |
| 90 | expect(failures([a])).toEqual([a]); |
| 91 | bFirst.resolve(success); |
| 92 | await older; |
| 93 | expect(failures([a, b])).toEqual([a]); |
| 94 | }); |
| 95 | |
| 96 | it("skips an automatic refresh inside the throttle window without touching the outcome", async () => { |
| 97 | const a = EnvironmentId.make("mobile-limits-throttle"); |
| 98 | const { registry, failures } = setup(() => Promise.resolve(failure)); |
| 99 | failures([a]); |
| 100 | await refreshEnvironmentLimits(registry, a); |
| 101 | expect(failures([a])).toEqual([a]); |
| 102 | state.refresh = () => Promise.resolve(success); |
| 103 | await refreshEnvironmentLimits(registry, a, { automatic: true }); |
| 104 | expect(failures([a])).toEqual([a]); |
| 105 | await refreshEnvironmentLimits(registry, a); |
| 106 | expect(failures([a])).toEqual([]); |
| 107 | }); |