import test from 'node:test'; import assert from 'node:assert/strict'; import {matchRegistered, filterByReason, runFinished, schemaDefaults, setConfigPath, generationError} from '../src/generation-logic.ts'; test('registration matches script identity or the whole converter path /, scoped to the run root', () => { const segments = [{script_id: 'a'}, {script_id: 'b'}, {script_id: 'c'}]; const registered = [{id: 'r1/a', script_id: 'a', root: 'r1'}, {id: 'r1/b', script_id: 'hash-b', root: 'r1'}, {id: 'r2/b', script_id: 'hash-b2', root: 'r2'}, {id: 'r2/xc', script_id: 'hash-c', root: 'r2'}]; assert.deepEqual(matchRegistered(segments, registered), [registered[0], registered[1], null]); assert.deepEqual(matchRegistered(segments, registered, 'r2'), [null, registered[2], null]); assert.deepEqual(matchRegistered([], registered), []); assert.deepEqual(matchRegistered(segments, []), [null, null, null]); }); test('reason filter distinguishes unavailable evidence from no matching segments', () => { const segments = [{reasons: {r: 2}}, {reasons: {r: 0}}, {reasons: {other: 1}}]; assert.deepEqual(filterByReason(segments, 'r'), [segments[0]]); assert.deepEqual(filterByReason(segments, 'absent'), []); assert.equal(filterByReason([{}], 'r'), null); assert.equal(filterByReason([segments[0], {}], 'r'), null); assert.equal(filterByReason([], 'r'), null); }); test('polling stops for every terminal run status and continues while running', () => { assert.equal(runFinished({status: 'running'}), false); for (const status of ['completed', 'completed_with_failures', 'failed'] as const) assert.equal(runFinished({status}), true); }); test('schema defaults preserve false and zero, nested edits do not mutate input', () => { assert.deepEqual(schemaDefaults({type: 'object', properties: {n: {default: 0}, enabled: {default: false}}}), {n: 0, enabled: false}); const config = {schema: 'np-batch-config/3', batch: {seed: 0}}; assert.deepEqual(setConfigPath(config, ['batch', 'template', 'groups'], []).batch, {seed: 0, template: {groups: []}}); assert.deepEqual(config.batch, {seed: 0}); }); test('run errors translate codes and preserve other backend messages', () => { assert.equal(generationError(Error('GENERATION_BUSY:busy')), '已经有一批在跑'); assert.equal(generationError(Error('NP_RUNS_NOT_CONFIGURED:missing')), '服务没配生成器,只能规划'); assert.equal(generationError(Error('GENERATOR_COMMIT_MISMATCH:mismatch')), '生成器版本和登记的不符'); assert.equal(generationError(Error('NP_CONFIG_INVALID:至少选一个观察者')), '至少选一个观察者'); });