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

pullRequestComposerTrigger.test.ts

145 lines5.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 debouncedPullRequestCompletionSearch,
5 detectPullRequestComposerTrigger,
6 insertPullRequestCompletion,
7 narrowPullRequestMentions,
8 narrowPullRequestReferences,
9 pullRequestCompletionKey,
10} from "./pullRequestComposerTrigger.ts";
11​
12const GITLAB = { mentions: true, referenceSigils: ["#", "!"] } as const;
13​
14describe("detectPullRequestComposerTrigger", () => {
15 it.each([
16 ["@", 0, "@", ""],
17 ["cc @ju", 3, "@", "ju"],
18 ["line one\n@first.last", 9, "@", "first.last"],
19 ["@jane-doe more", 0, "@", "jane-doe"],
20 ["cc @김철", 3, "@", "김철"],
21 ["@ㄱ", 0, "@", "ㄱ"],
22 ["@प्रिया", 0, "@", "प्रिया"],
23 ["fixes #", 6, "#", ""],
24 ["see #12", 4, "#", "12"],
25 ["after !5", 6, "!", "5"],
26 ["#crash", 0, "#", "crash"],
27 ["#किताब", 0, "#", "किताब"],
28 ["#cafe\u0301", 0, "#", "cafe\u0301"],
29 ])("finds the trigger in %j", (text, rangeStart, sigil, query) => {
30 const cursor = rangeStart + 1 + query.length;
31 expect(detectPullRequestComposerTrigger(text, cursor, GITLAB)).toEqual({
32 kind: sigil === "@" ? "mention" : "reference",
33 sigil,
34 query,
35 rangeStart,
36 rangeEnd: cursor,
37 });
38 });
39​
40 it.each([
41 ["mail me at jane@example.com", "mail me at jane@example.com".length],
42 ["@jane done", "@jane done".length],
43 ["@-jane", "@-jane".length],
44 ["`@decorator`", "`@decorator".length],
45 ["@jane", 3],
46 ["C#", 2],
47 ["![image", "![image".length],
48 ["## Heading", 2],
49 ["#\u0301", "#\u0301".length],
50 ])("ignores %j", (text, cursor) => {
51 expect(detectPullRequestComposerTrigger(text, cursor, GITLAB)).toBeNull();
52 });
53​
54 it.each([
55 ["@ju", {}],
56 ["#12", { mentions: true }],
57 ["!important", { mentions: true, referenceSigils: ["#"] }],
58 ["#12", { mentions: true, referenceSigils: [] }],
59 ] as const)("ignores %j on a host that cannot complete it", (text, support) => {
60 expect(detectPullRequestComposerTrigger(text, text.length, support)).toBeNull();
61 });
62});
63​
64describe("insertPullRequestCompletion", () => {
65 it.each(["", "\nnext line", "\r\nnext line"])(
66 "replaces the partial token and adds a space before %j",
67 (suffix) => {
68 const text = `thanks @ju${suffix}`;
69 const trigger = detectPullRequestComposerTrigger(text, "thanks @ju".length, GITLAB)!;
70 expect(insertPullRequestCompletion(text, trigger, "@julius")).toEqual({
71 text: `thanks @julius ${suffix}`,
72 cursor: "thanks @julius ".length,
73 });
74 },
75 );
76​
77 it("reuses the space that already follows and moves past it", () => {
78 const text = "#1 please look";
79 const trigger = detectPullRequestComposerTrigger(text, 2, GITLAB)!;
80 const inserted = insertPullRequestCompletion(text, trigger, "#123");
81 expect(inserted).toEqual({ text: "#123 please look", cursor: "#123 ".length });
82 expect(detectPullRequestComposerTrigger(inserted.text, inserted.cursor, GITLAB)).toBeNull();
83 });
84});
85​
86describe("pullRequestCompletionKey", () => {
87 it("backs up one code unit when the 100-unit cap splits a surrogate pair", () => {
88 const query = `${"界".repeat(99)}𠀀`;
89 const trigger = detectPullRequestComposerTrigger(`!${query}`, query.length + 1, {
90 referenceSigils: ["!"],
91 })!;
92 const key = pullRequestCompletionKey(trigger);
93 expect(key).toBe(`!${"界".repeat(99)}`);
94 expect(debouncedPullRequestCompletionSearch(trigger, key)).toBe("界".repeat(99));
95 expect(trigger.query).toBe(query);
96 });
97​
98 it("keeps a surrogate pair that ends at the 100-unit cap", () => {
99 const prefix = `${"界".repeat(98)}𠀀`;
100 expect(pullRequestCompletionKey({ sigil: "!", query: `${prefix}界` })).toBe(`!${prefix}`);
101 });
102});
103​
104describe("debouncedPullRequestCompletionSearch", () => {
105 it.each([
106 ["a settled search", { sigil: "#" }, "#crash", "crash"],
107 ["a settled bare sigil", { sigil: "@" }, "@", undefined],
108 ["a lagging search", { sigil: "#" }, "#cra", "cra"],
109 ["the other sigil's key", { sigil: "!" }, "#12", undefined],
110 ["no key yet", { sigil: "#" }, null, undefined],
111 ] as const)("sends %s as the host search", (_, trigger, key, sent) => {
112 expect(debouncedPullRequestCompletionSearch(trigger, key)).toBe(sent);
113 });
114});
115​
116describe("narrowPullRequestMentions", () => {
117 it("matches login or name, case-insensitively, up to the limit", () => {
118 const users = [
119 { login: "TonybynMp4", name: null },
120 { login: "16g", name: "Tom" },
121 { login: "theo", name: "Theo" },
122 ];
123 expect(narrowPullRequestMentions(users, "to", 5).map((u) => u.login)).toEqual([
124 "TonybynMp4",
125 "16g",
126 ]);
127 expect(narrowPullRequestMentions(users, "", 2)).toHaveLength(2);
128 });
129});
130​
131describe("narrowPullRequestReferences", () => {
132 it("keeps the sigil's kind and matches a number prefix or the title", () => {
133 const candidates = [
134 { reference: "#12", number: 12, title: "Crash on start" },
135 { reference: "!12", number: 12, title: "Fix the crash" },
136 { reference: "#3", number: 3, title: "Issue 12 follow-up" },
137 { reference: "#120", number: 120, title: "Docs" },
138 ];
139 const narrowed = (sigil: "#" | "!", query: string) =>
140 narrowPullRequestReferences(candidates, { sigil, query }, 5).map((c) => c.reference);
141 expect(narrowed("#", "12")).toEqual(["#12", "#3", "#120"]);
142 expect(narrowed("!", "CRASH")).toEqual(["!12"]);
143 expect(narrowed("#", "")).toEqual(["#12", "#3", "#120"]);
144 });
145});