test(vault): verify always-on background stat collectors and write-only vault lock

This commit is contained in:
tyuXX 2026-11-26 22:31:08 +00:00
parent d3ffa597d4
commit 7dac3d1261
3 changed files with 367 additions and 2 deletions

View file

@ -200,4 +200,64 @@ describe('VaultService & CLI key rotation', () => {
checkCli.unlock(rotatedKey2);
expect(checkCli.getSecret('api-1')).toMatchObject({ token: 'tok-xyz' });
});
it('supports runtime-ready write-locked mode via initRuntime and keyFilePath without blocking established secret reads', () => {
const dir = makeTempDir();
const vaultPath = path.join(dir, 'vault.enc');
const keyFilePath = path.join(dir, 'vault.key');
const v1 = new VaultService(vaultPath, { keyFilePath });
const initRes = v1.initRuntime({ autoCreateIfMissing: true });
expect(initRes.runtimeReady).toBe(true);
expect(initRes.initialized).toBe(true);
expect(v1.isRuntimeReady).toBe(true);
expect(v1.state).toBe('locked');
expect(fs.existsSync(keyFilePath)).toBe(true);
expect(fs.statSync(keyFilePath).mode & 0o777).toBe(0o600);
// Explicitly initialize / unlock for modifications and save an established credential
v1.unlockForModifications();
expect(v1.state).toBe('unlocked');
v1.upsertCredential({
id: 'runtime-cred',
name: 'Runtime SSH Key',
type: 'ssh-password',
password: 'established-runtime-secret',
});
v1.lockForModifications();
expect(v1.state).toBe('locked');
expect(v1.isRuntimeReady).toBe(true);
// Reload across restart with initRuntime() -> active for runtime use, locked for mutations/reveals
const v2 = new VaultService(vaultPath, { keyFilePath });
const bootRes = v2.initRuntime({ autoCreateIfMissing: true });
expect(bootRes.runtimeReady).toBe(true);
expect(v2.isRuntimeReady).toBe(true);
expect(v2.state).toBe('locked');
expect(v2.hasCredential('runtime-cred')).toBe(true);
expect(v2.listMetadata().map((m) => m.id)).toEqual(['runtime-cred']);
expect(v2.getRuntimeSecret('runtime-cred')).toMatchObject({
password: 'established-runtime-secret',
});
expect(() => v2.getSecret('runtime-cred')).toThrow(/locked/i);
expect(() =>
v2.upsertCredential({
id: 'new-cred',
name: 'Blocked While Locked',
type: 'api-token',
token: 'nope',
}),
).toThrow(/locked/i);
v2.unlockForModifications();
expect(v2.state).toBe('unlocked');
expect(v2.getSecret('runtime-cred')).toMatchObject({
password: 'established-runtime-secret',
});
v2.lockForModifications();
expect(v2.state).toBe('locked');
expect(v2.isRuntimeReady).toBe(true);
});
});