test: verify lost launch replies and reload recovery in a scratch browser

This commit is contained in:
2026-10-20 15:44:23 +01:00
parent 44834e91f0
commit e4f48ecbb1
4 changed files with 148 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
package server
import (
"context"
"fmt"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/stage"
)
func TestRunAdmissionBrowserLostResponse(t *testing.T) {
if os.Getenv("PIPELINES_BROWSER_TEST") != "1" {
t.Skip("set PIPELINES_BROWSER_TEST=1 for the scratch Chrome check")
}
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "scratch.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "Scratch", pipeline.Template([]string{"work"}, true)); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
handler := Handler(ctx, store).(*runHandler)
defer func() { cancel(); WaitRuns(handler) }()
handler.runner.execute = func(ctx context.Context, _ stage.Definition, _ stage.Values) (stage.Result, error) {
<-ctx.Done()
return stage.Result{}, ctx.Err()
}
srv := httptest.NewServer(handler)
defer srv.Close()
cmd := exec.CommandContext(ctx, "node", "../../web/test/run-admission-probe.mjs", strings.TrimPrefix(srv.URL, "http://"), fmt.Sprint(items[0].ID), t.TempDir())
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("lost ack browser regression: %v\n%s", err, output)
} else {
t.Log(string(output))
}
runs, err := store.ListRuns(ctx)
if err != nil || len(runs) != 1 {
t.Fatalf("lost response duplicated run: %d %v", len(runs), err)
}
}
+2
View File
@@ -455,6 +455,7 @@ async function startRun() {
const run = existing ?? await repo.runPipeline(start.intent);
clearRunIntent(globalThis.sessionStorage, state.url, start.intent);
state.runStart = null;
pushRoute({ kind: "run", id: run.id });
await applyRoute({ kind: "run", id: run.id });
} catch (error) {
if (state.runStart !== start) return;
@@ -466,6 +467,7 @@ async function startRun() {
if (existing) {
clearRunIntent(globalThis.sessionStorage, state.url, start.intent);
state.runStart = null;
pushRoute({ kind: "run", id: existing.id });
await applyRoute({ kind: "run", id: existing.id });
return;
}
+6
View File
@@ -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.");
+89
View File
@@ -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));
}