The best way to control Claude Code, Codex and more from any computer or phone

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