// static/remote.js without a browser: the badge, the suggestions the agent's // remote line offers, and how gates share out between the fetch gate and the // rest the form keeps as stored. // // 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"; const window = {}; Object.assign(globalThis, { window }); vm.runInThisContext(readFileSync(new URL("../../app/static/remote.js", import.meta.url), "utf8")); const rh = window.remoteHelpers; const name = (code) => ({ DK: "Denmark", SE: "Sweden", DE: "Germany" })[code] || code; const label = (v) => rh.fallbackLabel(v, name); test("the badge says the area after remote, and remote alone when it is not stated", () => { assert.equal(rh.remoteBadge({ remote: "remote", remote_where: "Europe" }), "remote · Europe"); assert.equal(rh.remoteBadge({ remote: "remote", remote_where: "anywhere" }), "remote · anywhere"); assert.equal(rh.remoteBadge({ remote: "remote", remote_where: "United States, Canada" }), "remote · United States, Canada"); assert.equal(rh.remoteBadge({ remote: "remote", remote_where: "" }), "remote"); assert.equal(rh.remoteBadge({ remote: "remote" }), "remote"); }); test("the badge leaves the other words as they are, and a role with none blank", () => { assert.equal(rh.remoteBadge({ remote: "hybrid", remote_where: "Europe" }), "hybrid"); assert.equal(rh.remoteBadge({ remote: "onsite", remote_where: "" }), "onsite"); assert.equal(rh.remoteBadge({ remote: "" }), ""); assert.equal(rh.remoteBadge(null), ""); }); test("a suggestion is offered to the remote line as a country or a region only", () => { assert.equal(rh.fromSuggestion({ token: "country:DK", kind: "country" }), "country:DK"); assert.equal(rh.fromSuggestion({ token: "country:EU", kind: "country" }), "country:EU"); assert.equal(rh.fromSuggestion({ token: "region:oresund", kind: "region" }), "region:oresund"); assert.equal(rh.fromSuggestion({ token: "remote:any", kind: "remote" }), null); assert.equal(rh.fromSuggestion({ token: "remote:country:DK", kind: "remote" }), null); assert.equal(rh.fromSuggestion({ token: "place:1", kind: "place" }), null); }); test("a value has a label before the server has said it", () => { assert.equal(label("anywhere"), "Anywhere"); assert.equal(label("country:DK"), "Denmark"); assert.equal(label("country:EU"), "Europe"); assert.equal(label("region:oresund"), "oresund"); }); test("the sentence lists the areas, and says anywhere in lower case", () => { assert.equal(rh.remoteSentence(["country:DK", "country:SE", "country:DE"], label), "remote from Denmark, Sweden or Germany"); assert.equal(rh.remoteSentence(["anywhere"], label), "remote from anywhere"); assert.equal(rh.remoteSentence(["country:DK"], label), "remote from Denmark"); }); // ------------------------------------------------------------- distribute const older = { field: "remote", values: ["remote"], mode: "any" }; const from = (...values) => ({ field: "remote_from", values, mode: "any" }); const dk = { field: "country", values: ["DK"], mode: "any" }; const must = (...values) => ({ field: "country", values, mode: "require" }); const europe = must(...rh.EUROPE); test("no gates: everywhere, no remote", () => { const { where, rest } = rh.distribute([]); assert.deepEqual(where.scope, { kind: "everywhere", values: [] }); assert.deepEqual(where.remote, { mode: "none", values: [] }); assert.deepEqual(rest, []); }); test("the Europe list is the Europe scope", () => { const { where, rest } = rh.distribute([europe, dk, older]); assert.equal(where.scope.kind, "europe"); assert.equal(where.countries.values[0], "DK"); assert.deepEqual(rest, []); }); test("Europe with the countries line added is still the Europe scope", () => { const us = { field: "country", values: ["US"], mode: "any" }; const { where } = rh.distribute([must(...rh.europeWith(["US"])), us]); assert.equal(where.scope.kind, "europe"); // The same gate without the line's country is not exactly Europe: chips. assert.equal(rh.distribute([europe, us]).where.scope.kind, "countries"); }); test("any other require gate on country is the scope with its codes as chips", () => { const { where, rest } = rh.distribute([must("DK", "SE"), dk]); assert.deepEqual(where.scope, { kind: "countries", values: ["DK", "SE"] }); assert.deepEqual(rest, []); }); test("a second require gate on country is kept as stored", () => { const second = must("NO"); const { where, rest } = rh.distribute([europe, second]); assert.equal(where.scope.kind, "europe"); assert.deepEqual(rest, [second]); }); test("the titles to leave out are the exclude gate on title, and any other exclude is kept as stored", () => { const titles = { field: "title", values: ["account executive"], mode: "exclude" }; const level = { field: "seniority", values: ["junior"], mode: "exclude" }; const { where, rest } = rh.distribute([titles, level]); assert.equal(where.titles, titles); assert.deepEqual(rest, [level]); }); test("a gate with a mode the form does not know is kept as stored and takes no Where line", () => { const prefer = { field: "remote", values: ["hybrid"], mode: "prefer" }; const { where, rest } = rh.distribute([prefer]); assert.deepEqual(where.remote, { mode: "none", values: [] }); assert.equal("hybrid" in where, false); assert.deepEqual(rest, [prefer]); }); test("a require gate on another field is kept as stored and leaves the scope everywhere", () => { const level = { field: "seniority", values: ["senior"], mode: "require" }; const { where, rest } = rh.distribute([level]); assert.equal(where.scope.kind, "everywhere"); assert.deepEqual(rest, [level]); }); test("remote, three modes: the plain gate is within the scope", () => { const scope = rh.distribute([older]); assert.deepEqual(scope.where.remote, { mode: "scope", values: [] }); assert.deepEqual(scope.rest, []); assert.deepEqual(rh.distribute([]).where.remote, { mode: "none", values: [] }); const named = rh.distribute([from("country:DK", "region:oresund")]); assert.deepEqual(named.where.remote, { mode: "from", values: ["country:DK", "region:oresund"] }); assert.deepEqual(named.rest, []); }); test("a remote_from gate wins over the plain one, which is kept as stored", () => { for (const gates of [[older, from("country:DK")], [from("country:DK"), older]]) { const { where, rest } = rh.distribute(gates); assert.deepEqual(where.remote, { mode: "from", values: ["country:DK"] }); assert.deepEqual(rest, [older]); } }); test("a second remote_from gate or a must is kept as stored", () => { const second = from("country:SE"); const required = { field: "remote_from", values: ["country:DK"], mode: "require" }; const { where, rest } = rh.distribute([from("country:DK"), second, required]); assert.deepEqual(where.remote, { mode: "from", values: ["country:DK"] }); assert.deepEqual(rest, [second, required]); });