restores Helder in all its glory

This commit is contained in:
2026-09-28 11:34:49 +02:00
parent c2b38b853c
commit 1d29d0952d
280 changed files with 29205 additions and 7 deletions
@@ -0,0 +1,106 @@
import HelderCore
import Testing
@Suite struct RipgrepJSONTests {
private func matchLine(path: String = "/p/a.ts", text: String = "const balance = 2\n", line: Int = 2, start: Int = 6) -> String {
"""
{"type":"match","data":{"path":{"text":"\(path)"},"lines":{"text":"\(text.replacingOccurrences(of: "\n", with: "\\n"))"},"line_number":\(line),"absolute_offset":12,"submatches":[{"match":{"text":"balance"},"start":\(start),"end":13}]}}
"""
}
@Test("a match event yields path, line, text without its newline, and the byte offset")
func parsesMatch() {
#expect(RipgrepJSON.parseLine(matchLine()) == RipgrepMatch(path: "/p/a.ts", line: 2, text: "const balance = 2", byteOffset: 6))
}
@Test("every other event type is skipped", arguments: [
#"{"type":"begin","data":{"path":{"text":"/p/a.ts"}}}"#,
#"{"type":"end","data":{"path":{"text":"/p/a.ts"}}}"#,
#"{"type":"summary","data":{"elapsed_total":{"secs":0}}}"#,
"not json",
"",
])
func skipsOtherEvents(line: String) {
#expect(RipgrepJSON.parseLine(line) == nil)
}
@Test("base64 bytes for a non-UTF-8 path or line skip the event")
func skipsBytes() {
#expect(RipgrepJSON.parseLine(#"{"type":"match","data":{"path":{"bytes":"L3AvYQ=="},"lines":{"text":"x\n"},"line_number":1,"submatches":[]}}"#) == nil)
#expect(RipgrepJSON.parseLine(#"{"type":"match","data":{"path":{"text":"/p/a"},"lines":{"bytes":"eA=="},"line_number":1,"submatches":[]}}"#) == nil)
}
@Test("a match without submatches or a line number defaults to zero")
func defaults() {
let parsed = RipgrepJSON.parseLine(#"{"type":"match","data":{"path":{"text":"/p/a"},"lines":{"text":"x\r\n"}}}"#)
#expect(parsed == RipgrepMatch(path: "/p/a", line: 0, text: "x", byteOffset: 0))
}
@Test("paths are made relative to the root")
func relativePath() {
#expect(RipgrepJSON.relativePath("/p/src/a.ts", root: "/p") == "src/a.ts")
#expect(RipgrepJSON.relativePath("/p/src/a.ts", root: "/p/") == "src/a.ts")
#expect(RipgrepJSON.relativePath("/other/a.ts", root: "/p") == "/other/a.ts")
}
}
@Suite struct ContentGrouperTests {
@Test("matches group per file in first-seen order")
func groups() {
var grouper = ContentGrouper(root: "/p")
grouper.add(RipgrepMatch(path: "/p/b.ts", line: 1, text: "x", byteOffset: 0))
grouper.add(RipgrepMatch(path: "/p/a.ts", line: 3, text: "y", byteOffset: 0))
grouper.add(RipgrepMatch(path: "/p/b.ts", line: 7, text: "z", byteOffset: 0))
#expect(grouper.groups.map(\.path) == ["b.ts", "a.ts"])
#expect(grouper.groups[0].hits.map(\.line) == [1, 7])
}
@Test("the byte offset becomes a UTF-16 offset", arguments: [
("const balance", 6, 6),
("é balance", 3, 2),
("😀 balance", 5, 3),
])
func utf16Offset(text: String, byteOffset: Int, expected: Int) {
var grouper = ContentGrouper(root: "/p")
grouper.add(RipgrepMatch(path: "/p/a", line: 1, text: text, byteOffset: byteOffset))
#expect(grouper.groups[0].hits[0].matchOffset == expected)
}
@Test("long lines are capped and the offset clamped to what is left")
func capsLongLines() {
var grouper = ContentGrouper(root: "/p")
let text = String(repeating: "a", count: 1500)
grouper.add(RipgrepMatch(path: "/p/a", line: 1, text: text, byteOffset: 1400))
let hit = grouper.groups[0].hits[0]
#expect(hit.text.utf16.count == SearchLimits.maxLineLength)
#expect(hit.matchOffset == SearchLimits.maxLineLength)
}
@Test("a cap never splits a surrogate pair")
func capKeepsSurrogates() {
var grouper = ContentGrouper(root: "/p")
let text = String(repeating: "a", count: SearchLimits.maxLineLength - 1) + "😀 tail"
grouper.add(RipgrepMatch(path: "/p/a", line: 1, text: text, byteOffset: 0))
#expect(grouper.groups[0].hits[0].text == String(repeating: "a", count: SearchLimits.maxLineLength - 1))
}
@Test("new files beyond the file limit are dropped, known files still collect hits")
func fileLimit() {
var grouper = ContentGrouper(root: "/p")
for k in 0..<(SearchLimits.maxFiles + 5) {
grouper.add(RipgrepMatch(path: "/p/f\(k)", line: 1, text: "x", byteOffset: 0))
}
grouper.add(RipgrepMatch(path: "/p/f0", line: 2, text: "x", byteOffset: 0))
#expect(grouper.groups.count == SearchLimits.maxFiles)
#expect(grouper.groups[0].hits.count == 2)
}
@Test("hits per file stop at the per-file limit")
func hitLimit() {
var grouper = ContentGrouper(root: "/p")
for k in 0..<(SearchLimits.maxHitsPerFile + 3) {
grouper.add(RipgrepMatch(path: "/p/a", line: k, text: "x", byteOffset: 0))
}
#expect(grouper.groups[0].hits.count == SearchLimits.maxHitsPerFile)
}
}