mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:38:38 +00:00
refactor: share embedded runner e2e fixtures
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import "./test-helpers/fast-coding-tools.js";
|
import "./test-helpers/fast-coding-tools.js";
|
||||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import {
|
||||||
|
cleanupEmbeddedPiRunnerTestWorkspace,
|
||||||
|
createEmbeddedPiRunnerOpenAiConfig,
|
||||||
|
createEmbeddedPiRunnerTestWorkspace,
|
||||||
|
type EmbeddedPiRunnerTestWorkspace,
|
||||||
|
immediateEnqueue,
|
||||||
|
} from "./test-helpers/pi-embedded-runner-e2e-fixtures.js";
|
||||||
|
|
||||||
function createMockUsage(input: number, output: number) {
|
function createMockUsage(input: number, output: number) {
|
||||||
return {
|
return {
|
||||||
@@ -88,7 +93,7 @@ vi.mock("@mariozechner/pi-ai", async () => {
|
|||||||
|
|
||||||
let runEmbeddedPiAgent: typeof import("./pi-embedded-runner/run.js").runEmbeddedPiAgent;
|
let runEmbeddedPiAgent: typeof import("./pi-embedded-runner/run.js").runEmbeddedPiAgent;
|
||||||
let SessionManager: typeof import("@mariozechner/pi-coding-agent").SessionManager;
|
let SessionManager: typeof import("@mariozechner/pi-coding-agent").SessionManager;
|
||||||
let tempRoot: string | undefined;
|
let e2eWorkspace: EmbeddedPiRunnerTestWorkspace | undefined;
|
||||||
let agentDir: string;
|
let agentDir: string;
|
||||||
let workspaceDir: string;
|
let workspaceDir: string;
|
||||||
let sessionCounter = 0;
|
let sessionCounter = 0;
|
||||||
@@ -98,50 +103,21 @@ beforeAll(async () => {
|
|||||||
vi.useRealTimers();
|
vi.useRealTimers();
|
||||||
({ runEmbeddedPiAgent } = await import("./pi-embedded-runner/run.js"));
|
({ runEmbeddedPiAgent } = await import("./pi-embedded-runner/run.js"));
|
||||||
({ SessionManager } = await import("@mariozechner/pi-coding-agent"));
|
({ SessionManager } = await import("@mariozechner/pi-coding-agent"));
|
||||||
tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-embedded-agent-"));
|
e2eWorkspace = await createEmbeddedPiRunnerTestWorkspace("openclaw-embedded-agent-");
|
||||||
agentDir = path.join(tempRoot, "agent");
|
({ agentDir, workspaceDir } = e2eWorkspace);
|
||||||
workspaceDir = path.join(tempRoot, "workspace");
|
|
||||||
await fs.mkdir(agentDir, { recursive: true });
|
|
||||||
await fs.mkdir(workspaceDir, { recursive: true });
|
|
||||||
}, 180_000);
|
}, 180_000);
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
if (!tempRoot) {
|
await cleanupEmbeddedPiRunnerTestWorkspace(e2eWorkspace);
|
||||||
return;
|
e2eWorkspace = undefined;
|
||||||
}
|
|
||||||
await fs.rm(tempRoot, { recursive: true, force: true });
|
|
||||||
tempRoot = undefined;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const makeOpenAiConfig = (modelIds: string[]) =>
|
|
||||||
({
|
|
||||||
models: {
|
|
||||||
providers: {
|
|
||||||
openai: {
|
|
||||||
api: "openai-responses",
|
|
||||||
apiKey: "sk-test",
|
|
||||||
baseUrl: "https://example.com",
|
|
||||||
models: modelIds.map((id) => ({
|
|
||||||
id,
|
|
||||||
name: `Mock ${id}`,
|
|
||||||
reasoning: false,
|
|
||||||
input: ["text"],
|
|
||||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
||||||
contextWindow: 16_000,
|
|
||||||
maxTokens: 2048,
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}) satisfies OpenClawConfig;
|
|
||||||
|
|
||||||
const nextSessionFile = () => {
|
const nextSessionFile = () => {
|
||||||
sessionCounter += 1;
|
sessionCounter += 1;
|
||||||
return path.join(workspaceDir, `session-${sessionCounter}.jsonl`);
|
return path.join(workspaceDir, `session-${sessionCounter}.jsonl`);
|
||||||
};
|
};
|
||||||
const nextRunId = (prefix = "run-embedded-test") => `${prefix}-${++runCounter}`;
|
const nextRunId = (prefix = "run-embedded-test") => `${prefix}-${++runCounter}`;
|
||||||
const nextSessionKey = () => `agent:test:embedded:${nextRunId("session-key")}`;
|
const nextSessionKey = () => `agent:test:embedded:${nextRunId("session-key")}`;
|
||||||
const immediateEnqueue = async <T>(task: () => Promise<T>) => task();
|
|
||||||
|
|
||||||
const runWithOrphanedSingleUserMessage = async (text: string, sessionKey: string) => {
|
const runWithOrphanedSingleUserMessage = async (text: string, sessionKey: string) => {
|
||||||
const sessionFile = nextSessionFile();
|
const sessionFile = nextSessionFile();
|
||||||
@@ -152,7 +128,7 @@ const runWithOrphanedSingleUserMessage = async (text: string, sessionKey: string
|
|||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const cfg = makeOpenAiConfig(["mock-1"]);
|
const cfg = createEmbeddedPiRunnerOpenAiConfig(["mock-1"]);
|
||||||
return await runEmbeddedPiAgent({
|
return await runEmbeddedPiAgent({
|
||||||
sessionId: "session:test",
|
sessionId: "session:test",
|
||||||
sessionKey,
|
sessionKey,
|
||||||
@@ -197,7 +173,7 @@ const readSessionMessages = async (sessionFile: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const runDefaultEmbeddedTurn = async (sessionFile: string, prompt: string, sessionKey: string) => {
|
const runDefaultEmbeddedTurn = async (sessionFile: string, prompt: string, sessionKey: string) => {
|
||||||
const cfg = makeOpenAiConfig(["mock-error"]);
|
const cfg = createEmbeddedPiRunnerOpenAiConfig(["mock-error"]);
|
||||||
await runEmbeddedPiAgent({
|
await runEmbeddedPiAgent({
|
||||||
sessionId: "session:test",
|
sessionId: "session:test",
|
||||||
sessionKey,
|
sessionKey,
|
||||||
@@ -217,7 +193,7 @@ const runDefaultEmbeddedTurn = async (sessionFile: string, prompt: string, sessi
|
|||||||
describe("runEmbeddedPiAgent", () => {
|
describe("runEmbeddedPiAgent", () => {
|
||||||
it("handles prompt error paths without dropping user state", async () => {
|
it("handles prompt error paths without dropping user state", async () => {
|
||||||
const sessionFile = nextSessionFile();
|
const sessionFile = nextSessionFile();
|
||||||
const cfg = makeOpenAiConfig(["mock-error"]);
|
const cfg = createEmbeddedPiRunnerOpenAiConfig(["mock-error"]);
|
||||||
const sessionKey = nextSessionKey();
|
const sessionKey = nextSessionKey();
|
||||||
const result = await runEmbeddedPiAgent({
|
const result = await runEmbeddedPiAgent({
|
||||||
sessionId: "session:test",
|
sessionId: "session:test",
|
||||||
|
|||||||
@@ -8,12 +8,17 @@
|
|||||||
* Follows the same pattern as pi-embedded-runner.e2e.test.ts.
|
* Follows the same pattern as pi-embedded-runner.e2e.test.ts.
|
||||||
*/
|
*/
|
||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import "./test-helpers/fast-coding-tools.js";
|
import "./test-helpers/fast-coding-tools.js";
|
||||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
|
||||||
import { isEmbeddedPiRunActive, queueEmbeddedPiMessage } from "./pi-embedded-runner/runs.js";
|
import { isEmbeddedPiRunActive, queueEmbeddedPiMessage } from "./pi-embedded-runner/runs.js";
|
||||||
|
import {
|
||||||
|
cleanupEmbeddedPiRunnerTestWorkspace,
|
||||||
|
createEmbeddedPiRunnerOpenAiConfig,
|
||||||
|
createEmbeddedPiRunnerTestWorkspace,
|
||||||
|
type EmbeddedPiRunnerTestWorkspace,
|
||||||
|
immediateEnqueue,
|
||||||
|
} from "./test-helpers/pi-embedded-runner-e2e-fixtures.js";
|
||||||
|
|
||||||
function createMockUsage(input: number, output: number) {
|
function createMockUsage(input: number, output: number) {
|
||||||
return {
|
return {
|
||||||
@@ -126,7 +131,7 @@ vi.mock("@mariozechner/pi-ai", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let runEmbeddedPiAgent: typeof import("./pi-embedded-runner/run.js").runEmbeddedPiAgent;
|
let runEmbeddedPiAgent: typeof import("./pi-embedded-runner/run.js").runEmbeddedPiAgent;
|
||||||
let tempRoot: string | undefined;
|
let e2eWorkspace: EmbeddedPiRunnerTestWorkspace | undefined;
|
||||||
let agentDir: string;
|
let agentDir: string;
|
||||||
let workspaceDir: string;
|
let workspaceDir: string;
|
||||||
|
|
||||||
@@ -136,45 +141,15 @@ beforeAll(async () => {
|
|||||||
responsePlan = [];
|
responsePlan = [];
|
||||||
observedContexts = [];
|
observedContexts = [];
|
||||||
({ runEmbeddedPiAgent } = await import("./pi-embedded-runner/run.js"));
|
({ runEmbeddedPiAgent } = await import("./pi-embedded-runner/run.js"));
|
||||||
tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-yield-e2e-"));
|
e2eWorkspace = await createEmbeddedPiRunnerTestWorkspace("openclaw-yield-e2e-");
|
||||||
agentDir = path.join(tempRoot, "agent");
|
({ agentDir, workspaceDir } = e2eWorkspace);
|
||||||
workspaceDir = path.join(tempRoot, "workspace");
|
|
||||||
await fs.mkdir(agentDir, { recursive: true });
|
|
||||||
await fs.mkdir(workspaceDir, { recursive: true });
|
|
||||||
}, 180_000);
|
}, 180_000);
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
if (!tempRoot) {
|
await cleanupEmbeddedPiRunnerTestWorkspace(e2eWorkspace);
|
||||||
return;
|
e2eWorkspace = undefined;
|
||||||
}
|
|
||||||
await fs.rm(tempRoot, { recursive: true, force: true });
|
|
||||||
tempRoot = undefined;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const makeConfig = (modelIds: string[]) =>
|
|
||||||
({
|
|
||||||
models: {
|
|
||||||
providers: {
|
|
||||||
openai: {
|
|
||||||
api: "openai-responses",
|
|
||||||
apiKey: "sk-test",
|
|
||||||
baseUrl: "https://example.com",
|
|
||||||
models: modelIds.map((id) => ({
|
|
||||||
id,
|
|
||||||
name: `Mock ${id}`,
|
|
||||||
reasoning: false,
|
|
||||||
input: ["text"],
|
|
||||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
||||||
contextWindow: 16_000,
|
|
||||||
maxTokens: 2048,
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}) satisfies OpenClawConfig;
|
|
||||||
|
|
||||||
const immediateEnqueue = async <T>(task: () => Promise<T>) => task();
|
|
||||||
|
|
||||||
const readSessionMessages = async (sessionFile: string) => {
|
const readSessionMessages = async (sessionFile: string) => {
|
||||||
const raw = await fs.readFile(sessionFile, "utf-8");
|
const raw = await fs.readFile(sessionFile, "utf-8");
|
||||||
return raw
|
return raw
|
||||||
@@ -205,7 +180,7 @@ describe("sessions_yield e2e", () => {
|
|||||||
|
|
||||||
const sessionId = "yield-e2e-parent";
|
const sessionId = "yield-e2e-parent";
|
||||||
const sessionFile = path.join(workspaceDir, "session-yield-e2e.jsonl");
|
const sessionFile = path.join(workspaceDir, "session-yield-e2e.jsonl");
|
||||||
const cfg = makeConfig(["mock-yield"]);
|
const cfg = createEmbeddedPiRunnerOpenAiConfig(["mock-yield"]);
|
||||||
|
|
||||||
const result = await runEmbeddedPiAgent({
|
const result = await runEmbeddedPiAgent({
|
||||||
sessionId,
|
sessionId,
|
||||||
@@ -304,7 +279,7 @@ describe("sessions_yield e2e", () => {
|
|||||||
|
|
||||||
const sessionId = "yield-e2e-abort";
|
const sessionId = "yield-e2e-abort";
|
||||||
const sessionFile = path.join(workspaceDir, "session-yield-abort.jsonl");
|
const sessionFile = path.join(workspaceDir, "session-yield-abort.jsonl");
|
||||||
const cfg = makeConfig(["mock-yield-abort"]);
|
const cfg = createEmbeddedPiRunnerOpenAiConfig(["mock-yield-abort"]);
|
||||||
|
|
||||||
const result = await runEmbeddedPiAgent({
|
const result = await runEmbeddedPiAgent({
|
||||||
sessionId,
|
sessionId,
|
||||||
|
|||||||
57
src/agents/test-helpers/pi-embedded-runner-e2e-fixtures.ts
Normal file
57
src/agents/test-helpers/pi-embedded-runner-e2e-fixtures.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
|
|
||||||
|
export type EmbeddedPiRunnerTestWorkspace = {
|
||||||
|
tempRoot: string;
|
||||||
|
agentDir: string;
|
||||||
|
workspaceDir: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function createEmbeddedPiRunnerTestWorkspace(
|
||||||
|
prefix: string,
|
||||||
|
): Promise<EmbeddedPiRunnerTestWorkspace> {
|
||||||
|
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||||
|
const agentDir = path.join(tempRoot, "agent");
|
||||||
|
const workspaceDir = path.join(tempRoot, "workspace");
|
||||||
|
await fs.mkdir(agentDir, { recursive: true });
|
||||||
|
await fs.mkdir(workspaceDir, { recursive: true });
|
||||||
|
return { tempRoot, agentDir, workspaceDir };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function cleanupEmbeddedPiRunnerTestWorkspace(
|
||||||
|
workspace: EmbeddedPiRunnerTestWorkspace | undefined,
|
||||||
|
): Promise<void> {
|
||||||
|
if (!workspace) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await fs.rm(workspace.tempRoot, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createEmbeddedPiRunnerOpenAiConfig(modelIds: string[]): OpenClawConfig {
|
||||||
|
return {
|
||||||
|
models: {
|
||||||
|
providers: {
|
||||||
|
openai: {
|
||||||
|
api: "openai-responses",
|
||||||
|
apiKey: "sk-test",
|
||||||
|
baseUrl: "https://example.com",
|
||||||
|
models: modelIds.map((id) => ({
|
||||||
|
id,
|
||||||
|
name: `Mock ${id}`,
|
||||||
|
reasoning: false,
|
||||||
|
input: ["text"],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
contextWindow: 16_000,
|
||||||
|
maxTokens: 2048,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function immediateEnqueue<T>(task: () => Promise<T>): Promise<T> {
|
||||||
|
return await task();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user