Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

4.3 KB
Raw Blame
import { afterAll, describe, expect, it } from 'bun:test';
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { DEFAULT_STATE_PATH, loadTranscriptWatchConfig } from '../../src/services/transcripts/config.js';
import { runTranscriptCommand } from '../../src/services/transcripts/cli.js';
const root = mkdtempSync(join(tmpdir(), 'cm-config-shapes-'));
const configPath = join(root, 'watch.json');
afterAll(() => rmSync(root, { recursive: true, force: true }));
describe('transcript config validation', () => {
  for (const input of [{version:1,watches:[null]},
    {version:1,watches:[{name:'owned',path:'owned.jsonl',schema:{name:'owned',events:{}}}]},
    {version:1,watches:[{name:'owned',path:'owned.jsonl',schema:{name:'owned',events:[null]}}]}]) {
    it(`rejects watcher-breaking config ${JSON.stringify(input)}`, () => {
      writeFileSync(configPath, JSON.stringify(input));
      expect(() => loadTranscriptWatchConfig(configPath)).toThrow('Invalid transcript watch config');
    });
  }
});

it('validates referenced schema events before claiming Config OK', async () => {
  writeFileSync(configPath, JSON.stringify({ version: 1, schemas: { owned: { name: 'owned', events: {} } },
    watches: [{ name: 'owned', path: 'owned.jsonl', schema: 'owned' }] }));
  await expect(runTranscriptCommand('validate', ['--config', configPath])).rejects.toThrow('Invalid transcript watch config');
});
for (const invalidEvent of [{ name: 'owned', action: 'user_message', match: { path: 42 } },
  { name: 'owned', action: 'user_message', fields: { prompt: { path: 42 } } },
  { name: 'owned', action: 'user_message', match: { all: [{ path: 42 }] } }]) {
  it(`rejects an invalid runtime event ${JSON.stringify(invalidEvent)}`, () => {
    writeFileSync(configPath, JSON.stringify({ version: 1, watches: [
      { name: 'owned', path: 'owned.jsonl', schema: { name: 'owned', events: [invalidEvent] } } ] }));
    expect(() => loadTranscriptWatchConfig(configPath)).toThrow('Invalid transcript watch config');
  });
}

describe('transcript config errors name what to fix', () => {
  const cases: Array<[input: unknown, problem: string]> = [
    [{ version: 2, watches: [] }, 'version must be 1'],
    [{ version: 1, watches: {} }, 'watches must be an array'],
    [{ version: 1, watches: [{ name: 'a', path: 'a.jsonl', schema: 'a' }, { name: 'b', path: 42, schema: 'b' }] },
      'watches[1].path must be a non-empty string'],
    [{ version: 1, schemas: { owned: { name: 'owned', events: {} } }, watches: [] }, 'schemas.owned.events must be an array'],
    [{ version: 1, watches: [{ name: 'owned', path: 'owned.jsonl', schema: { name: 'owned', events: [
      { name: 'turn', action: 'user_message', fields: { prompt: { coalesce: ['text', { path: 42 }] } } }] } }] },
      'watches[0].schema.events[0].fields.prompt.coalesce[1].path must be a string'],
  ];
  for (const [input, problem] of cases) {
    it(`reports "${problem}"`, () => {
      writeFileSync(configPath, JSON.stringify(input));
      expect(() => loadTranscriptWatchConfig(configPath))
        .toThrow(`Invalid transcript watch config: ${configPath} (${problem})`);
    });
  }

  it('treats a null stateFile as absent', () => {
    writeFileSync(configPath, JSON.stringify({ version: 1, watches: [], stateFile: null }));
    expect(loadTranscriptWatchConfig(configPath).stateFile).toBe(DEFAULT_STATE_PATH);
  });
});

describe('transcript CLI sample config', () => {
  it('never replaces an existing invalid config with the sample, even when its error mentions "not found"', async () => {
    const directory = join(root, 'not found');
    mkdirSync(directory, { recursive: true });
    const path = join(directory, 'watch.json');
    const document = JSON.stringify({ version: 1, watches: {} });
    writeFileSync(path, document);
    await expect(runTranscriptCommand('validate', ['--config', path])).rejects.toThrow('watches must be an array');
    expect(readFileSync(path, 'utf8')).toBe(document);
  });

  it('still writes the sample when the config is missing', async () => {
    const path = join(root, 'missing', 'watch.json');
    expect(await runTranscriptCommand('validate', ['--config', path])).toBe(0);
    expect(loadTranscriptWatchConfig(path).watches).toEqual([]);
  });
});