mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 07:21:23 +00:00
perf(test): replace module resets with direct spies and runtime seams
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
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 { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { installHooksFromPath } from "./install.js";
|
||||
import {
|
||||
clearInternalHooks,
|
||||
createInternalHookEvent,
|
||||
triggerInternalHook,
|
||||
} from "./internal-hooks.js";
|
||||
import { loadInternalHooks } from "./loader.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
@@ -12,36 +19,15 @@ async function makeTempDir() {
|
||||
}
|
||||
|
||||
describe("hooks install (e2e)", () => {
|
||||
let prevStateDir: string | undefined;
|
||||
let prevBundledDir: string | undefined;
|
||||
let workspaceDir: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
const baseDir = await makeTempDir();
|
||||
workspaceDir = path.join(baseDir, "workspace");
|
||||
await fs.mkdir(workspaceDir, { recursive: true });
|
||||
|
||||
prevStateDir = process.env.OPENCLAW_STATE_DIR;
|
||||
prevBundledDir = process.env.OPENCLAW_BUNDLED_HOOKS_DIR;
|
||||
process.env.OPENCLAW_STATE_DIR = path.join(baseDir, "state");
|
||||
process.env.OPENCLAW_BUNDLED_HOOKS_DIR = path.join(baseDir, "bundled-none");
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
if (prevStateDir === undefined) {
|
||||
delete process.env.OPENCLAW_STATE_DIR;
|
||||
} else {
|
||||
process.env.OPENCLAW_STATE_DIR = prevStateDir;
|
||||
}
|
||||
|
||||
if (prevBundledDir === undefined) {
|
||||
delete process.env.OPENCLAW_BUNDLED_HOOKS_DIR;
|
||||
} else {
|
||||
process.env.OPENCLAW_BUNDLED_HOOKS_DIR = prevBundledDir;
|
||||
}
|
||||
|
||||
vi.resetModules();
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
try {
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
@@ -92,23 +78,29 @@ describe("hooks install (e2e)", () => {
|
||||
"utf-8",
|
||||
);
|
||||
|
||||
const { installHooksFromPath } = await import("./install.js");
|
||||
const installResult = await installHooksFromPath({ path: packDir });
|
||||
const hooksDir = path.join(baseDir, "managed-hooks");
|
||||
const installResult = await installHooksFromPath({ path: packDir, hooksDir });
|
||||
expect(installResult.ok).toBe(true);
|
||||
if (!installResult.ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { clearInternalHooks, createInternalHookEvent, triggerInternalHook } =
|
||||
await import("./internal-hooks.js");
|
||||
const { loadInternalHooks } = await import("./loader.js");
|
||||
|
||||
clearInternalHooks();
|
||||
const bundledHooksDir = path.join(baseDir, "bundled-none");
|
||||
await fs.mkdir(bundledHooksDir, { recursive: true });
|
||||
const loaded = await loadInternalHooks(
|
||||
{ hooks: { internal: { enabled: true } } },
|
||||
{
|
||||
hooks: {
|
||||
internal: {
|
||||
enabled: true,
|
||||
load: { extraDirs: [hooksDir] },
|
||||
},
|
||||
},
|
||||
},
|
||||
workspaceDir,
|
||||
{ managedHooksDir: hooksDir, bundledHooksDir },
|
||||
);
|
||||
expect(loaded).toBe(1);
|
||||
expect(loaded).toBeGreaterThanOrEqual(1);
|
||||
|
||||
const event = createInternalHookEvent("command", "new", "test-session");
|
||||
await triggerInternalHook(event);
|
||||
|
||||
Reference in New Issue
Block a user