-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathanalyze.test.ts
More file actions
302 lines (277 loc) · 9.31 KB
/
Copy pathanalyze.test.ts
File metadata and controls
302 lines (277 loc) · 9.31 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { describe, test, expect } from "bun:test"
import { parseDiffForMarkerWarnings } from "./analyze"
// Helper to create a unified diff string from lines
function makeDiff(hunks: string): string {
return `diff --git a/file.ts b/file.ts
index abc1234..def5678 100644
--- a/file.ts
+++ b/file.ts
${hunks}`
}
describe("parseDiffForMarkerWarnings", () => {
test("returns no warnings for empty diff", () => {
expect(parseDiffForMarkerWarnings("file.ts", "")).toEqual([])
expect(parseDiffForMarkerWarnings("file.ts", " \n ")).toEqual([])
})
test("added code inside added markers — no warning", () => {
const diff = makeDiff(
`@@ -10,3 +10,5 @@
const existing = true
+// altimate_change start — new feature
+const custom = true
+// altimate_change end
const more = true`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("added code without markers — warning", () => {
const diff = makeDiff(
`@@ -10,3 +10,4 @@
const existing = true
+const unmarked = true
const more = true`,
)
const warnings = parseDiffForMarkerWarnings("file.ts", diff)
expect(warnings).toHaveLength(1)
expect(warnings[0].file).toBe("file.ts")
expect(warnings[0].context).toContain("unmarked")
})
test("REGRESSION: added code inside existing (context) markers — no warning", () => {
// This is the exact bug that caused the upgrade indicator leak.
// Markers are context lines (already committed), new code is added inside.
const diff = makeDiff(
`@@ -10,4 +10,5 @@
const existing = true
// altimate_change start — existing feature
+const newCodeInsideExistingBlock = true
// altimate_change end
const more = true`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("REGRESSION: context marker end followed by added code — warning", () => {
// New code added AFTER an existing marker block should be flagged.
const diff = makeDiff(
`@@ -10,4 +10,5 @@
// altimate_change start — block A
const blockA = true
// altimate_change end
+const outsideBlock = true
const existing = true`,
)
const warnings = parseDiffForMarkerWarnings("file.ts", diff)
expect(warnings).toHaveLength(1)
expect(warnings[0].context).toContain("outsideBlock")
})
test("REGRESSION: context marker start, added code, context marker end — no warning", () => {
// Entire marker block is pre-existing (context), only the inner code is new.
const diff = makeDiff(
`@@ -8,4 +8,5 @@
// altimate_change start - yolo mode visual indicator
import { Flag } from "@/flag/flag"
// altimate_change end
+import { UpgradeIndicator } from "../../component/upgrade-indicator"
const next = true`,
)
// The import line is skipped by the "import " heuristic, so no warning
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("REGRESSION: non-import code inside existing context markers — no warning", () => {
// Verifies the fix works independently of the import heuristic
const diff = makeDiff(
`@@ -10,4 +10,5 @@
const existing = true
// altimate_change start — custom feature
+const customCode = doSomething()
// altimate_change end
const more = true`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("REGRESSION: JSX comment markers on context lines — no warning", () => {
// JSX uses {/* altimate_change start ... */} syntax
const diff = makeDiff(
`@@ -95,4 +95,5 @@
{/* altimate_change start — upgrade indicator */}
+<UpgradeIndicator />
{/* altimate_change end */}
</box>`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("multiple hunks — marker state resets at hunk boundary", () => {
// Each hunk starts fresh, marker state should NOT carry across hunks
// since different parts of the file may have different marker context.
const diff = makeDiff(
`@@ -5,3 +5,4 @@
// altimate_change start — block 1
+const inBlock = true
// altimate_change end
@@ -50,3 +51,4 @@
const existing = true
+const unmarkedInSecondHunk = true
const more = true`,
)
const warnings = parseDiffForMarkerWarnings("file.ts", diff)
expect(warnings).toHaveLength(1)
expect(warnings[0].context).toContain("unmarkedInSecondHunk")
})
test("marker state from hunk 1 does not leak into hunk 2", () => {
// If hunk 1 ends inside a marker block (start without end in context),
// hunk 2 should NOT inherit that state.
const diff = makeDiff(
`@@ -5,3 +5,4 @@
// altimate_change start — block 1
+const inBlock = true
const moreInBlock = true
@@ -80,3 +81,4 @@
const unrelated = true
+const shouldBeWarned = true
const end = true`,
)
const warnings = parseDiffForMarkerWarnings("file.ts", diff)
expect(warnings).toHaveLength(1)
expect(warnings[0].context).toContain("shouldBeWarned")
})
test("import lines are skipped even without markers", () => {
const diff = makeDiff(
`@@ -1,3 +1,4 @@
import { existing } from "./existing"
+import { NewThing } from "./new-thing"
const x = 1`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("export lines are skipped even without markers", () => {
const diff = makeDiff(
`@@ -1,3 +1,4 @@
const x = 1
+export { x }
const y = 2`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("comment-only lines are skipped (not TODOs)", () => {
const diff = makeDiff(
`@@ -1,3 +1,4 @@
const x = 1
+// this is a harmless comment
const y = 2`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("TODO comments are NOT skipped", () => {
const diff = makeDiff(
`@@ -1,3 +1,4 @@
const x = 1
+// TODO: implement custom feature
const y = 2`,
)
const warnings = parseDiffForMarkerWarnings("file.ts", diff)
expect(warnings).toHaveLength(1)
})
test("empty added lines are skipped", () => {
const diff = makeDiff(
`@@ -1,3 +1,4 @@
const x = 1
+
const y = 2`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("deleted lines don't affect marker state or line numbers", () => {
const diff = makeDiff(
`@@ -5,5 +5,5 @@
// altimate_change start — feature
-const oldCode = true
+const newCode = true
// altimate_change end
const next = true`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("line number in warning matches diff hunk position", () => {
const diff = makeDiff(
`@@ -42,3 +42,4 @@
const existing = true
+const unmarked = true
const more = true`,
)
const warnings = parseDiffForMarkerWarnings("file.ts", diff)
expect(warnings).toHaveLength(1)
expect(warnings[0].line).toBe(43)
})
test("context truncated to 80 chars in warning", () => {
const longLine = "x".repeat(120)
const diff = makeDiff(
`@@ -1,3 +1,4 @@
const a = 1
+const ${longLine} = true
const b = 2`,
)
const warnings = parseDiffForMarkerWarnings("file.ts", diff)
expect(warnings).toHaveLength(1)
expect(warnings[0].context.length).toBeLessThanOrEqual(80)
})
test("only first unmarked line is reported per file", () => {
const diff = makeDiff(
`@@ -1,3 +1,5 @@
const a = 1
+const first = true
+const second = true
const b = 2`,
)
const warnings = parseDiffForMarkerWarnings("file.ts", diff)
expect(warnings).toHaveLength(1)
expect(warnings[0].context).toContain("first")
})
test("upstream_fix: tagged markers are recognized as valid markers", () => {
const diff = makeDiff(
`@@ -50,4 +50,6 @@
const existing = true
+// altimate_change start — upstream_fix: days/hours were swapped
+const days = Math.floor(input / 86400000)
+// altimate_change end
const more = true`,
)
expect(parseDiffForMarkerWarnings("file.ts", diff)).toEqual([])
})
test("real-world scenario: upgrade indicator in footer.tsx", () => {
// Simulates the exact diff that leaked: UpgradeIndicator added to
// session footer without markers, adjacent to existing yolo marker block.
const diff = makeDiff(
`@@ -8,4 +8,6 @@
// altimate_change start - yolo mode visual indicator
import { Flag } from "@/flag/flag"
// altimate_change end
+// altimate_change start — upgrade indicator import
+import { UpgradeIndicator } from "../../component/upgrade-indicator"
+// altimate_change end
@@ -96,4 +98,6 @@
</Switch>
+ {/* altimate_change start — upgrade indicator in session footer */}
+ <UpgradeIndicator />
+ {/* altimate_change end */}
</box>`,
)
expect(parseDiffForMarkerWarnings("footer.tsx", diff)).toEqual([])
})
test("real-world scenario: unmarked upgrade indicator would be caught", () => {
// Same scenario but WITHOUT markers — should flag
const diff = makeDiff(
`@@ -8,4 +8,5 @@
// altimate_change start - yolo mode visual indicator
import { Flag } from "@/flag/flag"
// altimate_change end
+import { UpgradeIndicator } from "../../component/upgrade-indicator"
@@ -96,4 +97,5 @@
</Switch>
+ <UpgradeIndicator />
</box>`,
)
const warnings = parseDiffForMarkerWarnings("footer.tsx", diff)
// import is skipped by heuristic, but <UpgradeIndicator /> is flagged
expect(warnings).toHaveLength(1)
expect(warnings[0].context).toContain("UpgradeIndicator")
})
})