import test from 'node:test'; import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; const workflow = readFileSync(new URL('../workflows/web-deploy.yml', import.meta.url), 'utf8'); const steps = new Map( workflow.split(/^ - name: /m).slice(1).map(chunk => { const lineEnd = chunk.indexOf('\n'); return [chunk.slice(0, lineEnd).trim(), chunk.slice(lineEnd + 1)]; }) ); const production = "github.repository == 'unimatrix/web' && github.ref == 'refs/heads/main'"; function step(name) { assert.ok(steps.has(name), 'Expected workflow step: ' + name); return steps.get(name); } test('development can dispatch a build and production still builds on main pushes', () => { assert.match(workflow, /^ workflow_dispatch:\s*$/m); assert.match(workflow, /^ push:\r?\n branches:\r?\n - main\r?$/m); assert.equal(workflow.match(/^ if: (.+)$/m)?.[1].trim(), "github.repository == 'unimatrix/web' || (github.repository == 'dev/MC' && github.event_name == 'workflow_dispatch')"); for (const name of ['Verify deployment policy', 'Build and test website']) { assert.doesNotMatch(step(name), /^ if:/m); } }); test('checkout fetches the exact event commit from the repository running the workflow', () => { const checkout = step('Check out exact revision'); assert.match(checkout, /fetch --depth=1 "https:\/\/www\.dunerealms\.com\/git\/\$\{GITHUB_REPOSITORY\}\.git" "\$GITHUB_SHA"/); assert.match(checkout, /git checkout --detach FETCH_HEAD\r?\n\s+test "\$\(git rev-parse HEAD\)" = "\$GITHUB_SHA"/); assert.doesNotMatch(checkout, /unimatrix\/web/); }); test('publication comparison, artifact upload and deployment require canonical main', () => { for (const name of ['Prepare publication comparison', 'Upload website build', 'Trigger deployment after successful CI']) { assert.equal(step(name).match(/^ if: (.+)$/m)?.[1].trim(), production); } assert.match(step('Prepare publication comparison'), /publication-changes\.mjs "\$BEFORE" "\$GITHUB_SHA"/); assert.match(step('Upload website build'), /uses: actions\/upload-artifact@v3/); assert.match(step('Trigger deployment after successful CI'), /StrictHostKeyChecking=yes[^\n]+"\$GITHUB_SHA"/); for (const [name, body] of steps) { if (name !== 'Trigger deployment after successful CI') assert.doesNotMatch(body, /DUNEREALMS_DEPLOY_KEY|ssh .*dunedeploy@/); } });