Skip to content

Commit bd23864

Browse files
steipetePeter Steinberger
andauthored
test(state): reuse canonical database template (#118552)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent c40faef commit bd23864

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import os from "node:os";
55
import path from "node:path";
66
import type { DatabaseSync } from "node:sqlite";
77
import { pathToFileURL } from "node:url";
8-
import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
8+
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
99
import { cleanupTempDirs, makeTempDir } from "../../test/helpers/temp-dir.js";
1010
import { buildApprovalResolutionRef } from "../infra/approval-resolution-ref.js";
1111
import {
@@ -52,6 +52,7 @@ type StateDbTestDatabase = Pick<
5252
>;
5353

5454
const stateDbTempDirs: string[] = [];
55+
let canonicalStateDatabaseTemplatePath: string | undefined;
5556

5657
function createTempStateDir(): string {
5758
return makeTempDir(stateDbTempDirs, "openclaw-state-db-");
@@ -418,9 +419,13 @@ function createLegacyAuditStateDatabase(stateDir: string): string {
418419
}
419420

420421
function createCanonicalAuditStateDatabase(stateDir: string): string {
421-
const database = openOpenClawStateDatabase({ env: { OPENCLAW_STATE_DIR: stateDir } });
422-
const databasePath = database.path;
423-
closeOpenClawStateDatabaseForTest();
422+
if (!canonicalStateDatabaseTemplatePath) {
423+
throw new Error("canonical state database template was not initialized");
424+
}
425+
// These cases own post-initialization schema drift; fresh creation and migrations stay real below.
426+
const databasePath = resolveOpenClawStateSqlitePath({ OPENCLAW_STATE_DIR: stateDir });
427+
fs.mkdirSync(path.dirname(databasePath), { recursive: true });
428+
fs.copyFileSync(canonicalStateDatabaseTemplatePath, databasePath);
424429
return databasePath;
425430
}
426431

@@ -1028,6 +1033,14 @@ function runConcurrentSchemaProbe(params: {
10281033
return JSON.parse(resultLine) as string[];
10291034
}
10301035

1036+
beforeAll(() => {
1037+
const stateDir = createTempStateDir();
1038+
canonicalStateDatabaseTemplatePath = openOpenClawStateDatabase({
1039+
env: { OPENCLAW_STATE_DIR: stateDir },
1040+
}).path;
1041+
closeOpenClawStateDatabaseForTest();
1042+
});
1043+
10311044
afterAll(() => {
10321045
cleanupTempDirs(stateDbTempDirs);
10331046
});

0 commit comments

Comments
 (0)