import Foundation import XCTest @testable import suixinkan /// 验证收款结构、金额精度和图片地址边界,不替代真实支付验证。 final class DroneDockPaymentModelTests: XCTestCase { /// 报价按十进制边界校验,固定两位格式交给后端。 func testPriceValidationAndNormalization() { for (input, expected) in [("0.01", "0.01"), ("99999.99", "99999.99"), (" 88.1 ", "88.10"), ("0099", "99.00"), ("1", "1.00")] { XCTAssertEqual(DroneDockPriceValidator.normalizedPrice(input), expected) XCTAssertNil(DroneDockPriceValidator.validationMessage(for: input)) } for input in ["", "0", "0.001", "1.001", "100000", "-1", "+1", "1.", ".1", "1e2", "1,000", "nan", "9元"] { XCTAssertNil(DroneDockPriceValidator.normalizedPrice(input), input) XCTAssertNotNil(DroneDockPriceValidator.validationMessage(for: input), input) } } /// ID、次数和时间适度兼容整数文本;金额兼容数字而不丢分。 func testNumericCompatibilityPreservesExactDecimalAndIntegers() throws { let projects = try DroneDockPaymentTestFixtures.decode([DroneDockProject].self, DroneDockPaymentTestFixtures.projects) XCTAssertEqual(projects[1].id, 102) XCTAssertEqual(projects[1].shootCount, 5) XCTAssertEqual(projects[1].price, Decimal(string: "199.99")) let payload = DroneDockPaymentTestFixtures.receipts .replacingOccurrences(of: "1793066400", with: "\"1793066400\"") .replacingOccurrences(of: "\"cash_paid\":\"88.00\"", with: "\"cash_paid\":0") let response = try DroneDockPaymentTestFixtures.decode(DroneDockReceiptsResponse.self, payload) XCTAssertEqual(response.records.currentPage, 1) XCTAssertEqual(response.records.items[0].payTime, 1_793_066_400) XCTAssertEqual(response.records.items[0].cashPaid, .zero) XCTAssertEqual(response.records.items[0].cashRefund, Decimal(8)) } /// 缺失、null、非法小数及布尔数值不能被当成可售套餐。 func testInvalidProjectFieldsAreRejected() { for price in ["null", "\"nan\"", "\"99oops\"", "\"1.001\"", "1.001", "true", "0", "100000"] { let payload = "{\"id\":101,\"name\":\"测试套餐\",\"price\":\(price),\"shoot_count\":3}" XCTAssertThrowsError(try DroneDockPaymentTestFixtures.decode(DroneDockProject.self, payload), price) } for payload in [#"{"id":101,"name":"测试套餐","shoot_count":3}"#, #"{"id":0,"name":"测试套餐","price":"99.00","shoot_count":3}"#, #"{"id":101,"name":"测试套餐","price":"99.00","shoot_count":0}"#, #"{"id":101,"name":"测试套餐","price":"99.00","shoot_count":1.5}"#] { XCTAssertThrowsError(try DroneDockPaymentTestFixtures.decode(DroneDockProject.self, payload)) } } /// 只允许完整 HTTP(S) 图片地址,拒绝脚本、自定义协议及带凭证地址。 func testPaymentURLValidation() throws { let original = "https://example.com/mini_qr/project-101.png" for url in ["http://example.com/pay.png", original, "https://example.com/image?id=101"] { let payload = DroneDockPaymentTestFixtures.code().replacingOccurrences(of: original, with: url) XCTAssertEqual(try DroneDockPaymentTestFixtures.decode(DroneDockPayCode.self, payload).payURL, url) } for url in ["", "javascript:alert(1)", "file:///tmp/pay", "/pay", "https://", "https://user:pass@example.com/pay", "https://example.com/a b"] { let payload = DroneDockPaymentTestFixtures.code().replacingOccurrences(of: original, with: url) XCTAssertThrowsError(try DroneDockPaymentTestFixtures.decode(DroneDockPayCode.self, payload), url) } } /// Laravel 列表缺失或汇总金额未知时报错,不产生伪造空记录与零金额。 func testReceiptsRequireRealPaginationAndMoneyFields() throws { let response = try DroneDockPaymentTestFixtures.decode(DroneDockReceiptsResponse.self, DroneDockPaymentTestFixtures.receipts) XCTAssertEqual(response.records.total, 3) XCTAssertEqual(response.records.items.count, 1) XCTAssertEqual(response.summary.cashPaid, Decimal(188)) for payload in [#"{"records":{"current_page":1,"per_page":20,"total":0,"last_page":1},"summary":{}}"#, DroneDockPaymentTestFixtures.receipts.replacingOccurrences(of: "\"cash_paid\":\"188.00\"", with: "\"cash_paid\":null"), DroneDockPaymentTestFixtures.receipts.replacingOccurrences(of: "\"points_net\":90", with: "\"points_net\":1.5")] { XCTAssertThrowsError(try DroneDockPaymentTestFixtures.decode(DroneDockReceiptsResponse.self, payload)) } } }