fix(auth): persist workbench state without cross-device renames

This commit is contained in:
阿白叔 committed 2026-10-17 12:17:45 +08:00
1 parent b477737b0f
commit 6c2cc526e6
6 files changed
+58 -9

No files matched your search

+18 -1
View File
@@ -26,7 +26,7 @@ test('admin bootstrap is one-time and credentials are hashed with usable roles',
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.json'),'utf8').includes('Test-password-123'));
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);
@@ -43,3 +43,20 @@ test('next slots follow Shanghai time, weekend policy and exact slot boundary',(
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);
});