|
| 1 | +import { expect, it } from "@effect/vitest"; |
| 2 | +import { ProviderDriverKind } from "@t3tools/contracts"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Fiber from "effect/Fiber"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import * as TestClock from "effect/testing/TestClock"; |
| 7 | +import * as HttpClient from "effect/http/HttpClient"; |
| 8 | +import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 9 | +import { |
| 10 | + resolveLatestProviderVersion, |
| 11 | + type ProviderMaintenanceCapabilities, |
| 12 | +} from "./maintenanceResolver.ts"; |
| 13 | +import * as ProviderLatestVersions from "./ProviderLatestVersions.ts"; |
| 14 | + |
| 15 | +type Reply = { readonly status: number; readonly version?: string } | "hang"; |
| 16 | + |
| 17 | +const capabilities = (packageName: string): ProviderMaintenanceCapabilities => ({ |
| 18 | + provider: ProviderDriverKind.make("packageTool"), |
| 19 | + packageName, |
| 20 | + update: null, |
| 21 | +}); |
| 22 | +const toolA = capabilities("@example/tool-a"); |
| 23 | +const toolB = capabilities("@example/tool-b"); |
| 24 | + |
| 25 | +/** An npm registry stand-in that records which packages were requested. */ |
| 26 | +function makeRegistry() { |
| 27 | + const requests: Array<string> = []; |
| 28 | + const replies = new Map<string, Reply>(); |
| 29 | + const client = HttpClient.make((request) => { |
| 30 | + const packageName = decodeURIComponent(new URL(request.url).pathname.split("/")[1]!); |
| 31 | + requests.push(packageName); |
| 32 | + const reply = replies.get(packageName) ?? { status: 404 }; |
| 33 | + if (reply === "hang") return Effect.never; |
| 34 | + return Effect.succeed( |
| 35 | + HttpClientResponse.fromWeb( |
| 36 | + request, |
| 37 | + new Response(JSON.stringify({ version: reply.version }), { status: reply.status }), |
| 38 | + ), |
| 39 | + ); |
| 40 | + }); |
| 41 | + const layerTest = Layer.mergeAll( |
| 42 | + ProviderLatestVersions.layer, |
| 43 | + Layer.succeed(HttpClient.HttpClient, client), |
| 44 | + ); |
| 45 | + const requestCount = (packageName: string) => |
| 46 | + requests.filter((requested) => requested === packageName).length; |
| 47 | + return { layerTest, replies, requestCount }; |
| 48 | +} |
| 49 | + |
| 50 | +it.effect("caches a found version for an hour", () => { |
| 51 | + const registry = makeRegistry(); |
| 52 | + registry.replies.set("@example/tool-a", { status: 200, version: "2.0.0" }); |
| 53 | + return Effect.gen(function* () { |
| 54 | + expect(yield* resolveLatestProviderVersion(toolA)).toBe("2.0.0"); |
| 55 | + expect(registry.requestCount("@example/tool-a")).toBe(1); |
| 56 | + |
| 57 | + registry.replies.set("@example/tool-a", { status: 200, version: "2.1.0" }); |
| 58 | + yield* TestClock.adjust("59 minutes"); |
| 59 | + expect(yield* resolveLatestProviderVersion(toolA)).toBe("2.0.0"); |
| 60 | + expect(registry.requestCount("@example/tool-a")).toBe(1); |
| 61 | + |
| 62 | + yield* TestClock.adjust("2 minutes"); |
| 63 | + expect(yield* resolveLatestProviderVersion(toolA)).toBe("2.1.0"); |
| 64 | + expect(registry.requestCount("@example/tool-a")).toBe(2); |
| 65 | + }).pipe(Effect.provide(registry.layerTest)); |
| 66 | +}); |
| 67 | + |
| 68 | +it.effect("retries a non-2xx lookup after a minute and keeps the later success", () => { |
| 69 | + const registry = makeRegistry(); |
| 70 | + registry.replies.set("@example/tool-a", { status: 503 }); |
| 71 | + return Effect.gen(function* () { |
| 72 | + expect(yield* resolveLatestProviderVersion(toolA)).toBeNull(); |
| 73 | + expect(registry.requestCount("@example/tool-a")).toBe(1); |
| 74 | + |
| 75 | + registry.replies.set("@example/tool-a", { status: 200, version: "2.0.0" }); |
| 76 | + yield* TestClock.adjust("59 seconds"); |
| 77 | + expect(yield* resolveLatestProviderVersion(toolA)).toBeNull(); |
| 78 | + expect(registry.requestCount("@example/tool-a")).toBe(1); |
| 79 | + |
| 80 | + yield* TestClock.adjust("2 seconds"); |
| 81 | + expect(yield* resolveLatestProviderVersion(toolA)).toBe("2.0.0"); |
| 82 | + expect(registry.requestCount("@example/tool-a")).toBe(2); |
| 83 | + |
| 84 | + // The success replaced the miss, so it now holds for the hour. |
| 85 | + yield* TestClock.adjust("30 minutes"); |
| 86 | + expect(yield* resolveLatestProviderVersion(toolA)).toBe("2.0.0"); |
| 87 | + expect(registry.requestCount("@example/tool-a")).toBe(2); |
| 88 | + }).pipe(Effect.provide(registry.layerTest)); |
| 89 | +}); |
| 90 | + |
| 91 | +it.effect("retries a timed-out lookup a minute after it started", () => { |
| 92 | + const registry = makeRegistry(); |
| 93 | + registry.replies.set("@example/tool-a", "hang"); |
| 94 | + return Effect.gen(function* () { |
| 95 | + const lookup = yield* resolveLatestProviderVersion(toolA).pipe(Effect.forkChild); |
| 96 | + yield* TestClock.adjust("4 seconds"); |
| 97 | + expect(yield* Fiber.join(lookup)).toBeNull(); |
| 98 | + expect(registry.requestCount("@example/tool-a")).toBe(1); |
| 99 | + |
| 100 | + registry.replies.set("@example/tool-a", { status: 200, version: "2.0.0" }); |
| 101 | + yield* TestClock.adjust("55 seconds"); |
| 102 | + expect(yield* resolveLatestProviderVersion(toolA)).toBeNull(); |
| 103 | + expect(registry.requestCount("@example/tool-a")).toBe(1); |
| 104 | + |
| 105 | + yield* TestClock.adjust("2 seconds"); |
| 106 | + expect(yield* resolveLatestProviderVersion(toolA)).toBe("2.0.0"); |
| 107 | + expect(registry.requestCount("@example/tool-a")).toBe(2); |
| 108 | + }).pipe(Effect.provide(registry.layerTest)); |
| 109 | +}); |
| 110 | + |
| 111 | +it.effect("keeps a separate entry per package", () => { |
| 112 | + const registry = makeRegistry(); |
| 113 | + registry.replies.set("@example/tool-a", { status: 200, version: "2.0.0" }); |
| 114 | + registry.replies.set("@example/tool-b", { status: 503 }); |
| 115 | + return Effect.gen(function* () { |
| 116 | + expect(yield* resolveLatestProviderVersion(toolA)).toBe("2.0.0"); |
| 117 | + expect(yield* resolveLatestProviderVersion(toolB)).toBeNull(); |
| 118 | + |
| 119 | + registry.replies.set("@example/tool-b", { status: 200, version: "5.0.0" }); |
| 120 | + yield* TestClock.adjust("61 seconds"); |
| 121 | + expect(yield* resolveLatestProviderVersion(toolA)).toBe("2.0.0"); |
| 122 | + expect(yield* resolveLatestProviderVersion(toolB)).toBe("5.0.0"); |
| 123 | + expect(registry.requestCount("@example/tool-a")).toBe(1); |
| 124 | + expect(registry.requestCount("@example/tool-b")).toBe(2); |
| 125 | + }).pipe(Effect.provide(registry.layerTest)); |
| 126 | +}); |
0 commit comments