import XCTest @testable import DomainDig /// Every persisted property on the view model used to read from and write to /// `UserDefaults.standard`, so no test could drive its lifecycle without /// writing into the store the app on the same simulator is using. The store is /// injected now. These cover the seam itself and the lifecycle rules that sit /// on top of it: history pruning, recent-search truncation, tracking, and the /// lookup generation that keeps a stale section result off a new snapshot. @MainActor final class DomainViewModelStoreTests: XCTestCase { private var suiteName = "" private var scratch = UserDefaults.standard /// History lives in a directory now, so the scratch store has two halves. private var historyLocation = HistoryFileStore.Location.default override func setUpWithError() throws { try super.setUpWithError() suiteName = "net.cleberg.DomainDig.tests.\(UUID().uuidString)" scratch = try XCTUnwrap(UserDefaults(suiteName: suiteName)) historyLocation = HistoryFileStore.Location( root: URL(fileURLWithPath: NSTemporaryDirectory()) .appendingPathComponent("DomainDigVMTest-\(UUID().uuidString)", isDirectory: true) ) } override func tearDown() { UserDefaults.standard.removePersistentDomain(forName: suiteName) HistoryFileStore.removeAll(at: historyLocation) super.tearDown() } /// Releasing a main-actor-isolated object aborts the test runner on iOS /// 18.6 — finding E1, a Swift runtime bug in `MarkerItem::create` fixed by /// iOS 26.5. `Scripts/test.sh` runs the floor tier, so a view model that /// went out of scope would kill the whole run there. Holding every instance /// for the life of the process avoids the release and needs no app change. private static var retained: [DomainViewModel] = [] private func makeViewModel() -> DomainViewModel { let viewModel = DomainViewModel(defaults: scratch, historyLocation: historyLocation) Self.retained.append(viewModel) return viewModel } private func makeEntry(domain: String, at timestamp: Date) -> HistoryEntry { HistoryEntry( identity: .init(domain: domain, timestamp: timestamp), inspection: .init( dnsSections: [], sslInfo: nil, httpHeaders: [], reachabilityResults: [], ipGeolocation: nil ), provenance: .init(resolverDisplayName: "Test Resolver", resolverURLString: "") ) } private func makeEntries(_ count: Int) -> [HistoryEntry] { (0..