2026-02-16 15:59:12 +09:00
|
|
|
import { describe, it, expect } from "bun:test"
|
2026-02-19 18:19:16 +09:00
|
|
|
import { computeLineHash } from "./hash-computation"
|
2026-02-22 03:38:04 +09:00
|
|
|
import { parseLineRef, validateLineRef, validateLineRefs } from "./validation"
|
2026-02-16 15:59:12 +09:00
|
|
|
|
|
|
|
|
describe("parseLineRef", () => {
|
2026-02-20 11:02:07 +09:00
|
|
|
it("parses valid LINE#ID reference", () => {
|
2026-02-16 15:59:12 +09:00
|
|
|
//#given
|
2026-02-20 11:02:07 +09:00
|
|
|
const ref = "42#VK"
|
2026-02-16 15:59:12 +09:00
|
|
|
|
|
|
|
|
//#when
|
|
|
|
|
const result = parseLineRef(ref)
|
|
|
|
|
|
|
|
|
|
//#then
|
2026-02-20 11:02:07 +09:00
|
|
|
expect(result).toEqual({ line: 42, hash: "VK" })
|
2026-02-16 15:59:12 +09:00
|
|
|
})
|
|
|
|
|
|
2026-02-20 11:02:07 +09:00
|
|
|
it("throws on invalid format", () => {
|
2026-02-16 15:59:12 +09:00
|
|
|
//#given
|
2026-02-20 11:02:07 +09:00
|
|
|
const ref = "42:VK"
|
2026-02-16 15:59:12 +09:00
|
|
|
|
2026-02-20 11:02:07 +09:00
|
|
|
//#when / #then
|
2026-02-24 15:35:10 +09:00
|
|
|
expect(() => parseLineRef(ref)).toThrow("{line_number}#{hash_id}")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("gives specific hint when literal text is used instead of line number", () => {
|
|
|
|
|
//#given — model sends "LINE#HK" instead of "1#HK"
|
|
|
|
|
const ref = "LINE#HK"
|
|
|
|
|
|
|
|
|
|
//#when / #then — error should mention that LINE is not a valid number
|
|
|
|
|
expect(() => parseLineRef(ref)).toThrow(/not a line number/i)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("gives specific hint for other non-numeric prefixes like POS#VK", () => {
|
|
|
|
|
//#given
|
|
|
|
|
const ref = "POS#VK"
|
|
|
|
|
|
|
|
|
|
//#when / #then
|
|
|
|
|
expect(() => parseLineRef(ref)).toThrow(/not a line number/i)
|
2026-02-16 15:59:12 +09:00
|
|
|
})
|
|
|
|
|
|
2026-02-20 11:02:07 +09:00
|
|
|
it("accepts refs copied with markers and trailing content", () => {
|
2026-02-16 15:59:12 +09:00
|
|
|
//#given
|
2026-02-24 06:01:24 +09:00
|
|
|
const ref = ">>> 42#VK|const value = 1"
|
2026-02-16 15:59:12 +09:00
|
|
|
|
2026-02-20 11:02:07 +09:00
|
|
|
//#when
|
|
|
|
|
const result = parseLineRef(ref)
|
2026-02-16 15:59:12 +09:00
|
|
|
|
2026-02-20 11:02:07 +09:00
|
|
|
//#then
|
|
|
|
|
expect(result).toEqual({ line: 42, hash: "VK" })
|
2026-02-16 15:59:12 +09:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("validateLineRef", () => {
|
2026-02-20 11:02:07 +09:00
|
|
|
it("accepts matching reference", () => {
|
2026-02-16 15:59:12 +09:00
|
|
|
//#given
|
|
|
|
|
const lines = ["function hello() {", " return 42", "}"]
|
2026-02-20 11:02:07 +09:00
|
|
|
const hash = computeLineHash(1, lines[0])
|
2026-02-16 15:59:12 +09:00
|
|
|
|
2026-02-20 11:02:07 +09:00
|
|
|
//#when / #then
|
|
|
|
|
expect(() => validateLineRef(lines, `1#${hash}`)).not.toThrow()
|
2026-02-16 15:59:12 +09:00
|
|
|
})
|
|
|
|
|
|
2026-02-20 11:02:07 +09:00
|
|
|
it("throws on mismatch and includes current hash", () => {
|
2026-02-16 15:59:12 +09:00
|
|
|
//#given
|
|
|
|
|
const lines = ["function hello() {"]
|
|
|
|
|
|
2026-02-20 11:02:07 +09:00
|
|
|
//#when / #then
|
2026-02-24 06:01:24 +09:00
|
|
|
expect(() => validateLineRef(lines, "1#ZZ")).toThrow(/>>>\s+1#[ZPMQVRWSNKTXJBYH]{2}\|/)
|
2026-02-22 03:38:04 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("shows >>> mismatch context in batched validation", () => {
|
|
|
|
|
//#given
|
|
|
|
|
const lines = ["one", "two", "three", "four"]
|
|
|
|
|
|
|
|
|
|
//#when / #then
|
|
|
|
|
expect(() => validateLineRefs(lines, ["2#ZZ"]))
|
2026-02-24 06:01:24 +09:00
|
|
|
.toThrow(/>>>\s+2#[ZPMQVRWSNKTXJBYH]{2}\|two/)
|
2026-02-16 15:59:12 +09:00
|
|
|
})
|
2026-02-24 15:35:10 +09:00
|
|
|
|
|
|
|
|
it("suggests correct line number when hash matches a file line", () => {
|
|
|
|
|
//#given — model sends LINE#XX where XX is the actual hash for line 1
|
|
|
|
|
const lines = ["function hello() {", " return 42", "}"]
|
|
|
|
|
const hash = computeLineHash(1, lines[0])
|
|
|
|
|
|
|
|
|
|
//#when / #then — error should suggest the correct reference
|
|
|
|
|
expect(() => validateLineRefs(lines, [`LINE#${hash}`])).toThrow(new RegExp(`1#${hash}`))
|
|
|
|
|
})
|
2026-02-16 15:59:12 +09:00
|
|
|
})
|