Files
claude-code/src/utils/__tests__/hash.test.ts
claude-code-best cad6409bfe test: 添加 Utils 纯函数单元测试 (测试计划 02)
覆盖 xml, hash, stringUtils, semver, uuid, format, frontmatterParser,
file, glob, diff 共 10 个模块的纯函数测试。
json.ts 因模块加载链路过重暂跳过。
共 190 个测试用例(含已有 array/set)全部通过。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-01 22:03:02 +08:00

58 lines
1.5 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { djb2Hash, hashContent, hashPair } from "../hash";
describe("djb2Hash", () => {
test("returns a number", () => {
expect(typeof djb2Hash("hello")).toBe("number");
});
test("returns 0 for empty string", () => {
expect(djb2Hash("")).toBe(0);
});
test("is deterministic", () => {
expect(djb2Hash("test")).toBe(djb2Hash("test"));
});
test("different strings produce different hashes", () => {
expect(djb2Hash("abc")).not.toBe(djb2Hash("def"));
});
test("returns 32-bit integer", () => {
const hash = djb2Hash("some long string to hash");
expect(hash).toBe(hash | 0); // bitwise OR with 0 preserves 32-bit int
});
});
describe("hashContent", () => {
test("returns a string", () => {
expect(typeof hashContent("hello")).toBe("string");
});
test("is deterministic", () => {
expect(hashContent("test")).toBe(hashContent("test"));
});
test("different strings produce different hashes", () => {
expect(hashContent("abc")).not.toBe(hashContent("def"));
});
});
describe("hashPair", () => {
test("returns a string", () => {
expect(typeof hashPair("a", "b")).toBe("string");
});
test("is deterministic", () => {
expect(hashPair("a", "b")).toBe(hashPair("a", "b"));
});
test("order matters", () => {
expect(hashPair("a", "b")).not.toBe(hashPair("b", "a"));
});
test("disambiguates different splits", () => {
expect(hashPair("ts", "code")).not.toBe(hashPair("tsc", "ode"));
});
});