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