e2e-setup-browser.sh signs a user with no organisation into the real web console, creates a farm, copies the gateway settings the page shows, connects a stand-in gateway configured from exactly those settings, adds a printer through the form, gets a failed-print alert and dismisses it, then removes the printer. Afterwards it searches every table and the backend log for the access code the browser typed. No printer is touched and dispatch is off. The e2e scripts that start the real proxy also give it its own store file in the scratch directory.
59 lines
2.5 KiB
TypeScript
59 lines
2.5 KiB
TypeScript
/**
|
|
* A stand-in gateway that stays connected and answers printer provisioning,
|
|
* for scripts/e2e-provision.sh.
|
|
*
|
|
* With TELEMETRY_AFTER_PROVISION=1 it also reports each provisioned printer
|
|
* (running, then failed a few seconds later), so the console has something to
|
|
* alert on.
|
|
*
|
|
* Needs FARM_ID in the environment (the hello must name the token's farm).
|
|
*
|
|
* Usage: bun scripts/fake-gateway-serve.ts <ws-url> <edge-jwt> <seconds> <frames-file> [refuse-access-code]
|
|
*
|
|
* Every provision_printer / remove_printer frame it receives is appended to
|
|
* <frames-file> as one JSON line, so the script can prove the credential
|
|
* arrived here. A provision whose access_code equals <refuse-access-code> is
|
|
* answered as a failure, like a printer that rejected the login.
|
|
*/
|
|
import { appendFileSync } from "node:fs";
|
|
|
|
const [url, jwt, seconds, framesFile, refuseCode] = process.argv.slice(2);
|
|
if (!url || !jwt || !seconds || !framesFile) {
|
|
console.error("usage: fake-gateway-serve.ts <ws-url> <edge-jwt> <seconds> <frames-file> [refuse-code]");
|
|
process.exit(2);
|
|
}
|
|
|
|
const ws = new WebSocket(url, { headers: { authorization: `Bearer ${jwt}` } } as never);
|
|
ws.onerror = () => {
|
|
console.error("socket error");
|
|
process.exit(1);
|
|
};
|
|
ws.onopen = () => {
|
|
// farm_id is required by the hello schema; the backend checks it against the token's farm.
|
|
ws.send(JSON.stringify({ type: "hello", gateway_id: "fake-gw", farm_id: process.env.FARM_ID }));
|
|
console.log("ready");
|
|
};
|
|
ws.onmessage = (event) => {
|
|
const message = JSON.parse(String(event.data));
|
|
if (message.type !== "provision_printer" && message.type !== "remove_printer") return;
|
|
appendFileSync(framesFile, JSON.stringify(message) + "\n");
|
|
if (message.type === "provision_printer" && process.env.TELEMETRY_AFTER_PROVISION === "1") {
|
|
const id = message.printer.id;
|
|
const report = (state: string) =>
|
|
ws.send(JSON.stringify({ type: "telemetry", printers: [{ printer_id: id, name: message.printer.name, state, percent: 40, print_error: 0 }] }));
|
|
setTimeout(() => report("RUNNING"), 1500);
|
|
setTimeout(() => report("RUNNING"), 3000);
|
|
setTimeout(() => report("FAILED"), 4500);
|
|
}
|
|
const refused = message.type === "provision_printer" && refuseCode && message.printer.access_code === refuseCode;
|
|
ws.send(
|
|
JSON.stringify({
|
|
type: "command_result",
|
|
command_id: message.request_id,
|
|
ok: !refused,
|
|
...(refused ? { error: "could not connect to the printer: the access code was rejected" } : {}),
|
|
}),
|
|
);
|
|
};
|
|
setTimeout(() => process.exit(0), Number(seconds) * 1000);
|