tests/fakes/test-app.ts
2 KB - הצגת הקובץ המקורי
// Builds the full app (tools, policy, audit, memory, sessions) on temp folders with a fake model.
import { mkdir, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createApp, type App } from '../../src/app.ts';
import { loadConfig, type StewardConfig } from '../../src/config/config.ts';
import { FakeModel, type ScriptStep } from './fake-model.ts';
import { tempDir } from './helpers.ts';
const EXAMPLE_CONFIG = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../../config/steward.example.json',
);
export interface TestApp extends App {
model: FakeModel;
/** The sub-agent's model (lab 10). */
subModel: FakeModel;
dir: string;
}
export async function testConfig(
overrides: (config: StewardConfig) => StewardConfig = (c) => c,
): Promise<{ config: StewardConfig; dir: string }> {
const dir = await tempDir();
// Always the shipped example: the labs ask you to change your local config (stricter policy,
// a tiny spending limit, other prompts), and the checkpoints must not depend on those edits.
const base = loadConfig(EXAMPLE_CONFIG);
const config = overrides({
...base,
owner: { ...base.owner, timezone: 'UTC' },
paths: {
...base.paths,
dataDir: path.join(dir, 'data'),
workspace: path.join(dir, 'workspace'),
},
});
return { config, dir };
}
export async function testApp(
script: ScriptStep[],
overrides?: (config: StewardConfig) => StewardConfig,
now: () => Date = () => new Date('2026-11-22T15:00:00Z'),
subScript: ScriptStep[] = [],
): Promise<TestApp> {
const { config, dir } = await testConfig(overrides);
const model = new FakeModel(script);
const subModel = new FakeModel(subScript);
const app = await createApp(config, { model, subagentModel: subModel, now });
return { ...app, model, subModel, dir };
}
/** Writes files into the test app's workspace. */
export async function seedWorkspace(app: App, files: Record<string, string>): Promise<void> {
for (const [name, content] of Object.entries(files)) {
const file = path.join(app.workspace.root, name);
await mkdir(path.dirname(file), { recursive: true });
await writeFile(file, content, 'utf8');
}
}