sokko-g/tests/deployment-workflow.test.mjs
Shinuwa 067a4a8fd6
Some checks failed
Validate and deploy Sokko G / validate (push) Failing after 19m40s
Validate and deploy Sokko G / deploy (push) Has been skipped
Fix the audit's concurrency, stale state and delivery findings
Share uploads now reserve their slot, bytes and per-IP count before the
disk check, so simultaneous uploads can no longer exceed the limits.
Writes to a share's metadata run one after another, and a revocation can
no longer be undone by a pending read count.

The TCG page ignores a state reload that started before or during a
mutation, the last mutation wins, and returning to the page triggers a
single reload. The card cache confirms writes only once the IndexedDB
transaction completes and shows only the current player's copy. The
administration panel loads on demand, which shrinks the TCG chunk from
311.6 to 191.8 kB.

A new /api/health route answers 503 when the cards or shares API failed
to start. The deploy script ships the commit's production dependencies
with the build, waits up to 30 s for a healthy server instead of trusting
systemctl is-active, and restores node_modules on rollback. Both Forgejo
workflows check the route on the public site. The updated script applies
once reinstalled on the server.

Unused helpers, the ignored join role and duplicate button selectors are
removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 21:38:20 +01:00

150 lines
7.9 KiB
JavaScript

// Rôle : vérifie que le workflow de livraison conserve ses barrières de validation et de provenance.
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { readFile } from "node:fs/promises";
import { test } from "node:test";
import { promisify } from "node:util";
const deployWorkflowPath = ".forgejo/workflows/deploy-prod.yml";
const packageLockPath = "package-lock.json";
const remoteScriptPath = "scripts/deploy-sokko-g.sh";
const servicePath = "ops/systemd/sokko-g.service";
const validationWorkflowPath = ".forgejo/workflows/validate-main.yml";
const execFileAsync = promisify(execFile);
test("main pushes validate then automatically deploy the same current commit", async () => {
const workflow = await readFile(validationWorkflowPath, "utf8");
const validateJob = workflow.match(/ {2}validate:\n[\s\S]*?(?=\n {2}deploy:)/)?.[0] || "";
assert.ok(validateJob);
assert.match(workflow, /push:\s+[\s\S]*branches:\s+[\s\S]*- main/);
assert.match(workflow, /ref: \$\{\{ gitea\.sha \}\}/);
assert.match(workflow, /container: "mcr\.microsoft\.com\/playwright:v1\.62\.1-noble@sha256:[0-9a-f]{64}"/);
assert.match(workflow, /npm ci/);
assert.match(workflow, /npm run check:full/);
assert.match(workflow, /deploy:\s+[\s\S]*needs: validate/);
assert.match(workflow, /DEPLOY_COMMIT: \$\{\{ needs\.validate\.outputs\.commit \}\}/);
assert.match(workflow, />> "\$GITHUB_OUTPUT"/);
assert.match(workflow, /steps\.freshness\.outputs\.deploy == 'true'/);
assert.match(workflow, /current_commit[\s\S]*DEPLOY_COMMIT/);
assert.match(workflow, /deploy-contract/);
assert.match(workflow, /deploy-sokko-g --commit/);
assert.match(workflow, /\[webapps\.shinuwa\.fr\]:2911/);
assert.match(workflow, /shinuwa@webapps\.shinuwa\.fr/);
assert.match(workflow, /StrictHostKeyChecking=yes/);
assert.doesNotMatch(workflow, /workflow_dispatch:/);
assert.doesNotMatch(workflow, /StrictHostKeyChecking=no/);
assert.doesNotMatch(workflow, /192\.168\.0\.100/);
assert.doesNotMatch(workflow, /GITEA_OUTPUT/);
assert.doesNotMatch(validateJob, /secrets\./);
assert.doesNotMatch(validateJob, /\bssh\b/);
});
test("production deployment requires a validated exact commit and pinned SSH host", async () => {
const workflow = await readFile(deployWorkflowPath, "utf8");
assert.match(workflow, /workflow_dispatch:\s+[\s\S]*commit:/);
assert.doesNotMatch(workflow, /push:/);
assert.match(workflow, /REQUESTED_COMMIT[\s\S]*\^\[0-9a-f\]\{40\}\$/);
assert.match(workflow, /deploy:\s+[\s\S]*needs: validate/);
assert.match(workflow, /validate:\s+[\s\S]*container: "mcr\.microsoft\.com\/playwright:v1\.62\.1-noble@sha256:[0-9a-f]{64}"/);
assert.match(workflow, /npm ci/);
assert.match(workflow, /npm run check:full/);
assert.match(workflow, /git merge-base --is-ancestor "\$commit" origin\/main/);
assert.match(workflow, />> "\$GITHUB_OUTPUT"/);
assert.match(workflow, /DEPLOY_COMMIT: \$\{\{ needs\.validate\.outputs\.commit \}\}/);
assert.match(workflow, /deploy-contract/);
assert.match(workflow, /deploy-sokko-g --commit/);
assert.match(workflow, /\[webapps\.shinuwa\.fr\]:2911/);
assert.match(workflow, /shinuwa@webapps\.shinuwa\.fr/);
assert.match(workflow, /StrictHostKeyChecking=yes/);
assert.doesNotMatch(workflow, /StrictHostKeyChecking=no/);
assert.doesNotMatch(workflow, /192\.168\.0\.100/);
assert.doesNotMatch(workflow, /GITEA_OUTPUT/);
assert.doesNotMatch(workflow, /\$\{\{\s*forgejo\./);
});
test("validation containers match the locked Playwright release", async () => {
const [lockContents, validationWorkflow, deployWorkflow] = await Promise.all([
readFile(packageLockPath, "utf8"),
readFile(validationWorkflowPath, "utf8"),
readFile(deployWorkflowPath, "utf8")
]);
const lock = JSON.parse(lockContents);
const playwrightVersion = lock.packages["node_modules/@playwright/test"].version;
const expectedImage = `mcr.microsoft.com/playwright:v${playwrightVersion}-noble@sha256:`;
assert.ok(validationWorkflow.includes(expectedImage));
assert.ok(deployWorkflow.includes(expectedImage));
assert.doesNotMatch(validationWorkflow, /playwright install --with-deps/);
assert.doesNotMatch(deployWorkflow, /playwright install --with-deps/);
});
test("the versioned remote script builds and activates only the requested commit", async () => {
await execFileAsync("bash", ["-n", remoteScriptPath]);
const script = await readFile(remoteScriptPath, "utf8");
assert.match(script, /\$1 == "--commit"/);
assert.match(script, /\^\[0-9a-f\]\{40\}\$/);
assert.match(script, /flock --wait 900/);
assert.match(script, /merge-base --is-ancestor/);
assert.match(script, /worktree add --detach/);
assert.match(script, /env PATH="\$NODE_HOME\/bin:\/usr\/bin:\/bin" "\$NPM_BIN" --version/);
assert.match(script, /"\$NPM_BIN" --prefix "\$worktree" ci/);
assert.match(script, /"\$NPM_BIN" --prefix "\$worktree" run build/);
assert.match(script, /checkout --detach "\$requested_commit"/);
assert.match(script, /write_state previous-commit/);
assert.match(script, /systemctl is-active --quiet/);
assert.match(script, /systemctl show --property=ExecStart/);
assert.doesNotMatch(script, /git\s+pull/);
});
// Le service ne doit jamais tourner avec les dépendances d'une livraison précédente, ni être déclaré livré sans avoir démarré.
test("the remote script ships the commit's dependencies and waits for a healthy server", async () => {
const script = await readFile(remoteScriptPath, "utf8");
assert.match(script, /"\$NPM_BIN" --prefix "\$worktree" prune --omit=dev/);
assert.match(script, /as_app_user mv "\$APP_ROOT\/node_modules" "\$backup_node_modules"/);
assert.match(script, /as_app_user mv "\$worktree\/node_modules" "\$APP_ROOT\/node_modules"/);
assert.match(script, /as_app_user mv "\$backup_node_modules" "\$APP_ROOT\/node_modules"/);
assert.match(script, /\/api\/health/);
assert.match(script, /systemctl restart "\$SERVICE_NAME" \|\| ! wait_until_healthy/);
for (const workflow of [validationWorkflowPath, deployWorkflowPath]) {
assert.match(await readFile(workflow, "utf8"), /sokkog\.shinuwa\.fr\/api\/health \| grep -Fq '"ok":true'/);
}
});
test("the production service uses the same pinned Node runtime", async () => {
const service = await readFile(servicePath, "utf8");
assert.match(service, /ExecStart=\/home\/shinuwa\/\.nvm\/versions\/node\/v24\.11\.1\/bin\/npm start/);
assert.match(service, /Environment=PATH=\/home\/shinuwa\/\.nvm\/versions\/node\/v24\.11\.1\/bin:/);
assert.doesNotMatch(service, /v26\.3\.0/);
});
test("the remote script never runs files owned by the service account as root", async () => {
const script = await readFile(remoteScriptPath, "utf8");
assert.match(script, /as_app_user "\$NODE_BIN" --version/);
assert.match(script, /as_app_user env PATH="\$NODE_HOME\/bin:\/usr\/bin:\/bin" "\$NPM_BIN" --version/);
assert.doesNotMatch(script, /\$\(\$NODE_BIN/);
assert.doesNotMatch(script, /\$\(env PATH=/);
assert.doesNotMatch(script, /chown -R/);
assert.doesNotMatch(script, /^\s*(?:mv .*(?:\$APP_ROOT|\$worktree|\$backup_dist|\$backup_node_modules)|rm -rf -- "\$APP_ROOT)/m);
});
test("the services cannot gain privileges through sudo", async () => {
for (const path of [servicePath, "ops/systemd/sokko-g-cards-backup.service"]) {
assert.match(await readFile(path, "utf8"), /^NoNewPrivileges=true$/m);
}
});
test("deployment secrets stay in one step and actions are pinned by commit", async () => {
for (const path of [deployWorkflowPath, validationWorkflowPath]) {
const workflow = await readFile(path, "utf8");
assert.equal(workflow.match(/secrets\.DEPLOY_SSH_KEY/g)?.length, 1);
assert.doesNotMatch(workflow.match(/ {2}deploy:\n[\s\S]*?\n {4}steps:/)?.[0] || "", /secrets\./);
assert.match(workflow, /- name: Configure the pinned production host key\n(?: {8}if: .*\n)? {8}env:\n {10}DEPLOY_SSH_KEY: \$\{\{ secrets\.DEPLOY_SSH_KEY \}\}/);
for (const [, reference] of workflow.matchAll(/uses: \S+@(\S+)/g)) assert.match(reference, /^[0-9a-f]{40}$/);
}
});