mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 17:01:53 +03:00
refactor(test): migrate web auto-reply tests to harness
This commit is contained in:
+10
-89
@@ -1,98 +1,19 @@
|
|||||||
import "./test-helpers.js";
|
import "./test-helpers.js";
|
||||||
import fs from "node:fs/promises";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
||||||
|
|
||||||
vi.mock("../agents/pi-embedded.js", () => ({
|
|
||||||
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
|
|
||||||
runEmbeddedPiAgent: vi.fn(),
|
|
||||||
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
|
|
||||||
resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
|
||||||
import { monitorWebChannel } from "./auto-reply.js";
|
import { monitorWebChannel } from "./auto-reply.js";
|
||||||
import { resetLoadConfigMock, setLoadConfigMock } from "./test-helpers.js";
|
import {
|
||||||
|
installWebAutoReplyTestHomeHooks,
|
||||||
|
installWebAutoReplyUnitTestHooks,
|
||||||
|
resetLoadConfigMock,
|
||||||
|
setLoadConfigMock,
|
||||||
|
} from "./auto-reply.test-harness.js";
|
||||||
|
|
||||||
let previousHome: string | undefined;
|
installWebAutoReplyTestHomeHooks();
|
||||||
let tempHome: string | undefined;
|
|
||||||
|
|
||||||
const rmDirWithRetries = async (dir: string): Promise<void> => {
|
|
||||||
// Some tests can leave async session-store writes in-flight; recursive deletion can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
previousHome = process.env.HOME;
|
|
||||||
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-web-home-"));
|
|
||||||
process.env.HOME = tempHome;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
process.env.HOME = previousHome;
|
|
||||||
if (tempHome) {
|
|
||||||
await rmDirWithRetries(tempHome);
|
|
||||||
tempHome = undefined;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const _makeSessionStore = async (
|
|
||||||
entries: Record<string, unknown> = {},
|
|
||||||
): Promise<{ storePath: string; cleanup: () => Promise<void> }> => {
|
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-"));
|
|
||||||
const storePath = path.join(dir, "sessions.json");
|
|
||||||
await fs.writeFile(storePath, JSON.stringify(entries));
|
|
||||||
const cleanup = async () => {
|
|
||||||
// Session store writes can be in-flight when the test finishes (e.g. updateLastRoute
|
|
||||||
// after a message flush). `fs.rm({ recursive })` can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
storePath,
|
|
||||||
cleanup,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("broadcast groups", () => {
|
describe("broadcast groups", () => {
|
||||||
|
installWebAutoReplyUnitTestHooks();
|
||||||
|
|
||||||
it("broadcasts sequentially in configured order", async () => {
|
it("broadcasts sequentially in configured order", async () => {
|
||||||
setLoadConfigMock({
|
setLoadConfigMock({
|
||||||
channels: { whatsapp: { allowFrom: ["*"] } },
|
channels: { whatsapp: { allowFrom: ["*"] } },
|
||||||
|
|||||||
+10
-89
@@ -1,98 +1,19 @@
|
|||||||
import "./test-helpers.js";
|
import "./test-helpers.js";
|
||||||
import fs from "node:fs/promises";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
||||||
|
|
||||||
vi.mock("../agents/pi-embedded.js", () => ({
|
|
||||||
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
|
|
||||||
runEmbeddedPiAgent: vi.fn(),
|
|
||||||
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
|
|
||||||
resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
|
||||||
import { monitorWebChannel } from "./auto-reply.js";
|
import { monitorWebChannel } from "./auto-reply.js";
|
||||||
import { resetLoadConfigMock, setLoadConfigMock } from "./test-helpers.js";
|
import {
|
||||||
|
installWebAutoReplyTestHomeHooks,
|
||||||
|
installWebAutoReplyUnitTestHooks,
|
||||||
|
resetLoadConfigMock,
|
||||||
|
setLoadConfigMock,
|
||||||
|
} from "./auto-reply.test-harness.js";
|
||||||
|
|
||||||
let previousHome: string | undefined;
|
installWebAutoReplyTestHomeHooks();
|
||||||
let tempHome: string | undefined;
|
|
||||||
|
|
||||||
const rmDirWithRetries = async (dir: string): Promise<void> => {
|
|
||||||
// Some tests can leave async session-store writes in-flight; recursive deletion can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
previousHome = process.env.HOME;
|
|
||||||
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-web-home-"));
|
|
||||||
process.env.HOME = tempHome;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
process.env.HOME = previousHome;
|
|
||||||
if (tempHome) {
|
|
||||||
await rmDirWithRetries(tempHome);
|
|
||||||
tempHome = undefined;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const _makeSessionStore = async (
|
|
||||||
entries: Record<string, unknown> = {},
|
|
||||||
): Promise<{ storePath: string; cleanup: () => Promise<void> }> => {
|
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-"));
|
|
||||||
const storePath = path.join(dir, "sessions.json");
|
|
||||||
await fs.writeFile(storePath, JSON.stringify(entries));
|
|
||||||
const cleanup = async () => {
|
|
||||||
// Session store writes can be in-flight when the test finishes (e.g. updateLastRoute
|
|
||||||
// after a message flush). `fs.rm({ recursive })` can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
storePath,
|
|
||||||
cleanup,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("broadcast groups", () => {
|
describe("broadcast groups", () => {
|
||||||
|
installWebAutoReplyUnitTestHooks();
|
||||||
|
|
||||||
it("skips unknown broadcast agent ids when agents.list is present", async () => {
|
it("skips unknown broadcast agent ids when agents.list is present", async () => {
|
||||||
setLoadConfigMock({
|
setLoadConfigMock({
|
||||||
channels: { whatsapp: { allowFrom: ["*"] } },
|
channels: { whatsapp: { allowFrom: ["*"] } },
|
||||||
|
|||||||
@@ -1,98 +1,19 @@
|
|||||||
import "./test-helpers.js";
|
import "./test-helpers.js";
|
||||||
import fs from "node:fs/promises";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
||||||
|
|
||||||
vi.mock("../agents/pi-embedded.js", () => ({
|
|
||||||
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
|
|
||||||
runEmbeddedPiAgent: vi.fn(),
|
|
||||||
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
|
|
||||||
resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
|
||||||
import { monitorWebChannel } from "./auto-reply.js";
|
import { monitorWebChannel } from "./auto-reply.js";
|
||||||
import { resetLoadConfigMock, setLoadConfigMock } from "./test-helpers.js";
|
import {
|
||||||
|
installWebAutoReplyTestHomeHooks,
|
||||||
|
installWebAutoReplyUnitTestHooks,
|
||||||
|
resetLoadConfigMock,
|
||||||
|
setLoadConfigMock,
|
||||||
|
} from "./auto-reply.test-harness.js";
|
||||||
|
|
||||||
let previousHome: string | undefined;
|
installWebAutoReplyTestHomeHooks();
|
||||||
let tempHome: string | undefined;
|
|
||||||
|
|
||||||
const rmDirWithRetries = async (dir: string): Promise<void> => {
|
|
||||||
// Some tests can leave async session-store writes in-flight; recursive deletion can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
previousHome = process.env.HOME;
|
|
||||||
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-web-home-"));
|
|
||||||
process.env.HOME = tempHome;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
process.env.HOME = previousHome;
|
|
||||||
if (tempHome) {
|
|
||||||
await rmDirWithRetries(tempHome);
|
|
||||||
tempHome = undefined;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const _makeSessionStore = async (
|
|
||||||
entries: Record<string, unknown> = {},
|
|
||||||
): Promise<{ storePath: string; cleanup: () => Promise<void> }> => {
|
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-"));
|
|
||||||
const storePath = path.join(dir, "sessions.json");
|
|
||||||
await fs.writeFile(storePath, JSON.stringify(entries));
|
|
||||||
const cleanup = async () => {
|
|
||||||
// Session store writes can be in-flight when the test finishes (e.g. updateLastRoute
|
|
||||||
// after a message flush). `fs.rm({ recursive })` can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
storePath,
|
|
||||||
cleanup,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("typing controller idle", () => {
|
describe("typing controller idle", () => {
|
||||||
|
installWebAutoReplyUnitTestHooks();
|
||||||
|
|
||||||
it("marks dispatch idle after replies flush", async () => {
|
it("marks dispatch idle after replies flush", async () => {
|
||||||
const markDispatchIdle = vi.fn();
|
const markDispatchIdle = vi.fn();
|
||||||
const typingMock = {
|
const typingMock = {
|
||||||
|
|||||||
@@ -1,109 +1,17 @@
|
|||||||
import "./test-helpers.js";
|
import "./test-helpers.js";
|
||||||
import fs from "node:fs/promises";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
||||||
|
|
||||||
vi.mock("../agents/pi-embedded.js", () => ({
|
|
||||||
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
|
|
||||||
runEmbeddedPiAgent: vi.fn(),
|
|
||||||
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
|
|
||||||
resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
|
||||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
|
||||||
import { HEARTBEAT_TOKEN, monitorWebChannel } from "./auto-reply.js";
|
import { HEARTBEAT_TOKEN, monitorWebChannel } from "./auto-reply.js";
|
||||||
import { resetBaileysMocks, resetLoadConfigMock, setLoadConfigMock } from "./test-helpers.js";
|
import {
|
||||||
|
installWebAutoReplyTestHomeHooks,
|
||||||
|
installWebAutoReplyUnitTestHooks,
|
||||||
|
resetLoadConfigMock,
|
||||||
|
setLoadConfigMock,
|
||||||
|
} from "./auto-reply.test-harness.js";
|
||||||
|
|
||||||
let previousHome: string | undefined;
|
installWebAutoReplyTestHomeHooks();
|
||||||
let tempHome: string | undefined;
|
|
||||||
|
|
||||||
const rmDirWithRetries = async (dir: string): Promise<void> => {
|
|
||||||
// Some tests can leave async session-store writes in-flight; recursive deletion can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
previousHome = process.env.HOME;
|
|
||||||
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-web-home-"));
|
|
||||||
process.env.HOME = tempHome;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
process.env.HOME = previousHome;
|
|
||||||
if (tempHome) {
|
|
||||||
await rmDirWithRetries(tempHome);
|
|
||||||
tempHome = undefined;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const _makeSessionStore = async (
|
|
||||||
entries: Record<string, unknown> = {},
|
|
||||||
): Promise<{ storePath: string; cleanup: () => Promise<void> }> => {
|
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-"));
|
|
||||||
const storePath = path.join(dir, "sessions.json");
|
|
||||||
await fs.writeFile(storePath, JSON.stringify(entries));
|
|
||||||
const cleanup = async () => {
|
|
||||||
// Session store writes can be in-flight when the test finishes (e.g. updateLastRoute
|
|
||||||
// after a message flush). `fs.rm({ recursive })` can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
storePath,
|
|
||||||
cleanup,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("web auto-reply", () => {
|
describe("web auto-reply", () => {
|
||||||
beforeEach(() => {
|
installWebAutoReplyUnitTestHooks();
|
||||||
vi.clearAllMocks();
|
|
||||||
resetBaileysMocks();
|
|
||||||
resetLoadConfigMock();
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
resetLogger();
|
|
||||||
setLoggerOverride(null);
|
|
||||||
vi.useRealTimers();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("prefixes body with same-phone marker when from === to", async () => {
|
it("prefixes body with same-phone marker when from === to", async () => {
|
||||||
// Enable messagePrefix for same-phone mode testing
|
// Enable messagePrefix for same-phone mode testing
|
||||||
|
|||||||
+9
-101
@@ -1,109 +1,17 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import "./test-helpers.js";
|
import "./test-helpers.js";
|
||||||
import fs from "node:fs/promises";
|
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
||||||
|
|
||||||
vi.mock("../agents/pi-embedded.js", () => ({
|
|
||||||
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
|
|
||||||
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
|
|
||||||
runEmbeddedPiAgent: vi.fn(),
|
|
||||||
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
|
|
||||||
resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
|
|
||||||
}));
|
|
||||||
|
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
|
||||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
|
||||||
import { monitorWebChannel } from "./auto-reply.js";
|
import { monitorWebChannel } from "./auto-reply.js";
|
||||||
import { resetBaileysMocks, resetLoadConfigMock, setLoadConfigMock } from "./test-helpers.js";
|
import {
|
||||||
|
installWebAutoReplyTestHomeHooks,
|
||||||
|
installWebAutoReplyUnitTestHooks,
|
||||||
|
resetLoadConfigMock,
|
||||||
|
setLoadConfigMock,
|
||||||
|
} from "./auto-reply.test-harness.js";
|
||||||
|
|
||||||
let previousHome: string | undefined;
|
installWebAutoReplyTestHomeHooks();
|
||||||
let tempHome: string | undefined;
|
|
||||||
|
|
||||||
const rmDirWithRetries = async (dir: string): Promise<void> => {
|
|
||||||
// Some tests can leave async session-store writes in-flight; recursive deletion can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
resetInboundDedupe();
|
|
||||||
previousHome = process.env.HOME;
|
|
||||||
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-web-home-"));
|
|
||||||
process.env.HOME = tempHome;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
process.env.HOME = previousHome;
|
|
||||||
if (tempHome) {
|
|
||||||
await rmDirWithRetries(tempHome);
|
|
||||||
tempHome = undefined;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const _makeSessionStore = async (
|
|
||||||
entries: Record<string, unknown> = {},
|
|
||||||
): Promise<{ storePath: string; cleanup: () => Promise<void> }> => {
|
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-"));
|
|
||||||
const storePath = path.join(dir, "sessions.json");
|
|
||||||
await fs.writeFile(storePath, JSON.stringify(entries));
|
|
||||||
const cleanup = async () => {
|
|
||||||
// Session store writes can be in-flight when the test finishes (e.g. updateLastRoute
|
|
||||||
// after a message flush). `fs.rm({ recursive })` can race and throw ENOTEMPTY.
|
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
||||||
try {
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
return;
|
|
||||||
} catch (err) {
|
|
||||||
const code =
|
|
||||||
err && typeof err === "object" && "code" in err
|
|
||||||
? String((err as { code?: unknown }).code)
|
|
||||||
: null;
|
|
||||||
if (code === "ENOTEMPTY" || code === "EBUSY" || code === "EPERM") {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.rm(dir, { recursive: true, force: true });
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
storePath,
|
|
||||||
cleanup,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("web auto-reply", () => {
|
describe("web auto-reply", () => {
|
||||||
beforeEach(() => {
|
installWebAutoReplyUnitTestHooks();
|
||||||
vi.clearAllMocks();
|
|
||||||
resetBaileysMocks();
|
|
||||||
resetLoadConfigMock();
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
resetLogger();
|
|
||||||
setLoggerOverride(null);
|
|
||||||
vi.useRealTimers();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("skips tool summaries and sends final reply with responsePrefix", async () => {
|
it("skips tool summaries and sends final reply with responsePrefix", async () => {
|
||||||
setLoadConfigMock(() => ({
|
setLoadConfigMock(() => ({
|
||||||
|
|||||||
@@ -44,6 +44,26 @@ vi.mock("../config/config.js", async (importOriginal) => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Some web modules live under `src/web/auto-reply/*` and import config via a different
|
||||||
|
// relative path (`../../config/config.js`). Mock both specifiers so tests stay stable
|
||||||
|
// across refactors that move files between folders.
|
||||||
|
vi.mock("../../config/config.js", async (importOriginal) => {
|
||||||
|
// `../../config/config.js` is correct for modules under `src/web/auto-reply/*`.
|
||||||
|
// For typing in this file (which lives in `src/web/*`), refer to the same module
|
||||||
|
// via the local relative path.
|
||||||
|
const actual = await importOriginal<typeof import("../config/config.js")>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
loadConfig: () => {
|
||||||
|
const getter = (globalThis as Record<symbol, unknown>)[CONFIG_KEY];
|
||||||
|
if (typeof getter === "function") {
|
||||||
|
return getter();
|
||||||
|
}
|
||||||
|
return DEFAULT_CONFIG;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
vi.mock("../media/store.js", () => ({
|
vi.mock("../media/store.js", () => ({
|
||||||
saveMediaBuffer: vi.fn().mockImplementation(async (_buf: Buffer, contentType?: string) => ({
|
saveMediaBuffer: vi.fn().mockImplementation(async (_buf: Buffer, contentType?: string) => ({
|
||||||
id: "mid",
|
id: "mid",
|
||||||
|
|||||||
Reference in New Issue
Block a user