import { describe, expect, test } from "bun:test"; import { ipBucket } from "./ip"; describe("ipBucket", () => { test("IPv4 and unknown stay exact", () => { expect(ipBucket("203.0.113.9")).toBe("203.0.113.9"); expect(ipBucket("unknown")).toBe("unknown"); expect(ipBucket("::ffff:203.0.113.9")).toBe("::ffff:203.0.113.9"); }); test("IPv6 addresses in one /64 share a key", () => { const key = ipBucket("2001:db8:1:2:aaaa:bbbb:cccc:dddd"); expect(key).toBe("2001:db8:1:2::/64"); expect(ipBucket("2001:0DB8:0001:0002::1")).toBe(key); expect(ipBucket("2001:db8:1:2::")).toBe(key); expect(ipBucket("2001:db8:1:3::1")).not.toBe(key); }); test("compressed forms expand before bucketing", () => { expect(ipBucket("2001:db8::1")).toBe("2001:db8:0:0::/64"); expect(ipBucket("::1")).toBe("0:0:0:0::/64"); }); test("malformed input is left as-is", () => { expect(ipBucket("1::2::3")).toBe("1::2::3"); expect(ipBucket("fe80::1%eth0")).toBe("fe80::1%eth0"); expect(ipBucket("12345::1")).toBe("12345::1"); }); });