mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-02 02:57:51 +00:00
test: remove redundant pi embedded runner cases
This commit is contained in:
@@ -5,16 +5,6 @@ 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 type { OpenClawConfig } from "../config/config.js";
|
||||||
|
|
||||||
type PiAiMockState = {
|
|
||||||
lastModel: { provider?: string; id?: string; compat?: unknown } | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const piAiMockState = vi.hoisted(
|
|
||||||
(): PiAiMockState => ({
|
|
||||||
lastModel: null,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
function createMockUsage(input: number, output: number) {
|
function createMockUsage(input: number, output: number) {
|
||||||
return {
|
return {
|
||||||
input,
|
input,
|
||||||
@@ -33,28 +23,9 @@ function createMockUsage(input: number, output: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vi.mock("@mariozechner/pi-coding-agent", async () => {
|
vi.mock("@mariozechner/pi-coding-agent", async () => {
|
||||||
const actual = await vi.importActual<typeof import("@mariozechner/pi-coding-agent")>(
|
return await vi.importActual<typeof import("@mariozechner/pi-coding-agent")>(
|
||||||
"@mariozechner/pi-coding-agent",
|
"@mariozechner/pi-coding-agent",
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
createAgentSession: async (
|
|
||||||
...args: Parameters<typeof actual.createAgentSession>
|
|
||||||
): ReturnType<typeof actual.createAgentSession> => {
|
|
||||||
const result = await actual.createAgentSession(...args);
|
|
||||||
const modelId = (args[0] as { model?: { id?: string } } | undefined)?.model?.id;
|
|
||||||
if (modelId === "mock-throw") {
|
|
||||||
const session = result.session as { prompt?: (...params: unknown[]) => Promise<unknown> };
|
|
||||||
if (session && typeof session.prompt === "function") {
|
|
||||||
session.prompt = async () => {
|
|
||||||
throw new Error("transport failed");
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
vi.mock("@mariozechner/pi-ai", async () => {
|
vi.mock("@mariozechner/pi-ai", async () => {
|
||||||
@@ -98,7 +69,6 @@ vi.mock("@mariozechner/pi-ai", async () => {
|
|||||||
return buildAssistantMessage(model);
|
return buildAssistantMessage(model);
|
||||||
},
|
},
|
||||||
streamSimple: (model: { api: string; provider: string; id: string }) => {
|
streamSimple: (model: { api: string; provider: string; id: string }) => {
|
||||||
piAiMockState.lastModel = model as { provider?: string; id?: string; compat?: unknown };
|
|
||||||
const stream = actual.createAssistantMessageEventStream();
|
const stream = actual.createAssistantMessageEventStream();
|
||||||
queueMicrotask(() => {
|
queueMicrotask(() => {
|
||||||
stream.push({
|
stream.push({
|
||||||
@@ -244,112 +214,32 @@ const runDefaultEmbeddedTurn = async (sessionFile: string, prompt: string, sessi
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const makeMoonshotConfig = (modelIds: string[]) =>
|
|
||||||
({
|
|
||||||
models: {
|
|
||||||
providers: {
|
|
||||||
moonshot: {
|
|
||||||
api: "openai-completions",
|
|
||||||
apiKey: "sk-test",
|
|
||||||
baseUrl: "https://api.moonshot.ai/v1",
|
|
||||||
models: modelIds.map((id) => ({
|
|
||||||
id,
|
|
||||||
name: `Moonshot ${id}`,
|
|
||||||
reasoning: false,
|
|
||||||
input: ["text", "image"],
|
|
||||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
||||||
contextWindow: 256_000,
|
|
||||||
maxTokens: 8_192,
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}) satisfies OpenClawConfig;
|
|
||||||
|
|
||||||
describe("runEmbeddedPiAgent", () => {
|
describe("runEmbeddedPiAgent", () => {
|
||||||
it("normalizes moonshot models to disable developer-role payloads in runner calls", async () => {
|
it("handles prompt error paths without dropping user state", async () => {
|
||||||
piAiMockState.lastModel = null;
|
|
||||||
const sessionFile = nextSessionFile();
|
const sessionFile = nextSessionFile();
|
||||||
|
const cfg = makeOpenAiConfig(["mock-error"]);
|
||||||
const sessionKey = nextSessionKey();
|
const sessionKey = nextSessionKey();
|
||||||
const cfg = makeMoonshotConfig(["kimi-k2.5"]);
|
const result = await runEmbeddedPiAgent({
|
||||||
|
|
||||||
await runEmbeddedPiAgent({
|
|
||||||
sessionId: "session:test",
|
sessionId: "session:test",
|
||||||
sessionKey,
|
sessionKey,
|
||||||
sessionFile,
|
sessionFile,
|
||||||
workspaceDir,
|
workspaceDir,
|
||||||
config: cfg,
|
config: cfg,
|
||||||
prompt: "reply with ok",
|
prompt: "boom",
|
||||||
provider: "moonshot",
|
provider: "openai",
|
||||||
model: "kimi-k2.5",
|
model: "mock-error",
|
||||||
timeoutMs: 5_000,
|
timeoutMs: 5_000,
|
||||||
agentDir,
|
agentDir,
|
||||||
runId: nextRunId("moonshot-compat"),
|
runId: nextRunId("prompt-error"),
|
||||||
enqueue: immediateEnqueue,
|
enqueue: immediateEnqueue,
|
||||||
});
|
});
|
||||||
|
expect(result.payloads?.[0]?.isError).toBe(true);
|
||||||
|
|
||||||
const capturedModel = piAiMockState.lastModel as {
|
const messages = await readSessionMessages(sessionFile);
|
||||||
provider?: string;
|
const userIndex = messages.findIndex(
|
||||||
id?: string;
|
(message) => message?.role === "user" && textFromContent(message.content) === "boom",
|
||||||
compat?: unknown;
|
);
|
||||||
} | null;
|
expect(userIndex).toBeGreaterThanOrEqual(0);
|
||||||
expect(capturedModel?.provider).toBe("moonshot");
|
|
||||||
expect(capturedModel?.id).toBe("kimi-k2.5");
|
|
||||||
expect(
|
|
||||||
(capturedModel?.compat as { supportsDeveloperRole?: boolean } | undefined)
|
|
||||||
?.supportsDeveloperRole,
|
|
||||||
).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("handles prompt error paths without dropping user state", async () => {
|
|
||||||
for (const testCase of [
|
|
||||||
{
|
|
||||||
label: "assistant error response keeps user message",
|
|
||||||
model: "mock-error",
|
|
||||||
prompt: "boom",
|
|
||||||
runIdPrefix: "prompt-error",
|
|
||||||
expectReject: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: "transport error fails fast before writing transcript",
|
|
||||||
model: "mock-throw",
|
|
||||||
prompt: "transport error",
|
|
||||||
runIdPrefix: "transport-error",
|
|
||||||
expectReject: true,
|
|
||||||
},
|
|
||||||
] as const) {
|
|
||||||
const sessionFile = nextSessionFile();
|
|
||||||
const cfg = makeOpenAiConfig([testCase.model]);
|
|
||||||
const sessionKey = nextSessionKey();
|
|
||||||
const execution = runEmbeddedPiAgent({
|
|
||||||
sessionId: "session:test",
|
|
||||||
sessionKey,
|
|
||||||
sessionFile,
|
|
||||||
workspaceDir,
|
|
||||||
config: cfg,
|
|
||||||
prompt: testCase.prompt,
|
|
||||||
provider: "openai",
|
|
||||||
model: testCase.model,
|
|
||||||
timeoutMs: 5_000,
|
|
||||||
agentDir,
|
|
||||||
runId: nextRunId(testCase.runIdPrefix),
|
|
||||||
enqueue: immediateEnqueue,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (testCase.expectReject) {
|
|
||||||
await expect(execution, testCase.label).rejects.toThrow("transport failed");
|
|
||||||
await expect(fs.stat(sessionFile), testCase.label).rejects.toBeTruthy();
|
|
||||||
} else {
|
|
||||||
const result = await execution;
|
|
||||||
expect(result.payloads?.[0]?.isError, testCase.label).toBe(true);
|
|
||||||
|
|
||||||
const messages = await readSessionMessages(sessionFile);
|
|
||||||
const userIndex = messages.findIndex(
|
|
||||||
(message) => message?.role === "user" && textFromContent(message.content) === "boom",
|
|
||||||
);
|
|
||||||
expect(userIndex, testCase.label).toBeGreaterThanOrEqual(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it(
|
it(
|
||||||
|
|||||||
Reference in New Issue
Block a user