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

pullRequestQuery.test.ts

261 lines10.4 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 applyPullRequestFilterChange,
4 parsePullRequestQuery,
5 pullRequestMenuFilters,
6} from "./pullRequestQuery.ts";
7​
8describe("reading qualifiers out of a typed query", () => {
9 it("keeps quoted values whole and separates negation", () => {
10 expect(parsePullRequestQuery('label:"needs design" -label:wip Fix the parser')).toEqual({
11 text: "Fix the parser",
12 filters: { labels: [["needs design"]], excludedLabels: ["wip"] },
13 });
14 });
15​
16 it("keeps the spaces in a quoted name of a list, and drops an empty part", () => {
17 expect(parsePullRequestQuery('label:"needs design","wip"').filters.labels).toEqual([
18 ["needs design", "wip"],
19 ]);
20 expect(parsePullRequestQuery("label:a,,b").filters.labels).toEqual([["a", "b"]]);
21 });
22​
23 it("makes each label qualifier its own group, so the groups are an AND", () => {
24 expect(parsePullRequestQuery("label:bug label:a,b").filters.labels).toEqual([
25 ["bug"],
26 ["a", "b"],
27 ]);
28 });
29​
30 it("reads the scalar qualifiers in GitHub's own spelling", () => {
31 expect(
32 parsePullRequestQuery("author:octocat DRAFT:false review:changes_requested status:failure")
33 .filters,
34 ).toEqual({
35 author: "octocat",
36 draft: "hide",
37 review: "changes-requested",
38 checks: "failing",
39 });
40 expect(parsePullRequestQuery("-draft:true").filters).toEqual({ draft: "hide" });
41 expect(
42 ["is:pr is:open is:draft", "-IS:DRAFT", "draft:false is:draft"].map((query) =>
43 parsePullRequestQuery(query),
44 ),
45 ).toEqual([
46 { text: "", filters: { draft: "only" } },
47 { text: "", filters: { draft: "hide" } },
48 { text: "", filters: { draft: "only" } },
49 ]);
50 });
51​
52 it("leaves an unknown value and a stray colon as text, and reads an unknown key as a label", () => {
53 const parsed = parsePullRequestQuery("milestone:v2 draft:maybe status: parser");
54 expect(parsed.filters).toEqual({ labels: [["milestone:v2"]] });
55 expect(parsed.text).toBe("draft:maybe status: parser");
56 expect(parsePullRequestQuery("review:constructor checks:__proto__ Status:toString")).toEqual({
57 text: "review:constructor checks:__proto__ Status:toString",
58 filters: {},
59 });
60 });
61​
62 it("reads a quoted name holding a comma as the one label it is", () => {
63 expect(parsePullRequestQuery('label:"needs,triage"').filters.labels).toEqual([
64 ["needs,triage"],
65 ]);
66 expect(parsePullRequestQuery('label:"needs,triage",wip').filters.labels).toEqual([
67 ["needs,triage", "wip"],
68 ]);
69 expect(parsePullRequestQuery('-label:"needs,triage"').filters.excludedLabels).toEqual([
70 "needs,triage",
71 ]);
72 });
73​
74 it("reads an escaped quote inside a quoted name as part of the label", () => {
75 expect(parsePullRequestQuery(String.raw`label:"needs \"repro\"" fix`)).toEqual({
76 text: "fix",
77 filters: { labels: [['needs "repro"']] },
78 });
79 expect(parsePullRequestQuery(String.raw`label:"say \"a,b\"",wip`).filters.labels).toEqual([
80 ['say "a,b"', "wip"],
81 ]);
82 });
83​
84 it("still closes a quoted name that ends in a backslash", () => {
85 expect(parsePullRequestQuery(String.raw`label:"ends with \" fix`)).toEqual({
86 text: "fix",
87 filters: { labels: [["ends with \\"]] },
88 });
89 expect(parsePullRequestQuery(String.raw`label:"ends with \","draft"`).filters.labels).toEqual([
90 ["ends with \\", "draft"],
91 ]);
92 });
93​
94 // regression: #30131 a query pasted from github.com lists nothing
95 it("ignores GitHub's own search qualifiers instead of reading them as labels", () => {
96 expect(
97 parsePullRequestQuery(
98 "is:pr state:open archived:false repo:org/repo org:org user:alice sort:updated-desc -repo:org/fork -state:closed fix",
99 ),
100 ).toEqual({ text: "fix", filters: {} });
101 expect(parsePullRequestQuery("type:pr is:open bug")).toEqual(
102 parsePullRequestQuery("is:pr is:open bug"),
103 );
104 expect(parsePullRequestQuery("TYPE:PR bug")).toEqual({ text: "bug", filters: {} });
105 expect(parsePullRequestQuery("type:bug -type:pr label:type:pr").filters).toEqual({
106 labels: [["type:bug"], ["type:pr"]],
107 excludedLabels: ["type:pr"],
108 });
109 });
110​
111 it("sets no author when the query names more than one", () => {
112 expect(parsePullRequestQuery("author:alice author:bob").filters).toEqual({});
113 expect(parsePullRequestQuery("author:me author:@me").filters).toEqual({ author: "me" });
114 });
115​
116 it("cuts a typed query back to what the contract carries", () => {
117 const many = Array.from({ length: 14 }, (_, index) => `l${index}`).join(",");
118 const long = "x".repeat(240);
119 const parsed = parsePullRequestQuery(
120 `label:${many} label:${long} author:${long} ${"fix ".repeat(80)}`,
121 );
122 expect(parsed.filters.labels?.[0]).toHaveLength(10);
123 expect(parsed.filters.labels?.[1]).toEqual(["x".repeat(200)]);
124 expect(parsed.filters.author).toBe("x".repeat(200));
125 expect(parsed.text).toBe("fix ".repeat(50).trim());
126​
127 const prefix = "x".repeat(199);
128 const split = `${prefix}😀`;
129 expect(parsePullRequestQuery(split).text).toBe(prefix);
130 expect(parsePullRequestQuery(`${"x".repeat(198)}😀x`).text).toBe(`${"x".repeat(198)}😀`);
131 expect(parsePullRequestQuery(`label:${split} -label:${split} author:${split}`).filters).toEqual(
132 {
133 labels: [[prefix]],
134 excludedLabels: [prefix],
135 author: prefix,
136 },
137 );
138 });
139});
140​
141describe("colon-namespaced labels typed as a search", () => {
142 it("reads an unknown key as the label it almost always is", () => {
143 expect(parsePullRequestQuery("size:XXL").filters.labels).toEqual([["size:XXL"]]);
144 expect(parsePullRequestQuery("vouch:trusted").filters.labels).toEqual([["vouch:trusted"]]);
145 expect(parsePullRequestQuery("size:XXL").text).toBe("");
146 });
147​
148 it("reads the key as the namespace of every bare name in its list", () => {
149 expect(parsePullRequestQuery("size:S,XS").filters.labels).toEqual([["size:S", "size:XS"]]);
150 });
151​
152 it("leaves a name that already carries its own namespace alone", () => {
153 expect(parsePullRequestQuery("size:S,size:XS").filters.labels).toEqual([["size:S", "size:XS"]]);
154 expect(parsePullRequestQuery("security:cve:critical,area:web,low").filters.labels).toEqual([
155 ["security:cve:critical", "area:web", "security:low"],
156 ]);
157 expect(parsePullRequestQuery("size:,area:web").filters.labels).toEqual([["area:web"]]);
158 });
159​
160 // regression: #19934 negated key:value qualifiers become namespaced label exclusions
161 it("excludes one the same way", () => {
162 expect(parsePullRequestQuery("-size:XXL").filters.excludedLabels).toEqual(["size:XXL"]);
163 expect(parsePullRequestQuery("-security:cve:critical").filters.excludedLabels).toEqual([
164 "security:cve:critical",
165 ]);
166 expect(parsePullRequestQuery("-size:,area:web").filters.excludedLabels).toEqual(["area:web"]);
167 expect(parsePullRequestQuery("-size:S,XS").filters.excludedLabels).toEqual([
168 "size:S",
169 "size:XS",
170 ]);
171 });
172​
173 it("keeps a quoted token as text, which is the way back to a literal search", () => {
174 const parsed = parsePullRequestQuery('"size:XXL"');
175 expect(parsed.filters.labels).toBeUndefined();
176 expect(parsed.text).toBe('"size:XXL"');
177 });
178​
179 it("reads a hyphenated namespace as a label too", () => {
180 expect(parsePullRequestQuery("priority-1:high").filters.labels).toEqual([["priority-1:high"]]);
181 expect(parsePullRequestQuery("priority-1:high").text).toBe("");
182 });
183​
184 it("leaves a pasted link alone rather than naming a label after its scheme", () => {
185 const parsed = parsePullRequestQuery("https://github.com/pingdotgg/t3code/pull/1");
186 expect(parsed.filters.labels).toBeUndefined();
187 expect(parsed.text).toBe("https://github.com/pingdotgg/t3code/pull/1");
188 });
189​
190 it("mixes with the keys it does know, and with plain words", () => {
191 const parsed = parsePullRequestQuery("area:web draft:true wizard label:bug");
192 expect(parsed.filters).toEqual({ labels: [["area:web"], ["bug"]], draft: "only" });
193 expect(parsed.text).toBe("wizard");
194 });
195});
196​
197describe("changing a menu filter after typing its qualifier", () => {
198 it("replaces or clears a typed draft qualifier and keeps the rest of the query", () => {
199 expect(
200 applyPullRequestFilterChange('draft:false fix label:"needs design"', {}, { draft: "only" }),
201 ).toEqual({ query: 'fix label:"needs design"', filters: { draft: "only" } });
202 expect(
203 applyPullRequestFilterChange('draft:true "draft:false" fix', { draft: "only" }, {}),
204 ).toEqual({
205 query: '"draft:false" fix',
206 filters: {},
207 });
208 });
209​
210 it("keeps the typed query and the stored menu draft when another filter changes", () => {
211 expect(
212 applyPullRequestFilterChange(
213 "draft:false fix",
214 { draft: "only" },
215 { draft: "hide", checks: "passing" },
216 ),
217 ).toEqual({ query: "draft:false fix", filters: { draft: "only", checks: "passing" } });
218 });
219​
220 it("replaces or clears a typed checks or status qualifier", () => {
221 expect(applyPullRequestFilterChange("checks:passing fix", {}, { checks: "failing" })).toEqual({
222 query: "fix",
223 filters: { checks: "failing" },
224 });
225 expect(
226 applyPullRequestFilterChange(
227 'status:success "checks:passing" fix',
228 { checks: "failing" },
229 {},
230 ),
231 ).toEqual({ query: '"checks:passing" fix', filters: {} });
232 });
233​
234 it("replaces a typed review qualifier and leaves the other typed qualifiers alone", () => {
235 expect(
236 applyPullRequestFilterChange(
237 "review:approved checks:failing draft:true fix",
238 {},
239 { review: "none", checks: "failing", draft: "only" },
240 ),
241 ).toEqual({ query: "checks:failing draft:true fix", filters: { review: "none" } });
242 });
243​
244 it("keeps text the tokenizer skips, like an unclosed quote, when it strips a qualifier", () => {
245 expect(
246 applyPullRequestFilterChange('checks:passing "size:XL', {}, { checks: "failing" }).query,
247 ).toBe('"size:XL');
248 expect(
249 applyPullRequestFilterChange("fix review:approved wip", {}, { review: "none" }).query,
250 ).toBe("fix wip");
251 });
252​
253 it("shows typed qualifiers in place of the stored menu values", () => {
254 expect(
255 pullRequestMenuFilters(
256 parsePullRequestQuery("status:success review:none label:bug fix").filters,
257 { checks: "failing", draft: "hide", labels: [["wip"]] },
258 ),
259 ).toEqual({ checks: "passing", review: "none", draft: "hide", labels: [["wip"]] });
260 });
261});