11import { describe , expect , test , beforeEach } from 'bun:test'
22import { z } from 'zod/v4'
3+
34import { initialSessionState } from '../run-state'
5+
46import type { CodebuffFileSystem } from '@codebuff/common/types/filesystem'
57
68describe ( 'Initial Session State' , ( ) => {
@@ -32,9 +34,17 @@ describe('Initial Session State', () => {
3234 return [
3335 { name : 'src' , isDirectory : ( ) => true , isFile : ( ) => false } ,
3436 { name : '.git' , isDirectory : ( ) => true , isFile : ( ) => false } ,
35- { name : 'knowledge.md' , isDirectory : ( ) => false , isFile : ( ) => true } ,
37+ {
38+ name : 'knowledge.md' ,
39+ isDirectory : ( ) => false ,
40+ isFile : ( ) => true ,
41+ } ,
3642 { name : 'README.md' , isDirectory : ( ) => false , isFile : ( ) => true } ,
37- { name : '.gitignore' , isDirectory : ( ) => false , isFile : ( ) => true } ,
43+ {
44+ name : '.gitignore' ,
45+ isDirectory : ( ) => false ,
46+ isFile : ( ) => true ,
47+ } ,
3848 ] as any
3949 }
4050 if ( path . includes ( 'src' ) ) {
@@ -45,10 +55,11 @@ describe('Initial Session State', () => {
4555 }
4656 return [ ]
4757 } ,
48- stat : async ( path : string ) => ( {
49- isDirectory : ( ) => path . includes ( 'src' ) || path . includes ( '.git' ) ,
50- isFile : ( ) => ! path . includes ( 'src' ) && ! path . includes ( '.git' ) ,
51- } ) as any ,
58+ stat : async ( path : string ) =>
59+ ( {
60+ isDirectory : ( ) => path . includes ( 'src' ) || path . includes ( '.git' ) ,
61+ isFile : ( ) => ! path . includes ( 'src' ) && ! path . includes ( '.git' ) ,
62+ } ) as any ,
5263 exists : async ( path : string ) => {
5364 if ( path . includes ( '.gitignore' ) ) return true
5465 if ( path . includes ( 'src' ) ) return true
@@ -70,7 +81,8 @@ describe('Initial Session State', () => {
7081 test ( 'creates initial session state with explicit projectFiles' , async ( ) => {
7182 const projectFiles = {
7283 'src/index.ts' : 'console.log("Hello world");' ,
73- 'src/utils.ts' : 'export function add(a: number, b: number) { return a + b; }' ,
84+ 'src/utils.ts' :
85+ 'export function add(a: number, b: number) { return a + b; }' ,
7486 'knowledge.md' : '# Knowledge\n\nThis is a knowledge file.' ,
7587 }
7688
@@ -121,7 +133,30 @@ describe('Initial Session State', () => {
121133 expect ( sessionState . fileContext . knowledgeFiles [ 'knowledge.md' ] ) . toBe (
122134 '# Knowledge\n\nThis is a knowledge file.' ,
123135 )
124- expect ( sessionState . fileContext . knowledgeFiles [ 'claude.md' ] ) . toBe (
136+ expect ( sessionState . fileContext . knowledgeFiles [ 'claude.md' ] ) . toBeUndefined ( )
137+ expect ( sessionState . fileContext . knowledgeFiles [ 'README.md' ] ) . toBeUndefined ( )
138+ } )
139+
140+ test ( 'derives reads knowledgeFiles from claude.md when knowledge.md is not present' , async ( ) => {
141+ const projectFiles = {
142+ 'src/index.ts' : 'console.log("Hello world");' ,
143+ 'claude.md' : '# Claude context\n\nThis is claude context.' ,
144+ 'README.md' : '# Project\n\nThis is a readme.' ,
145+ }
146+
147+ const sessionState = await initialSessionState ( {
148+ cwd : '/test-project' ,
149+ projectFiles,
150+ knowledgeFiles : undefined ,
151+ fs : mockFs ,
152+ logger : mockLogger ,
153+ } )
154+
155+ expect ( sessionState . fileContext . knowledgeFiles ) . toBeDefined ( )
156+ expect (
157+ sessionState . fileContext . knowledgeFiles [ 'knowledge.md' ] ,
158+ ) . toBeUndefined ( )
159+ expect ( sessionState . fileContext . knowledgeFiles [ 'claude.md' ] ) . toEqual (
125160 '# Claude context\n\nThis is claude context.' ,
126161 )
127162 expect ( sessionState . fileContext . knowledgeFiles [ 'README.md' ] ) . toBeUndefined ( )
@@ -146,7 +181,9 @@ describe('Initial Session State', () => {
146181 } )
147182
148183 expect ( sessionState . fileContext . knowledgeFiles ) . toEqual ( knowledgeFiles )
149- expect ( sessionState . fileContext . knowledgeFiles [ 'knowledge.md' ] ) . toBeUndefined ( )
184+ expect (
185+ sessionState . fileContext . knowledgeFiles [ 'knowledge.md' ] ,
186+ ) . toBeUndefined ( )
150187 } )
151188
152189 test ( 'sets maxAgentSteps when provided' , async ( ) => {
@@ -198,10 +235,12 @@ describe('Initial Session State', () => {
198235 } )
199236
200237 expect ( sessionState . fileContext . agentTemplates ) . toBeDefined ( )
201- expect ( sessionState . fileContext . agentTemplates [ 'custom-agent' ] ) . toBeDefined ( )
202- expect ( sessionState . fileContext . agentTemplates [ 'custom-agent' ] . displayName ) . toBe (
203- 'Custom Agent' ,
204- )
238+ expect (
239+ sessionState . fileContext . agentTemplates [ 'custom-agent' ] ,
240+ ) . toBeDefined ( )
241+ expect (
242+ sessionState . fileContext . agentTemplates [ 'custom-agent' ] . displayName ,
243+ ) . toBe ( 'Custom Agent' )
205244 } )
206245
207246 test ( 'includes custom tool definitions' , async ( ) => {
@@ -236,10 +275,12 @@ describe('Initial Session State', () => {
236275 } )
237276
238277 expect ( sessionState . fileContext . customToolDefinitions ) . toBeDefined ( )
239- expect ( sessionState . fileContext . customToolDefinitions [ 'custom_tool' ] ) . toBeDefined ( )
240- expect ( sessionState . fileContext . customToolDefinitions [ 'custom_tool' ] . description ) . toBe (
241- 'A custom tool' ,
242- )
278+ expect (
279+ sessionState . fileContext . customToolDefinitions [ 'custom_tool' ] ,
280+ ) . toBeDefined ( )
281+ expect (
282+ sessionState . fileContext . customToolDefinitions [ 'custom_tool' ] . description ,
283+ ) . toBe ( 'A custom tool' )
243284 } )
244285
245286 test ( 'populates system info correctly' , async ( ) => {
@@ -257,7 +298,9 @@ describe('Initial Session State', () => {
257298 expect ( sessionState . fileContext . systemInfo ) . toBeDefined ( )
258299 expect ( sessionState . fileContext . systemInfo . platform ) . toBe ( process . platform )
259300 expect ( sessionState . fileContext . systemInfo . shell ) . toBeDefined ( )
260- expect ( sessionState . fileContext . systemInfo . nodeVersion ) . toBe ( process . version )
301+ expect ( sessionState . fileContext . systemInfo . nodeVersion ) . toBe (
302+ process . version ,
303+ )
261304 expect ( sessionState . fileContext . systemInfo . cpus ) . toBeGreaterThan ( 0 )
262305 } )
263306
0 commit comments