1- // Fresh-store reads: a state DB created before the first plugin-state write has no
1+ // Fresh-store reads: a startup-checkpoint DB created before the first plugin-state write has no
22// plugin_state_entries table; read-only paths must report empty, not error (#117249).
3- import { mkdirSync } from "node:fs" ;
4- import path from "node:path" ;
53import { DatabaseSync } from "node:sqlite" ;
64import { afterEach , describe , expect , it } from "vitest" ;
5+ import { withOpenClawStateStartupMigrationCheckpointDatabase } from "../state/openclaw-state-db.js" ;
76import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js" ;
87import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js" ;
98import {
@@ -12,21 +11,37 @@ import {
1211 pluginStateEntriesInKeyRange ,
1312 resetPluginStateStoreForTests ,
1413} from "./plugin-state-store.js" ;
14+ import { PluginStateStoreError } from "./plugin-state-store.types.js" ;
1515
1616afterEach ( ( ) => {
1717 resetPluginStateStoreForTests ( { closeDatabase : false } ) ;
1818} ) ;
1919
20+ async function expectPluginStateReadFailure (
21+ promise : Promise < unknown > ,
22+ expected : { operation : "entries" | "lookup" ; path : string } ,
23+ ) : Promise < void > {
24+ let storeError : unknown ;
25+ try {
26+ await promise ;
27+ } catch ( error ) {
28+ storeError = error ;
29+ }
30+ expect ( storeError ) . toBeInstanceOf ( PluginStateStoreError ) ;
31+ expect ( storeError ) . toMatchObject ( {
32+ code : "PLUGIN_STATE_READ_FAILED" ,
33+ operation : expected . operation ,
34+ path : expected . path ,
35+ } ) ;
36+ }
37+
2038describe ( "plugin state fresh-store reads" , ( ) => {
2139 it ( "treats an existing state database without the lazy plugin-state table as empty" , async ( ) => {
2240 await withOpenClawTestState (
2341 { label : "plugin-state-read-only-table-missing" , applyEnv : false } ,
2442 async ( state ) => {
2543 const databasePath = resolveOpenClawStateSqlitePath ( state . env ) ;
26- mkdirSync ( path . dirname ( databasePath ) , { recursive : true } ) ;
27- const database = new DatabaseSync ( databasePath ) ;
28- database . exec ( "CREATE TABLE unrelated_state (id INTEGER PRIMARY KEY)" ) ;
29- database . close ( ) ;
44+ withOpenClawStateStartupMigrationCheckpointDatabase ( ( ) => undefined , { env : state . env } ) ;
3045
3146 const store = createPluginStateKeyedStore ( "discord" , {
3247 namespace : "read-only-table-missing" ,
@@ -49,14 +64,45 @@ describe("plugin state fresh-store reads", () => {
4964 expect ( countPluginStateLiveEntries ( "discord" , state . env ) ) . toBe ( 0 ) ;
5065
5166 const verify = new DatabaseSync ( databasePath , { readOnly : true } ) ;
52- expect (
53- verify
54- . prepare (
55- "SELECT name FROM sqlite_schema WHERE type = 'table' AND name = 'plugin_state_entries'" ,
56- )
57- . get ( ) ,
58- ) . toBeUndefined ( ) ;
67+ const tables = verify
68+ . prepare ( "SELECT name FROM sqlite_schema WHERE type = 'table' ORDER BY name" )
69+ . all ( ) ;
5970 verify . close ( ) ;
71+ expect ( tables ) . toEqual ( [ { name : "schema_meta" } , { name : "state_leases" } ] ) ;
72+ } ,
73+ ) ;
74+ } ) ;
75+
76+ it ( "rejects a missing plugin-state table after another state table has been initialized" , async ( ) => {
77+ await withOpenClawTestState (
78+ { label : "plugin-state-read-only-table-damaged" , applyEnv : false } ,
79+ async ( state ) => {
80+ const databasePath = resolveOpenClawStateSqlitePath ( state . env ) ;
81+ withOpenClawStateStartupMigrationCheckpointDatabase ( ( ) => undefined , { env : state . env } ) ;
82+ const database = new DatabaseSync ( databasePath ) ;
83+ database . exec ( `
84+ CREATE TABLE config_machine_state (
85+ state_key TEXT NOT NULL PRIMARY KEY,
86+ value_json TEXT NOT NULL,
87+ updated_at_ms INTEGER NOT NULL
88+ ) STRICT
89+ ` ) ;
90+ database . close ( ) ;
91+
92+ const store = createPluginStateKeyedStore ( "discord" , {
93+ namespace : "read-only-table-damaged" ,
94+ maxEntries : 10 ,
95+ env : state . env ,
96+ } ) ;
97+
98+ await expectPluginStateReadFailure ( store . lookup ( "k" ) , {
99+ operation : "lookup" ,
100+ path : databasePath ,
101+ } ) ;
102+ await expectPluginStateReadFailure ( store . entries ( ) , {
103+ operation : "entries" ,
104+ path : databasePath ,
105+ } ) ;
60106 } ,
61107 ) ;
62108 } ) ;
0 commit comments