summaryrefslogtreecommitdiffhomepage
path: root/packages/cli/src/args.test.ts
blob: 02b9e9bd8c4bc1be5b494720e421305447867427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { describe, expect, it } from "vitest";
import { parseArgs } from "./args.js";

const defaultServer = "http://localhost:24203";

describe("parseArgs", () => {
	it("returns help for empty argv", () => {
		expect(parseArgs([], { defaultServer })).toEqual({ kind: "help" });
	});

	it("returns help for --help", () => {
		expect(parseArgs(["--help"], { defaultServer })).toEqual({ kind: "help" });
	});

	it("returns help for -h", () => {
		expect(parseArgs(["-h"], { defaultServer })).toEqual({ kind: "help" });
	});

	describe("models", () => {
		it("parses 'models' with default server", () => {
			expect(parseArgs(["models"], { defaultServer })).toEqual({
				kind: "models",
				server: "http://localhost:24203",
			});
		});

		it("parses 'models --server <url>'", () => {
			expect(parseArgs(["models", "--server", "http://example.com"], { defaultServer })).toEqual({
				kind: "models",
				server: "http://example.com",
			});
		});

		it("errors on unknown argument for models", () => {
			const result = parseArgs(["models", "--foo"], { defaultServer });
			expect(result.kind).toBe("error");
			if (result.kind === "error") expect(result.message).toContain("Unknown argument");
		});
	});

	describe("chat", () => {
		it("parses a chat with --text", () => {
			const result = parseArgs(["my-model", "--text", "hello"], { defaultServer });
			expect(result).toEqual({
				kind: "chat",
				server: "http://localhost:24203",
				modelName: "my-model",
				text: "hello",
				file: undefined,
				cwd: undefined,
				conversationId: undefined,
				showReasoning: false,
			});
		});

		it("parses a chat with --file", () => {
			const result = parseArgs(["my-model", "--file", "foo.txt"], { defaultServer });
			expect(result).toEqual({
				kind: "chat",
				server: "http://localhost:24203",
				modelName: "my-model",
				text: undefined,
				file: "foo.txt",
				cwd: undefined,
				conversationId: undefined,
				showReasoning: false,
			});
		});

		it("parses a chat with both --text and --file", () => {
			const result = parseArgs(["m", "--text", "hi", "--file", "f.txt"], { defaultServer });
			expect(result).toMatchObject({ kind: "chat", text: "hi", file: "f.txt" });
		});

		it("parses --cwd, --conversation, --server, --show-reasoning", () => {
			const result = parseArgs(
				[
					"m",
					"--text",
					"x",
					"--cwd",
					"/tmp",
					"--conversation",
					"abc",
					"--server",
					"http://s",
					"--show-reasoning",
				],
				{ defaultServer },
			);
			expect(result).toEqual({
				kind: "chat",
				server: "http://s",
				modelName: "m",
				text: "x",
				file: undefined,
				cwd: "/tmp",
				conversationId: "abc",
				showReasoning: true,
			});
		});

		it("errors when text and file are both missing", () => {
			const result = parseArgs(["my-model"], { defaultServer });
			expect(result.kind).toBe("error");
			if (result.kind === "error") expect(result.message).toContain("--text or --file");
		});

		it("errors on unknown flag", () => {
			const result = parseArgs(["my-model", "--text", "hi", "--bogus"], { defaultServer });
			expect(result.kind).toBe("error");
			if (result.kind === "error") expect(result.message).toContain("Unknown flag");
		});

		it("errors when --text has no value", () => {
			const result = parseArgs(["m", "--text"], { defaultServer });
			expect(result.kind).toBe("error");
		});

		it("errors when --file has no value", () => {
			const result = parseArgs(["m", "--file"], { defaultServer });
			expect(result.kind).toBe("error");
		});

		it("errors when --server has no value", () => {
			const result = parseArgs(["models", "--server"], { defaultServer });
			expect(result.kind).toBe("error");
		});

		it("errors when --cwd has no value", () => {
			const result = parseArgs(["m", "--text", "x", "--cwd"], { defaultServer });
			expect(result.kind).toBe("error");
		});

		it("errors when --conversation has no value", () => {
			const result = parseArgs(["m", "--text", "x", "--conversation"], { defaultServer });
			expect(result.kind).toBe("error");
		});
	});
});