test: stabilize subagent persistence registry coverage

This commit is contained in:
Peter Steinberger 2026-04-05 23:50:14 +01:00
parent 579c50dd60
commit ec2f0edd45
No known key found for this signature in database
1 changed files with 17 additions and 30 deletions

View File

@ -7,7 +7,23 @@ import {
clearSessionStoreCacheForTest,
drainSessionStoreLockQueuesForTest,
} from "../config/sessions/store.js";
import { callGateway } from "../gateway/call.js";
import { onAgentEvent } from "../infra/agent-events.js";
import { captureEnv, withEnv } from "../test-utils/env.js";
import { persistSubagentSessionTiming } from "./subagent-registry-helpers.js";
import {
addSubagentRunForTests,
clearSubagentRunSteerRestart,
getLatestSubagentRunByChildSessionKey,
getSubagentRunByChildSessionKey,
initSubagentRegistry,
listSubagentRunsForRequester,
resetSubagentRegistryForTests,
} from "./subagent-registry.js";
import {
loadSubagentRegistryFromDisk,
resolveSubagentRegistryPath,
} from "./subagent-registry.store.js";
const { announceSpy } = vi.hoisted(() => ({
announceSpy: vi.fn(async () => true),
@ -20,29 +36,6 @@ vi.mock("./subagent-orphan-recovery.js", () => ({
scheduleOrphanRecovery: vi.fn(),
}));
let addSubagentRunForTests: typeof import("./subagent-registry.js").addSubagentRunForTests;
let clearSubagentRunSteerRestart: typeof import("./subagent-registry.js").clearSubagentRunSteerRestart;
let getSubagentRunByChildSessionKey: typeof import("./subagent-registry.js").getSubagentRunByChildSessionKey;
let getLatestSubagentRunByChildSessionKey: typeof import("./subagent-registry.js").getLatestSubagentRunByChildSessionKey;
let initSubagentRegistry: typeof import("./subagent-registry.js").initSubagentRegistry;
let listSubagentRunsForRequester: typeof import("./subagent-registry.js").listSubagentRunsForRequester;
let resetSubagentRegistryForTests: typeof import("./subagent-registry.js").resetSubagentRegistryForTests;
let loadSubagentRegistryFromDisk: typeof import("./subagent-registry.store.js").loadSubagentRegistryFromDisk;
async function loadSubagentRegistryModules(): Promise<void> {
vi.resetModules();
({
addSubagentRunForTests,
clearSubagentRunSteerRestart,
getLatestSubagentRunByChildSessionKey,
getSubagentRunByChildSessionKey,
initSubagentRegistry,
listSubagentRunsForRequester,
resetSubagentRegistryForTests,
} = await import("./subagent-registry.js"));
({ loadSubagentRegistryFromDisk } = await import("./subagent-registry.store.js"));
}
describe("subagent registry persistence", () => {
const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
let tempStateDir: string | null = null;
@ -184,10 +177,7 @@ describe("subagent registry persistence", () => {
await flushQueuedRegistryWork();
};
beforeEach(async () => {
await loadSubagentRegistryModules();
const { callGateway } = await import("../gateway/call.js");
const { onAgentEvent } = await import("../infra/agent-events.js");
beforeEach(() => {
vi.mocked(callGateway).mockReset();
vi.mocked(callGateway).mockResolvedValue({
status: "ok",
@ -214,7 +204,6 @@ describe("subagent registry persistence", () => {
tempStateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-subagent-"));
process.env.OPENCLAW_STATE_DIR = tempStateDir;
const { persistSubagentSessionTiming } = await import("./subagent-registry-helpers.js");
const now = Date.now();
const startedAt = now;
const endedAt = now + 500;
@ -601,8 +590,6 @@ describe("subagent registry persistence", () => {
it("uses isolated temp state when OPENCLAW_STATE_DIR is unset in tests", async () => {
delete process.env.OPENCLAW_STATE_DIR;
vi.resetModules();
const { resolveSubagentRegistryPath } = await import("./subagent-registry.store.js");
const registryPath = resolveSubagentRegistryPath();
expect(registryPath).toContain(path.join(os.tmpdir(), "openclaw-test-state"));
});