| 1 | import { FakeDraftClaimLock } from "./draft-claim-lock.fake"; |
| 2 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | import { fetchBoundedText } from "./bounded-body"; |
| 4 | import { runDigest, runDupes, runPrReview, runStale, runTriage } from "./community-agent-tasks"; |
| 5 | import { runLinkCheck } from "./content-watch"; |
| 6 | import { deriveFactsFromRemote } from "./facts-drift"; |
| 7 | |
| 8 | afterEach(() => vi.unstubAllGlobals()); |
| 9 | |
| 10 | /** A peer that accepts the request and never answers until aborted. */ |
| 11 | function hangingFetch() { |
| 12 | return vi.fn((_: unknown, init?: RequestInit) => new Promise<Response>((_, reject) => { |
| 13 | init?.signal?.addEventListener("abort", () => reject(init.signal!.reason), { once: true }); |
| 14 | })); |
| 15 | } |
| 16 | |
| 17 | describe("bounded outbound reads", () => { |
| 18 | it("gives up on a silent peer at the deadline and refuses an oversized body", async () => { |
| 19 | vi.stubGlobal("fetch", hangingFetch()); |
| 20 | const started = Date.now(); |
| 21 | await expect(fetchBoundedText("https://slow.test", {}, { timeoutMs: 50 })).rejects.toThrow(); |
| 22 | expect(Date.now() - started).toBeLessThan(2_000); |
| 23 | |
| 24 | vi.stubGlobal("fetch", vi.fn(async () => new Response("x".repeat(64)))); |
| 25 | await expect(fetchBoundedText("https://big.test", {}, { maxBytes: 16 })).rejects.toThrow("payload too large"); |
| 26 | vi.stubGlobal("fetch", vi.fn(async () => new Response("gone", { status: 404 }))); |
| 27 | expect(await fetchBoundedText("https://missing.test")).toEqual({ ok: false, status: 404, text: "" }); |
| 28 | }); |
| 29 | |
| 30 | it("puts every cron read of other hosts under a deadline", async () => { |
| 31 | // Callers swallow transport errors, so record rather than assert inside. |
| 32 | const seen = vi.fn(async () => new Response("{}", { status: 500 })); |
| 33 | vi.stubGlobal("fetch", seen); |
| 34 | await deriveFactsFromRemote(); |
| 35 | await runLinkCheck({ CURATED_KV: { get: async () => null, put: async () => undefined } } as never); |
| 36 | const kv = { get: async () => null, put: async () => undefined, delete: async () => undefined, list: async () => ({ keys: [], list_complete: true }) }; |
| 37 | for (const task of [runTriage, runPrReview, runStale, runDupes, runDigest]) { |
| 38 | await task({ CURATED_KV: kv, DRAFT_CLAIM_LOCK: new FakeDraftClaimLock(), DEEPSEEK_API_KEY: "k" } as never).catch(() => undefined); |
| 39 | } |
| 40 | expect(seen.mock.calls.length).toBeGreaterThan(1); |
| 41 | const unbounded = seen.mock.calls |
| 42 | .filter((call) => !((call as unknown[])[1] as RequestInit | undefined)?.signal) |
| 43 | .map((call) => String((call as unknown[])[0])); |
| 44 | expect(unbounded).toEqual([]); |
| 45 | }); |
| 46 | }); |
| 47 |