test: verify lost launch replies and reload recovery in a scratch browser
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
// Invoked by the Go scratch-server test, never against the deployed service.
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { launchChrome } from "./browser.mjs";
|
||||
|
||||
const url = process.argv[2];
|
||||
@@ -12,6 +14,10 @@ try {
|
||||
assert.equal(await page.evaluate("document.title"), "Safe report");
|
||||
assert.equal(await page.evaluate("document.querySelector('#report').textContent"), "Readable report");
|
||||
assert.equal(await page.evaluate("getComputedStyle(document.querySelector('#report')).color"), "rgb(12, 34, 56)");
|
||||
if (process.env.PIPELINES_CAPTURE_DIR) {
|
||||
await mkdir(process.env.PIPELINES_CAPTURE_DIR, { recursive: true });
|
||||
await page.screenshot(join(process.env.PIPELINES_CAPTURE_DIR, "artifact-script-blocked.png"));
|
||||
}
|
||||
// The inline attack would change the title and call /attack if it ran. The
|
||||
// server-side test also asserts that endpoint received no request.
|
||||
console.log("Raw HTML report is readable; inline script blocked by response policy.");
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
// 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));
|
||||
}
|
||||
Reference in New Issue
Block a user