Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

7.4 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import { parameterize, type ParamSubstitution } from './parameterize';

const EXPORTED_YAML = `room:
  name: local-deploy
  description: Local deployment room
  channel_type: channel_public
  read_visibility: public
  write_visibility: public
  agents:
  - agent-alpha
  - agent-beta
`;

describe('parameterize', () => {
  it('returns the input unchanged when there are no substitutions', () => {
    expect(parameterize(EXPORTED_YAML, [])).toBe(EXPORTED_YAML);
  });

  it('replaces the room name and adds a params block', () => {
    const result = parameterize(EXPORTED_YAML, [{ key: 'name', value: 'local-deploy' }]);
    expect(result).toContain("name: '{name}'");
    expect(result).toContain('params:\n');
    expect(result).toContain('    default: local-deploy');
    expect(result).toContain('    type: string');
    expect(result).toMatch(/^params:/m);
    expect(result).toMatch(/^room:/m);
  });

  it('replaces agent names in the sequence', () => {
    const result = parameterize(EXPORTED_YAML, [{ key: 'agent_a', value: 'agent-alpha' }]);
    expect(result).toContain("- '{agent_a}'");
    expect(result).not.toContain('- agent-alpha');
  });

  it('handles multiple substitutions', () => {
    const subs: ParamSubstitution[] = [
      { key: 'name', value: 'local-deploy' },
      { key: 'agent_a', value: 'agent-alpha' },
      { key: 'agent_b', value: 'agent-beta' },
    ];
    const result = parameterize(EXPORTED_YAML, subs);
    expect(result).toContain("name: '{name}'");
    expect(result).toContain("- '{agent_a}'");
    expect(result).toContain("- '{agent_b}'");
    expect(result).toContain('  name:\n    type: string\n    default: local-deploy');
    expect(result).toContain('  agent_a:\n    type: string\n    default: agent-alpha');
    expect(result).toContain('  agent_b:\n    type: string\n    default: agent-beta');
  });

  it('includes description when provided', () => {
    const result = parameterize(EXPORTED_YAML, [
      { key: 'name', value: 'local-deploy', description: 'The room name' },
    ]);
    expect(result).toContain('    description: The room name');
  });

  it('replaces longer values first to avoid partial matches', () => {
    const yaml = `room:
  name: agent-alpha-room
  agents:
  - agent-alpha
`;
    const result = parameterize(yaml, [
      { key: 'room_name', value: 'agent-alpha-room' },
      { key: 'agent', value: 'agent-alpha' },
    ]);
    // The room name contains 'agent-alpha' as a substring, so the longer value
    // is replaced first and the room name is not partially mangled.
    expect(result).toContain("name: '{room_name}'");
    expect(result).toContain("- '{agent}'");
  });

  it('quotes values containing single quotes', () => {
    const yaml = `room:
  name: it's-a-room
`;
    const result = parameterize(yaml, [{ key: 'name', value: "it's-a-room" }]);
    expect(result).toContain("name: '{name}'");
    // The default in the params block should have doubled single quotes.
    expect(result).toContain("default: 'it''s-a-room'");
  });

  it('replaces occurrences in instruction text', () => {
    const yaml = `room:
  name: my-room
  instructions: Welcome to my-room, the best room.
`;
    const result = parameterize(yaml, [{ key: 'name', value: 'my-room' }]);
    expect(result).toContain("instructions: 'Welcome to {name}, the best room.'");
  });

  it('throws on invalid param name', () => {
    expect(() => parameterize(EXPORTED_YAML, [{ key: '123bad', value: 'x' }])).toThrow(
      'Invalid param name'
    );
  });

  it('throws on empty value', () => {
    expect(() => parameterize(EXPORTED_YAML, [{ key: 'x', value: '' }])).toThrow('Empty value');
  });

  it('throws on duplicate keys', () => {
    expect(() =>
      parameterize(EXPORTED_YAML, [
        { key: 'name', value: 'a' },
        { key: 'name', value: 'b' },
      ])
    ).toThrow('Duplicate param key');
  });

  it('produces YAML that starts with params then room', () => {
    const result = parameterize(EXPORTED_YAML, [{ key: 'name', value: 'local-deploy' }]);
    const paramsIdx = result.indexOf('params:');
    const roomIdx = result.indexOf('room:');
    expect(paramsIdx).toBeLessThan(roomIdx);
    expect(paramsIdx).toBe(0);
  });

  it('round-trips: parameterized YAML is valid for the server parser', () => {
    // The server parser expects: top-level `params:` and `room:`, with
    // placeholder values as quoted strings. This test verifies structure only —
    // actual YAML parsing is the server's job.
    const result = parameterize(EXPORTED_YAML, [{ key: 'name', value: 'local-deploy' }]);
    // Has both top-level keys.
    expect(result).toMatch(/^params:/m);
    expect(result).toMatch(/^room:/m);
    // Placeholder is quoted (safe for yaml.safe_load).
    expect(result).toMatch(/'.*\{name\}.*'/);
  });
});

describe('parameterize: block scalars', () => {
  it('leaves the body of a | block alone, and still quotes a mapping value outside it', () => {
    const yaml = [
      'room:',
      '  name: alpha-room',
      '  instructions: |',
      '    Room rules:',
      '    - Ask agent-alpha for help when stuck.',
      '    - Respond with: {"ok": true}',
      '  topic: agent-alpha',
    ].join('\n');
    const out = parameterize(yaml, [{ key: 'agent_a', value: 'agent-alpha' }]);
    expect(out).toContain('    - Ask {agent_a} for help when stuck.');
    expect(out).toContain('    - Respond with: {"ok": true}');
    expect(out).toContain("  topic: '{agent_a}'");
  });

  it('recognises an indented block header with the chomping sign after the indent', () => {
    const yaml = [
      'room:',
      '  name: r',
      '  instructions: |2-',
      '     - Ask agent-alpha.',
      '  topic: agent-alpha',
    ].join('\n');
    const out = parameterize(yaml, [{ key: 'a', value: 'agent-alpha' }]);
    expect(out).toContain('     - Ask {a}.');
    expect(out).toContain("  topic: '{a}'");
  });
});

describe('parameterize: structure and keys', () => {
  it('never touches a structural key, even when a value spells it', () => {
    const yaml = ['room:', '  name: room', '  description: the room'].join('\n');
    const out = parameterize(yaml, [{ key: 'n', value: 'room' }]);
    expect(out).toContain('\nroom:\n');
    expect(out).toContain("  name: '{n}'");
    expect(out).toContain("  description: 'the {n}'");
  });

  it('quotes an alias key that becomes a placeholder', () => {
    const yaml = [
      'room:',
      '  name: r',
      '  agents:',
      '  - helper',
      '  aliases:',
      '    helper: buddy',
    ].join('\n');
    const out = parameterize(yaml, [{ key: 'a', value: 'helper' }]);
    expect(out).toContain("  - '{a}'");
    expect(out).toContain("    '{a}': buddy");
  });

  it('substitutes inside an already quoted value and keeps its quotes', () => {
    const yaml = ['room:', "  name: 'Ask helper: now'"].join('\n');
    const out = parameterize(yaml, [{ key: 'a', value: 'helper' }]);
    expect(out).toContain("  name: 'Ask {a}: now'");
  });

  it('quotes a default the loader would read as a number, boolean or null', () => {
    const yaml = ['room:', '  name: r', '  description: 001'].join('\n');
    const out = parameterize(yaml, [
      { key: 'code', value: '001' },
      { key: 'flag', value: 'true' },
      { key: 'nothing', value: 'null' },
    ]);
    expect(out).toContain("    default: '001'");
    expect(out).toContain("    default: 'true'");
    expect(out).toContain("    default: 'null'");
  });
});