Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/FormattingTests.swift3// Description: Unit tests of the app-side logic that must never distort a figure: exact amount rendering, CSV escaping, review lines built from an intent, error texts.4import Testing5import Foundation6import SPWalletCore7@testable import SPWallet89@Suite struct FormattingTests {10 @Test func amountsAreGroupedNeverRounded() {11 let sep = Locale.current.decimalSeparator ?? ","12 #expect(Fmt.amount("1234567.890000000000000001") == "1\u{202F}234\u{202F}567\(sep)890000000000000001")13 #expect(Fmt.amount("0.1") == "0\(sep)1")14 #expect(Fmt.amount("1000") == "1\u{202F}000")15 #expect(Fmt.amount(nil) == "—")16 // a shortened display says it is shortened, and truncates (never rounds up)17 #expect(Fmt.amount("0.123456789999", maxFraction: 8) == "0\(sep)12345678…")18 #expect(Fmt.amount("-5.5") == "−5\(sep)5")19 }20 @Test func fiatIsAlwaysMarkedAsEstimate() {21 #expect(Fmt.fiat(12.5, .cad).hasPrefix("≈ "))22 #expect(Fmt.fiat(nil, .usd) == "—")23 }24 @Test func addressesAreChunkedWithoutLoss() {25 let a = "0xA2dd4254339C03485579a47f198AD57032dac650"26 #expect(Fmt.chunked(a).replacingOccurrences(of: " ", with: "") == a)27 #expect(Fmt.short(a) == "0xA2dd42…dac650")28 }29 @Test func csvCellsCannotInjectFormulas() {30 #expect(ActivityExport.cell("=HYPERLINK(\"x\")") == "\"'=HYPERLINK(\"\"x\"\")\"")31 #expect(ActivityExport.cell("+1") == "\"'+1\"")32 #expect(ActivityExport.cell("@cmd") == "\"'@cmd\"")33 #expect(ActivityExport.cell("normal, \"quoted\"") == "\"normal, \"\"quoted\"\"\"")34 }35 @Test func onlyFinalizedIsSuccess() {36 for s in ProposalState.allCases where s != .finalized { #expect(!s.isSuccess) }37 #expect(ErrorText.state(.broadcast).contains("non confirmée"))38 #expect(ErrorText.state(.notBroadcast).contains("NON diffusée"))39 #expect(ErrorText.activityState("some_future_state") == "some_future_state")40 }41 @Test func approvalRefusalsSayNothingWasSigned() {42 for e in [ApprovalError.challengeMismatch, .wrongRelyingParty("evil.example"), .userVerificationNotRequired, .unsupported("x"), .inconsistent("amount")] {43 #expect(ErrorText.approval(e).contains("Rien n’a été signé"))44 }45 }46}4748@Suite struct IntentReviewTests {49 static func intent(_ extra: String = "", to: String = "0x903B11a2b7C0c2d5A1cB0B2f2c5C9e3fA682367C") throws -> Intent {50 let json = """51 {"v":1,"proposalId":"11111111-1111-4111-8111-111111111111","keyId":"spw-test-key","network":"base","chainRef":"eip155:8453","asset":"native","symbol":"ETH","decimals":18,52 "amount":"1000000000000000","from":"0xA2dd4254339C03485579a47f198AD57032dac650","to":"\(to)","maxFee":"90000000000000",53 "createdAt":1794950000,"expiresAt":1794950120,"policyVersion":4,"evm":{"nonce":7,"gasLimit":"21000","maxFeePerGas":"1000000000","maxPriorityFeePerGas":"1000000"}\(extra)}54 """55 return try Intent(served: try JSONDecoder().decode(JSONValue.self, from: Data(json.utf8)))56 }57 @Test func reviewShowsFullAddressExactAmountFeeAndTotal() throws {58 let i = try Self.intent()59 let lines = IntentReview.lines(i, network: nil, ownAddresses: [:])60 let text = lines.map { "\($0.label)|\($0.value)" }.joined(separator: "\n")61 #expect(text.contains("0x903B11a2b7C0c2d5A1cB0B2f2c5C9e3fA682367C")) // never abbreviated62 #expect(text.contains("1000000000000000 unités de base"))63 #expect(text.contains("Frais de réseau maximum"))64 let sep = Locale.current.decimalSeparator ?? ","65 #expect(text.contains("Total maximum débité|0\(sep)00109 ETH")) // amount + fee ceiling, exact66 #expect(text.contains("spw-test-key"))67 }68 @Test func swapReviewNamesRouterMinimumAndSpendApproval() throws {69 let i = try Self.intent(#","swap":{"provider":"kyberswap","toAsset":"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913","minOut":"2400000","router":"0x6131B5fae19EA4f9D964eAc0408E4408b66337b5","dataHash":"\#(String(repeating: "ab", count: 32))","approveGas":"60000"}"#, to: "0xA2dd4254339C03485579a47f198AD57032dac650")70 let text = IntentReview.lines(i, network: nil, ownAddresses: [:]).map(\.value).joined(separator: "\n")71 #expect(text.contains("0x6131B5fae19EA4f9D964eAc0408E4408b66337b5"))72 #expect(text.contains("2400000"))73 #expect(text.contains("approbation ERC-20"))74 }75}76