@@ -52,7 +52,11 @@ describe('race condition: cache file modification during validation', () => {
5252 const { git_current_commit_hash} = await import ( '@fuzdev/fuz_util/git.js' ) ;
5353 const { hash_blake3} = await import ( '@fuzdev/fuz_util/hash_blake3.js' ) ;
5454
55- const initial_metadata = create_mock_build_cache_metadata ( { git_commit : 'abc123' } ) ;
55+ // hash_blake3 mock returns 'hash123', so config's build_cache_config_hash will be 'hash123'
56+ const initial_metadata = create_mock_build_cache_metadata ( {
57+ git_commit : 'abc123' ,
58+ build_cache_config_hash : 'hash123' ,
59+ } ) ;
5660 const modified_metadata = create_mock_build_cache_metadata ( { git_commit : 'def456' } ) ;
5761
5862 // Simulate cache file being modified during validation
@@ -72,12 +76,12 @@ describe('race condition: cache file modification during validation', () => {
7276 const config = await create_mock_config ( ) ;
7377 const log = create_mock_logger ( ) ;
7478
75- // This represents a very unlikely race condition, but system should handle gracefully
79+ // Metadata is loaded once and used throughout validation,
80+ // so later file modifications don't affect the result
7681 const result = await is_build_cache_valid ( config , log ) ;
7782
78- // Cache validation should happen with the initially loaded metadata
79- // The result depends on when the validation happens vs when file is modified
80- expect ( typeof result ) . toBe ( 'boolean' ) ;
83+ // initial metadata matches current git commit, so cache is valid
84+ expect ( result ) . toBe ( true ) ;
8185 } ) ;
8286
8387 test ( 'handles concurrent cache writes' , async ( ) => {
@@ -86,19 +90,14 @@ describe('race condition: cache file modification during validation', () => {
8690 const metadata1 = create_mock_build_cache_metadata ( { git_commit : 'commit1' } ) ;
8791 const metadata2 = create_mock_build_cache_metadata ( { git_commit : 'commit2' } ) ;
8892
89- let write_count = 0 ;
90- vi . mocked ( writeFile ) . mockImplementation ( ( ) => {
91- write_count ++ ;
92- // Simulate concurrent writes - not expected in practice but should not crash
93- return Promise . resolve ( ) ;
94- } ) ;
93+ vi . mocked ( mkdir ) . mockResolvedValue ( undefined ) ;
94+ vi . mocked ( writeFile ) . mockResolvedValue ( undefined ) ;
9595
96- // Try to save two different cache states
97- await save_build_cache_metadata ( metadata1 ) ;
98- await save_build_cache_metadata ( metadata2 ) ;
96+ // actually concurrent writes via Promise.all
97+ await Promise . all ( [ save_build_cache_metadata ( metadata1 ) , save_build_cache_metadata ( metadata2 ) ] ) ;
9998
100- // Both should complete without throwing
101- expect ( write_count ) . toBe ( 2 ) ;
99+ // both should complete without throwing
100+ expect ( writeFile ) . toHaveBeenCalledTimes ( 2 ) ;
102101 expect ( mkdir ) . toHaveBeenCalledTimes ( 2 ) ;
103102 } ) ;
104103
@@ -108,7 +107,11 @@ describe('race condition: cache file modification during validation', () => {
108107 const { git_current_commit_hash} = await import ( '@fuzdev/fuz_util/git.js' ) ;
109108 const { hash_blake3} = await import ( '@fuzdev/fuz_util/hash_blake3.js' ) ;
110109
111- const metadata = create_mock_build_cache_metadata ( { git_commit : 'abc123' } ) ;
110+ // hash_blake3 mock returns 'hash123', so config's build_cache_config_hash will be 'hash123'
111+ const metadata = create_mock_build_cache_metadata ( {
112+ git_commit : 'abc123' ,
113+ build_cache_config_hash : 'hash123' ,
114+ } ) ;
112115
113116 vi . mocked ( fs_exists ) . mockResolvedValue ( true ) ;
114117 vi . mocked ( readFile ) . mockResolvedValue ( JSON . stringify ( metadata ) ) ;
@@ -118,19 +121,15 @@ describe('race condition: cache file modification during validation', () => {
118121 const config = await create_mock_config ( ) ;
119122 const log = create_mock_logger ( ) ;
120123
121- // Simulate multiple concurrent cache validation operations
122- // In practice this shouldn't happen, but system should handle gracefully
124+ // multiple concurrent validations should all succeed
123125 const validations = await Promise . all ( [
124126 is_build_cache_valid ( config , log ) ,
125127 is_build_cache_valid ( config , log ) ,
126128 is_build_cache_valid ( config , log ) ,
127129 ] ) ;
128130
129- // All validations should complete without throwing
130- expect ( validations ) . toHaveLength ( 3 ) ;
131- validations . forEach ( ( result ) => {
132- expect ( typeof result ) . toBe ( 'boolean' ) ;
133- } ) ;
131+ // all validations should return true since metadata matches
132+ expect ( validations ) . toEqual ( [ true , true , true ] ) ;
134133 } ) ;
135134
136135 // Note: Git commit changing during build is tested at the integration level
0 commit comments