39 lines
1.6 KiB
JavaScript
39 lines
1.6 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { createAccountStore } = require('../desktop/account-store.cjs');
|
|
|
|
test('account store migrates a legacy single-account record and persists its generated id', async () => {
|
|
let stored = { email: 'user@example.com', password: 'secret' };
|
|
let saves = 0;
|
|
const repository = createAccountStore({
|
|
async load() { return stored; },
|
|
async save(value) { stored = value; saves += 1; }
|
|
}, { createId: () => 'migrated-account' });
|
|
|
|
assert.deepEqual(await repository.load(), {
|
|
accounts: [{ email: 'user@example.com', password: 'secret', id: 'migrated-account' }],
|
|
activeId: 'migrated-account'
|
|
});
|
|
assert.equal(saves, 1);
|
|
assert.equal(stored.activeId, 'migrated-account');
|
|
});
|
|
|
|
test('account store retains current account records without rewriting them', async () => {
|
|
const stored = { accounts: [{ id: 'account-1', email: 'user@example.com' }], activeId: 'account-1' };
|
|
let saves = 0;
|
|
const repository = createAccountStore({
|
|
async load() { return stored; },
|
|
async save() { saves += 1; }
|
|
});
|
|
|
|
assert.equal(await repository.load(), stored);
|
|
assert.equal(saves, 0);
|
|
});
|
|
|
|
test('account store distinguishes missing records from invalid store failures', async () => {
|
|
const missing = createAccountStore({ async load() { throw Object.assign(new Error('missing'), { code: 'ENOENT' }); } });
|
|
const broken = createAccountStore({ async load() { throw new Error('decrypt failed'); } });
|
|
assert.equal(await missing.load(), null);
|
|
await assert.rejects(() => broken.load(), /decrypt failed/);
|
|
});
|