Files
muSRFlow/tests/ci-publish-packages.test.js
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

225 lines
15 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 crypto = require('crypto');
const { publishPackages } = require('../scripts/ci-publish-packages');
const { checkPackageRun } = require('../scripts/ci-check-package-run');
async function main() {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'musrflow-publish-'));
const env = {
GITEA_SERVER_URL: 'https://gitea.example.test', GITEA_REPOSITORY: 'owner/muSRFlow',
PACKAGE_RUN_ID: '123', PACKAGES_TOKEN: 'test-token'
};
const stored = new Map();
const linkedPackages = new Set();
const calls = [];
async function registry(url, options) {
calls.push({ url, method: options.method || 'GET' });
assert.strictEqual(options.headers.Authorization, 'token test-token');
assert.strictEqual(options.redirect, 'error');
if (options.method === 'PUT') {
const chunks = [];
for await (const chunk of options.body) chunks.push(chunk);
if (stored.has(url)) return new Response(null, { status: 409 });
stored.set(url, Buffer.concat(chunks));
return new Response(null, { status: 201 });
}
if (options.method === 'POST') {
assert(url.endsWith('/-/link/muSRFlow'));
const packageName = url.split('/generic/')[1].split('/')[0];
if (linkedPackages.has(packageName)) return new Response(null, { status: 400 });
linkedPackages.add(packageName);
return new Response(null, { status: 201 });
}
if (url.includes('/api/v1/packages/')) {
assert(linkedPackages.has(url.split('/generic/')[1].split('/')[0]));
return Response.json({ repository: { full_name: 'owner/muSRFlow' } });
}
assert(stored.has(url));
return new Response(stored.get(url));
}
try {
const runEnv = { ...env, ACTIONS_TOKEN: 'actions-token' };
let run = { path: '.gitea/workflows/build-and-package.yml', conclusion: 'success' };
let artifacts = [{ id: 456, name: 'musrflow-linux-packages', expired: false }];
const apiCalls = [];
async function actionsApi(url, options) {
apiCalls.push(url);
assert.strictEqual(options.headers.Authorization, 'token actions-token');
assert.strictEqual(options.redirect, 'error');
assert(url.startsWith('https://gitea.example.test/api/v1/repos/owner/muSRFlow/actions/runs/123'));
return Response.json(url.includes('/artifacts?name=') ? { artifacts } : run);
}
assert.strictEqual((await checkPackageRun(runEnv, actionsApi)).id, 456);
run.conclusion = 'failure';
assert.strictEqual((await checkPackageRun(runEnv, actionsApi)).id, 456, 'registry failures must be recoverable without rebuilding');
run.path = 'build-and-package.yml@refs/heads/master';
assert.strictEqual((await checkPackageRun(runEnv, actionsApi)).id, 456);
run.path = '.gitea/workflows/build-runtime.yml';
await assert.rejects(checkPackageRun(runEnv, actionsApi), /not Build and package/);
assert(apiCalls[apiCalls.length - 1].endsWith('/123'), 'reject the wrong workflow before requesting artifacts');
run.path = '.gitea/workflows/build-and-package.yml';
for (const conclusion of ['', 'cancelled', 'skipped']) {
run.conclusion = conclusion;
await assert.rejects(checkPackageRun(runEnv, actionsApi), /must finish/);
}
for (const conclusion of ['success', 'failure']) {
run.conclusion = conclusion;
for (const missingArtifacts of [[], [{ name: 'musrflow-runtime-linux-x64' }],
[{ name: 'musrflow-linux-packages' }, { name: 'musrflow-linux-packages' }]]) {
artifacts = missingArtifacts;
await assert.rejects(checkPackageRun(runEnv, actionsApi), /must contain one/);
}
}
artifacts = [{ name: 'musrflow-linux-packages', expired: true }];
await assert.rejects(checkPackageRun(runEnv, actionsApi), /expired/);
artifacts = [{ name: 'musrflow-linux-packages', expires_at: '2000-01-01T00:00:00Z' }];
await assert.rejects(checkPackageRun(runEnv, actionsApi), /expired/);
await assert.rejects(checkPackageRun(runEnv, async () => new Response(null, { status: 403 })), /inspect packaging run: HTTP 403/);
await assert.rejects(checkPackageRun(runEnv, async url => url.endsWith('/123')
? Response.json(run) : new Response(null, { status: 403 })), /inspect package artifacts: HTTP 403/);
const callCount = apiCalls.length;
await assert.rejects(checkPackageRun({ ...runEnv, PACKAGE_RUN_ID: '../123' }, actionsApi), /numeric/);
await assert.rejects(checkPackageRun({ ...runEnv, ACTIONS_TOKEN: '' }, actionsApi), /read token/);
await assert.rejects(checkPackageRun({ ...runEnv, GITEA_SERVER_URL: 'http://example.test' }, actionsApi), /HTTPS/);
assert.strictEqual(apiCalls.length, callCount);
run = { path: '.gitea/workflows/build-windows.yml', conclusion: 'success' };
artifacts = [{ id: 789, name: 'musrflow-windows-x64', expired: false }];
assert.strictEqual((await checkPackageRun({ ...runEnv, PACKAGE_PLATFORM: 'windows' }, actionsApi)).id, 789);
assert(apiCalls.at(-1).endsWith('/artifacts?name=musrflow-windows-x64'));
run.conclusion = 'failure';
assert.strictEqual((await checkPackageRun({ ...runEnv, PACKAGE_PLATFORM: 'windows' }, actionsApi)).id, 789);
await assert.rejects(checkPackageRun(runEnv, actionsApi), /not Build and package/);
run.path = '.gitea/workflows/build-and-package.yml';
await assert.rejects(checkPackageRun({ ...runEnv, PACKAGE_PLATFORM: 'windows' }, actionsApi), /not Build Windows installer/);
await assert.rejects(checkPackageRun({ ...runEnv, PACKAGE_PLATFORM: 'macos' }, actionsApi), /package platform/);
const names = ['musrflow-9.8.7-x86_64.rpm', 'muSRFlow-9.8.7-x86_64.AppImage',
'musrflow-runtime-9.8.7-x86_64.rpm', 'musrflow-runtime-9.8.7-x86_64.AppImage'];
for (const name of names) {
fs.writeFileSync(path.join(directory, name), name);
for (const algorithm of ['md5', 'sha256']) {
const sum = crypto.createHash(algorithm).update(name).digest('hex');
fs.writeFileSync(path.join(directory, `${name}.${algorithm}`), `${sum} ${name}\n`);
}
}
fs.writeFileSync(path.join(directory, 'runtime-manifest.json'), '{}\n');
fs.writeFileSync(path.join(directory, 'build-source.txt'), 'muSRFlow=test\n');
// Unrelated artifact files must not be accidentally published.
fs.writeFileSync(path.join(directory, 'private.txt'), 'do not publish');
const missing = path.join(directory, names[3]);
fs.renameSync(missing, missing + '.backup');
await assert.rejects(publishPackages(directory, env, registry), /ENOENT/);
assert.strictEqual(calls.length, 0);
fs.renameSync(missing + '.backup', missing);
fs.appendFileSync(missing, 'corruption');
await assert.rejects(publishPackages(directory, env, registry), /Checksum mismatch/);
assert.strictEqual(calls.length, 0);
fs.writeFileSync(missing, names[3]);
await assert.rejects(publishPackages(directory, { ...env, PACKAGE_RUN_ID: '../123' }, registry), /run ID/);
await assert.rejects(publishPackages(directory, { ...env, PACKAGES_TOKEN: '' }, registry), /write token/);
await assert.rejects(publishPackages(directory, { ...env, GITEA_SERVER_URL: 'http://example.test' }, registry), /HTTPS/);
assert.strictEqual(calls.length, 0);
assert.strictEqual(await publishPackages(directory, env, registry), '9.8.7-build.123');
assert.strictEqual(stored.size, 15);
assert.strictEqual(calls.filter(call => call.method === 'POST').length, 2);
assert([...stored.keys()].every(url => url.includes('/9.8.7-build.123/')));
assert(![...stored.keys()].some(url => url.includes('private.txt')));
// An interrupted publish may be retried without deleting existing packages.
await publishPackages(directory, env, registry);
assert.strictEqual(calls.filter(call => call.method === 'GET' && !call.url.includes('/api/v1/')).length, 15);
assert.strictEqual(calls.filter(call => call.url.includes('/api/v1/packages/') && call.method === 'GET').length, 2);
// Repository links apply to the package, not just one version. A new
// build must also succeed when the previous build already linked it.
assert.strictEqual(await publishPackages(directory, { ...env, PACKAGE_RUN_ID: '124' }, registry), '9.8.7-build.124');
for (const metadata of [
{ status: 200, body: { repository: { full_name: 'owner/another-repo' } }, error: /expected owner\/muSRFlow/ },
{ status: 200, body: { repository: null }, error: /not linked/ },
{ status: 403, body: {}, error: /cannot verify existing repository link: HTTP 403/ }
]) {
await assert.rejects(publishPackages(directory, env, async (url, options) => {
for await (const chunk of options.body || []) { /* consume upload */ }
if (options.method === 'PUT') return new Response(null, { status: 201 });
if (options.method === 'POST') return new Response(null, { status: 400 });
return Response.json(metadata.body, { status: metadata.status });
}), metadata.error);
}
stored.set(stored.keys().next().value, Buffer.from('different published build'));
await assert.rejects(publishPackages(directory, env, registry), /Existing package differs/);
await assert.rejects(publishPackages(directory, env, async () => new Response(null, { status: 403 })), /HTTP 403/);
await assert.rejects(publishPackages(directory, env, async (url, options) => {
for await (const chunk of options.body || []) { /* consume upload */ }
return new Response(null, { status: options.method === 'POST' ? 403 : 201 });
}), /Link musrflow to repository: HTTP 403/);
assert(!calls.some(call => call.method === 'DELETE'));
// Recovery uses the artifact's release identity, not a newer checkout
// or the ref on which Publish packages was manually dispatched.
const context = { schema: 1, tag: 'v9.8.7', version: '9.8.7', commit: 'a'.repeat(40), runId: '123' };
const contextFile = path.join(directory, 'release-context.json');
fs.writeFileSync(path.join(directory, 'build-source.txt'), `muSRFlow=${context.commit}\n`);
fs.writeFileSync(contextFile, JSON.stringify(context));
const releaseEnv = { ...env, GITEA_REF: 'refs/heads/master', GITEA_EVENT_NAME: 'workflow_dispatch' };
assert.strictEqual(await publishPackages(directory, releaseEnv, registry), '9.8.7');
assert([...stored.keys()].some(url => url.endsWith('/9.8.7/release-context.json')));
await publishPackages(directory, releaseEnv, registry);
const beforeInvalid = calls.length;
await assert.rejects(publishPackages(directory, { ...env, PACKAGE_RUN_ID: '999' }, registry), /run ID mismatch/);
fs.writeFileSync(contextFile, JSON.stringify({ ...context, version: '9.8.8', tag: 'v9.8.8' }));
await assert.rejects(publishPackages(directory, env, registry), /filenames do not match/);
assert.strictEqual(calls.length, beforeInvalid);
fs.writeFileSync(contextFile, JSON.stringify({ ...context, tag: null, runId: '125' }));
assert.strictEqual(await publishPackages(directory, { ...env, PACKAGE_RUN_ID: '125' }, registry), '9.8.7-build.125');
const windowsDirectory = path.join(directory, 'windows');
fs.mkdirSync(windowsDirectory);
const installer = 'muSRFlow-9.8.7-x64.exe';
fs.writeFileSync(path.join(windowsDirectory, installer), 'MZinstaller');
for (const algorithm of ['md5', 'sha256']) {
const sum = crypto.createHash(algorithm).update('MZinstaller').digest('hex');
fs.writeFileSync(path.join(windowsDirectory, `${installer}.${algorithm}`), `${sum} ${installer}\n`);
}
fs.writeFileSync(path.join(windowsDirectory, 'build-source.txt'), `muSRFlow=${context.commit}\n`);
fs.writeFileSync(path.join(windowsDirectory, 'runtime-manifest.json'), '{}\n');
fs.writeFileSync(path.join(windowsDirectory, 'wsl-rootfs.sha256'), `${'a'.repeat(64)} muSRFlow-runtime-rootfs.tar.gz\n`);
const windowsEnv = { ...env, PACKAGE_PLATFORM: 'windows' };
assert.strictEqual(await publishPackages(windowsDirectory, windowsEnv, registry), '9.8.7-build.123');
const windowsContext = path.join(windowsDirectory, 'release-context.json');
fs.writeFileSync(windowsContext, JSON.stringify(context));
const beforeWindows = calls.length;
assert.strictEqual(await publishPackages(windowsDirectory, windowsEnv, registry), '9.8.7');
const uploads = calls.slice(beforeWindows).filter(call => call.method === 'PUT');
assert.strictEqual(uploads.length, 7);
assert(uploads.every(call => call.url.includes('/generic/musrflow-windows/9.8.7/')));
assert(uploads.at(-1).url.endsWith('/release-context.json'), 'provenance marks completed publication');
await publishPackages(windowsDirectory, windowsEnv, registry);
const beforeCorrupt = calls.length;
fs.appendFileSync(path.join(windowsDirectory, installer), 'corrupted');
await assert.rejects(publishPackages(windowsDirectory, windowsEnv, registry), /Checksum mismatch/);
assert.strictEqual(calls.length, beforeCorrupt, 'validate before uploading');
fs.writeFileSync(path.join(windowsDirectory, installer), 'MZinstaller');
fs.writeFileSync(windowsContext, JSON.stringify({ ...context, tag: 'v9.8.8', version: '9.8.8' }));
await assert.rejects(publishPackages(windowsDirectory, windowsEnv, registry), /filenames do not match/);
fs.unlinkSync(windowsContext);
// The previously tested manual installer remains recoverable too.
fs.renameSync(path.join(windowsDirectory, 'build-source.txt'), path.join(windowsDirectory, 'windows-build-source.txt'));
assert.strictEqual(await publishPackages(windowsDirectory, { ...windowsEnv, PACKAGE_RUN_ID: '126' }, registry), '9.8.7-build.126');
fs.unlinkSync(path.join(windowsDirectory, 'wsl-rootfs.sha256'));
const beforeMissing = calls.length;
await assert.rejects(publishPackages(windowsDirectory, windowsEnv, registry), /ENOENT/);
assert.strictEqual(calls.length, beforeMissing);
console.log('Gitea package publishing tests passed');
} finally {
fs.rmSync(directory, { recursive: true, force: true });
}
}
main().catch(error => { console.error(error); process.exitCode = 1; });