From 0b3e4fc339ee34ab5ce3e62cc628ab49c8ea929c Mon Sep 17 00:00:00 2001 From: "shumeiko.it" Date: Sat, 24 Oct 2026 16:44:52 +0200 Subject: [PATCH] test(core): test the SecretStore contract on VaultStore (P1.2) 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. --- packages/core/tests/secret-store.test.ts | 129 +++++++++++++++++++++++ packages/core/tests/vault.test.ts | 82 -------------- 2 files changed, 129 insertions(+), 82 deletions(-) create mode 100644 packages/core/tests/secret-store.test.ts diff --git a/packages/core/tests/secret-store.test.ts b/packages/core/tests/secret-store.test.ts new file mode 100644 index 0000000..bbab8f6 --- /dev/null +++ b/packages/core/tests/secret-store.test.ts @@ -0,0 +1,129 @@ +/** + * 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'); + }); +}); diff --git a/packages/core/tests/vault.test.ts b/packages/core/tests/vault.test.ts index 1451e52..eeb904a 100644 --- a/packages/core/tests/vault.test.ts +++ b/packages/core/tests/vault.test.ts @@ -61,88 +61,6 @@ describe('VaultStore', () => { mockExecFileSync.mockReset(); }); - it('set and get — store a secret, retrieve it, value matches', () => { - const dir = makeTempDir(); - const vault = new VaultStore(dir); - - vault.set('anthropic', 'sk-ant-secret-key-123'); - - const entry = vault.get('anthropic'); - expect(entry).not.toBeNull(); - expect(entry!.name).toBe('anthropic'); - expect(entry!.value).toBe('sk-ant-secret-key-123'); - expect(entry!.updatedAt).toBeTruthy(); - }); - - it('set overwrites — set same name twice, get returns latest', () => { - const dir = makeTempDir(); - const vault = new VaultStore(dir); - - vault.set('openai', 'sk-old-key'); - vault.set('openai', 'sk-new-key'); - - const entry = vault.get('openai'); - expect(entry).not.toBeNull(); - expect(entry!.value).toBe('sk-new-key'); - }); - - it('get nonexistent — returns null', () => { - const dir = makeTempDir(); - const vault = new VaultStore(dir); - - expect(vault.get('doesnotexist')).toBeNull(); - }); - - it('delete — removes secret, get returns null after', () => { - const dir = makeTempDir(); - const vault = new VaultStore(dir); - - vault.set('anthropic', 'sk-ant-key'); - expect(vault.delete('anthropic')).toBe(true); - expect(vault.get('anthropic')).toBeNull(); - }); - - it('delete nonexistent — returns false', () => { - const dir = makeTempDir(); - const vault = new VaultStore(dir); - - expect(vault.delete('nope')).toBe(false); - }); - - it('list — shows names + metadata without values', () => { - const dir = makeTempDir(); - const vault = new VaultStore(dir); - - vault.set('anthropic', 'sk-ant-key', { models: ['claude-sonnet-4-6'] }); - vault.set('openai', 'sk-openai-key', { models: ['gpt-4o'], baseUrl: 'https://api.openai.com' }); - - const entries = vault.list(); - expect(entries).toHaveLength(2); - - const names = entries.map(e => e.name); - expect(names).toContain('anthropic'); - expect(names).toContain('openai'); - - // list must NOT contain secret values - for (const entry of entries) { - expect(entry).not.toHaveProperty('value'); - expect(entry.updatedAt).toBeTruthy(); - } - - const anthropicEntry = entries.find(e => e.name === 'anthropic')!; - expect(anthropicEntry.metadata).toEqual({ models: ['claude-sonnet-4-6'] }); - }); - - it('has — returns true for existing, false for missing', () => { - const dir = makeTempDir(); - const vault = new VaultStore(dir); - - vault.set('anthropic', 'sk-ant-key'); - - expect(vault.has('anthropic')).toBe(true); - expect(vault.has('missing')).toBe(false); - }); - it('encryption is real — vault.json does NOT contain plaintext value', () => { const dir = makeTempDir(); const vault = new VaultStore(dir);