mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 21:01:43 +03:00
perf(test): trim fixture and import overhead in hot suites
This commit is contained in:
@@ -39,23 +39,20 @@ describe("block streaming", () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
async function waitForCalls(fn: () => number, calls: number) {
|
|
||||||
const deadline = Date.now() + 5000;
|
|
||||||
while (fn() < calls) {
|
|
||||||
if (Date.now() > deadline) {
|
|
||||||
throw new Error(`Expected ${calls} call(s), got ${fn()}`);
|
|
||||||
}
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
it("waits for block replies before returning final payloads", async () => {
|
it("waits for block replies before returning final payloads", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempHome(async (home) => {
|
||||||
let releaseTyping: (() => void) | undefined;
|
let releaseTyping: (() => void) | undefined;
|
||||||
const typingGate = new Promise<void>((resolve) => {
|
const typingGate = new Promise<void>((resolve) => {
|
||||||
releaseTyping = resolve;
|
releaseTyping = resolve;
|
||||||
});
|
});
|
||||||
const onReplyStart = vi.fn(() => typingGate);
|
let resolveOnReplyStart: (() => void) | undefined;
|
||||||
|
const onReplyStartCalled = new Promise<void>((resolve) => {
|
||||||
|
resolveOnReplyStart = resolve;
|
||||||
|
});
|
||||||
|
const onReplyStart = vi.fn(() => {
|
||||||
|
resolveOnReplyStart?.();
|
||||||
|
return typingGate;
|
||||||
|
});
|
||||||
const onBlockReply = vi.fn().mockResolvedValue(undefined);
|
const onBlockReply = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
const impl = async (params: RunEmbeddedPiAgentParams) => {
|
const impl = async (params: RunEmbeddedPiAgentParams) => {
|
||||||
@@ -95,7 +92,7 @@ describe("block streaming", () => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
await waitForCalls(() => onReplyStart.mock.calls.length, 1);
|
await onReplyStartCalled;
|
||||||
releaseTyping?.();
|
releaseTyping?.();
|
||||||
|
|
||||||
const res = await replyPromise;
|
const res = await replyPromise;
|
||||||
@@ -110,7 +107,14 @@ describe("block streaming", () => {
|
|||||||
const typingGate = new Promise<void>((resolve) => {
|
const typingGate = new Promise<void>((resolve) => {
|
||||||
releaseTyping = resolve;
|
releaseTyping = resolve;
|
||||||
});
|
});
|
||||||
const onReplyStart = vi.fn(() => typingGate);
|
let resolveOnReplyStart: (() => void) | undefined;
|
||||||
|
const onReplyStartCalled = new Promise<void>((resolve) => {
|
||||||
|
resolveOnReplyStart = resolve;
|
||||||
|
});
|
||||||
|
const onReplyStart = vi.fn(() => {
|
||||||
|
resolveOnReplyStart?.();
|
||||||
|
return typingGate;
|
||||||
|
});
|
||||||
const seen: string[] = [];
|
const seen: string[] = [];
|
||||||
const onBlockReply = vi.fn(async (payload) => {
|
const onBlockReply = vi.fn(async (payload) => {
|
||||||
seen.push(payload.text ?? "");
|
seen.push(payload.text ?? "");
|
||||||
@@ -154,7 +158,7 @@ describe("block streaming", () => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
await waitForCalls(() => onReplyStart.mock.calls.length, 1);
|
await onReplyStartCalled;
|
||||||
releaseTyping?.();
|
releaseTyping?.();
|
||||||
|
|
||||||
const res = await replyPromise;
|
const res = await replyPromise;
|
||||||
|
|||||||
@@ -156,6 +156,9 @@ vi.mock("./screenshot.js", () => ({
|
|||||||
})),
|
})),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
const { startBrowserControlServerFromConfig, stopBrowserControlServer } =
|
||||||
|
await import("./server.js");
|
||||||
|
|
||||||
async function getFreePort(): Promise<number> {
|
async function getFreePort(): Promise<number> {
|
||||||
while (true) {
|
while (true) {
|
||||||
const port = await new Promise<number>((resolve, reject) => {
|
const port = await new Promise<number>((resolve, reject) => {
|
||||||
@@ -274,12 +277,10 @@ describe("browser control server", () => {
|
|||||||
} else {
|
} else {
|
||||||
process.env.OPENCLAW_GATEWAY_PORT = prevGatewayPort;
|
process.env.OPENCLAW_GATEWAY_PORT = prevGatewayPort;
|
||||||
}
|
}
|
||||||
const { stopBrowserControlServer } = await import("./server.js");
|
|
||||||
await stopBrowserControlServer();
|
await stopBrowserControlServer();
|
||||||
});
|
});
|
||||||
|
|
||||||
const startServerAndBase = async () => {
|
const startServerAndBase = async () => {
|
||||||
const { startBrowserControlServerFromConfig } = await import("./server.js");
|
|
||||||
await startBrowserControlServerFromConfig();
|
await startBrowserControlServerFromConfig();
|
||||||
const base = `http://127.0.0.1:${testPort}`;
|
const base = `http://127.0.0.1:${testPort}`;
|
||||||
await realFetch(`${base}/start`, { method: "POST" }).then((r) => r.json());
|
await realFetch(`${base}/start`, { method: "POST" }).then((r) => r.json());
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import fs from "node:fs/promises";
|
|||||||
import { createServer } from "node:http";
|
import { createServer } from "node:http";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||||
import { WebSocket } from "ws";
|
import { WebSocket } from "ws";
|
||||||
import { rawDataToString } from "../infra/ws.js";
|
import { rawDataToString } from "../infra/ws.js";
|
||||||
import { defaultRuntime } from "../runtime.js";
|
import { defaultRuntime } from "../runtime.js";
|
||||||
@@ -11,6 +11,23 @@ import { A2UI_PATH, CANVAS_HOST_PATH, CANVAS_WS_PATH, injectCanvasLiveReload } f
|
|||||||
import { createCanvasHostHandler, startCanvasHost } from "./server.js";
|
import { createCanvasHostHandler, startCanvasHost } from "./server.js";
|
||||||
|
|
||||||
describe("canvas host", () => {
|
describe("canvas host", () => {
|
||||||
|
let fixtureRoot = "";
|
||||||
|
let fixtureCount = 0;
|
||||||
|
|
||||||
|
const createCaseDir = async () => {
|
||||||
|
const dir = path.join(fixtureRoot, `case-${fixtureCount++}`);
|
||||||
|
await fs.mkdir(dir, { recursive: true });
|
||||||
|
return dir;
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-fixtures-"));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
it("injects live reload script", () => {
|
it("injects live reload script", () => {
|
||||||
const out = injectCanvasLiveReload("<html><body>Hello</body></html>");
|
const out = injectCanvasLiveReload("<html><body>Hello</body></html>");
|
||||||
expect(out).toContain(CANVAS_WS_PATH);
|
expect(out).toContain(CANVAS_WS_PATH);
|
||||||
@@ -20,7 +37,7 @@ describe("canvas host", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("creates a default index.html when missing", async () => {
|
it("creates a default index.html when missing", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-"));
|
const dir = await createCaseDir();
|
||||||
|
|
||||||
const server = await startCanvasHost({
|
const server = await startCanvasHost({
|
||||||
runtime: defaultRuntime,
|
runtime: defaultRuntime,
|
||||||
@@ -39,12 +56,11 @@ describe("canvas host", () => {
|
|||||||
expect(html).toContain(CANVAS_WS_PATH);
|
expect(html).toContain(CANVAS_WS_PATH);
|
||||||
} finally {
|
} finally {
|
||||||
await server.close();
|
await server.close();
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips live reload injection when disabled", async () => {
|
it("skips live reload injection when disabled", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-"));
|
const dir = await createCaseDir();
|
||||||
await fs.writeFile(path.join(dir, "index.html"), "<html><body>no-reload</body></html>", "utf8");
|
await fs.writeFile(path.join(dir, "index.html"), "<html><body>no-reload</body></html>", "utf8");
|
||||||
|
|
||||||
const server = await startCanvasHost({
|
const server = await startCanvasHost({
|
||||||
@@ -67,12 +83,11 @@ describe("canvas host", () => {
|
|||||||
expect(wsRes.status).toBe(404);
|
expect(wsRes.status).toBe(404);
|
||||||
} finally {
|
} finally {
|
||||||
await server.close();
|
await server.close();
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("serves canvas content from the mounted base path", async () => {
|
it("serves canvas content from the mounted base path", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-"));
|
const dir = await createCaseDir();
|
||||||
await fs.writeFile(path.join(dir, "index.html"), "<html><body>v1</body></html>", "utf8");
|
await fs.writeFile(path.join(dir, "index.html"), "<html><body>v1</body></html>", "utf8");
|
||||||
|
|
||||||
const handler = await createCanvasHostHandler({
|
const handler = await createCanvasHostHandler({
|
||||||
@@ -116,12 +131,11 @@ describe("canvas host", () => {
|
|||||||
await new Promise<void>((resolve, reject) =>
|
await new Promise<void>((resolve, reject) =>
|
||||||
server.close((err) => (err ? reject(err) : resolve())),
|
server.close((err) => (err ? reject(err) : resolve())),
|
||||||
);
|
);
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("reuses a handler without closing it twice", async () => {
|
it("reuses a handler without closing it twice", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-"));
|
const dir = await createCaseDir();
|
||||||
await fs.writeFile(path.join(dir, "index.html"), "<html><body>v1</body></html>", "utf8");
|
await fs.writeFile(path.join(dir, "index.html"), "<html><body>v1</body></html>", "utf8");
|
||||||
|
|
||||||
const handler = await createCanvasHostHandler({
|
const handler = await createCanvasHostHandler({
|
||||||
@@ -149,12 +163,11 @@ describe("canvas host", () => {
|
|||||||
await server.close();
|
await server.close();
|
||||||
expect(closeSpy).not.toHaveBeenCalled();
|
expect(closeSpy).not.toHaveBeenCalled();
|
||||||
await originalClose();
|
await originalClose();
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("serves HTML with injection and broadcasts reload on file changes", async () => {
|
it("serves HTML with injection and broadcasts reload on file changes", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-"));
|
const dir = await createCaseDir();
|
||||||
const index = path.join(dir, "index.html");
|
const index = path.join(dir, "index.html");
|
||||||
await fs.writeFile(index, "<html><body>v1</body></html>", "utf8");
|
await fs.writeFile(index, "<html><body>v1</body></html>", "utf8");
|
||||||
|
|
||||||
@@ -194,18 +207,16 @@ describe("canvas host", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
||||||
await fs.writeFile(index, "<html><body>v2</body></html>", "utf8");
|
await fs.writeFile(index, "<html><body>v2</body></html>", "utf8");
|
||||||
expect(await msg).toBe("reload");
|
expect(await msg).toBe("reload");
|
||||||
ws.close();
|
ws.close();
|
||||||
} finally {
|
} finally {
|
||||||
await server.close();
|
await server.close();
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
}
|
||||||
}, 20_000);
|
}, 20_000);
|
||||||
|
|
||||||
it("serves the gateway-hosted A2UI scaffold", async () => {
|
it("serves the gateway-hosted A2UI scaffold", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-"));
|
const dir = await createCaseDir();
|
||||||
const a2uiRoot = path.resolve(process.cwd(), "src/canvas-host/a2ui");
|
const a2uiRoot = path.resolve(process.cwd(), "src/canvas-host/a2ui");
|
||||||
const bundlePath = path.join(a2uiRoot, "a2ui.bundle.js");
|
const bundlePath = path.join(a2uiRoot, "a2ui.bundle.js");
|
||||||
let createdBundle = false;
|
let createdBundle = false;
|
||||||
@@ -243,12 +254,11 @@ describe("canvas host", () => {
|
|||||||
if (createdBundle) {
|
if (createdBundle) {
|
||||||
await fs.rm(bundlePath, { force: true });
|
await fs.rm(bundlePath, { force: true });
|
||||||
}
|
}
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rejects traversal-style A2UI asset requests", async () => {
|
it("rejects traversal-style A2UI asset requests", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-"));
|
const dir = await createCaseDir();
|
||||||
const a2uiRoot = path.resolve(process.cwd(), "src/canvas-host/a2ui");
|
const a2uiRoot = path.resolve(process.cwd(), "src/canvas-host/a2ui");
|
||||||
const bundlePath = path.join(a2uiRoot, "a2ui.bundle.js");
|
const bundlePath = path.join(a2uiRoot, "a2ui.bundle.js");
|
||||||
let createdBundle = false;
|
let createdBundle = false;
|
||||||
@@ -277,12 +287,11 @@ describe("canvas host", () => {
|
|||||||
if (createdBundle) {
|
if (createdBundle) {
|
||||||
await fs.rm(bundlePath, { force: true });
|
await fs.rm(bundlePath, { force: true });
|
||||||
}
|
}
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rejects A2UI symlink escapes", async () => {
|
it("rejects A2UI symlink escapes", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-"));
|
const dir = await createCaseDir();
|
||||||
const a2uiRoot = path.resolve(process.cwd(), "src/canvas-host/a2ui");
|
const a2uiRoot = path.resolve(process.cwd(), "src/canvas-host/a2ui");
|
||||||
const bundlePath = path.join(a2uiRoot, "a2ui.bundle.js");
|
const bundlePath = path.join(a2uiRoot, "a2ui.bundle.js");
|
||||||
const linkName = `test-link-${Date.now()}-${Math.random().toString(16).slice(2)}.txt`;
|
const linkName = `test-link-${Date.now()}-${Math.random().toString(16).slice(2)}.txt`;
|
||||||
@@ -320,7 +329,6 @@ describe("canvas host", () => {
|
|||||||
if (createdBundle) {
|
if (createdBundle) {
|
||||||
await fs.rm(bundlePath, { force: true });
|
await fs.rm(bundlePath, { force: true });
|
||||||
}
|
}
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,10 +1,78 @@
|
|||||||
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 { describe, expect, it, vi } from "vitest";
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||||
import { createConfigIO } from "./io.js";
|
import { createConfigIO } from "./io.js";
|
||||||
import { withTempHome } from "./test-helpers.js";
|
|
||||||
|
type HomeEnvSnapshot = {
|
||||||
|
home: string | undefined;
|
||||||
|
userProfile: string | undefined;
|
||||||
|
homeDrive: string | undefined;
|
||||||
|
homePath: string | undefined;
|
||||||
|
stateDir: string | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
function snapshotHomeEnv(): HomeEnvSnapshot {
|
||||||
|
return {
|
||||||
|
home: process.env.HOME,
|
||||||
|
userProfile: process.env.USERPROFILE,
|
||||||
|
homeDrive: process.env.HOMEDRIVE,
|
||||||
|
homePath: process.env.HOMEPATH,
|
||||||
|
stateDir: process.env.OPENCLAW_STATE_DIR,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function restoreHomeEnv(snapshot: HomeEnvSnapshot) {
|
||||||
|
const restoreKey = (key: string, value: string | undefined) => {
|
||||||
|
if (value === undefined) {
|
||||||
|
delete process.env[key];
|
||||||
|
} else {
|
||||||
|
process.env[key] = value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
restoreKey("HOME", snapshot.home);
|
||||||
|
restoreKey("USERPROFILE", snapshot.userProfile);
|
||||||
|
restoreKey("HOMEDRIVE", snapshot.homeDrive);
|
||||||
|
restoreKey("HOMEPATH", snapshot.homePath);
|
||||||
|
restoreKey("OPENCLAW_STATE_DIR", snapshot.stateDir);
|
||||||
|
}
|
||||||
|
|
||||||
describe("config io write", () => {
|
describe("config io write", () => {
|
||||||
|
let fixtureRoot = "";
|
||||||
|
let fixtureCount = 0;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-config-io-"));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
const withTempHome = async <T>(fn: (home: string) => Promise<T>): Promise<T> => {
|
||||||
|
const home = path.join(fixtureRoot, `home-${fixtureCount++}`);
|
||||||
|
await fs.mkdir(path.join(home, ".openclaw"), { recursive: true });
|
||||||
|
|
||||||
|
const snapshot = 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(snapshot);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
it("persists caller changes onto resolved config without leaking runtime defaults", async () => {
|
it("persists caller changes onto resolved config without leaking runtime defaults", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempHome(async (home) => {
|
||||||
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
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 { beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import type { CliDeps } from "../cli/deps.js";
|
import type { CliDeps } from "../cli/deps.js";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import type { CronJob } from "./types.js";
|
import type { CronJob } from "./types.js";
|
||||||
import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js";
|
|
||||||
import { telegramOutbound } from "../channels/plugins/outbound/telegram.js";
|
import { telegramOutbound } from "../channels/plugins/outbound/telegram.js";
|
||||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||||
import { createOutboundTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js";
|
import { createOutboundTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||||
@@ -26,8 +26,13 @@ import { runEmbeddedPiAgent } from "../agents/pi-embedded.js";
|
|||||||
import { runSubagentAnnounceFlow } from "../agents/subagent-announce.js";
|
import { runSubagentAnnounceFlow } from "../agents/subagent-announce.js";
|
||||||
import { runCronIsolatedAgentTurn } from "./isolated-agent.js";
|
import { runCronIsolatedAgentTurn } from "./isolated-agent.js";
|
||||||
|
|
||||||
|
let fixtureRoot = "";
|
||||||
|
let fixtureCount = 0;
|
||||||
|
|
||||||
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
|
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
|
||||||
return withTempHomeBase(fn, { prefix: "openclaw-cron-" });
|
const home = path.join(fixtureRoot, `home-${fixtureCount++}`);
|
||||||
|
await fs.mkdir(path.join(home, ".openclaw", "agents", "main", "sessions"), { recursive: true });
|
||||||
|
return await fn(home);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function writeSessionStore(home: string) {
|
async function writeSessionStore(home: string) {
|
||||||
@@ -87,6 +92,14 @@ function makeJob(payload: CronJob["payload"]): CronJob {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("runCronIsolatedAgentTurn", () => {
|
describe("runCronIsolatedAgentTurn", () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cron-fixtures-"));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.mocked(runEmbeddedPiAgent).mockReset();
|
vi.mocked(runEmbeddedPiAgent).mockReset();
|
||||||
vi.mocked(loadModelCatalog).mockResolvedValue([]);
|
vi.mocked(loadModelCatalog).mockResolvedValue([]);
|
||||||
|
|||||||
@@ -3,12 +3,16 @@ import fsSync from "node:fs";
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||||
import { resolveConfigPath, resolveGatewayLockDir, resolveStateDir } from "../config/paths.js";
|
import { resolveConfigPath, resolveGatewayLockDir, resolveStateDir } from "../config/paths.js";
|
||||||
import { acquireGatewayLock, GatewayLockError } from "./gateway-lock.js";
|
import { acquireGatewayLock, GatewayLockError } from "./gateway-lock.js";
|
||||||
|
|
||||||
|
let fixtureRoot = "";
|
||||||
|
let fixtureCount = 0;
|
||||||
|
|
||||||
async function makeEnv() {
|
async function makeEnv() {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gateway-lock-"));
|
const dir = path.join(fixtureRoot, `case-${fixtureCount++}`);
|
||||||
|
await fs.mkdir(dir, { recursive: true });
|
||||||
const configPath = path.join(dir, "openclaw.json");
|
const configPath = path.join(dir, "openclaw.json");
|
||||||
await fs.writeFile(configPath, "{}", "utf8");
|
await fs.writeFile(configPath, "{}", "utf8");
|
||||||
await fs.mkdir(resolveGatewayLockDir(), { recursive: true });
|
await fs.mkdir(resolveGatewayLockDir(), { recursive: true });
|
||||||
@@ -18,9 +22,7 @@ async function makeEnv() {
|
|||||||
OPENCLAW_STATE_DIR: dir,
|
OPENCLAW_STATE_DIR: dir,
|
||||||
OPENCLAW_CONFIG_PATH: configPath,
|
OPENCLAW_CONFIG_PATH: configPath,
|
||||||
},
|
},
|
||||||
cleanup: async () => {
|
cleanup: async () => {},
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,13 +63,21 @@ function makeProcStat(pid: number, startTime: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("gateway lock", () => {
|
describe("gateway lock", () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gateway-lock-"));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
it("blocks concurrent acquisition until release", async () => {
|
it("blocks concurrent acquisition until release", async () => {
|
||||||
const { env, cleanup } = await makeEnv();
|
const { env, cleanup } = await makeEnv();
|
||||||
const lock = await acquireGatewayLock({
|
const lock = await acquireGatewayLock({
|
||||||
env,
|
env,
|
||||||
allowInTests: true,
|
allowInTests: true,
|
||||||
timeoutMs: 200,
|
timeoutMs: 80,
|
||||||
pollIntervalMs: 20,
|
pollIntervalMs: 5,
|
||||||
});
|
});
|
||||||
expect(lock).not.toBeNull();
|
expect(lock).not.toBeNull();
|
||||||
|
|
||||||
@@ -75,8 +85,8 @@ describe("gateway lock", () => {
|
|||||||
acquireGatewayLock({
|
acquireGatewayLock({
|
||||||
env,
|
env,
|
||||||
allowInTests: true,
|
allowInTests: true,
|
||||||
timeoutMs: 200,
|
timeoutMs: 80,
|
||||||
pollIntervalMs: 20,
|
pollIntervalMs: 5,
|
||||||
}),
|
}),
|
||||||
).rejects.toBeInstanceOf(GatewayLockError);
|
).rejects.toBeInstanceOf(GatewayLockError);
|
||||||
|
|
||||||
@@ -84,8 +94,8 @@ describe("gateway lock", () => {
|
|||||||
const lock2 = await acquireGatewayLock({
|
const lock2 = await acquireGatewayLock({
|
||||||
env,
|
env,
|
||||||
allowInTests: true,
|
allowInTests: true,
|
||||||
timeoutMs: 200,
|
timeoutMs: 80,
|
||||||
pollIntervalMs: 20,
|
pollIntervalMs: 5,
|
||||||
});
|
});
|
||||||
await lock2?.release();
|
await lock2?.release();
|
||||||
await cleanup();
|
await cleanup();
|
||||||
@@ -114,8 +124,8 @@ describe("gateway lock", () => {
|
|||||||
const lock = await acquireGatewayLock({
|
const lock = await acquireGatewayLock({
|
||||||
env,
|
env,
|
||||||
allowInTests: true,
|
allowInTests: true,
|
||||||
timeoutMs: 200,
|
timeoutMs: 80,
|
||||||
pollIntervalMs: 20,
|
pollIntervalMs: 5,
|
||||||
platform: "linux",
|
platform: "linux",
|
||||||
});
|
});
|
||||||
expect(lock).not.toBeNull();
|
expect(lock).not.toBeNull();
|
||||||
@@ -148,8 +158,8 @@ describe("gateway lock", () => {
|
|||||||
acquireGatewayLock({
|
acquireGatewayLock({
|
||||||
env,
|
env,
|
||||||
allowInTests: true,
|
allowInTests: true,
|
||||||
timeoutMs: 120,
|
timeoutMs: 50,
|
||||||
pollIntervalMs: 20,
|
pollIntervalMs: 5,
|
||||||
staleMs: 10_000,
|
staleMs: 10_000,
|
||||||
platform: "linux",
|
platform: "linux",
|
||||||
}),
|
}),
|
||||||
@@ -173,8 +183,8 @@ describe("gateway lock", () => {
|
|||||||
const lock = await acquireGatewayLock({
|
const lock = await acquireGatewayLock({
|
||||||
env,
|
env,
|
||||||
allowInTests: true,
|
allowInTests: true,
|
||||||
timeoutMs: 200,
|
timeoutMs: 80,
|
||||||
pollIntervalMs: 20,
|
pollIntervalMs: 5,
|
||||||
staleMs: 1,
|
staleMs: 1,
|
||||||
platform: "linux",
|
platform: "linux",
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { getMemorySearchManager, type MemoryIndexManager } from "./index.js";
|
import { getMemorySearchManager, type MemoryIndexManager } from "./index.js";
|
||||||
|
|
||||||
let embedBatchCalls = 0;
|
let embedBatchCalls = 0;
|
||||||
@@ -34,14 +34,25 @@ vi.mock("./embeddings.js", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("memory index", () => {
|
describe("memory index", () => {
|
||||||
|
let fixtureRoot = "";
|
||||||
|
let fixtureCount = 0;
|
||||||
let workspaceDir: string;
|
let workspaceDir: string;
|
||||||
let indexPath: string;
|
let indexPath: string;
|
||||||
let manager: MemoryIndexManager | null = null;
|
let manager: MemoryIndexManager | null = null;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-mem-fixtures-"));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
embedBatchCalls = 0;
|
embedBatchCalls = 0;
|
||||||
failEmbeddings = false;
|
failEmbeddings = false;
|
||||||
workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-mem-"));
|
workspaceDir = path.join(fixtureRoot, `case-${fixtureCount++}`);
|
||||||
|
await fs.mkdir(workspaceDir, { recursive: true });
|
||||||
indexPath = path.join(workspaceDir, "index.sqlite");
|
indexPath = path.join(workspaceDir, "index.sqlite");
|
||||||
await fs.mkdir(path.join(workspaceDir, "memory"));
|
await fs.mkdir(path.join(workspaceDir, "memory"));
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
@@ -56,7 +67,6 @@ describe("memory index", () => {
|
|||||||
await manager.close();
|
await manager.close();
|
||||||
manager = null;
|
manager = null;
|
||||||
}
|
}
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("indexes memory files and searches by vector", async () => {
|
it("indexes memory files and searches by vector", async () => {
|
||||||
@@ -270,7 +280,7 @@ describe("memory index", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("hybrid weights can favor vector-only matches over keyword-only matches", async () => {
|
it("hybrid weights can favor vector-only matches over keyword-only matches", async () => {
|
||||||
const manyAlpha = Array.from({ length: 200 }, () => "Alpha").join(" ");
|
const manyAlpha = Array.from({ length: 80 }, () => "Alpha").join(" ");
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
path.join(workspaceDir, "memory", "vector-only.md"),
|
path.join(workspaceDir, "memory", "vector-only.md"),
|
||||||
"Alpha beta. Alpha beta. Alpha beta. Alpha beta.",
|
"Alpha beta. Alpha beta. Alpha beta. Alpha beta.",
|
||||||
@@ -328,7 +338,7 @@ describe("memory index", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("hybrid weights can favor keyword matches when text weight dominates", async () => {
|
it("hybrid weights can favor keyword matches when text weight dominates", async () => {
|
||||||
const manyAlpha = Array.from({ length: 200 }, () => "Alpha").join(" ");
|
const manyAlpha = Array.from({ length: 80 }, () => "Alpha").join(" ");
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
path.join(workspaceDir, "memory", "vector-only.md"),
|
path.join(workspaceDir, "memory", "vector-only.md"),
|
||||||
"Alpha beta. Alpha beta. Alpha beta. Alpha beta.",
|
"Alpha beta. Alpha beta. Alpha beta. Alpha beta.",
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { EventEmitter } from "node:events";
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
const { logWarnMock, logDebugMock, logInfoMock } = vi.hoisted(() => ({
|
const { logWarnMock, logDebugMock, logInfoMock } = vi.hoisted(() => ({
|
||||||
logWarnMock: vi.fn(),
|
logWarnMock: vi.fn(),
|
||||||
@@ -44,6 +44,18 @@ function createMockChild(params?: { autoClose?: boolean; closeDelayMs?: number }
|
|||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function emitAndClose(
|
||||||
|
child: MockChild,
|
||||||
|
stream: "stdout" | "stderr",
|
||||||
|
data: string,
|
||||||
|
code: number = 0,
|
||||||
|
) {
|
||||||
|
queueMicrotask(() => {
|
||||||
|
child[stream].emit("data", data);
|
||||||
|
child.closeWith(code);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
vi.mock("../logging/subsystem.js", () => ({
|
vi.mock("../logging/subsystem.js", () => ({
|
||||||
createSubsystemLogger: () => {
|
createSubsystemLogger: () => {
|
||||||
const logger = {
|
const logger = {
|
||||||
@@ -66,19 +78,30 @@ import { QmdMemoryManager } from "./qmd-manager.js";
|
|||||||
const spawnMock = mockedSpawn as unknown as vi.Mock;
|
const spawnMock = mockedSpawn as unknown as vi.Mock;
|
||||||
|
|
||||||
describe("QmdMemoryManager", () => {
|
describe("QmdMemoryManager", () => {
|
||||||
|
let fixtureRoot: string;
|
||||||
|
let fixtureCount = 0;
|
||||||
let tmpRoot: string;
|
let tmpRoot: string;
|
||||||
let workspaceDir: string;
|
let workspaceDir: string;
|
||||||
let stateDir: string;
|
let stateDir: string;
|
||||||
let cfg: OpenClawConfig;
|
let cfg: OpenClawConfig;
|
||||||
const agentId = "main";
|
const agentId = "main";
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "qmd-manager-test-fixtures-"));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
spawnMock.mockReset();
|
spawnMock.mockReset();
|
||||||
spawnMock.mockImplementation(() => createMockChild());
|
spawnMock.mockImplementation(() => createMockChild());
|
||||||
logWarnMock.mockReset();
|
logWarnMock.mockReset();
|
||||||
logDebugMock.mockReset();
|
logDebugMock.mockReset();
|
||||||
logInfoMock.mockReset();
|
logInfoMock.mockReset();
|
||||||
tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "qmd-manager-test-"));
|
tmpRoot = path.join(fixtureRoot, `case-${fixtureCount++}`);
|
||||||
|
await fs.mkdir(tmpRoot, { recursive: true });
|
||||||
workspaceDir = path.join(tmpRoot, "workspace");
|
workspaceDir = path.join(tmpRoot, "workspace");
|
||||||
await fs.mkdir(workspaceDir, { recursive: true });
|
await fs.mkdir(workspaceDir, { recursive: true });
|
||||||
stateDir = path.join(tmpRoot, "state");
|
stateDir = path.join(tmpRoot, "state");
|
||||||
@@ -102,7 +125,6 @@ describe("QmdMemoryManager", () => {
|
|||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
vi.useRealTimers();
|
vi.useRealTimers();
|
||||||
delete process.env.OPENCLAW_STATE_DIR;
|
delete process.env.OPENCLAW_STATE_DIR;
|
||||||
await fs.rm(tmpRoot, { recursive: true, force: true });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("debounces back-to-back sync calls", async () => {
|
it("debounces back-to-back sync calls", async () => {
|
||||||
@@ -158,14 +180,11 @@ describe("QmdMemoryManager", () => {
|
|||||||
const createPromise = QmdMemoryManager.create({ cfg, agentId, resolved });
|
const createPromise = QmdMemoryManager.create({ cfg, agentId, resolved });
|
||||||
const race = await Promise.race([
|
const race = await Promise.race([
|
||||||
createPromise.then(() => "created" as const),
|
createPromise.then(() => "created" as const),
|
||||||
new Promise<"timeout">((resolve) => setTimeout(() => resolve("timeout"), 80)),
|
new Promise<"timeout">((resolve) => setTimeout(() => resolve("timeout"), 40)),
|
||||||
]);
|
]);
|
||||||
expect(race).toBe("created");
|
expect(race).toBe("created");
|
||||||
|
await waitForCondition(() => releaseUpdate !== null, 200);
|
||||||
if (!releaseUpdate) {
|
releaseUpdate?.();
|
||||||
throw new Error("update child missing");
|
|
||||||
}
|
|
||||||
releaseUpdate();
|
|
||||||
const manager = await createPromise;
|
const manager = await createPromise;
|
||||||
await manager?.close();
|
await manager?.close();
|
||||||
});
|
});
|
||||||
@@ -202,14 +221,11 @@ describe("QmdMemoryManager", () => {
|
|||||||
const createPromise = QmdMemoryManager.create({ cfg, agentId, resolved });
|
const createPromise = QmdMemoryManager.create({ cfg, agentId, resolved });
|
||||||
const race = await Promise.race([
|
const race = await Promise.race([
|
||||||
createPromise.then(() => "created" as const),
|
createPromise.then(() => "created" as const),
|
||||||
new Promise<"timeout">((resolve) => setTimeout(() => resolve("timeout"), 80)),
|
new Promise<"timeout">((resolve) => setTimeout(() => resolve("timeout"), 40)),
|
||||||
]);
|
]);
|
||||||
expect(race).toBe("timeout");
|
expect(race).toBe("timeout");
|
||||||
|
await waitForCondition(() => releaseUpdate !== null, 200);
|
||||||
if (!releaseUpdate) {
|
releaseUpdate?.();
|
||||||
throw new Error("update child missing");
|
|
||||||
}
|
|
||||||
releaseUpdate();
|
|
||||||
const manager = await createPromise;
|
const manager = await createPromise;
|
||||||
await manager?.close();
|
await manager?.close();
|
||||||
});
|
});
|
||||||
@@ -301,10 +317,7 @@ describe("QmdMemoryManager", () => {
|
|||||||
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
||||||
if (args[0] === "search") {
|
if (args[0] === "search") {
|
||||||
const child = createMockChild({ autoClose: false });
|
const child = createMockChild({ autoClose: false });
|
||||||
setTimeout(() => {
|
emitAndClose(child, "stdout", "[]");
|
||||||
child.stdout.emit("data", "[]");
|
|
||||||
child.closeWith(0);
|
|
||||||
}, 0);
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
return createMockChild();
|
return createMockChild();
|
||||||
@@ -348,18 +361,12 @@ describe("QmdMemoryManager", () => {
|
|||||||
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
||||||
if (args[0] === "search") {
|
if (args[0] === "search") {
|
||||||
const child = createMockChild({ autoClose: false });
|
const child = createMockChild({ autoClose: false });
|
||||||
setTimeout(() => {
|
emitAndClose(child, "stderr", "unknown flag: --json", 2);
|
||||||
child.stderr.emit("data", "unknown flag: --json");
|
|
||||||
child.closeWith(2);
|
|
||||||
}, 0);
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
if (args[0] === "query") {
|
if (args[0] === "query") {
|
||||||
const child = createMockChild({ autoClose: false });
|
const child = createMockChild({ autoClose: false });
|
||||||
setTimeout(() => {
|
emitAndClose(child, "stdout", "[]");
|
||||||
child.stdout.emit("data", "[]");
|
|
||||||
child.closeWith(0);
|
|
||||||
}, 0);
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
return createMockChild();
|
return createMockChild();
|
||||||
@@ -435,7 +442,7 @@ describe("QmdMemoryManager", () => {
|
|||||||
const inFlight = manager.sync({ reason: "interval" });
|
const inFlight = manager.sync({ reason: "interval" });
|
||||||
const forced = manager.sync({ reason: "manual", force: true });
|
const forced = manager.sync({ reason: "manual", force: true });
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
await waitForCondition(() => updateCalls >= 1, 80);
|
||||||
expect(updateCalls).toBe(1);
|
expect(updateCalls).toBe(1);
|
||||||
if (!releaseFirstUpdate) {
|
if (!releaseFirstUpdate) {
|
||||||
throw new Error("first update release missing");
|
throw new Error("first update release missing");
|
||||||
@@ -496,14 +503,14 @@ describe("QmdMemoryManager", () => {
|
|||||||
const inFlight = manager.sync({ reason: "interval" });
|
const inFlight = manager.sync({ reason: "interval" });
|
||||||
const forcedOne = manager.sync({ reason: "manual", force: true });
|
const forcedOne = manager.sync({ reason: "manual", force: true });
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
await waitForCondition(() => updateCalls >= 1, 80);
|
||||||
expect(updateCalls).toBe(1);
|
expect(updateCalls).toBe(1);
|
||||||
if (!releaseFirstUpdate) {
|
if (!releaseFirstUpdate) {
|
||||||
throw new Error("first update release missing");
|
throw new Error("first update release missing");
|
||||||
}
|
}
|
||||||
releaseFirstUpdate();
|
releaseFirstUpdate();
|
||||||
|
|
||||||
await waitForCondition(() => updateCalls >= 2, 200);
|
await waitForCondition(() => updateCalls >= 2, 120);
|
||||||
const forcedTwo = manager.sync({ reason: "manual-again", force: true });
|
const forcedTwo = manager.sync({ reason: "manual-again", force: true });
|
||||||
|
|
||||||
if (!releaseSecondUpdate) {
|
if (!releaseSecondUpdate) {
|
||||||
@@ -535,10 +542,7 @@ describe("QmdMemoryManager", () => {
|
|||||||
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
||||||
if (args[0] === "query") {
|
if (args[0] === "query") {
|
||||||
const child = createMockChild({ autoClose: false });
|
const child = createMockChild({ autoClose: false });
|
||||||
setTimeout(() => {
|
emitAndClose(child, "stdout", "[]");
|
||||||
child.stdout.emit("data", "[]");
|
|
||||||
child.closeWith(0);
|
|
||||||
}, 0);
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
return createMockChild();
|
return createMockChild();
|
||||||
@@ -805,13 +809,11 @@ describe("QmdMemoryManager", () => {
|
|||||||
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
||||||
if (args[0] === "query") {
|
if (args[0] === "query") {
|
||||||
const child = createMockChild({ autoClose: false });
|
const child = createMockChild({ autoClose: false });
|
||||||
setTimeout(() => {
|
emitAndClose(
|
||||||
child.stdout.emit(
|
child,
|
||||||
"data",
|
"stdout",
|
||||||
JSON.stringify([{ docid: "abc123", score: 1, snippet: "@@ -1,1\nremember this" }]),
|
JSON.stringify([{ docid: "abc123", score: 1, snippet: "@@ -1,1\nremember this" }]),
|
||||||
);
|
);
|
||||||
child.closeWith(0);
|
|
||||||
}, 0);
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
return createMockChild();
|
return createMockChild();
|
||||||
@@ -844,10 +846,7 @@ describe("QmdMemoryManager", () => {
|
|||||||
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
||||||
if (args[0] === "query") {
|
if (args[0] === "query") {
|
||||||
const child = createMockChild({ autoClose: false });
|
const child = createMockChild({ autoClose: false });
|
||||||
setTimeout(() => {
|
emitAndClose(child, "stdout", "No results found.");
|
||||||
child.stdout.emit("data", "No results found.");
|
|
||||||
child.closeWith(0);
|
|
||||||
}, 0);
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
return createMockChild();
|
return createMockChild();
|
||||||
@@ -870,10 +869,7 @@ describe("QmdMemoryManager", () => {
|
|||||||
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
||||||
if (args[0] === "query") {
|
if (args[0] === "query") {
|
||||||
const child = createMockChild({ autoClose: false });
|
const child = createMockChild({ autoClose: false });
|
||||||
setTimeout(() => {
|
emitAndClose(child, "stdout", "No results found\n\n");
|
||||||
child.stdout.emit("data", "No results found\n\n");
|
|
||||||
child.closeWith(0);
|
|
||||||
}, 0);
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
return createMockChild();
|
return createMockChild();
|
||||||
@@ -896,10 +892,7 @@ describe("QmdMemoryManager", () => {
|
|||||||
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
||||||
if (args[0] === "query") {
|
if (args[0] === "query") {
|
||||||
const child = createMockChild({ autoClose: false });
|
const child = createMockChild({ autoClose: false });
|
||||||
setTimeout(() => {
|
emitAndClose(child, "stderr", "No results found.\n");
|
||||||
child.stderr.emit("data", "No results found.\n");
|
|
||||||
child.closeWith(0);
|
|
||||||
}, 0);
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
return createMockChild();
|
return createMockChild();
|
||||||
@@ -922,11 +915,11 @@ describe("QmdMemoryManager", () => {
|
|||||||
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
spawnMock.mockImplementation((_cmd: string, args: string[]) => {
|
||||||
if (args[0] === "query") {
|
if (args[0] === "query") {
|
||||||
const child = createMockChild({ autoClose: false });
|
const child = createMockChild({ autoClose: false });
|
||||||
setTimeout(() => {
|
queueMicrotask(() => {
|
||||||
child.stdout.emit("data", " \n");
|
child.stdout.emit("data", " \n");
|
||||||
child.stderr.emit("data", "unexpected parser error");
|
child.stderr.emit("data", "unexpected parser error");
|
||||||
child.closeWith(0);
|
child.closeWith(0);
|
||||||
}, 0);
|
});
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
return createMockChild();
|
return createMockChild();
|
||||||
@@ -1034,7 +1027,7 @@ async function waitForCondition(check: () => boolean, timeoutMs: number): Promis
|
|||||||
if (check()) {
|
if (check()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
await new Promise((resolve) => setTimeout(resolve, 2));
|
||||||
}
|
}
|
||||||
throw new Error("condition was not met in time");
|
throw new Error("condition was not met in time");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
|
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
|
||||||
import { expectInboundContextContract } from "../../test/helpers/inbound-contract.js";
|
import { expectInboundContextContract } from "../../test/helpers/inbound-contract.js";
|
||||||
import {
|
import {
|
||||||
@@ -23,6 +23,13 @@ vi.mock("../auto-reply/skill-commands.js", () => ({
|
|||||||
const { sessionStorePath } = vi.hoisted(() => ({
|
const { sessionStorePath } = vi.hoisted(() => ({
|
||||||
sessionStorePath: `/tmp/openclaw-telegram-bot-${Math.random().toString(16).slice(2)}.json`,
|
sessionStorePath: `/tmp/openclaw-telegram-bot-${Math.random().toString(16).slice(2)}.json`,
|
||||||
}));
|
}));
|
||||||
|
const tempDirs: string[] = [];
|
||||||
|
|
||||||
|
function createTempDir(prefix: string): string {
|
||||||
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||||
|
tempDirs.push(dir);
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
function resolveSkillCommands(config: Parameters<typeof listNativeCommandSpecsForConfig>[0]) {
|
function resolveSkillCommands(config: Parameters<typeof listNativeCommandSpecsForConfig>[0]) {
|
||||||
return listSkillCommandsForAgents({ cfg: config });
|
return listSkillCommandsForAgents({ cfg: config });
|
||||||
@@ -208,6 +215,13 @@ describe("createTelegramBot", () => {
|
|||||||
process.env.TZ = ORIGINAL_TZ;
|
process.env.TZ = ORIGINAL_TZ;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
for (const dir of tempDirs) {
|
||||||
|
fs.rmSync(dir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
tempDirs.length = 0;
|
||||||
|
});
|
||||||
|
|
||||||
it("installs grammY throttler", () => {
|
it("installs grammY throttler", () => {
|
||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
expect(throttlerSpy).toHaveBeenCalledTimes(1);
|
expect(throttlerSpy).toHaveBeenCalledTimes(1);
|
||||||
@@ -1214,7 +1228,7 @@ describe("createTelegramBot", () => {
|
|||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
||||||
replySpy.mockReset();
|
replySpy.mockReset();
|
||||||
const storeDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-"));
|
const storeDir = createTempDir("openclaw-telegram-");
|
||||||
const storePath = path.join(storeDir, "sessions.json");
|
const storePath = path.join(storeDir, "sessions.json");
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
storePath,
|
storePath,
|
||||||
|
|||||||
+22
-47
@@ -9,6 +9,8 @@ import { loadWebMedia, loadWebMediaRaw, optimizeImageToJpeg } from "./media.js";
|
|||||||
|
|
||||||
let fixtureRoot = "";
|
let fixtureRoot = "";
|
||||||
let fixtureFileCount = 0;
|
let fixtureFileCount = 0;
|
||||||
|
let largeJpegBuffer: Buffer;
|
||||||
|
let tinyPngBuffer: Buffer;
|
||||||
|
|
||||||
async function writeTempFile(buffer: Buffer, ext: string): Promise<string> {
|
async function writeTempFile(buffer: Buffer, ext: string): Promise<string> {
|
||||||
const file = path.join(fixtureRoot, `media-${fixtureFileCount++}${ext}`);
|
const file = path.join(fixtureRoot, `media-${fixtureFileCount++}${ext}`);
|
||||||
@@ -27,23 +29,27 @@ function buildDeterministicBytes(length: number): Buffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createLargeTestJpeg(): Promise<{ buffer: Buffer; file: string }> {
|
async function createLargeTestJpeg(): Promise<{ buffer: Buffer; file: string }> {
|
||||||
const buffer = await sharp({
|
const file = await writeTempFile(largeJpegBuffer, ".jpg");
|
||||||
|
return { buffer: largeJpegBuffer, file };
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-test-"));
|
||||||
|
largeJpegBuffer = await sharp({
|
||||||
create: {
|
create: {
|
||||||
width: 1600,
|
width: 1200,
|
||||||
height: 1600,
|
height: 1200,
|
||||||
channels: 3,
|
channels: 3,
|
||||||
background: "#ff0000",
|
background: "#ff0000",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.jpeg({ quality: 95 })
|
.jpeg({ quality: 95 })
|
||||||
.toBuffer();
|
.toBuffer();
|
||||||
|
tinyPngBuffer = await sharp({
|
||||||
const file = await writeTempFile(buffer, ".jpg");
|
create: { width: 10, height: 10, channels: 3, background: "#00ff00" },
|
||||||
return { buffer, file };
|
})
|
||||||
}
|
.png()
|
||||||
|
.toBuffer();
|
||||||
beforeAll(async () => {
|
|
||||||
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-test-"));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
@@ -68,18 +74,7 @@ describe("web media loading", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("compresses large local images under the provided cap", async () => {
|
it("compresses large local images under the provided cap", async () => {
|
||||||
const buffer = await sharp({
|
const { buffer, file } = await createLargeTestJpeg();
|
||||||
create: {
|
|
||||||
width: 1200,
|
|
||||||
height: 1200,
|
|
||||||
channels: 3,
|
|
||||||
background: "#ff0000",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.jpeg({ quality: 95 })
|
|
||||||
.toBuffer();
|
|
||||||
|
|
||||||
const file = await writeTempFile(buffer, ".jpg");
|
|
||||||
|
|
||||||
const cap = Math.floor(buffer.length * 0.8);
|
const cap = Math.floor(buffer.length * 0.8);
|
||||||
const result = await loadWebMedia(file, cap);
|
const result = await loadWebMedia(file, cap);
|
||||||
@@ -109,12 +104,7 @@ describe("web media loading", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("sniffs mime before extension when loading local files", async () => {
|
it("sniffs mime before extension when loading local files", async () => {
|
||||||
const pngBuffer = await sharp({
|
const wrongExt = await writeTempFile(tinyPngBuffer, ".bin");
|
||||||
create: { width: 2, height: 2, channels: 3, background: "#00ff00" },
|
|
||||||
})
|
|
||||||
.png()
|
|
||||||
.toBuffer();
|
|
||||||
const wrongExt = await writeTempFile(pngBuffer, ".bin");
|
|
||||||
|
|
||||||
const result = await loadWebMedia(wrongExt, 1024 * 1024);
|
const result = await loadWebMedia(wrongExt, 1024 * 1024);
|
||||||
|
|
||||||
@@ -292,7 +282,7 @@ describe("web media loading", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("falls back to JPEG when PNG alpha cannot fit under cap", async () => {
|
it("falls back to JPEG when PNG alpha cannot fit under cap", async () => {
|
||||||
const sizes = [320, 448, 640];
|
const sizes = [256, 320, 448];
|
||||||
let pngBuffer: Buffer | null = null;
|
let pngBuffer: Buffer | null = null;
|
||||||
let smallestPng: Awaited<ReturnType<typeof optimizeImageToPng>> | null = null;
|
let smallestPng: Awaited<ReturnType<typeof optimizeImageToPng>> | null = null;
|
||||||
let jpegOptimized: Awaited<ReturnType<typeof optimizeImageToJpeg>> | null = null;
|
let jpegOptimized: Awaited<ReturnType<typeof optimizeImageToJpeg>> | null = null;
|
||||||
@@ -333,12 +323,7 @@ describe("web media loading", () => {
|
|||||||
|
|
||||||
describe("local media root guard", () => {
|
describe("local media root guard", () => {
|
||||||
it("rejects local paths outside allowed roots", async () => {
|
it("rejects local paths outside allowed roots", async () => {
|
||||||
const pngBuffer = await sharp({
|
const file = await writeTempFile(tinyPngBuffer, ".png");
|
||||||
create: { width: 10, height: 10, channels: 3, background: "#00ff00" },
|
|
||||||
})
|
|
||||||
.png()
|
|
||||||
.toBuffer();
|
|
||||||
const file = await writeTempFile(pngBuffer, ".png");
|
|
||||||
|
|
||||||
// Explicit roots that don't contain the temp file.
|
// Explicit roots that don't contain the temp file.
|
||||||
await expect(
|
await expect(
|
||||||
@@ -347,24 +332,14 @@ describe("local media root guard", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("allows local paths under an explicit root", async () => {
|
it("allows local paths under an explicit root", async () => {
|
||||||
const pngBuffer = await sharp({
|
const file = await writeTempFile(tinyPngBuffer, ".png");
|
||||||
create: { width: 10, height: 10, channels: 3, background: "#00ff00" },
|
|
||||||
})
|
|
||||||
.png()
|
|
||||||
.toBuffer();
|
|
||||||
const file = await writeTempFile(pngBuffer, ".png");
|
|
||||||
|
|
||||||
const result = await loadWebMedia(file, 1024 * 1024, { localRoots: [os.tmpdir()] });
|
const result = await loadWebMedia(file, 1024 * 1024, { localRoots: [os.tmpdir()] });
|
||||||
expect(result.kind).toBe("image");
|
expect(result.kind).toBe("image");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("allows any path when localRoots is 'any'", async () => {
|
it("allows any path when localRoots is 'any'", async () => {
|
||||||
const pngBuffer = await sharp({
|
const file = await writeTempFile(tinyPngBuffer, ".png");
|
||||||
create: { width: 10, height: 10, channels: 3, background: "#00ff00" },
|
|
||||||
})
|
|
||||||
.png()
|
|
||||||
.toBuffer();
|
|
||||||
const file = await writeTempFile(pngBuffer, ".png");
|
|
||||||
|
|
||||||
const result = await loadWebMedia(file, 1024 * 1024, { localRoots: "any" });
|
const result = await loadWebMedia(file, 1024 * 1024, { localRoots: "any" });
|
||||||
expect(result.kind).toBe("image");
|
expect(result.kind).toBe("image");
|
||||||
|
|||||||
Reference in New Issue
Block a user