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

pullRequestQuery.test.ts

163 lines6.4 KB

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("reads a comma-separated list as one group either name satisfies", () => {
13 expect(parsePullRequestQuery("label:size:S,size:XS").filters.labels).toEqual([
14 ["size:S", "size:XS"],
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("excludes every name of a negated list", () => {
26 expect(parsePullRequestQuery("-label:a,b").filters).toEqual({ excludedLabels: ["a", "b"] });
27 });
28​
29 it("makes each label qualifier its own group, so the groups are an AND", () => {
30 expect(parsePullRequestQuery("label:bug label:a,b").filters.labels).toEqual([
31 ["bug"],
32 ["a", "b"],
33 ]);
34 });
35​
36 it("reads the scalar qualifiers in GitHub's own spelling", () => {
37 expect(
38 parsePullRequestQuery("author:octocat DRAFT:false review:changes_requested status:failure")
39 .filters,
40 ).toEqual({
41 author: "octocat",
42 draft: "hide",
43 review: "changes-requested",
44 checks: "failing",
45 });
46 });
47​
48 it("leaves an unknown value and a stray colon as text, and reads an unknown key as a label", () => {
49 const parsed = parsePullRequestQuery("milestone:v2 draft:maybe status: parser");
50 expect(parsed.filters).toEqual({ labels: [["milestone:v2"]] });
51 expect(parsed.text).toBe("draft:maybe status: parser");
52 });
53​
54 it("reads a quoted name holding a comma as the one label it is", () => {
55 expect(parsePullRequestQuery('label:"needs,triage"').filters.labels).toEqual([
56 ["needs,triage"],
57 ]);
58 expect(parsePullRequestQuery('label:"needs,triage",wip').filters.labels).toEqual([
59 ["needs,triage", "wip"],
60 ]);
61 expect(parsePullRequestQuery('-label:"needs,triage"').filters.excludedLabels).toEqual([
62 "needs,triage",
63 ]);
64 });
65​
66 it("cuts a typed query back to what the contract carries", () => {
67 const many = Array.from({ length: 14 }, (_, index) => `l${index}`).join(",");
68 const long = "x".repeat(240);
69 const parsed = parsePullRequestQuery(`label:${many} label:${long} author:${long}`);
70 expect(parsed.filters.labels?.[0]).toHaveLength(10);
71 expect(parsed.filters.labels?.[1]).toEqual(["x".repeat(200)]);
72 expect(parsed.filters.author).toBe("x".repeat(200));
73 });
74​
75 it("cuts a namespaced label after its key is prefixed on", () => {
76 const parsed = parsePullRequestQuery(`size:${"x".repeat(240)}`);
77 expect(parsed.filters.labels).toEqual([[`size:${"x".repeat(240)}`.slice(0, 200)]]);
78 });
79​
80 it("answers an empty query with an empty everything", () => {
81 expect(parsePullRequestQuery(" ")).toEqual({ text: "", filters: {} });
82 });
83});
84​
85describe("colon-namespaced labels typed as a search", () => {
86 it("reads an unknown key as the label it almost always is", () => {
87 expect(parsePullRequestQuery("size:XXL").filters.labels).toEqual([["size:XXL"]]);
88 expect(parsePullRequestQuery("vouch:trusted").filters.labels).toEqual([["vouch:trusted"]]);
89 expect(parsePullRequestQuery("size:XXL").text).toBe("");
90 });
91​
92 it("reads the key as the namespace of every bare name in its list", () => {
93 expect(parsePullRequestQuery("size:S,XS").filters.labels).toEqual([["size:S", "size:XS"]]);
94 });
95​
96 it("leaves a name that already carries its own namespace alone", () => {
97 expect(parsePullRequestQuery("size:S,size:XS").filters.labels).toEqual([["size:S", "size:XS"]]);
98 });
99​
100 it("excludes one the same way", () => {
101 expect(parsePullRequestQuery("-size:XXL").filters.excludedLabels).toEqual(["size:XXL"]);
102 expect(parsePullRequestQuery("-size:S,XS").filters.excludedLabels).toEqual([
103 "size:S",
104 "size:XS",
105 ]);
106 });
107​
108 it("keeps a quoted token as text, which is the way back to a literal search", () => {
109 const parsed = parsePullRequestQuery('"size:XXL"');
110 expect(parsed.filters.labels).toBeUndefined();
111 expect(parsed.text).toBe('"size:XXL"');
112 });
113​
114 it("reads a hyphenated namespace as a label too", () => {
115 expect(parsePullRequestQuery("priority-1:high").filters.labels).toEqual([["priority-1:high"]]);
116 expect(parsePullRequestQuery("priority-1:high").text).toBe("");
117 });
118​
119 it("leaves a pasted link alone rather than naming a label after its scheme", () => {
120 const parsed = parsePullRequestQuery("https://github.com/pingdotgg/t3code/pull/1");
121 expect(parsed.filters.labels).toBeUndefined();
122 expect(parsed.text).toBe("https://github.com/pingdotgg/t3code/pull/1");
123 });
124​
125 it("mixes with the keys it does know, and with plain words", () => {
126 const parsed = parsePullRequestQuery("area:web draft:true wizard label:bug");
127 expect(parsed.filters).toEqual({ labels: [["area:web"], ["bug"]], draft: "only" });
128 expect(parsed.text).toBe("wizard");
129 });
130​
131 it("shares the ceiling with the labels typed as labels", () => {
132 const many = Array.from({ length: 12 }, (_, index) => `area:${index}`).join(" ");
133 expect(parsePullRequestQuery(many).filters.labels).toHaveLength(10);
134 });
135});
136​
137describe("changing the draft menu after typing a qualifier", () => {
138 it("replaces or clears a typed draft qualifier and keeps the rest of the query", () => {
139 expect(
140 applyPullRequestDraftFilterChange(
141 'draft:false fix label:"needs design"',
142 {},
143 { draft: "only" },
144 ),
145 ).toEqual({ query: 'fix label:"needs design"', filters: { draft: "only" } });
146 expect(
147 applyPullRequestDraftFilterChange('draft:true "draft:false" fix', { draft: "only" }, {}),
148 ).toEqual({
149 query: '"draft:false" fix',
150 filters: {},
151 });
152 });
153​
154 it("keeps the typed query and the stored menu draft when another filter changes", () => {
155 expect(
156 applyPullRequestDraftFilterChange(
157 "draft:false fix",
158 { draft: "only" },
159 { draft: "hide", checks: "passing" },
160 ),
161 ).toEqual({ query: "draft:false fix", filters: { draft: "only", checks: "passing" } });
162 });
163});