-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffLine.test.tsx
More file actions
169 lines (152 loc) · 4.78 KB
/
Copy pathDiffLine.test.tsx
File metadata and controls
169 lines (152 loc) · 4.78 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
import { render } from "ink-testing-library";
import React from "react";
import { describe, expect, it, vi } from "vitest";
import type { DisplayLine } from "../utils/formatDiff.js";
import * as highlightCodeModule from "../utils/highlightCode.js";
import { renderDiffLine } from "./DiffLine.js";
function renderLine(line: DisplayLine, isCursor = false): string {
const { lastFrame } = render(<>{renderDiffLine(line, isCursor)}</>);
return lastFrame() ?? "";
}
describe("renderDiffLine with syntax highlighting", () => {
it("highlights add line with known language", () => {
const line: DisplayLine = {
type: "add",
text: "+const x = 42;",
filePath: "src/index.ts",
};
const output = renderLine(line);
expect(output).toContain("+");
expect(output).toContain("const");
expect(output).toContain("42");
});
it("highlights delete line with known language", () => {
const line: DisplayLine = {
type: "delete",
text: "-let y = 0;",
filePath: "src/index.ts",
};
const output = renderLine(line);
expect(output).toContain("-");
expect(output).toContain("let");
});
it("highlights context line with known language", () => {
const line: DisplayLine = {
type: "context",
text: " return x;",
filePath: "src/index.ts",
};
const output = renderLine(line);
expect(output).toContain("return");
});
it("falls back for unknown file extensions", () => {
const line: DisplayLine = {
type: "add",
text: "+some data",
filePath: "data.xyz",
};
const output = renderLine(line);
expect(output).toContain("+some data");
});
it("falls back when no filePath", () => {
const line: DisplayLine = {
type: "add",
text: "+hello",
};
const output = renderLine(line);
expect(output).toContain("+hello");
});
it("applies bold when cursor is active", () => {
const line: DisplayLine = {
type: "add",
text: "+const x = 1;",
filePath: "src/app.ts",
};
const output = renderLine(line, true);
expect(output).toContain("const");
});
it("renders context line without prefixColor", () => {
const line: DisplayLine = {
type: "context",
text: " const y = 2;",
filePath: "src/app.ts",
};
const output = renderLine(line);
expect(output).toContain("const");
expect(output).toContain("2");
});
it("handles empty content after prefix", () => {
const line: DisplayLine = {
type: "add",
text: "+",
filePath: "src/app.ts",
};
const output = renderLine(line);
expect(output).toContain("+");
});
it("highlights Python code", () => {
const line: DisplayLine = {
type: "add",
text: "+def hello():",
filePath: "main.py",
};
const output = renderLine(line);
expect(output).toContain("def");
});
it("highlights JSON", () => {
const line: DisplayLine = {
type: "context",
text: ' "name": "test"',
filePath: "package.json",
};
const output = renderLine(line);
expect(output).toContain("name");
});
it("renders segments with bold and dim attributes", () => {
const line: DisplayLine = {
type: "add",
text: "+import React from 'react';",
filePath: "src/app.tsx",
};
const output = renderLine(line);
expect(output).toContain("import");
expect(output).toContain("React");
});
it("renders non-highlighted line types unchanged", () => {
const header: DisplayLine = { type: "header", text: "src/auth.ts" };
expect(renderLine(header)).toContain("src/auth.ts");
const sep: DisplayLine = { type: "separator", text: "──────" };
expect(renderLine(sep)).toContain("──────");
const trunc: DisplayLine = { type: "truncation", text: "[t] show more" };
expect(renderLine(trunc)).toContain("[t] show more");
});
it("renders segments with bold, dim, and italic attributes", () => {
const spy = vi.spyOn(highlightCodeModule, "highlightLine").mockReturnValueOnce([
{ text: "/* ", dim: true },
{ text: "important", bold: true },
{ text: "comment", italic: true },
{ text: " */", dim: true },
]);
const line: DisplayLine = {
type: "context",
text: " /* comment */",
filePath: "src/index.ts",
};
const output = renderLine(line);
expect(output).toContain("comment");
spy.mockRestore();
});
it("renders single plain segment without wrapping", () => {
const spy = vi
.spyOn(highlightCodeModule, "highlightLine")
.mockReturnValueOnce([{ text: "plain text" }]);
const line: DisplayLine = {
type: "add",
text: "+plain text",
filePath: "src/index.ts",
};
const output = renderLine(line);
expect(output).toContain("plain text");
spy.mockRestore();
});
});