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:
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { GoogleCalendarConnector } from '../../src/connectors/gcal-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { getConnectorCredential } from '../../src/connectors/connector-credentials.js';
|
||||
import { createMemorySecretStore, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(opts?: {
|
||||
accessToken?: string;
|
||||
@@ -8,32 +9,23 @@ function createMockVault(opts?: {
|
||||
expiresAt?: string;
|
||||
clientId?: string;
|
||||
clientSecret?: string;
|
||||
}): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === 'gcal' && opts?.accessToken) {
|
||||
return {
|
||||
value: opts.accessToken,
|
||||
}): MockSecretStore {
|
||||
const secrets: Record<string, string> = {};
|
||||
if (opts?.clientId) secrets['connector:gcal:client_id'] = opts.clientId;
|
||||
if (opts?.clientSecret) secrets['connector:gcal:client_secret'] = opts.clientSecret;
|
||||
return createMemorySecretStore({
|
||||
connectors: opts?.accessToken
|
||||
? {
|
||||
gcal: {
|
||||
type: 'oauth2',
|
||||
isExpired: false,
|
||||
value: opts.accessToken,
|
||||
refreshToken: opts.refreshToken,
|
||||
expiresAt: opts.expiresAt,
|
||||
};
|
||||
},
|
||||
}
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn((name: string) => {
|
||||
if (name === 'connector:gcal:client_id' && opts?.clientId) return { value: opts.clientId };
|
||||
if (name === 'connector:gcal:client_secret' && opts?.clientSecret) return { value: opts.clientSecret };
|
||||
return 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;
|
||||
: {},
|
||||
secrets,
|
||||
});
|
||||
}
|
||||
|
||||
describe('GoogleCalendarConnector', () => {
|
||||
@@ -63,7 +55,7 @@ describe('GoogleCalendarConnector', () => {
|
||||
expiresAt: new Date(Date.now() + 3600000).toISOString(),
|
||||
});
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gcal');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:gcal');
|
||||
});
|
||||
|
||||
it('healthCheck() validates access token', async () => {
|
||||
@@ -104,10 +96,10 @@ describe('GoogleCalendarConnector', () => {
|
||||
const health = await connector.healthCheck();
|
||||
expect(health.status).toBe('connected');
|
||||
// Verify tokens were stored back in vault
|
||||
expect(vault.setConnectorCredential).toHaveBeenCalledWith('gcal', expect.objectContaining({
|
||||
expect(getConnectorCredential(vault, 'gcal')).toMatchObject({
|
||||
type: 'oauth2',
|
||||
value: 'ya29.refreshed',
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
it('execute(list_events) returns events', async () => {
|
||||
@@ -205,10 +197,10 @@ describe('OAuth2 token refresh', () => {
|
||||
|
||||
await connector.execute('list_events', {});
|
||||
|
||||
expect(vault.setConnectorCredential).toHaveBeenCalledWith('gcal', expect.objectContaining({
|
||||
expect(getConnectorCredential(vault, 'gcal')).toMatchObject({
|
||||
value: 'ya29.new',
|
||||
refreshToken: 'rt_new',
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when refresh token is invalid', async () => {
|
||||
|
||||
Reference in New Issue
Block a user