Files
jobboard/tests/js/remote.test.mjs
T

111 lines
5.1 KiB
JavaScript

// static/remote.js without a browser: the badge, the suggestions the agent's
// remote line offers, and how gates share out between Where and Other rules.
//
// 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, a region or Anywhere", () => {
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:anywhere", kind: "remote" }), "anywhere");
assert.equal(rh.fromSuggestion({ token: "remote:country:DK", kind: "remote" }), null);
assert.equal(rh.fromSuggestion({ token: "place:1", kind: "place" }), null);
});
test("Anywhere is offered for the words that start it", () => {
assert.equal(rh.offersAnywhere("any"), true);
assert.equal(rh.offersAnywhere(" World"), true);
assert.equal(rh.offersAnywhere("den"), false);
assert.equal(rh.offersAnywhere(""), false);
});
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 hybrid = { field: "remote", values: ["hybrid"], mode: "prefer", weight: 0.05 };
const europe = { field: "country", values: ["DK", "SE", "EU"], mode: "require" };
test("no remote gate: the remote line is empty", () => {
const { where, rest } = rh.distribute([{ field: "country", values: ["DK"], mode: "any" }, hybrid]);
assert.equal(where.remote, null);
assert.equal(where.hybrid, hybrid);
assert.deepEqual(rest, []);
});
test("a remote_from gate loads into the remote line", () => {
const { where, rest } = rh.distribute([from("country:DK", "anywhere")]);
assert.deepEqual(where.remote, { values: ["country:DK", "anywhere"] });
assert.deepEqual(rest, []);
});
test("the older remote gate loads as remote from Anywhere", () => {
const { where, rest } = rh.distribute([older]);
assert.deepEqual(where.remote, { values: ["anywhere"] });
assert.deepEqual(rest, []);
});
test("a remote_from gate wins over the older one, which stays a row", () => {
for (const gates of [[older, from("country:DK")], [from("country:DK"), older]]) {
const { where, rest } = rh.distribute(gates);
assert.deepEqual(where.remote, { values: ["country:DK"] });
assert.deepEqual(rest, [older]);
}
});
test("the within Europe must is a row, kept whole", () => {
const { where, rest } = rh.distribute([europe, older]);
assert.deepEqual(where.remote, { values: ["anywhere"] });
assert.deepEqual(rest, [europe]);
});
test("a second remote_from gate, a weighted one or a must stays a row", () => {
const second = from("country:SE");
const weighted = { ...from("country:DE"), weight: 2 };
const must = { field: "remote_from", values: ["country:DK"], mode: "require" };
const { where, rest } = rh.distribute([from("country:DK"), second, weighted, must]);
assert.deepEqual(where.remote, { values: ["country:DK"] });
assert.deepEqual(rest, [second, weighted, must]);
});