Blockers: - IYmtgTests: replace ScannerViewModel() (no-arg init removed) with CollectionViewModel() in testViewModelFiltering and testPortfolioCalculation - IYmtgTests: fix property access — scannedList, librarySearchText, filteredList, portfolioValue all live on CollectionViewModel, not ScannerViewModel; inject test data after async init settles Major: - ContentView: update .onChange(of:) to two-parameter closure syntax (iOS 17 deprecation) - ModelManager: add missing import FirebaseCore so FirebaseApp.app() resolves explicitly - CollectionViewModel.deleteCard: call CloudEngine.delete(card:) to remove Firebase entry when a card is deleted (prevents data accumulation) - CloudEngine: remove never-called batchUpdatePrices() — full backup already handled by backupAllToFirebase() Minor: - CardDetailView: move to Features/CardDetail/CardDetailView.swift; remove from ContentView.swift - Delete PersistenceActor.swift placeholder (superseded by PersistenceController.swift) - AppConfig.validate(): broaden placeholder-email guard to catch empty strings and common fake domains - ModelManager: document OTA restart requirement in code comment Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
153 lines
7.1 KiB
Swift
153 lines
7.1 KiB
Swift
import XCTest
|
|
@testable import IYmtg_App_iOS // Ensure this matches your Target name
|
|
|
|
final class IYmtgTests: XCTestCase {
|
|
|
|
// MARK: - Card Model Tests
|
|
|
|
func testSavedCardInitialization() {
|
|
let uuid = UUID()
|
|
let metadata = CardMetadata(
|
|
id: uuid,
|
|
name: "Black Lotus",
|
|
setCode: "LEA",
|
|
collectorNumber: "232",
|
|
hasFoilPrinting: false,
|
|
hasSerializedPrinting: false,
|
|
priceScanned: 10000.0,
|
|
rarity: "rare",
|
|
colorIdentity: ["U"],
|
|
isSerialized: false
|
|
)
|
|
|
|
let savedCard = SavedCard(from: metadata, imageName: "test_image.jpg", collection: "My Binder", location: "Page 1")
|
|
|
|
XCTAssertEqual(savedCard.name, "Black Lotus")
|
|
XCTAssertEqual(savedCard.setCode, "LEA")
|
|
XCTAssertEqual(savedCard.collectorNumber, "232")
|
|
XCTAssertEqual(savedCard.scryfallID, "LEA-232")
|
|
XCTAssertEqual(savedCard.collectionName, "My Binder")
|
|
XCTAssertEqual(savedCard.storageLocation, "Page 1")
|
|
XCTAssertEqual(savedCard.currentValuation, 10000.0)
|
|
XCTAssertEqual(savedCard.condition, "Near Mint (NM)") // Default
|
|
XCTAssertEqual(savedCard.foilType, "None") // Default
|
|
}
|
|
|
|
// MARK: - Condition Engine Tests
|
|
|
|
func testConditionGradingLogic() {
|
|
// Mock Damage Observations
|
|
let criticalDamage = DamageObservation(type: "Rips", rect: .zero, confidence: 0.9)
|
|
let minorDamage1 = DamageObservation(type: "EdgeWear", rect: .zero, confidence: 0.8)
|
|
let minorDamage2 = DamageObservation(type: "Scratch", rect: .zero, confidence: 0.8)
|
|
let minorDamage3 = DamageObservation(type: "Dent", rect: .zero, confidence: 0.8)
|
|
|
|
// 1. Excellent (1-2 minor damages)
|
|
XCTAssertEqual(ConditionEngine.overallGrade(damages: [minorDamage1]), "Excellent (EX)")
|
|
XCTAssertEqual(ConditionEngine.overallGrade(damages: [minorDamage1, minorDamage2]), "Excellent (EX)")
|
|
|
|
// 2. Played (>2 minor damages)
|
|
XCTAssertEqual(ConditionEngine.overallGrade(damages: [minorDamage1, minorDamage2, minorDamage3]), "Played (PL)")
|
|
|
|
// 3. Damaged (Any critical damage)
|
|
XCTAssertEqual(ConditionEngine.overallGrade(damages: [criticalDamage]), "Damaged")
|
|
XCTAssertEqual(ConditionEngine.overallGrade(damages: [minorDamage1, criticalDamage]), "Damaged")
|
|
|
|
// 4. Ungraded (Empty list + No Model loaded in Test Env)
|
|
// Note: If model were loaded, this would return "Near Mint (NM)"
|
|
XCTAssertEqual(ConditionEngine.overallGrade(damages: []), "Ungraded")
|
|
}
|
|
|
|
// MARK: - Export Engine Tests
|
|
|
|
func testCSVExportGeneration() {
|
|
let card1 = SavedCard(
|
|
from: CardMetadata(id: UUID(), name: "Card A", setCode: "SET", collectorNumber: "1", hasFoilPrinting: false, hasSerializedPrinting: false, priceScanned: 10.0),
|
|
imageName: "img1.jpg", collection: "Col", location: "Loc"
|
|
)
|
|
var card2 = SavedCard(
|
|
from: CardMetadata(id: UUID(), name: "Card B", setCode: "SET", collectorNumber: "2", hasFoilPrinting: true, hasSerializedPrinting: true, priceScanned: 20.0),
|
|
imageName: "img2.jpg", collection: "Col", location: "Loc"
|
|
)
|
|
card2.isSerialized = true
|
|
|
|
let csv = ExportEngine.generateString(cards: [card1, card2], format: .csv)
|
|
let lines = csv.components(separatedBy: "\n")
|
|
|
|
// Header + 2 cards + empty newline at end
|
|
XCTAssertTrue(lines[0].contains("Count,Name,Set,Number,Condition,Foil,Price,Serialized"))
|
|
|
|
// Check Card 1
|
|
XCTAssertTrue(lines[1].contains("\"Card A\",SET,1"))
|
|
XCTAssertTrue(lines[1].contains(",No"))
|
|
|
|
// Check Card 2
|
|
XCTAssertTrue(lines[2].contains("\"Card B\",SET,2"))
|
|
XCTAssertTrue(lines[2].contains(",Yes"))
|
|
}
|
|
|
|
func testArenaExportGeneration() {
|
|
let card = SavedCard(
|
|
from: CardMetadata(id: UUID(), name: "Lightning Bolt", setCode: "LEA", collectorNumber: "161", hasFoilPrinting: false, hasSerializedPrinting: false),
|
|
imageName: "img.jpg", collection: "Binder", location: "Box"
|
|
)
|
|
|
|
let arena = ExportEngine.generateString(cards: [card], format: .arena)
|
|
XCTAssertEqual(arena, "1 Lightning Bolt (LEA) 161")
|
|
}
|
|
|
|
func testMTGOExportGeneration() {
|
|
let card = SavedCard(
|
|
from: CardMetadata(id: UUID(), name: "Lightning Bolt", setCode: "LEA", collectorNumber: "161", hasFoilPrinting: false, hasSerializedPrinting: false),
|
|
imageName: "img.jpg", collection: "Binder", location: "Box"
|
|
)
|
|
|
|
let mtgo = ExportEngine.generateString(cards: [card], format: .mtgo)
|
|
XCTAssertEqual(mtgo, "1 Lightning Bolt")
|
|
}
|
|
|
|
// MARK: - ViewModel Logic Tests
|
|
// CollectionViewModel owns filtering and portfolio logic; tests target it directly.
|
|
|
|
@MainActor
|
|
func testViewModelFiltering() {
|
|
let vm = CollectionViewModel()
|
|
|
|
let card1 = SavedCard(from: CardMetadata(id: UUID(), name: "Alpha", setCode: "A", collectorNumber: "1", hasFoilPrinting: false, hasSerializedPrinting: false, priceScanned: 100), imageName: "1", collection: "Master Collection", location: "Box")
|
|
let card2 = SavedCard(from: CardMetadata(id: UUID(), name: "Beta", setCode: "B", collectorNumber: "2", hasFoilPrinting: false, hasSerializedPrinting: false, priceScanned: 200), imageName: "2", collection: "Master Collection", location: "Box")
|
|
|
|
// Inject test data after the async SwiftData init load has completed.
|
|
let expectation = XCTestExpectation(description: "Filter updates")
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
|
|
vm.scannedList = [card1, card2]
|
|
vm.librarySearchText = "Alpha"
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
XCTAssertEqual(vm.filteredList.count, 1)
|
|
XCTAssertEqual(vm.filteredList.first?.name, "Alpha")
|
|
expectation.fulfill()
|
|
}
|
|
}
|
|
|
|
wait(for: [expectation], timeout: 2.0)
|
|
}
|
|
|
|
@MainActor
|
|
func testPortfolioCalculation() {
|
|
let vm = CollectionViewModel()
|
|
let card1 = SavedCard(from: CardMetadata(id: UUID(), name: "A", setCode: "A", collectorNumber: "1", hasFoilPrinting: false, hasSerializedPrinting: false, priceScanned: 50.0), imageName: "1", collection: "Master Collection", location: "Box")
|
|
let card2 = SavedCard(from: CardMetadata(id: UUID(), name: "B", setCode: "B", collectorNumber: "2", hasFoilPrinting: false, hasSerializedPrinting: false, priceScanned: 150.0), imageName: "2", collection: "Master Collection", location: "Box")
|
|
|
|
let expectation = XCTestExpectation(description: "Stats update")
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
|
|
vm.scannedList = [card1, card2]
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
XCTAssertEqual(vm.portfolioValue, 200.0)
|
|
expectation.fulfill()
|
|
}
|
|
}
|
|
wait(for: [expectation], timeout: 2.0)
|
|
}
|
|
}
|