Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion tests/test_board_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: []});
Expand Down Expand Up @@ -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'<span>{label}</span> <span class="pl-badge" id="count-{kind}" hidden></span></button>' 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;});
"""
)
41 changes: 36 additions & 5 deletions view.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@
<div class="bar">
<select id="repo" title="Repository"></select>
<div class="pl-tabs" role="tablist">
<button class="pl-tab pl-tab--active" id="t-issues" role="tab" aria-selected="true" title="Issues">Issues</button>
<button class="pl-tab" id="t-prs" role="tab" aria-selected="false" title="Pull Requests">PRs</button>
<button class="pl-tab pl-tab--active" id="t-issues" role="tab" aria-selected="true" title="Issues"><span>Issues</span> <span class="pl-badge" id="count-issues" hidden></span></button>
<button class="pl-tab" id="t-prs" role="tab" aria-selected="false" title="Pull Requests"><span>PRs</span> <span class="pl-badge" id="count-prs" hidden></span></button>
</div>
<select id="state" title="State"><option value="open">Open</option><option value="closed">Closed</option><option value="all">All</option></select>
<input id="search" type="search" placeholder="Search…" title="Search (Enter to run, Esc to clear)" aria-label="Search" autocomplete="off" />
Expand Down Expand Up @@ -305,7 +305,7 @@
refresh: svg('<path d="M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/><path d="M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16"/><path d="M8 16H3v5"/>'),
};

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 => '<span class="pl-badge">'+esc(l.name||l)+'</span>').join(" "); }

function issueRow(it, repo){
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -807,7 +838,7 @@
const sel = $("repo");
sel.innerHTML = (cfg.repos||[]).map(r => '<option value="'+esc(r)+'">'+esc(r)+'</option>').join("");
if(cfg.default_repo){ sel.value = cfg.default_repo; }
load();
reload();
}

$("refresh").innerHTML = ICON.refresh;
Expand Down
Loading