Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestQuery.test.ts

374 lines14.7 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 applyPullRequestFilterChange,
4 boundPullRequestFilters,
5 combinePullRequestFilters,
6 parsePullRequestQuery,
7 pullRequestMenuFilters,
8} from "./pullRequestQuery.ts";
9​
10describe("reading qualifiers out of a typed query", () => {
11 it("keeps quoted values whole and separates negation", () => {
12 expect(parsePullRequestQuery('label:"needs design" -label:wip Fix the parser')).toEqual({
13 text: "Fix the parser",
14 filters: { labels: [["needs design"]], excludedLabels: ["wip"] },
15 });
16 });
17​
18 it("keeps the spaces in a quoted name of a list, and drops an empty part", () => {
19 expect(parsePullRequestQuery('label:"needs design","wip"').filters.labels).toEqual([
20 ["needs design", "wip"],
21 ]);
22 expect(parsePullRequestQuery("label:a,,b").filters.labels).toEqual([["a", "b"]]);
23 });
24​
25 it("makes each label qualifier its own group, so the groups are an AND", () => {
26 expect(parsePullRequestQuery("label:bug label:a,b").filters.labels).toEqual([
27 ["bug"],
28 ["a", "b"],
29 ]);
30 });
31​
32 it("reads the scalar qualifiers in GitHub's own spelling", () => {
33 expect(
34 parsePullRequestQuery("author:octocat DRAFT:false review:changes_requested status:failure")
35 .filters,
36 ).toEqual({
37 author: "octocat",
38 draft: "hide",
39 review: "changes-requested",
40 checks: "failing",
41 });
42 expect(parsePullRequestQuery("-draft:true").filters).toEqual({ draft: "hide" });
43 expect(
44 ["is:pr is:open is:draft", "-IS:DRAFT", "draft:false is:draft"].map((query) =>
45 parsePullRequestQuery(query),
46 ),
47 ).toEqual([
48 { text: "", filters: { draft: "only" } },
49 { text: "", filters: { draft: "hide" } },
50 { text: "", filters: { draft: "only" } },
51 ]);
52 });
53​
54 it("leaves an unknown value and a stray colon as text, and reads an unknown key as a label", () => {
55 const parsed = parsePullRequestQuery("scope:v2 draft:maybe status: parser");
56 expect(parsed.filters).toEqual({ labels: [["scope:v2"]] });
57 expect(parsed.text).toBe("draft:maybe status: parser");
58 expect(parsePullRequestQuery("review:constructor checks:__proto__ Status:toString")).toEqual({
59 text: "review:constructor checks:__proto__ Status:toString",
60 filters: {},
61 });
62 });
63​
64 it("reads a quoted name holding a comma as the one label it is", () => {
65 expect(parsePullRequestQuery('label:"needs,triage"').filters.labels).toEqual([
66 ["needs,triage"],
67 ]);
68 expect(parsePullRequestQuery('label:"needs,triage",wip').filters.labels).toEqual([
69 ["needs,triage", "wip"],
70 ]);
71 expect(parsePullRequestQuery('-label:"needs,triage"').filters.excludedLabels).toEqual([
72 "needs,triage",
73 ]);
74 });
75​
76 it("reads an escaped quote inside a quoted name as part of the label", () => {
77 expect(parsePullRequestQuery(String.raw`label:"needs \"repro\"" fix`)).toEqual({
78 text: "fix",
79 filters: { labels: [['needs "repro"']] },
80 });
81 expect(parsePullRequestQuery(String.raw`label:"say \"a,b\"",wip`).filters.labels).toEqual([
82 ['say "a,b"', "wip"],
83 ]);
84 });
85​
86 it("still closes a quoted name that ends in a backslash", () => {
87 expect(parsePullRequestQuery(String.raw`label:"ends with \" fix`)).toEqual({
88 text: "fix",
89 filters: { labels: [["ends with \\"]] },
90 });
91 expect(parsePullRequestQuery(String.raw`label:"ends with \","draft"`).filters.labels).toEqual([
92 ["ends with \\", "draft"],
93 ]);
94 });
95​
96 // regression: #30131 a query pasted from github.com lists nothing
97 it("ignores GitHub's own search qualifiers instead of reading them as labels", () => {
98 expect(
99 parsePullRequestQuery(
100 "is:pr state:open archived:false repo:org/repo org:org user:alice sort:updated-desc -repo:org/fork -state:closed fix",
101 ),
102 ).toEqual({ text: "fix", filters: {} });
103 expect(
104 parsePullRequestQuery(
105 'assignee:@me review-requested:@me base:main head:feature milestone:"v1.0" -assignee:me -base:main ASSIGNEE:@me fix',
106 ),
107 ).toEqual({ text: "fix", filters: {} });
108 expect(
109 parsePullRequestQuery(
110 "assignee:@me -base:main author:octocat area:web -area:mobile label:milestone:v1 fix",
111 ),
112 ).toEqual({
113 text: "fix",
114 filters: {
115 author: "octocat",
116 labels: [["area:web"], ["milestone:v1"]],
117 excludedLabels: ["area:mobile"],
118 },
119 });
120 expect(parsePullRequestQuery("type:pr is:open bug")).toEqual(
121 parsePullRequestQuery("is:pr is:open bug"),
122 );
123 expect(parsePullRequestQuery("TYPE:PR bug")).toEqual({ text: "bug", filters: {} });
124 expect(parsePullRequestQuery("type:bug -type:pr label:type:pr").filters).toEqual({
125 labels: [["type:bug"], ["type:pr"]],
126 excludedLabels: ["type:pr"],
127 });
128 });
129​
130 it("drops negated author, review and checks qualifiers, keeping unknown values as text", () => {
131 expect(
132 parsePullRequestQuery(
133 "author:octocat -author:app/dependabot -review:approved -checks:passing -status:failure fix",
134 ),
135 ).toEqual({ text: "fix", filters: { author: "octocat" } });
136 expect(
137 ["-status:pending fix", "status:pending fix", "STATUS:Pending fix"].map((query) =>
138 parsePullRequestQuery(query),
139 ),
140 ).toEqual(Array.from({ length: 3 }, () => ({ text: "fix", filters: {} })));
141 expect(parsePullRequestQuery("checks:pending fix").text).toBe("checks:pending fix");
142 expect(
143 parsePullRequestQuery("-review:constructor -checks:__proto__ -status:maybe -author: fix"),
144 ).toEqual({
145 text: "-review:constructor -checks:__proto__ -status:maybe -author: fix",
146 filters: {},
147 });
148 });
149​
150 it("sets no author when the query names more than one", () => {
151 expect(parsePullRequestQuery("author:alice author:bob").filters).toEqual({});
152 expect(parsePullRequestQuery("author:me author:@me").filters).toEqual({ author: "me" });
153 });
154​
155 it("cuts a typed query back to what the contract carries", () => {
156 const many = Array.from({ length: 14 }, (_, index) => `l${index}`).join(",");
157 const long = "x".repeat(240);
158 const parsed = parsePullRequestQuery(
159 `label:${many} label:${long} author:${long} ${"fix ".repeat(80)}`,
160 );
161 expect(parsed.filters.labels?.[0]).toHaveLength(10);
162 expect(parsed.filters.labels?.[1]).toEqual(["x".repeat(200)]);
163 expect(parsed.filters.author).toBe("x".repeat(200));
164 expect(parsed.text).toBe("fix ".repeat(50).trim());
165​
166 const prefix = "x".repeat(199);
167 const split = `${prefix}😀`;
168 expect(parsePullRequestQuery(split).text).toBe(prefix);
169 expect(parsePullRequestQuery(`${"x".repeat(198)}😀x`).text).toBe(`${"x".repeat(198)}😀`);
170 expect(parsePullRequestQuery(`label:${split} -label:${split} author:${split}`).filters).toEqual(
171 {
172 labels: [[prefix]],
173 excludedLabels: [prefix],
174 author: prefix,
175 },
176 );
177 });
178​
179 it("spends the cap on distinct labels, not on repeats of one", () => {
180 expect(parsePullRequestQuery(`${"label:bug ".repeat(10)}label:release`).filters.labels).toEqual(
181 [["bug"], ["release"]],
182 );
183 expect(parsePullRequestQuery(`label:${"bug,".repeat(10)}release`).filters.labels).toEqual([
184 ["bug", "release"],
185 ]);
186 expect(
187 parsePullRequestQuery(`${"-label:wip ".repeat(10)}-label:draft`).filters.excludedLabels,
188 ).toEqual(["wip", "draft"]);
189 });
190});
191​
192describe("colon-namespaced labels typed as a search", () => {
193 it("reads an unknown key as the label it almost always is", () => {
194 expect(parsePullRequestQuery("size:XXL").filters.labels).toEqual([["size:XXL"]]);
195 expect(parsePullRequestQuery("vouch:trusted").filters.labels).toEqual([["vouch:trusted"]]);
196 expect(parsePullRequestQuery("size:XXL").text).toBe("");
197 });
198​
199 it("reads the key as the namespace of every bare name in its list", () => {
200 expect(parsePullRequestQuery("size:S,XS").filters.labels).toEqual([["size:S", "size:XS"]]);
201 });
202​
203 it("leaves a name that already carries its own namespace alone", () => {
204 expect(parsePullRequestQuery("size:S,size:XS").filters.labels).toEqual([["size:S", "size:XS"]]);
205 expect(parsePullRequestQuery("security:cve:critical,area:web,low").filters.labels).toEqual([
206 ["security:cve:critical", "area:web", "security:low"],
207 ]);
208 expect(parsePullRequestQuery("size:,area:web").filters.labels).toEqual([["area:web"]]);
209 });
210​
211 // regression: #19934 negated key:value qualifiers become namespaced label exclusions
212 it("excludes one the same way", () => {
213 expect(parsePullRequestQuery("-size:XXL").filters.excludedLabels).toEqual(["size:XXL"]);
214 expect(parsePullRequestQuery("-security:cve:critical").filters.excludedLabels).toEqual([
215 "security:cve:critical",
216 ]);
217 expect(parsePullRequestQuery("-size:,area:web").filters.excludedLabels).toEqual(["area:web"]);
218 expect(parsePullRequestQuery("-size:S,XS").filters.excludedLabels).toEqual([
219 "size:S",
220 "size:XS",
221 ]);
222 });
223​
224 it("keeps a quoted token as text, which is the way back to a literal search", () => {
225 const parsed = parsePullRequestQuery('"size:XXL"');
226 expect(parsed.filters.labels).toBeUndefined();
227 expect(parsed.text).toBe('"size:XXL"');
228 });
229​
230 it("reads a hyphenated namespace as a label too", () => {
231 expect(parsePullRequestQuery("priority-1:high").filters.labels).toEqual([["priority-1:high"]]);
232 expect(parsePullRequestQuery("priority-1:high").text).toBe("");
233 });
234​
235 it("leaves a pasted link alone rather than naming a label after its scheme", () => {
236 const parsed = parsePullRequestQuery("https://github.com/pingdotgg/t3code/pull/1");
237 expect(parsed.filters.labels).toBeUndefined();
238 expect(parsed.text).toBe("https://github.com/pingdotgg/t3code/pull/1");
239 });
240​
241 it("mixes with the keys it does know, and with plain words", () => {
242 const parsed = parsePullRequestQuery("area:web draft:true wizard label:bug");
243 expect(parsed.filters).toEqual({ labels: [["area:web"], ["bug"]], draft: "only" });
244 expect(parsed.text).toBe("wizard");
245 });
246});
247​
248describe("changing a menu filter after typing its qualifier", () => {
249 it("replaces or clears a typed author so the menu and request agree", () => {
250 const update = applyPullRequestFilterChange("author:alice fix", {}, { author: "bob" });
251 expect(update.query).toBe("fix");
252 expect({ ...update.filters, ...parsePullRequestQuery(update.query).filters }).toEqual({
253 author: "bob",
254 });
255 expect(
256 applyPullRequestFilterChange("author:@me fix", {}, { author: "me", checks: "passing" }),
257 ).toEqual({ query: "author:@me fix", filters: { checks: "passing" } });
258 expect(applyPullRequestFilterChange("author:@me fix", {}, {})).toEqual({
259 query: "fix",
260 filters: {},
261 });
262 });
263​
264 it("replaces or clears a typed draft qualifier and keeps the rest of the query", () => {
265 expect(
266 applyPullRequestFilterChange('draft:false fix label:"needs design"', {}, { draft: "only" }),
267 ).toEqual({ query: 'fix label:"needs design"', filters: { draft: "only" } });
268 expect(
269 applyPullRequestFilterChange('draft:true "draft:false" fix', { draft: "only" }, {}),
270 ).toEqual({
271 query: '"draft:false" fix',
272 filters: {},
273 });
274 });
275​
276 it("keeps the typed query and the stored menu draft when another filter changes", () => {
277 expect(
278 applyPullRequestFilterChange(
279 "draft:false fix",
280 { draft: "only" },
281 { draft: "hide", checks: "passing" },
282 ),
283 ).toEqual({ query: "draft:false fix", filters: { draft: "only", checks: "passing" } });
284 });
285​
286 it("replaces or clears a typed checks or status qualifier", () => {
287 expect(applyPullRequestFilterChange("checks:passing fix", {}, { checks: "failing" })).toEqual({
288 query: "fix",
289 filters: { checks: "failing" },
290 });
291 expect(
292 applyPullRequestFilterChange(
293 'status:success "checks:passing" fix',
294 { checks: "failing" },
295 {},
296 ),
297 ).toEqual({ query: '"checks:passing" fix', filters: {} });
298 });
299​
300 it("replaces a typed review qualifier and leaves the other typed qualifiers alone", () => {
301 expect(
302 applyPullRequestFilterChange(
303 "review:approved checks:failing draft:true fix",
304 {},
305 { review: "none", checks: "failing", draft: "only" },
306 ),
307 ).toEqual({ query: "checks:failing draft:true fix", filters: { review: "none" } });
308 });
309​
310 it("keeps text the tokenizer skips, like an unclosed quote, when it strips a qualifier", () => {
311 expect(
312 applyPullRequestFilterChange('checks:passing "size:XL', {}, { checks: "failing" }).query,
313 ).toBe('"size:XL');
314 expect(
315 applyPullRequestFilterChange("fix review:approved wip", {}, { review: "none" }).query,
316 ).toBe("fix wip");
317 });
318​
319 it("shows typed qualifiers in place of the stored menu values", () => {
320 expect(
321 pullRequestMenuFilters(
322 parsePullRequestQuery("author:alice status:success review:none label:bug fix").filters,
323 { author: "bob", checks: "failing", draft: "hide", labels: [["wip"]] },
324 ),
325 ).toEqual({
326 author: "alice",
327 checks: "passing",
328 review: "none",
329 draft: "hide",
330 labels: [["wip"]],
331 });
332 for (const query of ["author:@me", "author:ME", "author:me author:@me"]) {
333 expect(pullRequestMenuFilters(parsePullRequestQuery(query).filters, {}).author).toBe("me");
334 }
335 expect(pullRequestMenuFilters(parsePullRequestQuery("author:OctoCat").filters, {}).author).toBe(
336 "OctoCat",
337 );
338 });
339});
340​
341describe("combining the menu filters with a typed query", () => {
342 it("applies typed and picked labels together while typed qualifiers win", () => {
343 expect(
344 combinePullRequestFilters(
345 { labels: [["docs"]], excludedLabels: ["wip"], author: "bob" },
346 parsePullRequestQuery("label:bug -label:stale author:alice").filters,
347 ),
348 ).toEqual({
349 labels: [["docs"], ["bug"]],
350 excludedLabels: ["wip", "stale"],
351 author: "alice",
352 });
353 expect(combinePullRequestFilters({ draft: "hide" }, {})).toEqual({ draft: "hide" });
354 });
355​
356 it("drops a picked label group the search already holds, ignoring case and order", () => {
357 expect(
358 combinePullRequestFilters(
359 { labels: [["Bug"], ["docs", "wip"]], excludedLabels: ["Stale"] },
360 { labels: [["bug"], ["wip", "DOCS"], ["ui"]], excludedLabels: ["stale"] },
361 ),
362 ).toEqual({ labels: [["Bug"], ["docs", "wip"], ["ui"]], excludedLabels: ["Stale"] });
363 });
364​
365 it("keeps every distinct group for local filtering and cuts only the request", () => {
366 const picked = Array.from({ length: 9 }, (_, index) => [`picked${index}`]);
367 const combined = combinePullRequestFilters(
368 { labels: picked },
369 { labels: [["typed0"], ["typed1"]] },
370 );
371 expect(combined.labels).toEqual([...picked, ["typed0"], ["typed1"]]);
372 expect(boundPullRequestFilters(combined).labels).toEqual([...picked, ["typed0"]]);
373 });
374});