import Foundation import Testing import EquipoShareCore @testable import EquipoKit @MainActor @Test func shareSelectionTracksTheActiveHostLifetime() throws { let store = MemorySecretStore(), accounts = DeviceAccounts(store: MemorySecretStore()) // Use one store for the actual boundary; the separate instance above has // a foreign lifetime and must never authorize a write. let host = DeviceAccounts(store: store) var account = DeviceAccount.new(origin: URL(string: "https://team.example")!, label: "Phone") account.address = "a@example.net"; account.teamName = "Team" account.credential = DeviceCredential(id: "d", token: "fixture", audience: "a", instanceId: "i", expiresAt: Date().addingTimeInterval(300)) try host.commit(.save(account), lifetime: host.lifetimes.current) #expect(throws: CancellationError.self) { try host.commit(.selectForSharing(account), lifetime: accounts.lifetimes.current) } #expect(store.read(ShareSelection.key) == nil) try host.commit(.selectForSharing(account), lifetime: host.lifetimes.current) let selection = try JSONDecoder().decode(ShareSelection.self, from: #require(store.read(ShareSelection.key))) #expect(selection.person == account.address) #expect(selection.origin == account.origin) try host.commit(.clearShareSelection(account), lifetime: host.lifetimes.current) #expect(store.read(ShareSelection.key) == nil) try host.commit(.selectForSharing(account), lifetime: host.lifetimes.current) try host.commit(.remove(account), lifetime: host.lifetimes.current) #expect(store.read(ShareSelection.key) == nil) }