Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2
3import { parseElicitationForm } from "./elicitationForm.ts";
4
5describe("parseElicitationForm", () => {
6 for (const type of ["string", "array"] as const) {
7 it.each([
8 { values: [" padded ", "", "plain"], labels: ["padded", '""', "plain"] },
9 { values: [" a", "a", "b"], labels: ['" a"', '"a"', "b"] },
10 { values: [" a", "a", '"a"'], labels: ['" a"', '"a"', '"\\"a\\""'] },
11 ])(
12 `preserves ${type} enum values with distinct readable labels: $values`,
13 ({ values, labels }) => {
14 const form = parseElicitationForm(
15 {
16 type: "object",
17 properties: {
18 answer:
19 type === "array"
20 ? { type, items: { type: "string", enum: values } }
21 : { type, enum: values },
22 },
23 required: ["answer"],
24 },
25 "Choose",
26 );
27 assert(form !== undefined);
28 const question = form.questions[0];
29 assert(question !== undefined);
30 assert.deepStrictEqual(
31 question.options.map((option) => option.label),
32 labels,
33 );
34 assert.deepStrictEqual(
35 question.options.map((option) => option.value),
36 values,
37 );
38 assert.strictEqual(question.allowCustomAnswer, false);
39 assert.strictEqual(question.multiSelect, type === "array" ? true : undefined);
40 for (const value of values) {
41 const answer = type === "array" ? [value] : value;
42 assert.deepStrictEqual(form.content({ answer }), { answer });
43 }
44 },
45 );
46 }
47
48 it("asks each question once when a choice has a free-text companion field", () => {
49 // OMP's ask dialog follows each choice `q<n>` with a free-text `q<n>__other` field.
50 const ask = (question: string, header: string) => ({
51 type: "string",
52 title: question,
53 description: header,
54 oneOf: [
55 { const: "Yes", title: "Yes" },
56 { const: "No", title: "No" },
57 ],
58 });
59 const form = parseElicitationForm(
60 {
61 type: "object",
62 properties: {
63 q0: ask("Use Postgres for the new service?", "Database"),
64 q0__other: { type: "string", title: "Other (type your own)" },
65 q1: ask("Deploy to staging first?", "Deploy"),
66 q1__other: { type: "string", title: "Other (type your own)" },
67 },
68 },
69 "Answer 2 questions",
70 );
71 assert(form !== undefined);
72 assert.deepStrictEqual(
73 form.questions.map((question) =>
74 [question.header, question.question].some((text) => text.endsWith("?")),
75 ),
76 [true, true],
77 );
78 assert.deepStrictEqual(form.content({ q0: "Yes", q1: "Only on Fridays" }), {
79 q0: "Yes",
80 q1__other: "Only on Fridays",
81 });
82
83 // A single-underscore `<id>_other` companion folds the same way.
84 const singleUnderscore = parseElicitationForm(
85 {
86 type: "object",
87 properties: {
88 q0: ask("Use Postgres for the new service?", "Database"),
89 q0_other: { type: "string", title: "Other" },
90 },
91 },
92 "Answer 1 question",
93 );
94 assert(singleUnderscore !== undefined);
95 assert.deepStrictEqual(
96 singleUnderscore.questions.map((question) => question.id),
97 ["q0"],
98 );
99 assert.deepStrictEqual(singleUnderscore.content({ q0: "Only on Fridays" }), {
100 q0_other: "Only on Fridays",
101 });
102
103 // A multi-select answer can carry options and free text together, so both fields stay.
104 const multi = parseElicitationForm(
105 {
106 type: "object",
107 properties: {
108 q0: { type: "array", title: "Databases?", items: { anyOf: [{ const: "Postgres" }] } },
109 q0__other: { type: "string", title: "Other (type your own)" },
110 },
111 },
112 "Databases?",
113 );
114 assert(multi !== undefined);
115 assert.deepStrictEqual(
116 multi.questions.map((question) => [question.id, question.required]),
117 [
118 ["q0", undefined],
119 ["q0__other", false],
120 ],
121 );
122 assert.deepStrictEqual(multi.content({ q0: ["Postgres"], q0__other: "SQLite" }), {
123 q0: ["Postgres"],
124 q0__other: "SQLite",
125 });
126 });
127
128 it("requires a multi-select only for a required minimum and accepts none otherwise", () => {
129 const items = { type: "string", enum: ["web", "mobile"] };
130 const form = parseElicitationForm(
131 {
132 type: "object",
133 properties: {
134 targets: { type: "array", minItems: 1, items },
135 extras: { type: "array", items },
136 platforms: { type: "array", minItems: 1, items },
137 },
138 required: ["targets"],
139 },
140 "Choose",
141 );
142 assert(form !== undefined);
143 assert.deepStrictEqual(
144 form.questions.map((question) => [question.id, question.required]),
145 [
146 ["targets", true],
147 ["extras", undefined],
148 ["platforms", undefined],
149 ],
150 );
151 assert.deepStrictEqual(form.content({ targets: ["web"], extras: [], platforms: [] }), {
152 targets: ["web"],
153 extras: [],
154 });
155 });
156
157 it("marks optional number and integer fields as not required and omits blank answers", () => {
158 const form = parseElicitationForm(
159 {
160 type: "object",
161 properties: {
162 limit: { type: "integer" },
163 ratio: { type: "number" },
164 retries: { type: "integer" },
165 threshold: { type: "number" },
166 },
167 required: ["retries", "threshold"],
168 },
169 "Configure limits",
170 );
171 assert(form !== undefined);
172 assert.deepStrictEqual(
173 form.questions.map((question) => [question.id, question.required]),
174 [
175 ["limit", false],
176 ["ratio", false],
177 ["retries", undefined],
178 ["threshold", undefined],
179 ],
180 );
181 assert.deepStrictEqual(form.content({ limit: "", ratio: "", retries: "3", threshold: "0.5" }), {
182 retries: 3,
183 threshold: 0.5,
184 });
185 assert.isUndefined(form.content({ limit: "", ratio: "", retries: "", threshold: "0.5" }));
186 });
187});