'use strict'; const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'); const {Store}=require('../../src/modules/dashboard/store.cjs'); function fixture(t){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'job-retention-'));t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));return {dir,store:new Store(dir)};} test('legacy 5001 PDF jobs migrate intact and an additional export never evicts their evidence',t=>{ const f=fixture(t),legacy={...f.store.data,jobs:Array.from({length:5001},(_,i)=>({jobId:'job-'+String(i).padStart(5,'0'),status:'success',filename:'record-'+i+'.pdf',metadata:{order:'ORDER-'+i},updatedAt:'2026-10-21T00:00:00Z'}))}; f.store.database(db=>db.prepare('UPDATE dashboard_state SET value=? WHERE id=1').run(JSON.stringify(legacy))); const store=new Store(f.dir);store.recordJob({jobId:'latest',status:'success',filename:'new.pdf'});assert.equal(store.jobs().length,5002);assert.ok(store.jobs().some(job=>job.jobId==='job-00000')); const restarted=new Store(f.dir);assert.equal(restarted.jobs().length,5002);assert.equal(typeof restarted.job,'function');assert.equal(restarted.job('job-00000').filename,'record-0.pdf'); const state=restarted.database(db=>JSON.parse(db.prepare('SELECT value FROM dashboard_state WHERE id=1').get().value));assert.ok(!state.jobs,'job rows must not remain in the growing settings JSON'); }); test('SQL job pagination clamps, searches literal wildcards and retains a stable empty first page',t=>{ const f=fixture(t);assert.equal(typeof f.store.jobPage,'function');for(let i=0;i<76;i++)f.store.recordJob({jobId:'j-'+i,status:'running',metadata:{order:i===0?'LITERAL_%':'ORDER-'+i}}); const last=f.store.jobPage({page:99,pageSize:25});assert.equal(last.page,4);assert.equal(last.jobs.length,1);assert.equal(last.total,76); assert.equal(f.store.jobPage({q:'_%'}).total,1);const empty=f.store.jobPage({status:'interrupted',page:99});assert.equal(empty.page,1);assert.equal(empty.total,0);assert.deepEqual(empty.jobs,[]); }); test('job and audit updates roll back together when SQLite rejects the new evidence row',t=>{ const f=fixture(t),exists=f.store.database(db=>db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='pdf_jobs'").get());assert.ok(exists,'jobs require their own durable table'); f.store.database(db=>db.exec("CREATE TRIGGER reject_job BEFORE INSERT ON pdf_jobs BEGIN SELECT RAISE(ABORT,'synthetic database failure'); END"));const before=f.store.audit(); assert.throws(()=>f.store.recordJob({jobId:'rejected',status:'error',error:'synthetic'}),/synthetic/);assert.deepEqual(f.store.audit(),before);assert.equal(f.store.jobs().length,0); });