From 5ae9de0e8e7aaac65c05c6ed203054cb3470c195 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Sat, 21 Feb 2026 04:14:54 +0900 Subject: [PATCH] fix: include line number in hashline computation --- src/tools/hashline-edit/hash-computation.test.ts | 16 ++++++++++++++-- src/tools/hashline-edit/hash-computation.ts | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/tools/hashline-edit/hash-computation.test.ts b/src/tools/hashline-edit/hash-computation.test.ts index e34712a8..7c2ae84d 100644 --- a/src/tools/hashline-edit/hash-computation.test.ts +++ b/src/tools/hashline-edit/hash-computation.test.ts @@ -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() {" diff --git a/src/tools/hashline-edit/hash-computation.ts b/src/tools/hashline-edit/hash-computation.ts index f76041f7..0942b93c 100644 --- a/src/tools/hashline-edit/hash-computation.ts +++ b/src/tools/hashline-edit/hash-computation.ts @@ -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] }