fix: include line number in hashline computation

This commit is contained in:
YeonGyu-Kim 2026-02-21 04:14:54 +09:00
parent df1a0a59d9
commit 5ae9de0e8e
2 changed files with 16 additions and 4 deletions

View File

@ -2,19 +2,31 @@ import { describe, it, expect } from "bun:test"
import { computeLineHash, formatHashLine, formatHashLines } from "./hash-computation"
describe("computeLineHash", () => {
it("returns deterministic 2-char CID hash", () => {
it("returns deterministic 2-char CID hash per line", () => {
//#given
const content = "function hello() {"
//#when
const hash1 = computeLineHash(1, content)
const hash2 = computeLineHash(999, content)
const hash2 = computeLineHash(1, content)
//#then
expect(hash1).toBe(hash2)
expect(hash1).toMatch(/^[ZPMQVRWSNKTXJBYH]{2}$/)
})
it("produces different hashes for same content on different lines", () => {
//#given
const content = "function hello() {"
//#when
const hash1 = computeLineHash(1, content)
const hash2 = computeLineHash(2, content)
//#then
expect(hash1).not.toBe(hash2)
})
it("ignores whitespace differences", () => {
//#given
const content1 = "function hello() {"

View File

@ -1,9 +1,9 @@
import { HASHLINE_DICT } from "./constants"
export function computeLineHash(lineNumber: number, content: string): string {
void lineNumber
const stripped = content.replace(/\s+/g, "")
const hash = Bun.hash.xxHash32(stripped)
const hashInput = `${lineNumber}:${stripped}`
const hash = Bun.hash.xxHash32(hashInput)
const index = hash % 256
return HASHLINE_DICT[index]
}