Files
waggle-os/packages/agent/tests/connectors/github-connector.test.ts
shumeiko.it 3528a242f6 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.
2026-10-24 17:35:36 +02:00

101 lines
3.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { GitHubConnector } from '../../src/connectors/github-connector.js';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(cred?: { value: string; isExpired: boolean }): MockSecretStore {
return createMemorySecretStore({ connectors: cred ? { github: seedCredential(cred) } : {} });
}
describe('GitHubConnector', () => {
let connector: GitHubConnector;
let originalFetch: typeof globalThis.fetch;
beforeEach(() => {
connector = new GitHubConnector();
originalFetch = globalThis.fetch;
});
afterEach(() => {
globalThis.fetch = originalFetch;
});
it('implements WaggleConnector interface', () => {
expect(connector.id).toBe('github');
expect(connector.name).toBe('GitHub');
expect(connector.authType).toBe('bearer');
expect(connector.actions.length).toBe(7);
});
it('connect() retrieves token from vault', async () => {
const vault = createMockVault({ value: 'ghp_test123', isExpired: false });
await connector.connect(vault);
expect(vault.get).toHaveBeenCalledWith('connector:github');
});
it('healthCheck() returns connected when API responds OK', async () => {
const vault = createMockVault({ value: 'ghp_test123', isExpired: false });
await connector.connect(vault);
globalThis.fetch = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ login: 'user' }) }) as unknown as typeof fetch;
const health = await connector.healthCheck();
expect(health.status).toBe('connected');
expect(health.id).toBe('github');
});
it('healthCheck() returns error when API fails', async () => {
const vault = createMockVault({ value: 'ghp_test123', isExpired: false });
await connector.connect(vault);
globalThis.fetch = vi.fn().mockResolvedValue({ ok: false, status: 401, text: async () => 'Unauthorized' }) as unknown as typeof fetch;
const health = await connector.healthCheck();
expect(health.status).toBe('error');
expect(health.error).toContain('401');
});
it('execute(list_repos) returns repo list', async () => {
const vault = createMockVault({ value: 'ghp_test123', isExpired: false });
await connector.connect(vault);
const mockRepos = [{ name: 'waggle', full_name: 'user/waggle' }];
globalThis.fetch = vi.fn().mockResolvedValue({ ok: true, json: async () => mockRepos }) as unknown as typeof fetch;
const result = await connector.execute('list_repos', { per_page: 10 });
expect(result.success).toBe(true);
expect(result.data).toEqual(mockRepos);
});
it('execute(create_issue) creates issue', async () => {
const vault = createMockVault({ value: 'ghp_test123', isExpired: false });
await connector.connect(vault);
const mockIssue = { number: 42, title: 'Bug report' };
globalThis.fetch = vi.fn().mockResolvedValue({ ok: true, json: async () => mockIssue }) as unknown as typeof fetch;
const result = await connector.execute('create_issue', {
owner: 'user', repo: 'waggle', title: 'Bug report',
});
expect(result.success).toBe(true);
expect(result.data).toEqual(mockIssue);
});
it('execute() returns error for unknown action', async () => {
const vault = createMockVault({ value: 'ghp_test123', isExpired: false });
await connector.connect(vault);
const result = await connector.execute('unknown_action', {});
expect(result.success).toBe(false);
expect(result.error).toContain('Unknown action');
});
it('toDefinition() maps correctly', () => {
const def = connector.toDefinition('connected');
expect(def.id).toBe('github');
expect(def.tools).toContain('connector_github_list_repos');
expect(def.tools).toContain('connector_github_create_issue');
expect(def.tools).toHaveLength(7);
expect(def.actions).toHaveLength(7);
});
});