import test from 'node:test';import assert from 'node:assert/strict';import {validatePatchnotes,makeRelease,makeBugfix} from '../scripts/patchnote-policy.mjs'; function fixture(count=5){const tasks=Array.from({length:count},(_,i)=>({id:'DUNE-'+(101+i),priority:'P2',kind:'product',status:'done',title:'Completed feature '+i}));return {register:{format:1,tasks},pending:{minimumTasks:5,maximumTasks:10,completed:tasks.map((t,i)=>({id:'feature-'+i,promptIds:[t.id],priority:'P2',completedAt:'2026-10-14',title:t.title,changes:['Released behaviour '+i]}))},releases:[]}} const input=n=>({version:'web-2026.10.14.1',date:'2026-10-14',title:'A real feature bundle',synopsis:'Completed product work grouped for readers.',taskIds:Array.from({length:n},(_,i)=>'feature-'+i)}); test('Normal bundles reject four and eleven tasks, accepting five and ten',()=>{for(const n of [4,11]){const f=fixture(n);assert.throws(()=>makeRelease(f.register,f.pending,f.releases,input(n)),/5–10/)}for(const n of [5,10]){const f=fixture(n);assert.equal(makeRelease(f.register,f.pending,f.releases,input(n)).release.tasks.length,n)}}); test('A release moves selected products once and preserves later pending work',()=>{const f=fixture(7),next=makeRelease(f.register,f.pending,[],input(5));assert.equal(next.pending.completed.length,2);assert.equal(next.register.tasks[0].release,input(5).version);assert.equal(next.register.tasks[6].release,undefined);assert.equal(f.pending.completed.length,7);assert.throws(()=>makeRelease(next.register,next.pending,next.releases,input(5)),/already exists/)}); test('Workflow agreements and unfinished tasks cannot inflate a release',()=>{for(const change of [{kind:'workflow'},{status:'open'}]){const f=fixture();Object.assign(f.register.tasks[0],change);assert.throws(()=>validatePatchnotes(f.register,f.pending,[]),/completed product/)}}); test('Duplicate prompt identities and colliding version filenames are rejected',()=>{const f=fixture();f.pending.completed[1].promptIds=f.pending.completed[0].promptIds;assert.throws(()=>validatePatchnotes(f.register,f.pending,[]),/more than once/);const g=fixture(10),one=makeRelease(g.register,g.pending,[],input(5));const second={...input(5),version:'web-2026_10_14_1',taskIds:Array.from({length:5},(_,i)=>'feature-'+(5+i))};assert.throws(()=>makeRelease(one.register,one.pending,one.releases,second),/already exists/)}); test('A single urgent bugfix publishes independently with problem, repair and checks',()=>{const register={format:1,tasks:[{id:'DUNE-201',priority:'BUGFIX',kind:'product',status:'done'}]},pending={minimumTasks:5,maximumTasks:10,completed:[]};const bug={version:'web-2026.10.14-hotfix1',date:'2026-10-14',title:'Bugfix · fixture only',synopsis:'Test record, never production data.',problem:'A broken path',fix:'The path was repaired',checks:['Regression check passed'],tasks:[{id:'fixture-fix',title:'Repair',promptIds:['DUNE-201'],priority:'BUGFIX',changes:['Restored behaviour']}]};assert.equal(makeBugfix(register,pending,[],bug).release.kind,'bugfix');assert.throws(()=>makeBugfix(register,pending,[],{...bug,checks:[]}),/checks/);assert.equal(pending.completed.length,0)}); test('Urgent fixes cannot be held in a normal feature queue',()=>{const f=fixture();f.register.tasks[0].priority='BUGFIX';f.pending.completed[0].priority='BUGFIX';assert.throws(()=>validatePatchnotes(f.register,f.pending,[]),/cannot wait/) });