Files
orca/src/shared/node-bounded-file-reader-sync.test.ts
T
Brennan Benson 91efcc4df0 Add a per-host running terminal limit setting (#31396)
* feat: configure the running terminal limit for each host

* fix: bound terminal policy saves through fresh confirmation

* Fix truncated zero-size system file reads

* Load actual filesystem fixtures once per test module

* Preserve slept terminal ownership through capacity retries

* Fix terminal limit CI quality checks

* Correct slept terminal resume test types

* Move the slept terminal Resume fix to its own pull request

The Resume correction fixes a bug that already exists at the default limit,
so it now lives in #35401 and this change only adds the terminal limit
setting.

* Keep terminal limits effective when saved settings cannot be read

* Require a numeric edit before repairing terminal limits

* Derive terminal default from startup host capacity

* Apply terminal policy reads when validated results settle

* Align terminal limit tests with static analysis gates

* Inject daemon startup errors at the publication boundary

* Format terminal capacity using the app language
2026-10-29 12:18:02 -07:00

90 lines
3.0 KiB
TypeScript

import { closeSync, ftruncateSync, mkdtempSync, openSync, rmSync, writeFileSync } from 'node:fs'
import * as fs from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { NodeFileReadTooLargeError, readNodeFileSyncWithinLimit } from './node-bounded-file-reader'
vi.mock('node:fs', async (importOriginal) => ({
...(await importOriginal<typeof fs>())
}))
const tempDirectories: string[] = []
function createTempFile(content: string): string {
const directory = mkdtempSync(join(tmpdir(), 'orca-bounded-sync-read-'))
tempDirectories.push(directory)
const path = join(directory, 'input')
writeFileSync(path, content)
return path
}
afterEach(() => {
vi.restoreAllMocks()
for (const directory of tempDirectories.splice(0)) {
rmSync(directory, { recursive: true })
}
})
describe('readNodeFileSyncWithinLimit', () => {
it('returns stable bytes without changing them', () => {
const path = createTempFile('stable 🐋 bytes')
expect(readNodeFileSyncWithinLimit(path, 1024).buffer.toString('utf8')).toBe('stable 🐋 bytes')
})
it('rejects an oversized sparse file before allocating its declared size', () => {
const path = createTempFile('')
const descriptor = openSync(path, 'r+')
ftruncateSync(descriptor, 1025)
closeSync(descriptor)
expect(() => readNodeFileSyncWithinLimit(path, 1024)).toThrow(NodeFileReadTooLargeError)
})
it('reads the full zero-size sysctl value when later reads return EOF', () => {
const path = createTempFile('4096\n')
const originalStat = fs.fstatSync
vi.spyOn(fs, 'fstatSync').mockImplementation((...args) =>
Object.assign(originalStat(...args), { size: 0 })
)
const originalRead = fs.readSync
const read = vi
.spyOn(fs, 'readSync')
.mockImplementation((...args) => (read.mock.calls.length === 1 ? originalRead(...args) : 0))
expect(readNodeFileSyncWithinLimit(path, 1024).buffer.toString('utf8')).toBe('4096\n')
})
})
describe('regular file growth evidence', () => {
it('refuses growth beyond the byte limit during a read', () => {
const path = createTempFile('abcd')
const original = fs.readSync
const spy = vi.spyOn(fs, 'readSync').mockImplementation((...args) => {
const bytes = original(...args)
if (spy.mock.calls.length === 1) {
fs.appendFileSync(path, 'x'.repeat(1024))
}
return bytes
})
expect(() => readNodeFileSyncWithinLimit(path, 1024, { regularFileOnly: true })).toThrow(
NodeFileReadTooLargeError
)
})
it('validates the opened descriptor when the path evidence changes', () => {
const path = createTempFile('abcd')
const original = fs.statSync
vi.spyOn(fs, 'statSync').mockImplementation((...args) => {
const evidence = original(...args)
fs.unlinkSync(path)
fs.mkdirSync(path)
return evidence
})
expect(() => readNodeFileSyncWithinLimit(path, 1024, { regularFileOnly: true })).toThrow(
'Expected a regular file'
)
})
})