import assert from 'node:assert/strict'; import test from 'node:test'; import { configureSourceBatch } from '../src/sourceBatchConfigure.ts'; type Body = { uploadId: string; fileId: string; year: number; fieldMap: Record }; type Candidate = { id: string; status: 'DISCOVERED' | 'RUNNING'; body: Body }; function candidateService() { const byIdentity = new Map(); const calls: Body[] = []; const configure = async (body: Body) => { calls.push(body); const identity = `${body.uploadId}:${body.fileId}`; const previous = byIdentity.get(identity); if (previous && previous.status !== 'DISCOVERED') throw new Error('409 candidate is no longer editable'); const candidate = previous || { id: `candidate-${body.fileId}`, status: 'DISCOVERED' as const, body }; candidate.body = body; byIdentity.set(identity, candidate); return { source: { id: candidate.id } }; }; return { byIdentity, calls, configure }; } const item = (id: string, fileId = id, year = 2026): { id: string; body: Body } => ({ id, body: { uploadId: `upload-${id}`, fileId, year, fieldMap: { weight: '重量' } }, }); test('keeps the confirmed source ID when a later file fails, then retries only the failed file', async () => { const service = candidateService(); const configured = new Map(); const files = [item('A'), item('B')]; let bAttempts = 0; const configure = async (body: Body) => { if (body.fileId === 'B' && ++bAttempts === 1) throw new Error('temporary configure failure'); return service.configure(body); }; await assert.rejects(configureSourceBatch(files, configured, configure), /temporary configure failure/); assert.equal(configured.get('A').sourceId, 'candidate-A'); assert.equal(configured.has('B'), false); const sourceIds = await configureSourceBatch(files, configured, configure); assert.deepEqual(sourceIds, ['candidate-A', 'candidate-B']); assert.equal(service.calls.filter(body => body.fileId === 'A').length, 1); assert.equal(service.calls.filter(body => body.fileId === 'B').length, 1); }); test('reconfigures changed metadata with the original uploadId and fileId while preserving the candidate ID', async () => { const service = candidateService(); const configured = new Map(); const first = item('A', 'logical-A', 2025); const second = item('A', 'logical-A', 2026); const [initialId] = await configureSourceBatch([first], configured, service.configure); const [updatedId] = await configureSourceBatch([second], configured, service.configure); assert.equal(initialId, updatedId); assert.equal(service.calls.length, 2); assert.equal(service.calls[0].uploadId, service.calls[1].uploadId); assert.equal(service.calls[0].fileId, service.calls[1].fileId); assert.equal(service.byIdentity.get('upload-A:logical-A')?.body.year, 2026); }); test('does not replace the cached candidate when the server rejects edits after processing starts', async () => { const service = candidateService(); const configured = new Map(); const first = item('A', 'logical-A', 2025); const changed = item('A', 'logical-A', 2026); await configureSourceBatch([first], configured, service.configure); const candidate = service.byIdentity.get('upload-A:logical-A'); assert.ok(candidate); candidate.status = 'RUNNING'; await assert.rejects(configureSourceBatch([changed], configured, service.configure), /409 candidate is no longer editable/); assert.equal(configured.get('A').signature, JSON.stringify(first.body)); });