Files
oh-my-pi/packages/coding-agent/test/utilities.ts
T
can1357 cb5bbbf382 refactor(coding-agent): flattened directory structure, eliminated core/ folder
- Eliminated core/ directory (252 files, 23 subdirs → distributed)
- Reduced max nesting from 9 levels to 5 levels
- Promoted tool subdirs to top level: exa/, lsp/, patch/, task/, web/
- Merged web-scrapers/ + web-search/ into web/{scrapers,search}/
- Flattened modes/interactive/ to modes/
- Split execution/ into: ipy/ (python), exec/ (bash), ssh/
- Renamed ipy/python-*.ts to ipy/*.ts (executor, kernel, etc.)
- Flattened cursor/exec-bridge.ts to cursor.ts
- Created logical groupings: config/, session/, extensibility/, export/
- Updated all imports across 500+ files
2026-01-23 12:24:47 +01:00

168 lines
4.4 KiB
TypeScript

/**
* Shared test utilities for coding-agent tests.
*/
import { existsSync, mkdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Agent } from "@oh-my-pi/pi-agent-core";
import { getModel } from "@oh-my-pi/pi-ai";
import { nanoid } from "nanoid";
import { ModelRegistry } from "$c/config/model-registry";
import { SettingsManager } from "$c/config/settings-manager";
import { AgentSession } from "$c/session/agent-session";
import { AuthStorage } from "$c/session/auth-storage";
import { SessionManager } from "$c/session/session-manager";
import { createTools, type ToolSession } from "$c/tools/index";
/**
* API key for authenticated tests. Tests using this should be wrapped in
* describe.skipIf(!API_KEY)
*/
export const API_KEY = process.env.ANTHROPIC_OAUTH_TOKEN || process.env.ANTHROPIC_API_KEY;
/**
* Create a minimal user message for testing.
*/
export function userMsg(text: string) {
return { role: "user" as const, content: text, timestamp: Date.now() };
}
/**
* Create a minimal assistant message for testing.
*/
export function assistantMsg(text: string) {
return {
role: "assistant" as const,
content: [{ type: "text" as const, text }],
api: "anthropic-messages" as const,
provider: "anthropic",
model: "test",
usage: {
input: 1,
output: 1,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 2,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
stopReason: "stop" as const,
timestamp: Date.now(),
};
}
/**
* Options for creating a test session.
*/
export interface TestSessionOptions {
/** Use in-memory session (no file persistence) */
inMemory?: boolean;
/** Custom system prompt */
systemPrompt?: string;
/** Custom settings overrides */
settingsOverrides?: Record<string, unknown>;
}
/**
* Resources returned by createTestSession that need cleanup.
*/
export interface TestSessionContext {
session: AgentSession;
sessionManager: SessionManager;
tempDir: string;
cleanup: () => void;
}
/**
* Create an AgentSession for testing with proper setup and cleanup.
* Use this for e2e tests that need real LLM calls.
*/
export async function createTestSession(options: TestSessionOptions = {}): Promise<TestSessionContext> {
const tempDir = join(tmpdir(), `omp-test-${nanoid()}`);
mkdirSync(tempDir, { recursive: true });
const toolSession: ToolSession = {
cwd: tempDir,
hasUI: false,
getSessionFile: () => null,
getSessionSpawns: () => "*",
};
const tools = await createTools(toolSession);
const model = getModel("anthropic", "claude-sonnet-4-5")!;
const agent = new Agent({
getApiKey: () => API_KEY,
initialState: {
model,
systemPrompt: options.systemPrompt ?? "You are a helpful assistant. Be extremely concise.",
tools,
},
});
const sessionManager = options.inMemory ? SessionManager.inMemory() : SessionManager.create(tempDir);
const settingsManager = await SettingsManager.create(tempDir, tempDir);
if (options.settingsOverrides) {
settingsManager.applyOverrides(options.settingsOverrides);
}
const authStorage = new AuthStorage(join(tempDir, "auth.json"));
const modelRegistry = new ModelRegistry(authStorage, tempDir);
const session = new AgentSession({
agent,
sessionManager,
settingsManager,
modelRegistry,
});
// Must subscribe to enable session persistence
session.subscribe(() => {});
const cleanup = () => {
session.dispose();
if (tempDir && existsSync(tempDir)) {
rmSync(tempDir, { recursive: true });
}
};
return { session, sessionManager, tempDir, cleanup };
}
/**
* Build a session tree for testing using SessionManager.
* Returns the IDs of all created entries.
*
* Example tree structure:
* ```
* u1 -> a1 -> u2 -> a2
* -> u3 -> a3 (branch from a1)
* u4 -> a4 (another root)
* ```
*/
export function buildTestTree(
session: SessionManager,
structure: {
messages: Array<{ role: "user" | "assistant"; text: string; branchFrom?: string }>;
},
): Map<string, string> {
const ids = new Map<string, string>();
for (const msg of structure.messages) {
if (msg.branchFrom) {
const branchFromId = ids.get(msg.branchFrom);
if (!branchFromId) {
throw new Error(`Cannot branch from unknown entry: ${msg.branchFrom}`);
}
session.branch(branchFromId);
}
const id =
msg.role === "user" ? session.appendMessage(userMsg(msg.text)) : session.appendMessage(assistantMsg(msg.text));
ids.set(msg.text, id);
}
return ids;
}