// static/agents.js without a browser: the gates Save sends from the fetch // gate's four parts, and the stored gates the parts cannot say, which the // form names in a note and sends back as they came. // // node --test tests/js/*.test.mjs import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import vm from "node:vm"; // Only what agents.js touches before DOMContentLoaded. const window = { api: async () => [], count: (n, noun) => `${n} ${noun}` }; const document = { addEventListener() {} }; Object.assign(globalThis, { window, document }); for (const file of ["remote.js", "agents.js"]) { vm.runInThisContext(readFileSync(new URL(`../../app/static/${file}`, import.meta.url), "utf8")); } const rh = window.remoteHelpers; const { formGates, keptNote, rulesLine } = window.agentGates; /** The four parts as the form shows them after loading `gates`, and what it keeps. */ function load(gates) { const { where, rest } = rh.distribute(gates); const parts = { scope: where.scope.kind, scoped: where.scope.values, within: where.countries ? where.countries.values : [], towns: where.places ? where.places.values : [], remote: where.remote.mode, from: where.remote.values, titles: where.titles ? where.titles.values : [], }; return { parts, rest }; } const europe = { field: "country", values: rh.europeWith(["DK"]), mode: "require" }; const dk = { field: "country", values: ["DK"], mode: "any" }; const remote = { field: "remote", values: ["remote"], mode: "any" }; const towns = { field: "place", values: ["Malmö", "Lund"], mode: "any" }; const titles = { field: "title", values: ["account executive", "sales"], mode: "exclude" }; const level = { field: "seniority", values: ["senior", "staff"], mode: "require" }; const acme = { field: "company", values: ["acme"], mode: "exclude" }; test("a save sends the four parts back as they were loaded", () => { const agents = [ [europe, dk, remote, towns, titles], [{ field: "country", values: ["DK", "SE"], mode: "require" }, { field: "remote_from", values: ["country:DK", "country:EU"], mode: "any" }, titles], [dk], [], ]; for (const gates of agents) { const { parts, rest } = load(gates); assert.deepEqual(rest, []); assert.deepEqual(formGates(parts, rest), gates); } }); test("each part sends its own gate, and an empty part none", () => { const parts = { scope: "everywhere", scoped: [], within: [], towns: [], remote: "none", from: [], titles: [] }; assert.deepEqual(formGates(parts), []); // A remote line open to no area yet, or chosen countries with none picked, say nothing. assert.deepEqual(formGates({ ...parts, remote: "from", scope: "countries" }), []); assert.deepEqual(formGates({ ...parts, scope: "europe", within: ["US"] }), [ { field: "country", values: rh.europeWith(["US"]), mode: "require" }, { field: "country", values: ["US"], mode: "any" }, ]); }); test("a stored gate the parts cannot say is kept across a save, named in the note and on the card", () => { const gates = [europe, dk, remote, titles, level, acme]; const { parts, rest } = load(gates); assert.deepEqual(rest, [level, acme]); assert.deepEqual(formGates(parts, rest), gates); assert.equal(keptNote(rest), "The form cannot edit these rules, and Save keeps them as stored: " + "must: with seniority senior, staff; not: at acme."); const words = rulesLine(gates); assert.match(words, /Only roles with seniority senior, staff\./); assert.match(words, /Leaves out roles at acme\./); }); test("an agent with nothing to keep has no note", () => { assert.equal(keptNote([]), ""); assert.equal(keptNote([level]), "The form cannot edit this rule, and Save keeps it as stored: must: with seniority senior, staff."); });