diff --git a/app/static/companies.js b/app/static/companies.js index 4e85538..2c83ca7 100644 --- a/app/static/companies.js +++ b/app/static/companies.js @@ -420,9 +420,13 @@ const row = document.createElement("div"); row.className = "crow"; if (company.affinity === -1) row.dataset.hidden = "true"; + // By key, as the feed's own company links are, so the Roles page opens + // the company view with its strip and gate note. A name would be a + // substring there, which also lists other companies whose names hold it + // and misses roles spelt otherwise. const link = document.createElement("a"); link.className = "name"; - link.href = `/?company=${encodeURIComponent(company.name)}`; + link.href = `/?company_keys=${encodeURIComponent(company.key)}`; link.title = "This company's open roles"; link.textContent = company.name; row.appendChild(link); diff --git a/app/static/roles.js b/app/static/roles.js index 7b3b086..e590031 100644 --- a/app/static/roles.js +++ b/app/static/roles.js @@ -30,6 +30,9 @@ * and each company affinity. One is always picked while any is enabled: the * one in `?agent=`, else the one this browser viewed last (localStorage), * else the enabled one with the lowest id, which is the server's default too. + * The feed's first request carries the first two before the agents are in, + * and waits for them when there is neither, since the server gates nothing + * for a request that names no agent (`start`). * Picking another reloads the feed and the counts under the reader, which * stays open and is fetched again for that agent's vote. * @@ -39,8 +42,10 @@ * the same three whichever agent is viewed, and all carry the agent's votes. * What fits me needs a CV: an agent without one is shown What I'm looking * for, as an agentless page is shown Newest. Every order keeps to the - * roles the agent's fetch gate passes, which a note under Viewing as says; - * Show gated (`gated=true`) adds the ones it rejects, with the reason. + * roles the agent's fetch gate passes, which a note under Viewing as says, + * and for a phone, whose rail is shut, the end of the list and the empty + * state; Show gated (`gated=true`) adds the ones it rejects, with the + * reason. The reader says when the gate rejects the role open. * * Saved filters belong to the viewing agent (`/api/agents/{id}/filters`): * pills in the rail, newsdesk's `#active` row, each the query string @@ -76,14 +81,14 @@ * screen with that company as its one company chip (`?company_keys=`); a * plain click filters in place and pushes. With one company filtered a strip * over the feed names it, counts its roles, links its careers page and - * carries its ⊘ ▲ ★ for the viewing agent. `?company=`, `?company_keys=` and - * `?agent=` are how the Companies and Search agents pages link in. + * carries its ⊘ ▲ ★ for the viewing agent. `?company_keys=` and `?agent=` + * are how Settings › Sources and Search agents link in. * * Company and Place in the rail are chip inputs (chips.js) on the suggest * routes: a chip is a company key or a place token (`country:DK`, * `region:skane`), several joined by OR. `?company=` and `?place=`, the - * substrings of the links from other pages, still filter and show as a chip - * of their own to take off; nothing in the rail types a new one. Seniority + * substrings of older links and saved filters, still filter and show as a + * chip of their own to take off; nothing in the rail types a new one. Seniority * (a From and a To rung and a Track, `level_from`, `level_to` and `track`) * and Pay carry the counts of `/api/roles/facets`, asked after each reload * with the feed's own parameters, so each says what a click would add. The @@ -91,8 +96,11 @@ * an old `?seniority=` is ignored. * * The list is paged: 60 rows, then 60 more each time the end of the list comes - * within 600px of the screen, with the offset the rows already shown. Back to - * this page from another tab puts the same number of rows and the scroll + * within 600px of the screen. The server sorts afresh on every request and a + * vote or a read moves a role in its list or out of it, so the next page + * starts a row earlier for each and a row already on screen is dropped + * (`pageAsk`, `pageMerge`): none is skipped and none is shown twice. Back + * to this page from another tab puts the same number of rows and the scroll * position back, from sessionStorage. */ (function () { @@ -130,9 +138,9 @@ const TRACKS = { "": "Both", ic: "Individual", people: "People" }; // What the list is in the order of. What I'm looking for is the viewing - // agent's want feed score, moved by its votes; Newest is first seen; What - // fits me is the agent's `judge` total, the roles not judged after by CV - // closeness. + // agent's want feed score, moved by its votes; Newest is the posting date; + // What fits me is the agent's `judge` total, the roles not judged after by + // CV closeness. const SORTS = { score: "What I\u2019m looking for", cv: "What fits me", time: "Newest" }; // The route's `order` for each. const ORDERS = { score: "score", time: "newest", cv: "cv" }; @@ -156,10 +164,11 @@ // Everything `clearFilters` has to put back, in one place, so a new filter // added here cannot be forgotten there. `company` and `place` are the - // substrings of links from other pages; `companyKeys` and `places` are the - // rail's chips. `levelFrom`, `levelTo` and `track` are the seniority range; - // `seniorityUnknown` and `payUnknown` are the "and roles with no level / no - // pay" boxes, asked only while a range, a track or a pay option is picked. + // substrings of older links and saved filters; `companyKeys` and `places` + // are the rail's chips. `levelFrom`, `levelTo` and `track` are the seniority + // range; `seniorityUnknown` and `payUnknown` are the "and roles with no + // level / no pay" boxes, asked only while a range, a track or a pay option + // is picked. function blankFilter() { return { sort: "score", agent: null, period: "all", tags: new Set(), showRead: true, @@ -193,6 +202,10 @@ tags: [], // [{tag, count, unread}] from /api/filters items: [], // the rows on screen, in order total: 0, // how many roles the filters match in all + offset: 0, // how far into the server's list the pages have read + more: false, // the server's list goes on past `offset` + moved: 0, // votes and reads no page has been asked after yet (`pageAsk`) + movedOnce: 0, // those one page has been asked after loaded: false, // the first page has landed loading: false, // a page is in flight, so the pager holds off selected: 0, // which row the keyboard is on @@ -403,18 +416,73 @@ // -------------------------------------------------------------- loading + /** The most rows one request may ask for (`limit` on /api/roles). */ + const MOST = 1000; + + /* Where the next page starts and how many rows it asks for. The server + sorts afresh on every request, and a vote moves a role in its order + (`_VOTED_SCORE`) or out of the list (Judged), as a read does with Show + read off. Each row that leaves the part already read lets a row from + below up into it, which a page asked from `offset` would never show, so + the page starts one row earlier for each such change and is that much + longer; the rows it repeats are dropped (`pageMerge`). `moved` holds + each change for two pages, the one asked after it and the next + (`state.moved` and `state.movedOnce`), since a vote still in flight + when a page is asked may land after the server has read that page. */ + function pageAsk(offset, moved, page = PAGE) { + const back = Math.min(offset, moved, MOST - page); + return { offset: offset - back, limit: page + back }; + } + + /* An answer onto the rows on screen: the rows not on screen yet after + them, in the server's order, so the role a pass sank is not shown a + second time; how far into the server's list the pages have now read; + and whether it goes on. A short page is its end whatever `total` says, + since the count and the page are two queries. */ + function pageMerge(items, ask, answer) { + const shown = new Set(items.map((r) => r.uid)); + const offset = ask.offset + answer.items.length; + return { + items: items.concat(answer.items.filter((r) => !shown.has(r.uid))), + offset, + more: answer.items.length >= ask.limit && offset < answer.total, + }; + } + + // For the tests, which page through a list sorted afresh without a DOM. + window.feedPaging = { pageAsk, pageMerge }; + let feedRequest = 0; + /* The feed has nothing to show, so the empty state offers a retry rather + than leaving the feed blank for good. */ + function feedFailed() { + state.feedError = true; + state.loaded = false; + renderFeed(); + } + /* A reload starts the list again; an append asks for the rows after the - ones shown. `keep` asks for that many in one go, which is how the list is - put back as it was. The latch is let go only by the newest request: a - reload overtaking an append must not let a second append start with an - offset into the list being replaced. */ + ones read (`pageAsk`). `keep` asks for that many in one go, which is how + the list is put back as it was. The latch is let go only by the newest + request: a reload overtaking an append must not let a second append + start with an offset into the list being replaced. + + Nothing is asked while no agent is named and the agents have not said + whether there is one, since the server would list every open role, + ungated (`start`); the empty state offers the retry instead. */ async function loadFeed({ append = false, keep = 0 } = {}) { - if (append && (state.loading || state.items.length >= state.total)) return; + if (state.filter.agent === null && !state.agentsLoaded) { + if (!append) feedFailed(); + return; + } + if (append && (state.loading || !state.more)) return; + const ask = append ? pageAsk(state.offset, state.moved + state.movedOnce) + : { offset: 0, limit: Math.min(MOST, Math.max(PAGE, keep)) }; const params = feedParams(); - params.set("offset", String(append ? state.items.length : 0)); - params.set("limit", String(Math.min(1000, Math.max(PAGE, keep)))); + params.set("offset", String(ask.offset)); + params.set("limit", String(ask.limit)); + const moved = state.moved; const request = ++feedRequest; state.loading = true; let data; @@ -422,23 +490,25 @@ data = await api(`/api/roles?${params}`); } catch (err) { // An append failure leaves what is already on screen; a first load or - // a reload has nothing to show, so the empty state offers a retry - // rather than leaving the feed blank for good. - if (!append && request === feedRequest) { - state.feedError = true; - state.loaded = false; - renderFeed(); - } + // a reload has nothing to show (`feedFailed`). + if (!append && request === feedRequest) feedFailed(); return; } finally { if (request === feedRequest) state.loading = false; } if (request !== feedRequest) return; state.feedError = false; + // The changes made before this page was asked are counted once more, for + // the next; those made while it was in flight wait for theirs. + state.movedOnce = moved; + state.moved -= moved; const held = append ? state.items[state.selected]?.uid : state.readingUid; + const page = pageMerge(append ? state.items : [], ask, data); state.total = data.total; - state.items = append ? state.items.concat(data.items) : data.items; + state.items = page.items; + state.offset = page.offset; + state.more = page.more; state.loaded = true; const at = held ? state.items.findIndex((r) => r.uid === held) : -1; state.selected = at >= 0 ? at : append ? state.selected : 0; @@ -739,7 +809,7 @@ input.set(want); } - /* `?company=` or `?place=`, the substring a link from another page + /* `?company=` or `?place=`, the substring an older link or saved filter brought: a plain chip saying what it matches, which takes it off and pushes, as a filter does. */ function renderText(id, text, noun, key) { @@ -1340,31 +1410,67 @@ empty.innerHTML = `

Could not load the feed

The server did not answer. .

`; + // Without the agents the feed may not know whose it is, so they are + // asked again with it. const retry = empty.querySelector("[data-retry]"); - if (retry) retry.onclick = () => loadFeed(); + if (retry) retry.onclick = () => (state.agentsLoaded ? loadFeed() : start()); } else { + // An agent's list is what its fetch gate passes, which a phone, + // whose rail is shut, learns here and at the end of the list. const filtered = state.q || filtersOn(); + const gate = gateKept() ? `${esc(gateOwner())} fetch gate` : ""; empty.innerHTML = filtered ? `

Nothing matches

-

No open role fits these filters. +

No open role ${gate ? `that passes ${gate} ` : ""}fits these filters. and start again.

` - : `

No roles yet

-

Nothing has been pulled yet. Runs starts one; - Job boards and - Company boards decide what it brings in.

`; + : gate + ? `

No roles yet

+

No open role passes ${gate} yet. Runs + starts a pull; Job boards and + Company boards decide what it + brings in, Search agents what the gate passes.

` + : `

No roles yet

+

Nothing has been pulled yet. Runs starts one; + Job boards and + Company boards decide what it brings in.

`; const clear = empty.querySelector("[data-clear]"); if (clear) clear.onclick = () => { state.q = ""; $("#search").value = ""; clearFilters(); }; } } // The end of the list says which end it is: a list that has run out and - // one that is still loading look the same otherwise. + // one that is still loading look the same otherwise. Whether it has run + // out is the server's to say (`pageMerge`), since the rows on screen + // keep the ones a vote or a read has since taken out of its list. const end = $("#feed-end"); const n = state.items.length; end.hidden = !state.loaded || !n; - end.textContent = n < state.total - ? "Loading more…" - : `${number.format(n)} ${n === 1 ? "role" : "roles"} — that is all of them.`; + end.textContent = state.more ? "Loading more…" : `${listSaid(n)} — that is all of them.`; + } + + /** Whether the list keeps to the viewing agent's fetch gate: there is an + agent, and Show gated is off. */ + const gateKept = () => state.filter.agent !== null && !state.filter.gated; + + /** Whose fetch gate, for a sentence: the viewing agent's name, or "this + agent's" until the agents are in. */ + function gateOwner() { + const agent = viewed(); + return agent ? `${agent.name}'s` : "this agent's"; + } + + /** The rows on screen in words, for the end of the list: how many and, + with an agent viewing, that they are what its fetch gate passes (and + the filters keep), or with Show gated that they hold what it rejects. */ + function listSaid(n) { + const roles = `${number.format(n)} ${n === 1 ? "role" : "roles"}`; + if (state.filter.agent === null) return roles; + const gate = `${gateOwner()} fetch gate`; + const filtered = Boolean(state.q) || filtersOn() > (state.filter.gated ? 1 : 0); + if (state.filter.gated) { + return `${roles}${filtered ? " in these filters" : ""}, the ones ${gate} rejects included`; + } + return `${roles} ${n === 1 ? "passes" : "pass"} ${gate}${filtered ? " and these filters" : ""}`; } /* With exactly one company filtered, which is where its name leads, a @@ -1453,7 +1559,8 @@ rows out at once, since a hidden company is never in the feed; where the others rank waits on the `score` it queued, whose id the affinity route answers with (`updated.score_job`), and the feed reloads when that has - finished. */ + finished. A list the company filter has come off meanwhile has its rows + asked again in the new order, as `renderAffinity` does. */ async function affinityChanged(key, company, updated) { if (!company) await loadCompanies(state.filter.agent).catch(() => {}); if (updated?.affinity === -1) reload(); else renderFeed(); @@ -1462,6 +1569,7 @@ await scored(updated?.score_job); if (status()) status().textContent = ""; if (state.filter.companyKeys.includes(key)) reload(); + else loadFeed({ keep: state.items.length }); } /* Wait for the `score` job `job`, the way agents.js's `rescore` waits: @@ -1561,6 +1669,8 @@ async function markRead(uid) { const item = state.items.find((r) => r.uid === uid); if (item && item.read_at) return; + // With Show read off the server's list loses it, see `pageAsk`. + if (!state.filter.showRead) state.moved += 1; if (item) { item.read_at = new Date().toISOString(); renderFeed(); } try { const role = await api(`/api/roles/${encodeURIComponent(uid)}/read`, { method: "POST" }); @@ -1732,6 +1842,10 @@ const waiting = state.summarising === state.readingUid; const teaser = loaded && !r.article_text && out && (r.description || "").length < TEASER; + // Under the title, in the copy note's style: that the viewing agent's + // fetch gate rejects the role and why (`gated_reason`), since a link + // from elsewhere, a saved role on Applications say, opens one the feed + // never lists. el.innerHTML = `
@@ -1755,6 +1869,9 @@

${esc(r.title || "")}

${loaded && r.dup_of ? `

A copy of a posting the feed lists once.

` : ""} + ${loaded && state.filter.agent !== null && r.gated_reason + ? `

Rejected by ${esc(gateOwner())} fetch gate: + ${esc(r.gated_reason)}. The feed lists it only with Show gated.

` : ""} ${pay ? `

${esc(pay.range)}

${pay.detail ? `

${esc(pay.detail)}

` : ""} ${pay.note ? `

${esc(pay.note)}

` : ""}` : ""} @@ -1807,14 +1924,21 @@ Companies page, and its two fetch overrides beside them. A company seen only through a search has no row until its first change creates one, so the list is reloaded then, and the overrides, which need a row, wait for it. - Hiding reloads the feed, which no longer holds the company's roles. */ + Hiding reloads the feed, which no longer holds the company's roles. Any + other stance moves every role of the company once the `score` it queued + has run, up into the rows already read as well as down out of them, + which no page asked from an offset makes up for (`pageAsk`); so the rows + on screen are asked again then, as many, in the new order. */ function renderAffinity(slot, r) { const company = companies.get(r.company_key); if (!r.company_key) { slot.replaceChildren(); return; } slot.replaceChildren(affinityControl(r.company_key, company ? company.affinity : 0, async (updated) => { if (!company) await loadCompanies(state.filter.agent).catch(() => {}); renderAffinity(slot, r); - if (updated?.affinity === -1) reload(); else renderFeed(); + if (updated?.affinity === -1) { reload(); return; } + renderFeed(); + await scored(updated?.score_job); + loadFeed({ keep: state.items.length }); }, state.filter.agent)); if (company) { slot.append(" ", overridesControl(r.company_key, company, @@ -2124,6 +2248,16 @@ new IntersectionObserver((entries) => { if (entries.some((e) => e.isIntersecting)) loadFeed({ append: true }); }, { rootMargin: "600px" }).observe($("#feed-end")); + // ▼ ● ▲ and ★, which likes, on a row or in the reader move the role in + // the server's order, or out of its list under Judged or with Show read + // off, which the next page makes up for (`pageAsk`). The keys press the + // same buttons. Counted at the click, failed or undone ones too, since a + // page that starts a row too early only repeats one. + for (const pane of [$("#roles-list"), $("#reader")]) { + pane.addEventListener("click", (e) => { + if (e.target.closest(".judge button")) state.moved += 1; + }); + } // Kept on the way out, whichever way that is, so back finds it. window.addEventListener("pagehide", keep); @@ -2143,6 +2277,60 @@ }); } + /* Whose feed it is, then the feed. The server keeps to a fetch gate only + for an agent it is told of, and without one lists every open role, so + the first request carries the agent whenever one is known before the + agents are in: the address's, else the one this browser viewed last + (`storedAgent`). `chooseAgent` checks it against the enabled ones when + they land, and the feed reloads if it picks another. With neither, the + feed waits for the agents: with none enabled the list of every open + role is the right one, and when they cannot be had the feed's error + state offers to try again (`loadFeed`), rather than every open role + passing for the agent's list. + + The rest fills in when it arrives and the feed does not wait on it: the + companies, the tag counts, the saved filters. Each one drawn before is + greyed or missing, which is what "no agent" (renderAgent), an unjoined + company (paintRow) and the tag row being empty already mean, so nothing + here is a new state to handle. `keep` and `y` put the list back as it + was (`kept`). Run again by the error state's retry when the agents did + not come. */ + async function start({ keep = 0, y = null } = {}) { + if (state.filter.agent === null) state.filter.agent = storedAgent(); + const agentsIn = loadAgents().then(() => { chooseAgent(); return true; }, () => false); + const trackedIn = loadTracked().catch(() => {}); + // What the reader's fetch and then the feed's first request go out with, + // so the agents landing later know whether either is to be asked again. + const routed = state.filter.agent; + route(); + if (state.filter.agent === null) await agentsIn; + const asked = state.filter.agent; + const sortAsked = sortShown(); + const enhance = (async () => { + const [ready] = await Promise.all([agentsIn, trackedIn]); + if (!ready) return; + await loadCompanies(state.filter.agent).catch(() => {}); + renderRail(); + renderActive(); + loadFilters().catch(() => {}); + loadSaved().catch(() => {}); + // Reload for the score and votes when the agent chosen is not the one + // asked for (a remembered one disabled since), and either way bring + // the reader's affinity control and judge buttons up to date, since + // `companies` was still empty when it first painted. What fits me + // asked of an agent with no CV is What I'm looking for. + if (state.filter.agent !== asked || sortShown() !== sortAsked) reload(); + else renderFeed(); + if (state.readingUid) { + if (state.filter.agent !== routed) refreshReader(state.readingUid); + else renderReader(); + } + })(); + await loadFeed({ keep }); + if (y !== null && !state.readingUid) window.scrollTo(0, y); + await enhance; + } + document.addEventListener("DOMContentLoaded", async () => { readUrl(); const was = kept(); @@ -2153,44 +2341,6 @@ // Named before the first request, which a token the server does not // know would turn into a 422 rather than a list. await labelPlaces(); - - // The feed is the page; agents and companies are enhancements that fill - // in when they arrive, not something it waits on — a slow or failed - // /api/agents or /api/companies must not leave it blank. Each one drawn - // here is greyed or missing until `enhance` lands, which is what "no - // agent" (renderAgent), an unjoined company (paintRow) and the tag row - // being empty already mean, so nothing here is a new state to handle. - const before = state.filter.agent; - const sortBefore = sortShown(); - const enhance = (async () => { - try { - await Promise.all([loadTracked(), loadAgents()]); - chooseAgent(); - await loadCompanies(state.filter.agent); - } catch (err) { - return; - } - renderRail(); - renderActive(); - loadFilters().catch(() => {}); - loadSaved().catch(() => {}); - // The feed's first request went out with whatever agent `readUrl` had - // (often none): reload for the score and votes once the real one is - // known, and either way bring the reader's affinity control and judge - // buttons up to date, since `companies` was still empty when it first - // painted. - // What fits me asked of an agent with no CV is What I'm looking for. - if (state.filter.agent !== before || sortShown() !== sortBefore) reload(); - else renderFeed(); - if (state.readingUid) { - if (state.filter.agent !== before) refreshReader(state.readingUid); - else renderReader(); - } - })(); - - route(); - await loadFeed({ keep: was ? was.count : 0 }); - if (was && !state.readingUid) window.scrollTo(0, was.y || 0); - await enhance; + await start(was ? { keep: was.count, y: was.y || 0 } : {}); }); })(); diff --git a/app/templates/roles.html b/app/templates/roles.html index abba27e..7696975 100644 --- a/app/templates/roles.html +++ b/app/templates/roles.html @@ -46,8 +46,9 @@

Viewing as agents

- {# Every order keeps to the agent's fetch gate (docs/architecture.md, - "5 Feed"), so the filters below only narrow what it passed. #} + {# Every order keeps to the agent's fetch gate (documentation/ADR Fetch + prep and score.md, "5 Feed"), so the filters below only narrow what + it passed. #} {# The agent's saved filters (`/api/agents/{id}/filters`), newsdesk's @@ -74,11 +75,13 @@

Order

- {# Agent match is the viewing agent's final score, Newest is first - seen, CV match the agent's `judge` total against its CV; all carry - that agent's votes and keep to its fetch gate. #} + {# What I'm looking for is the viewing agent's want feed score, Newest + the posting date, What fits me the agent's `judge` total against its + CV; all carry that agent's votes and keep to its fetch gate. The + period counts back over the posting date too, the date a row shows + as its age. #}
- +
@@ -92,8 +95,9 @@
{# Company and Place are chip inputs (static/chips.js), each pick a company key or a place token from the suggest routes. The chips - of `?company=` and `?place=`, the substrings other pages link - with, sit under them to be taken off; nothing types a new one. #} + of `?company=` and `?place=`, the substrings of older links and + saved filters, sit under them to be taken off; nothing types a + new one. #}
diff --git a/tests/js/paging.test.mjs b/tests/js/paging.test.mjs new file mode 100644 index 0000000..8442f2f --- /dev/null +++ b/tests/js/paging.test.mjs @@ -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()); +}); diff --git a/tests/test_feed_ui.py b/tests/test_feed_ui.py index e22dbb1..ad3ba38 100644 --- a/tests/test_feed_ui.py +++ b/tests/test_feed_ui.py @@ -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 diff --git a/tests/test_pages.py b/tests/test_pages.py index ee90b46..64d94ab 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -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('