Open History to see the last commit
1const assert = require("node:assert/strict");
2const test = require("node:test");
3const {
4 assertCommitOnDefaultBranch,
5 assertReleaseSource,
6 shouldReleaseNightly,
7} = require("./check-nightly-release.cjs");
8
9const now = Date.parse("2026-09-05T12:00:00Z");
10const hour = 60 * 60 * 1000;
11const nightly = (hoursAgo, overrides = {}) => ({
12 tag_name: "v1.0.1-nightly.20260905.123",
13 draft: false,
14 published_at: new Date(now - hoursAgo * hour).toISOString(),
15 ...overrides,
16});
17
18function releaseSourceFixture({
19 comparisonStatus = "ahead",
20 eventName = "workflow_dispatch",
21 ref = "refs/heads/main",
22 sha = "candidate",
23} = {}) {
24 const calls = [];
25 return {
26 calls,
27 options: {
28 context: {
29 eventName,
30 payload: { repository: { default_branch: "main" } },
31 ref,
32 repo: { owner: "example", repo: "app" },
33 sha,
34 },
35 github: {
36 rest: {
37 repos: {
38 async compareCommitsWithBasehead(params) {
39 calls.push(params);
40 return { data: { status: comparisonStatus } };
41 },
42 },
43 },
44 },
45 },
46 };
47}
48
49test("allows preview releases from any branch without consulting main", async () => {
50 const { options, calls } = releaseSourceFixture({ ref: "refs/heads/feature" });
51 await assertReleaseSource({ ...options, releaseChannel: "preview" });
52 assert.equal(calls.length, 0);
53});
54
55for (const releaseChannel of ["stable", "nightly"]) {
56 test(`rejects a manual ${releaseChannel} release from a feature branch`, async () => {
57 const { options, calls } = releaseSourceFixture({ ref: "refs/heads/feature" });
58 await assert.rejects(
59 assertReleaseSource({ ...options, releaseChannel }),
60 new RegExp(`${releaseChannel} releases must be dispatched from main`),
61 );
62 assert.equal(calls.length, 0);
63 });
64}
65
66for (const comparisonStatus of ["behind", "diverged"]) {
67 test(`rejects a release commit that is ${comparisonStatus} from main`, async () => {
68 const { options } = releaseSourceFixture({ comparisonStatus, eventName: "push" });
69 await assert.rejects(
70 assertCommitOnDefaultBranch({ ...options, sha: "release-commit" }),
71 new RegExp(`not contained in main \\(${comparisonStatus}\\)`),
72 );
73 });
74}
75
76function fixture({ releases = [nightly(7)], comparisonStatus = "ahead" } = {}) {
77 const calls = [];
78 return {
79 calls,
80 options: {
81 now,
82 context: { repo: { owner: "example", repo: "app" }, sha: "new" },
83 core: { info() {} },
84 github: {
85 rest: {
86 repos: {
87 listReleases() {},
88 async compareCommitsWithBasehead(params) {
89 calls.push(params);
90 return { data: { status: comparisonStatus } };
91 },
92 },
93 },
94 async paginate() {
95 return releases;
96 },
97 },
98 },
99 };
100}
101
102test("releases the first nightly when no nightly is published", async () => {
103 const { options } = fixture({
104 releases: [nightly(0, { tag_name: "v1.0.0" }), nightly(0, { draft: true })],
105 });
106 assert.equal(await shouldReleaseNightly(options), true);
107});
108
109test("uses publication time, not release order or the tagged commit date", async () => {
110 const { options } = fixture({
111 releases: [nightly(10), nightly(1), nightly(20, { tag_name: "nightly-v0.9.0" })],
112 });
113 assert.equal(await shouldReleaseNightly(options), false);
114});
115
116test("ignores stable releases and drafts when checking the gap", async () => {
117 const { options } = fixture({
118 releases: [nightly(0, { tag_name: "v1.0.0" }), nightly(0, { draft: true }), nightly(7)],
119 });
120 assert.equal(await shouldReleaseNightly(options), true);
121});
122
123test("fails instead of releasing when GitHub cannot supply release state", async () => {
124 const { options } = fixture();
125 options.github.paginate = async () => {
126 throw new Error("GitHub unavailable");
127 };
128 await assert.rejects(shouldReleaseNightly(options), /GitHub unavailable/);
129});
130
131for (const status of ["behind", "diverged"]) {
132 test(`skips a candidate commit that is ${status} relative to the last nightly`, async () => {
133 const { options } = fixture({ comparisonStatus: status });
134 assert.equal(await shouldReleaseNightly(options), false);
135 });
136}
137
138const { resolveLatestNightlyCommit } = require("./check-nightly-release.cjs");
139
140function nightlyCommitFixture({ releases, commitSha = "abc123" }) {
141 const refs = [];
142 const { options } = fixture({ releases });
143 options.github.rest.repos.getCommit = async ({ ref }) => {
144 refs.push(ref);
145 return { data: { sha: commitSha } };
146 };
147 return { options, refs };
148}
149
150test("stable releases resolve the commit of the newest published nightly", async () => {
151 const { options, refs } = nightlyCommitFixture({
152 releases: [
153 nightly(10, { tag_name: "v1.0.1-nightly.20260905.100" }),
154 nightly(1, { tag_name: "v1.0.1-nightly.20260905.123" }),
155 nightly(0, { tag_name: "v1.0.0" }),
156 nightly(0, { draft: true, tag_name: "v1.0.1-nightly.20260905.999" }),
157 ],
158 commitSha: "deadbeef",
159 });
160 assert.deepEqual(await resolveLatestNightlyCommit(options), {
161 tag: "v1.0.1-nightly.20260905.123",
162 sha: "deadbeef",
163 version: "1.0.1",
164 });
165 assert.deepEqual(refs, ["v1.0.1-nightly.20260905.123"]);
166});
167
168test("stable releases derive the version from legacy nightly tags", async () => {
169 const { options } = nightlyCommitFixture({
170 releases: [nightly(1, { tag_name: "nightly-v0.9.0-nightly.20260905.5" })],
171 });
172 assert.equal((await resolveLatestNightlyCommit(options)).version, "0.9.0");
173});