-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost-resolve-validation.test.ts
More file actions
227 lines (189 loc) · 7.21 KB
/
host-resolve-validation.test.ts
File metadata and controls
227 lines (189 loc) · 7.21 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { beforeEach, describe, expect, it, vi } from "vitest";
const createPromptMock = vi.fn();
vi.mock("../core/prompt.js", () => ({
createPrompt: (options: unknown) => createPromptMock(options),
}));
type CapturedPromptConfig = {
initialValue: unknown;
osc?: {
type: string;
options?: Array<{ value: unknown; label: string }>;
initialValue?: unknown;
initialValues?: unknown[];
};
parseOscResolveValue?: (value: unknown) => unknown;
};
function getLastConfig(): CapturedPromptConfig {
expect(createPromptMock).toHaveBeenCalled();
const calls = createPromptMock.mock.calls;
return calls[calls.length - 1]![0] as CapturedPromptConfig;
}
beforeEach(() => {
createPromptMock.mockReset();
createPromptMock.mockImplementation(async (options: CapturedPromptConfig) => options.initialValue);
});
describe("OSC payload values", () => {
it("select keeps non-string option values and initialValue", async () => {
const { select } = await import("../prompts/select.js");
const initial = { id: 2 };
await select({
message: "Pick one",
options: [
{ value: 1, label: "One" },
{ value: initial, label: "Two" },
],
initialValue: initial,
});
const config = getLastConfig();
expect(config.osc?.type).toBe("select");
expect(config.osc?.options?.map((opt) => opt.value)).toEqual([1, initial]);
expect(config.osc?.initialValue).toBe(initial);
});
it("multiselect keeps non-string option values and initialValues", async () => {
const { multiselect } = await import("../prompts/multiselect.js");
const a = { key: "a" };
const b = { key: "b" };
await multiselect({
message: "Pick many",
options: [
{ value: a, label: "A" },
{ value: b, label: "B" },
],
initialValues: [b],
});
const config = getLastConfig();
expect(config.osc?.type).toBe("multiselect");
expect(config.osc?.options?.map((opt) => opt.value)).toEqual([a, b]);
expect(config.osc?.initialValues).toEqual([b]);
});
it("search keeps non-string option values", async () => {
const { search } = await import("../prompts/search.js");
const value = { code: "eu" };
await search({
message: "Search",
options: [{ value, label: "Europe" }],
});
const config = getLastConfig();
expect(config.osc?.type).toBe("select");
expect(config.osc?.options?.[0]?.value).toBe(value);
});
});
describe("Host resolve validation", () => {
it("select maps deep-equal resolve values back to canonical option values", async () => {
const { select } = await import("../prompts/select.js");
const canonical = { id: 7 };
await select({
message: "Pick one",
options: [
{ value: canonical, label: "Seven" },
{ value: { id: 8 }, label: "Eight", disabled: true },
],
});
const config = getLastConfig();
expect(config.parseOscResolveValue?.({ id: 7 })).toBe(canonical);
expect(() => config.parseOscResolveValue?.({ id: 8 })).toThrow("Invalid resolve value");
expect(() => config.parseOscResolveValue?.({ id: 9 })).toThrow("Invalid resolve value");
});
it("multiselect validates required and option membership", async () => {
const { multiselect } = await import("../prompts/multiselect.js");
const a = { name: "a" };
const b = { name: "b" };
await multiselect({
message: "Pick",
options: [
{ value: a, label: "A" },
{ value: b, label: "B" },
],
required: true,
});
const config = getLastConfig();
expect(config.parseOscResolveValue?.([{ name: "b" }, { name: "a" }])).toEqual([a, b]);
expect(() => config.parseOscResolveValue?.([])).toThrow(
"Resolve value must include at least one option",
);
expect(() => config.parseOscResolveValue?.([{ name: "missing" }])).toThrow(
"Resolve value contains unknown option",
);
});
it("multiselect allows empty resolve values when required is false", async () => {
const { multiselect } = await import("../prompts/multiselect.js");
await multiselect({
message: "Pick",
options: [{ value: "a", label: "A" }],
required: false,
});
const config = getLastConfig();
expect(config.parseOscResolveValue?.([])).toEqual([]);
});
it("search validates membership and returns canonical values", async () => {
const { search } = await import("../prompts/search.js");
const canonical = { slug: "eu-west" };
await search({
message: "Region",
options: [{ value: canonical, label: "EU West" }],
});
const config = getLastConfig();
expect(config.parseOscResolveValue?.({ slug: "eu-west" })).toBe(canonical);
expect(() => config.parseOscResolveValue?.({ slug: "us-east" })).toThrow(
"Invalid resolve value",
);
});
it("confirm only accepts boolean resolve values", async () => {
const { confirm } = await import("../prompts/confirm.js");
await confirm({ message: "Continue?" });
const config = getLastConfig();
expect(config.parseOscResolveValue?.(true)).toBe(true);
expect(config.parseOscResolveValue?.(false)).toBe(false);
expect(() => config.parseOscResolveValue?.("true")).toThrow(
"Resolve value must be boolean",
);
});
it("input requires a string and runs validate", async () => {
const { input } = await import("../prompts/input.js");
await input({
message: "Name?",
validate: (value) => (value.length >= 3 ? true : "too short"),
});
const config = getLastConfig();
expect(config.parseOscResolveValue?.("alice")).toBe("alice");
expect(() => config.parseOscResolveValue?.("ab")).toThrow(
"Resolve value failed validation",
);
expect(() => config.parseOscResolveValue?.(123)).toThrow("Resolve value must be string");
});
it("password requires a string and runs validate", async () => {
const { password } = await import("../prompts/password.js");
await password({
message: "Secret?",
validate: (value) => (value.includes("!") ? true : "missing bang"),
});
const config = getLastConfig();
expect(config.parseOscResolveValue?.("abc!")).toBe("abc!");
expect(() => config.parseOscResolveValue?.("abc")).toThrow(
"Resolve value failed validation",
);
expect(() => config.parseOscResolveValue?.(null)).toThrow("Resolve value must be string");
});
it("number accepts numeric values/strings and enforces bounds + custom validation", async () => {
const { number } = await import("../prompts/number.js");
await number({
message: "Port",
min: 1,
max: 10,
validate: (value) => (value % 2 === 0 ? true : "must be even"),
initialValue: 6,
});
const config = getLastConfig();
expect(config.osc?.initialValue).toBe(6);
expect(config.parseOscResolveValue?.("8")).toBe(8);
expect(config.parseOscResolveValue?.(4)).toBe(4);
expect(() => config.parseOscResolveValue?.("abc")).toThrow(
"Resolve value must be numeric",
);
expect(() => config.parseOscResolveValue?.(11)).toThrow("Resolve value above max");
expect(() => config.parseOscResolveValue?.(0)).toThrow("Resolve value below min");
expect(() => config.parseOscResolveValue?.(3)).toThrow(
"Resolve value failed validation",
);
});
});