import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { parseArgs, run } from './stage-legacy-knowledge.mjs'; test('dry-run is the default and does not call the mutation endpoint', async () => { const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'aihr-legacy-dry-')); const options = parseArgs(['--manifest', path.join(directory, 'manifest.json')]); const result = await run(options, { fetchImpl: async () => assert.fail('fetch must not be called') }); assert.equal(result.mode, 'DRY_RUN'); assert.equal(result.preview, null); await assert.rejects(fs.access(options.manifest)); }); test('authenticated dry-run calls only the read-only preview endpoint', async () => { const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'aihr-legacy-preview-')); const options = parseArgs([ '--token', 'preview-secret', '--manifest', path.join(directory, 'manifest.json'), '--batch-size', '20' ]); let requestedUrl; const result = await run(options, { fetchImpl: async (url, request) => { requestedUrl = String(url); assert.equal(request.method, 'GET'); return { ok: true, status: 200, json: async () => ({ code: 200, data: { discovered: 20, sourceAvailable: 18, sourceUnavailable: 2, nextCursor: 42, moreAvailable: true } }) }; } }); assert.match(requestedUrl, /\/api\/knowledge\/quality\/legacy\/preview/); assert.equal(result.preview.sourceUnavailable, 2); await assert.rejects(fs.access(options.manifest)); }); test('execute writes a resumable manifest without credentials', async () => { const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'aihr-legacy-run-')); const options = parseArgs([ '--execute', '--token', 'top-secret', '--manifest', path.join(directory, 'manifest.json'), '--max-batches', '2', '--batch-size', '20' ]); const replies = [ { discovered: 20, staged: 20, reparsed: 18, quarantined: 2, stagedAssetIds: [11, 12], failedAttachmentIds: [], nextCursor: 100, moreAvailable: true }, { discovered: 3, staged: 3, reparsed: 2, quarantined: 1, stagedAssetIds: [13], failedAttachmentIds: [], nextCursor: 103, moreAvailable: false } ]; let calls = 0; const result = await run(options, { fetchImpl: async (_url, request) => { assert.match(request.headers.Authorization, /top-secret/); return { ok: true, status: 200, json: async () => ({ code: 200, data: replies[calls++] }) }; }, now: () => new Date('2026-08-01T00:00:00Z') }); const manifestText = await fs.readFile(options.manifest, 'utf8'); const manifest = JSON.parse(manifestText); assert.equal(result.status, 'COMPLETE'); assert.equal(manifest.cursor, 103); assert.equal(manifest.batches.length, 2); assert.doesNotMatch(manifestText, /top-secret/); }); test('failed attachment does not advance the recovery cursor', async () => { const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'aihr-legacy-fail-')); const options = parseArgs([ '--execute', '--token', 'secret', '--manifest', path.join(directory, 'manifest.json'), '--batch-size', '20' ]); await assert.rejects(run(options, { fetchImpl: async () => ({ ok: true, status: 200, json: async () => ({ code: 200, data: { discovered: 2, staged: 1, reparsed: 1, quarantined: 0, stagedAssetIds: [11], failedAttachmentIds: [42], nextCursor: 42, moreAvailable: true } }) }) }), /cursor was not advanced/); const manifest = JSON.parse(await fs.readFile(options.manifest, 'utf8')); assert.equal(manifest.status, 'FAILED'); assert.equal(manifest.cursor, 0); });