From b2bd6a3229f8213ff61df5e2667ca9930c4b60db Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Fri, 13 Mar 2026 19:35:44 +0000 Subject: [PATCH] Add some unit tests for `sync-checks.ts` --- pr-checks/sync-checks.test.ts | 49 +++++++++++++++++++++++++++++++++++ pr-checks/sync-checks.ts | 4 +-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 pr-checks/sync-checks.test.ts diff --git a/pr-checks/sync-checks.test.ts b/pr-checks/sync-checks.test.ts new file mode 100644 index 000000000..2f50b0e9e --- /dev/null +++ b/pr-checks/sync-checks.test.ts @@ -0,0 +1,49 @@ +#!/usr/bin/env npx tsx + +/* +Tests for the sync-checks.ts script +*/ + +import * as assert from "node:assert/strict"; +import { describe, it } from "node:test"; + +import { CheckInfo, Exclusions, removeExcluded } from "./sync-checks"; + +const toCheckInfo = (name: string) => + ({ context: name, app_id: -1 }) satisfies CheckInfo; + +const expectedPartialMatches = ["PR Check - Foo", "https://example.com"].map( + toCheckInfo, +); + +const expectedExactMatches = ["CodeQL", "Update"].map(toCheckInfo); + +const testChecks = expectedExactMatches.concat(expectedPartialMatches); + +const emptyExclusions: Exclusions = { + is: [], + contains: [], +}; + +describe("removeExcluded", async () => { + await it("retains all checks if no exclusions are configured", () => { + const retained = removeExcluded(emptyExclusions, testChecks); + assert.deepEqual(retained, testChecks); + }); + + await it("removes exact matches", () => { + const retained = removeExcluded( + { ...emptyExclusions, is: ["CodeQL", "Update"] }, + testChecks, + ); + assert.deepEqual(retained, expectedPartialMatches); + }); + + await it("removes partial matches", () => { + const retained = removeExcluded( + { ...emptyExclusions, contains: ["https://", "PR Check"] }, + testChecks, + ); + assert.deepEqual(retained, expectedExactMatches); + }); +}); diff --git a/pr-checks/sync-checks.ts b/pr-checks/sync-checks.ts index c969e2490..a8c3665b9 100755 --- a/pr-checks/sync-checks.ts +++ b/pr-checks/sync-checks.ts @@ -30,7 +30,7 @@ const codeqlActionRepo = { }; /** Represents a configuration of which checks should not be set up as required checks. */ -interface Exclusions { +export interface Exclusions { /** A list of strings that, if contained in a check name, are excluded. */ contains: string[]; /** A list of check names that are excluded if their name is an exact match. */ @@ -55,7 +55,7 @@ function getApiClient(token: string): ApiClient { * Represents information about a check run. We track the `app_id` that generated the check, * because the API will require it in addition to the name in the future. */ -interface CheckInfo { +export interface CheckInfo { /** The display name of the check. */ context: string; /** The ID of the app that generated the check. */