taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

OrderManagerTests.swift (5461B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2026 Bohdan, Volodymyr Potuzhnyi
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 import XCTest
     18 @testable import TalerPOS
     19 
     20 @MainActor
     21 final class OrderManagerTests: XCTestCase {
     22 
     23     private var orderManager: OrderManager!
     24 
     25     private let posConfig = PosConfig(
     26         categories: [
     27             Category(id: 1, name: "one"),
     28             Category(id: 2, name: "two"),
     29         ],
     30         products: [
     31             ConfigProduct(
     32                 description: "foo",
     33                 price: Amount(currency: "KUDOS", value: 1, fraction: 0),
     34                 categories: [1]
     35             ),
     36             ConfigProduct(
     37                 description: "bar",
     38                 price: Amount(currency: "KUDOS", value: 1, fraction: 50000000),
     39                 categories: [2]
     40             ),
     41         ]
     42     )
     43 
     44     override func setUp() {
     45         super.setUp()
     46         orderManager = OrderManager()
     47     }
     48 
     49     func testConfigErrorOnMissingCategories() {
     50         let config = PosConfig(categories: [], products: posConfig.products)
     51         let result = orderManager.onConfigurationReceived(
     52             posConfig: config, currency: "KUDOS", currencySpec: nil
     53         )
     54         XCTAssertNotNil(result)
     55     }
     56 
     57     func testCurrencyMismatchProductIsAcceptedButUnavailable() {
     58         let products = [
     59             ConfigProduct(
     60                 description: "foo",
     61                 price: Amount(currency: "WRONGCUR", value: 1, fraction: 0),
     62                 categories: [1]
     63             )
     64         ]
     65         let config = PosConfig(categories: posConfig.categories, products: products)
     66         let result = orderManager.onConfigurationReceived(
     67             posConfig: config, currency: "KUDOS", currencySpec: nil
     68         )
     69 
     70         XCTAssertNil(result)
     71         let product = orderManager.products.first { $0.description == "foo" }
     72         XCTAssertNotNil(product)
     73         XCTAssertEqual("WRONGCUR", product?.price.currency)
     74         XCTAssertTrue(product?.currencyMismatch == true)
     75         XCTAssertFalse(product?.availableToSell == true)
     76     }
     77 
     78     func testValidConfigGetsAccepted() {
     79         let result = orderManager.onConfigurationReceived(
     80             posConfig: posConfig, currency: "KUDOS", currencySpec: nil
     81         )
     82         XCTAssertNil(result)
     83     }
     84 
     85     func testAllObjectsIsSelectedByDefaultAndShownFirst() {
     86         let result = orderManager.onConfigurationReceived(
     87             posConfig: posConfig, currency: "KUDOS", currencySpec: nil
     88         )
     89         XCTAssertNil(result)
     90 
     91         let categories = orderManager.categories
     92         let products = orderManager.products
     93 
     94         XCTAssertFalse(categories.isEmpty)
     95         XCTAssertFalse(products.isEmpty)
     96         XCTAssertTrue(categories[0].selected)
     97         XCTAssertFalse(categories[1].selected)
     98         XCTAssertEqual(posConfig.products.count, products.count)
     99     }
    100 
    101     func testUncategorizedIsAppendedAtTheEndWhenNeeded() {
    102         let uncategorizedProduct = ConfigProduct(
    103             description: "baz",
    104             price: Amount(currency: "KUDOS", value: 2, fraction: 0),
    105             categories: []
    106         )
    107         let config = PosConfig(
    108             categories: posConfig.categories,
    109             products: posConfig.products + [uncategorizedProduct]
    110         )
    111 
    112         let result = orderManager.onConfigurationReceived(
    113             posConfig: config, currency: "KUDOS", currencySpec: nil
    114         )
    115         XCTAssertNil(result)
    116 
    117         let categories = orderManager.categories
    118         let uncategorized = categories.last!
    119         XCTAssertEqual(uncategorized.id, -2)
    120 
    121         orderManager.selectCategory(uncategorized)
    122         let uncatProducts = orderManager.products
    123         XCTAssertEqual(1, uncatProducts.count)
    124         XCTAssertEqual("baz", uncatProducts[0].description)
    125     }
    126 
    127     func testLegacyDefaultCategoryIsHiddenAndMappedToUncategorized() {
    128         let defaultCategory = Category(id: 3, name: "Default")
    129         let defaultProduct = ConfigProduct(
    130             description: "legacy",
    131             price: Amount(currency: "KUDOS", value: 3, fraction: 0),
    132             categories: [3]
    133         )
    134         let config = PosConfig(
    135             categories: posConfig.categories + [defaultCategory],
    136             products: posConfig.products + [defaultProduct]
    137         )
    138 
    139         let result = orderManager.onConfigurationReceived(
    140             posConfig: config, currency: "KUDOS", currencySpec: nil
    141         )
    142         XCTAssertNil(result)
    143 
    144         let categories = orderManager.categories
    145         XCTAssertFalse(categories.contains(where: { $0.name == "Default" }))
    146 
    147         let uncategorized = categories.last!
    148         XCTAssertEqual(uncategorized.id, -2)
    149 
    150         orderManager.selectCategory(uncategorized)
    151         let uncatProducts = orderManager.products
    152         XCTAssertEqual(1, uncatProducts.count)
    153         XCTAssertEqual("legacy", uncatProducts[0].description)
    154     }
    155 }