mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-11-18 01:54:23 +01:00
perf: paginate repository discovery and search
This commit is contained in:
1 parent
3fbf2cbad7
commit
0b967aa8af
11 files changed
+291
-76
No files matched your search
@@ -0,0 +1,47 @@
|
||||
const { expect } = require("chai");
|
||||
require("ts-node/register/transpile-only");
|
||||
const github = require("../src/core/GitHubUtils");
|
||||
const { repositoryPickerPage } = require("../src/core/github-repository-picker");
|
||||
|
||||
describe("repository picker bounded pages", () => {
|
||||
let original;
|
||||
beforeEach(() => { original = github.octokit; });
|
||||
afterEach(() => { github.octokit = original; });
|
||||
it("fetches only one small upstream page for browsing", async () => {
|
||||
let calls = 0;
|
||||
github.octokit = token => ({ request: async (route, params) => {
|
||||
calls++;
|
||||
expect(token).to.equal("private-token");
|
||||
expect(params).to.include({ page: 2, per_page: 20, sort: "full_name" });
|
||||
return { data: [{ id: 21, full_name: "org/repo" }], headers: {} };
|
||||
} });
|
||||
expect(await repositoryPickerPage("private-token", 20)).to.deep.equal({
|
||||
items: [{ id: 21, fullName: "org/repo" }], nextCursor: null,
|
||||
});
|
||||
expect(calls).to.equal(1);
|
||||
});
|
||||
it("searches case-insensitively and resumes without skipping matches", async () => {
|
||||
github.octokit = () => ({ request: async () => ({
|
||||
data: Array.from({ length: 100 }, (_, id) => ({ id, full_name: `Org/${id % 2 ? "other" : "Match"}-${id}` })),
|
||||
headers: { link: '<https://api.github.com/user/repos?page=2>; rel="next"' },
|
||||
}) });
|
||||
const first = await repositoryPickerPage("token", 0, "org/match");
|
||||
expect(first.items).to.have.length(20);
|
||||
expect(first.nextCursor).to.equal(39);
|
||||
const second = await repositoryPickerPage("token", first.nextCursor, "org/match");
|
||||
expect(second.items[0].id).to.equal(40);
|
||||
expect(second.items).to.have.length(20);
|
||||
});
|
||||
it("returns a continuation for an empty search batch, not a false end", async () => {
|
||||
github.octokit = () => ({ request: async () => ({
|
||||
data: Array.from({ length: 100 }, (_, id) => ({ id, full_name: `org/repo-${id}` })),
|
||||
headers: { link: '<https://api.github.com/user/repos?page=2>; rel="next"' },
|
||||
}) });
|
||||
expect(await repositoryPickerPage("token", 0, "missing")).to.deep.equal({ items: [], nextCursor: 100 });
|
||||
});
|
||||
it("propagates upstream failures so the UI can retry", async () => {
|
||||
github.octokit = () => ({ request: async () => { throw new Error("rate limit"); } });
|
||||
try { await repositoryPickerPage("token"); throw new Error("unexpected success"); }
|
||||
catch (error) { expect(error.message).to.equal("rate limit"); }
|
||||
});
|
||||
});
|
||||
@@ -394,6 +394,78 @@ describe("Vue 3 UI", function () {
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("pages the repository picker and searches beyond the loaded page", async () => {
|
||||
ui = await browser("/anonymize", {
|
||||
"/api/user/repository_picker": request => {
|
||||
const cursor = Number(request.url.searchParams.get("cursor"));
|
||||
const q = request.url.searchParams.get("q");
|
||||
if (q && cursor === 0) return { items: [], nextCursor: 100 };
|
||||
return { items: [{ id: cursor + 1, fullName: q ? "org/needle" : `org/repo-${cursor}` }], nextCursor: cursor === 0 ? 20 : null };
|
||||
},
|
||||
});
|
||||
const picker = () => ui.window.document.querySelector(".repo-picker");
|
||||
const button = text => [...picker().querySelectorAll("button")].find(b => b.textContent.trim() === text);
|
||||
expect(picker().textContent).to.include("repo-0");
|
||||
expect(button("Previous").disabled).to.equal(true);
|
||||
button("Next").click();
|
||||
await delay(30);
|
||||
expect(picker().textContent).to.include("repo-20");
|
||||
button("Previous").click();
|
||||
await delay(30);
|
||||
expect(picker().textContent).to.include("repo-0");
|
||||
await ui.input(".repo-picker-filter", "needle");
|
||||
await delay(350);
|
||||
expect(picker().textContent).to.include("needle");
|
||||
expect(picker().textContent).not.to.include("repo-0");
|
||||
expect(ui.requests.some(r => r.url.searchParams.get("q") === "needle" && r.url.searchParams.get("cursor") === "100")).to.equal(true);
|
||||
expect(ui.requests.some(r => r.url.pathname === "/api/user/all_repositories")).to.equal(false);
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("ignores stale repository responses after the search changes", async () => {
|
||||
let finish;
|
||||
ui = await browser("/anonymize", {
|
||||
"/api/user/repository_picker": request => request.url.searchParams.get("q")
|
||||
? { items: [{ id: 2, fullName: "org/current" }], nextCursor: null }
|
||||
: new Promise(resolve => { finish = resolve; }),
|
||||
});
|
||||
await ui.input(".repo-picker-filter", "current");
|
||||
await delay(350);
|
||||
finish({ items: [{ id: 1, fullName: "org/stale" }], nextCursor: 20 });
|
||||
await delay(30);
|
||||
const picker = ui.window.document.querySelector(".repo-picker");
|
||||
expect(picker.textContent).to.include("current");
|
||||
expect(picker.textContent).not.to.include("stale");
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("bounds sparse searches and lets the user continue and retry", async () => {
|
||||
let fail = false;
|
||||
ui = await browser("/anonymize", {
|
||||
"/api/user/repository_picker": request => {
|
||||
if (fail) return { __status: 503, body: {} };
|
||||
const cursor = Number(request.url.searchParams.get("cursor"));
|
||||
return { items: [], nextCursor: cursor + 100 };
|
||||
},
|
||||
});
|
||||
await ui.input(".repo-picker-filter", "absent");
|
||||
await delay(350);
|
||||
const searches = () => ui.requests.filter(r => r.url.searchParams.get("q") === "absent");
|
||||
expect(searches()).to.have.length(5);
|
||||
const button = text => [...ui.window.document.querySelectorAll(".repo-picker button")].find(b => b.textContent.trim() === text);
|
||||
expect(ui.window.document.querySelector(".repo-picker").textContent).to.include("No matches yet");
|
||||
fail = true;
|
||||
button("Continue search").click();
|
||||
await delay(30);
|
||||
expect(button("Try again")).not.to.equal(undefined);
|
||||
fail = false;
|
||||
button("Try again").click();
|
||||
await delay(30);
|
||||
expect(searches()[6].url.searchParams.get("cursor")).to.equal("500");
|
||||
expect(searches()).to.have.length(11);
|
||||
expect(ui.errors).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it("starts a deep-linked file and editor before directory listings finish", async () => {
|
||||
const pendingDirectories = [];
|
||||
let releaseFile;
|
||||
|
||||
Reference in new issue
Block a user