Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
4.7 KB
Raw Blame
/**
 * normalizeSourceInput / normalizeFederatedReadInput contract.
 *
 * The `/admin/api/register-client` HTTP endpoint historically hardcoded
 * source_id='default' (and federated_read=[source_id]) — only the CLI
 * (`--source` / `--federated-read`) could bind a client to a non-default
 * brain source. These two normalizers let the HTTP endpoint accept the same
 * inputs from the request body while preserving the historical default when
 * the fields are omitted.
 *
 * Hermetic — pure-function unit tests, no engine, no HTTP.
 */

import { describe, test, expect } from 'bun:test';
import { normalizeSourceInput, normalizeFederatedReadInput } from '../src/core/source-id.ts';

describe('normalizeSourceInput', () => {
  test('undefined → "default" (backward compat)', () => {
    expect(normalizeSourceInput(undefined)).toBe('default');
  });

  test('null → "default" (backward compat)', () => {
    expect(normalizeSourceInput(null)).toBe('default');
  });

  test('valid source_id passes through', () => {
    expect(normalizeSourceInput('mind-agent-brain')).toBe('mind-agent-brain');
  });

  test('single-char source_id is valid', () => {
    expect(normalizeSourceInput('a')).toBe('a');
  });

  test('invalid source_id (underscore) throws', () => {
    expect(() => normalizeSourceInput('mind_agent_brain')).toThrow(/Invalid source_id/);
  });

  test('invalid source_id (uppercase) throws', () => {
    expect(() => normalizeSourceInput('Mind')).toThrow(/Invalid source_id/);
  });

  test('invalid source_id (edge hyphen) throws', () => {
    expect(() => normalizeSourceInput('-mind')).toThrow(/Invalid source_id/);
  });

  test('non-string (number) throws', () => {
    expect(() => normalizeSourceInput(42)).toThrow(/Invalid source_id/);
  });
});

describe('register-client route wiring (structural)', () => {
  test('normalized source + federatedRead reach registerClientManual in the right positions', () => {
    // The unit tests above prove the normalizers; this pins the ROUTE —
    // a transposition of the two new positional args (or a regression to
    // the hardcoded 'default') would pass every unit test and still ship
    // clients bound to the wrong source.
    const { readFileSync } = require('fs');
    const src = readFileSync(new URL('../src/commands/serve-http-registration.ts', import.meta.url), 'utf-8');
    expect(src).toContain('sourceId = normalizeSourceInput(req.body.sourceId ?? source)');
    expect(src).toContain('federatedReadIds = normalizeFederatedReadInput(federatedRead)');
    // cathedral-6: the route composes registerScopedClient (the CLI's core)
    // instead of calling registerClientManual directly — now on the
    // tx-scoped sql handle (dup-check + INSERT are one transaction). The
    // transposition hazard this pin exists for is now a NAMED-FIELD hazard —
    // pin that the normalized values land on the right keys of the
    // parsed-args object.
    // Profile requests pass through the validated preview before registration;
    // the explicit sources must survive both named-field boundaries.
    expect(src).toContain('previewNewAdminGrant(engine, name, grantRequest.patch, { sourceId, federatedRead: federatedReadIds, scopes: scopeString })');
    expect(src).toMatch(/registerScopedClient\(txSql,\s*name,\s*\{[\s\S]*?scopes:\s*preview.scopes.join\(' '\),[\s\S]*?sourceId:\s*preview.sourceId!,[\s\S]*?federatedRead:\s*preview.federatedRead,[\s\S]*?tokenEndpointAuthMethod:\s*validatedAuthMethod[\s\S]*?\}/);
    expect(src).toContain('grant: grantRequest.patch');
  });
});

describe('normalizeFederatedReadInput', () => {
  test('undefined → undefined (let registerClientManual default to [sourceId])', () => {
    expect(normalizeFederatedReadInput(undefined)).toBeUndefined();
  });

  test('null → undefined', () => {
    expect(normalizeFederatedReadInput(null)).toBeUndefined();
  });

  test('valid single-element array passes through', () => {
    expect(normalizeFederatedReadInput(['mind-agent-brain'])).toEqual(['mind-agent-brain']);
  });

  test('valid multi-element array passes through (read across both sources)', () => {
    expect(normalizeFederatedReadInput(['default', 'mind-agent-brain'])).toEqual([
      'default',
      'mind-agent-brain',
    ]);
  });

  test('empty array throws (ambiguous — omit the field instead)', () => {
    expect(() => normalizeFederatedReadInput([])).toThrow(/Invalid federatedRead/);
  });

  test('non-array (string) throws', () => {
    expect(() => normalizeFederatedReadInput('default')).toThrow(/Invalid federatedRead/);
  });

  test('array with an invalid source_id element throws', () => {
    expect(() => normalizeFederatedReadInput(['default', 'bad_id'])).toThrow(/Invalid source_id/);
  });
});