forked from getagentseal/codeburn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs-utils.test.ts
More file actions
172 lines (151 loc) · 6.26 KB
/
Copy pathfs-utils.test.ts
File metadata and controls
172 lines (151 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { describe, it, expect, afterEach, vi } from 'vitest'
import { mkdtemp, writeFile, rm } from 'fs/promises'
import { tmpdir } from 'os'
import { join } from 'path'
import {
MAX_SESSION_FILE_BYTES,
readSessionFile,
readSessionLines,
} from '../src/fs-utils.js'
describe('readSessionFile', () => {
const tmpDirs: string[] = []
afterEach(async () => {
delete process.env.CODEBURN_VERBOSE
while (tmpDirs.length > 0) {
const d = tmpDirs.pop()
if (d) await rm(d, { recursive: true, force: true })
}
})
async function tmpPath(content: string | Buffer): Promise<string> {
const base = await mkdtemp(join(tmpdir(), 'codeburn-fs-'))
tmpDirs.push(base)
const p = join(base, 'x.jsonl')
await writeFile(p, content)
return p
}
it('returns content for small files via readFile fast path', async () => {
const p = await tmpPath('hello\nworld\n')
expect(await readSessionFile(p)).toBe('hello\nworld\n')
})
it('returns content for large files under the full-file cap', async () => {
const size = 8 * 1024 * 1024
const p = await tmpPath(Buffer.alloc(size, 'a'))
const got = await readSessionFile(p)
expect(got).not.toBeNull()
expect(got!.length).toBe(size)
})
it('returns null and skips files over the cap', async () => {
const p = await tmpPath(Buffer.alloc(MAX_SESSION_FILE_BYTES + 1, 'b'))
expect(await readSessionFile(p)).toBeNull()
})
it('emits stderr warning under CODEBURN_VERBOSE=1 for skipped file', async () => {
process.env.CODEBURN_VERBOSE = '1'
const p = await tmpPath(Buffer.alloc(MAX_SESSION_FILE_BYTES + 1, 'c'))
const spy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true)
await readSessionFile(p)
expect(spy).toHaveBeenCalled()
const msg = (spy.mock.calls[0][0] as string)
expect(msg).toContain('codeburn')
expect(msg).toContain('oversize')
spy.mockRestore()
})
it('returns null on stat failure without throwing', async () => {
expect(await readSessionFile('/nonexistent/path/x.jsonl')).toBeNull()
})
})
describe('readSessionLines', () => {
const tmpDirs: string[] = []
afterEach(async () => {
while (tmpDirs.length > 0) {
const d = tmpDirs.pop()
if (d) await rm(d, { recursive: true, force: true })
}
})
async function tmpPath(content: string): Promise<string> {
const base = await mkdtemp(join(tmpdir(), 'codeburn-lines-'))
tmpDirs.push(base)
const p = join(base, 'session.jsonl')
await writeFile(p, content)
return p
}
it('yields all lines from a file', async () => {
const p = await tmpPath('line1\nline2\nline3\n')
const lines: string[] = []
for await (const line of readSessionLines(p)) lines.push(line)
expect(lines).toEqual(['line1', 'line2', 'line3'])
})
it('skips old large lines before materializing the full line', async () => {
const oldLine = `{"type":"assistant","timestamp":"2026-01-01T00:00:00Z","payload":"${'x'.repeat(100_000)}"}`
const newLine = '{"type":"assistant","timestamp":"2026-05-01T00:00:00Z"}'
const p = await tmpPath(`${oldLine}\n${newLine}\n`)
const lines: string[] = []
for await (const line of readSessionLines(p, head => head.includes('2026-01-01'))) {
lines.push(line)
}
expect(lines).toEqual([newLine])
})
it('yields large lines as Buffers when requested', async () => {
const largeLine = `{"type":"assistant","timestamp":"2026-05-01T00:00:00Z","payload":"${'x'.repeat(100_000)}"}`
const p = await tmpPath(`${largeLine}\nsmall\n`)
const lines: Array<string | Buffer> = []
for await (const line of readSessionLines(p, undefined, { largeLineAsBuffer: true })) {
lines.push(line)
}
expect(Buffer.isBuffer(lines[0])).toBe(true)
expect(lines[1]).toBe('small')
})
it('does not leak file descriptors when generator is abandoned early', async () => {
const content = Array.from({ length: 1000 }, (_, i) => `line-${i}`).join('\n')
const p = await tmpPath(content)
const gen = readSessionLines(p)
await gen.next()
await gen.return(undefined)
})
it('reads from startByteOffset, yielding only lines after the offset', async () => {
const content = 'line1\nline2\nline3\n'
const p = await tmpPath(content)
const offset = Buffer.byteLength('line1\n')
const lines: string[] = []
for await (const line of readSessionLines(p, undefined, { startByteOffset: offset })) {
lines.push(line)
}
expect(lines).toEqual(['line2', 'line3'])
})
it('byteOffsetTracker tracks position after last complete newline', async () => {
const content = 'aaa\nbbb\nccc\n'
const p = await tmpPath(content)
const tracker = { lastCompleteLineOffset: 0 }
const lines: string[] = []
for await (const line of readSessionLines(p, undefined, { byteOffsetTracker: tracker })) {
lines.push(line)
}
expect(lines).toEqual(['aaa', 'bbb', 'ccc'])
expect(tracker.lastCompleteLineOffset).toBe(Buffer.byteLength(content))
})
it('byteOffsetTracker accounts for startByteOffset', async () => {
const content = 'line1\nline2\nline3\n'
const p = await tmpPath(content)
const offset = Buffer.byteLength('line1\n')
const tracker = { lastCompleteLineOffset: 0 }
for await (const _line of readSessionLines(p, undefined, { startByteOffset: offset, byteOffsetTracker: tracker })) {}
expect(tracker.lastCompleteLineOffset).toBe(Buffer.byteLength(content))
})
it('byteOffsetTracker excludes trailing partial line (no final newline)', async () => {
const content = 'line1\nline2\npartial'
const p = await tmpPath(content)
const tracker = { lastCompleteLineOffset: 0 }
for await (const _line of readSessionLines(p, undefined, { byteOffsetTracker: tracker })) {}
expect(tracker.lastCompleteLineOffset).toBe(Buffer.byteLength('line1\nline2\n'))
})
it('byteOffsetTracker updates for skipped lines too', async () => {
const content = 'skip-me\nkeep-me\n'
const p = await tmpPath(content)
const tracker = { lastCompleteLineOffset: 0 }
const lines: string[] = []
for await (const line of readSessionLines(p, head => head.includes('skip-me'), { byteOffsetTracker: tracker })) {
lines.push(line)
}
expect(lines).toEqual(['keep-me'])
expect(tracker.lastCompleteLineOffset).toBe(Buffer.byteLength(content))
})
})