singularity-forge/src/tests/fetch-test-helpers.ts
Iouri Goussev 642c0f5a9e test: fix Assertion Roulette, Eager Test, and contract test regressions (#1938)
* test: add assertion messages to fix Assertion Roulette in GSD tests

Add descriptive messages to multi-assertion tests where a bare failure
output ("expected true, got false") wouldn't identify which assertion
broke. Affected tests: auto-secrets-gate, search-tavily, search-provider-
command, tavily-helpers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: fix Eager Test smell in captures and worktree-manager tests

- Split captures: loadPendingCaptures test — extracted loadAllCaptures
  assertion into its own focused test
- Refactor worktree-manager: replace monolithic main() script with 11
  isolated test() calls, each with its own repo setup via helpers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: add assertion messages to remaining test files

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: fix contract test gate, dynamic roots, and shared fetch helpers

- Fix reject-notice sub-test gated on outcome.kind (actual) instead of
  expectedKind (map value) in web-command-parity-contract.test.ts
- Restore dynamic loop over registered non-gsd passthrough roots with
  an explicit count assertion so new registrations fail loudly
- Extract normalizeHeaders/parseJsonBody to src/tests/fetch-test-helpers.ts
  and import in both search-tavily and llm-context-tavily tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 21:24:15 -06:00

20 lines
786 B
TypeScript

/**
* Shared fetch-mocking utilities for test files that need to intercept
* globalThis.fetch and inspect request headers/body.
*/
export function normalizeHeaders(headers: HeadersInit | undefined): Record<string, string> | undefined {
if (headers == null) return undefined;
if (headers instanceof Headers) {
const result: Record<string, string> = {};
headers.forEach((v, k) => { result[k] = v; });
return result;
}
if (Array.isArray(headers)) return Object.fromEntries(headers);
return headers as Record<string, string>;
}
export function parseJsonBody(body: BodyInit | null | undefined): Record<string, unknown> | undefined {
if (body == null || typeof body !== "string") return undefined;
try { return JSON.parse(body); } catch { return undefined; }
}