crossmate

A collaborative crossword app for iOS
Log | Files | Refs | LICENSE

AuthorIdentityTests.swift (1710B)


      1 import CloudKit
      2 import Foundation
      3 import Testing
      4 
      5 @testable import Crossmate
      6 
      7 @Suite("AuthorIdentity")
      8 @MainActor
      9 struct AuthorIdentityTests {
     10 
     11     private func makeIsolatedStorage() -> UserDefaults {
     12         UserDefaults(suiteName: "test-authoridentity-\(UUID().uuidString)")!
     13     }
     14 
     15     @Test("Initial state returns nil before any refresh")
     16     func initialStateIsNil() {
     17         let identity = AuthorIdentity(storage: makeIsolatedStorage())
     18         #expect(identity.currentID == nil)
     19     }
     20 
     21     @Test("Testing initializer seeds currentID")
     22     func testingInitializerSeedsValue() {
     23         let identity = AuthorIdentity(testing: "_abc")
     24         #expect(identity.currentID == "_abc")
     25     }
     26 
     27     @Test("refresh with unavailable account leaves currentID nil")
     28     func refreshFailurePreservesNil() async {
     29         let identity = AuthorIdentity(storage: makeIsolatedStorage())
     30         // Using the default CKContainer without a signed-in account will fail.
     31         // The contract is: on failure, currentID stays nil (or unchanged).
     32         let containerWithNoAccount = CKContainer(identifier: "iCloud.net.inqk.crossmate.v3")
     33         await identity.refresh(using: containerWithNoAccount)
     34         // On a simulator/CI with no account, this should stay nil.
     35         // We can only assert it doesn't crash; value depends on sim state.
     36         _ = identity.currentID
     37     }
     38 
     39     @Test("Persists currentID across instances sharing storage")
     40     func currentIDPersistsAcrossInstances() {
     41         let storage = makeIsolatedStorage()
     42         storage.set("_persisted", forKey: "AuthorIdentity.currentID")
     43         let identity = AuthorIdentity(storage: storage)
     44         #expect(identity.currentID == "_persisted")
     45     }
     46 }