Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : MacLustr Identity9// File : sdk/swift/Tests/MacLustrIdentityCoreTests/ErrorTests.swift10// Module : MacLustrIdentityCoreTests11// Repository : https://git.spboucher.ai/maclustr-identity12// Project Website : https://identity.spboucher.ai13// Created : 2026-12-0114// Last Modified : 2026-12-0115//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author : Simon-Pierre Boucher21// Position : Professor of Finance22// Institution : Université du Québec en Outaouais (UQO)23// Department : Département des sciences administratives24// Organization : SP Boucher25//26// Website : https://spboucher.ai27// Git : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General : info@spboucher.ai34// Professional : contact@spboucher.ai35// Direct : simon@spboucher.ai36// Research : research@spboucher.ai37// Development : dev@spboucher.ai38// Support : support@spboucher.ai39// Security/Admin : admin@spboucher.ai40// Notifications : notifications@spboucher.ai41//42// Preferred Contact: admin@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification : Public67// Distribution : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License : None specified (no license file in this repository)81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import XCTest89@testable import MacLustrIdentityCore9091final class ErrorTests: XCTestCase {92 private func error(_ fixture: String, status: Int) async -> IdentityError? {93 let t = MockTransport()94 t.on("GET", "/", fixture: fixture, status: status)95 do { _ = try await IdentityClient(session: testSession, transport: t, retryPolicy: .none).flowState(); return nil } catch { return error as? IdentityError }96 }9798 func testCapturedErrorEnvelopes() async {99 let cases: [(String, Int, String, String, String?)] = [100 ("error_invalid_client_secret", 401, "authentication_error", "invalid_client_secret", nil),101 ("error_missing_client_secret", 401, "authentication_error", "missing_client_secret", nil),102 ("error_sandbox_no_real_documents", 403, "permission_error", "sandbox_no_real_documents", nil),103 ("error_biometric_consent_required", 400, "invalid_request_error", "biometric_consent_required", "biometric_consent"),104 ("error_consent_required", 400, "invalid_request_error", "consent_required", "accepted"),105 ("error_invalid_side", 400, "invalid_request_error", "invalid_side", "side"),106 ("error_unsupported_file_type", 400, "invalid_request_error", "unsupported_file_type", "file"),107 ("error_invalid_session_state", 409, "conflict_error", "invalid_session_state", nil),108 ("error_invalid_challenge", 409, "conflict_error", "invalid_challenge", nil),109 ("error_invalid_frame_count", 400, "invalid_request_error", "invalid_frame_count", "frames"),110 ("error_document_missing", 409, "conflict_error", "document_missing", nil),111 ("error_dispute_exists", 409, "conflict_error", "dispute_exists", nil),112 ("error_invalid_message", 400, "invalid_request_error", "invalid_message", "message"),113 ("error_unknown_scenario", 400, "invalid_request_error", "unknown_scenario", "scenario"),114 ("error_session_not_cancellable", 409, "conflict_error", "session_not_cancellable", nil),115 ]116 for (fixture, status, type, code, param) in cases {117 guard case .api(let e)? = await error(fixture, status: status) else { XCTFail(fixture); continue }118 XCTAssertEqual(e.status, status, fixture)119 XCTAssertEqual(e.type, type, fixture)120 XCTAssertEqual(e.code, code, fixture)121 XCTAssertEqual(e.param, param, fixture)122 XCTAssertTrue(e.requestID?.hasPrefix("req_") == true, fixture)123 XCTAssertFalse(e.message.isEmpty, fixture)124 }125 }126127 func testNonJSONErrorBody() async {128 let t = MockTransport()129 t.on("GET", "/", status: 502, Data("<html>Bad Gateway</html>".utf8))130 do { _ = try await IdentityClient(session: testSession, transport: t, retryPolicy: .none).flowState(); XCTFail() } catch {131 XCTAssertEqual(error as? IdentityError, .invalidResponse(status: 502))132 XCTAssertTrue((error as! IdentityError).isRetryable)133 }134 }135136 func testUnexpectedSuccessBody() async {137 let t = MockTransport()138 t.on("GET", "/", Data("{\"hello\":1}".utf8))139 do { _ = try await IdentityClient(session: testSession, transport: t, retryPolicy: .none).flowState(); XCTFail() } catch {140 XCTAssertEqual(error as? IdentityError, .invalidResponse(status: 200))141 }142 }143144 func testTransportErrors() async {145 let t = MockTransport()146 t.fail("GET", "/", URLError(.notConnectedToInternet))147 do { _ = try await IdentityClient(session: testSession, transport: t, retryPolicy: .none).flowState(); XCTFail() } catch {148 guard case .network = error as? IdentityError else { return XCTFail("\(error)") }149 }150 XCTAssertEqual(IdentityError.wrap(URLError(.cancelled)), .cancelled)151 XCTAssertEqual(IdentityError.wrap(CancellationError()), .cancelled)152 XCTAssertEqual(IdentityError.wrap(IdentityError.cameraDenied), .cameraDenied)153 }154155 func testClassification() {156 let auth = IdentityError.api(APIError(status: 401, type: "authentication_error", code: "invalid_client_secret", message: "x"))157 XCTAssertTrue(auth.isAuthenticationFailure)158 XCTAssertFalse(auth.isRetryable)159 XCTAssertEqual(auth.code, "invalid_client_secret")160 XCTAssertTrue(IdentityError.api(APIError(status: 429, type: "rate_limit_error", code: "rate_limited", message: "x")).isRetryable)161 // Quotas of the session do not clear by waiting a few seconds.162 XCTAssertFalse(IdentityError.api(APIError(status: 429, type: "rate_limit_error", code: "too_many_uploads", message: "x")).isRetryable)163 XCTAssertFalse(IdentityError.api(APIError(status: 429, type: "rate_limit_error", code: "too_many_challenges", message: "x")).isRetryable)164 XCTAssertTrue(IdentityError.api(APIError(status: 503, type: "api_error", code: "service_unavailable", message: "x")).isRetryable)165 XCTAssertFalse(IdentityError.api(APIError(status: 413, type: "invalid_request_error", code: "payload_too_large", message: "x")).isRetryable)166 XCTAssertFalse(IdentityError.api(APIError(status: 403, type: "permission_error", code: "forbidden_network", message: "x")).isRetryable)167 XCTAssertTrue(IdentityError.api(APIError(status: 409, type: "conflict_error", code: "invalid_session_state", message: "x")).isSessionStateConflict)168 XCTAssertFalse(IdentityError.api(APIError(status: 409, type: "conflict_error", code: "invalid_challenge", message: "x")).isSessionStateConflict)169 XCTAssertTrue(IdentityError.network("x").isRetryable)170 XCTAssertFalse(IdentityError.cameraDenied.isRetryable)171 XCTAssertNil(IdentityError.cancelled.code)172 }173174 func testUserFacingMessages() {175 XCTAssertEqual(IdentityString.forError(.network("x")), .errorNetwork)176 XCTAssertEqual(IdentityString.forError(.api(APIError(status: 401, type: "", code: "invalid_client_secret", message: ""))), .errorSessionInvalid)177 XCTAssertEqual(IdentityString.forError(.api(APIError(status: 429, type: "", code: "too_many_uploads", message: ""))), .errorRateLimited)178 XCTAssertEqual(IdentityString.forError(.api(APIError(status: 413, type: "", code: "file_too_large", message: ""))), .errorFileTooLarge)179 XCTAssertEqual(IdentityString.forError(.api(APIError(status: 400, type: "", code: "unsupported_file_type", message: ""))), .errorUnsupportedFile)180 XCTAssertEqual(IdentityString.forDocumentIssue("image_blurry"), .issueBlurry)181 XCTAssertEqual(IdentityString.forDocumentIssue("something_new"), .issueGeneric)182 }183184 func testStringsAreCompleteInBothLanguages() {185 func specifiers(_ s: String) -> [String] {186 var out: [String] = []187 var it = s.makeIterator()188 while let c = it.next() { if c == "%", let n = it.next() { out.append(String(n)) } }189 return out190 }191 for key in IdentityString.allCases {192 let fr = key.text(.fr), en = key.text(.en)193 XCTAssertFalse(fr.isEmpty, "\(key)")194 XCTAssertFalse(en.isEmpty, "\(key)")195 XCTAssertEqual(specifiers(fr), specifiers(en), "\(key)")196 }197 XCTAssertEqual(IdentityString.docAttemptsLeft.text(.fr, 2), "Essais restants : 2")198 XCTAssertEqual(IdentityString.a11yLogo.text(.en, "Acme"), "Acme logo")199 XCTAssertEqual(IdentityLanguage(localeIdentifier: "fr-CA"), .fr)200 XCTAssertEqual(IdentityLanguage(localeIdentifier: "en_US"), .en)201 XCTAssertEqual(IdentityLanguage(localeIdentifier: "de"), .en)202 }203}204