import { selectNextCommit, versionFilesProblem, WALK_LIMIT } from './next-publish-commit.mjs'; const sha = (n) => n.toString(16).padStart(7, '0').padEnd(40, 'a'); const history = (count) => Array.from({ length: count }, (_, i) => ({ sha: sha(count - i), parents: [sha(count - i - 1)], date: new Date(Date.UTC(2026, 11, 1, 0, count - i)).toISOString(), })); const ci = (conclusion, names = ['test (node 22)', 'test (node 24)', 'published runtime (node 22.12.0)']) => names.map((name, id) => ({ id, name, status: conclusion ? 'completed' : 'in_progress', conclusion })); const select = (commits, gates, nextVersion) => selectNextCommit({ commits, checkRunsAt: async (s) => gates[s] ?? [], nextVersion }); const nextAt = (commit) => `2.4.0-next.${commit.date.replace(/\D/g, '').slice(0, 14)}.g${commit.sha.slice(0, 7)}`; test('a head whose CI release gate passed is selected', async () => { const commits = history(3); expect(await select(commits, { [commits[0].sha]: ci('success') })).toEqual({ sha: commits[0].sha }); }); test('a merge gate pass is enough on its own', async () => { const commits = history(3); const gates = { [commits[0].sha]: [{ id: 1, name: 'merge gate', conclusion: 'success' }] }; expect(await select(commits, gates)).toEqual({ sha: commits[0].sha }); }); test('the walk passes over commits CI skipped, cancelled, failed or is still running', async () => { const commits = history(6); const gates = { [commits[0].sha]: [], [commits[1].sha]: ci('cancelled'), [commits[2].sha]: [ ...ci('success', ['test (node 22)', 'published runtime (node 22.12.0)']), ...ci('failure', ['test (node 24)']), ], [commits[3].sha]: ci(null), [commits[4].sha]: ci('success'), }; expect(await select(commits, gates)).toEqual({ sha: commits[4].sha }); }); test('the walk follows first parents, so a merged branch commit that passed is not selected', async () => { const [merge, mainParent] = history(2); const branch = { sha: 'b'.repeat(40), parents: [mainParent.parents[0]], date: merge.date }; merge.parents.push(branch.sha); const gates = { [branch.sha]: ci('success'), [mainParent.sha]: ci('success') }; expect(await select([merge, branch, mainParent], gates)).toEqual({ sha: mainParent.sha }); }); test('a rerun that passed replaces the cancelled run of the same job', async () => { const commits = history(2); const gates = { [commits[0].sha]: [...ci('cancelled'), ...ci('success').map((run) => Object.assign(run, { id: run.id + 10 }))], }; expect(await select(commits, gates)).toEqual({ sha: commits[0].sha }); }); test('the walk stops at the commit next already carries', async () => { const commits = history(4); const gates = { [commits[0].sha]: ci('cancelled'), [commits[2].sha]: ci('success') }; expect(await select(commits, gates, nextAt(commits[1]))).toEqual({ reason: expect.stringContaining('next already carries'), }); }); test('the passing commit next carries is selected again so a half-published version can be completed', async () => { const commits = history(3); const gates = { [commits[0].sha]: ci('cancelled'), [commits[1].sha]: ci('success') }; expect(await select(commits, gates, nextAt(commits[1]))).toEqual({ sha: commits[1].sha }); }); test('a passing child that shares its committer second with the commit next carries is selected', async () => { const commits = history(3); commits[0].date = commits[1].date; expect(await select(commits, { [commits[0].sha]: ci('success') }, nextAt(commits[1]))).toEqual({ sha: commits[0].sha, }); }); test('a commit older than the one next carries is never selected', async () => { const commits = history(3); const newer = { sha: 'f'.repeat(40), date: '2026-12-02T00:00:00Z' }; expect(await select(commits, { [commits[0].sha]: ci('success') }, nextAt(newer))).toMatchObject({ reason: expect.stringContaining('not newer than next'), }); }); test('the walk gives up after its limit', async () => { const commits = history(WALK_LIMIT + 1); expect(await select(commits, { [commits[WALK_LIMIT].sha]: ci('success') })).toMatchObject({ reason: expect.stringContaining(`newest ${WALK_LIMIT} commits`), }); }); const files = (version, nextVersion = null) => ({ 'packages/stim-cli/package.json': JSON.stringify({ version }), '.github/next-version': nextVersion, }); test.each([ ['the same version files', files('2.3.0'), files('2.3.0'), null], [ 'a manifest edit that keeps the version', { ...files('2.3.0'), 'packages/stim-cli/package.json': JSON.stringify({ version: '2.3.0', private: false }) }, files('2.3.0'), null, ], [ 'a release candidate at the head', files('2.3.0-rc.1'), files('2.2.0'), expect.stringContaining('release candidate'), ], [ 'an untested version commit after the selected commit', files('2.3.0'), files('2.3.0-rc.1'), expect.stringContaining('moved from 2.3.0-rc.1 to 2.3.0'), ], [ 'a major declared after the selected commit', files('2.3.0', '3.0.0\n'), files('2.3.0'), expect.stringContaining('next-version'), ], ])('%s', (_name, head, selected, problem) => { expect(versionFilesProblem(head, selected)).toEqual(problem); });