@@ -111,6 +111,26 @@ function assertRuntimeDataBootstrap(runtimeRoot) {
111111 }
112112}
113113
114+ function assertCodexAdapterState ( root ) {
115+ const sourceInstructions = fs . readFileSync ( path . join ( ROOT , 'instructions.md' ) , 'utf8' )
116+ const agentsMd = fs . readFileSync ( path . join ( root , 'AGENTS.md' ) , 'utf8' )
117+ const hooks = readJson ( root , '.codex/hooks.json' )
118+ const sourceSkillFiles = walk ( path . join ( ROOT , 'skills' ) ) . filter ( file => path . basename ( file ) === 'SKILL.md' )
119+ const installedSkillFiles = walk ( path . join ( root , '.agents' , 'skills' ) ) . filter ( file => path . basename ( file ) === 'SKILL.md' )
120+
121+ assert . strictEqual ( agentsMd , sourceInstructions )
122+ assert . strictEqual ( installedSkillFiles . length , sourceSkillFiles . length )
123+ assert . ok ( fs . existsSync ( path . join ( root , '.agents' , 'skills' , 'routing' , 'SKILL.md' ) ) )
124+ assert . ok ( fs . existsSync ( path . join ( root , '.codex' , 'hooks' , '_runtime' , 'lifecycle.cjs' ) ) )
125+
126+ for ( const eventName of [ 'PreToolUse' , 'UserPromptSubmit' , 'PostToolUse' , 'Stop' ] ) {
127+ const entries = hooks . hooks ?. [ eventName ]
128+ assert . ok ( Array . isArray ( entries ) && entries . length > 0 , `missing Codex hook event: ${ eventName } ` )
129+ const commands = JSON . stringify ( entries )
130+ assert . ok ( commands . includes ( 'node ./.codex/hooks/_runtime/lifecycle.cjs' ) , `unexpected hook command for ${ eventName } ` )
131+ }
132+ }
133+
114134function assertClaudeMergeState ( root , { claudeMdManaged } ) {
115135 const settings = readJson ( root , '.claude/settings.json' )
116136 const mcp = readJson ( root , '.mcp.json' )
@@ -149,6 +169,7 @@ function testClaudeInitPreservesCustomConfig() {
149169
150170 runCli ( [ 'init' , '--claude' ] , root )
151171 assertClaudeMergeState ( root , { claudeMdManaged : false } )
172+ assertRuntimeDataBootstrap ( path . join ( root , '.devcodex' ) )
152173
153174 const backupRoot = path . join ( root , '.devcodex' , '.tmp' , 'backups' )
154175 assert . strictEqual ( findBackups ( backupRoot , 'CLAUDE.md' ) . length , 0 )
@@ -164,6 +185,7 @@ function testClaudeUpdateBacksUpAndPreservesCustomConfig() {
164185
165186 runCli ( [ 'update' , '--claude' ] , root )
166187 assertClaudeMergeState ( root , { claudeMdManaged : true } )
188+ assertRuntimeDataBootstrap ( path . join ( root , '.devcodex' ) )
167189
168190 const backupRoot = path . join ( root , '.devcodex' , '.tmp' , 'backups' )
169191 assert . ok ( findBackups ( backupRoot , 'CLAUDE.md' ) . length >= 1 )
@@ -217,6 +239,51 @@ function testCodexInitBootstrapsWorkspaceNamespaceData() {
217239 fs . rmSync ( root , { recursive : true , force : true } )
218240}
219241
242+ function testCodexInitBacksUpManagedFiles ( ) {
243+ const root = createTempRoot ( 'devcodex-cli-codex-init-' )
244+ writeFile ( root , 'package.json' , '{ "name": "tmp-codex-init" }\n' )
245+ writeFile ( root , 'AGENTS.md' , '# custom agents instructions\n' )
246+ writeJson ( root , '.codex/hooks.json' , {
247+ hooks : {
248+ Stop : [ { hooks : [ { type : 'command' , command : 'echo custom-stop' } ] } ]
249+ }
250+ } )
251+
252+ runCli ( [ 'init' , '--codex' ] , root )
253+
254+ assertCodexAdapterState ( root )
255+ assertRuntimeDataBootstrap ( path . join ( root , '.devcodex' ) )
256+
257+ const backupRoot = path . join ( root , '.devcodex' , '.tmp' , 'backups' )
258+ assert . ok ( findBackups ( backupRoot , 'AGENTS.md' ) . length >= 1 )
259+ assert . ok ( findBackups ( backupRoot , 'hooks.json' ) . length >= 1 )
260+
261+ fs . rmSync ( root , { recursive : true , force : true } )
262+ }
263+
264+ function testCodexUpdateRefreshesAdapterInWorkspaceNamespace ( ) {
265+ const root = createTempRoot ( 'devcodex-cli-codex-update-' )
266+ writeJson ( root , '.devcodex/layout.json' , { version : 1 , mode : 'workspace-namespace' } )
267+ writeFile ( root , 'packages/app-a/package.json' , '{ "name": "app-a" }\n' )
268+ writeFile ( root , 'packages/app-a/AGENTS.md' , '# stale codex agent\n' )
269+ writeJson ( root , 'packages/app-a/.codex/hooks.json' , {
270+ hooks : {
271+ UserPromptSubmit : [ { hooks : [ { type : 'command' , command : 'echo stale-command' } ] } ]
272+ }
273+ } )
274+
275+ runCli ( [ 'update' , '--codex' ] , path . join ( root , 'packages' , 'app-a' ) )
276+
277+ assertCodexAdapterState ( path . join ( root , 'packages' , 'app-a' ) )
278+ assertRuntimeDataBootstrap ( path . join ( root , '.devcodex' , 'packages' , 'app-a' ) )
279+
280+ const backupRoot = path . join ( root , '.devcodex' , 'packages' , 'app-a' , '.tmp' , 'backups' )
281+ assert . ok ( findBackups ( backupRoot , 'AGENTS.md' ) . length >= 1 )
282+ assert . ok ( findBackups ( backupRoot , 'hooks.json' ) . length >= 1 )
283+
284+ fs . rmSync ( root , { recursive : true , force : true } )
285+ }
286+
220287function testProfileInitUsesNestedNamespaceRoot ( ) {
221288 const root = createTempRoot ( 'devcodex-cli-profile-' )
222289 writeJson ( root , '.devcodex/layout.json' , { version : 1 , mode : 'workspace-namespace' } )
@@ -239,6 +306,8 @@ function main() {
239306 testDoctorAvoidsCodexBiasInMixedHostRepo ( )
240307 testDefaultInitBootstrapsActiveRootData ( )
241308 testCodexInitBootstrapsWorkspaceNamespaceData ( )
309+ testCodexInitBacksUpManagedFiles ( )
310+ testCodexUpdateRefreshesAdapterInWorkspaceNamespace ( )
242311 testProfileInitUsesNestedNamespaceRoot ( )
243312 process . stdout . write ( 'cli behavior test passed\n' )
244313}
0 commit comments