@@ -0,0 +1,243 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { join } from "node:path";
5+import { COMMANDS, parseInput } from "./input.ts";
6+
7+// ---------------------------------------------------------------------------
8+// Helpers
9+// ---------------------------------------------------------------------------
10+
11+let tmp: string;
12+
13+beforeEach(() => {
14+ tmp = mkdtempSync(join(tmpdir(), "mc-input-"));
15+});
16+
17+afterEach(() => {
18+ rmSync(tmp, { recursive: true, force: true });
19+});
20+
21+// ---------------------------------------------------------------------------
22+// Command detection
23+// ---------------------------------------------------------------------------
24+
25+describe("parseInput — commands", () => {
26+ test("recognizes each known command", () => {
27+ for (const cmd of COMMANDS) {
28+ const result = parseInput(`/${cmd}`);
29+ expect(result).toEqual({ type: "command", command: cmd, args: "" });
30+ }
31+ });
32+
33+ test("unknown /foo is plain text", () => {
34+ expect(parseInput("/foo")).toEqual({ type: "text", text: "/foo" });
35+ });
36+
37+ test("/ alone is plain text", () => {
38+ expect(parseInput("/")).toEqual({ type: "text", text: "/" });
39+ });
40+
41+ test("space after slash is plain text", () => {
42+ expect(parseInput("/ model")).toEqual({ type: "text", text: "/ model" });
43+ });
44+
45+ test("command with trailing whitespace still matches", () => {
46+ const result = parseInput("/model ");
47+ expect(result).toEqual({ type: "command", command: "model", args: "" });
48+ });
49+
50+ test("command is case-sensitive (uppercase is text)", () => {
51+ expect(parseInput("/Model")).toEqual({ type: "text", text: "/Model" });
52+ });
53+});
54+
55+// ---------------------------------------------------------------------------
56+// Skill detection
57+// ---------------------------------------------------------------------------
58+
59+describe("parseInput — skills", () => {
60+ test("/skill:name extracts skill with no user text", () => {
61+ expect(parseInput("/skill:review")).toEqual({
62+ type: "skill",
63+ skillName: "review",
64+ userText: "",
65+ });
66+ });
67+
68+ test("/skill:name rest extracts skill and user text", () => {
69+ expect(parseInput("/skill:code-review check the auth module")).toEqual({
70+ type: "skill",
71+ skillName: "code-review",
72+ userText: "check the auth module",
73+ });
74+ });
75+
76+ test("/skill:name with trailing space has empty user text", () => {
77+ expect(parseInput("/skill:review ")).toEqual({
78+ type: "skill",
79+ skillName: "review",
80+ userText: "",
81+ });
82+ });
83+
84+ test("/skill: with no name is plain text", () => {
85+ expect(parseInput("/skill:")).toEqual({ type: "text", text: "/skill:" });
86+ });
87+
88+ test("/skill without colon is plain text (not a known command)", () => {
89+ expect(parseInput("/skill")).toEqual({ type: "text", text: "/skill" });
90+ });
91+
92+ test("skill name can contain hyphens and underscores", () => {
93+ expect(parseInput("/skill:my_cool-skill do stuff")).toEqual({
94+ type: "skill",
95+ skillName: "my_cool-skill",
96+ userText: "do stuff",
97+ });
98+ });
99+});
100+
101+// ---------------------------------------------------------------------------
102+// Image detection
103+// ---------------------------------------------------------------------------
104+
105+describe("parseInput — images", () => {
106+ test("existing image path with supported extension", () => {
107+ const img = join(tmp, "screenshot.png");
108+ writeFileSync(img, "fake png");
109+ const result = parseInput(img, { supportsImages: true });
110+ expect(result).toEqual({ type: "image", path: img });
111+ });
112+
113+ test("supports all image extensions", () => {
114+ for (const ext of [".png", ".jpg", ".jpeg", ".gif", ".webp"]) {
115+ const img = join(tmp, `test${ext}`);
116+ writeFileSync(img, "fake");
117+ const result = parseInput(img, { supportsImages: true });
118+ expect(result.type).toBe("image");
119+ }
120+ });
121+
122+ test("non-existent image path is plain text", () => {
123+ const img = join(tmp, "missing.png");
124+ const result = parseInput(img, { supportsImages: true });
125+ expect(result.type).toBe("text");
126+ });
127+
128+ test("image path when model does not support images is plain text", () => {
129+ const img = join(tmp, "screenshot.png");
130+ writeFileSync(img, "fake png");
131+ const result = parseInput(img, { supportsImages: false });
132+ expect(result.type).toBe("text");
133+ });
134+
135+ test("image path with supportsImages undefined is plain text", () => {
136+ const img = join(tmp, "screenshot.png");
137+ writeFileSync(img, "fake png");
138+ const result = parseInput(img);
139+ expect(result.type).toBe("text");
140+ });
141+
142+ test("non-image extension is plain text even if file exists", () => {
143+ const file = join(tmp, "readme.txt");
144+ writeFileSync(file, "hello");
145+ const result = parseInput(file, { supportsImages: true });
146+ expect(result.type).toBe("text");
147+ });
148+
149+ test("input with extra text is not an image", () => {
150+ const img = join(tmp, "screenshot.png");
151+ writeFileSync(img, "fake png");
152+ const result = parseInput(`look at ${img}`, { supportsImages: true });
153+ expect(result.type).toBe("text");
154+ });
155+
156+ test("trimmed whitespace still detects image", () => {
157+ const img = join(tmp, "photo.jpg");
158+ writeFileSync(img, "fake jpg");
159+ const result = parseInput(` ${img} `, { supportsImages: true });
160+ expect(result).toEqual({ type: "image", path: img });
161+ });
162+
163+ test("relative image path resolved against cwd", () => {
164+ const img = join(tmp, "img.png");
165+ writeFileSync(img, "fake png");
166+ const result = parseInput("img.png", { supportsImages: true, cwd: tmp });
167+ expect(result).toEqual({ type: "image", path: img });
168+ });
169+
170+ test("relative image path without cwd checks as-is", () => {
171+ // Relative path that doesn't exist from process.cwd
172+ const result = parseInput("nonexistent-dir/img.png", {
173+ supportsImages: true,
174+ });
175+ expect(result.type).toBe("text");
176+ });
177+
178+ test("image in subdirectory with cwd", () => {
179+ const sub = join(tmp, "assets");
180+ mkdirSync(sub);
181+ writeFileSync(join(sub, "logo.webp"), "fake webp");
182+ const result = parseInput("assets/logo.webp", {
183+ supportsImages: true,
184+ cwd: tmp,
185+ });
186+ expect(result).toEqual({
187+ type: "image",
188+ path: join(sub, "logo.webp"),
189+ });
190+ });
191+
192+ test("uppercase extension is detected", () => {
193+ const img = join(tmp, "PHOTO.PNG");
194+ writeFileSync(img, "fake png");
195+ const result = parseInput(img, { supportsImages: true });
196+ expect(result).toEqual({ type: "image", path: img });
197+ });
198+});
199+
200+// ---------------------------------------------------------------------------
201+// Plain text
202+// ---------------------------------------------------------------------------
203+
204+describe("parseInput — text", () => {
205+ test("regular message is text", () => {
206+ expect(parseInput("fix the tests")).toEqual({
207+ type: "text",
208+ text: "fix the tests",
209+ });
210+ });
211+
212+ test("empty string is text", () => {
213+ expect(parseInput("")).toEqual({ type: "text", text: "" });
214+ });
215+
216+ test("whitespace-only is text", () => {
217+ expect(parseInput(" ")).toEqual({ type: "text", text: "" });
218+ });
219+
220+ test("message starting with slash-like but not command", () => {
221+ expect(parseInput("/usr/bin/node")).toEqual({
222+ type: "text",
223+ text: "/usr/bin/node",
224+ });
225+ });
226+});
227+
228+// ---------------------------------------------------------------------------
229+// Priority
230+// ---------------------------------------------------------------------------
231+
232+describe("parseInput — priority", () => {
233+ test("commands take priority over image detection", () => {
234+ // Even if /model.png existed, /model is a command
235+ const result = parseInput("/model");
236+ expect(result.type).toBe("command");
237+ });
238+
239+ test("skill takes priority over image detection", () => {
240+ const result = parseInput("/skill:review");
241+ expect(result.type).toBe("skill");
242+ });
243+});