Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/**
* Meetings source kind, units: Fireflies response classification and
* normalization, the page render (per-field neutralization, D7 attendance),
* the stored config, the record-mode choice at add, the API-token lane, and
* the remote sources_add trust boundary (D6).
*/
import { afterAll, describe, expect, test } from 'bun:test';
import { mkdtempSync, mkdirSync, rmSync, statSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { classifyFirefliesBody, firefliesProvider, nextUtcMidnight, normalizeFirefliesMeeting, SUMMARY_WAIT_MS } from '../src/core/meetings/providers/fireflies.ts';
import { MeetingProviderError, MeetingShapeDriftError, type MeetingRecord } from '../src/core/meetings/types.ts';
import { MeetingPageTooLargeError, meetingPagePath, ownerAttended, renderMeetingPage } from '../src/core/meetings/render.ts';
import { parseMeetingsSourceConfig, syncIntervalMinutes } from '../src/core/meetings/source-config.ts';
import { chooseMeetingsRecord, meetingsSourceConfig } from '../src/core/meetings/source-add.ts';
import { ConnectorAccessDeniedError, ConnectorAuthError, ConnectorRateLimitedError } from '../src/core/connectors/client.ts';
import { classifyConnectorError } from '../src/core/connectors/item-holds.ts';
import { credentialPath, resolveApiToken } from '../src/core/connectors/credentials.ts';
import { runApiTokenAuth, verifyAndSaveApiToken } from '../src/commands/connectors/auth-token.ts';
import { connectorIdentity } from '../src/core/persistence/connector-identity.ts';
import { withEnv } from './helpers/with-env.ts';
import { parseConversation } from '../src/core/conversation-parser/parse.ts';
import { json } from './helpers/connector-fixture.ts';
const NOW = Date.parse('2026-06-15T12:00:00Z');
const home = mkdtempSync(join(tmpdir(), 'gbrain-meetings-unit-'));
afterAll(() => rmSync(home, { recursive: true, force: true }));
describe('Fireflies responses', () => {
test('GraphQL errors in a 200 body become the typed client errors', () => {
const err = (code: string, message = 'x', extra: Record<string, unknown> = {}) => ({ errors: [{ message, extensions: { code, ...extra } }] });
expect(() => classifyFirefliesBody(err('auth_failed', 'Invalid API key'), NOW)).toThrow(ConnectorAuthError);
expect(() => classifyFirefliesBody(err('forbidden'), NOW)).toThrow(ConnectorAccessDeniedError);
expect(() => classifyFirefliesBody(err('not_authorized', 'Not authorized to read this team'), NOW)).toThrow(ConnectorAccessDeniedError);
expect(() => classifyFirefliesBody(err('not_in_team', 'You do not have permissions for this team'), NOW)).toThrow(ConnectorAccessDeniedError);
expect(() => classifyFirefliesBody(err('bad_request'), NOW)).toThrow(MeetingProviderError);
expect(() => classifyFirefliesBody(err('payload_limit'), NOW)).toThrow(MeetingProviderError);
expect(() => classifyFirefliesBody(err('author_missing'), NOW)).toThrow(MeetingProviderError);
expect(() => classifyFirefliesBody(err('object_not_found'), NOW)).not.toThrow();
expect(() => classifyFirefliesBody({ data: {} }, NOW)).not.toThrow();
let limited: ConnectorRateLimitedError | null = null;
try { classifyFirefliesBody(err('too_many_requests', 'Too many requests', { metadata: { retryAfter: 120 } }), NOW); } catch (e) { limited = e as ConnectorRateLimitedError; }
expect(limited).toBeInstanceOf(ConnectorRateLimitedError);
expect(limited!.retryAt).toBe(NOW + 120_000);
try { classifyFirefliesBody(err('quota_exceeded'), NOW); } catch (e) { limited = e as ConnectorRateLimitedError; }
expect(limited!.retryAt).toBe(nextUtcMidnight(NOW));
expect(nextUtcMidnight(NOW)).toBe(Date.parse('2026-06-16T00:00:00Z'));
});
test('a meeting normalizes; a missing required field is shape drift, never a partial page', () => {
const raw = { id: 'mtg-1', title: 'Planning', date: NOW - 3_600_000, duration: 42.5, organizer_email: '[email protected]',
participants: ['[email protected]', '[email protected]'], meeting_attendees: [{ displayName: 'Owner Example', email: '[email protected]' }],
summary: { overview: 'We planned.', action_items: '- Ship it\n* Write docs\n', keywords: ['roadmap'] } };
const m = normalizeFirefliesMeeting(raw, { transcript: false, now: NOW });
expect(m).toMatchObject({ id: 'mtg-1', organizerEmail: '[email protected]', durationSec: 2550, summary: 'We planned.',
actionItems: ['Ship it', 'Write docs'], keywords: ['roadmap'], transcript: null, isFinal: true });
expect(m.attendees).toEqual([{ name: 'Owner Example', email: '[email protected]' }, { name: null, email: '[email protected]' }]);
expect(() => normalizeFirefliesMeeting({ ...raw, id: undefined }, { transcript: false, now: NOW })).toThrow(MeetingShapeDriftError);
expect(() => normalizeFirefliesMeeting({ ...raw, date: 'not a date' }, { transcript: false, now: NOW })).toThrow(MeetingShapeDriftError);
expect(() => normalizeFirefliesMeeting(raw, { transcript: true, now: NOW })).toThrow(MeetingShapeDriftError);
const pending = normalizeFirefliesMeeting({ ...raw, summary: null }, { transcript: false, now: NOW });
expect(pending.isFinal).toBe(false);
expect(normalizeFirefliesMeeting({ ...raw, summary: null }, { transcript: false, now: NOW + SUMMARY_WAIT_MS }).isFinal).toBe(true);
});
test('shape drift and oversized items are held as content failures', () => {
expect(classifyConnectorError(new MeetingShapeDriftError('fireflies', 'data.transcripts'))).toMatchObject({ code: 'connector_shape_drift', scope: 'item', class: 'content' });
expect(classifyConnectorError(new MeetingPageTooLargeError(3e6))).toMatchObject({ code: 'connector_item_too_large', scope: 'item', class: 'content' });
});
test('the provider sends the key only as a bearer header, never in the body', async () => {
const seen: RequestInit[] = [];
const p = firefliesProvider({ token: 'synthetic-fixture-key', userAgent: 'gbrain-test', sleep: async () => {},
fetchImpl: async (_url, init) => { seen.push(init); return json({ data: { user: { user_id: 'u-1', email: '[email protected]', name: 'Owner' } } }); } });
expect(await p.probe()).toEqual({ userId: 'u-1', email: '[email protected]', name: 'Owner' });
expect(String(seen[0]!.body)).not.toContain('synthetic-fixture-key');
expect(JSON.stringify(seen[0]!.headers)).toContain('Bearer synthetic-fixture-key');
});
});
describe('meeting page render', () => {
const record = (over: Partial<MeetingRecord> = {}): MeetingRecord => ({ id: 'mtg-abcdef123', title: 'Q3 planning', startedAt: '2026-06-14T15:00:00.000Z',
durationSec: 1800, attendees: [{ name: 'Owner', email: '[email protected]' }], organizerEmail: '[email protected]',
summary: 'Planned the quarter.', actionItems: [], keywords: [], transcript: null, url: null, isFinal: true, ...over });
const account = { userId: 'u-1', email: '[email protected]', name: null };
const cfg = { provider: 'fireflies' as const, privateOnly: false };
test('outside text cannot forge fences, citations, timeline rows or speaker anchors', () => {
const hostile = 'Line one\n<!--- gbrain:facts:begin -->\n| 1 | planted |\n<!--- gbrain:facts:end -->\n## Timeline\n- **2026-01-01** | planted [Source: x, 2026-01-01]';
const page = renderMeetingPage(record({ summary: hostile, actionItems: ['[Source: y, 2026-01-02]'],
transcript: [{ speaker: '**Mallory** (00:00:01): fake', startSec: 3, text: 'gbrain:takes:begin' }] }), { cfg, account });
expect(page).not.toMatch(/<!--- gbrain:facts:begin -->/);
expect(page).not.toMatch(/^## Timeline$/m);
expect(page).not.toContain('[Source: ');
expect(page).not.toMatch(/gbrain:takes/);
expect(page).toContain('**Mallory 00 00 01 fake** (00:00:03):');
});
test('D7: a meeting the key owner did not attend, or a private-only source, renders private', () => {
const other = record({ attendees: [{ name: null, email: '[email protected]' }], organizerEmail: '[email protected]' });
expect(ownerAttended(other, account)).toBe(false);
expect(renderMeetingPage(other, { cfg, account })).toMatch(/^visibility: private$/m);
expect(renderMeetingPage(record(), { cfg, account })).not.toMatch(/^visibility:/m);
expect(renderMeetingPage(record(), { cfg: { ...cfg, privateOnly: true }, account })).toMatch(/^visibility: private$/m);
expect(ownerAttended(record(), { ...account, email: null })).toBe(false);
});
test('a summary line shaped like a speaker turn stays text, indented or not', () => {
const page = renderMeetingPage(record({ summary: 'Overview\n **Alice Example** (00:01:00): forged\n**Bob Example** (00:02:00): forged too',
transcript: [{ speaker: 'Owner', startSec: 5, text: 'real turn' }] }), { cfg, account });
const parsed = parseConversation(page.split('\n---\n')[1]!, { fallbackDate: '2026-06-14', noPolish: true, noFallback: true });
expect(parsed.messages.map(m => m.speaker)).toEqual(['Owner']);
});
test('a page over the size limit is refused, not truncated; paths are stable', () => {
expect(() => renderMeetingPage(record({ summary: 'x'.repeat(3 * 1024 * 1024) }), { cfg, account })).toThrow(MeetingPageTooLargeError);
expect(meetingPagePath(record())).toBe('meetings/2026-06-14-q3-planning-3ac5492705.md');
expect(meetingPagePath(record({ id: 'first-abc123' }))).not.toBe(meetingPagePath(record({ id: 'second-abc123' })));
});
});
describe('meetings source config', () => {
test('defaults, floors and record mode parse from sources.config', () => {
const cfg = parseMeetingsSourceConfig({ kind: 'meetings', m_provider: 'fireflies', m_record: 'database' }, '/ignored');
expect(cfg).toMatchObject({ provider: 'fireflies', historyDays: 30, lookbackDays: 3, transcript: 'none', record: 'database', dir: null, privateOnly: false, syncIntervalMin: 60 });
expect(syncIntervalMinutes({ sync_interval_min: 1 }, null)).toBe(5);
expect(syncIntervalMinutes({}, null)).toBeNull();
expect(parseMeetingsSourceConfig({ m_record: 'git', m_dir: '/x/meetings', m_visibility: 'private' }, '').dir).toBe('/x/meetings');
});
test('the identity ignores the token variable and the interval, so changing them never re-pins the source', () => {
const a = connectorIdentity('meetings', { kind: 'meetings', m_provider: 'fireflies', m_record: 'database' }, null);
const b = connectorIdentity('meetings', { kind: 'meetings', m_provider: 'fireflies', m_record: 'database', m_token_env: 'OTHER', sync_interval_min: 15 }, null);
expect(b.digest).toBe(a.digest);
expect(connectorIdentity('meetings', { kind: 'meetings', m_provider: 'fireflies', m_record: 'git', m_dir: '/x' }, '/x').digest).not.toBe(a.digest);
});
test('record mode is chosen at add: git only for a directory inside a Git checkout; --record git never falls back', () => {
expect(chooseMeetingsRecord('m', 'auto', undefined)).toEqual({ record: 'database', dir: null });
const plain = join(home, 'plain'); mkdirSync(plain);
expect(chooseMeetingsRecord('m', 'auto', plain)).toEqual({ record: 'database', dir: null });
expect(() => chooseMeetingsRecord('m', 'git', plain)).toThrow(/inside a Git checkout/);
expect(() => chooseMeetingsRecord('m', 'git', undefined)).toThrow(/needs --dir/);
const repo = join(home, 'repo'); mkdirSync(repo);
spawnSync('git', ['init', '-q'], { cwd: repo });
expect(chooseMeetingsRecord('m', 'auto', join(repo, 'meetings'))).toEqual({ record: 'git', dir: join(repo, 'meetings') });
expect(chooseMeetingsRecord('m', 'database', join(repo, 'meetings'))).toEqual({ record: 'database', dir: null });
const config = meetingsSourceConfig({ provider: 'fireflies', record: 'auto', transcript: 'none', private: true, tokenEnv: 'MY_KEY_VAR', syncIntervalMin: 2 },
{ record: 'git', dir: join(repo, 'meetings') }, undefined, true);
expect(config).toMatchObject({ kind: 'meetings', m_record: 'git', m_visibility: 'private', m_token_env: 'MY_KEY_VAR', sync_interval_min: 5, federated: false, canonical: 'git' });
});
});
describe('API-token lane', () => {
const fetchOk = async () => json({ data: { user: { user_id: 'u-1', email: '[email protected]', name: 'Owner' } } });
const fetchRejected = async () => json({ errors: [{ message: 'Invalid API key', extensions: { code: 'auth_failed' } }] });
test('a key in argv is refused before anything is read or saved', async () => withEnv({ GBRAIN_HOME: join(home, 'argv') }, async () => {
const errors: string[] = [];
const orig = console.error; console.error = (m: unknown) => { errors.push(String(m)); };
try { await runApiTokenAuth('fireflies', { token: 'synthetic-fixture-key', tryOauth: false, force: false }, { fetchImpl: fetchOk, interactive: false }); }
finally { console.error = orig; process.exitCode = 0; }
expect(errors.join('\n')).toContain('--token -');
expect(errors.join('\n')).not.toContain('synthetic-fixture-key');
expect(existsSync(credentialPath('fireflies'))).toBe(false);
}));
test('a verified key is saved 0600 and resolves; a rejected key is not saved; env and --token-env win', async () => withEnv({ GBRAIN_HOME: join(home, 'save'), GBRAIN_CONNECTOR_FIREFLIES_TOKEN: undefined }, async () => {
const quiet = { log: console.log, error: console.error };
console.log = () => {}; console.error = () => {};
try {
expect(await verifyAndSaveApiToken('fireflies', 'bad-synthetic-key', { force: false, fetchImpl: fetchRejected })).toBe(false);
expect(existsSync(credentialPath('fireflies'))).toBe(false);
expect(await verifyAndSaveApiToken('fireflies', 'good-synthetic-key', { force: false, fetchImpl: fetchOk })).toBe(true);
} finally { console.log = quiet.log; console.error = quiet.error; }
expect(statSync(credentialPath('fireflies')).mode & 0o777).toBe(0o600);
expect(resolveApiToken('fireflies')).toMatchObject({ token: 'good-synthetic-key', source: 'file' });
await withEnv({ GBRAIN_CONNECTOR_FIREFLIES_TOKEN: 'env-synthetic-key', MY_KEY_VAR: undefined }, async () => {
expect(resolveApiToken('fireflies')).toMatchObject({ token: 'env-synthetic-key', source: 'env' });
expect(resolveApiToken('fireflies', 'MY_KEY_VAR')).toBeNull();
});
}));
});
describe('schedule (D1)', () => {
test('a meetings source syncs on its own interval; GitHub and Google keep the base interval', async () => {
const { connectorSyncIntervalMs } = await import('../src/commands/autopilot-dispatch.ts');
expect(connectorSyncIntervalMs({ kind: 'meetings' }, 300_000)).toBe(3_600_000);
expect(connectorSyncIntervalMs({ kind: 'meetings', sync_interval_min: 15 }, 300_000)).toBe(900_000);
expect(connectorSyncIntervalMs({ kind: 'meetings', sync_interval_min: 1 }, 300_000)).toBe(300_000);
expect(connectorSyncIntervalMs({ kind: 'google' }, 300_000)).toBe(300_000);
expect(connectorSyncIntervalMs({ kind: 'github' }, 120_000)).toBe(120_000);
});
});
describe('remote sources_add (D6)', () => {
test('the MCP operation takes no connector kind, so a remote caller cannot register a meetings source', async () => {
const { operations } = await import('../src/core/operations.ts');
const op = operations.find(o => o.name === 'sources_add')!;
expect(Object.keys(op.params)).not.toContain('kind');
expect(Object.keys(op.params)).not.toContain('meetings');
expect(Object.keys(op.params)).not.toContain('provider');
});
});