29 lines · 1.2 KB
import { describe, expect, test } from "bun:test";
import { concatBytes, FLUSH_PKT, pktLine } from "./pktline";
import { appliedCommands } from "./receive-report";

const commands = [{ ref: "refs/heads/main" }, { ref: "refs/heads/dev" }];

describe("appliedCommands", () => {
  test("returns only refs acknowledged with ok", () => {
    const report = concatBytes([
      pktLine("unpack ok\n"),
      pktLine("ok refs/heads/main\n"),
      pktLine("ng refs/heads/dev x\n"),
      FLUSH_PKT,
    ]);
    expect(appliedCommands(report, commands)).toEqual([{ ref: "refs/heads/main" }]);
  });

  test("treats a report without ok lines as nothing applied", () => {
    expect(appliedCommands(concatBytes([pktLine("unpack error\n"), FLUSH_PKT]), commands)).toEqual([]);
    expect(appliedCommands(new Uint8Array(), commands)).toEqual([]);
  });

  test("reads ok lines inside side-band packets", () => {
    const inner = concatBytes([pktLine("unpack ok\n"), pktLine("ok refs/heads/dev\n"), FLUSH_PKT]);
    const report = concatBytes([pktLine(concatBytes([new Uint8Array([1]), inner])), FLUSH_PKT]);
    expect(appliedCommands(report, commands)).toEqual([{ ref: "refs/heads/dev" }]);
  });
});