60 lines · 2.0 KB
import { describe, expect, mock, test } from "bun:test";

class StubEntrypoint {}
mock.module("cloudflare:workers", () => ({
  env: {},
  waitUntil: () => undefined,
  WorkflowEntrypoint: StubEntrypoint,
  DurableObject: StubEntrypoint,
  WorkerEntrypoint: StubEntrypoint,
}));

const ZERO = "0".repeat(40);
let rows: { ref: string; after_sha: string; created_at: number }[] = [];
let captured: unknown[] = [];

mock.module("./db", () => ({
  all: async (_sql: string, ...params: unknown[]) => {
    captured = params;
    return rows;
  },
  first: async () => null,
  run: async () => ({ meta: { changes: 0 } }),
  batch: async () => [],
  stmt: () => ({}),
  placeholders: () => "",
  chunks: <T>(items: T[]) => [items],
  MAX_IN_LIST: 90,
}));

const { recentBranchPushesFor } = await import("./branch-pushes");

const repo = { id: "r1", default_branch: "main" };

describe("recentBranchPushesFor", () => {
  test("skips default branch, deletions, duplicates and caps at three newest", async () => {
    rows = [
      { ref: "refs/heads/main", after_sha: "a".repeat(40), created_at: 900 },
      { ref: "refs/heads/gone", after_sha: ZERO, created_at: 800 },
      { ref: "refs/heads/one", after_sha: "b".repeat(40), created_at: 700 },
      { ref: "refs/heads/one", after_sha: "c".repeat(40), created_at: 650 },