From 97d3e18bfb24b0d783eda2b997fd5779e5e83487 Mon Sep 17 00:00:00 2001 From: Oyinkans0la12 <147610565+Oyinkans0la12@users.noreply.github.com> Date: Mon, 14 Sep 2026 11:37:39 +0000 Subject: [PATCH 1/2] fix(cli): read the remote manifest body once in loadManifest `loadManifest` called `response.text()` twice, once for the parser and once for the `json` field. A fetch body can only be consumed once, so every remote manifest load - `--repo owner/name` on `status`, `claim` and `report` - failed with "Body is unusable: Body has already been read" instead of parsing the manifest it had just downloaded. Read the body into a local and hand the same text to both consumers. --- packages/cli/src/manifest.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/manifest.ts b/packages/cli/src/manifest.ts index b1e2067..397119b 100644 --- a/packages/cli/src/manifest.ts +++ b/packages/cli/src/manifest.ts @@ -159,7 +159,11 @@ export async function loadManifest(options: LoadManifestOptions): Promise.`, ); } - return { manifest: parseOrExplain(await response.text(), url), source: url, json: await response.text() }; + // The body can only be read once: keep the text and hand the same string to + // both the parser and the caller (a second `response.text()` rejects with + // "Body is unusable"). + const json = await response.text(); + return { manifest: parseOrExplain(json, url), source: url, json }; } for (const candidate of candidateLocalPaths(cycleId)) { From 1ed02ebf2ed7e3b38cc0ef82106077e5fdab9eaa Mon Sep 17 00:00:00 2001 From: Oyinkans0la12 <147610565+Oyinkans0la12@users.noreply.github.com> Date: Mon, 14 Sep 2026 11:37:39 +0000 Subject: [PATCH 2/2] test(cli): cover loadManifest's source preference and remote fetch `loadManifest` decides which manifest becomes a payout, and nothing tested it: the four-step preference order, the remote URL shape and the failure messages were all unverified. The fetch stub is deliberately strict - it rejects a second `text()` call, like real fetch does - which is what makes the double-read fix a regression test. Verified by reverting the fix: the two remote-body cases fail, both green again with it. Part of #8. --- packages/cli/test/manifest.test.ts | 218 +++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 packages/cli/test/manifest.test.ts diff --git a/packages/cli/test/manifest.test.ts b/packages/cli/test/manifest.test.ts new file mode 100644 index 0000000..bbb1f14 --- /dev/null +++ b/packages/cli/test/manifest.test.ts @@ -0,0 +1,218 @@ +import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +import { describe, expect, it } from 'vitest'; + +import { + ManifestNotFoundError, + candidateLocalPaths, + discoverManifestCycleIds, + loadManifest, + type FetchTextLike, +} from '../src/manifest.js'; +import { ManifestParseError } from '@splitstream/sdk'; +import { manifestDirectory, writeManifestFile } from './fixtures.js'; + +const REPO = 'owner/name'; +const URL_FOR_CYCLE_3 = `https://raw.githubusercontent.com/${REPO}/main/manifests/cycle-3.json`; + +/** + * A `fetch` stub that behaves like the real thing: the body can be read once. + * + * The strictness is the point. A stub whose `text()` can be called repeatedly + * hides a double-read defect, which is exactly how the remote branch of + * `loadManifest` shipped broken. + */ +function remoteFetch( + body: string, + options: { ok?: boolean; status?: number } = {}, +): { fetchImpl: FetchTextLike; calls: string[]; readCount: () => number } { + const calls: string[] = []; + let reads = 0; + const fetchImpl: FetchTextLike = async (url) => { + calls.push(url); + return { + ok: options.ok ?? true, + status: options.status ?? 200, + text: async () => { + reads += 1; + if (reads > 1) { + throw new TypeError('Body is unusable: Body has already been read'); + } + return body; + }, + }; + }; + return { fetchImpl, calls, readCount: () => reads }; +} + +function fixtureManifestText(cycleId = 3): string { + return readFileSync(writeManifestFile({ cycleId }), 'utf8'); +} + +/** Runs `fn` with the process working directory temporarily changed. */ +async function withCwd(directory: string, fn: () => Promise): Promise { + const previous = process.cwd(); + process.chdir(directory); + try { + return await fn(); + } finally { + process.chdir(previous); + } +} + +/** + * Captures the error a promise rejects with. + * + * The stub's single-read guard is per-stub, so each rejecting case is loaded + * once and asserted against the captured error instead of loading twice. + */ +async function rejection(promise: Promise): Promise<{ error: unknown; message: string }> { + try { + await promise; + } catch (error) { + return { + error, + message: error instanceof Error ? error.message : String(error), + }; + } + throw new Error('expected the promise to reject, but it resolved'); +} + +describe('discoverManifestCycleIds', () => { + it('lists the cycles that have a manifest, ascending', () => { + expect(discoverManifestCycleIds(manifestDirectory([5, 3, 4]))).toEqual([3, 4, 5]); + }); + + it('returns nothing when there is no manifests directory', () => { + expect(discoverManifestCycleIds(mkdtempSync(join(tmpdir(), 'splitstream-empty-')))).toEqual([]); + }); +}); + +describe('candidateLocalPaths', () => { + it('prefers manifests/cycle-.json, then manifests/.json, then the root', () => { + expect(candidateLocalPaths(3, '/repo')).toEqual([ + join('/repo', 'manifests', 'cycle-3.json'), + join('/repo', 'manifests', '3.json'), + join('/repo', 'cycle-3.json'), + ]); + }); +}); + +describe('loadManifest source preference', () => { + it('uses an explicit --manifest path and never calls GitHub', async () => { + const path = writeManifestFile({ cycleId: 3 }); + const remote = remoteFetch(fixtureManifestText(3)); + + const loaded = await loadManifest({ cycleId: 3, manifestPath: path, repo: REPO, fetchImpl: remote.fetchImpl }); + + expect(loaded.manifest.cycleId).toBe(3); + expect(loaded.source).toBe(path); + expect(remote.calls).toEqual([]); + }); + + it('prefers a local checkout passed via --repo over fetching from GitHub', async () => { + const checkout = manifestDirectory([3]); + const remote = remoteFetch('{}'); + + const loaded = await loadManifest({ cycleId: 3, repo: checkout, fetchImpl: remote.fetchImpl }); + + expect(loaded.manifest.cycleId).toBe(3); + expect(loaded.source).toBe(join(checkout, 'manifests', 'cycle-3.json')); + expect(remote.calls).toEqual([]); + }); + + it('falls back to the working directory', async () => { + const cwd = manifestDirectory([3]); + const loaded = await withCwd(cwd, () => loadManifest({ cycleId: 3 })); + expect(loaded.manifest.cycleId).toBe(3); + }); +}); + +describe('loadManifest remote branch', () => { + it('reads the manifest from raw.githubusercontent.com at the given ref', async () => { + const body = fixtureManifestText(3); + const remote = remoteFetch(body); + + const loaded = await loadManifest({ cycleId: 3, repo: REPO, gitRef: 'release', fetchImpl: remote.fetchImpl }); + + expect(remote.calls).toEqual([ + `https://raw.githubusercontent.com/${REPO}/release/manifests/cycle-3.json`, + ]); + expect(loaded.manifest.cycleId).toBe(3); + expect(loaded.manifest.totalIssuesClosed).toBeGreaterThan(0); + expect(loaded.source).toBe(remote.calls[0]); + }); + + it('reads the response body exactly once and returns it verbatim', async () => { + const body = fixtureManifestText(3); + const remote = remoteFetch(body); + + const loaded = await loadManifest({ cycleId: 3, repo: REPO, fetchImpl: remote.fetchImpl }); + + // The default `--ref` is `main`. + expect(remote.calls).toEqual([URL_FOR_CYCLE_3]); + expect(remote.readCount()).toBe(1); + expect(loaded.json).toBe(body); + }); + + it('reports a 404 as a manifest-not-found error naming the ways out', async () => { + const remote = remoteFetch('Not Found', { ok: false, status: 404 }); + + const { error, message } = await rejection( + loadManifest({ cycleId: 3, repo: REPO, fetchImpl: remote.fetchImpl }), + ); + + expect(error).toBeInstanceOf(ManifestNotFoundError); + expect(message).toMatch(/HTTP 404/); + expect(message).toMatch(/--repo and --ref/); + expect(message).toMatch(/--manifest/); + expect(remote.readCount()).toBe(0); + }); + + it('names the URL when the fetched body is not valid JSON', async () => { + const remote = remoteFetch('not json'); + + const { error, message } = await rejection( + loadManifest({ cycleId: 3, repo: REPO, fetchImpl: remote.fetchImpl }), + ); + + expect(error).toBeInstanceOf(ManifestParseError); + expect(message).toContain(URL_FOR_CYCLE_3); + expect(message).toMatch(/not valid JSON/); + }); + + it('names the URL when the fetched body is JSON but not a manifest', async () => { + const remote = remoteFetch('{"hello":"world"}'); + + await expect( + loadManifest({ cycleId: 3, repo: REPO, fetchImpl: remote.fetchImpl }), + ).rejects.toThrow(URL_FOR_CYCLE_3); + }); + + it('surfaces a transport failure instead of pretending there is no manifest', async () => { + const fetchImpl: FetchTextLike = async () => { + throw new Error('getaddrinfo ENOTFOUND raw.githubusercontent.com'); + }; + + await expect(loadManifest({ cycleId: 3, repo: REPO, fetchImpl })).rejects.toThrow( + /could not reach GitHub.*ENOTFOUND/s, + ); + }); +}); + +describe('loadManifest when nothing is found', () => { + it('lists every candidate path it looked at', async () => { + const cwd = mkdtempSync(join(tmpdir(), 'splitstream-none-')); + try { + const { error, message } = await rejection(withCwd(cwd, () => loadManifest({ cycleId: 12 }))); + + expect(error).toBeInstanceOf(ManifestNotFoundError); + expect(message).toMatch(/no manifest for cycle 12 found/); + expect(message).toMatch(/manifests\/cycle-12\.json/); + } finally { + rmSync(cwd, { recursive: true, force: true }); + } + }); +});