Open History to see the last commit
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 parsePullRequestQuery("-review:constructor -checks:__proto__ -status:maybe -author: fix"),
138 ).toEqual({
139 text: "-review:constructor -checks:__proto__ -status:maybe -author: fix",
140 filters: {},
141 });
142 });
143
144 it("sets no author when the query names more than one", () => {
145 expect(parsePullRequestQuery("author:alice author:bob").filters).toEqual({});
146 expect(parsePullRequestQuery("author:me author:@me").filters).toEqual({ author: "me" });
147 });
148
149 it("cuts a typed query back to what the contract carries", () => {
150 const many = Array.from({ length: 14 }, (_, index) => `l${index}`).join(",");
151 const long = "x".repeat(240);
152 const parsed = parsePullRequestQuery(
153 `label:${many} label:${long} author:${long} ${"fix ".repeat(80)}`,
154 );
155 expect(parsed.filters.labels?.[0]).toHaveLength(10);
156 expect(parsed.filters.labels?.[1]).toEqual(["x".repeat(200)]);
157 expect(parsed.filters.author).toBe("x".repeat(200));
158 expect(parsed.text).toBe("fix ".repeat(50).trim());
159
160 const prefix = "x".repeat(199);
161 const split = `${prefix}😀`;
162 expect(parsePullRequestQuery(split).text).toBe(prefix);
163 expect(parsePullRequestQuery(`${"x".repeat(198)}😀x`).text).toBe(`${"x".repeat(198)}😀`);
164 expect(parsePullRequestQuery(`label:${split} -label:${split} author:${split}`).filters).toEqual(
165 {
166 labels: [[prefix]],
167 excludedLabels: [prefix],
168 author: prefix,
169 },
170 );
171 });
172});
173
174describe("colon-namespaced labels typed as a search", () => {
175 it("reads an unknown key as the label it almost always is", () => {
176 expect(parsePullRequestQuery("size:XXL").filters.labels).toEqual([["size:XXL"]]);
177 expect(parsePullRequestQuery("vouch:trusted").filters.labels).toEqual([["vouch:trusted"]]);
178 expect(parsePullRequestQuery("size:XXL").text).toBe("");
179 });
180
181 it("reads the key as the namespace of every bare name in its list", () => {
182 expect(parsePullRequestQuery("size:S,XS").filters.labels).toEqual([["size:S", "size:XS"]]);
183 });
184
185 it("leaves a name that already carries its own namespace alone", () => {
186 expect(parsePullRequestQuery("size:S,size:XS").filters.labels).toEqual([["size:S", "size:XS"]]);
187 expect(parsePullRequestQuery("security:cve:critical,area:web,low").filters.labels).toEqual([
188 ["security:cve:critical", "area:web", "security:low"],
189 ]);
190 expect(parsePullRequestQuery("size:,area:web").filters.labels).toEqual([["area:web"]]);
191 });
192
193 // regression: #19934 negated key:value qualifiers become namespaced label exclusions
194 it("excludes one the same way", () => {
195 expect(parsePullRequestQuery("-size:XXL").filters.excludedLabels).toEqual(["size:XXL"]);
196 expect(parsePullRequestQuery("-security:cve:critical").filters.excludedLabels).toEqual([
197 "security:cve:critical",
198 ]);
199 expect(parsePullRequestQuery("-size:,area:web").filters.excludedLabels).toEqual(["area:web"]);
200 expect(parsePullRequestQuery("-size:S,XS").filters.excludedLabels).toEqual([
201 "size:S",
202 "size:XS",
203 ]);
204 });
205
206 it("keeps a quoted token as text, which is the way back to a literal search", () => {
207 const parsed = parsePullRequestQuery('"size:XXL"');
208 expect(parsed.filters.labels).toBeUndefined();
209 expect(parsed.text).toBe('"size:XXL"');
210 });
211
212 it("reads a hyphenated namespace as a label too", () => {
213 expect(parsePullRequestQuery("priority-1:high").filters.labels).toEqual([["priority-1:high"]]);
214 expect(parsePullRequestQuery("priority-1:high").text).toBe("");
215 });
216
217 it("leaves a pasted link alone rather than naming a label after its scheme", () => {
218 const parsed = parsePullRequestQuery("https://github.com/pingdotgg/t3code/pull/1");
219 expect(parsed.filters.labels).toBeUndefined();
220 expect(parsed.text).toBe("https://github.com/pingdotgg/t3code/pull/1");
221 });
222
223 it("mixes with the keys it does know, and with plain words", () => {
224 const parsed = parsePullRequestQuery("area:web draft:true wizard label:bug");
225 expect(parsed.filters).toEqual({ labels: [["area:web"], ["bug"]], draft: "only" });
226 expect(parsed.text).toBe("wizard");
227 });
228});
229
230describe("changing a menu filter after typing its qualifier", () => {
231 it("replaces or clears a typed author so the menu and request agree", () => {
232 const update = applyPullRequestFilterChange("author:alice fix", {}, { author: "bob" });
233 expect(update.query).toBe("fix");
234 expect({ ...update.filters, ...parsePullRequestQuery(update.query).filters }).toEqual({
235 author: "bob",
236 });
237 expect(
238 applyPullRequestFilterChange("author:@me fix", {}, { author: "me", checks: "passing" }),
239 ).toEqual({ query: "author:@me fix", filters: { checks: "passing" } });
240 expect(applyPullRequestFilterChange("author:@me fix", {}, {})).toEqual({
241 query: "fix",
242 filters: {},
243 });
244 });
245
246 it("replaces or clears a typed draft qualifier and keeps the rest of the query", () => {
247 expect(
248 applyPullRequestFilterChange('draft:false fix label:"needs design"', {}, { draft: "only" }),
249 ).toEqual({ query: 'fix label:"needs design"', filters: { draft: "only" } });
250 expect(
251 applyPullRequestFilterChange('draft:true "draft:false" fix', { draft: "only" }, {}),
252 ).toEqual({
253 query: '"draft:false" fix',
254 filters: {},
255 });
256 });
257
258 it("keeps the typed query and the stored menu draft when another filter changes", () => {
259 expect(
260 applyPullRequestFilterChange(
261 "draft:false fix",
262 { draft: "only" },
263 { draft: "hide", checks: "passing" },
264 ),
265 ).toEqual({ query: "draft:false fix", filters: { draft: "only", checks: "passing" } });
266 });
267
268 it("replaces or clears a typed checks or status qualifier", () => {
269 expect(applyPullRequestFilterChange("checks:passing fix", {}, { checks: "failing" })).toEqual({
270 query: "fix",
271 filters: { checks: "failing" },
272 });
273 expect(
274 applyPullRequestFilterChange(
275 'status:success "checks:passing" fix',
276 { checks: "failing" },
277 {},
278 ),
279 ).toEqual({ query: '"checks:passing" fix', filters: {} });
280 });
281
282 it("replaces a typed review qualifier and leaves the other typed qualifiers alone", () => {
283 expect(
284 applyPullRequestFilterChange(
285 "review:approved checks:failing draft:true fix",
286 {},
287 { review: "none", checks: "failing", draft: "only" },
288 ),
289 ).toEqual({ query: "checks:failing draft:true fix", filters: { review: "none" } });
290 });
291
292 it("keeps text the tokenizer skips, like an unclosed quote, when it strips a qualifier", () => {
293 expect(
294 applyPullRequestFilterChange('checks:passing "size:XL', {}, { checks: "failing" }).query,
295 ).toBe('"size:XL');
296 expect(
297 applyPullRequestFilterChange("fix review:approved wip", {}, { review: "none" }).query,
298 ).toBe("fix wip");
299 });
300
301 it("shows typed qualifiers in place of the stored menu values", () => {
302 expect(
303 pullRequestMenuFilters(
304 parsePullRequestQuery("author:alice status:success review:none label:bug fix").filters,
305 { author: "bob", checks: "failing", draft: "hide", labels: [["wip"]] },
306 ),
307 ).toEqual({
308 author: "alice",
309 checks: "passing",
310 review: "none",
311 draft: "hide",
312 labels: [["wip"]],
313 });
314 for (const query of ["author:@me", "author:ME", "author:me author:@me"]) {
315 expect(pullRequestMenuFilters(parsePullRequestQuery(query).filters, {}).author).toBe("me");
316 }
317 expect(pullRequestMenuFilters(parsePullRequestQuery("author:OctoCat").filters, {}).author).toBe(
318 "OctoCat",
319 );
320 });
321});
322
323describe("combining the menu filters with a typed query", () => {
324 it("applies typed and picked labels together while typed qualifiers win", () => {
325 expect(
326 combinePullRequestFilters(
327 { labels: [["docs"]], excludedLabels: ["wip"], author: "bob" },
328 parsePullRequestQuery("label:bug -label:stale author:alice").filters,
329 ),
330 ).toEqual({
331 labels: [["docs"], ["bug"]],
332 excludedLabels: ["wip", "stale"],
333 author: "alice",
334 });
335 expect(combinePullRequestFilters({ draft: "hide" }, {})).toEqual({ draft: "hide" });
336 });
337
338 it("drops a picked label group the search already holds, ignoring case and order", () => {
339 expect(
340 combinePullRequestFilters(
341 { labels: [["Bug"], ["docs", "wip"]], excludedLabels: ["Stale"] },
342 { labels: [["bug"], ["wip", "DOCS"], ["ui"]], excludedLabels: ["stale"] },
343 ),
344 ).toEqual({ labels: [["Bug"], ["docs", "wip"], ["ui"]], excludedLabels: ["Stale"] });
345 });
346
347 it("keeps every distinct group for local filtering and cuts only the request", () => {
348 const picked = Array.from({ length: 9 }, (_, index) => [`picked${index}`]);
349 const combined = combinePullRequestFilters(
350 { labels: picked },
351 { labels: [["typed0"], ["typed1"]] },
352 );
353 expect(combined.labels).toEqual([...picked, ["typed0"], ["typed1"]]);
354 expect(boundPullRequestFilters(combined).labels).toEqual([...picked, ["typed0"]]);
355 });
356});