Skip to content

Commit 1474fee

Browse files
bartvenemanclaude
andauthored
fix(chunkify): correct trailing chunk boundary condition (#86)
The condition `offset !== text.length - 1` incorrectly skipped the trailing uncovered chunk when exactly one byte remained, silently dropping the last byte. Changed to `offset < text.length`. https://claude.ai/code/session_01CyibEZDpRhyvg4fw3edy1Z Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2a325c2 commit 1474fee

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎src/lib/chunkify.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,36 @@ test('creates a single chunk when none is covered', () => {
103103
],
104104
} satisfies ChunkedCoverage)
105105
})
106+
107+
test('includes a trailing uncovered chunk when the last byte is not covered', () => {
108+
// text length = 4; range covers first 3 bytes, leaving the last byte uncovered
109+
let coverage = {
110+
text: 'abcd',
111+
ranges: [{ start: 0, end: 3 }],
112+
url: 'https://example.com',
113+
}
114+
let result = chunkify(coverage)
115+
delete coverage.ranges
116+
expect(result).toEqual({
117+
...coverage,
118+
chunks: [
119+
{ start_offset: 0, end_offset: 3, is_covered: true },
120+
{ start_offset: 3, end_offset: 4, is_covered: false },
121+
],
122+
} satisfies ChunkedCoverage)
123+
})
124+
125+
test('does not emit a spurious empty chunk when the last byte is covered', () => {
126+
// range covers the full text — no trailing chunk should appear
127+
let coverage = {
128+
text: 'abcd',
129+
ranges: [{ start: 0, end: 4 }],
130+
url: 'https://example.com',
131+
}
132+
let result = chunkify(coverage)
133+
delete coverage.ranges
134+
expect(result).toEqual({
135+
...coverage,
136+
chunks: [{ start_offset: 0, end_offset: 4, is_covered: true }],
137+
} satisfies ChunkedCoverage)
138+
})

‎src/lib/chunkify.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function chunkify(stylesheet: Coverage): ChunkedCoverage {
7878
}
7979

8080
// fill up last chunk if necessary:
81-
if (offset !== stylesheet.text.length - 1) {
81+
if (offset < stylesheet.text.length) {
8282
chunks.push({
8383
start_offset: offset,
8484
end_offset: stylesheet.text.length,

0 commit comments

Comments
 (0)