@@ -5,7 +5,8 @@ import path from "node:path";
55import { DatabaseSync } from "node:sqlite" ;
66import { type WorkboardCard , WORKBOARD_STATUSES } from "@openclaw/workboard-contract" ;
77import { MAX_DATE_TIMESTAMP_MS } from "openclaw/plugin-sdk/number-runtime" ;
8- import { describe , expect , it , vi } from "vitest" ;
8+ import { useAutoCleanupTempDirTracker } from "openclaw/plugin-sdk/test-env" ;
9+ import { afterEach , describe , expect , it , vi } from "vitest" ;
910import type {
1011 PersistedWorkboardAttachment ,
1112 PersistedWorkboardBoard ,
@@ -200,6 +201,45 @@ function statfsFixture(type: number): ReturnType<typeof fs.statfsSync> {
200201 } ;
201202}
202203
204+ const WORKBOARD_CARD_CHILD_INDEXES = [
205+ [ "workboard_card_events" , "workboard_card_events_card_idx" ] ,
206+ [ "workboard_card_attempts" , "workboard_card_attempts_card_idx" ] ,
207+ [ "workboard_card_comments" , "workboard_card_comments_card_idx" ] ,
208+ [ "workboard_card_links" , "workboard_card_links_card_idx" ] ,
209+ [ "workboard_card_proof" , "workboard_card_proof_card_idx" ] ,
210+ [ "workboard_card_artifacts" , "workboard_card_artifacts_card_idx" ] ,
211+ [ "workboard_card_notifications" , "workboard_card_notifications_card_idx" ] ,
212+ [ "workboard_worker_logs" , "workboard_worker_logs_card_idx" ] ,
213+ ] as const ;
214+
215+ const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
216+
217+ function explainWorkboardQueryPlan (
218+ db : DatabaseSync ,
219+ sql : string ,
220+ params : readonly ( number | string | null ) [ ] = [ ] ,
221+ ) : string {
222+ const rows = db . prepare ( `EXPLAIN QUERY PLAN ${ sql } ` ) . all ( ...params ) as Array < {
223+ detail ?: unknown ;
224+ } > ;
225+ return rows
226+ . map ( ( row ) => ( typeof row . detail === "string" ? row . detail : JSON . stringify ( row . detail ?? "" ) ) )
227+ . join ( "\n" ) ;
228+ }
229+
230+ function withWorkboardSqliteDatabase ( prefix : string , run : ( db : DatabaseSync ) => void ) : void {
231+ const dir = tempDirs . make ( prefix ) ;
232+ const dbPath = path . join ( dir , "workboard.sqlite" ) ;
233+ const stores = createWorkboardSqliteStores ( { dbPath } ) ;
234+ stores . close ( ) ;
235+ const db = new DatabaseSync ( dbPath ) ;
236+ try {
237+ run ( db ) ;
238+ } finally {
239+ db . close ( ) ;
240+ }
241+ }
242+
203243describe ( "WorkboardStore" , ( ) => {
204244 it ( "emits one monotonic change after each visible mutation" , async ( ) => {
205245 const store = new WorkboardStore ( createMemoryStore ( ) ) ;
@@ -542,6 +582,85 @@ describe("WorkboardStore", () => {
542582 expect ( restored . metadata ?. archivedAt ) . toBeUndefined ( ) ;
543583 } ) ;
544584
585+ it ( "uses card child indexes for per-card ordered reads" , ( ) => {
586+ withWorkboardSqliteDatabase ( "openclaw-workboard-index-read-" , ( db ) => {
587+ for ( const [ table , index ] of WORKBOARD_CARD_CHILD_INDEXES ) {
588+ const plan = explainWorkboardQueryPlan (
589+ db ,
590+ `SELECT * FROM ${ table } WHERE card_id = ? ORDER BY ordinal ASC` ,
591+ [ "card-1" ] ,
592+ ) ;
593+ expect ( plan ) . toContain ( index ) ;
594+ expect ( plan ) . not . toContain ( "USE TEMP B-TREE FOR ORDER BY" ) ;
595+ }
596+ } ) ;
597+ } ) ;
598+
599+ it ( "uses card child indexes for whole-board ordered scans" , ( ) => {
600+ withWorkboardSqliteDatabase ( "openclaw-workboard-index-scan-" , ( db ) => {
601+ for ( const [ table , index ] of WORKBOARD_CARD_CHILD_INDEXES ) {
602+ const plan = explainWorkboardQueryPlan (
603+ db ,
604+ `SELECT * FROM ${ table } ORDER BY card_id ASC, ordinal ASC` ,
605+ ) ;
606+ expect ( plan ) . toContain ( index ) ;
607+ expect ( plan ) . not . toContain ( "USE TEMP B-TREE FOR ORDER BY" ) ;
608+ }
609+ } ) ;
610+ } ) ;
611+
612+ it ( "uses card child indexes for parent-card cascades" , ( ) => {
613+ withWorkboardSqliteDatabase ( "openclaw-workboard-index-cascade-" , ( db ) => {
614+ db . exec ( "PRAGMA foreign_keys = ON" ) ;
615+ const plan = explainWorkboardQueryPlan ( db , "DELETE FROM workboard_cards WHERE id = ?" , [
616+ "card-1" ,
617+ ] ) ;
618+ for ( const [ , index ] of WORKBOARD_CARD_CHILD_INDEXES ) {
619+ expect ( plan ) . toContain ( index ) ;
620+ }
621+ } ) ;
622+ } ) ;
623+
624+ it ( "restores dropped card child indexes without changing the schema version" , ( ) => {
625+ const dir = tempDirs . make ( "openclaw-workboard-index-reopen-" ) ;
626+ const dbPath = path . join ( dir , "workboard.sqlite" ) ;
627+ const initialized = createWorkboardSqliteStores ( { dbPath } ) ;
628+ initialized . close ( ) ;
629+ const db = new DatabaseSync ( dbPath ) ;
630+ let initialMigrationIds : Array < { id : string } > ;
631+ try {
632+ initialMigrationIds = db
633+ . prepare ( "SELECT id FROM workboard_schema_migrations ORDER BY id" )
634+ . all ( ) as Array < { id : string } > ;
635+ for ( const [ , index ] of WORKBOARD_CARD_CHILD_INDEXES ) {
636+ db . exec ( `DROP INDEX ${ index } ` ) ;
637+ }
638+ } finally {
639+ db . close ( ) ;
640+ }
641+
642+ const reopened = createWorkboardSqliteStores ( { dbPath } ) ;
643+ reopened . close ( ) ;
644+ const verified = new DatabaseSync ( dbPath , { readOnly : true } ) ;
645+ try {
646+ const indexes = new Set (
647+ (
648+ verified . prepare ( "SELECT name FROM sqlite_schema WHERE type = 'index'" ) . all ( ) as Array < {
649+ name : string ;
650+ } >
651+ ) . map ( ( row ) => row . name ) ,
652+ ) ;
653+ for ( const [ , index ] of WORKBOARD_CARD_CHILD_INDEXES ) {
654+ expect ( indexes ) . toContain ( index ) ;
655+ }
656+ expect (
657+ verified . prepare ( "SELECT id FROM workboard_schema_migrations ORDER BY id" ) . all ( ) ,
658+ ) . toEqual ( initialMigrationIds ) ;
659+ } finally {
660+ verified . close ( ) ;
661+ }
662+ } ) ;
663+
545664 it ( "persists boards, cards, subscriptions, and attachment blobs in sqlite" , async ( ) => {
546665 const dir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-workboard-sqlite-" ) ) ;
547666 const dbPath = path . join ( dir , "workboard.sqlite" ) ;
0 commit comments