Open History to see the last commit
1import Foundation
2import Testing
3@testable import T3Code
4
5@Suite("User input answers")
6struct UserInputAnswerTests {
7 @Test
8 func attachmentDraftsKeepEachFileWithItsQuestion() async throws {
9 let first = FeatureDraftAttachment(data: Data([1]), filename: "one.png", mimeType: "image/png")
10 let second = FeatureDraftAttachment(data: Data([2]), filename: "two.png", mimeType: "image/png")
11 let value = ["first": [first], "second": [second]]
12 #expect(try FeatureQuestionAttachmentDraft.decode(FeatureQuestionAttachmentDraft.encode(value)) == value)
13 #expect(try FeatureQuestionAttachmentDraft.encode(["empty": []]).isEmpty)
14 let left = FeatureScopedID.input(environmentID: "left", wireID: "same-request")
15 let right = FeatureScopedID.input(environmentID: "right", wireID: "same-request")
16 #expect(FeatureQuestionAttachmentDraft.key(inputID: left) != FeatureQuestionAttachmentDraft.key(inputID: right))
17 let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
18 defer { try? FileManager.default.removeItem(at: directory) }
19 let fileURL = directory.appendingPathComponent("drafts.json")
20 let store = FeatureComposerDraftStore(fileURL: fileURL)
21 let key = FeatureQuestionAttachmentDraft.key(inputID: left)
22 try await store.setDraft(FeatureQuestionAttachmentDraft.encode(value), for: key)
23 let reopened = FeatureComposerDraftStore(fileURL: fileURL)
24 #expect(try await FeatureQuestionAttachmentDraft.decode(reopened.draft(for: key)) == value)
25 }
26
27 @Test
28 func answerHistoryRendersQuestionTextAndAttachmentsWithoutRawJSON() {
29 let activity = OrchestrationActivity(
30 id: "answer", tone: "info", kind: "user-input.answer-submitted", summary: "Question answer submitted",
31 payload: .object([
32 "requestId": .string("request"),
33 "questionTextById": .object(["scope": .string("Which parts?")]),
34 "answers": .object(["scope": .array([.string("Server"), .string("Web")])]),
35 "attachmentsByQuestionId": .object(["scope": .array([
36 .object([
37 "type": .string("image"), "id": .string("image"),
38 "name": .string("screenshot.png"), "mimeType": .string("image/png"), "sizeBytes": .number(2),
39 ]),
40 .null,
41 ])]),
42 ]), turnId: nil, sequence: nil, createdAt: "2026-09-08T12:00:00Z"
43 )
44 let messages = NativeQuestionAnswerHistory.messages(activity, createdAt: .distantPast)
45 #expect(messages.count == 1)
46 #expect(messages.first?.text == "Which parts?\n\nServer, Web")
47 #expect(messages.first?.role == .user)
48 #expect(messages.first?.attachments.map(\.id) == ["image"])
49 }
50
51 @Test
52 func cachedQuestionsWithoutResponseModeCannotBeDismissed() throws {
53 let input = try JSONDecoder().decode(FeatureUserInput.self, from: Data(
54 #"{"id":"old","threadID":"thread","questions":[]}"#.utf8
55 ))
56 #expect(!input.canDismiss)
57 }
58
59 @Test
60 func testCodableShapeMatchesProviderWireValues() throws {
61 let encoder = JSONEncoder()
62 let decoder = JSONDecoder()
63
64 let textData = try encoder.encode(FeatureInputAnswer.text("Deploy"))
65 let selectionsData = try encoder.encode(
66 FeatureInputAnswer.selections(["Server", "Web"])
67 )
68
69 #expect(try decoder.decode(JSONValue.self, from: textData) == .string("Deploy"))
70 #expect(
71 try decoder.decode(JSONValue.self, from: selectionsData)
72 == .array([.string("Server"), .string("Web")])
73 )
74 #expect(try decoder.decode(FeatureInputAnswer.self, from: textData) == .text("Deploy"))
75 #expect(
76 try decoder.decode(FeatureInputAnswer.self, from: selectionsData)
77 == .selections(["Server", "Web"])
78 )
79 }
80
81 @Test
82 func testNativeJSONMappingPreservesStringAndArrayTypes() {
83 #expect(FeatureInputAnswer.text("Deploy").jsonValue == .string("Deploy"))
84 #expect(
85 FeatureInputAnswer.selections(["Server", "Web"]).jsonValue
86 == .array([.string("Server"), .string("Web")])
87 )
88 }
89
90 @Test
91 func testMultiSelectTogglesWithoutFlatteningSelections() {
92 let first = FeatureInputAnswer.selections([])
93 .togglingOption("Server", allowsMultiple: true)
94 let second = first.togglingOption("Web", allowsMultiple: true)
95 let deselected = second.togglingOption("Server", allowsMultiple: true)
96
97 #expect(first == .selections(["Server"]))
98 #expect(second == .selections(["Server", "Web"]))
99 #expect(deselected == .selections(["Web"]))
100 #expect(
101 second.togglingOption("CLI", allowsMultiple: false)
102 == .text("CLI")
103 )
104 }
105
106 @Test
107 func testAnswersNormalizeBeforeSubmission() {
108 #expect(FeatureInputAnswer.text(" ship it ").normalized == .text("ship it"))
109 #expect(
110 FeatureInputAnswer.selections([" Server ", "", "Server", "Web"]).normalized
111 == .selections(["Server", "Web"])
112 )
113 #expect(FeatureInputAnswer.text(" ").normalized == nil)
114 #expect(FeatureInputAnswer.selections([]).normalized == nil)
115 }
116
117 @Test
118 func testMultiSelectCustomTextStaysInTheSelectionArray() {
119 let question = FeatureInputQuestion(
120 id: "surfaces",
121 header: "Surfaces",
122 question: "Where should this ship?",
123 options: [
124 .init(label: "Server", detail: "Backend"),
125 .init(label: "Web", detail: "Browser"),
126 ],
127 allowsMultiple: true
128 )
129 let selected = FeatureInputAnswer.selections(["Server"])
130 let withCustom = FeatureComposerCustomAnswer.replacingText(
131 in: selected,
132 with: "CLI",
133 for: question
134 )
135
136 #expect(withCustom == .selections(["Server", "CLI"]))
137 #expect(FeatureComposerCustomAnswer.text(in: withCustom, for: question) == "CLI")
138 #expect(
139 FeatureComposerCustomAnswer.replacingText(
140 in: withCustom,
141 with: "",
142 for: question
143 ) == .selections(["Server"])
144 )
145 }
146}