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

pullRequestComposerTrigger.test.ts

213 lines8.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 debouncedPullRequestCompletionSearch,
5 detectPullRequestComposerTrigger,
6 insertPullRequestCompletion,
7 narrowPullRequestMentions,
8 narrowPullRequestReferences,
9 pullRequestCompletionKey,
10 pullRequestCompletions,
11} from "./pullRequestComposerTrigger.ts";
12​
13const GITLAB = { mentions: true, referenceSigils: ["#", "!"] } as const;
14​
15describe("detectPullRequestComposerTrigger", () => {
16 it.each([
17 ["@", 0, "@", ""],
18 ["cc @ju", 3, "@", "ju"],
19 ["line one\n@first.last", 9, "@", "first.last"],
20 ["@jane-doe more", 0, "@", "jane-doe"],
21 ["cc @김철", 3, "@", "김철"],
22 ["@ㄱ", 0, "@", "ㄱ"],
23 ["@प्रिया", 0, "@", "प्रिया"],
24 ["fixes #", 6, "#", ""],
25 ["see #12", 4, "#", "12"],
26 ["after !5", 6, "!", "5"],
27 ["#crash", 0, "#", "crash"],
28 ["#किताब", 0, "#", "किताब"],
29 ["#cafe\u0301", 0, "#", "cafe\u0301"],
30 ])("finds the trigger in %j", (text, rangeStart, sigil, query) => {
31 const cursor = rangeStart + 1 + query.length;
32 expect(detectPullRequestComposerTrigger(text, cursor, GITLAB)).toEqual({
33 kind: sigil === "@" ? "mention" : "reference",
34 sigil,
35 query,
36 rangeStart,
37 rangeEnd: cursor,
38 });
39 });
40​
41 it.each([
42 ["mail me at jane@example.com", "mail me at jane@example.com".length],
43 ["@jane done", "@jane done".length],
44 ["@-jane", "@-jane".length],
45 ["`@decorator`", "`@decorator".length],
46 ["@jane", 3],
47 ["C#", 2],
48 ["![image", "![image".length],
49 ["## Heading", 2],
50 ["#\u0301", "#\u0301".length],
51 ])("ignores %j", (text, cursor) => {
52 expect(detectPullRequestComposerTrigger(text, cursor, GITLAB)).toBeNull();
53 });
54​
55 it.each([
56 ["@ju", {}],
57 ["#12", { mentions: true }],
58 ["!important", { mentions: true, referenceSigils: ["#"] }],
59 ["#12", { mentions: true, referenceSigils: [] }],
60 ] as const)("ignores %j on a host that cannot complete it", (text, support) => {
61 expect(detectPullRequestComposerTrigger(text, text.length, support)).toBeNull();
62 });
63});
64​
65describe("insertPullRequestCompletion", () => {
66 it.each(["", "\nnext line", "\r\nnext line"])(
67 "replaces the partial token and adds a space before %j",
68 (suffix) => {
69 const text = `thanks @ju${suffix}`;
70 const trigger = detectPullRequestComposerTrigger(text, "thanks @ju".length, GITLAB)!;
71 expect(insertPullRequestCompletion(text, trigger, "@julius")).toEqual({
72 text: `thanks @julius ${suffix}`,
73 cursor: "thanks @julius ".length,
74 });
75 },
76 );
77​
78 it("reuses the space that already follows and moves past it", () => {
79 const text = "#1 please look";
80 const trigger = detectPullRequestComposerTrigger(text, 2, GITLAB)!;
81 const inserted = insertPullRequestCompletion(text, trigger, "#123");
82 expect(inserted).toEqual({ text: "#123 please look", cursor: "#123 ".length });
83 expect(detectPullRequestComposerTrigger(inserted.text, inserted.cursor, GITLAB)).toBeNull();
84 });
85});
86​
87describe("pullRequestCompletionKey", () => {
88 it("backs up one code unit when the 100-unit cap splits a surrogate pair", () => {
89 const query = `${"界".repeat(99)}𠀀`;
90 const trigger = detectPullRequestComposerTrigger(`!${query}`, query.length + 1, {
91 referenceSigils: ["!"],
92 })!;
93 const key = pullRequestCompletionKey(trigger);
94 expect(key).toBe(`!${"界".repeat(99)}`);
95 expect(debouncedPullRequestCompletionSearch(trigger, key)).toBe("界".repeat(99));
96 expect(trigger.query).toBe(query);
97 });
98​
99 it("keeps a surrogate pair that ends at the 100-unit cap", () => {
100 const prefix = `${"界".repeat(98)}𠀀`;
101 expect(pullRequestCompletionKey({ sigil: "!", query: `${prefix}界` })).toBe(`!${prefix}`);
102 });
103​
104 it("composes accents before the 100-unit cap so both spellings send the same search", () => {
105 const prefix = "a".repeat(99);
106 const key = `#${prefix}\u00e9`;
107 expect(pullRequestCompletionKey({ sigil: "#", query: `${prefix}\u00e9` })).toBe(key);
108 expect(pullRequestCompletionKey({ sigil: "#", query: `${prefix}e\u0301` })).toBe(key);
109 });
110​
111 it("sends a query within the cap as typed", () => {
112 expect(pullRequestCompletionKey({ sigil: "@", query: "e\u0301" })).toBe("@e\u0301");
113 });
114});
115​
116describe("debouncedPullRequestCompletionSearch", () => {
117 it.each([
118 ["a settled search", { sigil: "#" }, "#crash", "crash"],
119 ["a settled bare sigil", { sigil: "@" }, "@", undefined],
120 ["a lagging search", { sigil: "#" }, "#cra", "cra"],
121 ["the other sigil's key", { sigil: "!" }, "#12", undefined],
122 ["no key yet", { sigil: "#" }, null, undefined],
123 ] as const)("sends %s as the host search", (_, trigger, key, sent) => {
124 expect(debouncedPullRequestCompletionSearch(trigger, key)).toBe(sent);
125 });
126});
127​
128describe("narrowPullRequestMentions", () => {
129 it("matches login or name, case-insensitively, up to the limit", () => {
130 const users = [
131 { login: "TonybynMp4", name: null },
132 { login: "16g", name: "Tom" },
133 { login: "theo", name: "Theo" },
134 ];
135 expect(narrowPullRequestMentions(users, "to", 5).map((u) => u.login)).toEqual([
136 "TonybynMp4",
137 "16g",
138 ]);
139 expect(narrowPullRequestMentions(users, "", 2)).toHaveLength(2);
140 });
141});
142​
143describe("narrowPullRequestReferences", () => {
144 it("keeps the sigil's kind and matches a number prefix or the title", () => {
145 const candidates = [
146 { reference: "#12", number: 12, title: "Crash on start" },
147 { reference: "!12", number: 12, title: "Fix the crash" },
148 { reference: "#3", number: 3, title: "Issue 12 follow-up" },
149 { reference: "#120", number: 120, title: "Docs" },
150 ];
151 const narrowed = (sigil: "#" | "!", query: string) =>
152 narrowPullRequestReferences(candidates, { sigil, query }, 5).map((c) => c.reference);
153 expect(narrowed("#", "12")).toEqual(["#12", "#3", "#120"]);
154 expect(narrowed("!", "CRASH")).toEqual(["!12"]);
155 expect(narrowed("#", "")).toEqual(["#12", "#3", "#120"]);
156 });
157});
158​
159describe("pullRequestCompletions", () => {
160 it("treats medial, final and capital sigma as the same letter", () => {
161 const options = {
162 authors: [{ login: "alex", name: "Νίκος", avatarUrl: null }],
163 references: [
164 { reference: "!23", kind: "change-request", number: 23, title: "ΟΣΑ", state: "open" },
165 ],
166 } as const;
167 const tokens = (text: string) =>
168 pullRequestCompletions(
169 detectPullRequestComposerTrigger(text, text.length, GITLAB),
170 options,
171 5,
172 ).map((completion) => completion.token);
173 expect(tokens("@Σ")).toEqual(["@alex"]);
174 expect(tokens("!ος")).toEqual(["!23"]);
175 });
176​
177 it("matches precomposed and decomposed accents alike, but not unaccented letters", () => {
178 const options = {
179 authors: [{ login: "contributor", name: "Jos\u00e9", avatarUrl: null }],
180 references: [
181 { reference: "#7", kind: "issue", number: 7, title: "Caf\u00e9 fix", state: "open" },
182 ],
183 } as const;
184 const tokens = (text: string) =>
185 pullRequestCompletions(
186 detectPullRequestComposerTrigger(text, text.length, GITLAB),
187 options,
188 5,
189 ).map((completion) => completion.token);
190 expect(tokens("@Jos\u00e9")).toEqual(["@contributor"]);
191 expect(tokens("@Jose\u0301")).toEqual(["@contributor"]);
192 expect(tokens("#Caf\u00e9")).toEqual(["#7"]);
193 expect(tokens("#Cafe\u0301")).toEqual(["#7"]);
194 expect(tokens("@Jose")).toEqual([]);
195 });
196​
197 it("matches dotted capital I with a plain lowercase i", () => {
198 const options = {
199 authors: [{ login: "iyilmaz", name: "İsmail Yılmaz", avatarUrl: null }],
200 references: [
201 { reference: "#12", kind: "issue", number: 12, title: "istanbul fix", state: "open" },
202 ],
203 } as const;
204 const tokens = (text: string) =>
205 pullRequestCompletions(
206 detectPullRequestComposerTrigger(text, text.length, GITLAB),
207 options,
208 5,
209 ).map((completion) => completion.token);
210 expect(tokens("@ism")).toEqual(["@iyilmaz"]);
211 expect(tokens("#İst")).toEqual(["#12"]);
212 });
213});