Files
muSRFlow/tests/ci-release.test.js
T
salman 12ecbf4f7d Automate Windows publication and coordinated cross-platform releases
Build and publish Windows installers on version tags using the tested WSL packaging path. Keep Windows metadata in its own package and support publication recovery from saved Linux or Windows artifacts.

Create one draft Release only after matching Linux and Windows provenance is published. Preserve reviewed release notes and handle concurrent creation safely. Cover publishing, recovery, and coordination with regression tests and update deployment and installation guides.
2026-10-30 09:22:59 +01:00

170 lines
11 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { execFileSync } = require('child_process');
const { prepareRelease, readReleaseContext, createDraftRelease } = require('../scripts/ci-release');
async function main() {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'musrflow-tag-release-'));
try {
const gitOptions = { cwd: directory, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] };
fs.writeFileSync(path.join(directory, 'package.json'), JSON.stringify({ version: '2.3.4-rc.1' }));
execFileSync('git', ['init'], gitOptions);
execFileSync('git', ['add', 'package.json'], gitOptions);
const identity = ['-c', 'user.name=Release Tests', '-c', 'user.email=tests@example.test', '-c', 'commit.gpgsign=false', '-c', 'tag.gpgSign=false'];
execFileSync('git', [...identity, 'commit', '-m', 'Fixture'], gitOptions);
execFileSync('git', [...identity, 'tag', '-a', 'v2.3.4-rc.1', '-m', 'Annotated fixture'], gitOptions);
const env = { PACKAGE_RUN_ID: '456', GITEA_EVENT_NAME: 'push', GITEA_REF: 'refs/tags/v2.3.4-rc.1',
GITEA_SERVER_URL: 'https://gitea.example.test', GITEA_REPOSITORY: 'owner/muSRFlow',
RELEASES_TOKEN: 'release-token', PACKAGES_TOKEN: 'package-token' };
const context = prepareRelease(directory, env);
assert.strictEqual(context.tag, 'v2.3.4-rc.1');
execFileSync('git', ['tag', '-d', context.tag], gitOptions);
execFileSync('git', [...identity, 'tag', context.tag], gitOptions);
assert.deepStrictEqual(prepareRelease(directory, env), context, 'lightweight tags resolve to the same commit');
assert.strictEqual(prepareRelease(directory, { ...env, GITEA_EVENT_NAME: 'workflow_dispatch' }).tag, null);
for (const ref of ['refs/tags/v2.3.5', 'refs/heads/master', 'refs/tags/invalid']) {
assert.throws(() => prepareRelease(directory, { ...env, GITEA_REF: ref }), /must match package.json/);
}
assert.throws(() => prepareRelease(directory, { ...env, GITEA_EVENT_NAME: 'release' }), /Only version-tag/);
execFileSync('git', [...identity, 'commit', '--allow-empty', '-m', 'Wrong checkout'], gitOptions);
assert.throws(() => prepareRelease(directory, env), /does not match the release tag/);
assert.strictEqual(readReleaseContext(directory, '456'), null);
const sourceFile = path.join(directory, 'build-source.txt');
const contextFile = path.join(directory, 'release-context.json');
fs.writeFileSync(sourceFile, `muSRFlow=${context.commit}\n`);
fs.writeFileSync(contextFile, JSON.stringify(context));
assert.deepStrictEqual(readReleaseContext(directory, '456'), context);
assert.throws(() => readReleaseContext(directory, '457'), /run ID mismatch/);
fs.writeFileSync(sourceFile, 'muSRFlow=wrong\n');
assert.throws(() => readReleaseContext(directory, '456'), /source commit/);
fs.writeFileSync(sourceFile, `muSRFlow=${context.commit}\n`);
for (const change of [{ schema: 2 }, { tag: 'v9.9.9' }, { version: '2.3.4-01' }, { commit: 'invalid' }]) {
fs.writeFileSync(contextFile, JSON.stringify({ ...context, ...change }));
assert.throws(() => readReleaseContext(directory, '456'), /Invalid release context/);
}
fs.writeFileSync(contextFile, JSON.stringify(context));
let existing = null;
let tagCommit = context.commit;
let tagStatus = 200;
let createStatus = 201;
let missingPackage = '';
let packageStatus = 200;
let remoteChange = {};
let badChecksum = false;
let concurrentRelease = null;
const writes = [];
async function api(url, options) {
assert.strictEqual(options.redirect, 'error');
if (url.includes('/api/packages/')) {
assert.strictEqual(options.headers.Authorization, 'token package-token');
const [name, version, file] = url.split('/generic/')[1].split('/').map(decodeURIComponent);
const remoteContext = JSON.parse(fs.readFileSync(contextFile, 'utf8'));
assert.strictEqual(version, remoteContext.version);
if (name === missingPackage) return new Response(null, { status: 404 });
if (packageStatus !== 200) return new Response(null, { status: packageStatus });
if (file === 'release-context.json') {
return Response.json({ ...remoteContext,
runId: name === 'musrflow-windows' ? '789' : '456', ...remoteChange[name] });
}
assert(file.endsWith('.sha256'));
return new Response(badChecksum ? 'invalid' : `${'a'.repeat(64)} ${file.slice(0, -7)}\n`);
}
assert.strictEqual(options.headers.Authorization, 'token release-token');
if (options.method === 'POST') {
assert(url.endsWith('/releases'));
const payload = JSON.parse(options.body);
writes.push(payload);
if (concurrentRelease) existing = concurrentRelease;
return Response.json({ ...payload, id: 10, html_url: 'https://gitea.example.test/draft' }, { status: createStatus });
}
if (url.includes('/releases/tags/')) return existing ? Response.json(existing) : new Response(null, { status: 404 });
assert(url.endsWith(`/tags/${JSON.parse(fs.readFileSync(contextFile, 'utf8')).tag}`));
return Response.json({ commit: { sha: tagCommit } }, { status: tagStatus });
}
const draft = await createDraftRelease(directory, env, api);
assert.strictEqual(draft.draft, true);
assert.strictEqual(draft.prerelease, true);
assert.strictEqual(draft.tag_name, context.tag);
assert.strictEqual(draft.target_commitish, context.commit);
assert(draft.body.includes('/generic/musrflow/2.3.4-rc.1/'));
assert(draft.body.includes('/generic/musrflow-runtime/2.3.4-rc.1/'));
assert(draft.body.includes('/generic/musrflow-windows/2.3.4-rc.1/muSRFlow-2.3.4-rc.1-x64.exe'));
assert(draft.body.includes('a'.repeat(64)));
assert(draft.body.includes('Linux packaging workflow run ID: 456'));
assert(draft.body.includes('Windows packaging workflow run ID: 789'));
existing = { ...draft, body: draft.body + '\nUser-written notes', draft: false };
assert.deepStrictEqual(await createDraftRelease(directory, env, api), existing);
assert.strictEqual(writes.length, 1, 'do not edit or unpublish an existing Release');
existing = { ...draft, body: 'Unrelated release' };
await assert.rejects(createDraftRelease(directory, env, api), /not changed/);
tagCommit = 'b'.repeat(40);
await assert.rejects(createDraftRelease(directory, env, api), /tag moved/);
tagCommit = context.commit;
tagStatus = 404;
await assert.rejects(createDraftRelease(directory, env, api), /verify release tag: HTTP 404/);
assert.strictEqual(writes.length, 1, 'never create a missing tag');
tagStatus = 200;
existing = null;
// First platform waits; the last completed platform assembles the same
// five downloads regardless of which workflow reaches this step last.
for (const name of ['musrflow', 'musrflow-runtime', 'musrflow-windows']) {
missingPackage = name;
assert.strictEqual(await createDraftRelease(directory, env, api), null);
}
missingPackage = '';
packageStatus = 403;
await assert.rejects(createDraftRelease(directory, env, api), /release context: HTTP 403/);
packageStatus = 200;
for (const change of [{ commit: 'b'.repeat(40) }, { tag: 'v9.9.9' }, { version: '9.9.9' }, { schema: 2 }, { runId: 'invalid' }]) {
remoteChange = { 'musrflow-windows': change };
await assert.rejects(createDraftRelease(directory, env, api), /does not match the release/);
}
remoteChange = { 'musrflow-runtime': { runId: '999' } };
await assert.rejects(createDraftRelease(directory, env, api), /different packaging runs/);
remoteChange = {};
badChecksum = true;
await assert.rejects(createDraftRelease(directory, env, api), /Invalid checksum/);
badChecksum = false;
await assert.rejects(createDraftRelease(directory, { ...env, PACKAGES_TOKEN: '' }, api), /package read token/);
assert.strictEqual(writes.length, 1, 'incomplete or mismatched builds must not create a draft');
for (const status of [409, 422]) {
createStatus = status;
concurrentRelease = { ...draft, draft: false, body: draft.body + '\nReviewed notes' };
assert.deepStrictEqual(await createDraftRelease(directory, env, api), concurrentRelease);
existing = null;
}
concurrentRelease = { ...draft, body: 'Unrelated concurrent release' };
await assert.rejects(createDraftRelease(directory, env, api), /Concurrent Release could not be verified/);
concurrentRelease = null;
existing = null;
createStatus = 403;
await assert.rejects(createDraftRelease(directory, env, api), /Create draft Release: HTTP 403/);
await assert.rejects(createDraftRelease(directory, { ...env, RELEASES_TOKEN: '' }, api), /write token/);
// Stable versions are not marked as prereleases.
const stable = { ...context, tag: 'v2.3.4', version: '2.3.4' };
fs.writeFileSync(contextFile, JSON.stringify(stable));
createStatus = 201;
const stableDraft = await createDraftRelease(directory, env, api);
assert.strictEqual(stableDraft.prerelease, false);
// Simulate a Windows artifact arriving last, with its own run ID.
fs.writeFileSync(contextFile, JSON.stringify({ ...context, runId: '789' }));
const windowsDraft = await createDraftRelease(directory, { ...env, PACKAGE_RUN_ID: '789' }, api);
assert.strictEqual(windowsDraft.body, draft.body);
fs.writeFileSync(contextFile, JSON.stringify({ ...context, tag: null }));
assert.strictEqual(await createDraftRelease(directory, env, async () => { throw new Error('Manual builds must not call the API'); }), null);
fs.unlinkSync(contextFile);
assert.strictEqual(await createDraftRelease(directory, env, async () => { throw new Error('Old artifacts must not call the API'); }), null);
console.log('Tag validation and draft release tests passed');
} finally {
fs.rmSync(directory, { recursive: true, force: true });
}
}
main().catch(error => { console.error(error); process.exitCode = 1; });