Files
order-notification-automation/tests/dashboard/dashboard-store.test.cjs
T

63 lines
4.4 KiB
JavaScript

const {test}=require('node:test');
const assert=require('node:assert/strict');
const fs=require('node:fs');
const os=require('node:os');
const path=require('node:path');
let Store, nextSlots;
try { ({Store}=require('../../src/modules/dashboard/store.cjs')); ({nextSlots}=require('../../src/shared/schedule.cjs')); } catch(e){if(e.code!=='MODULE_NOT_FOUND')throw e;}
function fixture(t){assert.equal(typeof Store,'function','dashboard store not implemented');const dir=fs.mkdtempSync(path.join(os.tmpdir(),'dashboard-store-'));t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));return {dir,store:new Store(dir)};}
test('configuration is persisted and stale edits cannot overwrite another colleague',t=>{
const {dir,store}=fixture(t);
const original=store.settings();
assert.equal(original.schedule.timezone,'Asia/Shanghai');
const updated=store.saveSettings({schedule:{...original.schedule,times:['10:30','16:45'],weekends:false}},original.version,'admin');
assert.deepEqual(new Store(dir).settings().schedule.times,['10:30','16:45']);
assert.throws(()=>store.saveSettings({schedule:original.schedule},original.version,'admin'),/已被其他人/);
assert.throws(()=>store.saveSettings({schedule:{...updated.schedule,times:['25:00','16:00']}},updated.version,'admin'),/时间/);
assert.throws(()=>store.saveSettings({schedule:{...updated.schedule,times:['09:00','09:00']}},updated.version,'admin'),/两个不同/);
});
test('admin bootstrap is one-time and credentials are hashed with usable roles',t=>{
const {dir,store}=fixture(t);
const admin=store.bootstrap('admin','Test-password-123');
assert.equal(admin.role,'admin');
assert.equal(store.authenticate('admin','wrong'),null);
assert.equal(store.authenticate('admin','Test-password-123').id,admin.id);
assert.throws(()=>store.bootstrap('second','Test-password-123'),/管理员/);
const reader=store.addUser({username:'reader',password:'Reader-password-123',role:'viewer'},admin.username);
assert.equal(reader.role,'viewer');
assert.ok(!JSON.stringify(reader).includes('passwordHash'));
assert.ok(!fs.readFileSync(path.join(dir,'state.sqlite')).includes(Buffer.from('Test-password-123')));
});
test('export job records update in place and remain visible after restart',t=>{
const {dir,store}=fixture(t);
store.recordJob({jobId:'abc',status:'running',metadata:{order:'900001',project:'DEMO-PROJECT-001'}});
store.recordJob({jobId:'abc',status:'success',filename:'order.pdf',path:'C:/output/order.pdf',bytes:1234});
const jobs=new Store(dir).jobs();
assert.equal(jobs.length,1);
assert.equal(jobs[0].metadata.order,'900001');
assert.equal(jobs[0].status,'success');
});
test('next slots follow Shanghai time, weekend policy and exact slot boundary',()=>{
assert.equal(typeof nextSlots,'function','schedule calculation not implemented');
const schedule={times:['09:00','17:00'],weekends:false};
assert.deepEqual(nextSlots(schedule,new Date('2026-10-16T09:00:00Z'),2),['2026-10-19T01:00:00.000Z','2026-10-19T09:00:00.000Z']);
assert.deepEqual(nextSlots({...schedule,weekends:true},new Date('2026-10-16T08:59:00Z'),2),['2026-10-16T09:00:00.000Z','2026-10-17T01:00:00.000Z']);
});
test('admin creation and subsequent settings saves survive EXDEV renames',t=>{
const {dir,store}=fixture(t);
t.mock.method(fs,'renameSync',()=>{throw Object.assign(new Error('cross-device rename'),{code:'EXDEV'});});
const admin=store.bootstrap('admin','Test-password-123');
const settings=store.settings();store.saveSettings({templates:{subject:'Saved subject',body:'Saved body'}},settings.version,admin.username);
const restarted=new Store(dir);assert.equal(restarted.authenticate('admin','Test-password-123').id,admin.id);assert.equal(restarted.settings().templates.subject,'Saved subject');
});
test('legacy JSON state imports once and remains intact as a backup',t=>{
const {dir,store}=fixture(t);store.bootstrap('legacy','Test-password-123');
const legacy=JSON.stringify(store.data);
const importedDir=fs.mkdtempSync(path.join(os.tmpdir(),'dashboard-import-'));t.after(()=>fs.rmSync(importedDir,{recursive:true,force:true}));fs.writeFileSync(path.join(importedDir,'state.json'),legacy);
const imported=new Store(importedDir);assert.equal(imported.authenticate('legacy','Test-password-123').role,'admin');
imported.addUser({username:'reader',password:'Reader-password-123',role:'viewer'},'legacy');
assert.equal(new Store(importedDir).users().length,2);assert.equal(fs.readFileSync(path.join(importedDir,'state.json'),'utf8'),legacy);
});