|
| 1 | +const assert = require("node:assert/strict"); |
| 2 | +const test = require("node:test"); |
| 3 | +const { shouldReleaseNightly } = require("./check-nightly-release.cjs"); |
| 4 | + |
| 5 | +const now = Date.parse("2026-09-05T12:00:00Z"); |
| 6 | +const hour = 60 * 60 * 1000; |
| 7 | +const nightly = (hoursAgo, overrides = {}) => ({ |
| 8 | + tag_name: "v1.0.1-nightly.20260905.123", |
| 9 | + draft: false, |
| 10 | + published_at: new Date(now - hoursAgo * hour).toISOString(), |
| 11 | + ...overrides, |
| 12 | +}); |
| 13 | + |
| 14 | +function fixture({ releases = [nightly(7)], comparisonStatus = "ahead" } = {}) { |
| 15 | + const calls = []; |
| 16 | + return { |
| 17 | + calls, |
| 18 | + options: { |
| 19 | + now, |
| 20 | + context: { repo: { owner: "example", repo: "app" }, sha: "new" }, |
| 21 | + core: { info() {} }, |
| 22 | + github: { |
| 23 | + rest: { |
| 24 | + repos: { |
| 25 | + listReleases() {}, |
| 26 | + async compareCommitsWithBasehead(params) { |
| 27 | + calls.push(params); |
| 28 | + return { data: { status: comparisonStatus } }; |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + async paginate() { |
| 33 | + return releases; |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +test("releases the first nightly when no nightly is published", async () => { |
| 41 | + const { options } = fixture({ |
| 42 | + releases: [nightly(0, { tag_name: "v1.0.0" }), nightly(0, { draft: true })], |
| 43 | + }); |
| 44 | + assert.equal(await shouldReleaseNightly(options), true); |
| 45 | +}); |
| 46 | + |
| 47 | +test("waits six hours after publication, including manual nightlies", async () => { |
| 48 | + for (const age of [0, 3, 6 - 1 / 3600]) { |
| 49 | + const { options, calls } = fixture({ releases: [nightly(age)] }); |
| 50 | + assert.equal(await shouldReleaseNightly(options), false); |
| 51 | + assert.equal(calls.length, 0); |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +test("releases new commits at six hours and after an idle period", async () => { |
| 56 | + for (const age of [6, 7, 24]) { |
| 57 | + const { options } = fixture({ releases: [nightly(age)] }); |
| 58 | + assert.equal(await shouldReleaseNightly(options), true); |
| 59 | + } |
| 60 | +}); |
| 61 | + |
| 62 | +test("skips unchanged commits after the gap", async () => { |
| 63 | + const { options } = fixture({ comparisonStatus: "identical" }); |
| 64 | + assert.equal(await shouldReleaseNightly(options), false); |
| 65 | +}); |
| 66 | + |
| 67 | +test("uses publication time, not release order or the tagged commit date", async () => { |
| 68 | + const { options } = fixture({ |
| 69 | + releases: [nightly(10), nightly(1), nightly(20, { tag_name: "nightly-v0.9.0" })], |
| 70 | + }); |
| 71 | + assert.equal(await shouldReleaseNightly(options), false); |
| 72 | +}); |
| 73 | + |
| 74 | +test("ignores stable releases and drafts when checking the gap", async () => { |
| 75 | + const { options } = fixture({ |
| 76 | + releases: [nightly(0, { tag_name: "v1.0.0" }), nightly(0, { draft: true }), nightly(7)], |
| 77 | + }); |
| 78 | + assert.equal(await shouldReleaseNightly(options), true); |
| 79 | +}); |
| 80 | + |
| 81 | +test("compares against the published tag, including legacy nightly tags", async () => { |
| 82 | + const tag = "nightly-v0.9.0"; |
| 83 | + const { options, calls } = fixture({ releases: [nightly(7, { tag_name: tag })] }); |
| 84 | + assert.equal(await shouldReleaseNightly(options), true); |
| 85 | + assert.equal(calls[0].basehead, `${tag}...new`); |
| 86 | +}); |
| 87 | + |
| 88 | +test("fails instead of releasing when GitHub cannot supply release state", async () => { |
| 89 | + const { options } = fixture(); |
| 90 | + options.github.paginate = async () => { |
| 91 | + throw new Error("GitHub unavailable"); |
| 92 | + }; |
| 93 | + await assert.rejects(shouldReleaseNightly(options), /GitHub unavailable/); |
| 94 | +}); |
| 95 | + |
| 96 | +for (const status of ["behind", "diverged"]) { |
| 97 | + test(`skips a candidate commit that is ${status} relative to the last nightly`, async () => { |
| 98 | + const { options } = fixture({ comparisonStatus: status }); |
| 99 | + assert.equal(await shouldReleaseNightly(options), false); |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +const { resolveLatestNightlyCommit } = require("./check-nightly-release.cjs"); |
| 104 | + |
| 105 | +function nightlyCommitFixture({ releases, commitSha = "abc123" }) { |
| 106 | + const refs = []; |
| 107 | + const { options } = fixture({ releases }); |
| 108 | + options.github.rest.repos.getCommit = async ({ ref }) => { |
| 109 | + refs.push(ref); |
| 110 | + return { data: { sha: commitSha } }; |
| 111 | + }; |
| 112 | + return { options, refs }; |
| 113 | +} |
| 114 | + |
| 115 | +test("stable releases resolve the commit of the newest published nightly", async () => { |
| 116 | + const { options, refs } = nightlyCommitFixture({ |
| 117 | + releases: [ |
| 118 | + nightly(10, { tag_name: "v1.0.1-nightly.20260905.100" }), |
| 119 | + nightly(1, { tag_name: "v1.0.1-nightly.20260905.123" }), |
| 120 | + nightly(0, { tag_name: "v1.0.0" }), |
| 121 | + nightly(0, { draft: true, tag_name: "v1.0.1-nightly.20260905.999" }), |
| 122 | + ], |
| 123 | + commitSha: "deadbeef", |
| 124 | + }); |
| 125 | + assert.deepEqual(await resolveLatestNightlyCommit(options), { |
| 126 | + tag: "v1.0.1-nightly.20260905.123", |
| 127 | + sha: "deadbeef", |
| 128 | + version: "1.0.1", |
| 129 | + }); |
| 130 | + assert.deepEqual(refs, ["v1.0.1-nightly.20260905.123"]); |
| 131 | +}); |
| 132 | + |
| 133 | +test("stable releases derive the version from legacy nightly tags", async () => { |
| 134 | + const { options } = nightlyCommitFixture({ |
| 135 | + releases: [nightly(1, { tag_name: "nightly-v0.9.0-nightly.20260905.5" })], |
| 136 | + }); |
| 137 | + assert.equal((await resolveLatestNightlyCommit(options)).version, "0.9.0"); |
| 138 | +}); |
| 139 | + |
| 140 | +test("stable releases fail without a published nightly", async () => { |
| 141 | + const { options } = nightlyCommitFixture({ releases: [nightly(0, { tag_name: "v1.0.0" })] }); |
| 142 | + await assert.rejects(resolveLatestNightlyCommit(options), /No published nightly/); |
| 143 | +}); |
0 commit comments