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
105 lines · 3.3 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('hunks share a block while they fit, then one opens', () => {
11 const { blocks, isTruncated } = Views.codeBlocksOf([
12 Fixtures.wideHunk(),
13 Fixtures.wideHunk(Fixtures.BIG_LINES),
14 Fixtures.wideHunk(2 * Fixtures.BIG_LINES),
15 ])
16
17 expect(isTruncated).toBe(false)
18 expect(blocks.map(block => block.hasDivider)).toEqual([false, true])
19 expect(blocks.every(Fixtures.isWithinCodeCap)).toBe(true)
20
21 expect(blocks.map(block => Fixtures.hunksOf(block.source).length)).toEqual([
22 2, 1,
23 ])
24
25 expect(Fixtures.hunksOf(blocks[1]?.source ?? '')[0]?.oldStart).toBe(
26 2 * Fixtures.BIG_LINES,
27 )
28 })
29
30 test('an oversize hunk stacks as sub-hunks that add up', () => {
31 const hunk = Fixtures.wideHunk(
32 Fixtures.BIG_LINES,
33 Limits.MAX_LINES_PER_FILE,
34 )
35
36 const body = Views.codeBlocksOf([hunk])
37 const parts = Fixtures.parsedHunksOf(body)
38
39 expect(body.isTruncated).toBe(false)
40 expect(body.blocks.length).toBeGreaterThan(2)
41 expect(body.blocks.some(block => block.hasDivider)).toBe(false)
42 expect(body.blocks.every(Fixtures.isWithinCodeCap)).toBe(true)
43 expect(parts).toHaveLength(body.blocks.length)
44 expect(parts.flatMap(part => part.lines)).toEqual([...hunk.lines])
45
46 expect(parts.reduce((sum, part) => sum + part.oldLines, 0)).toBe(
47 hunk.lines.filter(line => !line.startsWith('+')).length,
48 )
49
50 expect(parts.reduce((sum, part) => sum + part.newLines, 0)).toBe(
51 hunk.lines.filter(line => !line.startsWith('-')).length,
52 )
53
54 expect(parts[0]?.oldStart).toBe(Fixtures.BIG_LINES)
55 expect(Fixtures.areContiguous(parts)).toBe(true)
56 })
57
58 test('letters beyond the BMP are capped in UTF-16 units', () => {
59 const hunk = Fixtures.wideHunk(
60 Fixtures.BIG_LINES,
61 Limits.MAX_LINES_PER_FILE / 2,
62 Fixtures.MATHEMATICAL_BOLD_A,
63 )
64
65 const body = Views.codeBlocksOf([hunk])
66 const parts = Fixtures.parsedHunksOf(body)
67
68 expect(Fixtures.MATHEMATICAL_BOLD_A).toHaveLength(2)
69 expect(body.isTruncated).toBe(false)
70 expect(body.blocks.length).toBeGreaterThan(2)
71 expect(body.blocks.every(Fixtures.isWithinCodeCap)).toBe(true)
72 expect(parts.flatMap(part => part.lines)).toEqual([...hunk.lines])
73 expect(Fixtures.areContiguous(parts)).toBe(true)
74 })
75
76 test('a line longer than any source holds is cut, reported', () => {
77 const body = Views.codeBlocksOf([
78 {
79 oldStart: 1,
80 newStart: 1,
81 lines: [`+${'z'.repeat(2 * Views.MAX_CODE_CHARS)}`, '+b'],
82 },
83 ])
84
85 const lines = Fixtures.parsedHunksOf(body).flatMap(part => part.lines)
86
87 expect(body.isTruncated).toBe(true)
88 expect(body.blocks.every(Fixtures.isWithinCodeCap)).toBe(true)
89 expect(lines).toEqual([expect.stringMatching(/^\+z+$/), '+b'])
90 })
91
92 test("a line's controls and format characters go, tabs stay", () => {
93 const { blocks } = Views.codeBlocksOf([
94 {
95 oldStart: 1,
96 newStart: 1,
97 lines: ['-\tesc\u001b[31m', '+\tbidi\u202Eok\r', '\\ No newline'],
98 },
99 ])
100
101 expect(blocks.map(block => block.source)).toEqual([
102 '@@ -1,1 +1,1 @@\n-\tesc[31m\n+\tbidiok',
103 ])
104 })
105})