Files
order-notification-automation/tests/notifications/history-adapter.test.cjs
T

9 lines
1.9 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'),crypto=require('node:crypto');const {NotificationHistory}=require('../../src/modules/notifications/history.cjs'),{NotificationStore}=require('../../src/modules/notifications/store.cjs');
test('retired engine reads old encrypted evidence without recovery or database writes',t=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'readonly-notifications-'));t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));const writer=new NotificationStore(dir);writer.ingest({sourceKey:'fixture-source',sourceId:'old',sourceVersion:'1',industry:'历史类别',brief:'合成历史',subject:'合成主题'});const expected=writer.items();writer.close();const file=path.join(dir,'notifications.sqlite'),hash=()=>crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex'),before=hash();const history=new NotificationHistory(dir);assert.deepEqual(history.items(),expected);assert.deepEqual(history.batches(),[]);assert.equal(history.settings().historyOnly,true);assert.equal(history.settings().enabled,false);assert.equal(history.settings().sourceKey,undefined);assert.equal(history.recover,undefined);assert.equal(history.ingest,undefined);history.close();assert.equal(hash(),before);
});
test('older uncertain legacy batches remain visible beyond the recent history cap',t=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'readonly-unknown-'));t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));const writer=new NotificationStore(dir);for(let i=0;i<102;i++)writer.db.prepare('INSERT INTO batches VALUES(?,?,?,?,?)').run('batch-'+i,i===0?'unknown':'sent',writer.encode('batch-'+i,{text:'合成历史'}),null,'2020-01-01');writer.close();const history=new NotificationHistory(dir);try{assert.equal(history.batches()[0].id,'batch-0');assert.equal(history.batches().length,100);}finally{history.close();}
});