@@ -5,7 +5,7 @@ import os from "node:os";
55import path from "node:path" ;
66import type { DatabaseSync } from "node:sqlite" ;
77import { 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" ;
99import { cleanupTempDirs , makeTempDir } from "../../test/helpers/temp-dir.js" ;
1010import { buildApprovalResolutionRef } from "../infra/approval-resolution-ref.js" ;
1111import {
@@ -52,6 +52,7 @@ type StateDbTestDatabase = Pick<
5252> ;
5353
5454const stateDbTempDirs : string [ ] = [ ] ;
55+ let canonicalStateDatabaseTemplatePath : string | undefined ;
5556
5657function createTempStateDir ( ) : string {
5758 return makeTempDir ( stateDbTempDirs , "openclaw-state-db-" ) ;
@@ -418,9 +419,13 @@ function createLegacyAuditStateDatabase(stateDir: string): string {
418419}
419420
420421function 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+
10311044afterAll ( ( ) => {
10321045 cleanupTempDirs ( stateDbTempDirs ) ;
10331046} ) ;
0 commit comments