diff --git a/tests/test_board_view.py b/tests/test_board_view.py index fda12d1..18dfdb4 100644 --- a/tests/test_board_view.py +++ b/tests/test_board_view.py @@ -1274,7 +1274,7 @@ def test_search_box_debounces_runs_on_enter_and_clears_on_esc(): element('search').handlers.input(); assert.equal(requests.length, base); // debounced, not yet sent runTimeouts(); // the 300ms timer fires reload() - assert.equal(requests.length, base+1); + assert.equal(requests.length, base+2); // list and independent counts assert.ok(requests[latest()].path.includes('&search=crash%20bug')); // one encoded param assert.ok(requests[latest()].path.includes('limit=30')); // search resets to page 1 await reply(latest(), {items: []}); @@ -1539,3 +1539,72 @@ def test_board_component_variants_and_tab_selection(): })().catch(e=>{console.error(e);process.exitCode=1;}); """ ) + + +def test_board_count_badges_and_query_contract(): + from ghplugin.view import PAGE + + for kind, label in (("issues", "Issues"), ("prs", "PRs")): + assert f'{label} ' in PAGE + counts = PAGE.split("async function loadCounts(){", 1)[1].split("\n function reload", 1)[0] + assert '"/api/plugins/github/counts"' in counts + assert '"?repo="+encodeURIComponent(repo)+"&state="+encodeURIComponent($("state").value)' in counts + assert '(q ? "&search="+encodeURIComponent(q) : "")' in counts + assert 'q = ($("search").value || "").trim()' in counts + assert "const my = ++countsSeq;" in counts + assert "if(my !== countsSeq) return;" in counts + assert "response.ok ? await response.json() : null" in counts + assert "catch(e){ if(my === countsSeq) renderCounts(null); }" in counts + assert 'typeof value === "number" && Number.isFinite(value) ? value.toLocaleString() : ""' in PAGE + assert "data && !data.error ? data[kind] : null" in PAGE + assert 'badge.hidden = count === "";' in PAGE # formatted zero is non-empty + assert 'count === "" ? title : title+" ("+count+")"' in PAGE + assert "if(counts !== false) loadCounts();\n load(false);" in PAGE + assert "await loadCounts(" not in PAGE + assert "reload(false);" in PAGE + boot = PAGE.split("async function boot(){", 1)[1].split("\n }", 1)[0] + assert "reload();" in boot + + +def test_board_counts_zero_errors_and_stale_responses(): + _run_drawer_js( + r""" +(async()=>{ + element('repo').value='o/n'; element('state').value='open'; element('search').value=' bug '; + reload(); + assert.equal(requests[0].path, '/api/plugins/github/counts?repo=o%2Fn&state=open&search=bug'); + await reply(1, {items: []}); // list renders while counts remain pending + assert.ok(element('list').innerHTML.includes('No issues')); + await reply(0, {issues:0, prs:1234}); + assert.equal(element('count-issues').textContent, '0'); + assert.equal(element('count-issues').hidden, false); + assert.equal(element('t-issues').title, 'Issues (0)'); + assert.equal(element('count-prs').textContent, (1234).toLocaleString()); + setTab('prs'); + assert.equal(requests.length, 3); + loadCounts(); const old = latest(); + loadCounts(); const fresh = latest(); + await reply(fresh, {issues:42, prs:7}); + await reply(old, {error:'stale failure'}); + assert.equal(element('count-issues').textContent, '42'); + loadCounts(); const rejected = latest(); + loadCounts(); await reply(latest(), {issues:8, prs:9}); + requests[rejected].reject(new Error('stale network failure')); await flush(); + assert.equal(element('count-issues').textContent, '8'); + for(const [data, ok] of [[{error:'failed'},true], [{issues:0,prs:0},false], + [{issues:'0',prs:null},true], [null,true]]){ + loadCounts(); await reply(latest(), data, ok); + for(const kind of ['issues','prs']){ + assert.equal(element('count-'+kind).textContent, ''); + assert.equal(element('count-'+kind).hidden, true); + } + assert.equal(element('t-issues').title, 'Issues'); + assert.equal(element('t-prs').title, 'Pull Requests'); + } + loadCounts(); requests[latest()].reject(new Error('offline')); await flush(); + assert.equal(element('count-issues').hidden, true); + element('repo').value=''; const before = requests.length; + loadCounts(); assert.equal(requests.length, before); +})().catch(e=>{console.error(e);process.exitCode=1;}); +""" + ) diff --git a/view.py b/view.py index 615d2d6..3773a8e 100644 --- a/view.py +++ b/view.py @@ -170,8 +170,8 @@
- - + +
@@ -305,7 +305,7 @@ refresh: svg(''), }; - function setTab(t){ tab = t; $("t-issues").setAttribute("aria-selected", t==="issues"); $("t-prs").setAttribute("aria-selected", t==="prs"); $("t-issues").classList.toggle("pl-tab--active", t==="issues"); $("t-prs").classList.toggle("pl-tab--active", t==="prs"); reload(); } + function setTab(t){ tab = t; $("t-issues").setAttribute("aria-selected", t==="issues"); $("t-prs").setAttribute("aria-selected", t==="prs"); $("t-issues").classList.toggle("pl-tab--active", t==="issues"); $("t-prs").classList.toggle("pl-tab--active", t==="prs"); reload(false); } function labelPills(labels){ return (labels||[]).slice(0,5).map(l => ''+esc(l.name||l)+'').join(" "); } function issueRow(it, repo){ @@ -769,7 +769,38 @@ + "?repo="+encodeURIComponent(repo)+"&state="+encodeURIComponent($("state").value) + "&limit="+pageLimit + (q ? "&search="+encodeURIComponent(q) : ""); } - function reload(){ pageLimit = PAGE_STEP; load(false); } + // Independent fence: tab switches and pagination do not invalidate counts. + let countsSeq = 0; + function renderCounts(data){ + for(const [kind, title] of [["issues", "Issues"], ["prs", "Pull Requests"]]){ + const value = data && !data.error ? data[kind] : null; + const count = typeof value === "number" && Number.isFinite(value) ? value.toLocaleString() : ""; + const badge = $("count-"+kind); + badge.textContent = count; + badge.hidden = count === ""; + $("t-"+kind).title = count === "" ? title : title+" ("+count+")"; + } + } + async function loadCounts(){ + const my = ++countsSeq; + renderCounts(null); + const repo = $("repo").value, q = ($("search").value || "").trim(); + if(!repo) return; + const path = "/api/plugins/github/counts" + + "?repo="+encodeURIComponent(repo)+"&state="+encodeURIComponent($("state").value) + + (q ? "&search="+encodeURIComponent(q) : ""); + try { + const response = await kit.apiFetch(path); + const data = response.ok ? await response.json() : null; + if(my !== countsSeq) return; + renderCounts(data); + } catch(e){ if(my === countsSeq) renderCounts(null); } + } + function reload(counts){ + pageLimit = PAGE_STEP; + if(counts !== false) loadCounts(); + load(false); + } async function load(more){ const my = ++loadSeq; const repo = $("repo").value, list = $("list"); @@ -807,7 +838,7 @@ const sel = $("repo"); sel.innerHTML = (cfg.repos||[]).map(r => '').join(""); if(cfg.default_repo){ sel.value = cfg.default_repo; } - load(); + reload(); } $("refresh").innerHTML = ICON.refresh;