|
| 1 | +import { describe, it, expect } from "bun:test"; |
| 2 | +import { parseCommand } from "./parser"; |
| 3 | + |
| 4 | +// Test helpers |
| 5 | +const expectParse = (input: string, expected: ReturnType<typeof parseCommand>) => { |
| 6 | + expect(parseCommand(input)).toEqual(expected); |
| 7 | +}; |
| 8 | + |
| 9 | +const expectProvidersSet = (input: string, provider: string, keyPath: string[], value: string) => { |
| 10 | + expectParse(input, { type: "providers-set", provider, keyPath, value }); |
| 11 | +}; |
| 12 | + |
| 13 | +const expectModelSet = (input: string, modelString: string) => { |
| 14 | + expectParse(input, { type: "model-set", modelString }); |
| 15 | +}; |
| 16 | + |
| 17 | +describe("commandParser", () => { |
| 18 | + describe("parseCommand", () => { |
| 19 | + it("should return null for non-command input", () => { |
| 20 | + expect(parseCommand("hello world")).toBeNull(); |
| 21 | + expect(parseCommand("")).toBeNull(); |
| 22 | + expect(parseCommand(" ")).toBeNull(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should parse /clear command", () => { |
| 26 | + expectParse("/clear", { type: "clear" }); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should parse /providers help when no subcommand", () => { |
| 30 | + expectParse("/providers", { type: "providers-help" }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should parse /providers with invalid subcommand", () => { |
| 34 | + expectParse("/providers invalid", { |
| 35 | + type: "providers-invalid-subcommand", |
| 36 | + subcommand: "invalid", |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should parse /providers set with missing args", () => { |
| 41 | + const missingArgsCases = [ |
| 42 | + { input: "/providers set", argCount: 0 }, |
| 43 | + { input: "/providers set anthropic", argCount: 1 }, |
| 44 | + { input: "/providers set anthropic apiKey", argCount: 2 }, |
| 45 | + ]; |
| 46 | + |
| 47 | + missingArgsCases.forEach(({ input, argCount }) => { |
| 48 | + expectParse(input, { |
| 49 | + type: "providers-missing-args", |
| 50 | + subcommand: "set", |
| 51 | + argCount, |
| 52 | + }); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should parse /providers set with all arguments", () => { |
| 57 | + expectProvidersSet( |
| 58 | + "/providers set anthropic apiKey sk-123", |
| 59 | + "anthropic", |
| 60 | + ["apiKey"], |
| 61 | + "sk-123" |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should handle quoted arguments", () => { |
| 66 | + expectProvidersSet( |
| 67 | + '/providers set anthropic apiKey "my key with spaces"', |
| 68 | + "anthropic", |
| 69 | + ["apiKey"], |
| 70 | + "my key with spaces" |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should handle multiple spaces in value", () => { |
| 75 | + expectProvidersSet( |
| 76 | + "/providers set anthropic apiKey My Anthropic API", |
| 77 | + "anthropic", |
| 78 | + ["apiKey"], |
| 79 | + "My Anthropic API" |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should handle nested key paths", () => { |
| 84 | + expectProvidersSet( |
| 85 | + "/providers set anthropic baseUrl.scheme https", |
| 86 | + "anthropic", |
| 87 | + ["baseUrl", "scheme"], |
| 88 | + "https" |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should parse unknown commands", () => { |
| 93 | + expectParse("/foo", { |
| 94 | + type: "unknown-command", |
| 95 | + command: "foo", |
| 96 | + subcommand: undefined, |
| 97 | + }); |
| 98 | + |
| 99 | + expectParse("/foo bar", { |
| 100 | + type: "unknown-command", |
| 101 | + command: "foo", |
| 102 | + subcommand: "bar", |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should handle multiple spaces between arguments", () => { |
| 107 | + expectProvidersSet( |
| 108 | + "/providers set anthropic apiKey sk-12345", |
| 109 | + "anthropic", |
| 110 | + ["apiKey"], |
| 111 | + "sk-12345" |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should handle quoted URL values", () => { |
| 116 | + expectProvidersSet( |
| 117 | + '/providers set anthropic baseUrl "https://api.anthropic.com/v1"', |
| 118 | + "anthropic", |
| 119 | + ["baseUrl"], |
| 120 | + "https://api.anthropic.com/v1" |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it("should parse /model with abbreviation", () => { |
| 125 | + expectModelSet("/model opus", "anthropic:claude-opus-4-1"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should parse /model with full provider:model format", () => { |
| 129 | + expectModelSet("/model anthropic:claude-sonnet-4-5", "anthropic:claude-sonnet-4-5"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should parse /model help when no args", () => { |
| 133 | + expectParse("/model", { type: "model-help" }); |
| 134 | + }); |
| 135 | + |
| 136 | + it("should handle unknown abbreviation as full model string", () => { |
| 137 | + expectModelSet("/model custom:model-name", "custom:model-name"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should reject /model with too many arguments", () => { |
| 141 | + expectParse("/model anthropic claude extra", { |
| 142 | + type: "unknown-command", |
| 143 | + command: "model", |
| 144 | + subcommand: "claude", |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should parse /vim command", () => { |
| 149 | + expectParse("/vim", { type: "vim-toggle" }); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should reject /vim with arguments", () => { |
| 153 | + expectParse("/vim enable", { |
| 154 | + type: "unknown-command", |
| 155 | + command: "vim", |
| 156 | + subcommand: "enable", |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it("should parse /fork command with name only", () => { |
| 161 | + expectParse("/fork feature-branch", { |
| 162 | + type: "fork", |
| 163 | + newName: "feature-branch", |
| 164 | + startMessage: undefined, |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it("should parse /fork command with start message", () => { |
| 169 | + expectParse("/fork feature-branch let's go", { |
| 170 | + type: "fork", |
| 171 | + newName: "feature-branch", |
| 172 | + startMessage: "let's go", |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + it("should show /fork help when missing args", () => { |
| 177 | + expectParse("/fork", { type: "fork-help" }); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments