Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import {
4 selectionTouchesMentionBoundary,
5 splitPromptIntoComposerSegments,
6} from "./composer-editor-mentions";
7import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "./lib/terminalContext";
8
9describe("splitPromptIntoComposerSegments", () => {
10 it("splits mention tokens followed by whitespace into mention segments", () => {
11 expect(splitPromptIntoComposerSegments("Inspect @AGENTS.md please")).toEqual([
12 { type: "text", text: "Inspect " },
13 { type: "mention", path: "AGENTS.md" },
14 { type: "text", text: " please" },
15 ]);
16 });
17
18 it("does not convert an incomplete trailing mention token", () => {
19 expect(splitPromptIntoComposerSegments("Inspect @AGENTS.md")).toEqual([
20 { type: "text", text: "Inspect @AGENTS.md" },
21 ]);
22 });
23
24 it("keeps newlines around mention tokens", () => {
25 expect(splitPromptIntoComposerSegments("one\n@src/index.ts \ntwo")).toEqual([
26 { type: "text", text: "one\n" },
27 { type: "mention", path: "src/index.ts" },
28 { type: "text", text: " \ntwo" },
29 ]);
30 });
31
32 it("splits skill tokens followed by whitespace into skill segments", () => {
33 expect(splitPromptIntoComposerSegments("Use $review-follow-up please")).toEqual([
34 { type: "text", text: "Use " },
35 { type: "skill", name: "review-follow-up" },
36 { type: "text", text: " please" },
37 ]);
38 });
39
40 it("does not convert an incomplete trailing skill token", () => {
41 expect(splitPromptIntoComposerSegments("Use $review-follow-up")).toEqual([
42 { type: "text", text: "Use $review-follow-up" },
43 ]);
44 });
45
46 it("keeps inline terminal context placeholders at their prompt positions", () => {
47 expect(
48 splitPromptIntoComposerSegments(
49 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md please`,
50 ),
51 ).toEqual([
52 { type: "text", text: "Inspect " },
53 { type: "terminal-context", context: null },
54 { type: "mention", path: "AGENTS.md" },
55 { type: "text", text: " please" },
56 ]);
57 });
58
59 it("preserves consecutive terminal context placeholders without dropping positions", () => {
60 expect(
61 splitPromptIntoComposerSegments(
62 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}tail`,
63 ),
64 ).toEqual([
65 { type: "terminal-context", context: null },
66 { type: "terminal-context", context: null },
67 { type: "text", text: "tail" },
68 ]);
69 });
70
71 it("keeps skill parsing alongside mentions and terminal placeholders", () => {
72 expect(
73 splitPromptIntoComposerSegments(
74 `Inspect ${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}$review-follow-up after @AGENTS.md `,
75 ),
76 ).toEqual([
77 { type: "text", text: "Inspect " },
78 { type: "terminal-context", context: null },
79 { type: "skill", name: "review-follow-up" },
80 { type: "text", text: " after " },
81 { type: "mention", path: "AGENTS.md" },
82 { type: "text", text: " " },
83 ]);
84 });
85});
86
87describe("selectionTouchesMentionBoundary", () => {
88 it("returns true when selection includes the whitespace after a mention", () => {
89 expect(
90 selectionTouchesMentionBoundary(
91 "hi @package.json there",
92 "hi @package.json".length,
93 "hi @package.json there".length,
94 ),
95 ).toBe(true);
96 });
97
98 it("returns true when selection includes the whitespace before a mention", () => {
99 expect(
100 selectionTouchesMentionBoundary(
101 "hi there @package.json later",
102 "hi there".length,
103 "hi there ".length,
104 ),
105 ).toBe(true);
106 });
107
108 it("returns false when selection starts after the mention boundary whitespace", () => {
109 expect(
110 selectionTouchesMentionBoundary(
111 "hi @package.json there",
112 "hi @package.json ".length,
113 "hi @package.json there".length,
114 ),
115 ).toBe(false);
116 });
117
118 it("returns true when selection includes whitespace after a mention following a terminal placeholder", () => {
119 const prompt = `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md there`;
120 expect(
121 selectionTouchesMentionBoundary(
122 prompt,
123 `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md`.length,
124 prompt.length,
125 ),
126 ).toBe(true);
127 });
128});