Skip to content

Commit 541ea41

Browse files
committed
test(hooks): verify shared preimage reuse in post-read handler
1 parent 30f8840 commit 541ea41

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

‎tests/hooks_read_universal_filetype_readfilesync_guard.test.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as fs from 'node:fs'
2323

2424
import type { HookEvent } from '../src/hook_registry.js'
2525
import { preReadHandler } from '../src/hooks_read.js'
26+
import { postReadHandler } from '../src/hooks_read_post.js'
2627
import { normalizePath } from '../src/paths.js'
2728
import { clearModuleCaches } from '../src/reset.js'
2829
import { makeHookEvent } from './helpers/hook-event.js'
@@ -67,6 +68,16 @@ function makeTmpXml(totalBytes: number): string {
6768
return p
6869
}
6970

71+
function makeTmpSource(lineCount: number): string {
72+
const content = Array.from({ length: lineCount }, (_, i) =>
73+
`// synthetic source line ${i + 1}: enough content to clear the hint savings floor`,
74+
).join('\n') + '\n'
75+
const p = path.join(os.tmpdir(), `tg-read-post-${process.pid}-${Math.random().toString(36).slice(2)}.ts`)
76+
fs.writeFileSync(p, content)
77+
tmpFiles.push(p)
78+
return p
79+
}
80+
7081
describe('universal file-type handler readFileSync guard (regression)', () => {
7182
beforeEach(() => {
7283
clearModuleCaches()
@@ -110,7 +121,24 @@ describe('universal file-type handler readFileSync guard (regression)', () => {
110121
const output = preReadHandler(readEventWithRange(p, 1, 50))
111122

112123
expect(output.hookType).not.toBe('deny')
113-
// One requested-window estimate plus one full preimage; the line-count nudge must not reopen it.
114-
expect(mockState.callCount).toBe(2)
124+
// The type scan's bounded full preimage is shared with the line-count nudge.
125+
expect(mockState.callCount).toBe(1)
126+
})
127+
128+
it('reuses the source preimage for snapshot and structural-navigation checks after a Read', () => {
129+
const p = makeTmpSource(250)
130+
mockState.watchedPath = normalizePath(p)
131+
132+
postReadHandler(makeHookEvent({
133+
eventName: 'post_tool_use',
134+
toolName: 'Read',
135+
toolInput: { file_path: p },
136+
sessionId: `post-read-preimage-${process.pid}-${Math.random().toString(36).slice(2)}`,
137+
raw: { tool_response: 'source read result' },
138+
}))
139+
140+
// The fold scan and served-window reconciliation each read independently; the snapshot and
141+
// structural-navigation gates share their single preimage.
142+
expect(mockState.callCount).toBe(3)
115143
})
116144
})

0 commit comments

Comments
 (0)