fix(ask): always allow custom select answers

This commit is contained in:
云服务部-叶林立
2026-08-28 14:26:58 +08:00
parent 221e978622
commit d1b11f86f7
7 changed files with 19 additions and 14 deletions
+15 -3
View File
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import test from "node:test";
import { formatAnswers, normalizeQuestions, orderedAnswers } from "../src/normalize.ts";
import type { UserAnswer } from "../src/types.ts";
import type { QuestionInput, UserAnswer } from "../src/types.ts";
test("normalizes select and text questions with bounded defaults", () => {
const questions = normalizeQuestions([
@@ -27,14 +27,26 @@ test("normalizes select and text questions with bounded defaults", () => {
prompt: "Choose a language",
type: "select",
options: [{ value: "ts", label: "TypeScript" }],
allowCustom: true,
required: true,
});
assert.equal(questions[1]?.label, "Q2");
assert.equal(questions[1]?.allowCustom, false);
assert.equal(questions[1]?.required, false);
});
test("ignores the removed allowCustom input from legacy callers", () => {
const legacyQuestion = {
id: "language",
prompt: "Choose a language",
type: "select",
options: [{ value: "ts", label: "TypeScript" }],
allowCustom: false,
} satisfies QuestionInput & { allowCustom: boolean };
const [question] = normalizeQuestions([legacyQuestion]);
assert.ok(question);
assert.equal("allowCustom" in question, false);
});
test("rejects invalid ids, duplicate ids, invalid option combinations, and duplicate values", () => {
assert.throws(() => normalizeQuestions([{ id: "bad id", prompt: "Bad", type: "text" }]), /must start with a letter/);
assert.throws(() => normalizeQuestions([null as never]), /must be an object/);