|
| 1 | +// |
| 2 | +// TransparentViewSyncOperationResponseTests.swift |
| 3 | +// MindboxTests |
| 4 | +// |
| 5 | + |
| 6 | +import Testing |
| 7 | +import Foundation |
| 8 | +@_spi(Internal) @testable import Mindbox |
| 9 | + |
| 10 | +@Suite("TransparentView.makeSyncOperationResponse") |
| 11 | +struct TransparentViewSyncOperationResponseTests { |
| 12 | + |
| 13 | + private let action = "syncOperation" |
| 14 | + private let requestId = UUID() |
| 15 | + |
| 16 | + // MARK: - HTTP 200 + ValidationError body → .response with raw body (regression: MOBILE-164) |
| 17 | + |
| 18 | + @Test("HTTP 200 ValidationError body becomes .response with raw body string") |
| 19 | + func validationErrorBody_becomesResponseWithRawBody() throws { |
| 20 | + let rawBody = #"{"status":"ValidationError","validationMessages":[{"message":"Invalid email","location":"/customer/email"}]}"# |
| 21 | + let data = try #require(rawBody.data(using: .utf8)) |
| 22 | + |
| 23 | + let outgoing = TransparentView.makeSyncOperationResponse( |
| 24 | + result: .success(data), |
| 25 | + action: action, |
| 26 | + id: requestId |
| 27 | + ) |
| 28 | + |
| 29 | + #expect(outgoing.type == .response) |
| 30 | + #expect(outgoing.action == action) |
| 31 | + #expect(outgoing.id == requestId) |
| 32 | + if case .string(let value) = outgoing.payload { |
| 33 | + #expect(value == rawBody, "Payload must be the raw body, not re-serialized") |
| 34 | + } else { |
| 35 | + Issue.record("Expected .string payload, got \(String(describing: outgoing.payload))") |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // MARK: - HTTP 200 + Success body → .response with raw body |
| 40 | + |
| 41 | + @Test("HTTP 200 Success body becomes .response with raw body string (not re-serialized)") |
| 42 | + func successBody_becomesResponseWithRawBody() throws { |
| 43 | + let rawBody = #"{"status":"Success","customer":{"email":"a@b.c"}}"# |
| 44 | + let data = try #require(rawBody.data(using: .utf8)) |
| 45 | + |
| 46 | + let outgoing = TransparentView.makeSyncOperationResponse( |
| 47 | + result: .success(data), |
| 48 | + action: action, |
| 49 | + id: requestId |
| 50 | + ) |
| 51 | + |
| 52 | + #expect(outgoing.type == .response) |
| 53 | + if case .string(let value) = outgoing.payload { |
| 54 | + #expect(value == rawBody) |
| 55 | + } else { |
| 56 | + Issue.record("Expected .string payload, got \(String(describing: outgoing.payload))") |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // MARK: - HTTP 200 + non-JSON body → .response with raw body string |
| 61 | + |
| 62 | + @Test("HTTP 200 with non-JSON body still becomes .response (JS decides)") |
| 63 | + func nonJSONBody_becomesResponseWithRawBody() throws { |
| 64 | + let rawBody = "plain text body" |
| 65 | + let data = try #require(rawBody.data(using: .utf8)) |
| 66 | + |
| 67 | + let outgoing = TransparentView.makeSyncOperationResponse( |
| 68 | + result: .success(data), |
| 69 | + action: action, |
| 70 | + id: requestId |
| 71 | + ) |
| 72 | + |
| 73 | + #expect(outgoing.type == .response) |
| 74 | + if case .string(let value) = outgoing.payload { |
| 75 | + #expect(value == rawBody) |
| 76 | + } else { |
| 77 | + Issue.record("Expected .string payload") |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // MARK: - HTTP 200 + empty body → .response with empty string |
| 82 | + |
| 83 | + @Test("HTTP 200 with empty body becomes .response with empty string payload") |
| 84 | + func emptyBody_becomesResponseWithEmptyString() { |
| 85 | + let outgoing = TransparentView.makeSyncOperationResponse( |
| 86 | + result: .success(Data()), |
| 87 | + action: action, |
| 88 | + id: requestId |
| 89 | + ) |
| 90 | + |
| 91 | + #expect(outgoing.type == .response) |
| 92 | + if case .string(let value) = outgoing.payload { |
| 93 | + #expect(value == "") |
| 94 | + } else { |
| 95 | + Issue.record("Expected .string payload") |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // MARK: - Non-UTF-8 body → .error with explanatory payload |
| 100 | + |
| 101 | + @Test("Non-UTF-8 body becomes .error with 'Response body is not valid UTF-8'") |
| 102 | + func nonUTF8Body_becomesError() { |
| 103 | + // Bytes that are not valid UTF-8: lone continuation byte 0xC3 + invalid follow-up |
| 104 | + let data = Data([0xC3, 0x28]) |
| 105 | + |
| 106 | + let outgoing = TransparentView.makeSyncOperationResponse( |
| 107 | + result: .success(data), |
| 108 | + action: action, |
| 109 | + id: requestId |
| 110 | + ) |
| 111 | + |
| 112 | + #expect(outgoing.type == .error) |
| 113 | + if case .object(let dict) = outgoing.payload, |
| 114 | + case .string(let errorMessage) = dict["error"] { |
| 115 | + #expect(errorMessage == "Response body is not valid UTF-8") |
| 116 | + } else { |
| 117 | + Issue.record("Expected .object payload with 'error' key, got \(String(describing: outgoing.payload))") |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // MARK: - Failure (.connectionError) → .error with createJSON payload |
| 122 | + |
| 123 | + @Test("Connection failure becomes .error with MindboxError.createJSON payload") |
| 124 | + func connectionError_becomesError() { |
| 125 | + let outgoing = TransparentView.makeSyncOperationResponse( |
| 126 | + result: .failure(.connectionError), |
| 127 | + action: action, |
| 128 | + id: requestId |
| 129 | + ) |
| 130 | + |
| 131 | + #expect(outgoing.type == .error) |
| 132 | + if case .string(let value) = outgoing.payload { |
| 133 | + #expect(value.contains("NetworkError"), "createJSON for connectionError produces a NetworkError envelope") |
| 134 | + #expect(value.contains("Connection error")) |
| 135 | + } else { |
| 136 | + Issue.record("Expected .string payload") |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // MARK: - Failure (.protocolError) → .error with createJSON payload |
| 141 | + |
| 142 | + @Test("Protocol error becomes .error with MindboxError.createJSON payload") |
| 143 | + func protocolError_becomesError() { |
| 144 | + let pe = ProtocolError(status: .protocolError, errorMessage: "Bad", httpStatusCode: 400) |
| 145 | + let outgoing = TransparentView.makeSyncOperationResponse( |
| 146 | + result: .failure(.protocolError(pe)), |
| 147 | + action: action, |
| 148 | + id: requestId |
| 149 | + ) |
| 150 | + |
| 151 | + #expect(outgoing.type == .error) |
| 152 | + if case .string(let value) = outgoing.payload { |
| 153 | + #expect(value.contains("MindboxError")) |
| 154 | + #expect(value.contains("ProtocolError")) |
| 155 | + } else { |
| 156 | + Issue.record("Expected .string payload") |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // MARK: - id and action propagated |
| 161 | + |
| 162 | + @Test("Action and id from the request are preserved on the outgoing message") |
| 163 | + func actionAndIdPreserved() throws { |
| 164 | + let specificAction = "customAction" |
| 165 | + let specificId = UUID() |
| 166 | + let data = try #require("body".data(using: .utf8)) |
| 167 | + |
| 168 | + let outgoing = TransparentView.makeSyncOperationResponse( |
| 169 | + result: .success(data), |
| 170 | + action: specificAction, |
| 171 | + id: specificId |
| 172 | + ) |
| 173 | + |
| 174 | + #expect(outgoing.action == specificAction) |
| 175 | + #expect(outgoing.id == specificId) |
| 176 | + } |
| 177 | +} |
0 commit comments