secret-store.test.ts covers get, set, setAsync, writeBatch, delete, deleteAsync, list and has through the interface. vault.test.ts keeps what is specific to the file backend: encryption, the key file and the Windows ACL.
130 lines
4.6 KiB
TypeScript
130 lines
4.6 KiB
TypeScript
/**
|
|
* The SecretStore contract, checked on the file-backed VaultStore. Code that
|
|
* reads or writes secrets depends on these methods only; VaultStore specifics
|
|
* (encryption, key file, Windows ACL) are covered in vault*.test.ts.
|
|
*/
|
|
|
|
import { describe, it, expect, afterEach } from 'vitest';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import type { SecretStore } from '../src/secret-store.js';
|
|
import { VaultStore } from '../src/vault.js';
|
|
|
|
describe('SecretStore (VaultStore)', () => {
|
|
const tempDirs: string[] = [];
|
|
|
|
function openStore(): SecretStore {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-secret-store-'));
|
|
tempDirs.push(dir);
|
|
return new VaultStore(dir);
|
|
}
|
|
|
|
afterEach(() => {
|
|
for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true });
|
|
tempDirs.length = 0;
|
|
});
|
|
|
|
it('get returns the stored value, metadata and update time', () => {
|
|
const store = openStore();
|
|
store.set('anthropic', 'sk-ant-secret-key-123', { models: ['claude-sonnet-4-6'] });
|
|
|
|
const entry = store.get('anthropic');
|
|
expect(entry).toMatchObject({
|
|
name: 'anthropic',
|
|
value: 'sk-ant-secret-key-123',
|
|
metadata: { models: ['claude-sonnet-4-6'] },
|
|
});
|
|
expect(Number.isNaN(Date.parse(entry!.updatedAt))).toBe(false);
|
|
});
|
|
|
|
it('get returns null for a missing secret', () => {
|
|
expect(openStore().get('doesnotexist')).toBeNull();
|
|
});
|
|
|
|
it('set replaces an existing secret and its metadata', () => {
|
|
const store = openStore();
|
|
store.set('openai', 'sk-old-key', { baseUrl: 'https://old.example' });
|
|
store.set('openai', 'sk-new-key');
|
|
|
|
const entry = store.get('openai');
|
|
expect(entry!.value).toBe('sk-new-key');
|
|
expect(entry!.metadata).toBeUndefined();
|
|
});
|
|
|
|
it('setAsync stores a secret by the time it resolves', async () => {
|
|
const store = openStore();
|
|
await store.setAsync('openai', 'sk-async-key', { models: ['gpt-4o'] });
|
|
|
|
expect(store.get('openai')).toMatchObject({ value: 'sk-async-key', metadata: { models: ['gpt-4o'] } });
|
|
});
|
|
|
|
it('delete removes a secret and reports whether it existed', () => {
|
|
const store = openStore();
|
|
store.set('anthropic', 'sk-ant-key');
|
|
|
|
expect(store.delete('anthropic')).toBe(true);
|
|
expect(store.get('anthropic')).toBeNull();
|
|
expect(store.delete('anthropic')).toBe(false);
|
|
});
|
|
|
|
it('deleteAsync removes a secret and reports whether it existed', async () => {
|
|
const store = openStore();
|
|
store.set('anthropic', 'sk-ant-key');
|
|
|
|
await expect(store.deleteAsync('anthropic')).resolves.toBe(true);
|
|
expect(store.has('anthropic')).toBe(false);
|
|
await expect(store.deleteAsync('anthropic')).resolves.toBe(false);
|
|
});
|
|
|
|
it('list returns names, metadata and update times, never values', () => {
|
|
const store = openStore();
|
|
store.set('anthropic', 'sk-ant-key', { models: ['claude-sonnet-4-6'] });
|
|
store.set('openai', 'sk-openai-key');
|
|
|
|
const entries = store.list();
|
|
expect(entries.map(entry => entry.name).sort()).toEqual(['anthropic', 'openai']);
|
|
for (const entry of entries) {
|
|
expect(entry).not.toHaveProperty('value');
|
|
expect(entry.updatedAt).toBeTruthy();
|
|
}
|
|
expect(entries.find(entry => entry.name === 'anthropic')!.metadata).toEqual({ models: ['claude-sonnet-4-6'] });
|
|
});
|
|
|
|
it('has reports whether a secret exists', () => {
|
|
const store = openStore();
|
|
store.set('anthropic', 'sk-ant-key');
|
|
|
|
expect(store.has('anthropic')).toBe(true);
|
|
expect(store.has('missing')).toBe(false);
|
|
});
|
|
|
|
it('writeBatch applies writes and deletions together', () => {
|
|
const store = openStore();
|
|
store.set('connector:jira:refresh', 'old-refresh');
|
|
store.set('untouched', 'keep-me');
|
|
|
|
store.writeBatch({
|
|
set: [
|
|
{ name: 'connector:jira', value: 'jira-token', metadata: { credentialType: 'basic' } },
|
|
{ name: 'connector:jira:email', value: 'user@example.com' },
|
|
],
|
|
delete: ['connector:jira:refresh', 'never-existed'],
|
|
});
|
|
|
|
expect(store.get('connector:jira')).toMatchObject({ value: 'jira-token', metadata: { credentialType: 'basic' } });
|
|
expect(store.get('connector:jira:email')).toMatchObject({ value: 'user@example.com', metadata: undefined });
|
|
expect(store.has('connector:jira:refresh')).toBe(false);
|
|
expect(store.get('untouched')!.value).toBe('keep-me');
|
|
});
|
|
|
|
it('writeBatch applies deletions before writes', () => {
|
|
const store = openStore();
|
|
store.set('token', 'old');
|
|
|
|
store.writeBatch({ set: [{ name: 'token', value: 'new' }], delete: ['token'] });
|
|
|
|
expect(store.get('token')!.value).toBe('new');
|
|
});
|
|
});
|