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:
67
packages/agent/tests/helpers/memory-secret-store.ts
Normal file
67
packages/agent/tests/helpers/memory-secret-store.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { vi, type Mock } from 'vitest';
|
||||
import type { SecretEntry, SecretStore } from '@waggle/core';
|
||||
import {
|
||||
setConnectorCredential,
|
||||
type ConnectorCredentialInput,
|
||||
} from '../../src/connectors/connector-credentials.js';
|
||||
|
||||
export type MockSecretStore = { [K in keyof SecretStore]: Mock<SecretStore[K]> };
|
||||
|
||||
/** An expiry in the past, for credentials that should read as expired. */
|
||||
export const EXPIRED_AT = '2020-01-01T00:00:00.000Z';
|
||||
|
||||
/** A seed credential from the `{ value, isExpired }` shape the connector tests use. */
|
||||
export function seedCredential(
|
||||
cred: { value: string; isExpired: boolean },
|
||||
type: ConnectorCredentialInput['type'] = 'bearer',
|
||||
): ConnectorCredentialInput {
|
||||
return { type, value: cred.value, expiresAt: cred.isExpired ? EXPIRED_AT : undefined };
|
||||
}
|
||||
|
||||
export interface SecretStoreSeed {
|
||||
/** Connector credentials by connector id, stored with setConnectorCredential. */
|
||||
connectors?: Record<string, ConnectorCredentialInput>;
|
||||
/** Plain secrets by name, such as `connector:jira:email`. */
|
||||
secrets?: Record<string, string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* A Map-backed SecretStore for connector tests. Every method is a vitest spy.
|
||||
* The seed is written before the spies exist, so it does not show up in their calls.
|
||||
*/
|
||||
export function createMemorySecretStore(seed: SecretStoreSeed = {}): MockSecretStore {
|
||||
const entries = new Map<string, SecretEntry>();
|
||||
const store: SecretStore = {
|
||||
get: name => entries.get(name) ?? null,
|
||||
set: (name, value, metadata) => {
|
||||
entries.set(name, { name, value, metadata, updatedAt: new Date().toISOString() });
|
||||
},
|
||||
setAsync: async (name, value, metadata) => store.set(name, value, metadata),
|
||||
writeBatch: (batch) => {
|
||||
for (const name of batch.delete ?? []) entries.delete(name);
|
||||
for (const { name, value, metadata } of batch.set ?? []) store.set(name, value, metadata);
|
||||
},
|
||||
delete: name => entries.delete(name),
|
||||
deleteAsync: async name => store.delete(name),
|
||||
list: () => [...entries.values()].map(({ name, metadata, updatedAt }) => ({ name, metadata, updatedAt })),
|
||||
has: name => entries.has(name),
|
||||
};
|
||||
|
||||
for (const [connectorId, credential] of Object.entries(seed.connectors ?? {})) {
|
||||
setConnectorCredential(store, connectorId, credential);
|
||||
}
|
||||
for (const [name, value] of Object.entries(seed.secrets ?? {})) {
|
||||
store.set(name, value);
|
||||
}
|
||||
|
||||
return {
|
||||
get: vi.fn(store.get),
|
||||
set: vi.fn(store.set),
|
||||
setAsync: vi.fn(store.setAsync),
|
||||
writeBatch: vi.fn(store.writeBatch),
|
||||
delete: vi.fn(store.delete),
|
||||
deleteAsync: vi.fn(store.deleteAsync),
|
||||
list: vi.fn(store.list),
|
||||
has: vi.fn(store.has),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user