Add remote.js: the remote badge words, the picks of the remote_from line and how gates share out between Where and Other rules, with tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus 5.5 committed 2026-09-30 15:23:04 +02:00
1 parent d47f4f5a59
commit 593c351037
2 files changed
+202

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
/* Where a remote role may be worked from, as the pages say it: the words of
* the badge, the picks of the agent's "Remote, open to someone in" chips, and
* how an agent's gates share out between the Where block and Other rules. No
* DOM in it, so `tests/js/remote.test.mjs` can walk it without a browser.
*
* The area is a place token in three spellings, one per layer: `remote:<place
* token>` on the Roles rail and in the address, `<place token>` in a
* `remote_from` gate, and a label on a row (`remote_where`). `anywhere` is
* the one place that is no gazetteer town, region or country.
*/
(function () {
"use strict";
const ANYWHERE = "anywhere";
/** The badge of a row and the reader: "remote · Europe", "remote · anywhere",
"remote · Denmark" from `remote_where`, "remote" alone when the area is
not stated, and the other words (hybrid, onsite) as they are. */
function remoteBadge(role) {
const word = role && role.remote;
if (!word) return "";
const where = word === "remote" ? String(role.remote_where || "").trim() : "";
return where ? `${word} · ${where}` : word;
}
/** A `/api/suggest/places` answer as the value of a `remote_from` gate, or
null when the Where block does not offer it: countries, regions and
Anywhere. A town, or a remote token that is not Anywhere (the block's
own country pick is the plain one), is left out. */
function fromSuggestion(s) {
const token = String(s.token || "");
if (token === `remote:${ANYWHERE}`) return ANYWHERE;
if (token.startsWith("country:") || token.startsWith("region:")) return token;
return null;
}
/** The words a person can type for Anywhere, which the suggestions may or
may not carry. */
function offersAnywhere(q) {
const text = String(q).trim().toLowerCase();
return text.length > 0 && ["anywhere", "worldwide"].some((w) => w.startsWith(text));
}
/** The chip label of a gate value when the server has not said it yet. */
function fallbackLabel(value, countryName) {
if (value === ANYWHERE) return "Anywhere";
if (value === "country:EU") return "Europe";
if (value.startsWith("country:")) return countryName(value.slice(8));
return value.slice(value.indexOf(":") + 1);
}
/** "remote from Denmark, Sweden or Germany", "remote from anywhere". */
function remoteSentence(values, label) {
const names = values.map((v) => (v === ANYWHERE ? "anywhere" : label(v)));
const list = names.length < 2 ? names.join("")
: `${names.slice(0, -1).join(", ")} or ${names[names.length - 1]}`;
return `remote from ${list}`;
}
/** Share out an agent's gates between the Where block and Other rules. Each
Where line takes the first gate it can say whole; the rest are rows. A
must or one-of gate with a weight other than the default stays a row,
since Where would drop it. The remote line is a `remote_from` one-of, or
the older one-of on `remote: [remote]`, which reads as remote from
Anywhere; both save as `remote_from`. Every require gate on country is a
row, "within Europe" among them: it holds for every role and not only
the remote ones, so the block does not fold it into the remote line. */
function distribute(gates) {
const where = { countries: null, places: null, remote: null, hybrid: null };
const rest = [];
const plain = (g) => g.weight === undefined || g.weight === 1;
const only = (g, value) => g.values.length === 1 && g.values[0] === value;
let older = null;
for (const g of gates) {
if (g.field === "country" && g.mode === "any" && plain(g) && !where.countries) where.countries = g;
else if (g.field === "place" && g.mode === "any" && plain(g) && !where.places) where.places = g;
else if (g.field === "remote_from" && g.mode === "any" && plain(g) && !where.remote) {
where.remote = { values: g.values.map(String) };
} else if (g.field === "remote" && g.mode === "any" && plain(g) && only(g, "remote") && !older) {
older = g;
} else if (g.field === "remote" && g.mode === "prefer" && only(g, "hybrid") && !where.hybrid) where.hybrid = g;
else rest.push(g);
}
if (older) {
if (where.remote) rest.push(older);
else where.remote = { values: [ANYWHERE] };
}
return { where, rest };
}
window.remoteHelpers = { ANYWHERE, remoteBadge, fromSuggestion, offersAnywhere, fallbackLabel, remoteSentence, distribute };
})();
+110
View File
@@ -0,0 +1,110 @@
// 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]);
});