mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
test(auto-reply): reuse shared directive and home test harnesses
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { loadModelCatalog } from "../agents/model-catalog.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { withTempHome as withTempHomeHarness } from "../config/home-env.test-harness.js";
|
||||
import { getReplyFromConfig } from "./reply.js";
|
||||
|
||||
type RunEmbeddedPiAgent = typeof import("../agents/pi-embedded.js").runEmbeddedPiAgent;
|
||||
@@ -25,37 +25,6 @@ vi.mock("../agents/model-catalog.js", () => ({
|
||||
loadModelCatalog: vi.fn(),
|
||||
}));
|
||||
|
||||
type HomeEnvSnapshot = {
|
||||
HOME: string | undefined;
|
||||
USERPROFILE: string | undefined;
|
||||
HOMEDRIVE: string | undefined;
|
||||
HOMEPATH: string | undefined;
|
||||
OPENCLAW_STATE_DIR: string | undefined;
|
||||
};
|
||||
|
||||
function snapshotHomeEnv(): HomeEnvSnapshot {
|
||||
return {
|
||||
HOME: process.env.HOME,
|
||||
USERPROFILE: process.env.USERPROFILE,
|
||||
HOMEDRIVE: process.env.HOMEDRIVE,
|
||||
HOMEPATH: process.env.HOMEPATH,
|
||||
OPENCLAW_STATE_DIR: process.env.OPENCLAW_STATE_DIR,
|
||||
};
|
||||
}
|
||||
|
||||
function restoreHomeEnv(snapshot: HomeEnvSnapshot) {
|
||||
for (const [key, value] of Object.entries(snapshot)) {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let fixtureRoot = "";
|
||||
let caseId = 0;
|
||||
|
||||
type GetReplyOptions = NonNullable<Parameters<typeof getReplyFromConfig>[1]>;
|
||||
|
||||
function createEmbeddedReply(text: string): RunEmbeddedPiAgentReply {
|
||||
@@ -111,49 +80,13 @@ async function runTelegramReply(params: {
|
||||
}
|
||||
|
||||
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
|
||||
const home = path.join(fixtureRoot, `case-${++caseId}`);
|
||||
await fs.mkdir(path.join(home, ".openclaw", "agents", "main", "sessions"), { recursive: true });
|
||||
const envSnapshot = snapshotHomeEnv();
|
||||
process.env.HOME = home;
|
||||
process.env.USERPROFILE = home;
|
||||
process.env.OPENCLAW_STATE_DIR = path.join(home, ".openclaw");
|
||||
|
||||
if (process.platform === "win32") {
|
||||
const match = home.match(/^([A-Za-z]:)(.*)$/);
|
||||
if (match) {
|
||||
process.env.HOMEDRIVE = match[1];
|
||||
process.env.HOMEPATH = match[2] || "\\";
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await fn(home);
|
||||
} finally {
|
||||
restoreHomeEnv(envSnapshot);
|
||||
}
|
||||
return withTempHomeHarness("openclaw-stream-", async (home) => {
|
||||
await fs.mkdir(path.join(home, ".openclaw", "agents", "main", "sessions"), { recursive: true });
|
||||
return fn(home);
|
||||
});
|
||||
}
|
||||
|
||||
describe("block streaming", () => {
|
||||
beforeAll(async () => {
|
||||
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-stream-"));
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (process.platform === "win32") {
|
||||
await fs.rm(fixtureRoot, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: 10,
|
||||
retryDelay: 50,
|
||||
});
|
||||
} else {
|
||||
await fs.rm(fixtureRoot, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubEnv("OPENCLAW_TEST_FAST", "1");
|
||||
piEmbeddedMock.abortEmbeddedPiRun.mockReset().mockReturnValue(false);
|
||||
|
||||
@@ -1,19 +1,11 @@
|
||||
import path from "node:path";
|
||||
import "./reply.directive.directive-behavior.e2e-mocks.js";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js";
|
||||
import { runEmbeddedPiAgent } from "../agents/pi-embedded.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { getReplyFromConfig } from "./reply.js";
|
||||
|
||||
vi.mock("../agents/pi-embedded.js", () => ({
|
||||
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
||||
runEmbeddedPiAgent: vi.fn(),
|
||||
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
|
||||
resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
|
||||
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
|
||||
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
|
||||
}));
|
||||
|
||||
function makeResult(text: string) {
|
||||
return {
|
||||
payloads: [{ text }],
|
||||
|
||||
Reference in New Issue
Block a user