@@ -42,6 +42,19 @@ function runRestore(codexHome: string, ocxHome: string): { stdout: string; statu
4242 return { stdout : result . stdout ?. trim ( ) ?? "" , status : result . status ?? 1 } ;
4343}
4444
45+ function runRestoreAsync ( codexHome : string , ocxHome : string ) : { stdout : string ; status : number } {
46+ const script = `
47+ const { restoreNativeCodexAsync } = require("./src/codex/inject");
48+ restoreNativeCodexAsync().then(result => console.log(JSON.stringify(result)));
49+ ` ;
50+ const result = spawnSync ( process . execPath , [ "--eval" , script ] , {
51+ cwd : repoRoot ,
52+ env : { ...process . env , CODEX_HOME : codexHome , OPENCODEX_HOME : ocxHome } ,
53+ encoding : "utf8" ,
54+ } ) ;
55+ return { stdout : result . stdout ?. trim ( ) ?? "" , status : result . status ?? 1 } ;
56+ }
57+
4558describe ( "injectCodexConfig integration (Design B)" , ( ) => {
4659 let codexHome : string ;
4760 let ocxHome : string ;
@@ -505,6 +518,35 @@ describe("injectCodexConfig integration (Design B)", () => {
505518 expect ( existsSync ( journalPath ) ) . toBe ( false ) ;
506519 } ) ;
507520
521+ test ( "async restore preserves external provider history" , ( ) => {
522+ const configPath = join ( codexHome , "config.toml" ) ;
523+ writeFileSync ( configPath , 'model_provider = "custom"\n' , "utf8" ) ;
524+ const rolloutPath = join ( codexHome , "rollout-custom.jsonl" ) ;
525+ const rollout = `${ JSON . stringify ( {
526+ type : "session_meta" ,
527+ payload : { id : "thread-custom" , model_provider : "opencodex" , source : "cli" } ,
528+ } ) } \n`;
529+ writeFileSync ( rolloutPath , rollout , "utf8" ) ;
530+ const dbPath = join ( codexHome , "state_5.sqlite" ) ;
531+ const db = new Database ( dbPath ) ;
532+ db . run ( `CREATE TABLE threads (
533+ id TEXT PRIMARY KEY, rollout_path TEXT NOT NULL, model_provider TEXT NOT NULL,
534+ source TEXT NOT NULL, first_user_message TEXT NOT NULL, has_user_event INTEGER NOT NULL
535+ )` ) ;
536+ db . run ( `INSERT INTO threads VALUES ('thread-custom', ?, 'opencodex', 'cli', 'hello', 1)` , rolloutPath ) ;
537+ db . close ( ) ;
538+
539+ const result = runRestoreAsync ( codexHome , ocxHome ) ;
540+
541+ expect ( result . status ) . toBe ( 0 ) ;
542+ expect ( JSON . parse ( result . stdout ) . message ) . toContain ( 'External Codex provider "custom" preserved' ) ;
543+ const restored = new Database ( dbPath , { readonly : true } ) ;
544+ expect ( restored . query ( "SELECT model_provider FROM threads WHERE id = 'thread-custom'" ) . get ( ) )
545+ . toEqual ( { model_provider : "opencodex" } ) ;
546+ restored . close ( ) ;
547+ expect ( readFileSync ( rolloutPath , "utf8" ) ) . toBe ( rollout ) ;
548+ } ) ;
549+
508550 test ( "provider selected through a legacy root profile is also preserved" , ( ) => {
509551 const original = [
510552 'profile = "work"' ,
0 commit comments