Files
pipelines/web/test/run-admission-probe.mjs
T

90 lines
4.0 KiB
JavaScript

// A real-page lost-ack regression driven only by a Go httptest server. The Go
// fixture supplies a fake executor and checks that precisely one run exists.
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import { mkdir } from "node:fs/promises";
import { join } from "node:path";
import net from "node:net";
import { fileURLToPath } from "node:url";
import { eventually, launchChrome } from "./browser.mjs";
const [target, pipelineId, workspace] = process.argv.slice(2);
assert.match(target ?? "", /^127\.0\.0\.1:\d+$/);
const reserve = net.createServer();
await new Promise((resolve) => reserve.listen(0, "127.0.0.1", resolve));
const port = reserve.address().port;
await new Promise((resolve) => reserve.close(resolve));
const serve = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--pipelines", target], {
cwd: fileURLToPath(new URL("..", import.meta.url)),
env: { ...process.env, PIPELINES_WEB_AUTH: "disabled" },
stdio: ["ignore", "pipe", "pipe"],
});
let logs = "";
serve.stdout.on("data", (data) => { logs += data; });
serve.stderr.on("data", (data) => { logs += data; });
let browser;
try {
await eventually(async () => {
if (serve.exitCode != null) throw Error(logs);
return fetch(`http://127.0.0.1:${port}/`).then((r) => r.ok).catch(() => false);
});
browser = await launchChrome({ width: 390, height: 844 });
const page = await browser.openPage("about:blank");
await page.addInitScript(`(() => {
const Native = window.WebSocket;
window.WebSocket = class extends Native {
send(data) {
const frame = JSON.parse(data);
if (frame.type === 'run_pipeline') {
const keys = JSON.parse(sessionStorage.getItem('probe-launches') || '[]');
keys.push(frame.request_key);
sessionStorage.setItem('probe-launches', JSON.stringify(keys));
}
super.send(data);
}
set onmessage(handler) {
super.onmessage = (event) => {
const frame = JSON.parse(event.data);
if (frame.type === 'run_started' && !sessionStorage.getItem('probe-dropped')) {
sessionStorage.setItem('probe-dropped', 'yes');
this.close(4000, 'scratch lost ack');
return;
}
handler(event);
};
}
};
})()`);
const pipeline = `http://127.0.0.1:${port}/pipelines/${pipelineId}`;
await page.goto(pipeline);
await page.waitForSelector(".run-pipeline");
await page.click(".run-pipeline");
await page.waitForSelector("#runPrompt");
await page.fill("#runPrompt", "Scratch admission reconciliation");
await page.fill("#runWorkspace", workspace);
await page.click(".run-start-confirm");
await eventually(() => page.evaluate("sessionStorage.getItem('probe-dropped') === 'yes'"));
// Reload before any manual retry: the tab's launch identity must survive.
await page.goto(pipeline);
await page.waitForSelector(".run-pipeline");
await page.click(".run-pipeline");
await page.waitForSelector(".run-start-confirm");
assert.equal(await page.evaluate("document.querySelector('.run-start-confirm').textContent"), "Check / retry");
assert.equal(await page.evaluate("document.querySelector('#runPrompt').disabled"), true);
if (process.env.PIPELINES_CAPTURE_DIR) {
await mkdir(process.env.PIPELINES_CAPTURE_DIR, { recursive: true });
await page.screenshot(join(process.env.PIPELINES_CAPTURE_DIR, "uncertain-run-recovered.png"));
}
await page.click(".run-start-confirm");
await eventually(() => page.evaluate("/^\\/runs\\/\\d+$/.test(location.pathname)"));
assert.equal(await page.evaluate("JSON.parse(sessionStorage.getItem('probe-launches')).length"), 1);
if (process.env.PIPELINES_CAPTURE_DIR) {
await page.screenshot(join(process.env.PIPELINES_CAPTURE_DIR, "single-admitted-run.png"));
}
console.log("Lost launch acknowledgement + reload reconciled to the single admitted run.");
} finally {
await browser?.close();
serve.kill("SIGTERM");
await new Promise((resolve) => serve.exitCode != null ? resolve() : serve.once("exit", resolve));
}