Skip to content

Commit ff83e3e

Browse files
authored
test(extensions): close cached agent databases before removing fixture state dirs (#126352)
Doctor migrations and auth-profile writes open per-agent and shared state databases under the fixture's temporary directory. Clearing the plugin state store or the runtime auth snapshots does not release those handles, so Windows fails the directory removal with EBUSY while Linux unlinks the open files and stays green. Close the cached databases before each removal, matching the ordering the zalouser and zalo fixtures already use.
1 parent f94f945 commit ff83e3e

8 files changed

Lines changed: 49 additions & 27 deletions

File tree

‎extensions/acpx/doctor-contract-api.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ describe("acpx doctor state migration", () => {
105105
});
106106

107107
afterEach(async () => {
108+
resetPluginStateStoreForTests();
108109
await fs.rm(stateDir, { recursive: true, force: true });
109110
});
110111

‎extensions/active-memory/doctor-contract-api.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ describe("active-memory doctor state migration", () => {
6565

6666
afterEach(async () => {
6767
vi.useRealTimers();
68+
resetPluginStateStoreForTests();
6869
await fs.rm(stateDir, { recursive: true, force: true });
6970
});
7071

‎extensions/alibaba/video-generation-provider.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
mockSuccessfulDashscopeVideoTask,
2323
} from "openclaw/plugin-sdk/provider-test-contracts";
2424
// Alibaba tests cover video generation provider plugin behavior.
25+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
2526
import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures";
2627
import {
2728
DASHSCOPE_WAN_VIDEO_MODELS,
@@ -276,6 +277,10 @@ describe("alibaba video generation provider", () => {
276277
expect(alibabaVideoGenerationProvider.isConfigured?.({ cfg: {}, agentDir })).toBe(expected);
277278
} finally {
278279
clearRuntimeAuthProfileStoreSnapshots();
280+
// Saving the profile store opens the per-agent database under the temporary agent
281+
// dir, and clearing the snapshots does not release it, so Windows fails the removal
282+
// with EBUSY unless the cached handles are closed first.
283+
closeOpenClawAgentDatabasesForTest();
279284
await fs.rm(agentDir, { force: true, recursive: true });
280285
}
281286
});

‎extensions/codex/doctor-contract-api.test.ts‎

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
PluginDoctorStateMigrationContext,
1212
} from "openclaw/plugin-sdk/runtime-doctor-migrations";
1313
import { getSessionEntry, upsertSessionEntry } from "openclaw/plugin-sdk/session-store-runtime";
14+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
1415
import { afterEach, describe, expect, it } from "vitest";
1516
import {
1617
legacyConfigRules,
@@ -59,6 +60,16 @@ function openBindingStore(env: NodeJS.ProcessEnv) {
5960
});
6061
}
6162

63+
async function removeCodexDoctorFixture(stateDir: string): Promise<void> {
64+
// Doctor migrations open per-agent databases and leave the shared state database open under
65+
// the temporary state dir; both must be released before removal or Windows keeps the files
66+
// locked and the removal fails with EBUSY. Agent close first: it releases leases through
67+
// shared state, so the reverse order can reopen it.
68+
closeOpenClawAgentDatabasesForTest();
69+
resetPluginStateStoreForTests();
70+
await fs.rm(stateDir, { recursive: true, force: true });
71+
}
72+
6273
async function createBindingMigrationFixture(options: {
6374
binding?: Record<string, unknown>;
6475
legacySharedRoot?: boolean;
@@ -272,7 +283,7 @@ describe("codex doctor contract", () => {
272283
}),
273284
).toMatchObject({ agentHarnessId: "codex" });
274285
} finally {
275-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
286+
await removeCodexDoctorFixture(fixture.stateDir);
276287
}
277288
});
278289

@@ -357,7 +368,7 @@ describe("codex doctor contract", () => {
357368
fs.readFile(fixture.storePath, "utf8").then(JSON.parse),
358369
).resolves.not.toHaveProperty("agent:main:session-1.agentHarnessId");
359370

360-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
371+
await removeCodexDoctorFixture(fixture.stateDir);
361372
});
362373

363374
it.each([
@@ -396,7 +407,7 @@ describe("codex doctor contract", () => {
396407
await expect(fs.access(`${fixture.sidecarPath}.migrated`)).rejects.toThrow();
397408
await expect(openBindingStore(fixture.env).entries()).resolves.toEqual([]);
398409

399-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
410+
await removeCodexDoctorFixture(fixture.stateDir);
400411
});
401412

402413
it("migrates a shared-root binding to the configured system agent", async () => {
@@ -448,7 +459,7 @@ describe("codex doctor contract", () => {
448459
).toMatchObject({ agentHarnessId: "codex" });
449460
await expect(fs.access(`${fixture.sidecarPath}.migrated`)).resolves.toBeUndefined();
450461

451-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
462+
await removeCodexDoctorFixture(fixture.stateDir);
452463
});
453464

454465
it("keeps an agent-scoped shared-root binding with its explicit owner", async () => {
@@ -492,7 +503,7 @@ describe("codex doctor contract", () => {
492503
).resolves.toMatchObject({ sessionId: "explicit-ops-owner" });
493504
await expect(fs.access(`${fixture.sidecarPath}.migrated`)).resolves.toBeUndefined();
494505

495-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
506+
await removeCodexDoctorFixture(fixture.stateDir);
496507
});
497508

498509
it("bounds oversized legacy fingerprints before plugin-state import", async () => {
@@ -560,7 +571,7 @@ describe("codex doctor contract", () => {
560571
warnings: [],
561572
});
562573

563-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
574+
await removeCodexDoctorFixture(fixture.stateDir);
564575
});
565576

566577
it("normalizes a partial raw conversation import before copying the session row", async () => {
@@ -638,7 +649,7 @@ describe("codex doctor contract", () => {
638649
warnings: [],
639650
});
640651

641-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
652+
await removeCodexDoctorFixture(fixture.stateDir);
642653
});
643654

644655
it("normalizes retained raw conversation and session rows before comparison", async () => {
@@ -710,7 +721,7 @@ describe("codex doctor contract", () => {
710721
warnings: [],
711722
});
712723

713-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
724+
await removeCodexDoctorFixture(fixture.stateDir);
714725
});
715726

716727
it("rejects an explicit session file locator outside the session directory", async () => {
@@ -743,7 +754,7 @@ describe("codex doctor contract", () => {
743754
).toBeUndefined();
744755
await expect(openBindingStore(fixture.env).entries()).resolves.toEqual([]);
745756

746-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
757+
await removeCodexDoctorFixture(fixture.stateDir);
747758
});
748759

749760
it("deduplicates session-store aliases before classifying binding ownership", async () => {
@@ -795,7 +806,7 @@ describe("codex doctor contract", () => {
795806
expect(configuredIndex["agent:main:aliased-store"]).not.toHaveProperty("agentHarnessId");
796807
expect(targetIndex["agent:main:aliased-store"]).not.toHaveProperty("agentHarnessId");
797808

798-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
809+
await removeCodexDoctorFixture(fixture.stateDir);
799810
});
800811

801812
it("resolves relative session files from a symlinked store path", async () => {
@@ -847,7 +858,7 @@ describe("codex doctor contract", () => {
847858
`${sessionKey}.agentHarnessId`,
848859
);
849860

850-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
861+
await removeCodexDoctorFixture(fixture.stateDir);
851862
});
852863

853864
it.each([
@@ -929,7 +940,7 @@ describe("codex doctor contract", () => {
929940
retired: true,
930941
});
931942

932-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
943+
await removeCodexDoctorFixture(fixture.stateDir);
933944
},
934945
);
935946

@@ -986,7 +997,7 @@ describe("codex doctor contract", () => {
986997
retired: true,
987998
});
988999

989-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
1000+
await removeCodexDoctorFixture(fixture.stateDir);
9901001
});
9911002

9921003
it("does not resurrect a retired session generation from its legacy sidecar", async () => {
@@ -1041,7 +1052,7 @@ describe("codex doctor contract", () => {
10411052
fs.readFile(path.join(fixture.sessionsDir, "sessions.json"), "utf8").then(JSON.parse),
10421053
).resolves.not.toHaveProperty(`${sessionKey}.agentHarnessId`);
10431054

1044-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
1055+
await removeCodexDoctorFixture(fixture.stateDir);
10451056
});
10461057

10471058
it.each(["active", "cleared"] as const)(
@@ -1081,7 +1092,7 @@ describe("codex doctor contract", () => {
10811092
warnings: [],
10821093
});
10831094

1084-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
1095+
await removeCodexDoctorFixture(fixture.stateDir);
10851096
},
10861097
);
10871098

@@ -1106,7 +1117,7 @@ describe("codex doctor contract", () => {
11061117
await expect(fs.access(fixture.sidecarPath)).rejects.toThrow();
11071118
await expect(fs.access(`${fixture.sidecarPath}.migrated`)).resolves.toBeUndefined();
11081119

1109-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
1120+
await removeCodexDoctorFixture(fixture.stateDir);
11101121
});
11111122

11121123
it("retains a zero-owner sidecar when canonical plugin state is malformed", async () => {
@@ -1135,7 +1146,7 @@ describe("codex doctor contract", () => {
11351146
await expect(fs.access(fixture.sidecarPath)).resolves.toBeUndefined();
11361147
await expect(store.lookup(bindingKey)).resolves.toEqual(malformed);
11371148

1138-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
1149+
await removeCodexDoctorFixture(fixture.stateDir);
11391150
});
11401151

11411152
it("retains mixed Codex and foreign ambiguous binding owners", async () => {
@@ -1164,7 +1175,7 @@ describe("codex doctor contract", () => {
11641175
await expect(fs.access(fixture.sidecarPath)).resolves.toBeUndefined();
11651176
await expect(openBindingStore(fixture.env).entries()).resolves.toEqual([]);
11661177

1167-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
1178+
await removeCodexDoctorFixture(fixture.stateDir);
11681179
});
11691180

11701181
it("retains a sidecar owned by a foreign harness without importing plugin state", async () => {
@@ -1188,7 +1199,7 @@ describe("codex doctor contract", () => {
11881199
await expect(fs.access(fixture.sidecarPath)).resolves.toBeUndefined();
11891200
await expect(openBindingStore(fixture.env).entries()).resolves.toEqual([]);
11901201

1191-
await fs.rm(fixture.stateDir, { recursive: true, force: true });
1202+
await removeCodexDoctorFixture(fixture.stateDir);
11921203
});
11931204

11941205
it.each([
@@ -1230,10 +1241,8 @@ describe("codex doctor contract", () => {
12301241
await expect(fs.access(`${fixture.sidecarPath}.migrated`)).rejects.toThrow();
12311242
await expect(openBindingStore(fixture.env).entries()).resolves.toEqual([]);
12321243

1233-
await Promise.all([
1234-
fs.rm(fixture.stateDir, { recursive: true, force: true }),
1235-
fs.rm(externalDir, { recursive: true, force: true }),
1236-
]);
1244+
await removeCodexDoctorFixture(fixture.stateDir);
1245+
await fs.rm(externalDir, { recursive: true, force: true });
12371246
});
12381247

12391248
it("does not scan above stateDir or follow escaped external store locators", async () => {
@@ -1284,10 +1293,8 @@ describe("codex doctor contract", () => {
12841293

12851294
await expect(migration.detectLegacyState(params)).resolves.toBeNull();
12861295

1287-
await Promise.all([
1288-
fs.rm(outerDir, { recursive: true, force: true }),
1289-
fs.rm(outsideDir, { recursive: true, force: true }),
1290-
]);
1296+
await removeCodexDoctorFixture(outerDir);
1297+
await fs.rm(outsideDir, { recursive: true, force: true });
12911298
});
12921299

12931300
it("renames old approval-routed destructive plugin policy values", () => {

‎extensions/device-pair/doctor-contract-api.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe("device-pair doctor notify migration", () => {
4343
});
4444

4545
afterEach(async () => {
46+
resetPluginStateStoreForTests();
4647
await fs.rm(stateDir, { recursive: true, force: true });
4748
});
4849

‎extensions/memory-core/doctor-contract-api.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ describe("memory-core doctor dreaming migration", () => {
488488

489489
afterEach(async () => {
490490
resetMemoryCoreDreamingStateForTests();
491+
resetPluginStateStoreForTests();
491492
await fs.rm(rootDir, { recursive: true, force: true });
492493
});
493494

‎extensions/msteams/doctor-contract-api.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ describe("msteams doctor state migration", () => {
8383
});
8484

8585
afterEach(async () => {
86+
resetPluginStateStoreForTests();
8687
await fs.rm(stateDir, { recursive: true, force: true });
8788
});
8889

‎extensions/openai/video-generation-provider.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
installProviderHttpMockCleanup,
1414
} from "openclaw/plugin-sdk/provider-http-test-mocks";
1515
import { expectExplicitVideoGenerationCapabilities } from "openclaw/plugin-sdk/provider-test-contracts";
16+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
1617
import { beforeAll, describe, expect, it, vi } from "vitest";
1718

1819
const {
@@ -171,6 +172,10 @@ describe("openai video generation provider", () => {
171172
} else {
172173
process.env.OPENAI_API_KEY = previousOpenAIKey;
173174
}
175+
// Saving the profile store opens the per-agent database under the temporary agent
176+
// dir, and clearing the snapshots does not release it, so Windows fails the removal
177+
// with EBUSY unless the cached handles are closed first.
178+
closeOpenClawAgentDatabasesForTest();
174179
fs.rmSync(agentDir, { recursive: true, force: true });
175180
}
176181
});

0 commit comments

Comments
 (0)