|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + GitHubApi, |
| 4 | + type GitHubIssueResponse, |
| 5 | +} from "../src/github-api"; |
| 6 | +import { |
| 7 | + assertIssueSearchResultsInRepo, |
| 8 | + buildIssueSearchQuery, |
| 9 | +} from "../src/github-search"; |
| 10 | + |
| 11 | +function issueAt(htmlUrl: string): Pick<GitHubIssueResponse, "html_url"> { |
| 12 | + return { html_url: htmlUrl }; |
| 13 | +} |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + vi.unstubAllGlobals(); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("assertIssueSearchResultsInRepo", () => { |
| 20 | + it("accepts exact repository path segments case-insensitively", () => { |
| 21 | + expect(() => assertIssueSearchResultsInRepo("Cloudflare", "Workerd", [ |
| 22 | + issueAt("https://github.com/cloudflare/workerd/issues/1"), |
| 23 | + ])).not.toThrow(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("rejects results from another repository", () => { |
| 27 | + expect(() => assertIssueSearchResultsInRepo("cloudflare", "workerd", [ |
| 28 | + issueAt("https://github.com/cloudflare/quiche/issues/1"), |
| 29 | + ])).toThrow("outside the connected repository"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("does not accept repository names that only share a prefix", () => { |
| 33 | + expect(() => assertIssueSearchResultsInRepo("cloudflare", "workerd", [ |
| 34 | + issueAt("https://github.com/cloudflare/workerd-private/issues/1"), |
| 35 | + ])).toThrow("outside the connected repository"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("rejects pull requests returned by an injected search expression", () => { |
| 39 | + expect(() => assertIssueSearchResultsInRepo("cloudflare", "workerd", [ |
| 40 | + issueAt("https://github.com/cloudflare/workerd/pull/1"), |
| 41 | + ])).toThrow("non-issue result"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("rejects malformed and non-GitHub result URLs", () => { |
| 45 | + expect(() => assertIssueSearchResultsInRepo("cloudflare", "workerd", [ |
| 46 | + issueAt("not a URL"), |
| 47 | + ])).toThrow("outside the connected repository"); |
| 48 | + expect(() => assertIssueSearchResultsInRepo("cloudflare", "workerd", [ |
| 49 | + issueAt("https://example.com/cloudflare/workerd/issues/1"), |
| 50 | + ])).toThrow("outside the connected repository"); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe("buildIssueSearchQuery", () => { |
| 55 | + it("builds a benign literal phrase search with structured filters", () => { |
| 56 | + expect(buildIssueSearchQuery("cloudflare", "workerd", { |
| 57 | + text: "durable objects", |
| 58 | + state: "open", |
| 59 | + labels: ["bug"], |
| 60 | + author: "jasnell", |
| 61 | + })).toBe( |
| 62 | + '"durable objects" repo:cloudflare/workerd is:issue state:open label:"bug" author:"jasnell"', |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it("quotes every caller-controlled query fragment", () => { |
| 67 | + expect(buildIssueSearchQuery("cloudflare", "workerd", { |
| 68 | + text: "repo:cloudflare/quiche OR scheduler", |
| 69 | + author: "jasnell OR repo:cloudflare/quiche", |
| 70 | + assignee: "octocat OR repo:cloudflare/quiche", |
| 71 | + })).toBe( |
| 72 | + '"repo:cloudflare/quiche OR scheduler" repo:cloudflare/workerd is:issue ' |
| 73 | + + 'author:"jasnell OR repo:cloudflare/quiche" assignee:"octocat OR repo:cloudflare/quiche"', |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it("escapes quotes inside plain search text", () => { |
| 78 | + expect(buildIssueSearchQuery("cloudflare", "workerd", { |
| 79 | + text: 'bug" OR repo:cloudflare/quiche OR "', |
| 80 | + })).toBe('"bug\\" OR repo:cloudflare/quiche OR \\"" repo:cloudflare/workerd is:issue'); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe("GitHubApi.searchIssuesConditional", () => { |
| 85 | + it("enables GitHub advanced search parsing", async () => { |
| 86 | + let requestUrl: URL | undefined; |
| 87 | + vi.stubGlobal("fetch", vi.fn(async (input: string | URL | Request) => { |
| 88 | + requestUrl = new URL(String(input)); |
| 89 | + return new Response(JSON.stringify({ items: [] }), { |
| 90 | + headers: { "content-type": "application/json" }, |
| 91 | + }); |
| 92 | + })); |
| 93 | + |
| 94 | + const api = new GitHubApi(async () => "test-token"); |
| 95 | + await api.searchIssuesConditional( |
| 96 | + "repo:cloudflare/quiche OR repo:cloudflare/workerd is:issue", |
| 97 | + 1, |
| 98 | + 100, |
| 99 | + ); |
| 100 | + |
| 101 | + expect(requestUrl?.searchParams.get("advanced_search")).toBe("true"); |
| 102 | + }); |
| 103 | +}); |
0 commit comments