xonecas.io
64875
Mc

mini-coder / commit 6837f35

feat: add input parsing module with tests (Phase 4a)

- parseInput() detects slash commands, /skill:name references, image
  paths, and plain text with clear priority ordering
- 30 tests covering all parsing cases: commands, skills, images (with
  real temp files), plain text, edge cases, and priority

xonecas2026-04-05 6837f35679f82f7e4cfdc8cc3920f0e559469249 parent 21378b9

2 files changed

src/input.test.ts+243−0
@@ -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+});
src/input.ts+133−0
@@ -0,0 +1,133 @@
1+/**
2+ * User input parsing.
3+ *
4+ * Pure logic for detecting slash commands, skill references, image paths,
5+ * and plain text from raw user input. No UI or IO beyond `existsSync`
6+ * for image path validation.
7+ *
8+ * @module
9+ */
10+
11+import { existsSync } from "node:fs";
12+import { extname, isAbsolute, join } from "node:path";
13+
14+// ---------------------------------------------------------------------------
15+// Constants
16+// ---------------------------------------------------------------------------
17+
18+/** All recognized slash commands. */
19+export const COMMANDS = [
20+ "model",
21+ "session",
22+ "new",
23+ "fork",
24+ "undo",
25+ "reasoning",
26+ "verbose",
27+ "login",
28+ "logout",
29+ "help",
30+ "effort",
31+] as const;
32+
33+/** A recognized slash command name. */
34+export type Command = (typeof COMMANDS)[number];
35+
36+const COMMAND_SET: ReadonlySet<string> = new Set(COMMANDS);
37+
38+/** Image file extensions we recognize for embedding. */
39+const IMAGE_EXTENSIONS: ReadonlySet<string> = new Set([
40+ ".png",
41+ ".jpg",
42+ ".jpeg",
43+ ".gif",
44+ ".webp",
45+]);
46+
47+// ---------------------------------------------------------------------------
48+// Types
49+// ---------------------------------------------------------------------------
50+
51+/** Result of parsing user input. */
52+export type ParsedInput =
53+ | { type: "command"; command: Command; args: string }
54+ | { type: "skill"; skillName: string; userText: string }
55+ | { type: "image"; path: string }
56+ | { type: "text"; text: string };
57+
58+/** Options for input parsing. */
59+export interface ParseInputOpts {
60+ /** Whether the current model supports image input. */
61+ supportsImages?: boolean;
62+ /** Working directory for resolving relative image paths. */
63+ cwd?: string;
64+}
65+
66+// ---------------------------------------------------------------------------
67+// Parsing
68+// ---------------------------------------------------------------------------
69+
70+/**
71+ * Parse raw user input into a structured result.
72+ *
73+ * Priority order:
74+ * 1. Slash commands (`/model`, `/help`, etc.)
75+ * 2. Skill references (`/skill:name rest of message`)
76+ * 3. Image paths (entire input is an existing image file)
77+ * 4. Plain text
78+ *
79+ * @param raw - The raw input string from the user.
80+ * @param opts - Optional parsing context (image support, cwd).
81+ * @returns A {@link ParsedInput} describing what the input represents.
82+ */
83+export function parseInput(raw: string, opts?: ParseInputOpts): ParsedInput {
84+ const trimmed = raw.trim();
85+
86+ // Empty or whitespace-only
87+ if (trimmed.length === 0) {
88+ return { type: "text", text: "" };
89+ }
90+
91+ // Slash-prefixed: command or skill
92+ if (trimmed[0] === "/") {
93+ // Skill reference: /skill:name [rest]
94+ const skillMatch = trimmed.match(/^\/skill:(\S+)(?:\s+(.*))?$/s);
95+ if (skillMatch?.[1]) {
96+ return {
97+ type: "skill",
98+ skillName: skillMatch[1],
99+ userText: skillMatch[2]?.trim() ?? "",
100+ };
101+ }
102+
103+ // Slash command: /name [args]
104+ const cmdMatch = trimmed.match(/^\/(\S+)(?:\s+(.*))?$/s);
105+ if (cmdMatch?.[1] && COMMAND_SET.has(cmdMatch[1])) {
106+ return {
107+ type: "command",
108+ command: cmdMatch[1] as Command,
109+ args: cmdMatch[2]?.trim() ?? "",
110+ };
111+ }
112+
113+ // Not a recognized command or skill — fall through to text
114+ }
115+
116+ // Image path detection
117+ if (opts?.supportsImages) {
118+ const ext = extname(trimmed).toLowerCase();
119+ if (IMAGE_EXTENSIONS.has(ext)) {
120+ const resolved = isAbsolute(trimmed)
121+ ? trimmed
122+ : opts.cwd
123+ ? join(opts.cwd, trimmed)
124+ : trimmed;
125+ if (existsSync(resolved)) {
126+ return { type: "image", path: resolved };
127+ }
128+ }
129+ }
130+
131+ // Plain text
132+ return { type: "text", text: trimmed };
133+}