mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 05:37:27 +00:00
refactor(test): reuse web auto-reply harness in more tests
This commit is contained in:
@@ -1,110 +1,17 @@
|
|||||||
import "./test-helpers.js";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
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";
|
|
||||||
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
|
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
|
||||||
|
|
||||||
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,
|
||||||
|
makeSessionStore,
|
||||||
|
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("handles helper envelope timestamps with trimmed timezones (regression)", () => {
|
it("handles helper envelope timestamps with trimmed timezones (regression)", () => {
|
||||||
const d = new Date("2025-01-01T00:00:00.000Z");
|
const d = new Date("2025-01-01T00:00:00.000Z");
|
||||||
|
|||||||
@@ -1,111 +1,21 @@
|
|||||||
import "./test-helpers.js";
|
|
||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
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 { expectInboundContextContract } from "../../test/helpers/inbound-contract.js";
|
import { expectInboundContextContract } from "../../test/helpers/inbound-contract.js";
|
||||||
import { resetInboundDedupe } from "../auto-reply/reply/inbound-dedupe.js";
|
import { setLoggerOverride } from "../logging.js";
|
||||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
|
||||||
import { monitorWebChannel, SILENT_REPLY_TOKEN } from "./auto-reply.js";
|
import { monitorWebChannel, SILENT_REPLY_TOKEN } from "./auto-reply.js";
|
||||||
import { resetBaileysMocks, resetLoadConfigMock, setLoadConfigMock } from "./test-helpers.js";
|
import {
|
||||||
|
installWebAutoReplyTestHomeHooks,
|
||||||
|
installWebAutoReplyUnitTestHooks,
|
||||||
|
makeSessionStore,
|
||||||
|
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("supports always-on group activation with silent token and clears pending history", async () => {
|
it("supports always-on group activation with silent token and clears pending history", async () => {
|
||||||
const sendMedia = vi.fn();
|
const sendMedia = vi.fn();
|
||||||
|
|||||||
Reference in New Issue
Block a user