Merge feed paging that skips no role after a vote or a read, a first request that names the agent, the company link by key, and the fetch gate named at the end of the list and in the reader (wp-rev-ui)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-10-05 10:12:39 +02:00
commit d2f18cb3c7
6 files changed
+472 -92

No files matched your search

+189
View File
@@ -0,0 +1,189 @@
// The feed's paging, static/roles.js `pageAsk` and `pageMerge`, without a
// browser: against a fake /api/roles that sorts afresh on every request, as
// the store does, with a pass halving a role's score and a like lifting it a
// fifth (`_VOTED_SCORE`), and Show read off or Judged taking a role out.
//
// 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 roles.js touches before DOMContentLoaded.
const window = {
matchMedia: () => ({ matches: false, addEventListener() {} }),
addEventListener() {},
};
const document = { addEventListener() {} };
Object.assign(globalThis, { window, document });
vm.runInThisContext(readFileSync(new URL("../../app/static/roles.js", import.meta.url), "utf8"));
const { pageAsk, pageMerge } = window.feedPaging;
const PAGE = 60;
// ------------------------------------------------------------ the server
/** `n` roles, scores falling evenly from 0.99, all unread and unjudged. */
function server(n, { includeRead = true, judged = "" } = {}) {
const roles = Array.from({ length: n }, (_, i) => ({
uid: `r${String(i).padStart(3, "0")}`, score: 0.99 - (i * 0.5) / n, vote: null, read: false,
}));
const voted = (r) => Math.min(1, r.score * (r.vote === -1 ? 0.5 : r.vote === 1 ? 1.2 : 1));
function list() {
return roles
.filter((r) => includeRead || !r.read)
.filter((r) => judged !== "unjudged" || r.vote === null)
.sort((a, b) => voted(b) - voted(a) || a.uid.localeCompare(b.uid));
}
return {
page: ({ offset, limit }) => {
const all = list();
return { items: all.slice(offset, offset + limit).map((r) => ({ uid: r.uid })), total: all.length };
},
uids: () => list().map((r) => r.uid),
// A vote stamps the role read, as `Store.set_vote` does.
vote: (uid, value) => { const r = roles.find((x) => x.uid === uid); r.vote = value; r.read = true; },
read: (uid) => { roles.find((x) => x.uid === uid).read = true; },
};
}
// ------------------------------------------------------------ the client
/** loadFeed's bookkeeping: the pages asked with `pageAsk`, merged with
`pageMerge`, each change counted for two pages. */
function client(api) {
const c = { items: [], offset: 0, more: false, moved: 0, movedOnce: 0 };
c.load = (append = false) => {
const ask = append ? pageAsk(c.offset, c.moved + c.movedOnce) : { offset: 0, limit: PAGE };
const answer = api.page(ask);
c.movedOnce = c.moved;
c.moved = 0;
Object.assign(c, pageMerge(append ? c.items : [], ask, answer));
};
c.toEnd = () => { for (let i = 0; c.more && i < 100; i += 1) c.load(true); };
c.uids = () => c.items.map((r) => r.uid);
return c;
}
/** Every uid once, and nothing the server lists left out. */
function assertWhole(shown, listed) {
assert.equal(new Set(shown).size, shown.length, "a role is shown twice");
const missing = listed.filter((uid) => !shown.includes(uid));
assert.deepEqual(missing, [], "a role is never shown");
}
// ------------------------------------------------------------ pageAsk
test("with nothing moved the next page starts where the last one ended", () => {
assert.deepEqual(pageAsk(120, 0), { offset: 120, limit: PAGE });
});
test("each change starts the page a row earlier and asks for a row more", () => {
assert.deepEqual(pageAsk(120, 3), { offset: 117, limit: PAGE + 3 });
// Never before the first row, and never more than the route allows.
assert.deepEqual(pageAsk(2, 5), { offset: 0, limit: PAGE + 2 });
assert.deepEqual(pageAsk(5000, 4000), { offset: 5000 - 940, limit: 1000 });
});
// ------------------------------------------------------------ pageMerge
test("a row already on screen is dropped and the offset counts what the server sent", () => {
const items = [{ uid: "a" }, { uid: "b" }];
const merged = pageMerge(items, { offset: 1, limit: 3 },
{ items: [{ uid: "b" }, { uid: "c" }, { uid: "d" }], total: 10 });
assert.deepEqual(merged.items.map((r) => r.uid), ["a", "b", "c", "d"]);
assert.equal(merged.offset, 4);
assert.equal(merged.more, true);
});
test("the list ends at the total, or at a short page whatever the total says", () => {
const at = (items, total) => pageMerge([], { offset: 0, limit: 2 }, { items, total });
assert.equal(at([{ uid: "a" }, { uid: "b" }], 2).more, false);
assert.equal(at([{ uid: "a" }], 5).more, false);
assert.equal(at([{ uid: "a" }, { uid: "b" }], 5).more, true);
});
// ------------------------------------------------------------ against the server
test("a page asked from the rows on screen skips a role after a pass (the bug)", () => {
const api = server(200);
let items = api.page({ offset: 0, limit: PAGE }).items;
api.vote(items[2].uid, -1);
while (items.length < api.page({ offset: 0, limit: 1 }).total) {
items = items.concat(api.page({ offset: items.length, limit: PAGE }).items);
}
const uids = items.map((r) => r.uid);
assert.notEqual(new Set(uids).size, uids.length);
assert.ok(api.uids().some((uid) => !uids.includes(uid)));
});
test("a pass on the first page skips nothing and shows nothing twice", () => {
const api = server(200);
const c = client(api);
c.load();
api.vote(c.items[2].uid, -1);
c.moved += 1;
c.toEnd();
assertWhole(c.uids(), api.uids());
assert.equal(c.items.length, 200);
assert.equal(c.more, false);
});
test("likes, passes and undos on several pages skip nothing", () => {
const api = server(500);
const c = client(api);
c.load();
let seed = 7;
const pick = () => { seed = (seed * 48271) % 2147483647; return seed; };
while (c.more) {
for (let i = 0; i < 6; i += 1) {
const uid = c.items[pick() % c.items.length].uid;
api.vote(uid, [-1, 1, null][pick() % 3]);
c.moved += 1;
}
c.load(true);
}
assertWhole(c.uids(), api.uids());
assert.equal(c.items.length, 500);
});
test("with Show read off each role opened costs no role", () => {
const api = server(200, { includeRead: false });
const c = client(api);
c.load();
for (const i of [0, 5, 30, 59]) { api.read(c.items[i].uid); c.moved += 1; }
c.load(true);
api.read(c.items[90].uid);
c.moved += 1;
c.toEnd();
assertWhole(c.uids(), api.uids());
// The read ones stay on screen; the server's list no longer has them.
assert.equal(c.items.length, 200);
assert.equal(api.uids().length, 195);
assert.equal(c.more, false);
});
test("under Judged, Unjudged a vote takes the role out and the next page makes up for it", () => {
const api = server(150, { judged: "unjudged" });
const c = client(api);
c.load();
for (const i of [1, 2, 3]) { api.vote(c.items[i].uid, 1); c.moved += 1; }
c.toEnd();
assertWhole(c.uids(), api.uids());
assert.equal(c.items.length, 150);
});
test("a change counts for the page after the next too, for a vote that lands late", () => {
const api = server(200);
const c = client(api);
c.load();
c.load(true);
// Clicked before the third page was asked, landed after the server read it.
const late = c.items[10].uid;
c.moved += 1;
c.load(true);
api.vote(late, -1);
c.toEnd();
assertWhole(c.uids(), api.uids());
});
+22
View File
@@ -110,3 +110,25 @@ def test_every_page_says_all_locations() -> None:
assert "All countries" not in path.read_text(), path.name
for path in (STATIC.parent / "templates").rglob("*.html"):
assert "All countries" not in path.read_text(), path.name
def test_a_company_on_settings_sources_links_to_its_company_view_by_key() -> None:
"""By key, as the feed's own company links are: a name is a substring on
the Roles page, which lists other companies whose names hold it and
shows no company strip with its gate note."""
script = (STATIC / "companies.js").read_text()
assert "`/?company_keys=${encodeURIComponent(company.key)}`" in script
assert "/?company=" not in script
def test_the_end_of_the_list_the_empty_state_and_the_reader_name_the_fetch_gate() -> None:
"""A phone's rail, which holds the note, is shut, so the end of the list
and the empty state say whose fetch gate the list keeps to, and the
reader says when that gate rejects the role open (`gated_reason`)."""
script = (STATIC / "roles.js").read_text()
assert "`${listSaid(n)} — that is all of them.`" in script
assert '${n === 1 ? "passes" : "pass"} ${gate}' in script
assert "No open role ${gate ? `that passes ${gate} ` : \"\"}fits these filters." in script
assert "No open role passes ${gate} yet." in script
assert "Rejected by ${esc(gateOwner())} fetch gate:" in script
assert "loaded && state.filter.agent !== null && r.gated_reason" in script
+11
View File
@@ -133,3 +133,14 @@ def test_the_reader_excerpt_falls_back_in_the_servers_order() -> None:
description, then teaser, or a teaser-only role's row blanks after Fetch."""
text = (STATIC / "roles.js").read_text("utf-8")
assert "role.summary || role.description || role.teaser" in text
def test_the_period_is_called_posted_as_the_date_it_counts_back_over(
client: TestClient) -> None:
"""`days` keeps `posted_at` within the window, the date a row shows as its
age, so the select is not called First seen."""
rail = client.get("/").text.split('<aside id="rail"', 1)[1].split("</aside>", 1)[0]
assert '<label class="rail-field" for="period">Posted</label>' in rail
assert "First seen" not in rail
template = (STATIC.parent / "templates" / "roles.html").read_text("utf-8")
assert "docs/architecture.md" not in template and "Agent match" not in template