@@ -505,6 +505,129 @@ describe("addCustomInstructions", () => {
505505 expect ( result ) . toContain ( "Rules from .roorules-test-mode:\nmode specific rules" )
506506 } )
507507
508+ it ( "should load AGENTS.md when settings.useAgentRules is true" , async ( ) => {
509+ // Simulate no .roo/rules-test-mode directory
510+ statMock . mockRejectedValueOnce ( { code : "ENOENT" } )
511+
512+ readFileMock . mockImplementation ( ( filePath : PathLike ) => {
513+ const pathStr = filePath . toString ( )
514+ if ( pathStr . endsWith ( "AGENTS.md" ) ) {
515+ return Promise . resolve ( "Agent rules from AGENTS.md file" )
516+ }
517+ return Promise . reject ( { code : "ENOENT" } )
518+ } )
519+
520+ const result = await addCustomInstructions (
521+ "mode instructions" ,
522+ "global instructions" ,
523+ "/fake/path" ,
524+ "test-mode" ,
525+ { settings : { maxConcurrentFileReads : 5 , todoListEnabled : true , useAgentRules : true } } ,
526+ )
527+
528+ expect ( result ) . toContain ( "# Agent Rules Standard (AGENTS.md):" )
529+ expect ( result ) . toContain ( "Agent rules from AGENTS.md file" )
530+ expect ( readFileMock ) . toHaveBeenCalledWith ( expect . stringContaining ( "AGENTS.md" ) , "utf-8" )
531+ } )
532+
533+ it ( "should not load AGENTS.md when settings.useAgentRules is false" , async ( ) => {
534+ // Simulate no .roo/rules-test-mode directory
535+ statMock . mockRejectedValueOnce ( { code : "ENOENT" } )
536+
537+ readFileMock . mockImplementation ( ( filePath : PathLike ) => {
538+ const pathStr = filePath . toString ( )
539+ if ( pathStr . endsWith ( "AGENTS.md" ) ) {
540+ return Promise . resolve ( "Agent rules from AGENTS.md file" )
541+ }
542+ return Promise . reject ( { code : "ENOENT" } )
543+ } )
544+
545+ const result = await addCustomInstructions (
546+ "mode instructions" ,
547+ "global instructions" ,
548+ "/fake/path" ,
549+ "test-mode" ,
550+ { settings : { maxConcurrentFileReads : 5 , todoListEnabled : true , useAgentRules : false } } ,
551+ )
552+
553+ expect ( result ) . not . toContain ( "# Agent Rules Standard (AGENTS.md):" )
554+ expect ( result ) . not . toContain ( "Agent rules from AGENTS.md file" )
555+ } )
556+
557+ it ( "should load AGENTS.md by default when settings.useAgentRules is undefined" , async ( ) => {
558+ // Simulate no .roo/rules-test-mode directory
559+ statMock . mockRejectedValueOnce ( { code : "ENOENT" } )
560+
561+ readFileMock . mockImplementation ( ( filePath : PathLike ) => {
562+ const pathStr = filePath . toString ( )
563+ if ( pathStr . endsWith ( "AGENTS.md" ) ) {
564+ return Promise . resolve ( "Agent rules from AGENTS.md file" )
565+ }
566+ return Promise . reject ( { code : "ENOENT" } )
567+ } )
568+
569+ const result = await addCustomInstructions (
570+ "mode instructions" ,
571+ "global instructions" ,
572+ "/fake/path" ,
573+ "test-mode" ,
574+ { } , // No settings.useAgentRules specified
575+ )
576+
577+ expect ( result ) . toContain ( "# Agent Rules Standard (AGENTS.md):" )
578+ expect ( result ) . toContain ( "Agent rules from AGENTS.md file" )
579+ expect ( readFileMock ) . toHaveBeenCalledWith ( expect . stringContaining ( "AGENTS.md" ) , "utf-8" )
580+ } )
581+
582+ it ( "should handle missing AGENTS.md gracefully" , async ( ) => {
583+ // Simulate no .roo/rules-test-mode directory
584+ statMock . mockRejectedValueOnce ( { code : "ENOENT" } )
585+
586+ readFileMock . mockRejectedValue ( { code : "ENOENT" } )
587+
588+ const result = await addCustomInstructions (
589+ "mode instructions" ,
590+ "global instructions" ,
591+ "/fake/path" ,
592+ "test-mode" ,
593+ { settings : { maxConcurrentFileReads : 5 , todoListEnabled : true , useAgentRules : true } } ,
594+ )
595+
596+ expect ( result ) . toContain ( "Global Instructions:\nglobal instructions" )
597+ expect ( result ) . toContain ( "Mode-specific Instructions:\nmode instructions" )
598+ expect ( result ) . not . toContain ( "# Agent Rules Standard (AGENTS.md):" )
599+ } )
600+
601+ it ( "should include AGENTS.md content along with other rules" , async ( ) => {
602+ // Simulate no .roo/rules-test-mode directory
603+ statMock . mockRejectedValueOnce ( { code : "ENOENT" } )
604+
605+ readFileMock . mockImplementation ( ( filePath : PathLike ) => {
606+ const pathStr = filePath . toString ( )
607+ if ( pathStr . endsWith ( "AGENTS.md" ) ) {
608+ return Promise . resolve ( "Agent rules content" )
609+ }
610+ if ( pathStr . endsWith ( ".roorules" ) ) {
611+ return Promise . resolve ( "Roo rules content" )
612+ }
613+ return Promise . reject ( { code : "ENOENT" } )
614+ } )
615+
616+ const result = await addCustomInstructions (
617+ "mode instructions" ,
618+ "global instructions" ,
619+ "/fake/path" ,
620+ "test-mode" ,
621+ { settings : { maxConcurrentFileReads : 5 , todoListEnabled : true , useAgentRules : true } } ,
622+ )
623+
624+ // Should contain both AGENTS.md and .roorules content
625+ expect ( result ) . toContain ( "# Agent Rules Standard (AGENTS.md):" )
626+ expect ( result ) . toContain ( "Agent rules content" )
627+ expect ( result ) . toContain ( "# Rules from .roorules:" )
628+ expect ( result ) . toContain ( "Roo rules content" )
629+ } )
630+
508631 it ( "should return empty string when no instructions provided" , async ( ) => {
509632 // Simulate no .roo/rules directory
510633 statMock . mockRejectedValueOnce ( { code : "ENOENT" } )
0 commit comments