|
| 1 | +import { describe, it, beforeEach, afterEach, mock } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { mkdtempSync, writeFileSync, rmSync } from "node:fs"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { tmpdir } from "node:os"; |
| 6 | + |
| 7 | +const originalFetch = globalThis.fetch; |
| 8 | +let tempDir: string; |
| 9 | + |
| 10 | +beforeEach(() => { |
| 11 | + tempDir = mkdtempSync(join(tmpdir(), "run402-paid-fetch-test-")); |
| 12 | + process.env.RUN402_CONFIG_DIR = tempDir; |
| 13 | + process.env.RUN402_API_BASE = "https://test-api.run402.com"; |
| 14 | +}); |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + globalThis.fetch = originalFetch; |
| 18 | + rmSync(tempDir, { recursive: true, force: true }); |
| 19 | + delete process.env.RUN402_CONFIG_DIR; |
| 20 | + delete process.env.RUN402_API_BASE; |
| 21 | +}); |
| 22 | + |
| 23 | +describe("setupPaidFetch", () => { |
| 24 | + it("returns null when no allowance file exists", async () => { |
| 25 | + const { setupPaidFetch, _resetPaidFetchCache } = await import(`./paid-fetch.js?t=${Date.now()}`); |
| 26 | + _resetPaidFetchCache(); |
| 27 | + const result = await setupPaidFetch(); |
| 28 | + assert.equal(result, null); |
| 29 | + }); |
| 30 | + |
| 31 | + it("returns null when allowance exists but payment libs fail to import", async () => { |
| 32 | + // Write an allowance file so readAllowance succeeds |
| 33 | + writeFileSync( |
| 34 | + join(tempDir, "allowance.json"), |
| 35 | + JSON.stringify({ |
| 36 | + address: "0x1234567890abcdef1234567890abcdef12345678", |
| 37 | + privateKey: "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", |
| 38 | + created: "2026-01-01T00:00:00Z", |
| 39 | + funded: true, |
| 40 | + rail: "x402", |
| 41 | + }), |
| 42 | + ); |
| 43 | + |
| 44 | + // The test environment doesn't have @x402/fetch etc. installed as real modules, |
| 45 | + // so setupPaidFetch should catch the import error and return null |
| 46 | + const { setupPaidFetch, _resetPaidFetchCache } = await import(`./paid-fetch.js?t=${Date.now()}`); |
| 47 | + _resetPaidFetchCache(); |
| 48 | + const result = await setupPaidFetch(); |
| 49 | + // May be null (import fails) or a function (if libs are available in dev) |
| 50 | + assert.ok(result === null || typeof result === "function"); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe("paidApiRequest", () => { |
| 55 | + it("falls back to bare apiRequest when no allowance", async () => { |
| 56 | + // No allowance file → paidApiRequest should just call apiRequest |
| 57 | + globalThis.fetch = (async () => |
| 58 | + new Response(JSON.stringify({ ok: true }), { |
| 59 | + status: 200, |
| 60 | + headers: { "Content-Type": "application/json" }, |
| 61 | + })) as typeof fetch; |
| 62 | + |
| 63 | + const { paidApiRequest, _resetPaidFetchCache } = await import(`./paid-fetch.js?t=${Date.now()}`); |
| 64 | + _resetPaidFetchCache(); |
| 65 | + const result = await paidApiRequest("/test"); |
| 66 | + assert.equal(result.ok, true); |
| 67 | + assert.equal(result.status, 200); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns is402 when no paid fetch and server returns 402", async () => { |
| 71 | + // No allowance → no paid fetch → 402 passes through |
| 72 | + globalThis.fetch = (async () => |
| 73 | + new Response( |
| 74 | + JSON.stringify({ x402: { price: "$0.10" } }), |
| 75 | + { status: 402, headers: { "Content-Type": "application/json" } }, |
| 76 | + )) as typeof fetch; |
| 77 | + |
| 78 | + const { paidApiRequest, _resetPaidFetchCache } = await import(`./paid-fetch.js?t=${Date.now()}`); |
| 79 | + _resetPaidFetchCache(); |
| 80 | + const result = await paidApiRequest("/test"); |
| 81 | + assert.equal(result.ok, false); |
| 82 | + assert.equal(result.is402, true); |
| 83 | + assert.equal(result.status, 402); |
| 84 | + }); |
| 85 | + |
| 86 | + it("caches setupPaidFetch result across calls", async () => { |
| 87 | + globalThis.fetch = (async () => |
| 88 | + new Response(JSON.stringify({ ok: true }), { |
| 89 | + status: 200, |
| 90 | + headers: { "Content-Type": "application/json" }, |
| 91 | + })) as typeof fetch; |
| 92 | + |
| 93 | + const mod = await import(`./paid-fetch.js?t=${Date.now()}`); |
| 94 | + mod._resetPaidFetchCache(); |
| 95 | + |
| 96 | + // First call initializes cache |
| 97 | + await mod.paidApiRequest("/test1"); |
| 98 | + // Second call should reuse cache (no re-initialization) |
| 99 | + await mod.paidApiRequest("/test2"); |
| 100 | + |
| 101 | + // If we got here without error, caching works |
| 102 | + assert.ok(true); |
| 103 | + }); |
| 104 | + |
| 105 | + it("restores globalThis.fetch after call", async () => { |
| 106 | + const myFetch = (async () => |
| 107 | + new Response(JSON.stringify({ ok: true }), { |
| 108 | + status: 200, |
| 109 | + headers: { "Content-Type": "application/json" }, |
| 110 | + })) as typeof fetch; |
| 111 | + globalThis.fetch = myFetch; |
| 112 | + |
| 113 | + const { paidApiRequest, _resetPaidFetchCache } = await import(`./paid-fetch.js?t=${Date.now()}`); |
| 114 | + _resetPaidFetchCache(); |
| 115 | + await paidApiRequest("/test"); |
| 116 | + |
| 117 | + // globalThis.fetch should be restored to our myFetch |
| 118 | + assert.equal(globalThis.fetch, myFetch); |
| 119 | + }); |
| 120 | +}); |
0 commit comments