|
1 | 1 | import { vi } from "vitest"; |
2 | | - |
3 | | -import { parseArgs, processPromptOrRule } from "./args.js"; |
4 | | - |
5 | | -describe("parseArgs", () => { |
6 | | - const originalArgv = process.argv; |
7 | | - |
8 | | - beforeEach(() => { |
9 | | - // Reset process.argv before each test |
10 | | - process.argv = ["node", "script.js"]; |
11 | | - }); |
12 | | - |
13 | | - afterAll(() => { |
14 | | - // Restore original argv |
15 | | - process.argv = originalArgv; |
16 | | - }); |
17 | | - |
18 | | - it("should return default values when no arguments provided", () => { |
19 | | - const result = parseArgs(); |
20 | | - expect(result).toEqual({}); |
21 | | - }); |
22 | | - |
23 | | - it("should set resume to true when --resume flag is present", () => { |
24 | | - process.argv = ["node", "script.js", "--resume"]; |
25 | | - const result = parseArgs(); |
26 | | - expect(result.resume).toBe(true); |
27 | | - }); |
28 | | - |
29 | | - it("should set readonly to true when --readonly flag is present", () => { |
30 | | - process.argv = ["node", "script.js", "--readonly"]; |
31 | | - const result = parseArgs(); |
32 | | - expect(result.readonly).toBe(true); |
33 | | - }); |
34 | | - |
35 | | - it("should parse config path from --config flag", () => { |
36 | | - process.argv = ["node", "script.js", "--config", "/path/to/config.yaml"]; |
37 | | - const result = parseArgs(); |
38 | | - expect(result.configPath).toBe("/path/to/config.yaml"); |
39 | | - }); |
40 | | - |
41 | | - it("should parse single rule from --rule flag", () => { |
42 | | - process.argv = ["node", "script.js", "--rule", "my-rule"]; |
43 | | - const result = parseArgs(); |
44 | | - expect(result.rules).toEqual(["my-rule"]); |
45 | | - }); |
46 | | - |
47 | | - it("should parse multiple rules from multiple --rule flags", () => { |
48 | | - process.argv = ["node", "script.js", "--rule", "rule1", "--rule", "rule2"]; |
49 | | - const result = parseArgs(); |
50 | | - expect(result.rules).toEqual(["rule1", "rule2"]); |
51 | | - }); |
52 | | - |
53 | | - it("should parse prompt from last non-flag argument", () => { |
54 | | - process.argv = ["node", "script.js", "Hello world"]; |
55 | | - const result = parseArgs(); |
56 | | - expect(result.prompt).toBe("Hello world"); |
57 | | - }); |
58 | | - |
59 | | - it("should handle multiple flags together", () => { |
60 | | - process.argv = ["node", "script.js", "--print", "--readonly", "--resume"]; |
61 | | - const result = parseArgs(); |
62 | | - expect(result).toEqual({ |
63 | | - readonly: true, |
64 | | - resume: true, |
65 | | - }); |
66 | | - }); |
67 | | - |
68 | | - it("should handle config flag with other flags", () => { |
69 | | - process.argv = ["node", "script.js", "--config", "config.yaml", "--print"]; |
70 | | - const result = parseArgs(); |
71 | | - expect(result).toEqual({ |
72 | | - configPath: "config.yaml", |
73 | | - }); |
74 | | - }); |
75 | | - |
76 | | - it("should handle rule flag with other flags", () => { |
77 | | - process.argv = ["node", "script.js", "--rule", "my-rule", "--print"]; |
78 | | - const result = parseArgs(); |
79 | | - expect(result).toEqual({ |
80 | | - rules: ["my-rule"], |
81 | | - }); |
82 | | - }); |
83 | | - |
84 | | - it("should handle prompt with flags", () => { |
85 | | - process.argv = ["node", "script.js", "--print", "What is the weather?"]; |
86 | | - const result = parseArgs(); |
87 | | - expect(result).toEqual({ |
88 | | - prompt: "What is the weather?", |
89 | | - }); |
90 | | - }); |
91 | | - |
92 | | - it("should handle config flag with prompt", () => { |
93 | | - process.argv = [ |
94 | | - "node", |
95 | | - "script.js", |
96 | | - "--config", |
97 | | - "config.yaml", |
98 | | - "Test prompt", |
99 | | - ]; |
100 | | - const result = parseArgs(); |
101 | | - expect(result).toEqual({ |
102 | | - configPath: "config.yaml", |
103 | | - prompt: "Test prompt", |
104 | | - }); |
105 | | - }); |
106 | | - |
107 | | - it("should handle rule flag with prompt", () => { |
108 | | - process.argv = ["node", "script.js", "--rule", "my-rule", "Test prompt"]; |
109 | | - const result = parseArgs(); |
110 | | - expect(result).toEqual({ |
111 | | - rules: ["my-rule"], |
112 | | - prompt: "Test prompt", |
113 | | - }); |
114 | | - }); |
115 | | - |
116 | | - it("should ignore config and rule flag values when extracting prompt", () => { |
117 | | - process.argv = [ |
118 | | - "node", |
119 | | - "script.js", |
120 | | - "--config", |
121 | | - "config.yaml", |
122 | | - "--rule", |
123 | | - "my-rule", |
124 | | - "actual-prompt", |
125 | | - ]; |
126 | | - const result = parseArgs(); |
127 | | - expect(result.prompt).toBe("actual-prompt"); |
128 | | - expect(result.configPath).toBe("config.yaml"); |
129 | | - expect(result.rules).toEqual(["my-rule"]); |
130 | | - }); |
131 | | - |
132 | | - it("should handle multiple non-flag arguments and use the last one as prompt", () => { |
133 | | - process.argv = ["node", "script.js", "first", "second", "third"]; |
134 | | - const result = parseArgs(); |
135 | | - expect(result.prompt).toBe("third"); |
136 | | - }); |
137 | | - |
138 | | - it("should handle complex argument combinations", () => { |
139 | | - process.argv = [ |
140 | | - "node", |
141 | | - "script.js", |
142 | | - "--print", |
143 | | - "--config", |
144 | | - "my-config.yaml", |
145 | | - "--rule", |
146 | | - "rule1", |
147 | | - "--rule", |
148 | | - "rule2", |
149 | | - "--readonly", |
150 | | - "--resume", |
151 | | - "Complex prompt with spaces", |
152 | | - ]; |
153 | | - const result = parseArgs(); |
154 | | - expect(result).toEqual({ |
155 | | - configPath: "my-config.yaml", |
156 | | - rules: ["rule1", "rule2"], |
157 | | - readonly: true, |
158 | | - resume: true, |
159 | | - prompt: "Complex prompt with spaces", |
160 | | - }); |
161 | | - }); |
162 | | - |
163 | | - it("should handle config flag without value", () => { |
164 | | - process.argv = ["node", "script.js", "--config"]; |
165 | | - const result = parseArgs(); |
166 | | - expect(result.configPath).toBeUndefined(); |
167 | | - }); |
168 | | - |
169 | | - it("should handle rule flag without value", () => { |
170 | | - process.argv = ["node", "script.js", "--rule"]; |
171 | | - const result = parseArgs(); |
172 | | - expect(result.rules).toEqual([]); |
173 | | - }); |
174 | | - |
175 | | - it("should handle config flag at the end without value", () => { |
176 | | - process.argv = ["node", "script.js", "--print", "--config"]; |
177 | | - const result = parseArgs(); |
178 | | - expect(result.configPath).toBeUndefined(); |
179 | | - }); |
180 | | - |
181 | | - it("should handle rule flag at the end without value", () => { |
182 | | - process.argv = ["node", "script.js", "--print", "--rule"]; |
183 | | - const result = parseArgs(); |
184 | | - expect(result.rules).toEqual([]); |
185 | | - }); |
186 | | - |
187 | | - it("should handle empty arguments array", () => { |
188 | | - process.argv = ["node", "script.js"]; |
189 | | - const result = parseArgs(); |
190 | | - expect(result).toEqual({}); |
191 | | - }); |
192 | | - |
193 | | - it("should handle multiple boolean flags", () => { |
194 | | - process.argv = ["node", "script.js", "--readonly", "--resume"]; |
195 | | - const result = parseArgs(); |
196 | | - expect(result.readonly).toBe(true); |
197 | | - expect(result.resume).toBe(true); |
198 | | - }); |
199 | | - |
200 | | - it("should handle multiple rules with complex combinations", () => { |
201 | | - process.argv = [ |
202 | | - "node", |
203 | | - "script.js", |
204 | | - "--rule", |
205 | | - "file.txt", |
206 | | - "--rule", |
207 | | - "owner/package", |
208 | | - "--rule", |
209 | | - "direct string rule", |
210 | | - ]; |
211 | | - const result = parseArgs(); |
212 | | - expect(result.rules).toEqual([ |
213 | | - "file.txt", |
214 | | - "owner/package", |
215 | | - "direct string rule", |
216 | | - ]); |
217 | | - }); |
218 | | -}); |
219 | | - |
| 2 | +import { processRule as processPromptOrRule } from "./hubLoader.js"; |
220 | 3 | describe("processPromptOrRule (loadRuleFromHub integration)", () => { |
221 | 4 | // Mock fetch for hub tests |
222 | 5 | const originalFetch = global.fetch; |
|
0 commit comments