refactor(agent): move the connector credential helpers out of VaultStore (P1.2)

getConnectorCredential, setConnectorCredential and setConnectorCredentialBundle
are now plain functions over a SecretStore in
packages/agent/src/connectors/connector-credentials.ts. The secret names,
metadata layout and the rule that tokens never go into plaintext metadata are
unchanged, so existing vault files keep working.

A bundle still lands as one update: SecretStore gains writeBatch, which
VaultStore applies with a single vault-file replacement.

Connector tests seed a Map-backed SecretStore instead of mocking VaultStore
methods; the helper tests from core and server moved to the agent package.
This commit is contained in:
shumeiko.it
2026-10-24 16:34:20 +02:00
parent 23431550ae
commit 3528a242f6
60 changed files with 602 additions and 606 deletions

View File

@@ -5,7 +5,7 @@ import path from 'node:path';
import { NotionConnector } from '../../src/connectors/notion-connector.js';
import { ConfluenceConnector } from '../../src/connectors/confluence-connector.js';
import { ObsidianConnector } from '../../src/connectors/obsidian-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
/** Shape of ConnectorResult.data fields asserted by these knowledge-connector tests. */
type KnowledgeData = {
@@ -21,20 +21,11 @@ function createMockVault(
connectorId: string,
cred?: { value: string; isExpired: boolean },
extras?: Record<string, { value: string }>,
): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === connectorId && cred) return { ...cred, type: 'api_key' };
return null;
}),
get: vi.fn((key: string) => extras?.[key] ?? null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
): MockSecretStore {
return createMemorySecretStore({
connectors: cred ? { [connectorId]: seedCredential(cred, 'api_key') } : {},
secrets: Object.fromEntries(Object.entries(extras ?? {}).map(([name, entry]) => [name, entry.value])),
});
}
// ── Notion Connector ──────────────────────────────────────────────────
@@ -85,7 +76,7 @@ describe('NotionConnector', () => {
it('connect() retrieves token from vault', async () => {
const vault = createMockVault('notion', { value: 'ntn_test123', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('notion');
expect(vault.get).toHaveBeenCalledWith('connector:notion');
});
it('healthCheck() returns connected when API responds OK', async () => {
@@ -193,7 +184,7 @@ describe('ConfluenceConnector', () => {
'connector:confluence:domain': { value: 'mycompany' },
});
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('confluence');
expect(vault.get).toHaveBeenCalledWith('connector:confluence');
expect(vault.get).toHaveBeenCalledWith('connector:confluence:email');
expect(vault.get).toHaveBeenCalledWith('connector:confluence:domain');
});
@@ -314,7 +305,7 @@ describe('ObsidianConnector', () => {
it('connect() retrieves vault path from vault', async () => {
const vault = createMockVault('obsidian', { value: tmpDir, isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('obsidian');
expect(vault.get).toHaveBeenCalledWith('connector:obsidian');
});
it('healthCheck() returns connected when directory exists', async () => {