1+ // @effect -diagnostics nodeBuiltinImport:off
2+ import * as NodeSqlite from "node:sqlite" ;
3+
14import * as NodeServices from "@effect/platform-node/NodeServices" ;
25import { describe , expect , it } from "@effect/vitest" ;
36import * as Effect from "effect/Effect" ;
@@ -6,9 +9,11 @@ import * as Path from "effect/Path";
69import * as Schema from "effect/Schema" ;
710
811import { CodexSettings } from "@t3tools/contracts" ;
12+ import * as HostProcess from "@t3tools/shared/HostProcess" ;
913import {
1014 CodexShadowHomeEntryConflictError ,
1115 CodexShadowHomePathConflictError ,
16+ codexHomeEnvironment ,
1217 materializeCodexShadowHome ,
1318 resolveCodexHomeLayout ,
1419} from "./CodexHomeLayout.ts" ;
@@ -64,6 +69,65 @@ it.layer(NodeServices.layer)("CodexHomeLayout", (it) => {
6469 ) ;
6570 } ) ;
6671
72+ describe ( "codexHomeEnvironment" , ( ) => {
73+ it . effect . each ( [ "live" , "interrupted" ] as const ) (
74+ "reads committed shadow WAL data with a %s connection" ,
75+ ( connection ) =>
76+ Effect . gen ( function * ( ) {
77+ const fileSystem = yield * FileSystem . FileSystem ;
78+ const path = yield * Path . Path ;
79+ const sharedHome = yield * makeTempDir ( "t3code-codex-shared-" ) ;
80+ const shadowHome = yield * makeTempDir ( "t3code-codex-shadow-" ) ;
81+ const sharedDatabasePath = path . join ( sharedHome , "state_5.sqlite" ) ;
82+ const shadowDatabasePath = path . join ( shadowHome , "state_5.sqlite" ) ;
83+ {
84+ using seed = new NodeSqlite . DatabaseSync ( sharedDatabasePath ) ;
85+ seed . exec ( "PRAGMA journal_mode = WAL; CREATE TABLE entries (value TEXT);" ) ;
86+ }
87+ // Windows places sidecars beside database links; Unix resolves the target first.
88+ if ( ( yield * HostProcess . isWindows ) && symlinksSupported ) {
89+ yield * fileSystem . symlink ( sharedDatabasePath , shadowDatabasePath ) ;
90+ } else {
91+ yield * fileSystem . copyFile ( sharedDatabasePath , shadowDatabasePath ) ;
92+ }
93+ using writer = new NodeSqlite . DatabaseSync ( shadowDatabasePath ) ;
94+ writer . exec ( "PRAGMA wal_autocheckpoint = 0; INSERT INTO entries VALUES ('committed');" ) ;
95+ const database = yield * fileSystem . readFile ( shadowDatabasePath ) ;
96+ const walPath = `${ shadowDatabasePath } -wal` ;
97+ const wal = yield * fileSystem . readFile ( walPath ) ;
98+ expect ( wal . byteLength ) . toBeGreaterThan ( 0 ) ;
99+ if ( connection === "interrupted" ) {
100+ // Restore the uncheckpointed disk image only after SQLite closes our fixture.
101+ writer . close ( ) ;
102+ yield * fileSystem . writeFile ( shadowDatabasePath , database ) ;
103+ yield * fileSystem . writeFile ( walPath , wal ) ;
104+ }
105+ {
106+ using sharedReader = new NodeSqlite . DatabaseSync ( sharedDatabasePath ) ;
107+ expect ( sharedReader . prepare ( "SELECT value FROM entries" ) . all ( ) ) . toEqual ( [ ] ) ;
108+ }
109+
110+ const layout = yield * resolveCodexHomeLayout (
111+ decodeCodexSettings ( { homePath : sharedHome , shadowHomePath : shadowHome } ) ,
112+ ) ;
113+ const environment = yield * codexHomeEnvironment ( layout , { } ) ;
114+ expect ( yield * fileSystem . readFile ( walPath ) ) . toEqual ( wal ) ;
115+ using reader = new NodeSqlite . DatabaseSync (
116+ path . join ( environment . CODEX_SQLITE_HOME ! , "state_5.sqlite" ) ,
117+ ) ;
118+ expect ( reader . prepare ( "SELECT value FROM entries" ) . all ( ) ) . toEqual ( [
119+ { value : "committed" } ,
120+ ] ) ;
121+ if ( connection === "live" ) {
122+ writer . exec ( "INSERT INTO entries VALUES ('still writable');" ) ;
123+ expect ( reader . prepare ( "SELECT count(*) AS count FROM entries" ) . get ( ) ) . toEqual ( {
124+ count : 2 ,
125+ } ) ;
126+ }
127+ } ) ,
128+ ) ;
129+ } ) ;
130+
67131 describe ( "materializeCodexShadowHome" , ( ) => {
68132 it . effect . skipIf ( ! symlinksSupported ) (
69133 "preserves SQLite files when shared sidecars appear after the shadow home starts" ,
0 commit comments