Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { applyPullRequestDraftFilterChange, parsePullRequestQuery } from "./pullRequestQuery.ts";
3
4describe("reading qualifiers out of a typed query", () => {
5 it("keeps quoted values whole and separates negation", () => {
6 expect(parsePullRequestQuery('label:"needs design" -label:wip Fix the parser')).toEqual({
7 text: "Fix the parser",
8 filters: { labels: [["needs design"]], excludedLabels: ["wip"] },
9 });
10 });
11
12 it("keeps the spaces in a quoted name of a list, and drops an empty part", () => {
13 expect(parsePullRequestQuery('label:"needs design","wip"').filters.labels).toEqual([
14 ["needs design", "wip"],
15 ]);
16 expect(parsePullRequestQuery("label:a,,b").filters.labels).toEqual([["a", "b"]]);
17 });
18
19 it("makes each label qualifier its own group, so the groups are an AND", () => {
20 expect(parsePullRequestQuery("label:bug label:a,b").filters.labels).toEqual([
21 ["bug"],
22 ["a", "b"],
23 ]);
24 });
25
26 it("reads the scalar qualifiers in GitHub's own spelling", () => {
27 expect(
28 parsePullRequestQuery("author:octocat DRAFT:false review:changes_requested status:failure")
29 .filters,
30 ).toEqual({
31 author: "octocat",
32 draft: "hide",
33 review: "changes-requested",
34 checks: "failing",
35 });
36 });
37
38 it("reads an assignee rather than a label named after one", () => {
39 expect(parsePullRequestQuery("assignee:me fix").filters).toEqual({ assignee: "me" });
40 expect(parsePullRequestQuery("-assignee:me").filters).toEqual({});
41 });
42
43 it("leaves an unknown value and a stray colon as text, and reads an unknown key as a label", () => {
44 const parsed = parsePullRequestQuery("milestone:v2 draft:maybe status: parser");
45 expect(parsed.filters).toEqual({ labels: [["milestone:v2"]] });
46 expect(parsed.text).toBe("draft:maybe status: parser");
47 });
48
49 it("reads a quoted name holding a comma as the one label it is", () => {
50 expect(parsePullRequestQuery('label:"needs,triage"').filters.labels).toEqual([
51 ["needs,triage"],
52 ]);
53 expect(parsePullRequestQuery('label:"needs,triage",wip').filters.labels).toEqual([
54 ["needs,triage", "wip"],
55 ]);
56 expect(parsePullRequestQuery('-label:"needs,triage"').filters.excludedLabels).toEqual([
57 "needs,triage",
58 ]);
59 });
60
61 it("cuts a typed query back to what the contract carries", () => {
62 const many = Array.from({ length: 14 }, (_, index) => `l${index}`).join(",");
63 const long = "x".repeat(240);
64 const parsed = parsePullRequestQuery(`label:${many} label:${long} author:${long}`);
65 expect(parsed.filters.labels?.[0]).toHaveLength(10);
66 expect(parsed.filters.labels?.[1]).toEqual(["x".repeat(200)]);
67 expect(parsed.filters.author).toBe("x".repeat(200));
68 });
69});
70
71describe("colon-namespaced labels typed as a search", () => {
72 it("reads an unknown key as the label it almost always is", () => {
73 expect(parsePullRequestQuery("size:XXL").filters.labels).toEqual([["size:XXL"]]);
74 expect(parsePullRequestQuery("vouch:trusted").filters.labels).toEqual([["vouch:trusted"]]);
75 expect(parsePullRequestQuery("size:XXL").text).toBe("");
76 });
77
78 it("reads the key as the namespace of every bare name in its list", () => {
79 expect(parsePullRequestQuery("size:S,XS").filters.labels).toEqual([["size:S", "size:XS"]]);
80 });
81
82 it("leaves a name that already carries its own namespace alone", () => {
83 expect(parsePullRequestQuery("size:S,size:XS").filters.labels).toEqual([["size:S", "size:XS"]]);
84 });
85
86 // regression: #19934 negated key:value qualifiers become namespaced label exclusions
87 it("excludes one the same way", () => {
88 expect(parsePullRequestQuery("-size:XXL").filters.excludedLabels).toEqual(["size:XXL"]);
89 expect(parsePullRequestQuery("-size:S,XS").filters.excludedLabels).toEqual([
90 "size:S",
91 "size:XS",
92 ]);
93 });
94
95 it("keeps a quoted token as text, which is the way back to a literal search", () => {
96 const parsed = parsePullRequestQuery('"size:XXL"');
97 expect(parsed.filters.labels).toBeUndefined();
98 expect(parsed.text).toBe('"size:XXL"');
99 });
100
101 it("reads a hyphenated namespace as a label too", () => {
102 expect(parsePullRequestQuery("priority-1:high").filters.labels).toEqual([["priority-1:high"]]);
103 expect(parsePullRequestQuery("priority-1:high").text).toBe("");
104 });
105
106 it("leaves a pasted link alone rather than naming a label after its scheme", () => {
107 const parsed = parsePullRequestQuery("https://github.com/pingdotgg/t3code/pull/1");
108 expect(parsed.filters.labels).toBeUndefined();
109 expect(parsed.text).toBe("https://github.com/pingdotgg/t3code/pull/1");
110 });
111
112 it("mixes with the keys it does know, and with plain words", () => {
113 const parsed = parsePullRequestQuery("area:web draft:true wizard label:bug");
114 expect(parsed.filters).toEqual({ labels: [["area:web"], ["bug"]], draft: "only" });
115 expect(parsed.text).toBe("wizard");
116 });
117});
118
119describe("changing the draft menu after typing a qualifier", () => {
120 it("replaces or clears a typed draft qualifier and keeps the rest of the query", () => {
121 expect(
122 applyPullRequestDraftFilterChange(
123 'draft:false fix label:"needs design"',
124 {},
125 { draft: "only" },
126 ),
127 ).toEqual({ query: 'fix label:"needs design"', filters: { draft: "only" } });
128 expect(
129 applyPullRequestDraftFilterChange('draft:true "draft:false" fix', { draft: "only" }, {}),
130 ).toEqual({
131 query: '"draft:false" fix',
132 filters: {},
133 });
134 });
135
136 it("keeps the typed query and the stored menu draft when another filter changes", () => {
137 expect(
138 applyPullRequestDraftFilterChange(
139 "draft:false fix",
140 { draft: "only" },
141 { draft: "hide", checks: "passing" },
142 ),
143 ).toEqual({ query: "draft:false fix", filters: { draft: "only", checks: "passing" } });
144 });
145});