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

135 lines
6.4 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 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 hybrid = { field: "remote", values: ["hybrid"], mode: "prefer", weight: 0.05 };
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 or weighted require gate on country stays a row", () => {
const second = must("NO");
const weighted = { ...must("DE"), weight: 2 };
const { where, rest } = rh.distribute([europe, second, weighted]);
assert.equal(where.scope.kind, "europe");
assert.deepEqual(rest, [second, weighted]);
});
test("a require gate on another field is a row 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, hybrid]);
assert.deepEqual(scope.where.remote, { mode: "scope", values: [] });
assert.equal(scope.where.hybrid, hybrid);
assert.deepEqual(scope.rest, []);
assert.deepEqual(rh.distribute([hybrid]).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 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, { mode: "from", values: ["country:DK"] });
assert.deepEqual(rest, [older]);
}
});
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 required = { field: "remote_from", values: ["country:DK"], mode: "require" };
const { where, rest } = rh.distribute([from("country:DK"), second, weighted, required]);
assert.deepEqual(where.remote, { mode: "from", values: ["country:DK"] });
assert.deepEqual(rest, [second, weighted, required]);
});