Skip to content

Commit 48d65f9

Browse files
authored
fix(test): release SQLite fixtures before Windows cleanup (#113316)
* fix(test): close SQLite fixtures before Windows cleanup * test(windows): extend fixture cleanup retries * fix(test): avoid registry reads during fixture cleanup
1 parent 4302896 commit 48d65f9

8 files changed

Lines changed: 178 additions & 23 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@
18391839
"test:unit:fast:audit": "node scripts/test-unit-fast-audit.mjs",
18401840
"test:voicecall:closedloop": "node scripts/test-voicecall-closedloop.mjs",
18411841
"test:watch": "node scripts/test-projects.mjs --watch",
1842-
"test:windows:ci": "node scripts/test-projects.mjs src/shared/runtime-import.test.ts src/infra/sqlite-snapshot.test.ts src/infra/state-migrations.legacy-session-store.test.ts src/agents/sessions/windows-git-bash-path.test.ts src/process/exec.windows.test.ts src/process/exec.windows.integration.test.ts src/process/windows-command.test.ts src/infra/windows-install-roots.test.ts src/node-host/invoke-system-run-allowlist.test.ts src/daemon/schtasks.startup-fallback.test.ts extensions/lobster/src/lobster-runner.test.ts test/scripts/format-generated-module.test.ts test/scripts/npm-runner.test.ts test/scripts/openclaw-cross-os-installer.windows.test.ts test/scripts/openclaw-cross-os-release-workflow.test.ts test/scripts/pnpm-runner.test.ts test/scripts/run-with-env.test.ts test/scripts/ts-topology.test.ts test/scripts/ui.test.ts test/scripts/vitest-process-group.test.ts",
1842+
"test:windows:ci": "node scripts/test-projects.mjs src/shared/runtime-import.test.ts src/infra/sqlite-snapshot.test.ts src/infra/state-migrations.legacy-session-store.test.ts src/test-utils/openclaw-test-state.test.ts src/agents/sessions/windows-git-bash-path.test.ts src/process/exec.windows.test.ts src/process/exec.windows.integration.test.ts src/process/windows-command.test.ts src/infra/windows-install-roots.test.ts src/node-host/invoke-system-run-allowlist.test.ts src/daemon/schtasks.startup-fallback.test.ts extensions/lobster/src/lobster-runner.test.ts test/scripts/format-generated-module.test.ts test/scripts/npm-runner.test.ts test/scripts/openclaw-cross-os-installer.windows.test.ts test/scripts/openclaw-cross-os-release-workflow.test.ts test/scripts/pnpm-runner.test.ts test/scripts/run-with-env.test.ts test/scripts/ts-topology.test.ts test/scripts/ui.test.ts test/scripts/vitest-process-group.test.ts",
18431843
"tool-display:check": "node --import tsx scripts/tool-display.ts --check",
18441844
"tool-display:write": "node --import tsx scripts/tool-display.ts --write",
18451845
"ts-topology": "node --import tsx scripts/ts-topology.ts",

‎scripts/ci-changed-scope.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ const ANDROID_NATIVE_RE = /^(apps\/android\/|apps\/shared\/)/;
4949
const NODE_SCOPE_RE =
5050
/^(src\/|test\/|extensions\/|packages\/|scripts\/|ui\/|\.github\/|openclaw\.mjs$|package\.json$|pnpm-lock\.yaml$|pnpm-workspace\.yaml$|tsconfig.*\.json$|vitest.*\.ts$|tsdown\.config\.ts$|\.oxlintrc\.json$|\.oxfmtrc\.jsonc$)/;
5151
const WINDOWS_SCOPE_RE =
52-
/^(src\/config\/sessions\/(?:session-accessor\.sqlite-archive|store\.session-lifecycle-mutation\.test)\.ts$|src\/process\/|src\/infra\/windows-install-roots\.ts$|src\/shared\/(?:import-specifier|runtime-import)(?:\.test)?\.ts$|scripts\/(?:install\.ps1|openclaw-cross-os-release-checks\.ts|github\/run-openclaw-cross-os-release-checks\.sh|(?:npm-runner|pnpm-runner|ui|vitest-process-group)\.(?:mjs|js)|lib\/(?:format-generated-module\.mjs|cross-os-release-checks\/[^/]+\.ts))$|test\/scripts\/(?:format-generated-module|install-ps1|npm-runner|openclaw-cross-os-release-workflow|pnpm-runner|ui|vitest-process-group)\.test\.ts$|package\.json$|pnpm-lock\.yaml$|pnpm-workspace\.yaml$|\.github\/workflows\/(?:ci|openclaw-cross-os-release-checks-reusable)\.yml$|\.github\/actions\/setup-node-env\/action\.yml$|\.github\/actions\/setup-pnpm-store-cache\/action\.yml$)/;
52+
/^(src\/config\/sessions\/(?:session-accessor\.sqlite-archive|store\.session-lifecycle-mutation\.test)\.ts$|src\/process\/|src\/infra\/windows-install-roots\.ts$|src\/shared\/(?:import-specifier|runtime-import)(?:\.test)?\.ts$|src\/test-utils\/openclaw-test-state(?:\.test)?\.ts$|scripts\/(?:install\.ps1|openclaw-cross-os-release-checks\.ts|github\/run-openclaw-cross-os-release-checks\.sh|(?:npm-runner|pnpm-runner|ui|vitest-process-group)\.(?:mjs|js)|lib\/(?:format-generated-module\.mjs|cross-os-release-checks\/[^/]+\.ts))$|test\/scripts\/(?:format-generated-module|install-ps1|npm-runner|openclaw-cross-os-release-workflow|pnpm-runner|ui|vitest-process-group)\.test\.ts$|package\.json$|pnpm-lock\.yaml$|pnpm-workspace\.yaml$|\.github\/workflows\/(?:ci|openclaw-cross-os-release-checks-reusable)\.yml$|\.github\/actions\/setup-node-env\/action\.yml$|\.github\/actions\/setup-pnpm-store-cache\/action\.yml$)/;
5353
const WINDOWS_TEST_SCOPE_RE =
54-
/^(src\/config\/sessions\/store\.session-lifecycle-mutation\.test\.ts$|src\/process\/(?:exec\.windows|windows-command)\.test\.ts$|src\/infra\/windows-install-roots\.test\.ts$|src\/shared\/runtime-import\.test\.ts$|test\/scripts\/(?:format-generated-module|npm-runner|openclaw-cross-os-release-workflow|pnpm-runner|ui|vitest-process-group)\.test\.ts$)/;
54+
/^(src\/config\/sessions\/store\.session-lifecycle-mutation\.test\.ts$|src\/process\/(?:exec\.windows|windows-command)\.test\.ts$|src\/infra\/windows-install-roots\.test\.ts$|src\/shared\/runtime-import\.test\.ts$|src\/test-utils\/openclaw-test-state\.test\.ts$|test\/scripts\/(?:format-generated-module|npm-runner|openclaw-cross-os-release-workflow|pnpm-runner|ui|vitest-process-group)\.test\.ts$)/;
5555
const WINDOWS_DAEMON_SCOPE_RE =
5656
/^src\/daemon\/(?:schtasks(?:[-.][^/]+)?|runtime-hints\.windows-paths(?:\.test)?|test-helpers\/schtasks-(?:base-mocks|fixtures))\.ts$/;
5757
const CONTROL_UI_I18N_SCOPE_RE =

‎src/infra/backup-create.test.ts‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { RuntimeEnv } from "../runtime.js";
1313
import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js";
1414
import {
1515
closeOpenClawStateDatabase,
16+
closeOpenClawStateDatabaseByPath,
1617
openOpenClawStateDatabase,
1718
} from "../state/openclaw-state-db.js";
1819
import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
@@ -904,24 +905,27 @@ describe("createBackupArchive", () => {
904905

905906
const restoredDatabasePath = path.join(extractDir, databaseEntry);
906907
const restoredStateDir = path.dirname(path.dirname(restoredDatabasePath));
907-
const restoredDetection = detectLegacyAuditLogs({
908-
stateDir: restoredStateDir,
909-
doctorOnlyStateMigrations: true,
910-
});
911-
expect(restoredDetection.hasLegacy).toBe(true);
912-
await migrateLegacyAuditLogs({
913-
detected: restoredDetection,
914-
stateDir: restoredStateDir,
915-
});
916-
const restoredEntries = createSqliteAuditRecordStore({
917-
scope: CONFIG_AUDIT_SCOPE,
918-
maxEntries: CONFIG_AUDIT_MAX_ENTRIES,
919-
env: { ...process.env, OPENCLAW_STATE_DIR: restoredStateDir },
920-
}).entries();
921-
expect(restoredEntries).toHaveLength(2);
922-
expect(new Set(restoredEntries.map((entry) => entry.key)).size).toBe(2);
923-
expect(restoredEntries.map((entry) => entry.value)).toEqual([record, record]);
924-
closeOpenClawStateDatabase();
908+
try {
909+
const restoredDetection = detectLegacyAuditLogs({
910+
stateDir: restoredStateDir,
911+
doctorOnlyStateMigrations: true,
912+
});
913+
expect(restoredDetection.hasLegacy).toBe(true);
914+
await migrateLegacyAuditLogs({
915+
detected: restoredDetection,
916+
stateDir: restoredStateDir,
917+
});
918+
const restoredEntries = createSqliteAuditRecordStore({
919+
scope: CONFIG_AUDIT_SCOPE,
920+
maxEntries: CONFIG_AUDIT_MAX_ENTRIES,
921+
env: { ...process.env, OPENCLAW_STATE_DIR: restoredStateDir },
922+
}).entries();
923+
expect(restoredEntries).toHaveLength(2);
924+
expect(new Set(restoredEntries.map((entry) => entry.key)).size).toBe(2);
925+
expect(restoredEntries.map((entry) => entry.value)).toEqual([record, record]);
926+
} finally {
927+
closeOpenClawStateDatabaseByPath(restoredDatabasePath);
928+
}
925929
},
926930
);
927931
});

‎src/scripts/ci-changed-scope.windows.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,16 @@ describe("detectChangedScope Windows routing", () => {
1212
});
1313
}
1414
});
15+
16+
it("routes shared test-state fixture changes to Windows", () => {
17+
for (const fixturePath of [
18+
"src/test-utils/openclaw-test-state.ts",
19+
"src/test-utils/openclaw-test-state.test.ts",
20+
]) {
21+
expect(detectChangedScope([fixturePath]), fixturePath).toMatchObject({
22+
runNode: true,
23+
runWindows: true,
24+
});
25+
}
26+
});
1527
});

‎src/state/openclaw-agent-db.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,17 @@ export function listOpenIncognitoAgentDatabases(): Array<{ agentId: string; stor
547547
);
548548
}
549549

550+
/** List process-held agent databases without opening or inspecting fixture state. */
551+
export function listOpenClawAgentDatabasesForTest(): Array<{ agentId: string; path: string }> {
552+
return [...cachedDatabases.values()]
553+
.filter((database) => database.db.isOpen)
554+
.map((database) => ({ agentId: database.agentId, path: database.path }))
555+
.toSorted(
556+
(left, right) =>
557+
left.agentId.localeCompare(right.agentId) || left.path.localeCompare(right.path),
558+
);
559+
}
560+
550561
/** Close one cached agent database identified by its exact resolved pathname. */
551562
export function closeOpenClawAgentDatabaseByPath(pathname: string): boolean {
552563
// Cache keys are lexical resolved paths. Do not realpath aliases here: a

‎src/test-utils/openclaw-test-state.test.ts‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
// Tests isolated OpenClaw test-state setup and cleanup behavior.
22
import fs from "node:fs/promises";
3+
import os from "node:os";
34
import path from "node:path";
4-
import { describe, expect, it } from "vitest";
5+
import { describe, expect, it, vi } from "vitest";
56
import { loadPersistedAuthProfileStore } from "../agents/auth-profiles/persisted.js";
7+
import {
8+
closeOpenClawAgentDatabaseByPath,
9+
openOpenClawAgentDatabase,
10+
} from "../state/openclaw-agent-db.js";
11+
import {
12+
closeOpenClawStateDatabaseByPath,
13+
openOpenClawStateDatabase,
14+
} from "../state/openclaw-state-db.js";
615
import { withEnvAsync } from "./env.js";
716
import { createOpenClawTestState, withOpenClawTestState } from "./openclaw-test-state.js";
817

@@ -145,6 +154,99 @@ describe("openclaw test state", () => {
145154
);
146155
});
147156

157+
it("closes only fixture-owned databases before restoring env", async () => {
158+
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
159+
const unrelatedRoot = await fs.mkdtemp(
160+
path.join(os.tmpdir(), "openclaw-test-state-unrelated-"),
161+
);
162+
const unrelatedEnv = {
163+
...process.env,
164+
OPENCLAW_STATE_DIR: path.join(unrelatedRoot, "state"),
165+
};
166+
const state = await createOpenClawTestState({
167+
layout: "state-only",
168+
label: "database-cleanup",
169+
});
170+
const fixtureShared = openOpenClawStateDatabase({ env: state.env });
171+
const fixtureAgent = openOpenClawAgentDatabase({
172+
agentId: "worker",
173+
env: state.env,
174+
});
175+
const unrelatedShared = openOpenClawStateDatabase({ env: unrelatedEnv });
176+
const unrelatedAgent = openOpenClawAgentDatabase({
177+
agentId: "outside",
178+
env: unrelatedEnv,
179+
});
180+
const restoreEnv = state.restoreEnv;
181+
const rmSpy = vi.spyOn(fs, "rm");
182+
state.restoreEnv = () => {
183+
expect(process.env.OPENCLAW_STATE_DIR).toBe(state.stateDir);
184+
expect(fixtureShared.db.isOpen).toBe(false);
185+
expect(fixtureAgent.db.isOpen).toBe(false);
186+
expect(unrelatedShared.db.isOpen).toBe(true);
187+
expect(unrelatedAgent.db.isOpen).toBe(true);
188+
restoreEnv();
189+
};
190+
191+
try {
192+
await state.cleanup();
193+
194+
expect(process.env.OPENCLAW_STATE_DIR).toBe(previousStateDir);
195+
expect(rmSpy).toHaveBeenCalledWith(state.root, {
196+
recursive: true,
197+
force: true,
198+
maxRetries: 20,
199+
retryDelay: 25,
200+
});
201+
await expectPathMissing(state.root);
202+
expect(unrelatedShared.db.isOpen).toBe(true);
203+
expect(unrelatedAgent.db.isOpen).toBe(true);
204+
} finally {
205+
state.restoreEnv = restoreEnv;
206+
restoreEnv();
207+
closeOpenClawAgentDatabaseByPath(fixtureAgent.path);
208+
closeOpenClawAgentDatabaseByPath(unrelatedAgent.path);
209+
closeOpenClawStateDatabaseByPath(fixtureShared.path);
210+
closeOpenClawStateDatabaseByPath(unrelatedShared.path);
211+
rmSpy.mockRestore();
212+
await fs.rm(state.root, {
213+
recursive: true,
214+
force: true,
215+
maxRetries: 20,
216+
retryDelay: 25,
217+
});
218+
await fs.rm(unrelatedRoot, {
219+
recursive: true,
220+
force: true,
221+
maxRetries: 20,
222+
retryDelay: 25,
223+
});
224+
}
225+
});
226+
227+
it("preserves callback failures after closing fixture databases", async () => {
228+
const callbackError = new Error("fixture callback failed");
229+
let root = "";
230+
let shared: ReturnType<typeof openOpenClawStateDatabase> | undefined;
231+
let agent: ReturnType<typeof openOpenClawAgentDatabase> | undefined;
232+
233+
await expect(
234+
withOpenClawTestState({ layout: "state-only", label: "callback-failure" }, async (state) => {
235+
root = state.root;
236+
shared = openOpenClawStateDatabase({ env: state.env });
237+
agent = openOpenClawAgentDatabase({
238+
agentId: "main",
239+
env: state.env,
240+
});
241+
throw callbackError;
242+
}),
243+
).rejects.toBe(callbackError);
244+
245+
expect(shared?.db.isOpen).toBe(false);
246+
expect(agent?.db.isOpen).toBe(false);
247+
await expectPathMissing(root);
248+
});
249+
148250
it("creates upgrade survivor fixture state", async () => {
149251
await withOpenClawTestState(
150252
{

‎src/test-utils/openclaw-test-state.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import { resolveAuthProfileDatabasePath } from "../agents/auth-profiles/sqlite.j
77
import { saveAuthProfileStore } from "../agents/auth-profiles/store.js";
88
import type { AuthProfileStore } from "../agents/auth-profiles/types.js";
99
import * as configRuntime from "../config/config.js";
10+
import { isPathInside } from "../infra/path-guards.js";
11+
import {
12+
closeOpenClawAgentDatabaseByPath,
13+
listOpenClawAgentDatabasesForTest,
14+
} from "../state/openclaw-agent-db.js";
15+
import { closeOpenClawStateDatabaseByPath } from "../state/openclaw-state-db.js";
16+
import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
1017
import { captureEnv } from "./env.js";
1118
import { cleanupSessionStateForTest } from "./session-state-cleanup.js";
1219

@@ -357,8 +364,21 @@ export async function createOpenClawTestState(
357364
}
358365
cleaned = true;
359366
await cleanupSessionStateForTest().catch(() => undefined);
367+
// Agent close releases leases through shared state; closing shared state first
368+
// can reopen it during teardown and leave Windows handles under the fixture root.
369+
for (const database of listOpenClawAgentDatabasesForTest()) {
370+
if (isPathInside(paths.stateDir, database.path)) {
371+
closeOpenClawAgentDatabaseByPath(database.path);
372+
}
373+
}
374+
closeOpenClawStateDatabaseByPath(resolveOpenClawStateSqlitePath(env));
360375
state.restoreEnv();
361-
await fs.rm(root, { recursive: true, force: true });
376+
await fs.rm(root, {
377+
recursive: true,
378+
force: true,
379+
maxRetries: 20,
380+
retryDelay: 25,
381+
});
362382
},
363383
};
364384

‎test/package-scripts.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ describe("package scripts", () => {
181181
);
182182
});
183183

184+
it("runs shared test-state cleanup coverage in Windows CI", () => {
185+
expect(readPackageJson().scripts["test:windows:ci"]).toContain(
186+
"src/test-utils/openclaw-test-state.test.ts",
187+
);
188+
});
189+
184190
it("runs cross-OS installer behavior coverage in Windows CI", () => {
185191
expect(readPackageJson().scripts["test:windows:ci"]).toContain(
186192
"test/scripts/openclaw-cross-os-installer.windows.test.ts",

0 commit comments

Comments
 (0)