Files

190 lines
7.1 KiB
JavaScript

// 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());
});