Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/**
* Tests for parseRegisterClientArgs() in src/commands/auth.ts.
*
* v0.41.3 (T3): the pre-fix CLI parser used `args.indexOf('--flag')` which
* silently took only the FIRST occurrence of a flag. That broke
* `--redirect-uri A --redirect-uri B` (only A made it through). The rewrite
* loops over argv and accumulates repeatable flags into arrays.
*
* Pure function — no DB, no fetch. The full register-client flow against
* a live PGLite OAuth provider is covered in test/oauth.test.ts.
*/
import { describe, test, expect } from 'bun:test';
import { parseRegisterClientArgs } from '../src/commands/auth.ts';
import { assertAllowedScopes, parseScopeString } from '../src/core/scope.ts';
describe('parseRegisterClientArgs', () => {
test('empty args → all defaults', () => {
const out = parseRegisterClientArgs([]);
expect(out.grantTypes).toEqual(['client_credentials']);
expect(out.scopes).toBe('read');
expect(out.sourceId).toBe('default');
expect(out.federatedRead).toBeUndefined();
expect(out.redirectUris).toEqual([]);
expect(out.tokenEndpointAuthMethod).toBeUndefined();
expect(out.boundTools).toBeUndefined();
expect(out.boundSourceId).toBeUndefined();
expect(out.boundBrainId).toBeUndefined();
expect(out.boundSlugPrefixes).toBeUndefined();
expect(out.boundMaxConcurrent).toBeUndefined();
expect(out.budgetUsdPerDay).toBeUndefined();
});
test('--grant-types comma-separated → array', () => {
const out = parseRegisterClientArgs(['--grant-types', 'authorization_code,refresh_token']);
expect(out.grantTypes).toEqual(['authorization_code', 'refresh_token']);
});
test('--scopes preserves the whitespace-joined string', () => {
const out = parseRegisterClientArgs(['--scopes', 'read write']);
expect(out.scopes).toBe('read write');
});
// init.ts's own `gbrain auth register-client` hint (line ~730) recommends
// `--scopes read,write,admin`, but the parser previously only split on
// whitespace, so the comma-joined string fell through as a single
// unrecognized token and registerClientManual's assertAllowedScopes
// rejected it with `Unknown scope "read,write,admin"` — self-contradicting
// the hint it printed. These pin the comma form as accepted, alongside the
// pre-existing space form, without changing the shared OAuth-wire-format
// parseScopeString (RFC 6749 space-delimited) used for DCR/refresh/request
// scope parsing.
describe('--scopes comma-separated (matches init.ts hint)', () => {
test('comma-separated → normalized to the space-joined wire form', () => {
const out = parseRegisterClientArgs(['--scopes', 'read,write,admin']);
expect(out.scopes).toBe('read write admin');
});
test('mixed comma+space form normalizes the same way', () => {
const out = parseRegisterClientArgs(['--scopes', 'read, write ,admin']);
expect(out.scopes).toBe('read write admin');
});
test('single scope (no separators) is unaffected', () => {
const out = parseRegisterClientArgs(['--scopes', 'read']);
expect(out.scopes).toBe('read');
});
test('comma, space, and mixed forms all parse to the identical scope set downstream', () => {
const comma = parseRegisterClientArgs(['--scopes', 'read,write,admin']).scopes;
const spaced = parseRegisterClientArgs(['--scopes', 'read write admin']).scopes;
const mixed = parseRegisterClientArgs(['--scopes', 'read, write ,admin']).scopes;
const expected = ['read', 'write', 'admin'];
expect(parseScopeString(comma)).toEqual(expected);
expect(parseScopeString(spaced)).toEqual(expected);
expect(parseScopeString(mixed)).toEqual(expected);
// Control: all three forms also clear the registration-time allowlist
// gate that init.ts's hint promises works.
expect(() => assertAllowedScopes(parseScopeString(comma))).not.toThrow();
expect(() => assertAllowedScopes(parseScopeString(spaced))).not.toThrow();
expect(() => assertAllowedScopes(parseScopeString(mixed))).not.toThrow();
});
// Control: comma-splitting must not smuggle a genuinely unknown scope
// past the registration-time allowlist gate.
test('a genuinely unknown scope in comma form is still rejected downstream', () => {
const out = parseRegisterClientArgs(['--scopes', 'read,flying-unicorn']);
expect(out.scopes).toBe('read flying-unicorn');
expect(() => assertAllowedScopes(parseScopeString(out.scopes)))
.toThrow(/Unknown scope "flying-unicorn"/);
});
// Codex review finding (round 1): separator-only input (e.g. "," or
// ",,,") split to zero tokens under the comma/whitespace regex, which
// collapsed to `''` and would have passed assertAllowedScopes
// vacuously downstream (empty scope list silently accepted).
//
// Codex review finding (round 2, P1): an initial fix that forwarded the
// raw string only for comma-only input left whitespace-only (`" "`)
// and empty-string (`""`) inputs unguarded — those also normalize to
// zero tokens under the same regex (whitespace is a supported
// separator too), so the same silent-empty-scope-set hole remained for
// them. The parser now rejects ANY input that normalizes to zero
// tokens, uniformly, with a clear CLI usage error.
test('zero-token --scopes input (commas, whitespace, or empty) is rejected at parse time', () => {
for (const raw of [',', ',,,', ' , ', ' ', '']) {
expect(() => parseRegisterClientArgs(['--scopes', raw]))
.toThrow(/--scopes requires at least one scope/);
}
});
});
test('--source scopes the OAuth client', () => {
const out = parseRegisterClientArgs(['--source', 'dept-x']);
expect(out.sourceId).toBe('dept-x');
});
test('--federated-read comma-separated → array', () => {
const out = parseRegisterClientArgs(['--federated-read', 'dept-x,wecare,shared']);
expect(out.federatedRead).toEqual(['dept-x', 'wecare', 'shared']);
});
// T3 REGRESSION: pre-fix indexOf parser only took the first --redirect-uri
describe('--redirect-uri (REPEATABLE — T3 regression)', () => {
test('single --redirect-uri → single-element array', () => {
const out = parseRegisterClientArgs(['--redirect-uri', 'https://claude.ai/api/mcp/auth_callback']);
expect(out.redirectUris).toEqual(['https://claude.ai/api/mcp/auth_callback']);
});
test('two --redirect-uri → both preserved', () => {
// THE REGRESSION: pre-fix this returned only the first URI.
const out = parseRegisterClientArgs([
'--redirect-uri', 'https://claude.ai/api/mcp/auth_callback',
'--redirect-uri', 'https://claude.com/api/mcp/auth_callback',
]);
expect(out.redirectUris).toEqual([
'https://claude.ai/api/mcp/auth_callback',
'https://claude.com/api/mcp/auth_callback',
]);
});
test('three --redirect-uri → all three preserved', () => {
const out = parseRegisterClientArgs([
'--redirect-uri', 'https://a.example/cb',
'--redirect-uri', 'https://b.example/cb',
'--redirect-uri', 'https://c.example/cb',
]);
expect(out.redirectUris).toHaveLength(3);
});
});
describe('--token-endpoint-auth-method', () => {
test('omitted → undefined (provider applies RFC 7591 default)', () => {
const out = parseRegisterClientArgs([]);
expect(out.tokenEndpointAuthMethod).toBeUndefined();
});
test('"none" → "none" (public PKCE client)', () => {
const out = parseRegisterClientArgs(['--token-endpoint-auth-method', 'none']);
expect(out.tokenEndpointAuthMethod).toBe('none');
});
test('"client_secret_post" → "client_secret_post"', () => {
const out = parseRegisterClientArgs(['--token-endpoint-auth-method', 'client_secret_post']);
expect(out.tokenEndpointAuthMethod).toBe('client_secret_post');
});
test('"client_secret_basic" → "client_secret_basic"', () => {
const out = parseRegisterClientArgs(['--token-endpoint-auth-method', 'client_secret_basic']);
expect(out.tokenEndpointAuthMethod).toBe('client_secret_basic');
});
test('CLI parser does NOT validate the value — validator is on registerClientManual', () => {
// Parser is shape-only. The validator runs at the registration boundary
// so the same gate applies to CLI / admin / DCR. Putting validation in
// the parser would mean DCR'd ApiClient strings bypass the same gate.
const out = parseRegisterClientArgs(['--token-endpoint-auth-method', 'frobnicate']);
expect(out.tokenEndpointAuthMethod).toBe('frobnicate');
});
});
describe('combination flows (worked examples from SECURITY.md)', () => {
test('claude.ai pre-registration (confidential, two redirect URIs)', () => {
const out = parseRegisterClientArgs([
'--grant-types', 'authorization_code,refresh_token',
'--scopes', 'read write',
'--redirect-uri', 'https://claude.ai/api/mcp/auth_callback',
'--redirect-uri', 'https://claude.com/api/mcp/auth_callback',
]);
expect(out.grantTypes).toEqual(['authorization_code', 'refresh_token']);
expect(out.scopes).toBe('read write');
expect(out.redirectUris).toHaveLength(2);
expect(out.tokenEndpointAuthMethod).toBeUndefined();
});
test('ChatGPT pre-registration (PKCE public client)', () => {
const out = parseRegisterClientArgs([
'--grant-types', 'authorization_code,refresh_token',
'--scopes', 'read write',
'--redirect-uri', 'https://chatgpt.com/connector/oauth/HASH',
'--token-endpoint-auth-method', 'none',
]);
expect(out.grantTypes).toEqual(['authorization_code', 'refresh_token']);
expect(out.redirectUris).toEqual(['https://chatgpt.com/connector/oauth/HASH']);
expect(out.tokenEndpointAuthMethod).toBe('none');
});
test('--redirect-uri without --grant-types → auto-infers authorization_code,refresh_token', () => {
// Operator ergonomics: --redirect-uri without grant_types implies the
// browser-OAuth flow; redundantly passing --grant-types is footgun.
const out = parseRegisterClientArgs([
'--redirect-uri', 'https://claude.ai/api/mcp/auth_callback',
]);
expect(out.grantTypes).toEqual(['authorization_code', 'refresh_token']);
});
test('--redirect-uri + explicit --grant-types keeps the explicit set', () => {
const out = parseRegisterClientArgs([
'--grant-types', 'authorization_code', // no refresh
'--redirect-uri', 'https://example.test/cb',
]);
expect(out.grantTypes).toEqual(['authorization_code']);
});
});
describe('submit_agent binding flags', () => {
test('parses register-time submit_agent bindings', () => {
const out = parseRegisterClientArgs([
'--scopes', 'read agent',
'--bound-tools', 'search, get_page,put_page',
'--bound-source', 'dept-x',
'--bound-brain', 'company-brain',
'--bound-slug-prefixes', 'wiki/agents/alice/,notes/',
'--bound-max-concurrent', '3',
'--budget-usd-per-day', '12.50',
]);
expect(out.boundTools).toEqual(['search', 'get_page', 'put_page']);
expect(out.boundSourceId).toBe('dept-x');
expect(out.boundBrainId).toBe('company-brain');
expect(out.boundSlugPrefixes).toEqual(['wiki/agents/alice/', 'notes/']);
expect(out.boundMaxConcurrent).toBe(3);
expect(out.budgetUsdPerDay).toBe('12.50');
});
});
describe('error cases', () => {
test('--redirect-uri without value → throws', () => {
expect(() => parseRegisterClientArgs(['--redirect-uri'])).toThrow(/requires a value/);
});
test('--redirect-uri followed by another flag → throws (no greedy consume)', () => {
expect(() => parseRegisterClientArgs(['--redirect-uri', '--scopes', 'read'])).toThrow(/requires a value/);
});
test('unknown --flag throws', () => {
expect(() => parseRegisterClientArgs(['--frobnicate', 'value'])).toThrow(/Unknown flag/);
});
test('--bound-max-concurrent requires a positive integer', () => {
expect(() => parseRegisterClientArgs(['--bound-max-concurrent', '0'])).toThrow(/positive integer/);
expect(() => parseRegisterClientArgs(['--bound-max-concurrent', '1.5'])).toThrow(/positive integer/);
});
test('--budget-usd-per-day requires a currency-shaped decimal', () => {
expect(() => parseRegisterClientArgs(['--budget-usd-per-day', '1.234'])).toThrow(/non-negative decimal/);
expect(() => parseRegisterClientArgs(['--budget-usd-per-day', 'abc'])).toThrow(/non-negative decimal/);
});
});
});
describe('--token-ttl (cathedral-6 T2)', () => {
const { TOKEN_TTL_MIN_SECONDS, TOKEN_TTL_MAX_SECONDS } = require('../src/commands/auth.ts');
test('accepts the bounds and passes the value through', () => {
expect(parseRegisterClientArgs(['--token-ttl', String(TOKEN_TTL_MIN_SECONDS)]).tokenTtlSeconds).toBe(60);
expect(parseRegisterClientArgs(['--token-ttl', String(TOKEN_TTL_MAX_SECONDS)]).tokenTtlSeconds).toBe(7_776_000);
expect(parseRegisterClientArgs(['--token-ttl', '2592000']).tokenTtlSeconds).toBe(2_592_000);
});
test('omitted flag leaves tokenTtlSeconds undefined (server default applies)', () => {
expect(parseRegisterClientArgs([]).tokenTtlSeconds).toBeUndefined();
});
test.each([['0'], ['-1'], ['abc'], ['10000000'], ['59'], ['3600.5']])(
'rejects %s with the range in the message',
(raw) => {
expect(() => parseRegisterClientArgs(['--token-ttl', raw])).toThrow(/between 60 and 7776000/);
},
);
test('requires a value', () => {
expect(() => parseRegisterClientArgs(['--token-ttl'])).toThrow(/requires a value/);
});
});