The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | buildPendingUserInputAnswers, |
| 5 | isPendingUserInputOptionSelected, |
| 6 | resolvePendingUserInputAnswer, |
| 7 | setPendingUserInputCustomAnswer, |
| 8 | togglePendingUserInputOptionSelection, |
| 9 | } from "./pendingUserInput.ts"; |
| 10 | |
| 11 | const singleSelectQuestion = { |
| 12 | id: "scope", |
| 13 | header: "Scope", |
| 14 | question: "What should the plan target first?", |
| 15 | options: [ |
| 16 | { label: "Orchestration-first", description: "Focus on orchestration first" }, |
| 17 | { label: "Node.js", description: "Reuse TypeScript" }, |
| 18 | ], |
| 19 | multiSelect: false, |
| 20 | } as const; |
| 21 | |
| 22 | const nativeChoiceQuestion = { |
| 23 | id: "result", |
| 24 | header: "Result", |
| 25 | question: "Which result should be used?", |
| 26 | options: [ |
| 27 | { value: " first\t", label: "Result", description: "First result" }, |
| 28 | { value: "second", label: "Result", description: "Second result" }, |
| 29 | ], |
| 30 | allowCustomAnswer: false, |
| 31 | multiSelect: false, |
| 32 | } as const; |
| 33 | |
| 34 | describe("resolvePendingUserInputAnswer", () => { |
| 35 | it("preserves exact editor text, including a deliberately cleared answer", () => { |
| 36 | const question = { ...singleSelectQuestion, initialAnswer: " Proposed message\n" }; |
| 37 | expect(resolvePendingUserInputAnswer(question, { customAnswer: question.initialAnswer })).toBe( |
| 38 | question.initialAnswer, |
| 39 | ); |
| 40 | expect(resolvePendingUserInputAnswer(question, { customAnswer: " Edited message\n\n" })).toBe( |
| 41 | " Edited message\n\n", |
| 42 | ); |
| 43 | expect(resolvePendingUserInputAnswer(question, { customAnswer: "" })).toBe(""); |
| 44 | expect(resolvePendingUserInputAnswer(question, { customAnswer: " \n" })).toBe(" \n"); |
| 45 | expect( |
| 46 | setPendingUserInputCustomAnswer( |
| 47 | question, |
| 48 | { selectedOptionValues: ["Orchestration-first"] }, |
| 49 | "", |
| 50 | ), |
| 51 | ).toEqual({ customAnswer: "" }); |
| 52 | }); |
| 53 | |
| 54 | it("prefers a custom answer over selected options", () => { |
| 55 | expect( |
| 56 | resolvePendingUserInputAnswer(singleSelectQuestion, { |
| 57 | selectedOptionValues: ["Orchestration-first"], |
| 58 | customAnswer: "Keep the existing envelope for one release", |
| 59 | }), |
| 60 | ).toBe("Keep the existing envelope for one release"); |
| 61 | }); |
| 62 | |
| 63 | it("does not replace a required choice with a custom answer", () => { |
| 64 | expect( |
| 65 | resolvePendingUserInputAnswer(nativeChoiceQuestion, { |
| 66 | selectedOptionValues: ["second"], |
| 67 | customAnswer: "Use another result", |
| 68 | }), |
| 69 | ).toBe("second"); |
| 70 | }); |
| 71 | |
| 72 | it("does not submit labels or unknown values when an option has a value", () => { |
| 73 | expect( |
| 74 | resolvePendingUserInputAnswer(nativeChoiceQuestion, { |
| 75 | selectedOptionValues: ["Result", "unknown"], |
| 76 | }), |
| 77 | ).toBeNull(); |
| 78 | }); |
| 79 | |
| 80 | it("matches a label only for an option without a value", () => { |
| 81 | expect( |
| 82 | resolvePendingUserInputAnswer(singleSelectQuestion, { selectedOptionValues: [" Node.js "] }), |
| 83 | ).toBe("Node.js"); |
| 84 | }); |
| 85 | |
| 86 | it("lets an optional text question stay blank", () => { |
| 87 | const textQuestion = { id: "notes", options: [] }; |
| 88 | expect(resolvePendingUserInputAnswer({ ...textQuestion, required: false }, undefined)).toBe(""); |
| 89 | expect( |
| 90 | resolvePendingUserInputAnswer({ ...textQuestion, required: false }, { customAnswer: " " }), |
| 91 | ).toBe(""); |
| 92 | expect(resolvePendingUserInputAnswer(textQuestion, { customAnswer: " " })).toBeNull(); |
| 93 | expect( |
| 94 | resolvePendingUserInputAnswer({ ...textQuestion, required: true }, { customAnswer: "" }), |
| 95 | ).toBeNull(); |
| 96 | }); |
| 97 | |
| 98 | it("accepts ready attachment-only answers while preserving selected options", () => { |
| 99 | expect(resolvePendingUserInputAnswer(singleSelectQuestion, { attachmentCount: 1 })).toBe(""); |
| 100 | expect( |
| 101 | resolvePendingUserInputAnswer(singleSelectQuestion, { |
| 102 | attachmentCount: 1, |
| 103 | selectedOptionValues: ["Node.js"], |
| 104 | }), |
| 105 | ).toBe("Node.js"); |
| 106 | expect( |
| 107 | resolvePendingUserInputAnswer(singleSelectQuestion, { |
| 108 | attachmentCount: 1, |
| 109 | attachmentsBlocked: true, |
| 110 | }), |
| 111 | ).toBeNull(); |
| 112 | expect( |
| 113 | resolvePendingUserInputAnswer( |
| 114 | { ...singleSelectQuestion, allowCustomAnswer: false }, |
| 115 | { attachmentCount: 1 }, |
| 116 | ), |
| 117 | ).toBeNull(); |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | describe("togglePendingUserInputOptionSelection", () => { |
| 122 | it("selects and removes options with the same label independently", () => { |
| 123 | const question = { ...nativeChoiceQuestion, multiSelect: true }; |
| 124 | const firstSelected = togglePendingUserInputOptionSelection(question, undefined, " first\t"); |
| 125 | const bothSelected = togglePendingUserInputOptionSelection(question, firstSelected, "second"); |
| 126 | |
| 127 | expect(buildPendingUserInputAnswers([question], { result: bothSelected })).toEqual({ |
| 128 | result: [" first\t", "second"], |
| 129 | }); |
| 130 | |
| 131 | const secondSelected = togglePendingUserInputOptionSelection( |
| 132 | question, |
| 133 | bothSelected, |
| 134 | " first\t", |
| 135 | ); |
| 136 | expect(buildPendingUserInputAnswers([question], { result: secondSelected })).toEqual({ |
| 137 | result: ["second"], |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | it("submits raw option values and leaves the draft alone for an unknown option", () => { |
| 142 | const first = togglePendingUserInputOptionSelection( |
| 143 | nativeChoiceQuestion, |
| 144 | undefined, |
| 145 | " first\t", |
| 146 | ); |
| 147 | expect(isPendingUserInputOptionSelected(nativeChoiceQuestion, first, " first\t")).toBe(true); |
| 148 | expect(isPendingUserInputOptionSelected(nativeChoiceQuestion, first, "second")).toBe(false); |
| 149 | expect(buildPendingUserInputAnswers([nativeChoiceQuestion], { result: first })).toEqual({ |
| 150 | result: " first\t", |
| 151 | }); |
| 152 | expect(togglePendingUserInputOptionSelection(nativeChoiceQuestion, first, "Result")).toBe( |
| 153 | first, |
| 154 | ); |
| 155 | }); |
| 156 | }); |
| 157 | |
| 158 | describe("setPendingUserInputCustomAnswer", () => { |
| 159 | it("rejects arbitrary text when the provider only accepts offered options", () => { |
| 160 | expect(setPendingUserInputCustomAnswer(nativeChoiceQuestion, undefined, "Other")).toEqual({}); |
| 161 | expect( |
| 162 | buildPendingUserInputAnswers([nativeChoiceQuestion], { result: { customAnswer: "Other" } }), |
| 163 | ).toBeNull(); |
| 164 | expect( |
| 165 | buildPendingUserInputAnswers([nativeChoiceQuestion], { |
| 166 | result: { selectedOptionValues: ["second"], customAnswer: "stale draft" }, |
| 167 | }), |
| 168 | ).toEqual({ result: "second" }); |
| 169 | }); |
| 170 | }); |
| 171 | |
| 172 | const compatQuestion = { |
| 173 | id: "compat", |
| 174 | header: "Compat", |
| 175 | question: "How strict should compatibility be?", |
| 176 | options: [{ label: "Keep current envelope", description: "Preserve current wire format" }], |
| 177 | multiSelect: false, |
| 178 | } as const; |
| 179 | |
| 180 | describe("buildPendingUserInputAnswers", () => { |
| 181 | it("returns a canonical answer map for complete prompts", () => { |
| 182 | expect( |
| 183 | buildPendingUserInputAnswers([singleSelectQuestion, compatQuestion], { |
| 184 | scope: { selectedOptionValues: ["Orchestration-first"] }, |
| 185 | compat: { customAnswer: "Keep the current envelope for one release window" }, |
| 186 | }), |
| 187 | ).toEqual({ |
| 188 | scope: "Orchestration-first", |
| 189 | compat: "Keep the current envelope for one release window", |
| 190 | }); |
| 191 | }); |
| 192 | }); |
| 193 | |
| 194 | it.each([ |
| 195 | ["single-select", singleSelectQuestion, ["Orchestration-first"], '"Orchestration-first"'], |
| 196 | [ |
| 197 | "multi-select", |
| 198 | { |
| 199 | ...singleSelectQuestion, |
| 200 | multiSelect: true, |
| 201 | options: [{ label: "Server" }, { label: "Web" }], |
| 202 | }, |
| 203 | ["Server", "Web"], |
| 204 | '["Server","Web"]', |
| 205 | ], |
| 206 | ])("sends a %s answer to a question with the id __proto__", (_kind, question, selected, json) => { |
| 207 | const answers = buildPendingUserInputAnswers( |
| 208 | [ |
| 209 | { ...question, id: "__proto__" }, |
| 210 | { ...singleSelectQuestion, id: "name" }, |
| 211 | ], |
| 212 | { |
| 213 | ["__proto__"]: { selectedOptionValues: selected }, |
| 214 | name: { selectedOptionValues: ["Orchestration-first"] }, |
| 215 | }, |
| 216 | ); |
| 217 | |
| 218 | expect(JSON.stringify(answers)).toBe(`{"__proto__":${json},"name":"Orchestration-first"}`); |
| 219 | }); |