import { describe, expect, test } from "bun:test";
import { filterAdvertisement, filterLsRefs } from "./ref-advertisement";
import { concatBytes, FLUSH_PKT, packetText, parseAllPackets, pktLine } from "./pktline";
const A = "a".repeat(40);
const B = "b".repeat(40);
const C = "c".repeat(40);
function lines(body: Uint8Array): string[] {
return parseAllPackets(body).map((packet) => (packet.kind === "data" ? (packetText(packet) ?? "") : packet.kind));
}
describe("filterAdvertisement", () => {
const body = concatBytes([
pktLine("# service=git-upload-pack\n"),
FLUSH_PKT,
pktLine(`${A} HEAD\0multi_ack symref=HEAD:refs/heads/main agent=x\n`),
pktLine(`${C} refs/gitfor/pull/r/1/base\n`),
pktLine(`${B} refs/heads/dev\n`),
pktLine(`${A} refs/heads/main\n`),
FLUSH_PKT,
]);
test("drops hidden refs and points HEAD at the default branch", () => {
const out = lines(filterAdvertisement(body, "dev"));
expect(out.some((line) => line.includes("refs/gitfor/"))).toBe(false);
expect(out[2]).toBe(`${B} HEAD\0multi_ack agent=x symref=HEAD:refs/heads/dev`);
});
test("leaves HEAD alone when the default branch is missing", () => {
const out = lines(filterAdvertisement(body, "gone"));
expect(out[2]).toBe(`${A} HEAD\0multi_ack symref=HEAD:refs/heads/main agent=x`);
});
test("moves capabilities when the first ref is hidden", () => {
const hiddenFirst = concatBytes([
pktLine(`${C} refs/gitfor/x\0caps\n`),
pktLine(`${B} refs/heads/dev\n`),
FLUSH_PKT,
]);