1- import { afterEach , describe , expect , it } from 'vitest' ;
1+ import { afterEach , describe , expect , it , vi } from 'vitest' ;
22import { execFileSync , spawnSync } from 'node:child_process' ;
33import { mkdirSync , mkdtempSync , readFileSync , rmSync , writeFileSync } from 'node:fs' ;
44import { tmpdir } from 'node:os' ;
@@ -12,6 +12,11 @@ import {
1212} from '../workflows/shared/squad-install-verifier.mjs' ;
1313import { createFirstInstallFixture } from './helpers/gh-aw-install-fixture.js' ;
1414
15+ vi . mock ( 'node:child_process' , async ( importOriginal ) => {
16+ const actual = await importOriginal < typeof import ( 'node:child_process' ) > ( ) ;
17+ return { ...actual , spawnSync : vi . fn ( actual . spawnSync ) } ;
18+ } ) ;
19+
1520const revision = 'a' . repeat ( 40 ) ;
1621const roots : string [ ] = [ ] ;
1722const verifier = resolve ( 'workflows/shared/squad-install-verifier.mjs' ) ;
@@ -49,10 +54,52 @@ function stagedPaths(root: string): string[] {
4954}
5055
5156afterEach ( ( ) => {
57+ vi . clearAllMocks ( ) ;
5258 for ( const root of roots . splice ( 0 ) ) rmSync ( root , { recursive : true , force : true } ) ;
5359} ) ;
5460
5561describe ( 'gh-aw: verified ownership staging under consumer ignore rules' , ( ) => {
62+ it ( 'bounds staged reads to required paths in a large unrelated consumer tree' , ( ) => {
63+ const root = consumer ( ) ;
64+ const unrelated = 'consumer-data' ;
65+ for ( let index = 0 ; index < 20_000 ; index += 1 ) {
66+ write ( root , `${ unrelated } /ordinary-file-${ index . toString ( ) . padStart ( 5 , '0' ) } .txt` , 'consumer content\n' ) ;
67+ }
68+ git ( root , 'add' , '--' , unrelated ) ;
69+ git ( root , '-c' , 'user.name=Test' , '-c' , 'user.email=test@example.invalid' ,
70+ '-c' , 'core.hooksPath=/dev/null' , 'commit' , '--quiet' , '-m' , 'large consumer baseline' ) ;
71+ write ( root , `${ unrelated } /ordinary-file-00000.txt` , 'unstaged consumer edit\n' ) ;
72+ write ( root , `${ unrelated } /untracked.txt` , 'untracked consumer content\n' ) ;
73+ const before = stagedPaths ( root ) ;
74+ const tree = git ( root , 'write-tree' ) . trim ( ) ;
75+ const unbounded = spawnSync ( 'git' , [ 'ls-tree' , '-r' , '-z' , tree ] , { cwd : root } ) ;
76+ expect ( unbounded . error ) . toMatchObject ( { code : 'ENOBUFS' } ) ;
77+ const manifest = JSON . parse ( readFileSync ( join ( root , CONTRACT_DESTINATION ) , 'utf8' ) ) ;
78+ const required = [
79+ CONTRACT_DESTINATION ,
80+ OWNERSHIP_DESTINATION ,
81+ ...manifest . workflows . flatMap ( ( entry : { destination : string ; lock : string } ) =>
82+ [ entry . destination , entry . lock ] ) ,
83+ ...manifest . shared_runtime . flatMap ( ( entry : { package_destination : string ; destination : string } ) =>
84+ [ entry . package_destination , entry . destination ] ) ,
85+ ...manifest . skills . map ( ( entry : { destination : string } ) => entry . destination ) ,
86+ ] ;
87+ const spawn = vi . mocked ( spawnSync ) ;
88+ spawn . mockClear ( ) ;
89+ expect ( verifyStagedInstall ( root , { expectedRevision : revision , stageOwnership : true } ) . failures ) . toEqual ( [ ] ) ;
90+ const queries = spawn . mock . calls . filter ( ( [ command , args ] ) => command === 'git' && args ?. [ 0 ] === 'ls-tree' ) ;
91+ expect ( queries ) . toHaveLength ( 1 ) ;
92+ expect ( queries [ 0 ] [ 1 ] ) . toEqual ( [ 'ls-tree' , '-z' , expect . any ( String ) , '--' , ...new Set ( required ) ] ) ;
93+ const unrelatedObject = git ( root , 'rev-parse' , `HEAD:${ unrelated } /ordinary-file-00000.txt` ) . trim ( ) ;
94+ const blobReads = spawn . mock . calls . filter ( ( [ command , args ] ) => command === 'git' && args ?. [ 0 ] === 'cat-file' ) ;
95+ expect ( blobReads ) . toHaveLength ( new Set ( required ) . size ) ;
96+ expect ( blobReads . some ( ( [ , args ] ) => args ?. includes ( unrelatedObject ) ) ) . toBe ( false ) ;
97+ expect ( stagedPaths ( root ) ) . toEqual ( [ ...before , OWNERSHIP_DESTINATION ] . sort ( ) ) ;
98+ expect ( git ( root , 'diff' , '--cached' , '--name-only' ) . trim ( ) ) . toBe ( OWNERSHIP_DESTINATION ) ;
99+ expect ( git ( root , 'diff' , '--name-only' ) . trim ( ) ) . toBe ( `${ unrelated } /ordinary-file-00000.txt` ) ;
100+ expect ( readFileSync ( join ( root , unrelated , 'untracked.txt' ) , 'utf8' ) ) . toBe ( 'untracked consumer content\n' ) ;
101+ } , 30_000 ) ;
102+
56103 it ( 'reproduces the silent omission, then force-stages only the exact required JSON' , ( ) => {
57104 const root = consumer ( ) ;
58105 expect ( verifyInstall ( root , { expectedRevision : revision } ) . failures ) . toEqual ( [ ] ) ;
@@ -97,6 +144,13 @@ describe('gh-aw: verified ownership staging under consumer ignore rules', () =>
97144 expect ( stagedPaths ( root ) ) . not . toContain ( OWNERSHIP_DESTINATION ) ;
98145 } ) ;
99146
147+ it ( 'fails closed when the verified index cannot be snapshotted' , ( ) => {
148+ const root = consumer ( ) ;
149+ expect ( verifyStagedInstall ( root , { stageOwnership : true } ) . failures ) . toEqual ( [ ] ) ;
150+ write ( root , '.git/index.lock' , 'locked\n' ) ;
151+ expect ( verifyStagedInstall ( root ) . failures . join ( '\n' ) ) . toMatch ( / g i t f a i l e d : [ \s \S ] * i n d e x \. l o c k / ) ;
152+ } ) ;
153+
100154 it . each ( [
101155 CONTRACT_DESTINATION ,
102156 '.github/workflows/squad.md' ,
@@ -136,4 +190,16 @@ describe('gh-aw: verified ownership staging under consumer ignore rules', () =>
136190 expect ( verifyStagedInstall ( root ) . failures . join ( '\n' ) )
137191 . toContain ( `Required staged path is not a regular file: ${ path } ` ) ;
138192 } ) ;
193+
194+ it ( 'rejects a required path replaced by an index directory without reading its descendants' , ( ) => {
195+ const root = consumer ( ) ;
196+ expect ( verifyStagedInstall ( root , { stageOwnership : true } ) . failures ) . toEqual ( [ ] ) ;
197+ const path = '.github/workflows/squad.md' ;
198+ const object = git ( root , 'rev-parse' , `:${ path } ` ) . trim ( ) ;
199+ git ( root , 'update-index' , '--force-remove' , '--' , path ) ;
200+ git ( root , 'update-index' , '--add' , '--cacheinfo' , `100644,${ object } ,${ path } /unexpected.txt` ) ;
201+ expect ( verifyInstall ( root ) . failures ) . toEqual ( [ ] ) ;
202+ expect ( verifyStagedInstall ( root ) . failures . join ( '\n' ) )
203+ . toContain ( `Required staged path is not a regular file: ${ path } ` ) ;
204+ } ) ;
139205} ) ;
0 commit comments