spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / tests / views / detail / code-blocks-of.test.ts
132 lines · 4.0 KBRaw
1import { describe, expect, test, tier } from 'claude-code/testing'
2
3import Limits from '../../../hooks/limits'
4import Views from '../../../hooks/views'
5import Fixtures from '../../fixtures'
6
7tier('builtin')
8
9describe('code-blocks-of', () => {
10 test('each hunk is a source of its own, nothing between them', () => {
11 const { sources, isTruncated } = Views.codeBlocksOf(
12 [
13 Fixtures.wideHunk(),
14 Fixtures.wideHunk(Fixtures.BIG_LINES),
15 Fixtures.wideHunk(2 * Fixtures.BIG_LINES),
16 ],
17 Views.BODY_BUDGET,
18 )
19
20 expect(isTruncated).toBe(false)
21 expect(sources).toHaveLength(3)
22 expect(sources.every(Fixtures.isWithinCodeCap)).toBe(true)
23
24 expect(sources.map(source => Fixtures.hunksOf(source).length)).toEqual([
25 1, 1, 1,
26 ])
27
28 expect(Fixtures.hunksOf(sources[2] ?? '')[0]?.oldStart).toBe(
29 2 * Fixtures.BIG_LINES,
30 )
31 })
32
33 test('an oversize hunk stacks as sub-hunks that add up', () => {
34 const hunk = Fixtures.wideHunk(
35 Fixtures.BIG_LINES,
36 Limits.MAX_LINES_PER_FILE,
37 )
38
39 const body = Views.codeBlocksOf([hunk], Views.BODY_BUDGET)
40 const parts = Fixtures.parsedHunksOf(body)
41
42 expect(body.isTruncated).toBe(false)
43 expect(body.sources.length).toBeGreaterThan(2)
44 expect(body.sources.every(Fixtures.isWithinCodeCap)).toBe(true)
45 expect(parts).toHaveLength(body.sources.length)
46 expect(parts.flatMap(part => part.lines)).toEqual([...hunk.lines])
47
48 expect(parts.reduce((sum, part) => sum + part.oldLines, 0)).toBe(
49 hunk.lines.filter(line => !line.startsWith('+')).length,
50 )
51
52 expect(parts.reduce((sum, part) => sum + part.newLines, 0)).toBe(
53 hunk.lines.filter(line => !line.startsWith('-')).length,
54 )
55
56 expect(parts[0]?.oldStart).toBe(Fixtures.BIG_LINES)
57 expect(Fixtures.areContiguous(parts)).toBe(true)
58 })
59
60 test('letters beyond the BMP are capped in UTF-16 units', () => {
61 const hunk = Fixtures.wideHunk(
62 Fixtures.BIG_LINES,
63 Limits.MAX_LINES_PER_FILE / 2,
64 Fixtures.MATHEMATICAL_BOLD_A,
65 )
66
67 const body = Views.codeBlocksOf([hunk], Views.BODY_BUDGET)
68 const parts = Fixtures.parsedHunksOf(body)
69
70 expect(Fixtures.MATHEMATICAL_BOLD_A).toHaveLength(2)
71 expect(body.isTruncated).toBe(false)
72 expect(body.sources.length).toBeGreaterThan(2)
73 expect(body.sources.every(Fixtures.isWithinCodeCap)).toBe(true)
74 expect(parts.flatMap(part => part.lines)).toEqual([...hunk.lines])
75 expect(Fixtures.areContiguous(parts)).toBe(true)
76 })
77
78 test('a line longer than any source holds is cut, reported', () => {
79 const body = Views.codeBlocksOf(
80 [
81 {
82 oldStart: 1,
83 newStart: 1,
84 lines: [`+${'z'.repeat(2 * Views.MAX_CODE_CHARS)}`, '+b'],
85 },
86 ],
87 Views.BODY_BUDGET,
88 )
89
90 const lines = Fixtures.parsedHunksOf(body).flatMap(part => part.lines)
91
92 expect(body.isTruncated).toBe(true)
93 expect(body.sources.every(Fixtures.isWithinCodeCap)).toBe(true)
94 expect(lines).toEqual([expect.stringMatching(/^\+z+$/), '+b'])
95 })
96
97 test('the room runs down by what is drawn, and out at the budget', () => {
98 const hunks = [Fixtures.wideHunk(), Fixtures.wideHunk(Fixtures.BIG_LINES)]
99 const whole = Views.codeBlocksOf(hunks, Views.BODY_BUDGET)
100
101 const starved = Views.codeBlocksOf(hunks, {
102 chars: Views.BODY_BUDGET.chars,
103 nodes: 1,
104 })
105
106 expect(whole.room.nodes).toBe(Views.BODY_BUDGET.nodes - 2)
107
108 expect(whole.room.chars).toBe(
109 Views.BODY_BUDGET.chars -
110 whole.sources.reduce((sum, source) => sum + source.length, 0),
111 )
112
113 expect(starved.sources).toHaveLength(1)
114 expect(starved.isTruncated).toBe(true)
115 expect(starved.room.nodes).toBe(0)
116 })
117
118 test("a line's controls and format characters go, tabs stay", () => {
119 const { sources } = Views.codeBlocksOf(
120 [
121 {
122 oldStart: 1,
123 newStart: 1,
124 lines: ['-\tesc\u001b[31m', '+\tbidi\u202Eok\r', '\\ No newline'],
125 },
126 ],
127 Views.BODY_BUDGET,
128 )
129
130 expect(sources).toEqual(['@@ -1,1 +1,1 @@\n-\tesc[31m\n+\tbidiok'])
131 })
132})