2026-02-16 15:59:12 +09:00
|
|
|
import { describe, it, expect } from "bun:test"
|
|
|
|
|
import { computeLineHash, formatHashLine, formatHashLines } from "./hash-computation"
|
|
|
|
|
|
|
|
|
|
describe("computeLineHash", () => {
|
2026-02-20 11:07:42 +09:00
|
|
|
it("returns deterministic 2-char CID hash", () => {
|
2026-02-16 15:59:12 +09:00
|
|
|
//#given
|
|
|
|
|
const content = "function hello() {"
|
|
|
|
|
|
|
|
|
|
//#when
|
2026-02-20 11:07:42 +09:00
|
|
|
const hash1 = computeLineHash(1, content)
|
|
|
|
|
const hash2 = computeLineHash(999, content)
|
2026-02-16 15:59:12 +09:00
|
|
|
|
|
|
|
|
//#then
|
|
|
|
|
expect(hash1).toBe(hash2)
|
2026-02-20 11:07:42 +09:00
|
|
|
expect(hash1).toMatch(/^[ZPMQVRWSNKTXJBYH]{2}$/)
|
2026-02-16 15:59:12 +09:00
|
|
|
})
|
|
|
|
|
|
2026-02-20 11:07:42 +09:00
|
|
|
it("ignores whitespace differences", () => {
|
2026-02-16 15:59:12 +09:00
|
|
|
//#given
|
|
|
|
|
const content1 = "function hello() {"
|
|
|
|
|
const content2 = " function hello() { "
|
|
|
|
|
|
|
|
|
|
//#when
|
2026-02-20 11:07:42 +09:00
|
|
|
const hash1 = computeLineHash(1, content1)
|
|
|
|
|
const hash2 = computeLineHash(1, content2)
|
2026-02-16 15:59:12 +09:00
|
|
|
|
|
|
|
|
//#then
|
|
|
|
|
expect(hash1).toBe(hash2)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("formatHashLine", () => {
|
2026-02-20 11:07:42 +09:00
|
|
|
it("formats single line as LINE#ID:content", () => {
|
2026-02-16 15:59:12 +09:00
|
|
|
//#given
|
|
|
|
|
const lineNumber = 42
|
|
|
|
|
const content = "const x = 42"
|
|
|
|
|
|
|
|
|
|
//#when
|
|
|
|
|
const result = formatHashLine(lineNumber, content)
|
|
|
|
|
|
|
|
|
|
//#then
|
2026-02-20 11:07:42 +09:00
|
|
|
expect(result).toMatch(/^42#[ZPMQVRWSNKTXJBYH]{2}:const x = 42$/)
|
2026-02-16 15:59:12 +09:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("formatHashLines", () => {
|
2026-02-20 11:07:42 +09:00
|
|
|
it("formats all lines as LINE#ID:content", () => {
|
2026-02-16 15:59:12 +09:00
|
|
|
//#given
|
2026-02-20 11:07:42 +09:00
|
|
|
const content = "a\nb\nc"
|
2026-02-16 15:59:12 +09:00
|
|
|
|
|
|
|
|
//#when
|
|
|
|
|
const result = formatHashLines(content)
|
|
|
|
|
|
|
|
|
|
//#then
|
|
|
|
|
const lines = result.split("\n")
|
|
|
|
|
expect(lines).toHaveLength(3)
|
2026-02-20 11:07:42 +09:00
|
|
|
expect(lines[0]).toMatch(/^1#[ZPMQVRWSNKTXJBYH]{2}:a$/)
|
|
|
|
|
expect(lines[1]).toMatch(/^2#[ZPMQVRWSNKTXJBYH]{2}:b$/)
|
|
|
|
|
expect(lines[2]).toMatch(/^3#[ZPMQVRWSNKTXJBYH]{2}:c$/)
|
2026-02-16 15:59:12 +09:00
|
|
|
})
|
|
|
|
|
})
|