37 lines · 1.4 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { fakeD1 } from "../metering/fake-d1.test-helper";
import { scopedTestState } from "./scoped.test-helper";

let d1 = fakeD1();
const fakeEnv: Record<string, unknown> = {
  get DB() {
    return d1.binding;
  },
};
const scoped = scopedTestState();
scoped.install();
mock.module("cloudflare:workers", () => ({ DurableObject: class {}, env: fakeEnv, waitUntil: () => undefined }));

const { deploySitesForPush, isMergeQueueBranch, shaPusher } = await import("./triggers");

beforeEach(() => {
  d1 = fakeD1();
  scoped.reset();
});

describe("site triggers", () => {
  test("merge-queue branches never deploy", async () => {
    expect(isMergeQueueBranch("gitfor-merge-queue/main/pr-4")).toBe(true);
    expect(isMergeQueueBranch("feature/gitfor-merge-queue")).toBe(false);
    expect(await deploySitesForPush("r1", "gitfor-merge-queue/main/pr-4", "a".repeat(40), "u1")).toBe(0);
  });

  test("preview trust follows the SHA's pusher, not whoever reran it", async () => {
    scoped.repo("r1").run(`INSERT INTO push_events VALUES ('p1', 'r1', 'refs/heads/f', 'x', 'abc', 'author', 1),
      ('p2', 'r1', 'refs/heads/f', 'abc', 'def', 'other', 2)`);
    expect(await shaPusher("r1", "refs/heads/f", "abc")).toBe("author");
    expect(await shaPusher("r1", "refs/heads/f", "zzz")).toBeNull();
    expect(await shaPusher("r1", "refs/heads/g", "abc")).toBeNull();
  });
});