diff --git a/App/OPNMainAccountActions.swift b/App/OPNMainAccountActions.swift new file mode 100644 index 000000000..c0f088984 --- /dev/null +++ b/App/OPNMainAccountActions.swift @@ -0,0 +1,144 @@ +import Foundation +import SwiftData + +@MainActor +final class OPNMainAccountActions { + static let shared = OPNMainAccountActions() + + struct Target { + let identifier: PersistentIdentifier + let accountID: String + let email: String + let userID: String + let providerID: String + + init(account: LoginAccount) { + identifier = account.persistentModelID + accountID = account.stableAccountID + email = account.email + userID = account.userId + providerID = account.providerIdpId + } + } + + enum Request { + case addAccount + case reauthenticate(Target) + case signOut(Target) + case forget(Target) + } + + private weak var login: LoginViewModel? + private var pendingRequests: [Request] = [] + private let confirmation: OPNConfirmationPresentation + private let handoff: OPNMainWindowAccountHandoff + + private enum ActionError: Error { + case missingContext + } + + init(confirmation: OPNConfirmationPresentation = .shared, handoff: OPNMainWindowAccountHandoff = .shared) { + self.confirmation = confirmation + self.handoff = handoff + } + + @discardableResult + func bind(login: LoginViewModel) -> Task { + self.login = login + let queuedRequests = pendingRequests + pendingRequests.removeAll() + return Task { + for request in queuedRequests { + await perform(request, login: login) + } + } + } + + func unbind(login: LoginViewModel) { + guard self.login === login else { return } + self.login = nil + } + + @discardableResult + func request(_ request: Request) -> Task? { + guard let login else { + pendingRequests.append(request) + return nil + } + return Task { await perform(request, login: login) } + } + + private func perform(_ request: Request, login: LoginViewModel) async { + do { + try reload(login: login) + } catch { + showFailure("Saved accounts could not be loaded. Try again.") + return + } + switch request { + case .addAccount: + login.beginAddAccount() + case .reauthenticate(let target): + guard let account = resolve(target, login: login) else { return } + login.dismissAccountChooser() + login.beginReauthentication(for: account) + case .signOut(let target): + guard let account = resolve(target, login: login) else { return } + login.validationMessage = "" + await login.signOutAccount(account) + finishMutation(login: login) + case .forget(let target): + guard let account = resolve(target, login: login) else { return } + login.validationMessage = "" + login.forgetAccount(account) + finishMutation(login: login) + } + } + + private func reload(login: LoginViewModel) throws { + guard let modelContext = login.modelContext else { throw ActionError.missingContext } + login.update( + modelContext: modelContext, + accounts: try modelContext.fetch(FetchDescriptor()), + sessions: try modelContext.fetch(FetchDescriptor()), + devices: try modelContext.fetch(FetchDescriptor()) + ) + } + + private func resolve(_ target: Target, login: LoginViewModel) -> LoginAccount? { + guard let account = login.accounts.first(where: { + $0.persistentModelID == target.identifier && $0.stableAccountID == target.accountID + && $0.email == target.email && $0.userId == target.userID && $0.providerIdpId == target.providerID + }) else { + showFailure("This saved account changed or was removed. Choose the account again before continuing.") + return nil + } + return account + } + + private func finishMutation(login: LoginViewModel) { + guard login.validationMessage.isEmpty else { + showFailure(login.validationMessage) + return + } + endHandoff() + } + + private func showFailure(_ message: String) { + confirmation.present( + .init( + eyebrow: "ACCOUNT", + title: "Account action unavailable", + message: message, + actions: [OPNConfirmationAction("CLOSE") { [weak confirmation] in confirmation?.dismiss() }] + ), + onDismiss: { [weak self] in self?.endHandoff() } + ) + handoff.begin(from: nil) + } + + private func endHandoff() { + guard login?.signInRequest == nil else { return } + handoff.end() + } +} diff --git a/App/OPNReportIssueCommands.swift b/App/OPNReportIssueCommands.swift new file mode 100644 index 000000000..84743d24f --- /dev/null +++ b/App/OPNReportIssueCommands.swift @@ -0,0 +1,15 @@ +import SwiftUI + +struct OPNReportIssueCommands: Commands { + @FocusedValue(\.opnReportIssuePresentation) private var presentation + + var body: some Commands { + CommandGroup(replacing: .help) { + Button { + (presentation ?? .shared).present(context: .appEnvironment()) + } label: { + Label("Report an Issue…", systemImage: "exclamationmark.bubble") + } + } + } +} diff --git a/DESIGN.md b/DESIGN.md index 1da99c71f..56f42e437 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -1321,7 +1321,7 @@ The screen renders at 100 % interface scale like every other transient splash. ### Confirmation Modal (`OPNConfirmationModal`) The app-shell replacement for the native `.confirmationDialog` and `.alert`, which render rounded -macOS system chrome. Raised through the `opnConfirmation` modifier and rendered once at the app root +macOS system chrome. Raised through the `opnConfirmation` modifier and rendered once at its window root by `OPNConfirmationOverlay` — above the update prompt, below the splash — so it covers the whole surface even when the page that raised it lives inside a scroll view. Full-cover Scrim behind a centered panel following the modal spec (Panel background, 1px Stroke Regular, 2px accent top bar, @@ -1336,10 +1336,16 @@ actions trailing. Actions are `OPNConfirmationAction`s in visual order: `.cancel centered panel springs up from 0.96 scale with a fade. Reduce Motion collapses both to a plain cross-fade. +Detached catalogs own a separate confirmation and report presenter through `DetachedSessionModalHost`. +The host injects these references into its descendants and the focused scene's Help command; it never +observes the main window's shared requests. Confirmation modifiers dismiss only their own request, so +an unrelated page disappearing cannot clear another page's dialog. Main-window update prompts keep +their existing host and ordering. + ### Report an Issue Modal (`OPNReportIssueModal`) -The two-channel support form, raised through `OPNReportIssuePresentation.shared.present(context:)` -and rendered once at the app root by `OPNReportIssueOverlay` — above the update prompt, below the +The two-channel support form, raised through the window's `OPNReportIssuePresentation.present(context:)` +(the shared presenter in the main window) and rendered once at that window root by `OPNReportIssueOverlay` — above the update prompt, below the confirmation modal — so it covers the whole surface even when the page that raised it lives inside a scroll view. It reuses the confirmation modal's shell exactly (Panel background, 1px Stroke Regular, 2px accent top bar, modal shadow #000000 @ 0.58 radius 28 y 20, App Bar header with a 10pt bold diff --git a/GFN/Jarvis/JarvisAuthService.swift b/GFN/Jarvis/JarvisAuthService.swift index 2ddee6aa9..dd5904ef7 100644 --- a/GFN/Jarvis/JarvisAuthService.swift +++ b/GFN/Jarvis/JarvisAuthService.swift @@ -137,14 +137,25 @@ public actor JarvisAuthService { } guard isSessionRefreshable else { throw JarvisAuthError.noRefreshMechanism } if !session.clientToken.isEmpty { + let initiating = session let body = JarvisOAuthRequestFactory.clientTokenGrantBody(clientToken: session.clientToken, userId: session.userId, configuration: configuration) - let refreshed = merge(saved: session, refreshed: try await requestSession(body: body, operation: .getSessionToken)) - let enriched = try await ensureClientToken(refreshed) - setSessionInMemory(enriched) - try await persistSessionIfAutomatic(enriched) - telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "success", "grant_type": "client_token"]) - span.finish(success: true) - return enriched + do { + let refreshed = merge(saved: session, refreshed: try await requestSession(body: body, operation: .getSessionToken)) + let enriched = try await ensureClientToken(refreshed) + setSessionInMemory(enriched) + try await persistSessionIfAutomatic(enriched) + telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "success", "grant_type": "client_token"]) + span.finish(success: true) + return enriched + } catch let error as JarvisAuthError where error.category == .authorization && !session.refreshToken.isEmpty { + guard session.userId == initiating.userId, session.refreshToken == initiating.refreshToken else { + throw CancellationError() + } + telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "fallback", "grant_type": "client_token"]) + session.clientToken = "" + session.clientTokenExpiry = 0 + session.clientTokenExpiryLength = 0 + } } let refreshed = try await refreshWithOAuthToken() telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "success", "grant_type": "refresh_token"]) @@ -523,7 +534,7 @@ extension JarvisAuthService { } guard response.statusCode == 200 else { - let error = JarvisAuthError.httpStatus(response.statusCode) + let error = JarvisAuthError.rejectedGrant(statusCode: response.statusCode, body: data) ?? JarvisAuthError.httpStatus(response.statusCode) telemetry.recordError(error, operation: operation, attributes: ["status_code": String(response.statusCode), "category": error.category.rawValue]) throw error } diff --git a/GFN/Jarvis/JarvisService.swift b/GFN/Jarvis/JarvisService.swift index ab4bcf772..23f60adb8 100644 --- a/GFN/Jarvis/JarvisService.swift +++ b/GFN/Jarvis/JarvisService.swift @@ -466,6 +466,13 @@ public enum JarvisAuthError: LocalizedError, Equatable, Sendable { } } + static func rejectedGrant(statusCode: Int, body: Data) -> JarvisAuthError? { + guard statusCode == 400, + let json = try? JSONSerialization.jsonObject(with: body) as? [String: Any], + json["error"] as? String == "invalid_grant" else { return nil } + return .oauthError("invalid_grant") + } + public static func transportFailure(_ error: Error) -> JarvisAuthError { let nsError = error as NSError let category: JarvisAuthFailureCategory diff --git a/Model/Stream/ControllerBindingOutputLedger.swift b/Model/Stream/ControllerBindingOutputLedger.swift index 77cedaf3e..ed2753458 100644 --- a/Model/Stream/ControllerBindingOutputLedger.swift +++ b/Model/Stream/ControllerBindingOutputLedger.swift @@ -24,6 +24,22 @@ struct ControllerBindingOutputLedger { } } + mutating func releaseHeldOutputs(timestamp: MediaTimestamp) -> [UserInputEvent] { + var releaseEvents: [UserInputEvent] = [] + for keyCode in keys.keys.sorted() { + guard let ownerDeviceID = keys[keyCode]?.keys.sorted(by: { $0.rawValue < $1.rawValue }).first else { continue } + releaseEvents.append(.keyboard(KeyboardEvent(deviceID: ownerDeviceID, keyCode: keyCode, scanCode: keyCode, + modifiers: [], isPressed: false, timestamp: timestamp))) + } + for mouseButton in mouseButtons.keys.sorted(by: { $0.rawValue < $1.rawValue }) { + guard let ownerDeviceID = mouseButtons[mouseButton]?.sorted(by: { $0.rawValue < $1.rawValue }).first else { continue } + releaseEvents.append(.mouse(.button(deviceID: ownerDeviceID, button: mouseButton, isPressed: false, timestamp: timestamp))) + } + keys.removeAll() + mouseButtons.removeAll() + return releaseEvents + } + private mutating func keyboardEvents(for event: KeyboardEvent) -> [UserInputEvent] { let previous = keys let previousModifiers = modifiers diff --git a/Model/Stream/ControllerMappingFocusPolicy.swift b/Model/Stream/ControllerMappingFocusPolicy.swift index 5edbaea90..41a40425c 100644 --- a/Model/Stream/ControllerMappingFocusPolicy.swift +++ b/Model/Stream/ControllerMappingFocusPolicy.swift @@ -1,6 +1,9 @@ enum ControllerMappingFocusPolicy { + /// Mappings follow the frontmost stream, except that couch co-op lets a pad keep playing its own + /// stream while another window has the focus. The overlay and remote-input gates still apply. static func allowsMappings(appIsActive: Bool, windowIsKey: Bool, - remoteInputEnabled: Bool, overlayCapturesInput: Bool) -> Bool { - appIsActive && windowIsKey && remoteInputEnabled && !overlayCapturesInput + remoteInputEnabled: Bool, overlayCapturesInput: Bool, + allowsBackgroundPads: Bool = false) -> Bool { + ((appIsActive && windowIsKey) || allowsBackgroundPads) && remoteInputEnabled && !overlayCapturesInput } } diff --git a/Model/Stream/StreamingTypes.swift b/Model/Stream/StreamingTypes.swift index c90678db4..49ffce290 100644 --- a/Model/Stream/StreamingTypes.swift +++ b/Model/Stream/StreamingTypes.swift @@ -49,11 +49,18 @@ extension StreamLaunchConfiguration { /// What the catalog said about HDR for the store edition this launch picked. Client-side only: /// it describes the launch to this app, not to the vendor. static let hdrCatalogSupportKey = "hdrCatalogSupport" + /// The stable id of the account that owns the session, so per-account choices made before the + /// stream existed (which controllers it plays with) find their way to it. + static let owningAccountIDKey = "owningAccountID" var owningAccountDisplayName: String { (metadata[Self.owningAccountDisplayNameKey] ?? "").trimmingCharacters(in: .whitespacesAndNewlines) } + var owningAccountID: String { + (metadata[Self.owningAccountIDKey] ?? "").trimmingCharacters(in: .whitespacesAndNewlines) + } + /// The catalog's HDR answer as the stream profile should read it. A store-launcher session /// starts the game through the store's own launcher, so the catalog's per-edition answer does /// not describe what will run and is not applied. @@ -70,9 +77,10 @@ extension StreamLaunchConfiguration { /// by, whose game it is, and which game's mappings it uses. The id has to come from the session /// that owns the launch - the stream surface registers its commands under the configuration's id, /// so a different one here would leave every targeted control reaching nothing. - func snapshottingSession(id: UUID, ownerDisplayName: String, mappingGameIdentity: String) -> StreamLaunchConfiguration { + func snapshottingSession(id: UUID, ownerDisplayName: String, ownerAccountID: String, mappingGameIdentity: String) -> StreamLaunchConfiguration { var metadata = metadata metadata[Self.owningAccountDisplayNameKey] = ownerDisplayName + metadata[Self.owningAccountIDKey] = ownerAccountID metadata[Self.mappingGameIdentityKey] = mappingGameIdentity return StreamLaunchConfiguration( id: id, diff --git a/OPN/Core/OPNDlssPreferences.swift b/OPN/Core/OPNDlssPreferences.swift index b15666f74..631dea565 100644 --- a/OPN/Core/OPNDlssPreferences.swift +++ b/OPN/Core/OPNDlssPreferences.swift @@ -15,10 +15,12 @@ extension OPNStreamPreferences { storage.synchronize() } - /// The entitlements the launch path filters against, remembered so a session request does not - /// need a subscription fetch on its own path. + /// The active browsing account's presentation mirror. Launches carry their own snapshot. public static func loadDlssEntitlements() -> DlssEntitlements { - guard let dictionary = storage.object(forKey: k.dlssEntitlements) as? [String: Any] else { return .none } + dlssEntitlements(from: storage.object(forKey: k.dlssEntitlements) as? [String: Any] ?? [:]) + } + + static func dlssEntitlements(from dictionary: [String: Any]) -> DlssEntitlements { return DlssEntitlements( superResolution: capability(from: dictionary, key: "superResolution"), frameGeneration: capability(from: dictionary, key: "frameGeneration"), @@ -28,14 +30,17 @@ extension OPNStreamPreferences { } public static func saveDlssEntitlements(_ entitlements: DlssEntitlements) { - let dictionary: [String: Any] = [ + storage.set(dlssEntitlementsDictionary(entitlements), forKey: k.dlssEntitlements) + storage.synchronize() + } + + static func dlssEntitlementsDictionary(_ entitlements: DlssEntitlements) -> [String: Any] { + [ "superResolution": capabilityDictionary(entitlements.superResolution), "frameGeneration": capabilityDictionary(entitlements.frameGeneration), "rayReconstruction": capabilityDictionary(entitlements.rayReconstruction), "frameGenerationMultipliers": entitlements.frameGenerationMultipliers, ] - storage.set(dictionary, forKey: k.dlssEntitlements) - storage.synchronize() } /// Signing out drops the stored entitlements, so a later launch cannot ask for an override the diff --git a/OPN/Core/OPNLaunchEntitlements.swift b/OPN/Core/OPNLaunchEntitlements.swift new file mode 100644 index 000000000..2ccfbcd60 --- /dev/null +++ b/OPN/Core/OPNLaunchEntitlements.swift @@ -0,0 +1,80 @@ +import Foundation + +struct OPNLaunchEntitlements: Equatable, Sendable { + static let none = OPNLaunchEntitlements() + static let settingsKey = "accountEntitlements" + static let metadataKey = "launchEntitlements" + static let providerMetadataKey = "providerStreamingBaseURL" + + let dlss: DlssEntitlements + let audioChannelCount: Int + let isInGameSettingsPersistenceEntitled: Bool + + init(dlss: DlssEntitlements = .none, audioChannelCount: Int = 2, isInGameSettingsPersistenceEntitled: Bool = false) { + self.dlss = dlss + self.audioChannelCount = min(max(audioChannelCount, 2), 8) + self.isInGameSettingsPersistenceEntitled = isInGameSettingsPersistenceEntitled + } + + init(subscription: OPNSubscriptionInfo) { + self.init( + dlss: subscription.dlssEntitlements, + audioChannelCount: subscription.entitledAudioChannelCount, + isInGameSettingsPersistenceEntitled: subscription.isInGameSettingsPersistenceEntitled + ) + } + + init(settings: [String: Any]) { + self.init(stored: settings[Self.settingsKey] as? [String: Any] ?? [:]) + } + + init(metadata: [String: String]) { + let stored = metadata[Self.metadataKey]?.data(using: .utf8).flatMap { + (try? JSONSerialization.jsonObject(with: $0)) as? [String: Any] + } + self.init(stored: stored ?? [:]) + } + + private init(stored: [String: Any]) { + self.init( + dlss: OPNStreamPreferences.dlssEntitlements(from: stored["dlss"] as? [String: Any] ?? [:]), + audioChannelCount: stored["audioChannelCount"] as? Int ?? 2, + isInGameSettingsPersistenceEntitled: stored["isInGameSettingsPersistenceEntitled"] as? Bool ?? false + ) + } + + var storedDictionary: [String: Any] { + [ + "dlss": OPNStreamPreferences.dlssEntitlementsDictionary(dlss), + "audioChannelCount": audioChannelCount, + "isInGameSettingsPersistenceEntitled": isInGameSettingsPersistenceEntitled, + ] + } + + var metadataValue: String { + guard let encodedEntitlements = try? JSONSerialization.data(withJSONObject: storedDictionary, options: .sortedKeys), + let entitlementsJSON = String(data: encodedEntitlements, encoding: .utf8) else { return "{}" } + return entitlementsJSON + } +} + +extension StreamLaunchConfiguration { + var launchEntitlements: OPNLaunchEntitlements { OPNLaunchEntitlements(metadata: metadata) } + + func snapshottingEntitlements(_ entitlements: OPNLaunchEntitlements, providerStreamingBaseURL: String) -> StreamLaunchConfiguration { + var metadata = metadata + metadata[OPNLaunchEntitlements.metadataKey] = entitlements.metadataValue + metadata[OPNLaunchEntitlements.providerMetadataKey] = providerStreamingBaseURL + return StreamLaunchConfiguration( + id: id, + title: title, + applicationID: applicationID, + accessToken: accessToken, + accountLinked: accountLinked, + selectedStore: selectedStore, + resumeSessionID: resumeSessionID, + resumeServer: resumeServer, + metadata: metadata + ) + } +} diff --git a/OPN/Core/OPNStreamPreferences+Regions.swift b/OPN/Core/OPNStreamPreferences+Regions.swift index f9f17c921..e888a907f 100644 --- a/OPN/Core/OPNStreamPreferences+Regions.swift +++ b/OPN/Core/OPNStreamPreferences+Regions.swift @@ -9,10 +9,10 @@ extension OPNStreamPreferences { normalizedHTTPSBaseUrlOrEmpty(storage.string(forKey: k.selectedRegionUrl) ?? "") } - public static func loadSelectedStreamingBaseUrl() -> String { + public static func loadSelectedStreamingBaseUrl(providerStreamingBaseURL: String? = nil) -> String { let selected = loadSelectedRegionUrl() if !selected.isEmpty { return normalizedBaseUrl(selected) } - return defaultStreamingBaseUrl + return normalizedBaseUrl(providerStreamingBaseURL ?? defaultStreamingBaseUrl) } public static func loadSelectedRegionUrl(forGame appId: String) -> String { @@ -20,12 +20,12 @@ extension OPNStreamPreferences { return normalizedHTTPSBaseUrlOrEmpty(string(dictionary[k.selectedRegionUrl], "")) } - public static func loadSelectedStreamingBaseUrl(forGame appId: String) -> String { + public static func loadSelectedStreamingBaseUrl(forGame appId: String, providerStreamingBaseURL: String? = nil) -> String { if let dictionary = gameProfileDictionary(for: appId), bool(dictionary[k.gameProfileEnabled], false) { let selected = string(dictionary[k.selectedRegionUrl], "") if !selected.isEmpty { return normalizedBaseUrl(selected) } } - return loadSelectedStreamingBaseUrl() + return loadSelectedStreamingBaseUrl(providerStreamingBaseURL: providerStreamingBaseURL) } public static func saveSelectedRegionUrl(_ url: String) { diff --git a/OPN/Core/OPNStreamPreferences+Settings.swift b/OPN/Core/OPNStreamPreferences+Settings.swift index 419413b19..126caaa40 100644 --- a/OPN/Core/OPNStreamPreferences+Settings.swift +++ b/OPN/Core/OPNStreamPreferences+Settings.swift @@ -51,7 +51,7 @@ extension OPNStreamPreferences { public static func runNetworkPreflight(token: String, providerStreamingBaseUrl: String, requestedMaxBitrateMbps: Int, completion: @escaping @MainActor @Sendable (OPNStreamNetworkPreflightResult) -> Void) { var initial = OPNStreamNetworkPreflightResult() - initial.streamingBaseUrl = loadSelectedStreamingBaseUrl() + initial.streamingBaseUrl = loadSelectedStreamingBaseUrl(providerStreamingBaseURL: providerStreamingBaseUrl) initial.networkType = currentNetworkType() initial.recommendedMaxBitrateMbps = max(1, requestedMaxBitrateMbps) let initialResult = initial diff --git a/OPN/GameServices/CatalogLaunchPrefetch.swift b/OPN/GameServices/CatalogLaunchPrefetch.swift index cda6eae4a..e183b7caa 100644 --- a/OPN/GameServices/CatalogLaunchPrefetch.swift +++ b/OPN/GameServices/CatalogLaunchPrefetch.swift @@ -89,7 +89,7 @@ final class CatalogLaunchPrefetch { case failed } - let gameService = OPNGameService.shared + let gameService = OPNGameService() let imageCache = CatalogImageCache.shared private(set) var accountIdentifier = "" @@ -103,13 +103,13 @@ final class CatalogLaunchPrefetch { private var didPrefetchHeroImages = false private var didPrefetchRailImages = false - private init() {} + init() {} var hasHomePanels: Bool { !(panels[.main] ?? []).isEmpty } var isFetching: Bool { isActiveState(panelStates[.marquee]) || isActiveState(panelStates[.main]) } - func start(accountIdentifier: String, accessToken: String, idToken: String) { + func start(accountIdentifier: String, accessToken: String, idToken: String, idpId: String = "") { guard !retention.isHandoverFinished else { return } guard (panelStates[.marquee] ?? .idle) == .idle, (panelStates[.main] ?? .idle) == .idle else { return } guard !accountIdentifier.isEmpty, !accessToken.isEmpty || !idToken.isEmpty else { return } @@ -120,6 +120,17 @@ final class CatalogLaunchPrefetch { retention.recordDeliveriesStarted(deliveryKeys) startedAt = ContinuousClock.now StartupReadiness.shared.noteProgress() + guard !idpId.isEmpty else { + fetchLaunchData(accountIdentifier: accountIdentifier, accessToken: accessToken, idToken: idToken) + return + } + gameService.fetchProviderInfo(idpId: idpId) { [weak self] _, _, _, _ in + self?.fetchLaunchData(accountIdentifier: accountIdentifier, accessToken: accessToken, idToken: idToken) + } + } + + private func fetchLaunchData(accountIdentifier: String, accessToken: String, idToken: String) { + guard accountIdentifier == self.accountIdentifier, !retention.isHandoverFinished else { return } // Also prewarms the vpcId lookup, which every catalog query waits on. gameService.configureCatalogSession(accessToken: accessToken, idToken: idToken, userId: accountIdentifier) OPNLog.info(.catalog, "Launch panel prefetch started") @@ -170,7 +181,8 @@ final class CatalogLaunchPrefetch { /// delivered snapshot to every later caller, so without this a launch-time favorites list was /// re-adopted by every reload for the rest of the session: a favorite removed in this session /// came straight back on the next refresh, which made the control look dead. - func invalidate(_ kind: GameListKind) { + func invalidate(_ kind: GameListKind, accountIdentifier: String? = nil) { + guard accountIdentifier == nil || accountIdentifier == self.accountIdentifier else { return } gameLists[kind] = nil gameListStates[kind] = .idle } @@ -211,16 +223,18 @@ final class CatalogLaunchPrefetch { /// Tells this prefetch the home catalog has run every launch attach point, so the graphs may go /// as soon as the last delivery lands. - func recordLaunchResultsAdopted() { + func recordLaunchResultsAdopted(accountIdentifier: String? = nil) { + guard accountIdentifier == nil || accountIdentifier == self.accountIdentifier else { return } retention.recordLaunchResultsAdopted() releaseRetainedGraphsWhenReady() } /// Drops everything so a later explicit refresh goes to the network instead /// of adopting launch-time results. - func invalidate() { + func invalidate(accountIdentifier: String? = nil) { + guard accountIdentifier == nil || accountIdentifier == self.accountIdentifier else { return } dropRetainedGraphs() - accountIdentifier = "" + self.accountIdentifier = "" panelStates = [:] gameListStates = [:] startedAt = nil diff --git a/OPN/GameServices/GameLaunchBridging.swift b/OPN/GameServices/GameLaunchBridging.swift index 15727b1c2..f30d88076 100644 --- a/OPN/GameServices/GameLaunchBridging.swift +++ b/OPN/GameServices/GameLaunchBridging.swift @@ -13,6 +13,7 @@ protocol GameLaunchServiceConfiguring { func setAccountLinkingToken(_ token: String) func setUserId(_ id: String) func setVpcId(_ id: String) + func providerStreamingBaseURL() -> String func resolveLaunchAppId(game: OPNGameInfo, variantIndex: Int, completion: @escaping OPNLaunchAppIdCallback) } diff --git a/OPN/GameServices/OPNGameLaunchBridge.swift b/OPN/GameServices/OPNGameLaunchBridge.swift index 51b97719d..a9036a02f 100644 --- a/OPN/GameServices/OPNGameLaunchBridge.swift +++ b/OPN/GameServices/OPNGameLaunchBridge.swift @@ -99,7 +99,7 @@ public final class OPNGameLaunchBridge { selectedStore: selectedStore, metadata: launchMetadata.merging(Self.hdrCatalogSupportMetadata(for: selectedVariant)) { current, _ in current } ) - let streamingBaseUrl = OPNStreamPreferences.loadSelectedStreamingBaseUrl(forGame: appId) + let streamingBaseUrl = OPNStreamPreferences.loadSelectedStreamingBaseUrl(forGame: appId, providerStreamingBaseURL: gameService.providerStreamingBaseURL()) OPNActiveSessionService.fetchActiveSessions(accessToken: token, streamingBaseUrl: streamingBaseUrl, deviceId: deviceId) { [weak self] ok, sessions, _ in guard let self else { return } if ok { diff --git a/OPN/GameServices/OPNGameService+Panels.swift b/OPN/GameServices/OPNGameService+Panels.swift index d0c5084a3..83374347c 100644 --- a/OPN/GameServices/OPNGameService+Panels.swift +++ b/OPN/GameServices/OPNGameService+Panels.swift @@ -28,7 +28,7 @@ extension OPNGameService { // once (and the vpcId correction below asks again), so identical requests join // one fetch instead of each paying for a 660KB response. A late joiner is // replayed whatever the fetch has delivered so far. - let groupKey = "\(cacheKind)|\(accountIdentifier)|\(locale)" + let groupKey = "\(cacheKind)|\(accountIdentifier)|\(providerBaseUrl)|\(accessToken.hashValue)|\(locale)" Self.panelFetchLock.lock() if let existing = Self.pendingPanelFetches[groupKey] { Self.panelFetchLock.unlock() diff --git a/OPN/GameServices/OPNGameService+Provider.swift b/OPN/GameServices/OPNGameService+Provider.swift index d5668de1e..0cc73c927 100644 --- a/OPN/GameServices/OPNGameService+Provider.swift +++ b/OPN/GameServices/OPNGameService+Provider.swift @@ -2,37 +2,49 @@ import AppKit import Foundation extension OPNGameService { - // The service-urls document is global reference data, but it is looked up by both - // the login provider picker and the catalog session setup, so a single launch - // requested it twice (measured 1280ms + 1028ms). Memoized per idpId with in-flight - // coalescing; the streaming base URL side effect is replayed on every hit. func fetchProviderInfo(idpId: String, completion: @escaping OPNProviderInfoCallback) { + let requestID = UUID() + let requestedAccessToken = accessToken + let requestedUserID = userId + providerContextLock.withLock { providerRequestID = requestID } + let deliverProviderInfo: OPNProviderInfoCallback = { [weak self] isSuccessful, providerInfo, providerEndpoint, errorMessage in + guard let self else { return } + let isCurrentProviderRequest = self.providerContextLock.withLock { + guard self.providerRequestID == requestID, self.accessToken == requestedAccessToken, + self.userId == requestedUserID else { return false } + self.storedProviderStreamingBaseURL = providerEndpoint.streamingServiceUrl + return true + } + guard isCurrentProviderRequest else { + completion(false, providerInfo, providerEndpoint, "Provider lookup superseded") + return + } + completion(isSuccessful, providerInfo, providerEndpoint, errorMessage) + } Self.referenceDataLock.lock() if let entry = Self.providerInfoCache[idpId], Date().timeIntervalSince(entry.timestamp) <= Self.referenceDataFreshSeconds { Self.referenceDataLock.unlock() let (info, endpoint) = entry.value - providerStreamingBaseUrl = endpoint.streamingServiceUrl - dispatchProviderInfo(completion, true, info, endpoint, "") + dispatchProviderInfo(deliverProviderInfo, true, info, endpoint, "") return } if Self.pendingProviderInfoCallbacks[idpId] != nil { - Self.pendingProviderInfoCallbacks[idpId]?.append(completion) + Self.pendingProviderInfoCallbacks[idpId]?.append(deliverProviderInfo) Self.referenceDataLock.unlock() return } - Self.pendingProviderInfoCallbacks[idpId] = [completion] + Self.pendingProviderInfoCallbacks[idpId] = [deliverProviderInfo] Self.referenceDataLock.unlock() - fetchProviderInfoUncached(idpId: idpId) { [weak self] success, info, endpoint, error in + fetchProviderInfoUncached(idpId: idpId) { success, info, endpoint, error in Self.referenceDataLock.lock() if success { Self.providerInfoCache[idpId] = ReferenceDataEntry(value: (info, endpoint), timestamp: Date()) } let callbacks = Self.pendingProviderInfoCallbacks.removeValue(forKey: idpId) ?? [] Self.referenceDataLock.unlock() - guard let self else { return } for callback in callbacks { - self.dispatchProviderInfo(callback, success, info, endpoint, error) + callback(success, info, endpoint, error) } } } @@ -47,14 +59,12 @@ extension OPNGameService { request.setValue("application/json", forHTTPHeaderField: "Accept") request.setValue(Self.gfnUserAgent, forHTTPHeaderField: "User-Agent") let networkStart = OPNNetworkLog.start(request, operation: "provider.serviceUrls") - OPNSessionProxySessionProvider.shared.controlPlaneURLSession().dataTask(with: request) { [weak self] data, response, error in + OPNSessionProxySessionProvider.shared.controlPlaneURLSession().dataTask(with: request) { [self] data, response, error in OPNNetworkLog.finish(operation: "provider.serviceUrls", startedAt: networkStart, data: data, response: response, error: error) - guard let self else { return } let statusCode = (response as? HTTPURLResponse)?.statusCode ?? 0 guard error == nil, let data, statusCode == 200 else { var endpoint = OPNGameProviderEndpoint() endpoint.streamingServiceUrl = Self.defaultStreamingBaseUrl - self.providerStreamingBaseUrl = endpoint.streamingServiceUrl self.dispatchProviderInfo(completion, false, OPNGameProviderInfo(), endpoint, error?.localizedDescription ?? "Provider info request failed") return } @@ -63,9 +73,8 @@ extension OPNGameService { let info = self.parseGameProviderInfo(json) var endpoint = self.selectGameProviderEndpoint(info, idpId: idpId) if endpoint.streamingServiceUrl.isEmpty { endpoint.streamingServiceUrl = Self.defaultStreamingBaseUrl } - self.providerStreamingBaseUrl = self.normalizeStreamingBaseUrl(endpoint.streamingServiceUrl) - if self.providerStreamingBaseUrl.isEmpty { self.providerStreamingBaseUrl = Self.defaultStreamingBaseUrl } - endpoint.streamingServiceUrl = self.providerStreamingBaseUrl + let normalizedStreamingBaseURL = self.normalizeStreamingBaseUrl(endpoint.streamingServiceUrl) + endpoint.streamingServiceUrl = normalizedStreamingBaseURL.isEmpty ? Self.defaultStreamingBaseUrl : normalizedStreamingBaseURL self.dispatchProviderInfo(completion, true, info, endpoint, "") }.resume() } diff --git a/OPN/GameServices/OPNGameService.swift b/OPN/GameServices/OPNGameService.swift index 309a7439f..9dfc85b3e 100644 --- a/OPN/GameServices/OPNGameService.swift +++ b/OPN/GameServices/OPNGameService.swift @@ -101,7 +101,13 @@ final class OPNGameService: @unchecked Sendable { var graphqlURL = "https://games.geforce.com/graphql" var persistedGraphqlURL = LCARS.productionAppsGraphQLURLString var streamingBaseUrl = "" - var providerStreamingBaseUrl = OPNGameService.defaultStreamingBaseUrl + let providerContextLock = NSLock() + var providerRequestID = UUID() + var storedProviderStreamingBaseURL = OPNGameService.defaultStreamingBaseUrl + var providerStreamingBaseUrl: String { + get { providerContextLock.withLock { storedProviderStreamingBaseURL } } + set { providerContextLock.withLock { storedProviderStreamingBaseURL = newValue } } + } init(sessionManager: any StreamSessionManaging = OPNSessionManager.shared, dataCache: any CatalogDataCaching = OPNGameDataCache.shared) { self.sessionManager = sessionManager diff --git a/OPN/GameServices/OPNSessionManager+SessionInfo.swift b/OPN/GameServices/OPNSessionManager+SessionInfo.swift index 125abbd02..ec8f64ba7 100644 --- a/OPN/GameServices/OPNSessionManager+SessionInfo.swift +++ b/OPN/GameServices/OPNSessionManager+SessionInfo.swift @@ -56,7 +56,7 @@ extension OPNSessionManager { /// so a stream that looks like it forgot the setting can be told apart from an unsent one. func logInGameSettingsPersistenceRequest(_ settings: [String: Any]) { let toggle = OPNStreamPreferences.loadPersistInGameSettings() - let entitled = OPNStreamPreferences.loadEntitledInGameSettingsPersistence() + let entitled = OPNLaunchEntitlements(settings: settings).isInGameSettingsPersistenceEntitled let sent = bool(settings["enablePersistingInGameSettings"]) OPNDiagnostics.logInfoMessage(OPNDiagnostics.formattedLogMessage(level: "info", area: "SessionManager", message: "In-game settings persistence sent=\(sent ? "on" : "off") toggle=\(toggle ? "on" : "off") membership=\(entitled ? "entitled" : "not-entitled")")) } diff --git a/OPN/GameServices/OPNSessionManager.swift b/OPN/GameServices/OPNSessionManager.swift index 9c2580c14..ecabb3a43 100644 --- a/OPN/GameServices/OPNSessionManager.swift +++ b/OPN/GameServices/OPNSessionManager.swift @@ -104,6 +104,10 @@ final class OPNSessionManager: NSObject, @unchecked Sendable { hdrEnabled: Bool, deviceId: String, selectedStore: String) -> [String: Any] { + let entitlements = OPNLaunchEntitlements(settings: effectiveSettings) + var effectiveSettings = effectiveSettings + effectiveSettings["audioChannelCount"] = min(requestedAudioChannelCount(effectiveSettings), entitlements.audioChannelCount) + effectiveSettings["enablePersistingInGameSettings"] = bool(effectiveSettings["enablePersistingInGameSettings"]) && entitlements.isInGameSettingsPersistenceEntitled let timezoneOffset = -TimeZone.current.secondsFromGMT() * 1000 let metadata = [ ["key": "SubSessionId", "value": UUID().uuidString.lowercased()], diff --git a/OPN/GameServices/OPNSessionPayloads.swift b/OPN/GameServices/OPNSessionPayloads.swift index d179c2a33..28b2c6862 100644 --- a/OPN/GameServices/OPNSessionPayloads.swift +++ b/OPN/GameServices/OPNSessionPayloads.swift @@ -63,13 +63,17 @@ final class OPNPollClaimSessionContext: @unchecked Sendable { // exist elsewhere in the module. extension OPNSessionManager { func settingsByApplyingCloudVariables(_ settings: [String: Any], capabilities: OPNStreamDeviceCapabilities) -> [String: Any] { + let entitlements = OPNLaunchEntitlements(settings: settings) + var cloudVariables = OPNStreamPreferences.loadCachedCloudVariables() + cloudVariables.entitledAudioChannelCount = entitlements.audioChannelCount let resolved = StreamSettingsResolver.resolve( profile: streamProfile(from: settings), capabilities: streamDeviceCapabilities(from: capabilities), - cloudVariables: streamCloudVariables(from: OPNStreamPreferences.loadCachedCloudVariables()) + cloudVariables: streamCloudVariables(from: cloudVariables) ) var result = settings result.merge(resolved.dictionary(gameLanguage: string(settings["gameLanguage"]), accountLinked: bool(settings["accountLinked"], fallback: true), selectedStore: string(settings["selectedStore"]))) { _, new in new } + result["enablePersistingInGameSettings"] = bool(result["enablePersistingInGameSettings"]) && entitlements.isInGameSettingsPersistenceEntitled return result } @@ -150,11 +154,9 @@ extension OPNSessionManager { if let prefilterModel = chosenPrefilterModel(int(settings["prefilterModel"])) { features["prefilterModel"] = prefilterModel } - // Cloud DLSS overrides ride the same object, filtered against the stored entitlements at the - // single point where the payload is built: an unentitled override can never reach the wire. let dlssPreference = settings["dlssOverrides"] as? [String: Any] let dlssOverrides = (dlssPreference.map(DlssOverrides.init(stored:)) ?? OPNStreamPreferences.loadDlssOverrides()) - .applyingAccess(OPNStreamPreferences.loadDlssEntitlements()) + .applyingAccess(OPNLaunchEntitlements(settings: settings).dlss) features["dlssOverrides"] = dlssOverrides.sessionRequestDictionary return features } diff --git a/OPN/GameServices/OPNStreamSessionCoordinator+Allocation.swift b/OPN/GameServices/OPNStreamSessionCoordinator+Allocation.swift index b678c28e2..913989a48 100644 --- a/OPN/GameServices/OPNStreamSessionCoordinator+Allocation.swift +++ b/OPN/GameServices/OPNStreamSessionCoordinator+Allocation.swift @@ -170,7 +170,7 @@ extension OPNStreamSessionCoordinator { let cloudVariables = await fetchCloudVariables(configuration: configuration) var settings = settingsByApplyingCloudVariables(baseSettings, variables: cloudVariables) let requestedMaxBitrateMbps = int(settings["maxBitrateMbps"]) - let preflight = await runNetworkPreflight(token: configuration.accessToken, requestedMaxBitrateMbps: requestedMaxBitrateMbps) + let preflight = await runNetworkPreflight(token: configuration.accessToken, requestedMaxBitrateMbps: requestedMaxBitrateMbps, providerStreamingBaseURL: configuration.metadata[OPNLaunchEntitlements.providerMetadataKey] ?? OPNGameService.defaultStreamingBaseUrl) if preflight.serverReportedWarning || !preflight.continueRecommended || !preflight.warningMessage.isEmpty { OPNTelemetryRecorder.record(OPNTelemetryEvent(name: .networkTest, parameters: [ "status": "warning", @@ -185,7 +185,7 @@ extension OPNStreamSessionCoordinator { if preflight.recommendedMaxBitrateMbps > 0 { settings["maxBitrateMbps"] = min(int(settings["maxBitrateMbps"]), preflight.recommendedMaxBitrateMbps) } - let streamingBaseUrl = preflight.streamingBaseUrl.isEmpty ? OPNStreamPreferences.loadSelectedStreamingBaseUrl(forGame: configuration.applicationID) : preflight.streamingBaseUrl + let streamingBaseUrl = preflight.streamingBaseUrl.isEmpty ? OPNStreamPreferences.loadSelectedStreamingBaseUrl(forGame: configuration.applicationID, providerStreamingBaseURL: configuration.metadata[OPNLaunchEntitlements.providerMetadataKey]) : preflight.streamingBaseUrl return PreparedStreamLaunch(settings: settings, streamingBaseUrl: streamingBaseUrl) } @@ -197,11 +197,11 @@ extension OPNStreamSessionCoordinator { } } - func runNetworkPreflight(token: String, requestedMaxBitrateMbps: Int) async -> OPNStreamNetworkPreflightResult { + func runNetworkPreflight(token: String, requestedMaxBitrateMbps: Int, providerStreamingBaseURL: String = OPNGameService.defaultStreamingBaseUrl) async -> OPNStreamNetworkPreflightResult { await withCheckedContinuation { continuation in OPNStreamPreferences.runNetworkPreflight( token: token, - providerStreamingBaseUrl: OPNGameService.shared.providerStreamingBaseURL(), + providerStreamingBaseUrl: providerStreamingBaseURL, requestedMaxBitrateMbps: requestedMaxBitrateMbps, completion: { preflight in continuation.resume(returning: preflight) } ) @@ -210,6 +210,9 @@ extension OPNStreamSessionCoordinator { func settingsByApplyingCloudVariables(_ settings: [String: Any], variables: OPNStreamCloudVariables) -> [String: Any] { let capabilities = OPNStreamPreferences.loadDeviceCapabilities() + let entitlements = OPNLaunchEntitlements(settings: settings) + var variables = variables + variables.entitledAudioChannelCount = entitlements.audioChannelCount let resolved = StreamSettingsResolver.resolve( profile: streamProfile(from: settings), capabilities: streamDeviceCapabilities(from: capabilities), @@ -217,6 +220,7 @@ extension OPNStreamSessionCoordinator { ) var result = settings result.merge(resolved.dictionary(gameLanguage: string(settings["gameLanguage"], fallback: OPNLocale.currentGFNLocale()), accountLinked: bool(settings["accountLinked"], fallback: true), selectedStore: string(settings["selectedStore"]))) { _, new in new } + result["enablePersistingInGameSettings"] = bool(result["enablePersistingInGameSettings"]) && entitlements.isInGameSettingsPersistenceEntitled return result } } diff --git a/OPN/GameServices/OPNStreamSessionCoordinator.swift b/OPN/GameServices/OPNStreamSessionCoordinator.swift index 383d46d40..24e0306c8 100644 --- a/OPN/GameServices/OPNStreamSessionCoordinator.swift +++ b/OPN/GameServices/OPNStreamSessionCoordinator.swift @@ -311,13 +311,17 @@ public final class OPNStreamSessionCoordinator: NativeNVSTSessionProvider, Strea func makeSettings(configuration: StreamLaunchConfiguration) -> [String: Any] { let capabilities = OPNStreamPreferences.loadDeviceCapabilities() let profile = OPNStreamPreferences.launchProfile(forGame: configuration.applicationID, capabilities: capabilities) + let entitlements = configuration.launchEntitlements + var cloudVariables = OPNStreamPreferences.loadCachedCloudVariables() + cloudVariables.entitledAudioChannelCount = entitlements.audioChannelCount let resolved = StreamSettingsResolver.resolve( profile: streamProfile(from: profile, catalogHdrSupport: configuration.hdrCatalogSupport(isStoreLauncherSession: profile.steamBigPictureMode)), capabilities: streamDeviceCapabilities(from: capabilities), - cloudVariables: streamCloudVariables(from: OPNStreamPreferences.loadCachedCloudVariables()) + cloudVariables: streamCloudVariables(from: cloudVariables) ) var settings = resolved.dictionary(gameLanguage: OPNLocale.currentGFNLocale(), accountLinked: configuration.accountLinked, selectedStore: configuration.selectedStore) - settings["enablePersistingInGameSettings"] = bool(settings["enablePersistingInGameSettings"]) && OPNStreamPreferences.loadEntitledInGameSettingsPersistence() + settings[OPNLaunchEntitlements.settingsKey] = entitlements.storedDictionary + settings["enablePersistingInGameSettings"] = bool(settings["enablePersistingInGameSettings"]) && entitlements.isInGameSettingsPersistenceEntitled // The saved DLSS preference rides the launch settings flow unfiltered; the payload builder // is what applies membership access. settings["dlssOverrides"] = OPNStreamPreferences.loadDlssOverrides().storedDictionary diff --git a/OPN/Services/OPNAppRelaunch.swift b/OPN/Services/OPNAppRelaunch.swift new file mode 100644 index 000000000..337e77872 --- /dev/null +++ b/OPN/Services/OPNAppRelaunch.swift @@ -0,0 +1,30 @@ +// Restarts the app, for a setting that is only read once at launch. + +import AppKit + +@MainActor +enum OPNAppRelaunch { + /// How long the helper waits for this process to exit before giving up, so a quit the reader + /// cancels does not leave a relaunch pending forever. + static let waitSeconds = 60 + + /// The shell line that outlives this process: it waits for `pid` to go, then opens the bundle. + /// The bundle path travels as an argument rather than inside the script text. + static func helperArguments(pid: Int32) -> [String] { + let script = "i=0; while kill -0 \(pid) 2>/dev/null && [ $i -lt \(waitSeconds * 5) ]; do sleep 0.2; i=$((i+1)); done; /usr/bin/open \"$0\"" + return ["-c", script, Bundle.main.bundlePath] + } + + static func relaunch() { + let helper = Process() + helper.executableURL = URL(fileURLWithPath: "/bin/sh") + helper.arguments = helperArguments(pid: ProcessInfo.processInfo.processIdentifier) + do { + try helper.run() + } catch { + OPNLog.warning(.app, "Relaunch helper could not start: \(error.localizedDescription)") + return + } + NSApp.terminate(nil) + } +} diff --git a/OPN/Services/OPNLabs.swift b/OPN/Services/OPNLabs.swift index 88fab9037..7117c4ef0 100644 --- a/OPN/Services/OPNLabs.swift +++ b/OPN/Services/OPNLabs.swift @@ -15,6 +15,9 @@ struct OPNLabsFlag: Identifiable, Equatable, Sendable { let summary: String /// The release it went on trial in, so a flag nobody promoted is visible as stale. let since: String + /// Whether the flag is only read as the app starts, so switching it asks for a restart rather + /// than trying to change what is already running. + var requiresRestart = false var storageKey: String { "OpenNOW.Labs.\(id)" } } @@ -30,14 +33,27 @@ enum OPNLabs { since: "0.15" ) + /// Couch co-op on one Mac: extra accounts stream in their own windows, each window gets its own + /// controllers, and the windows tile themselves. Off by default because it changes how the stream + /// windows are placed and lets pads keep playing while another window has the keyboard focus. + static let couchCoop = OPNLabsFlag( + id: "couchCoop", + title: "Couch Co-op", + summary: "Adds Stream > Detach New Session, which opens a window for a second or third account. Each window takes its own controllers, plays while another window is in front, and the stream windows tile across your screens.", + since: "0.15", + requiresRestart: true + ) + /// Every trial in flight. Empty is the normal state, and the Settings rail drops the Labs /// destination while it is. - static let flags: [OPNLabsFlag] = [clipboardCapture] + static let flags: [OPNLabsFlag] = [clipboardCapture, couchCoop] /// Whether the in-stream clipboard history is switched on. Read at every point the feature is /// offered — the HUD panel, the Settings card, the binding and the capture path itself. static var isClipboardCaptureEnabled: Bool { isEnabled(clipboardCapture) } + static var isCouchCoopEnabled: Bool { isEnabled(couchCoop) } + static var hasFlags: Bool { !flags.isEmpty } static func isEnabled(_ flag: OPNLabsFlag) -> Bool { diff --git a/OPN/Services/OPNMainWindowCloseGuard.swift b/OPN/Services/OPNMainWindowCloseGuard.swift index cd9537ee2..7e2acb9a6 100644 --- a/OPN/Services/OPNMainWindowCloseGuard.swift +++ b/OPN/Services/OPNMainWindowCloseGuard.swift @@ -22,6 +22,14 @@ enum OPNMainWindow { } } + /// Puts the window in front of everything else, for a request that starts in another window and + /// ends in this one. + static func bringForward() { + guard let window = existing() else { return } + window.makeKeyAndOrderFront(nil) + NSApp.activate(ignoringOtherApps: true) + } + /// Whether a surface outside the window has to build one before handing it work. /// /// The close button hides the window rather than tearing the scene down, so a hidden or minimized @@ -67,6 +75,101 @@ enum OPNMainWindow { } } +@MainActor +final class OPNMainWindowAccountHandoff { + static let shared = OPNMainWindowAccountHandoff() + + private weak var window: NSWindow? + private weak var sourceWindow: NSWindow? + private var originalLevel: NSWindow.Level? + private var isRequested = false + private var observerTokens: [NSObjectProtocol] = [] + private var closeObserver: NSObjectProtocol? + private let mainWindow: () -> NSWindow? + private let coveringLevels: () -> [NSWindow.Level] + + init( + mainWindow: @escaping () -> NSWindow? = { OPNMainWindow.existing() }, + coveringLevels: @escaping () -> [NSWindow.Level] = { + NSApp.windows.compactMap { window in + guard let stream = window as? OPNStreamWindow, + stream.layoutState.isBorderless, stream.isVisible else { return nil } + return stream.level + } + } + ) { + self.mainWindow = mainWindow + self.coveringLevels = coveringLevels + } + + func begin(from sourceWindow: NSWindow?) { + isRequested = true + if let sourceWindow { self.sourceWindow = sourceWindow } + installObservers() + guard let window = mainWindow() else { return } + attach(window) + if window.isMiniaturized { window.deminiaturize(nil) } + window.makeKeyAndOrderFront(nil) + NSApp.activate(ignoringOtherApps: true) + } + + func attach(_ window: NSWindow) { + guard isRequested else { return } + bindWindow(window) + applyLevel() + } + + func end() { + let returnWindow = sourceWindow + let isReturningFocus = window?.isKeyWindow == true && NSApp.isActive + isRequested = false + restoreLevel() + window = nil + sourceWindow = nil + for token in observerTokens { NotificationCenter.default.removeObserver(token) } + observerTokens.removeAll() + if let closeObserver { NotificationCenter.default.removeObserver(closeObserver) } + closeObserver = nil + if isReturningFocus, let returnWindow, returnWindow.isVisible { returnWindow.makeKeyAndOrderFront(nil) } + } + + private func bindWindow(_ window: NSWindow) { + guard self.window !== window else { return } + restoreLevel() + if let closeObserver { NotificationCenter.default.removeObserver(closeObserver) } + self.window = window + originalLevel = window.level + closeObserver = NotificationCenter.default.addObserver( + forName: NSWindow.willCloseNotification, object: window, queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.end() } + } + } + + private func applyLevel() { + guard let window, let originalLevel else { return } + let coveringLevel = coveringLevels().map(\.rawValue).max() ?? NSWindow.Level.normal.rawValue + let sourceLevel = sourceWindow?.level.rawValue ?? NSWindow.Level.normal.rawValue + let obstructionLevel = max(coveringLevel, sourceLevel) + let requiredLevel = obstructionLevel > NSWindow.Level.normal.rawValue ? obstructionLevel + 1 : originalLevel.rawValue + window.level = NSWindow.Level(rawValue: max(originalLevel.rawValue, requiredLevel)) + } + + private func restoreLevel() { + if let window, let originalLevel { window.level = originalLevel } + originalLevel = nil + } + + private func installObservers() { + guard observerTokens.isEmpty else { return } + observerTokens.append(NotificationCenter.default.addObserver( + forName: OPNStreamBorderless.coveringStateDidChange, object: nil, queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.applyLevel() } + }) + } +} + /// Enforces `OPNWindowClosePreferences.behavior` on the main window's close button. /// /// The quit choice is enforced here rather than left to AppKit's last-window hook: a status item @@ -104,6 +207,7 @@ enum OPNMainWindowCloseGuard { /// notification, and the Dock icon decision is `OPNDockIconController`'s: with nothing on screen /// and the menu-bar-only choice in force, the app leaves the Dock. static var hideMainWindow: @MainActor () -> Void = { + OPNMainWindowAccountHandoff.shared.end() OPNMainWindow.existing()?.orderOut(nil) OPNDockIconController.apply() } diff --git a/OPN/Stream/NativeGamepadMonitor+Mapping.swift b/OPN/Stream/NativeGamepadMonitor+Mapping.swift index 49d26217a..6175620cb 100644 --- a/OPN/Stream/NativeGamepadMonitor+Mapping.swift +++ b/OPN/Stream/NativeGamepadMonitor+Mapping.swift @@ -1,6 +1,14 @@ import Foundation extension NativeGamepadMonitor { + func dispatchPendingInput() { + let (commands, events) = pollingQueue.sync { + (pollState.takePendingCommands(), pollState.takePendingEvents()) + } + for command in commands { onStreamCommand?(command) } + for event in events { emitInputEvent(event) } + } + /// While the Guide/Steam button is held, `includePointerMotion` is false — trackpad /// and stick pointer motion is suppressed here so `SteamControllerLocalCursorInjector` /// can drive the real macOS cursor from the right pad instead. Buttons/triggers/sticks @@ -34,7 +42,7 @@ extension NativeGamepadMonitor { } func emitInputEvent(_ event: UserInputEvent) { - if !mappingsEnabled || !pollingAllowed { + if !mappingsEnabled || !pollingAllowed || !isKeyboardMouseInputEnabled { switch event { case .keyboard(let key) where key.isPressed: return case .mouse(.button(_, _, true, _)), .mouse(.moved), .mouse(.wheel), .mouse(.horizontalWheel): return @@ -44,6 +52,14 @@ extension NativeGamepadMonitor { for output in bindingOutputLedger.events(for: event) { onInputEvent?(output) } } + func setKeyboardMouseInputEnabled(_ isEnabled: Bool) { + guard isKeyboardMouseInputEnabled != isEnabled else { return } + isKeyboardMouseInputEnabled = isEnabled + guard !isEnabled else { return } + let timestamp = MediaTimestamp(nanoseconds: DispatchTime.now().uptimeNanoseconds) + for event in bindingOutputLedger.releaseHeldOutputs(timestamp: timestamp) { onInputEvent?(event) } + } + func setMappingsEnabled(_ enabled: Bool) { guard mappingsEnabled != enabled else { return } mappingsEnabled = enabled diff --git a/OPN/Stream/NativeGamepadMonitor.swift b/OPN/Stream/NativeGamepadMonitor.swift index 205509245..4d077b34b 100644 --- a/OPN/Stream/NativeGamepadMonitor.swift +++ b/OPN/Stream/NativeGamepadMonitor.swift @@ -78,6 +78,7 @@ public final class NativeGamepadMonitor { let pollingQueue = DispatchQueue(label: "com.opennow.gamepad-poll", qos: .userInteractive) var pollingAllowed = false var mappingsEnabled = false + var isKeyboardMouseInputEnabled = true private var mappingSubscription: AnyCancellable? private var orderSubscription: AnyCancellable? var bindingOutputLedger = ControllerBindingOutputLedger() @@ -365,11 +366,7 @@ public final class NativeGamepadMonitor { guard let self else { return } self.pollState.pendingEvents.append(contentsOf: events) Task { @MainActor [weak self] in - guard let self else { return } - let commands = self.pollingQueue.sync { self.pollState.takePendingCommands() } - let pending = self.pollingQueue.sync { self.pollState.takePendingEvents() } - for command in commands { self.onStreamCommand?(command) } - for event in pending { self.emitInputEvent(event) } + self?.dispatchPendingInput() } }, onBatteryChange: { [weak self] changes in guard let self else { return } @@ -520,6 +517,20 @@ extension NativeGamepadMonitor { return true } + /// Disables every listed device that this stream knows, in one slot refresh. The couch co-op + /// preset and the hand-off between windows both land here, so the pads another window plays + /// with are out of this one before its first poll. + func disableForStream(_ deviceIDs: Set) { + var next = streamSelection + let changed = deviceIDs.filter { next.setEnabled(false, for: $0) } + guard !changed.isEmpty else { return } + streamSelection = next + OPNStreamTelemetry.capture("webrtc.input.gamepad.stream_selection", level: .info, + message: "Controllers left out of this stream.", + attributes: ["count": String(changed.count)]) + refreshControllerSlots() + } + /// The HUD's rows for this stream: effective player per enabled device, plus the remembered rows /// for whatever is disabled, waiting or gone. var streamControllerRows: [ControllerStreamSelection.Row] { streamSelection.rows } diff --git a/OPN/Stream/NativeStreamView.swift b/OPN/Stream/NativeStreamView.swift index fb56c9f9a..af9f9acf2 100644 --- a/OPN/Stream/NativeStreamView.swift +++ b/OPN/Stream/NativeStreamView.swift @@ -173,17 +173,12 @@ public final class NativeStreamView: NSView { } var cursorLocationProvider: () -> CGPoint = { NSEvent.mouseLocation } var cursorWarpHandler: ((CGPoint) -> Void)? + var inputFocusProvider: (() -> (isApplicationActive: Bool, isWindowKey: Bool))? // `internal(set)` rather than `private(set)`: the type's own extensions in the neighbouring // files write these, and the public contract is unchanged — nothing outside the module can. public internal(set) var isPointerLocked = false public internal(set) var isAbsoluteCursorConfined = false public internal(set) var isEmittingNeutralizingAbsolutePosition = false - public var isCursorCaptured: Bool { isPointerLocked || isAbsoluteCursorConfined } - /// A cursor rect and a pointer capture only mean anything for the key window of the active app. - /// Taking either while another app is frontmost steals a pointer this window cannot even draw in. - public var isFrontmostInputTarget: Bool { - NSApplication.shared.isActive && window?.isKeyWindow == true - } /// PiP is a small, floating viewing surface. While it is on screen the stream must not take the /// pointer - the cursor has to stay usable for the strip and for whatever else the user moved /// to - and the controller has to keep working even though the window is deliberately never @@ -197,6 +192,11 @@ public final class NativeStreamView: NSView { updateControllerMappingFocus() } } + /// Couch co-op: this stream's pads keep playing while another stream window has the focus. Keyboard + /// and mouse stay with the frontmost window either way. Set by the presenter as windows come and go. + public internal(set) var allowsBackgroundGamepadInput = false { + didSet { if oldValue != allowsBackgroundGamepadInput { updateControllerMappingFocus() } } + } public var locksPointerWhenRelativeModeSelected = false public var confinesCursorToWindowInAbsoluteMode = false { didSet { @@ -424,8 +424,12 @@ public final class NativeStreamView: NSView { /// The provider is fixed per surface: a stream's mappings come from the game it was launched for, /// which is what keeps two accounts' concurrent games from sharing one game's overrides. - init(frame frameRect: NSRect, mappingProvider: any ControllerMappingProviding = ControllerMappingStore.shared) { - gamepadMonitor = NativeGamepadMonitor(mappingProvider: mappingProvider) + init(frame frameRect: NSRect, + mappingProvider: any ControllerMappingProviding = ControllerMappingStore.shared, + initiallyDisabledControllers: Set = [], + controllerRegistry: ControllerMappingDevices = .shared) { + gamepadMonitor = NativeGamepadMonitor(mappingProvider: mappingProvider, registry: controllerRegistry) + gamepadMonitor.disableForStream(initiallyDisabledControllers) super.init(frame: frameRect) wantsLayer = true layer?.backgroundColor = NSColor.black.cgColor diff --git a/OPN/Stream/NativeStreamViewInput.swift b/OPN/Stream/NativeStreamViewInput.swift index c5de5f063..b8fd2936c 100644 --- a/OPN/Stream/NativeStreamViewInput.swift +++ b/OPN/Stream/NativeStreamViewInput.swift @@ -245,17 +245,17 @@ extension NativeStreamView { onInputEvent?(.mouse(.button(deviceID: "mouse", button: button, isPressed: isPressed, timestamp: Self.timestamp()))) } - func releasePressedInputs() { + func releasePressedInputs(includingGamepads: Bool = true) { let timestamp = Self.timestamp() let keyboardEvents = pressedKeyboardEvents.values let mouseButtons = pressedMouseButtons - let gamepadStates = activeGamepadStates.values + let gamepadStates = includingGamepads ? Array(activeGamepadStates.values) : [] pressedKeyboardEvents.removeAll() textInputKeyCodes.removeAll() pushToTalkState?.release() textInputState.cancel() updateCompositionBar() - activeGamepadStates.removeAll() + if includingGamepads { activeGamepadStates.removeAll() } preciseScrollRemainder = 0 preciseHorizontalScrollRemainder = 0 lastEmittedAbsoluteMouseEvent = nil @@ -291,15 +291,19 @@ extension NativeStreamView { } func updateControllerMappingFocus() { + let inputFocusState = inputFocus + gamepadMonitor.setKeyboardMouseInputEnabled(isFrontmostInputTarget && remoteInputEnabled && !localOverlayCapturesInput) gamepadMonitor.setMappingsEnabled(ControllerMappingFocusPolicy.allowsMappings( - appIsActive: NSApplication.shared.isActive, windowIsKey: window?.isKeyWindow == true, - remoteInputEnabled: remoteInputEnabled, overlayCapturesInput: localOverlayCapturesInput + appIsActive: inputFocusState.isApplicationActive, windowIsKey: inputFocusState.isWindowKey, + remoteInputEnabled: remoteInputEnabled, overlayCapturesInput: localOverlayCapturesInput, + allowsBackgroundPads: allowsBackgroundGamepadInput )) } func handleFocusLoss() { - gamepadMonitor.setMappingsEnabled(false) - releasePressedInputs() + gamepadMonitor.setKeyboardMouseInputEnabled(false) + gamepadMonitor.setMappingsEnabled(allowsBackgroundGamepadInput && remoteInputEnabled && !localOverlayCapturesInput) + releasePressedInputs(includingGamepads: !allowsBackgroundGamepadInput) setPointerLocked(false) applyLocalCursorPolicy() } diff --git a/OPN/Stream/NativeStreamViewPointer.swift b/OPN/Stream/NativeStreamViewPointer.swift index ed04b8d82..dfb0be2b0 100644 --- a/OPN/Stream/NativeStreamViewPointer.swift +++ b/OPN/Stream/NativeStreamViewPointer.swift @@ -5,8 +5,25 @@ import AppKit import QuartzCore extension NativeStreamView { + public var isCursorCaptured: Bool { isPointerLocked || isAbsoluteCursorConfined } + + /// A cursor rect and a pointer capture only mean anything for the key window of the active app. + /// Taking either while another app is frontmost steals a pointer this window cannot even draw in. + public var isFrontmostInputTarget: Bool { + let inputFocusState = inputFocus + return inputFocusState.isApplicationActive && inputFocusState.isWindowKey + } + + var inputFocus: (isApplicationActive: Bool, isWindowKey: Bool) { + inputFocusProvider?() ?? (NSApplication.shared.isActive, window?.isKeyWindow == true) + } + + var canAcquirePointerCapture: Bool { + remoteInputEnabled && !localOverlayCapturesInput && isFrontmostInputTarget && !isPictureInPictureMode + } + func enablePointerLock() { - guard window != nil else { return } + guard canAcquirePointerCapture, window != nil else { return } disableAbsoluteCursorConfinement() guard !isAbsoluteCursorConfined else { return } guard cursorAssociationHandler(false) == .success else { @@ -89,9 +106,9 @@ extension NativeStreamView { } func captureAbsoluteCursorIfNeeded() { - guard remoteInputEnabled, directMouseInputEnabled, confinesCursorToWindowInAbsoluteMode, + guard canAcquirePointerCapture, directMouseInputEnabled, confinesCursorToWindowInAbsoluteMode, mouseInputMode == .absolute, !isPointerLocked, !isAbsoluteCursorConfined, - !isPictureInPictureMode, window != nil else { return } + window != nil else { return } guard Self.confinedCursorPoint(cursorLocationProvider(), to: window?.frame ?? .zero) != nil else { return } isAbsoluteCursorConfined = true window?.acceptsMouseMovedEvents = true diff --git a/OPN/Stream/OPNCouchCoopInputPolicy.swift b/OPN/Stream/OPNCouchCoopInputPolicy.swift new file mode 100644 index 000000000..828236f97 --- /dev/null +++ b/OPN/Stream/OPNCouchCoopInputPolicy.swift @@ -0,0 +1,12 @@ +// When a stream's controllers keep playing while another stream window has the focus. + +import Foundation + +enum OPNCouchCoopInputPolicy { + /// Pads are a device the system delivers to the process rather than to a window, so two streams + /// can each play their own while one holds the keyboard. That is only wanted when couch co-op is + /// on and there is a second stream for it to matter to. + static func allowsBackgroundGamepads(isFlagEnabled: Bool, liveStreamCount: Int) -> Bool { + isFlagEnabled && liveStreamCount >= 2 + } +} diff --git a/OPN/Stream/OPNGameSession.swift b/OPN/Stream/OPNGameSession.swift index e76286a84..b0ba967a3 100644 --- a/OPN/Stream/OPNGameSession.swift +++ b/OPN/Stream/OPNGameSession.swift @@ -24,6 +24,8 @@ final class OPNGameSession { let account: LoginAccount let session: LoginSession let streamProfile: OPNStreamPreferenceProfile + let launchEntitlements: OPNLaunchEntitlements + let providerStreamingBaseURL: String private(set) var accountID: OPNAccountID var phase: OPNGameSessionPhase = .checkingSession @@ -70,6 +72,8 @@ final class OPNGameSession { launchBridge: any GameLaunchBridging, discordPresence: any DiscordPresenceServing, streamProfile: OPNStreamPreferenceProfile, + launchEntitlements: OPNLaunchEntitlements = .none, + providerStreamingBaseURL: String = OPNGameService.defaultStreamingBaseUrl, registry: OPNGameSessionRegistry, results: OPNGameSessionResultStore ) { @@ -81,6 +85,8 @@ final class OPNGameSession { self.launchBridge = launchBridge self.discordPresence = discordPresence self.streamProfile = streamProfile + self.launchEntitlements = launchEntitlements + self.providerStreamingBaseURL = providerStreamingBaseURL self.registry = registry self.results = results } @@ -303,9 +309,9 @@ final class OPNGameSession { ) let ownedConfiguration = configuration.snapshottingSession( id: id, - ownerDisplayName: account.displayName, + ownerDisplayName: account.displayName, ownerAccountID: accountID.rawValue, mappingGameIdentity: ControllerMappingStore.shared.currentGameIdentity ?? "" - ) + ).snapshottingEntitlements(launchEntitlements, providerStreamingBaseURL: providerStreamingBaseURL) if activeDiscordPresence == nil { activeDiscordPresence = discordPresence(for: ownedConfiguration) } @@ -467,6 +473,9 @@ final class OPNGameSession { return discordPresence(for: game) } +} + +extension OPNGameSession { // MARK: - Shared launch helpers static func mediaConfiguration(from configuration: OPNStreamLaunchConfiguration, titleOverride: String = "", membershipTier: String = "") -> StreamLaunchConfiguration { diff --git a/OPN/Stream/OPNGameSessionOwnership.swift b/OPN/Stream/OPNGameSessionOwnership.swift index cdd38ac0f..22cf7be07 100644 --- a/OPN/Stream/OPNGameSessionOwnership.swift +++ b/OPN/Stream/OPNGameSessionOwnership.swift @@ -30,6 +30,8 @@ final class OPNGameSessionRegistry { launchBridge: any GameLaunchBridging, discordPresence: any DiscordPresenceServing, streamProfile: OPNStreamPreferenceProfile, + launchEntitlements: OPNLaunchEntitlements = .none, + providerStreamingBaseURL: String = OPNGameService.defaultStreamingBaseUrl, results: OPNGameSessionResultStore = .shared ) -> OPNGameSession? { guard let accountID = account.resolveStableAccountID() else { @@ -45,6 +47,8 @@ final class OPNGameSessionRegistry { launchBridge: launchBridge, discordPresence: discordPresence, streamProfile: streamProfile, + launchEntitlements: launchEntitlements, + providerStreamingBaseURL: providerStreamingBaseURL, registry: self, results: results ) diff --git a/OPN/Stream/OPNStreamBorderless.swift b/OPN/Stream/OPNStreamBorderless.swift new file mode 100644 index 000000000..6ee51c76b --- /dev/null +++ b/OPN/Stream/OPNStreamBorderless.swift @@ -0,0 +1,145 @@ +// Borderless windowed full screen, as a mode of the dedicated stream window. +// +// The window keeps its style mask. Changing `styleMask` on a window that is already on screen is +// the trap `OPNStreamPictureInPicture` documents, and `.titled` is what the Dock icon counts. The +// mode is the frame, the level and the standard buttons instead: the window is set to a screen's +// frame, raised above the menu bar and the Dock when it covers the whole screen, and its traffic +// lights are hidden. Leaving puts back what entering replaced. + +import AppKit + +@MainActor +enum OPNStreamBorderless { + /// Above the menu bar, so a window that covers a screen covers it too. + static let coveringLevel = NSWindow.Level(rawValue: NSWindow.Level.mainMenu.rawValue + 1) + + /// Where a window the reader has to reach sits: above the borderless stream windows, which cover + /// the whole screen they are on. + static let aboveCoveringLevel = NSWindow.Level(rawValue: coveringLevel.rawValue + 1) + + /// Whether `frame` fills one of `screenFrames`. Only such a window is raised over the menu bar + /// and the Dock; a tile that shares a screen stays an ordinary window beside its neighbours. + static func coversWholeScreen(_ frame: NSRect, screenFrames: [NSRect]) -> Bool { + screenFrames.contains { screen in + abs(screen.minX - frame.minX) < 1 && abs(screen.minY - frame.minY) < 1 + && abs(screen.width - frame.width) < 1 && abs(screen.height - frame.height) < 1 + } + } + + static func level(for frame: NSRect, screenFrames: [NSRect]) -> NSWindow.Level { + coversWholeScreen(frame, screenFrames: screenFrames) ? coveringLevel : .normal + } + + /// Puts the window into borderless mode at `frame`, or moves an already borderless window to a + /// new one. What was there before the first entry is kept for the way out. `coversMenuBar` raises + /// the window over the menu bar and the Dock even when it is only one tile of a screen, which is + /// how tiles fill a screen together; without it only a window that fills a screen is raised. + static func enter(_ window: OPNStreamWindow, frame: NSRect, coversMenuBar: Bool = false, + screenFrames: [NSRect] = NSScreen.screens.map(\.frame)) { + guard !window.isPresentationEnding, !window.isPictureInPicture else { return } + if window.borderlessRestoreState == nil { + window.borderlessRestoreState = OPNStreamWindow.WindowedState( + level: window.level, + collectionBehavior: window.collectionBehavior, + frame: window.frame, + isMiniaturized: window.isMiniaturized + ) + OPNStreamWindowFrameStore.save(window.frame, isPictureInPicture: false, defaults: window.frameStoreDefaults) + } + window.layoutState.isBorderless = true + let level = coversMenuBar ? coveringLevel : level(for: frame, screenFrames: screenFrames) + window.scheduleLayoutChange { window in + if window.isMiniaturized { window.deminiaturize(nil) } + StreamWindowGeometryGate.releaseAspectRatioLock(window) + OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: true) + window.level = level + for child in window.childWindows ?? [] { child.level = level } + window.setFrameFromLayout(frame) + postCoveringStateChange(for: window) + } + } + + static func exit(_ window: OPNStreamWindow, completion: (@MainActor () -> Void)? = nil) { + guard !window.isPresentationEnding else { return } + guard window.layoutState.isBorderless || window.borderlessRestoreState != nil || + StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { + window.cancelPendingLayout() + completion?() + return + } + window.scheduleLayoutChange { window in + restoreWindowedState(window, isFrameRestorationEnabled: true) + guard !Task.isCancelled, !window.isPresentationEnding else { return } + completion?() + } + window.isBorderlessExitPending = window.layoutState.isBorderless + } + + private static func restoreWindowedState(_ window: OPNStreamWindow, isFrameRestorationEnabled: Bool) { + guard window.layoutState.isBorderless || window.borderlessRestoreState != nil else { + window.clearLayoutFrame() + return + } + let isBorderlessBeforeRestoration = window.layoutState.isBorderless + window.layoutState.isBorderless = false + defer { + window.clearLayoutFrame() + if isBorderlessBeforeRestoration { postCoveringStateChange(for: window) } + } + OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: false) + guard let state = window.borderlessRestoreState else { + window.level = .normal + for child in window.childWindows ?? [] { child.level = .normal } + return + } + window.borderlessRestoreState = nil + window.level = state.level + for child in window.childWindows ?? [] { child.level = state.level } + guard isFrameRestorationEnabled else { return } + window.setFrameFromLayout(state.frame) + if state.isMiniaturized { window.miniaturize(nil) } + } + + /// Posted whenever a stream window takes or gives up the covering level, so a window that has to + /// stay reachable above it re-reads its level then instead of on a timer. + static let coveringStateDidChange = Notification.Name("OpenNOW.StreamBorderless.coveringStateDidChange") + + static func postCoveringStateChange(for window: OPNStreamWindow? = nil) { + NotificationCenter.default.post(name: coveringStateDidChange, object: window) + } + + /// The window's own toggle, from the HUD: the window is the reader's from here on, so the layout + /// stops placing it whichever way it goes. + static func toggleByReader(_ window: OPNStreamWindow) { + if window.layoutState.isBorderless, !window.isBorderlessExitPending { + releaseToReader(window) + return + } + window.layoutState.isManuallyPlaced = true + enter(window, frame: targetFrame(for: window)) + window.layoutMembershipDidChange?() + } + + /// Leaves borderless and the layout together, for a window that is about to become something + /// else (Picture in Picture) or that the reader no longer wants tiled. + static func releaseToReader(_ window: OPNStreamWindow, completion: (@MainActor () -> Void)? = nil) { + window.layoutState.isManuallyPlaced = true + window.layoutState.isAutoTiled = false + window.clearLayoutFrame() + exit(window, completion: completion) + window.layoutMembershipDidChange?() + } + + static func takePlacement(_ window: OPNStreamWindow) { + window.cancelPendingLayout() + window.layoutState.isManuallyPlaced = true + window.layoutState.isAutoTiled = false + restoreWindowedState(window, isFrameRestorationEnabled: false) + window.layoutMembershipDidChange?() + } + + /// The screen a borderless toggle fills when nothing else decided: the one the window is on. + static func targetFrame(for window: NSWindow) -> NSRect { + (window.screen ?? NSScreen.main)?.frame ?? window.frame + } +} diff --git a/OPN/Stream/OPNStreamControllerPreset.swift b/OPN/Stream/OPNStreamControllerPreset.swift new file mode 100644 index 000000000..aff3bbb3e --- /dev/null +++ b/OPN/Stream/OPNStreamControllerPreset.swift @@ -0,0 +1,47 @@ +// Which controllers a stream should play with, decided before the stream exists. +// +// The Detach New Session window picks an account and the pads to leave out of its stream. The +// stream is created later, by the launch, so the choice waits here under the account's id and is +// handed over once, when that account's stream first sees its controllers. + +import Foundation + +struct OPNStreamControllerSelection: Equatable, Sendable { + /// Pads this stream leaves out. + var disabled: Set = [] + /// Whether the pads this stream plays with are taken out of every other live stream, so one + /// tap sets a pad per player instead of the same pads answering in every window. + var takesPadsFromOtherStreams = false + + /// The pads the new stream keeps, which are the ones the others give up. + func handedOff(connected: [InputDeviceID]) -> Set { + guard takesPadsFromOtherStreams else { return [] } + return Set(connected).subtracting(disabled) + } +} + +@MainActor +final class OPNStreamControllerPreset { + static let shared = OPNStreamControllerPreset() + + private var selections: [String: OPNStreamControllerSelection] = [:] + + init() {} + + func store(_ selection: OPNStreamControllerSelection, forAccount accountID: String) { + guard !accountID.isEmpty else { return } + guard selection != OPNStreamControllerSelection() else { + selections[accountID] = nil + return + } + selections[accountID] = selection + } + + func take(forAccount accountID: String) -> OPNStreamControllerSelection { + selections.removeValue(forKey: accountID) ?? OPNStreamControllerSelection() + } + + func peek(forAccount accountID: String) -> OPNStreamControllerSelection { + selections[accountID] ?? OPNStreamControllerSelection() + } +} diff --git a/OPN/Stream/OPNStreamPictureInPicture.swift b/OPN/Stream/OPNStreamPictureInPicture.swift index b3ed7d3cf..8c86bcf3f 100644 --- a/OPN/Stream/OPNStreamPictureInPicture.swift +++ b/OPN/Stream/OPNStreamPictureInPicture.swift @@ -66,8 +66,20 @@ enum OPNStreamPictureInPicture { OPNStreamStageGeometry.centered(size: contentSize, within: visibleFrame) } - static func enter(_ window: OPNStreamWindow, aspectRatio: CGFloat) { - guard !window.isPictureInPicture else { return } + static func enter(_ window: OPNStreamWindow, aspectRatio: CGFloat, + prepareForEntry: (@MainActor () -> Bool)? = nil, + completion: (@MainActor () -> Void)? = nil) { + guard !window.isPictureInPicture, !window.isPresentationEnding, !window.styleMask.contains(.fullScreen) else { return } + OPNStreamBorderless.releaseToReader(window) { [weak window] in + guard let window, !window.isPresentationEnding, prepareForEntry?() != false else { return } + enterRestoredWindow(window, aspectRatio: aspectRatio) + completion?() + } + guard !window.isPictureInPicture, window.hasPendingLayoutChange else { return } + window.pictureInPictureRequestID = UUID() + } + + private static func enterRestoredWindow(_ window: OPNStreamWindow, aspectRatio: CGFloat) { window.windowedState = OPNStreamWindow.WindowedState( level: window.level, collectionBehavior: window.collectionBehavior, @@ -102,7 +114,12 @@ enum OPNStreamPictureInPicture { } static func exit(_ window: OPNStreamWindow) { + if window.isPictureInPicturePending { + OPNStreamBorderless.exit(window) + window.layoutMembershipDidChange?() + } guard window.isPictureInPicture else { return } + window.cancelPendingLayout() OPNStreamWindowFrameStore.save(window.frame, isPictureInPicture: true, defaults: window.frameStoreDefaults) window.isPictureInPicture = false OPNStreamWindowChrome.apply(to: window, isPictureInPicture: false) @@ -110,6 +127,7 @@ enum OPNStreamPictureInPicture { window.level = .normal window.collectionBehavior = [.fullScreenPrimary] setAllSpacesMembership(onChildWindowsOf: window, isMember: false) + window.layoutMembershipDidChange?() return } window.windowedState = nil @@ -118,6 +136,7 @@ enum OPNStreamPictureInPicture { setAllSpacesMembership(onChildWindowsOf: window, isMember: false) window.setFrame(state.frame, display: true) if state.isMiniaturized { window.miniaturize(nil) } + window.layoutMembershipDidChange?() } /// The carrier child windows are not children in the view hierarchy, so nothing else re-derives @@ -152,3 +171,13 @@ enum OPNStreamWindowChrome { window.isMovableByWindowBackground = isPictureInPicture } } + +extension OPNStreamWindowChrome { + /// Borderless drops the traffic lights and nothing else: the window stays where the layout put it + /// rather than becoming draggable, because a stray drag would take it out of the layout. + static func applyBorderless(to window: NSWindow, isBorderless: Bool) { + for button in [NSWindow.ButtonType.closeButton, .miniaturizeButton, .zoomButton] { + window.standardWindowButton(button)?.isHidden = isBorderless + } + } +} diff --git a/OPN/Stream/OPNStreamTileLayout.swift b/OPN/Stream/OPNStreamTileLayout.swift new file mode 100644 index 000000000..6f054ef58 --- /dev/null +++ b/OPN/Stream/OPNStreamTileLayout.swift @@ -0,0 +1,133 @@ +// Where each stream window goes when several are on screen, as pure geometry. +// +// The presenter owns the windows and the screens; this owns only the arithmetic, so the placement +// rules can be asserted without a window server. + +import CoreGraphics +import Foundation + +enum OPNStreamTileOrientation: String, CaseIterable, Identifiable, Sendable { + case auto + case sideBySide + case topAndBottom + + static let storageKey = "OpenNOW.CouchCoop.Tiling" + + var id: String { rawValue } + + var title: String { + switch self { + case .auto: "Automatic" + case .sideBySide: "Side by Side" + case .topAndBottom: "Top and Bottom" + } + } + + static var stored: OPNStreamTileOrientation { + OPNAppPreferenceStorage.standard.string(forKey: storageKey).flatMap(Self.init(rawValue:)) ?? .auto + } + + static func store(_ orientation: OPNStreamTileOrientation) { + OPNAppPreferenceStorage.standard.set(orientation.rawValue, forKey: storageKey) + } +} + +/// Whether tiled windows stay below the menu bar and the Dock, or fill each screen over them. +enum OPNStreamTileCoverage: String, CaseIterable, Identifiable, Sendable { + case visibleArea + case fullScreen + + static let storageKey = "OpenNOW.CouchCoop.Coverage" + + var id: String { rawValue } + + var title: String { + switch self { + case .visibleArea: "Below the Menu Bar" + case .fullScreen: "Full Screen" + } + } + + static var stored: OPNStreamTileCoverage { + OPNAppPreferenceStorage.standard.string(forKey: storageKey).flatMap(Self.init(rawValue:)) ?? .visibleArea + } + + static func store(_ coverage: OPNStreamTileCoverage) { + OPNAppPreferenceStorage.standard.set(coverage.rawValue, forKey: storageKey) + } + + /// The area the tiles of each screen divide: the whole frame when covering, the part the menu + /// bar and Dock leave free otherwise. Both lists are in screen order. + func areas(frames: [CGRect], visibleFrames: [CGRect]) -> [CGRect] { + self == .fullScreen ? frames : visibleFrames + } + + var flipped: OPNStreamTileCoverage { self == .fullScreen ? .visibleArea : .fullScreen } +} + +struct OPNStreamTile: Equatable, Sendable { + let frame: CGRect + let screenIndex: Int +} + +enum OPNStreamTileLayout { + /// One tile per window, in window order. A window gets a whole screen while there are screens to + /// spare; past that the windows are dealt over the screens in order and a shared screen is split. + static func tiles(screens: [CGRect], windowCount: Int, orientation: OPNStreamTileOrientation) -> [OPNStreamTile] { + guard windowCount > 0, !screens.isEmpty else { return [] } + let share = windowsPerScreen(screenCount: screens.count, windowCount: windowCount) + var tiles: [OPNStreamTile] = [] + for (screenIndex, count) in share.enumerated() where count > 0 { + let cells = split(screens[screenIndex], into: count, orientation: orientation) + tiles.append(contentsOf: cells.map { OPNStreamTile(frame: $0, screenIndex: screenIndex) }) + } + return tiles + } + + /// How many windows each screen holds. Earlier screens take the remainder, so the main screen is + /// never the one left with fewer. + static func windowsPerScreen(screenCount: Int, windowCount: Int) -> [Int] { + guard screenCount > 0, windowCount > 0 else { return [] } + let base = windowCount / screenCount + let remainder = windowCount % screenCount + return (0.. [CGRect] { + guard count > 0 else { return [] } + guard count > 1 else { return [area] } + let grid = gridShape(for: count, in: area, orientation: orientation) + var cells: [CGRect] = [] + for index in 0.. (columns: Int, rows: Int) { + let prefersColumns = switch orientation { + case .auto: area.width >= area.height + case .sideBySide: true + case .topAndBottom: false + } + if count <= 3 { + return prefersColumns ? (count, 1) : (1, count) + } + let long = Int(Double(count).squareRoot().rounded(.up)) + let short = Int((Double(count) / Double(long)).rounded(.up)) + return prefersColumns ? (long, short) : (short, long) + } + + private static func edge(of origin: CGFloat, length: CGFloat, step: Int, steps: Int) -> CGFloat { + origin + (length * CGFloat(step) / CGFloat(steps)).rounded() + } +} diff --git a/OPN/Stream/OPNStreamWindow.swift b/OPN/Stream/OPNStreamWindow.swift index 5d7f74df6..02a8b4c50 100644 --- a/OPN/Stream/OPNStreamWindow.swift +++ b/OPN/Stream/OPNStreamWindow.swift @@ -39,6 +39,95 @@ final class OPNStreamWindow: NSWindow { /// What PiP entry replaced, so leaving PiP puts the window back exactly where it was. var windowedState: WindowedState? + /// What borderless entry replaced, so leaving it puts the window back where the reader left it. + var borderlessRestoreState: WindowedState? + + /// Shared with the window's content, which letterboxes instead of locking its aspect while the + /// layout owns the frame. + let layoutState = OPNStreamWindowLayoutState() + + /// The last frame the layout gave this window. A move or resize that leaves it is the reader + /// taking the window back, which a frame change alone cannot tell from the layout's own. + private(set) var layoutFrame: NSRect? + + private(set) var isApplyingLayoutChange = false + private(set) var isPresentationEnding = false + private var layoutTask: Task? + private var layoutRequestID = UUID() + var isBorderlessExitPending = false + var pictureInPictureRequestID: UUID? + var isPictureInPicturePending: Bool { pictureInPictureRequestID != nil } + var hasPendingLayoutChange: Bool { layoutTask != nil } + var layoutMembershipDidChange: (@MainActor () -> Void)? + + func cancelPendingLayout() { + layoutRequestID = UUID() + layoutTask?.cancel() + layoutTask = nil + pictureInPictureRequestID = nil + isBorderlessExitPending = false + } + + func scheduleLayoutChange(_ applyLayoutChange: @escaping @MainActor (OPNStreamWindow) -> Void) { + cancelPendingLayout() + guard !isPresentationEnding else { return } + let scheduledLayoutRequestID = layoutRequestID + layoutTask = Task { @MainActor [weak self] in + guard let self else { return } + defer { self.clearFinishedLayoutRequest(scheduledLayoutRequestID) } + guard await StreamWindowGeometryGate.waitUntilGeometryIsMutable(for: self), + self.layoutRequestID == scheduledLayoutRequestID, !self.isPresentationEnding, + !self.styleMask.contains(.fullScreen) else { return } + self.isApplyingLayoutChange = true + defer { self.isApplyingLayoutChange = false } + applyLayoutChange(self) + } + } + + private func clearFinishedLayoutRequest(_ finishedLayoutRequestID: UUID) { + guard layoutRequestID == finishedLayoutRequestID else { return } + layoutTask = nil + pictureInPictureRequestID = nil + isBorderlessExitPending = false + } + + func endPresentation() { + isPresentationEnding = true + cancelPendingLayout() + layoutMembershipDidChange = nil + } + + override func close() { + endPresentation() + super.close() + OPNStreamBorderless.postCoveringStateChange(for: self) + } + + func setFrameFromLayout(_ frame: NSRect) { + let isApplyingEnclosingLayoutChange = isApplyingLayoutChange + isApplyingLayoutChange = true + defer { isApplyingLayoutChange = isApplyingEnclosingLayoutChange } + layoutFrame = frame + setFrame(frame, display: true) + } + + func clearLayoutFrame() { + layoutFrame = nil + } + + /// Whether the window is where the layout left it. + var isAtLayoutFrame: Bool { + guard let layoutFrame else { return false } + return abs(frame.minX - layoutFrame.minX) < 1 && abs(frame.minY - layoutFrame.minY) < 1 + && abs(frame.width - layoutFrame.width) < 1 && abs(frame.height - layoutFrame.height) < 1 + } + + /// A titled window is pushed below the menu bar when it is placed. A borderless one is asked to + /// cover it, so the frame it was given is the frame it keeps. + override func constrainFrameRect(_ frameRect: NSRect, to screen: NSScreen?) -> NSRect { + layoutState.isBorderless ? frameRect : super.constrainFrameRect(frameRect, to: screen) + } + /// The stream surface's answer to the close button, installed once the surface is up. Returning /// `true` keeps the window open - see `OPNStreamWindowCloseGuard` for why the window itself /// never decides. @@ -49,6 +138,16 @@ final class OPNStreamWindow: NSWindow { /// all, and the controls panel it raises - and the surface is the only thing that has it. weak var sessionSurface: (any OPNStreamWindowSessionSurface)? + /// Couch co-op: the stream in this window keeps its pads while another stream window is in + /// front. Written by the presenter and handed to the stream surface, which may not have + /// resolved yet, so the surface also reads it when it attaches. + var allowsBackgroundGamepadInput = false { + didSet { + guard oldValue != allowsBackgroundGamepadInput else { return } + sessionSurface?.setBackgroundGamepadInput(allowsBackgroundGamepadInput) + } + } + /// The close guard, held **strongly and by the window**. /// /// `NSWindow.delegate` is a `weak` property (AppKit's `NSWindow.h`), so a guard that is only @@ -88,7 +187,7 @@ final class OPNStreamWindow: NSWindow { // A full-screen frame belongs to the Space, not to the user: restoring it would open the // next session full-bleed. The exit re-fires the move, so the windowed placement is saved // once the window is back on the desktop. - guard !styleMask.contains(.fullScreen) else { return } + guard !styleMask.contains(.fullScreen), layoutState.persistsFrame else { return } OPNStreamWindowFrameStore.save(frame, isPictureInPicture: isPictureInPicture, defaults: frameStoreDefaults) } @@ -107,6 +206,8 @@ final class OPNStreamWindow: NSWindow { protocol OPNStreamWindowSessionSurface: AnyObject { var isConnected: Bool { get } var isPictureInPicture: Bool { get } + func setBackgroundGamepadInput(_ isAllowed: Bool) + func disableControllersForStream(_ deviceIDs: Set) /// Raises the stream controls panel. Called with a `nil` completion, which is what keeps the /// third button reading "End Stream" rather than "Quit OpenNOW". Deliberately written out /// rather than leaning on `showStreamControls`'s default argument: a defaulted parameter does diff --git a/OPN/Stream/OPNStreamWindowLayoutState.swift b/OPN/Stream/OPNStreamWindowLayoutState.swift new file mode 100644 index 000000000..100480cbc --- /dev/null +++ b/OPN/Stream/OPNStreamWindowLayoutState.swift @@ -0,0 +1,29 @@ +// How one stream window is laid out on the desktop, as state the window and its content share. +// +// Borderless and tiled windows both give up the aspect lock and the titlebar strip: the picture +// letterboxes inside whatever frame the window was given instead of the frame following the +// picture. The window owns this, the stream's root view reads it, and the presenter writes it. + +import Foundation +import Observation + +@MainActor +@Observable +final class OPNStreamWindowLayoutState { + /// The window covers a frame edge to edge with its chrome hidden. + var isBorderless = false + /// The presenter owns this window's frame. Cleared the moment the reader moves or resizes it. + var isAutoTiled = false + + /// The reader placed this window themselves, so the layout leaves it where it is. + var isManuallyPlaced = false + + var releasesAspectLock: Bool { isBorderless || isAutoTiled } + + /// A window the layout owns never enters native full screen by itself, and the session-ready + /// full-screen request leaves it alone. + var suppressesNativeFullScreen: Bool { isBorderless || isAutoTiled } + + /// Placements are remembered only for a window the reader placed. + var persistsFrame: Bool { !isBorderless && !isAutoTiled } +} diff --git a/OPN/Stream/StreamWindowGeometryGate.swift b/OPN/Stream/StreamWindowGeometryGate.swift index d9afc942d..4114c622d 100644 --- a/OPN/Stream/StreamWindowGeometryGate.swift +++ b/OPN/Stream/StreamWindowGeometryGate.swift @@ -66,6 +66,20 @@ enum StreamWindowGeometryGate { window.resizeIncrements = onePointIncrements } + @MainActor + static func waitUntilGeometryIsMutable(for window: NSWindow) async -> Bool { + for _ in 0..<200 { + guard !Task.isCancelled else { return false } + guard shouldDeferGeometryMutation(for: window) else { return true } + do { + try await Task.sleep(for: .milliseconds(50)) + } catch { + return false + } + } + return !Task.isCancelled && !shouldDeferGeometryMutation(for: window) + } + /// Runs `body` once the nested loop ends, and drops it if that never happens. /// /// A zoom animation has no "did end" notification to observe, so this polls at roughly its own diff --git a/OPNApp.swift b/OPNApp.swift index fdf2c211d..f8498021b 100644 --- a/OPNApp.swift +++ b/OPNApp.swift @@ -20,6 +20,10 @@ struct OPNApp: App { /// changes nothing, which is what breaks the loop. @State private var isMenuBarStatusItemInserted = OPNMenuBarSessionModel.shared.isStatusItemInserted + /// Mirrors the Couch Co-op Labs flag so the Stream menu follows it live: the flag is the only + /// thing that decides whether the Detach New Session command exists. + @AppStorage(OPNLabs.couchCoop.storageKey) private var isCouchCoopEnabled = false + let sharedModelContainer: ModelContainer init() { @@ -176,7 +180,12 @@ struct OPNApp: App { CatalogLaunchPrefetch.shared.primeFromCache(accountIdentifier: userId) return } - CatalogLaunchPrefetch.shared.start(accountIdentifier: userId, accessToken: session.accessToken, idToken: session.idToken) + CatalogLaunchPrefetch.shared.start( + accountIdentifier: userId, + accessToken: session.accessToken, + idToken: session.idToken, + idpId: session.idpId.isEmpty ? (account?.providerIdpId ?? "") : session.idpId + ) } } @@ -248,17 +257,16 @@ struct OPNApp: App { } } OPNCatalogCommands() - CommandGroup(replacing: .help) { - Button { - OPNReportIssuePresentation.shared.present(context: .appEnvironment()) - } label: { - Label("Report an Issue…", systemImage: "exclamationmark.bubble") - } - } + OPNReportIssueCommands() CommandMenu("Stream") { Button("Join Remote Co-Op as Guest…") { openWindow(id: "remote-coop-guest") } + if isCouchCoopEnabled { + Button("Detach New Session…") { + openWindow(id: OPNDetachedSessionScene.id, value: UUID()) + } + } Button("Toggle Microphone") { _ = StreamSessionLifecycle.sendCommand(.toggleMicrophone) } @@ -303,6 +311,14 @@ struct OPNApp: App { isMenuBarStatusItemInserted = isInserted } + WindowGroup("Detached Session", id: OPNDetachedSessionScene.id, for: UUID.self) { _ in + DetachedSessionView() + .environmentObject(systemAppearance) + } + .defaultSize(width: 1100, height: 680) + .restorationBehavior(.disabled) + .modelContainer(sharedModelContainer) + Window("Join Remote Co-Op", id: "remote-coop-guest") { RemoteCoOpGuestView() } diff --git a/Tests/Auth/DetachedAccountActionTests.swift b/Tests/Auth/DetachedAccountActionTests.swift new file mode 100644 index 000000000..34da5f531 --- /dev/null +++ b/Tests/Auth/DetachedAccountActionTests.swift @@ -0,0 +1,313 @@ +import Foundation +import SwiftData +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct DetachedAccountActionTests { + @Test func signOutTargetsTheDetachedAccountWithoutChangingTheMainAccount() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let confirmation = OPNConfirmationPresentation() + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: confirmation, handoff: handoff) + await actions.bind(login: fixture.login).value + + await actions.request(.signOut(.init(account: fixture.detachedAccount)))?.value + + #expect(fixture.auth.endedEmails == [fixture.detachedEmail]) + #expect(fixture.detachedSession.accessToken.isEmpty) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(!fixture.mainSession.accessToken.isEmpty) + #expect(confirmation.request == nil) + } + + @Test func forgettingAnAccountQueuedWithoutAMainSceneUsesTheCapturedIdentity() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: OPNConfirmationPresentation(), handoff: handoff) + let request = actions.request(.forget(.init(account: fixture.detachedAccount))) + #expect(request == nil) + #expect(fixture.auth.endedEmails.isEmpty) + + await actions.bind(login: fixture.login).value + + #expect(fixture.auth.endedEmails == [fixture.detachedEmail]) + #expect(fixture.login.accounts.map(\.email) == [fixture.mainAccount.email]) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(GFNTokenStore.load(forIdentity: "session.\(fixture.detachedSessionID)") == nil) + } + + @Test func aReplacedAccountDoesNotReceiveAPreviouslyQueuedDestructiveAction() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let confirmation = OPNConfirmationPresentation() + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: confirmation, handoff: handoff) + actions.request(.forget(.init(account: fixture.detachedAccount))) + let userID = fixture.detachedAccount.userId + let context = fixture.container.mainContext + context.delete(fixture.detachedAccount) + try context.save() + let replacement = makeLoginAccountForTesting(email: fixture.detachedEmail, userId: userID, isActive: false) + context.insert(replacement) + try context.save() + + await actions.bind(login: fixture.login).value + + #expect(fixture.login.accounts.contains { $0 === replacement }) + #expect(fixture.auth.endedEmails.isEmpty) + #expect(confirmation.request?.title == "Account action unavailable") + #expect(fixture.login.activeAccount === fixture.mainAccount) + confirmation.dismiss() + } + + @Test func accountActionsRecheckOwnershipWhenTheRequestArrives() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let confirmation = OPNConfirmationPresentation() + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: confirmation, handoff: handoff) + let target = OPNMainAccountActions.Target(account: fixture.detachedAccount) + actions.request(.signOut(target)) + _ = try #require(fixture.registry.begin( + account: fixture.detachedAccount, session: fixture.detachedSession, + gameService: OPNGameService.shared, launchBridge: OPNGameLaunchBridge.shared, + discordPresence: DiscordRichPresence.shared, streamProfile: OPNStreamPreferenceProfile() + )) + + await actions.bind(login: fixture.login).value + #expect(confirmation.request?.message == OPNAccountMutationGuard.activeGameSessionMessage) + confirmation.dismiss() + await actions.request(.forget(target))?.value + + #expect(confirmation.request?.message == OPNAccountMutationGuard.activeGameSessionMessage) + #expect(fixture.auth.endedEmails.isEmpty) + #expect(!fixture.detachedSession.accessToken.isEmpty) + #expect(fixture.login.accounts.contains { $0.email == fixture.detachedEmail }) + confirmation.dismiss() + } + + @Test func addingAnAccountUsesTheMainLoginFlow() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: OPNConfirmationPresentation(), handoff: handoff) + actions.request(.addAccount) + + await actions.bind(login: fixture.login).value + #expect(fixture.login.signInRequest == .addAccount) + } + + @Test(arguments: [false, true]) + func reauthenticationRevealsTheExactAccountWhileTheMainChooserIsOpen(isQueuedBeforeBind: Bool) async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: OPNConfirmationPresentation(), handoff: handoff) + let mainAccessToken = fixture.mainSession.accessToken + let detachedAccessToken = fixture.detachedSession.accessToken + let gameSession = try #require(fixture.registry.begin( + account: fixture.detachedAccount, session: fixture.detachedSession, + gameService: OPNGameService.shared, launchBridge: OPNGameLaunchBridge.shared, + discordPresence: DiscordRichPresence.shared, streamProfile: OPNStreamPreferenceProfile() + )) + if !isQueuedBeforeBind { await actions.bind(login: fixture.login).value } + let chooserReason: AccountChooserReason = isQueuedBeforeBind ? .startup : .switchAccount + fixture.login.accountChooserReason = chooserReason + + let request = actions.request(.reauthenticate(.init(account: fixture.detachedAccount))) + if isQueuedBeforeBind { + #expect(request == nil) + #expect(fixture.login.signInRequest == nil) + #expect(fixture.login.accountChooserReason == chooserReason) + await actions.bind(login: fixture.login).value + } + await request?.value + + #expect(fixture.login.signInRequest == .reauthenticate(email: fixture.detachedEmail)) + #expect(fixture.login.accountChooserReason == nil) + #expect(fixture.login.reauthAccount === fixture.detachedAccount) + #expect(fixture.login.email == fixture.detachedEmail) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(fixture.mainSession.accessToken == mainAccessToken) + #expect(fixture.detachedSession.accessToken == detachedAccessToken) + #expect(fixture.registry.sessions.count == 1) + #expect(fixture.registry.sessions.first === gameSession) + #expect(fixture.auth.endedEmails.isEmpty) + + fixture.login.cancelReauthentication() + + #expect(fixture.login.signInRequest == nil) + #expect(fixture.login.accountChooserReason == nil) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(fixture.registry.sessions.first === gameSession) + } + + @Test(arguments: [false, true], [400, 401]) + func rejectedSavedGrantsRevealTheExactAccountWithoutOfflineContinuation(_ isSavedSessionUnexpired: Bool, _ status: Int) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + try fixture.expireIDTokens() + if !isSavedSessionUnexpired { fixture.detachedSession.expiresAt = .distantPast } + let mainTokens = fixture.tokens(for: fixture.mainSession) + let detachedTokens = fixture.tokens(for: fixture.detachedSession) + SessionManagerURLProtocol.install(host: "login.nvidia.com") { _ in + SessionManagerURLProtocol.response(json: ["error": "invalid_grant"], status: status) + } + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + fixture.login.accountChooserReason = .switchAccount + + fixture.login.activateAccount(fixture.detachedAccount) + try await waitUntil { fixture.login.signInRequest == .reauthenticate(email: fixture.detachedEmail) } + + #expect(fixture.login.accountChooserReason == nil) + #expect(fixture.login.reauthAccount === fixture.detachedAccount) + #expect(fixture.login.validationMessage.contains("saved session was rejected")) + #expect(fixture.login.successMessage.isEmpty) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(fixture.tokens(for: fixture.mainSession) == mainTokens) + #expect(fixture.tokens(for: fixture.detachedSession) == detachedTokens) + let authSession = await fixture.login.jarvisAuthService.session + #expect(authSession.clientToken.isEmpty) + let requests = SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com") + #expect(requests.count == 2) + #expect(requests.allSatisfy { $0.httpMethod == "POST" && $0.url?.path == "/token" }) + } + } + + @Test func aTransientFailureForAnExpiredSavedSessionShowsARetryNoticeInsteadOfSignIn() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + defer { fixture.login.dismissAccountSwitchNotice() } + fixture.detachedSession.expiresAt = .distantPast + let detachedTokens = fixture.tokens(for: fixture.detachedSession) + SessionManagerURLProtocol.install(host: "login.nvidia.com") { _ in + SessionManagerURLProtocol.response(json: ["error": "temporarily_unavailable"], status: 503) + } + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + + fixture.login.activateAccount(fixture.detachedAccount) + try await waitUntil { fixture.login.accountSwitchNotice != nil } + + #expect(fixture.login.accountSwitchNotice == fixture.login.validationMessage) + #expect(fixture.login.validationMessage.contains("Check your connection")) + #expect(fixture.login.signInRequest == nil) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(fixture.tokens(for: fixture.detachedSession) == detachedTokens) + #expect(SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count == JarvisRetryPolicy.gfnPC.maxRetries + 1) + } + } + + @Test(.timeLimit(.minutes(1))) + func aRejectedRestoreCannotDisplaceANewerExactAccountHandoffAfterSignOut() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + fixture.detachedSession.expiresAt = .distantPast + let mainTokens = fixture.tokens(for: fixture.mainSession) + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: OPNConfirmationPresentation(), handoff: handoff) + await actions.bind(login: fixture.login).value + let revokeBeforeResponse: @MainActor @Sendable () async -> Void = { + await actions.request(.signOut(.init(account: fixture.detachedAccount)))?.value + await actions.request(.reauthenticate(.init(account: fixture.mainAccount)))?.value + } + SessionManagerURLProtocol.install(host: "login.nvidia.com") { _ in + if SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count == 1 { + let revocation = DispatchSemaphore(value: 0) + Task { @MainActor in + await revokeBeforeResponse() + revocation.signal() + } + revocation.wait() + } + return SessionManagerURLProtocol.response(json: ["error": "invalid_grant"], status: 401) + } + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + fixture.login.accountChooserReason = .switchAccount + + fixture.login.activateAccount(fixture.detachedAccount) + try await waitUntil { + SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count == 2 && !fixture.login.isAuthenticating + } + + #expect(fixture.auth.endedEmails == [fixture.detachedEmail]) + #expect(fixture.detachedSession.accessToken.isEmpty && fixture.detachedSession.refreshToken.isEmpty) + #expect(fixture.login.signInRequest == .reauthenticate(email: fixture.mainAccount.email)) + #expect(fixture.login.reauthAccount === fixture.mainAccount) + #expect(fixture.login.email == fixture.mainAccount.email) + #expect(fixture.login.accountChooserReason == nil) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(fixture.tokens(for: fixture.mainSession) == mainTokens) + } + } + + @Test(.timeLimit(.minutes(1)), arguments: [400, 401]) + func aJarvisSessionSwitchCancelsRestoreWithoutOfflineSuccess(_ status: Int) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + try fixture.expireIDTokens() + try #require(fixture.detachedSession.canContinueOffline && !fixture.detachedSession.isExpired) + let mainTokens = fixture.tokens(for: fixture.mainSession) + let detachedTokens = fixture.tokens(for: fixture.detachedSession) + let generation = fixture.login.loginLaunchGeneration + let replacement = JarvisSession( + accessToken: "new-owner-access", idToken: "new-owner-id", refreshToken: "new-owner-refresh", + userId: fixture.mainSession.userId, isAuthenticated: true, clientToken: "new-owner-client", + clientTokenExpiry: JarvisSession.currentEpochMs() + 3_600_000, + accessTokenExpiry: JarvisSession.currentEpochMs() + 3_600_000 + ) + SessionManagerURLProtocol.install(host: "login.nvidia.com") { _ in + if SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count == 1 { + let sessionChanged = DispatchSemaphore(value: 0) + Task { @MainActor in + await fixture.login.jarvisAuthService.setSession(replacement) + sessionChanged.signal() + } + sessionChanged.wait() + } + return SessionManagerURLProtocol.response(json: ["error": "invalid_grant"], status: status) + } + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + + let outcome = await fixture.login.restoreAccountSessionOutcome(fixture.detachedAccount) + + #expect(outcome == .cancelled) + #expect(!Task.isCancelled) + #expect(fixture.login.loginLaunchGeneration == generation) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(!fixture.detachedAccount.isActive && !fixture.detachedSession.isActive) + #expect(!fixture.login.isAuthenticating) + #expect(fixture.login.successMessage.isEmpty && fixture.login.validationMessage.isEmpty) + #expect(fixture.login.signInRequest == nil) + #expect(fixture.tokens(for: fixture.mainSession) == mainTokens) + #expect(fixture.tokens(for: fixture.detachedSession) == detachedTokens) + #expect(await fixture.login.jarvisAuthService.session == replacement) + let requests = SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com") + #expect(requests.count == 1) + #expect(requests.first?.httpMethod == "POST" && requests.first?.url?.path == "/token") + } + } + + private func waitUntil(_ predicate: () -> Bool) async throws { + for _ in 0..<600 { + if predicate() { return } + try await Task.sleep(for: .milliseconds(10)) + } + try #require(predicate(), "The scripted account restore did not finish") + } +} diff --git a/Tests/Auth/DetachedAccountTestSupport.swift b/Tests/Auth/DetachedAccountTestSupport.swift new file mode 100644 index 000000000..d2e78abbb --- /dev/null +++ b/Tests/Auth/DetachedAccountTestSupport.swift @@ -0,0 +1,136 @@ +import Foundation +import SwiftData +@testable import OpenNOW + +final class DetachedRecordingAuthService: LoginAuthServing, @unchecked Sendable { + private(set) var endedEmails: [String] = [] + + func invalidatePendingAuthentication() {} + + func startOAuthLogin(providerIdpId: String, completion: @escaping OPNAuthCallback) { + Task { @MainActor in completion(false, OPNAuthSession(), "No network in detached-account tests") } + } + + func startStarfleetDeviceCodeLogin(providerIdpId: String, challengeHandler: @escaping OPNDeviceCodeChallengeCallback, completion: @escaping OPNAuthCallback) { + Task { @MainActor in completion(false, OPNAuthSession(), "No network in detached-account tests") } + } + + func endSavedSession(userId: String, email: String) { + endedEmails.append(email) + } +} + +private final class DetachedProviderInfoService: GameProviderInfoServing, @unchecked Sendable { + func fetchProviderInfo(idpId: String, completion: @escaping OPNProviderInfoCallback) { + Task { @MainActor in completion(false, OPNGameProviderInfo(), OPNGameProviderEndpoint(), "No network in detached-account tests") } + } +} + +@MainActor +final class DetachedAccountTestFixture { + let container: ModelContainer + let login: LoginViewModel + let registry = OPNGameSessionRegistry() + let auth = DetachedRecordingAuthService() + let mainAccount: LoginAccount + let detachedAccount: LoginAccount + let mainSession: LoginSession + let detachedSession: LoginSession + let detachedEmail: String + let detachedSessionID: String + private var sessionIDs: [String] = [] + + init() throws { + container = try ModelContainer( + for: LoginAccount.self, LoginSession.self, LoginDeviceRegistration.self, + configurations: ModelConfiguration(isStoredInMemoryOnly: true, cloudKitDatabase: .none) + ) + let identity = UUID().uuidString + mainAccount = makeLoginAccountForTesting(email: "main-\(identity)@example.invalid", userId: "main-\(identity)") + detachedEmail = "detached-\(identity)@example.invalid" + detachedAccount = makeLoginAccountForTesting(email: detachedEmail, userId: "detached-\(identity)", isActive: false) + mainSession = makeLoginSessionForTesting(accountEmail: mainAccount.email, userId: mainAccount.userId) + detachedSessionID = UUID().uuidString + detachedSession = makeLoginSessionForTesting(accountEmail: detachedEmail, id: detachedSessionID, userId: detachedAccount.userId) + detachedSession.isActive = false + login = LoginViewModel(authService: auth, providerInfoService: DetachedProviderInfoService(), sessionRegistry: registry) + let context = container.mainContext + context.insert(mainAccount) + context.insert(detachedAccount) + context.insert(mainSession) + context.insert(detachedSession) + try context.save() + login.update(modelContext: context, accounts: [mainAccount, detachedAccount], sessions: [mainSession, detachedSession], devices: []) + sessionIDs = [mainSession.id, detachedSessionID] + GFNTokenStore.save( + .init(accessToken: detachedSession.accessToken, idToken: detachedSession.idToken, + refreshToken: detachedSession.refreshToken, clientToken: detachedSession.clientToken), + forIdentity: "session.\(detachedSessionID)" + ) + } + + func expireIDTokens() throws { + for session in [mainSession, detachedSession] { + let claims: [String: Any] = ["sub": session.userId, "email": session.accountEmail, "exp": 1] + let payload = try JSONSerialization.data(withJSONObject: claims).base64EncodedString() + .replacingOccurrences(of: "+", with: "-") + .replacingOccurrences(of: "/", with: "_") + .replacingOccurrences(of: "=", with: "") + session.idToken = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.\(payload)." + } + } + + func tokens(for session: LoginSession) -> GFNTokenStore.Tokens { + .init(accessToken: session.accessToken, idToken: session.idToken, refreshToken: session.refreshToken, clientToken: session.clientToken) + } + + func insertReplacementSession(id: String = UUID().uuidString) throws -> LoginSession { + let replacement = makeLoginSessionForTesting(accountEmail: detachedEmail, id: id, userId: detachedAccount.userId) + replacement.isActive = false + sessionIDs.append(replacement.id) + container.mainContext.insert(replacement) + try container.mainContext.save() + return replacement + } + + func tearDown() { + for id in sessionIDs { GFNTokenStore.delete(forIdentity: "session.\(id)") } + } +} + +actor SuspendedDetachedRefresh { + enum Failure: Error { + case unavailable + } + + private var started: CheckedContinuation? + private var completion: CheckedContinuation? + private var input: JarvisSession? + + func refresh(_ session: JarvisSession) async throws -> JarvisSession { + input = session + started?.resume() + started = nil + return try await withCheckedThrowingContinuation { completion = $0 } + } + + func waitUntilStarted() async { + guard input == nil else { return } + await withCheckedContinuation { started = $0 } + } + + func complete(isSuccessful: Bool = true, replacementUserID: String? = nil) { + guard var session = input else { return } + defer { completion = nil } + guard isSuccessful else { + completion?.resume(throwing: Failure.unavailable) + return + } + session.accessToken = "refreshed-access" + session.idToken = "refreshed-id" + session.refreshToken = "refreshed-refresh" + session.clientToken = "refreshed-client" + if let replacementUserID { session.userId = replacementUserID } + completion?.resume(returning: session) + } +} diff --git a/Tests/Auth/DetachedSessionRefresherTests.swift b/Tests/Auth/DetachedSessionRefresherTests.swift new file mode 100644 index 000000000..7556c7941 --- /dev/null +++ b/Tests/Auth/DetachedSessionRefresherTests.swift @@ -0,0 +1,251 @@ +import Foundation +import SwiftData +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct DetachedSessionRefresherTests { + enum Invalidation: CaseIterable, Equatable, Sendable { + case purge + case signOut + case forget + case deleteSession + case replaceSession + case replaceSessionUsingOriginalID + case deleteAccount + case replaceAccount + case replaceCredentials + } + + @Test(.timeLimit(.minutes(1)), arguments: Invalidation.allCases) + func aSuspendedRefreshDiscardsInvalidatedCredentials(_ invalidation: Invalidation) async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let originalToken = fixture.detachedSession.accessToken + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + + try await invalidate(invalidation, fixture: fixture) + await suspended.complete() + + #expect(await task.value == false) + #expect(fixture.mainAccount.isActive) + #expect(fixture.mainSession.isActive) + let persisted = GFNTokenStore.load(forIdentity: "session.\(fixture.detachedSessionID)") + switch invalidation { + case .replaceCredentials: + #expect(persisted?.accessToken == "new-login-access") + #expect(persisted?.refreshToken == "new-login-refresh") + case .purge, .signOut, .forget: + #expect(persisted == nil) + default: + #expect(persisted?.accessToken == originalToken) + } + } + + @Test(.timeLimit(.minutes(1))) + func aFreshRefreshPersistsOnlyTheInactiveAccountsTokens() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let originalMainToken = fixture.mainSession.accessToken + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + await suspended.complete() + + #expect(await task.value) + #expect(fixture.detachedSession.accessToken == "refreshed-access") + #expect(GFNTokenStore.load(forIdentity: "session.\(fixture.detachedSessionID)")?.refreshToken == "refreshed-refresh") + #expect(!fixture.detachedAccount.isActive) + #expect(!fixture.detachedSession.isActive) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(fixture.mainSession.accessToken == originalMainToken) + } + + @Test(.timeLimit(.minutes(1))) + func aFailedRefreshCannotOfferOfflineContinuationAfterAPurge() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + fixture.detachedSession.purgeTokens() + await suspended.complete(isSuccessful: false) + + #expect(await task.value == false) + #expect(fixture.detachedSession.accessToken.isEmpty) + } + + @Test(.timeLimit(.minutes(1))) + func aCancelledRefreshDiscardsItsLateResponse() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let originalToken = fixture.detachedSession.accessToken + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + task.cancel() + await suspended.complete() + + #expect(await task.value == false) + #expect(fixture.detachedSession.accessToken == originalToken) + } + + @Test(.timeLimit(.minutes(1))) + func aResponseForAnotherSubjectNeverReplacesTheSelectedAccountsTokens() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let originalToken = fixture.detachedSession.accessToken + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + await suspended.complete(replacementUserID: fixture.mainAccount.userId) + + #expect(await task.value == false) + #expect(fixture.detachedSession.accessToken == originalToken) + } + + @Test func deletedModelsAreRejectedBeforeStartingARefresh() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let context = fixture.container.mainContext + context.delete(fixture.detachedSession) + context.delete(fixture.detachedAccount) + try context.save() + let refresher = DetachedSessionRefresher(modelContext: context) { session in + Issue.record("Deleted models reached the refresh transport") + return session + } + + #expect(await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) == false) + } + + @Test func anUnchangedUnexpiredSessionCanContinueOfflineWhenRefreshFails() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext) { _ in + throw SuspendedDetachedRefresh.Failure.unavailable + } + + #expect(await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession)) + #expect(!fixture.detachedAccount.isActive) + #expect(!fixture.detachedSession.isActive) + } + + @Test func aServiceCancellationCannotOfferOfflineContinuationForUnexpiredCredentials() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + try #require(fixture.detachedSession.canContinueOffline && !fixture.detachedSession.isExpired) + let mainTokens = fixture.tokens(for: fixture.mainSession) + let detachedTokens = fixture.tokens(for: fixture.detachedSession) + let issuedAt = fixture.detachedSession.issuedAt + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext) { _ in + throw CancellationError() + } + + #expect(await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) == false) + #expect(!Task.isCancelled) + #expect(fixture.tokens(for: fixture.mainSession) == mainTokens) + #expect(fixture.tokens(for: fixture.detachedSession) == detachedTokens) + #expect(GFNTokenStore.load(forIdentity: "session.\(fixture.detachedSessionID)") == detachedTokens) + #expect(fixture.detachedSession.issuedAt == issuedAt) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(!fixture.detachedAccount.isActive && !fixture.detachedSession.isActive) + } + + @Test(arguments: [400, 401, 403, 503]) + func theDefaultRefresherOnlyContinuesOfflineForNonCredentialFailures(_ status: Int) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + try fixture.expireIDTokens() + let mainTokens = fixture.tokens(for: fixture.mainSession) + let detachedTokens = fixture.tokens(for: fixture.detachedSession) + let detachedStatus = fixture.detachedAccount.authStatus + SessionManagerURLProtocol.install(host: "login.nvidia.com") { _ in + SessionManagerURLProtocol.response(json: ["error": status == 503 ? "temporarily_unavailable" : "invalid_grant"], status: status) + } + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext) + #expect(await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) == (status == 503)) + #expect(fixture.tokens(for: fixture.detachedSession) == detachedTokens) + #expect(fixture.tokens(for: fixture.mainSession) == mainTokens) + #expect(GFNTokenStore.load(forIdentity: "session.\(fixture.detachedSessionID)") == detachedTokens) + #expect(fixture.detachedAccount.authStatus == detachedStatus) + #expect(!fixture.detachedAccount.isActive && !fixture.detachedSession.isActive) + #expect(fixture.login.activeSession === fixture.mainSession) + let requests = SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com") + #expect(requests.count == (status == 503 ? JarvisRetryPolicy.gfnPC.maxRetries + 1 : 2)) + #expect(requests.allSatisfy { $0.httpMethod == "POST" && $0.url?.path == "/token" }) + } + } + + @Test func theDefaultRefresherReplacesARejectedClientTokenThroughTheOwnersRefreshGrant() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + try fixture.expireIDTokens() + let mainTokens = fixture.tokens(for: fixture.mainSession) + SessionManagerURLProtocol.install(host: "login.nvidia.com") { request in + if request.url?.path == "/client_token" { + return SessionManagerURLProtocol.response(json: ["client_token": "fresh-owner-client", "expires_in": 240]) + } + if SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count == 1 { + return SessionManagerURLProtocol.response(json: ["error": "invalid_grant"], status: 401) + } + return SessionManagerURLProtocol.response(json: ["access_token": "fresh-owner-access", "refresh_token": "fresh-owner-refresh", "id_token": "fresh-owner-id", "expires_in": 3600]) + } + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext) + #expect(await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession)) + let expected = GFNTokenStore.Tokens(accessToken: "fresh-owner-access", idToken: "fresh-owner-id", refreshToken: "fresh-owner-refresh", clientToken: "fresh-owner-client") + #expect(fixture.tokens(for: fixture.detachedSession) == expected) + #expect(GFNTokenStore.load(forIdentity: "session.\(fixture.detachedSessionID)") == expected) + #expect(fixture.tokens(for: fixture.mainSession) == mainTokens) + #expect(!fixture.detachedAccount.isActive && !fixture.detachedSession.isActive) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").map { $0.url?.path } == ["/token", "/token", "/client_token"]) + } + } + + private func invalidate(_ invalidation: Invalidation, fixture: DetachedAccountTestFixture) async throws { + let context = fixture.container.mainContext + switch invalidation { + case .purge: + fixture.detachedSession.purgeTokens() + case .signOut: + await fixture.login.signOutAccount(fixture.detachedAccount) + case .forget: + fixture.login.forgetAccount(fixture.detachedAccount) + case .deleteSession: + context.delete(fixture.detachedSession) + case .replaceSession: + context.delete(fixture.detachedSession) + try context.save() + _ = try fixture.insertReplacementSession() + case .replaceSessionUsingOriginalID: + context.delete(fixture.detachedSession) + try context.save() + _ = try fixture.insertReplacementSession(id: fixture.detachedSessionID) + case .deleteAccount: + context.delete(fixture.detachedAccount) + case .replaceAccount: + let userID = fixture.detachedAccount.userId + context.delete(fixture.detachedAccount) + try context.save() + context.insert(makeLoginAccountForTesting(email: fixture.detachedEmail, userId: userID, isActive: false)) + case .replaceCredentials: + fixture.detachedSession.accessToken = "new-login-access" + fixture.detachedSession.refreshToken = "new-login-refresh" + } + try context.save() + } +} diff --git a/Tests/Auth/LoginRestoreIntentTests.swift b/Tests/Auth/LoginRestoreIntentTests.swift new file mode 100644 index 000000000..6a2a7671c --- /dev/null +++ b/Tests/Auth/LoginRestoreIntentTests.swift @@ -0,0 +1,189 @@ +import Foundation +import SwiftData +import Testing +@testable import OpenNOW + +private enum NewAccountHandoff: CaseIterable, Sendable { + case addAccount, reauthenticateOther +} + +private enum FailedRestoreResponse: Int, CaseIterable, Sendable { + case rejected = 401 + case unavailable = 503 + + var requestCount: Int { + self == .rejected ? 2 : JarvisRetryPolicy.gfnPC.maxRetries + 1 + } +} + +@MainActor +private final class LoginRestoreIntentFixture { + let saved: DetachedAccountTestFixture + let otherAccount: LoginAccount + let actions: OPNMainAccountActions + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + let mainTokens: GFNTokenStore.Tokens + let detachedTokens: GFNTokenStore.Tokens + private(set) var isRestorePendingAtHandoff = false + + init() throws { + let saved = try DetachedAccountTestFixture() + let other = LoginAccount( + email: "other-\(UUID().uuidString.lowercased())@example.invalid", displayName: "Other owner", + providerIdpId: "other-provider", providerName: "Other provider", userId: "other-user", + rememberSession: false, isActive: false + ) + let context = saved.container.mainContext + context.insert(other) + do { + try context.save() + } catch { + saved.tearDown() + throw error + } + saved.login.update(modelContext: context, accounts: [saved.mainAccount, saved.detachedAccount, other], sessions: [saved.mainSession, saved.detachedSession], devices: []) + saved.detachedSession.expiresAt = .distantPast + self.saved = saved + otherAccount = other + mainTokens = saved.tokens(for: saved.mainSession) + detachedTokens = saved.tokens(for: saved.detachedSession) + actions = OPNMainAccountActions(confirmation: OPNConfirmationPresentation(), handoff: handoff) + } + + func tearDown() { + saved.login.dismissAccountSwitchNotice() + handoff.end() + saved.tearDown() + } + + func bind() async { + await actions.bind(login: saved.login).value + } + + func requestHandoff(_ requested: NewAccountHandoff, isCancelled: Bool) async { + isRestorePendingAtHandoff = saved.login.isAuthenticating + switch requested { + case .addAccount: + await actions.request(.addAccount)?.value + case .reauthenticateOther: + await actions.request(.reauthenticate(.init(account: otherAccount)))?.value + } + if isCancelled { saved.login.cancelReauthentication() } + } + + func beginHandoff(_ requested: NewAccountHandoff, isCancelled: Bool) { + switch requested { + case .addAccount: + saved.login.beginAddAccount() + case .reauthenticateOther: + saved.login.beginReauthentication(for: otherAccount) + } + if isCancelled { saved.login.cancelReauthentication() } + } + + func installResponse(_ response: FailedRestoreResponse, beforeFirstResponse: @escaping @MainActor @Sendable () async -> Void = {}) { + SessionManagerURLProtocol.install(host: "login.nvidia.com") { _ in + if SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count == 1 { + let handoffFinished = DispatchSemaphore(value: 0) + Task { @MainActor in + await beforeFirstResponse() + handoffFinished.signal() + } + handoffFinished.wait() + } + return SessionManagerURLProtocol.response( + json: ["error": response == .rejected ? "invalid_grant" : "temporarily_unavailable"], + status: response.rawValue + ) + } + } + + func expectHandoff(_ requested: NewAccountHandoff, isCancelled: Bool) { + let login = saved.login + let expectedRequest: LoginSignInRequest = requested == .addAccount ? .addAccount : .reauthenticate(email: otherAccount.email) + #expect(login.signInRequest == (isCancelled ? nil : expectedRequest)) + if requested == .reauthenticateOther && !isCancelled { #expect(login.reauthAccount === otherAccount) } + #expect(login.email == (requested == .addAccount ? "" : otherAccount.email)) + #expect(login.selectedProvider.idpId == (requested == .addAccount ? LoginProvider.nvidia.idpId : otherAccount.providerIdpId)) + #expect(login.rememberSession == (requested == .addAccount)) + let expectedMessage = requested == .addAccount || isCancelled ? "" : "\(otherAccount.displayName) is signed out. Sign in again to switch to it." + #expect(login.validationMessage == expectedMessage) + #expect(login.successMessage.isEmpty) + #expect(login.accountSwitchNotice == nil) + #expect(login.accountChooserReason == nil) + #expect(!login.isAuthenticating && !login.isLaunchingOAuth) + #expect(login.activeAccount === saved.mainAccount) + #expect(login.activeSession === saved.mainSession) + #expect(saved.mainAccount.isActive && saved.mainSession.isActive) + #expect(!saved.detachedAccount.isActive && !saved.detachedSession.isActive) + #expect(saved.tokens(for: saved.mainSession) == mainTokens) + #expect(saved.tokens(for: saved.detachedSession) == detachedTokens) + #expect(GFNTokenStore.load(forIdentity: "session.\(saved.detachedSessionID)") == detachedTokens) + #expect(saved.auth.endedEmails.isEmpty) + } +} + +@MainActor +@Suite struct LoginRestoreIntentTests { + @Test(.timeLimit(.minutes(1)), arguments: NewAccountHandoff.allCases, FailedRestoreResponse.allCases) + private func aSuspendedRestoreCannotReplaceANewerDetachedAccountHandoff(_ requested: NewAccountHandoff, _ response: FailedRestoreResponse) async throws { + try await verifySuspendedRestore(requested, response: response, isNewFlowCancelled: false) + } + + @Test(.timeLimit(.minutes(1)), arguments: NewAccountHandoff.allCases, FailedRestoreResponse.allCases) + private func cancellingANewerHandoffDoesNotReviveASuspendedRestore(_ requested: NewAccountHandoff, _ response: FailedRestoreResponse) async throws { + try await verifySuspendedRestore(requested, response: response, isNewFlowCancelled: true) + } + + @Test(arguments: NewAccountHandoff.allCases) + private func aQueuedRestoreCannotStartAfterANewerHandoff(_ requested: NewAccountHandoff) async throws { + try await verifyQueuedRestore(requested, isNewFlowCancelled: false) + } + + @Test(arguments: NewAccountHandoff.allCases) + private func cancellingANewerHandoffDoesNotReviveAQueuedRestore(_ requested: NewAccountHandoff) async throws { + try await verifyQueuedRestore(requested, isNewFlowCancelled: true) + } + + private func verifySuspendedRestore(_ requested: NewAccountHandoff, response: FailedRestoreResponse, isNewFlowCancelled: Bool) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let fixture = try LoginRestoreIntentFixture() + defer { fixture.tearDown() } + await fixture.bind() + fixture.installResponse(response) { + await fixture.requestHandoff(requested, isCancelled: isNewFlowCancelled) + } + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + + let restore = fixture.saved.login.restoreAccountSelection(fixture.saved.detachedAccount) + let generation = fixture.saved.login.loginLaunchGeneration + await restore.value + + #expect(fixture.saved.login.loginLaunchGeneration > generation) + #expect(fixture.isRestorePendingAtHandoff) + fixture.expectHandoff(requested, isCancelled: isNewFlowCancelled) + let requests = SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com") + #expect(requests.count == response.requestCount) + #expect(requests.allSatisfy { $0.httpMethod == "POST" && $0.url?.path == "/token" }) + } + } + + private func verifyQueuedRestore(_ requested: NewAccountHandoff, isNewFlowCancelled: Bool) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let fixture = try LoginRestoreIntentFixture() + defer { fixture.tearDown() } + await fixture.bind() + fixture.installResponse(.rejected) + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + + let restore = fixture.saved.login.restoreAccountSelection(fixture.saved.detachedAccount) + let generation = fixture.saved.login.loginLaunchGeneration + fixture.beginHandoff(requested, isCancelled: isNewFlowCancelled) + await restore.value + + #expect(fixture.saved.login.loginLaunchGeneration > generation) + fixture.expectHandoff(requested, isCancelled: isNewFlowCancelled) + #expect(SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").isEmpty) + } + } +} diff --git a/Tests/Catalog/CatalogAccountMembershipTests.swift b/Tests/Catalog/CatalogAccountMembershipTests.swift new file mode 100644 index 000000000..07c396117 --- /dev/null +++ b/Tests/Catalog/CatalogAccountMembershipTests.swift @@ -0,0 +1,126 @@ +import Foundation +import SwiftData +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct CatalogAccountMembershipTests { + private let preferenceKeys = ["OpenNOW.Stream.DlssEntitlements", "OpenNOW.Stream.EntitledAudioChannelCount", "OpenNOW.Stream.EntitledInGameSettingsPersistence"] + + @Test func anInactiveAuthenticatedCatalogLoadsItsOwnMembershipWithoutChangingTheActiveAccount() { + withPreservedPreferences(preferenceKeys) { + let active = makeCatalog(email: "active@example.test", isActive: true) + let detached = makeCatalog(email: "detached@example.test", isActive: false) + let activeMembership = subscription(tier: "Ultimate", channels: 8, isEntitled: true) + OPNStreamPreferences.saveDlssEntitlements(activeMembership.dlssEntitlements) + defer { + active.model.session.purgeTokens() + detached.model.session.purgeTokens() + } + active.model.loadAccount() + detached.model.loadAccount() + #expect(active.model.dlssMembershipState == .loading) + #expect(detached.model.dlssMembershipState == .loading) + #expect(OPNStreamPreferences.loadDlssEntitlements() == activeMembership.dlssEntitlements) + + active.service.completeSubscription(at: 0, subscription: activeMembership) + detached.service.completeSubscription(at: 0, subscription: subscription(tier: "Free", channels: 2, isEntitled: false)) + + #expect(detached.model.account.membershipTier == "Free") + #expect(detached.model.dlssMembershipState == .loaded(.none)) + #expect(active.model.account.membershipTier == "Ultimate") + #expect(active.model.launchEntitlements.audioChannelCount == 8) + #expect(detached.model.launchEntitlements.audioChannelCount == 2) + #expect(OPNStreamPreferences.loadDlssEntitlements() == active.model.launchEntitlements.dlss) + #expect(OPNStreamPreferences.loadEntitledAudioChannelCount() == 8) + #expect(OPNStreamPreferences.loadEntitledInGameSettingsPersistence()) + } + } + + enum Invalidation: CaseIterable, Sendable { + case signOut, deleteAccount, deleteSession, replaceCredentials, replaceProvider + } + + @Test(arguments: Invalidation.allCases) + func anInvalidatedCatalogRejectsLateMembership(_ invalidation: Invalidation) throws { + let container = try ModelContainer(for: LoginAccount.self, LoginSession.self, configurations: ModelConfiguration(isStoredInMemoryOnly: true, cloudKitDatabase: .none)) + let catalog = makeCatalog(email: "late-\(invalidation)@example.test", isActive: false) + container.mainContext.insert(catalog.model.account) + container.mainContext.insert(catalog.model.session) + defer { + if invalidation != .deleteSession { catalog.model.session.purgeTokens() } + } + catalog.model.loadAccount() + #expect(catalog.service.subscriptionRequestCount == 1) + switch invalidation { + case .signOut: + catalog.model.session.purgeTokens() + case .deleteAccount: + container.mainContext.delete(catalog.model.account) + try container.mainContext.save() + case .deleteSession: + catalog.model.session.purgeTokens() + container.mainContext.delete(catalog.model.session) + try container.mainContext.save() + case .replaceCredentials: + catalog.model.session.idToken = "replacement-token" + case .replaceProvider: + catalog.model.session.idpId = "replacement-provider" + } + catalog.service.completeSubscription(at: 0, subscription: subscription(tier: "Ultimate", channels: 8, isEntitled: true)) + #expect(catalog.model.launchEntitlements == .none) + #expect(catalog.model.dlssMembershipState == .loading) + } + + @Test func replacingCredentialsCannotReusePreviouslyLoadedLaunchEntitlements() { + let catalog = makeCatalog(email: "replaced@example.test", isActive: false) + defer { catalog.model.session.purgeTokens() } + catalog.model.loadAccount() + catalog.service.completeSubscription(at: 0, subscription: subscription(tier: "Ultimate", channels: 8, isEntitled: true)) + #expect(catalog.model.currentLaunchEntitlements.audioChannelCount == 8) + catalog.model.session.idToken = "replacement-token" + #expect(catalog.model.currentLaunchEntitlements == .none) + } + + @Test func aNewMembershipRequestCannotBeOverwrittenByAnOlderCompletion() { + let catalog = makeCatalog(email: "reloaded@example.test", isActive: false) + defer { catalog.model.session.purgeTokens() } + catalog.model.loadAccount() + catalog.model.loadAccount() + catalog.service.completeSubscription(at: 1, subscription: subscription(tier: "Performance", channels: 6, isEntitled: false)) + catalog.service.completeSubscription(at: 0, subscription: subscription(tier: "Ultimate", channels: 8, isEntitled: true)) + #expect(catalog.model.account.membershipTier == "Performance") + #expect(catalog.model.launchEntitlements.audioChannelCount == 6) + #expect(catalog.model.dlssMembershipState == .loaded(.none)) + } + + @Test func anInactiveMembershipFailureFinishesCheckingWithoutClearingAnotherAccount() { + withPreservedPreferences(preferenceKeys) { + OPNStreamPreferences.saveDlssEntitlements(subscription(tier: "Ultimate", channels: 8, isEntitled: true).dlssEntitlements) + let catalog = makeCatalog(email: "failed@example.test", isActive: false) + defer { catalog.model.session.purgeTokens() } + catalog.model.loadAccount() + catalog.service.completeSubscription(at: 0, subscription: OPNSubscriptionInfo(), isSuccessful: false) + #expect(catalog.model.dlssMembershipState == .unavailable) + #expect(catalog.model.launchEntitlements == .none) + #expect(OPNStreamPreferences.loadDlssEntitlements().superResolution.isEntitled) + } + } + + private func makeCatalog(email: String, isActive: Bool) -> (model: CatalogViewModel, service: CatalogAccountServiceStub) { + let account = makeLoginAccountForTesting(email: email, userId: email, isActive: isActive) + let session = makeLoginSessionForTesting(accountEmail: email, userId: email) + session.isActive = isActive + let service = CatalogAccountServiceStub() + return (CatalogViewModel(account: account, session: session, gameService: service, onRefreshAuth: { false }), service) + } + + private func subscription(tier: String, channels: Int, isEntitled: Bool) -> OPNSubscriptionInfo { + var subscription = OPNSubscriptionInfo() + subscription.membershipTier = tier + subscription.entitledAudioChannelCount = channels + subscription.isInGameSettingsPersistenceEntitled = isEntitled + if isEntitled { subscription.dlssEntitlements = DlssEntitlements(superResolution: DlssCapability(isEntitled: true)) } + return subscription + } +} diff --git a/Tests/Catalog/CatalogAccountServiceStub.swift b/Tests/Catalog/CatalogAccountServiceStub.swift new file mode 100644 index 000000000..549f261df --- /dev/null +++ b/Tests/Catalog/CatalogAccountServiceStub.swift @@ -0,0 +1,128 @@ +import Foundation +@testable import OpenNOW + +final class CatalogAccountServiceStub: CatalogGameServing, @unchecked Sendable { + struct SessionCredentials: Equatable, Sendable { + let accessToken: String + let idToken: String + let userID: String + } + + private let lock = NSLock() + private var subscriptions: [OPNSubscriptionCallback] = [] + private var sessionCredentials: SessionCredentials? + private var subscriptionUsers: [String] = [] + + var subscriptionRequestCount: Int { lock.withLock { subscriptions.count } } + var configuredCredentials: SessionCredentials? { lock.withLock { sessionCredentials } } + var subscriptionUserIDs: [String] { lock.withLock { subscriptionUsers } } + + @MainActor + func completeSubscription(at index: Int, subscription: OPNSubscriptionInfo, isSuccessful: Bool = true) { + let completion = lock.withLock { subscriptions[index] } + completion(isSuccessful, subscription, isSuccessful ? "" : "Membership unavailable") + } + + func configureCatalogSession(accessToken: String, idToken: String, userId: String) { + lock.withLock { + sessionCredentials = SessionCredentials(accessToken: accessToken, idToken: idToken, userID: userId) + } + } + + func fetchSubscriptionInfo(userId: String, completion: @escaping OPNSubscriptionCallback) { + lock.withLock { + subscriptions.append(completion) + subscriptionUsers.append(userId) + } + } + + func fetchUserAccount(completion: @escaping OPNUserAccountCallback) { + Task { @MainActor in completion(true, OPNUserAccountInfo(), "") } + } + + func browseCatalogObject(searchQuery: String, sortId: String, filterIds: [String], fetchCount: Int, completion: @escaping @MainActor @Sendable (Bool, OPNCatalogBrowseResultObject, String) -> Void) { + Task { @MainActor in completion(false, OPNCatalogBrowseResultObject(), "Unused browse") } + } + + func browseCatalogObject(searchQuery: String, sortId: String, filterIds: [String], fetchCount: Int, forceRefresh: Bool, completion: @escaping @MainActor @Sendable (Bool, OPNCatalogBrowseResultObject, String) -> Void) { + browseCatalogObject(searchQuery: searchQuery, sortId: sortId, filterIds: filterIds, fetchCount: fetchCount, completion: completion) + } + + func browseCatalogObject(searchQuery: String, sortId: String, filterIds: [String], fetchCount: Int, forceRefresh: Bool, cursor: String, completion: @escaping @MainActor @Sendable (Bool, OPNCatalogBrowseResultObject, String) -> Void) { + browseCatalogObject(searchQuery: searchQuery, sortId: sortId, filterIds: filterIds, fetchCount: fetchCount, completion: completion) + } + + func fetchMarqueePanelObjects(completion: @escaping @MainActor @Sendable (Bool, [OPNCatalogPanelObject], String) -> Void) { + Task { @MainActor in completion(false, [], "Unused marquee") } + } + + func fetchMainPanelObjects(completion: @escaping @MainActor @Sendable (Bool, [OPNCatalogPanelObject], String) -> Void) { + Task { @MainActor in completion(false, [], "Unused panels") } + } + + func fetchLibraryGameObjects(completion: @escaping @MainActor @Sendable (Bool, [OPNCatalogGameObject], String) -> Void) { + Task { @MainActor in completion(false, [], "Unused library") } + } + + func fetchFavoriteGameObjects(completion: @escaping @MainActor @Sendable (Bool, [OPNCatalogGameObject], String) -> Void) { + Task { @MainActor in completion(false, [], "Unused favorites") } + } + + func fetchGameObjectByCMSId(_ cmsId: String, completion: @escaping @MainActor @Sendable (Bool, OPNCatalogGameObject?, String) -> Void) { + Task { @MainActor in completion(false, nil, "Unused game lookup") } + } + + func fetchStoreDefinitions(completion: @escaping OPNStoreDefinitionsCallback) { + Task { @MainActor in completion(false, [], "Unused stores") } + } + + func fetchSubscriptionDefinitions(completion: @escaping OPNSubscriptionDefinitionsCallback) { + Task { @MainActor in completion(false, [], "Unused subscriptions") } + } + + func fetchAppPatchStatuses(appIds: [String], completion: @escaping OPNAppPatchStatusesCallback) { + Task { @MainActor in completion(false, [:], "Unused patch statuses") } + } + + func fetchLibraryPatchStatuses(completion: @escaping OPNAppPatchStatusesCallback) { + Task { @MainActor in completion(false, [:], "Unused patch statuses") } + } + + func addOwnedVariant(_ variantId: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused ownership mutation") } + } + + func removeOwnedVariant(_ variantId: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused ownership mutation") } + } + + func selectOwnedVariant(_ variantId: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused ownership mutation") } + } + + func addFavoriteApp(_ appId: String, completion: @escaping OPNFavoriteActionCallback) { + Task { @MainActor in completion(false, "Unused favorite mutation") } + } + + func removeFavoriteApp(_ appId: String, completion: @escaping OPNFavoriteActionCallback) { + Task { @MainActor in completion(false, "Unused favorite mutation") } + } + + func syncAccountProvider(store: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused store sync") } + } + + func startAccountLinking(store: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused store linking") } + } + + func resolveStoreURL(game: OPNGameInfo, variantIndex: Int, completion: @escaping OPNStoreURLCallback) { + Task { @MainActor in completion(false, "", "Unused store URL") } + } + + func fetchProviderInfo(idpId: String, completion: @escaping OPNProviderInfoCallback) { + Task { @MainActor in completion(false, OPNGameProviderInfo(), OPNGameProviderEndpoint(), "Unused provider") } + } + + func providerStreamingBaseURL() -> String { OPNGameService.defaultStreamingBaseUrl } +} diff --git a/Tests/Catalog/CatalogOfflineSwitchTests.swift b/Tests/Catalog/CatalogOfflineSwitchTests.swift new file mode 100644 index 000000000..17228d289 --- /dev/null +++ b/Tests/Catalog/CatalogOfflineSwitchTests.swift @@ -0,0 +1,163 @@ +import Foundation +import Testing +@testable import OpenNOW + +@MainActor +private final class OfflineCatalogPresence: DiscordPresenceServing { + var isEnabled = false + private(set) var lastState = DiscordPresenceState.idle + + func update(_ state: DiscordPresenceState) { lastState = state } +} + +@MainActor +@Suite struct CatalogOfflineSwitchTests { + @Test(arguments: [false, true]) + func anOfflineSwitchKeepsDetachedMembershipOwnedByItsSavedSession(_ isRequestPendingDuringSwitch: Bool) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + installOfflineRefreshResponse() + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + try fixture.expireIDTokens() + let service = CatalogAccountServiceStub() + let model = makeDetachedCatalog(fixture: fixture, service: service) + let ownerCredentials = credentials(for: fixture.detachedSession) + try await restoreOfflineSession(fixture.detachedAccount, fixture: fixture) + if isRequestPendingDuringSwitch { model.loadAccount() } + + try await restoreOfflineSession(fixture.mainAccount, fixture: fixture) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(!fixture.detachedAccount.isActive && !fixture.detachedSession.isActive) + #expect(fixture.detachedAccount.authStatus == JarvisAuthStatus.notLoggedIn.rawValue) + #expect(credentials(for: fixture.detachedSession) == ownerCredentials) + if !isRequestPendingDuringSwitch { model.loadAccount() } + + try #require(service.subscriptionRequestCount == 1) + #expect(model.dlssMembershipState == .loading) + #expect(service.subscriptionUserIDs == [fixture.detachedSession.userId]) + let mainService = CatalogAccountServiceStub() + let mainCatalog = CatalogViewModel(account: fixture.mainAccount, session: fixture.mainSession, gameService: mainService, onRefreshAuth: { false }) + #expect(mainCatalog.configureCatalogService()) + #expect(model.configureCatalogService()) + #expect(service.configuredCredentials == ownerCredentials) + #expect(mainService.configuredCredentials == credentials(for: fixture.mainSession)) + withPreservedPreferences(["OpenNOW.Stream.DlssEntitlements", "OpenNOW.Stream.EntitledAudioChannelCount", "OpenNOW.Stream.EntitledInGameSettingsPersistence"]) { + OPNStreamPreferences.clearDlssEntitlements() + OPNStreamPreferences.saveEntitledAudioChannelCount(2) + OPNStreamPreferences.saveEntitledInGameSettingsPersistence(false) + service.completeSubscription(at: 0, subscription: membership()) + #expect(model.dlssMembershipState == .loaded(membership().dlssEntitlements)) + #expect(model.currentLaunchEntitlements == OPNLaunchEntitlements(subscription: membership())) + #expect(OPNStreamPreferences.loadDlssEntitlements() == .none) + #expect(OPNStreamPreferences.loadEntitledAudioChannelCount() == 2) + #expect(!OPNStreamPreferences.loadEntitledInGameSettingsPersistence()) + } + try expectDetachedResumeIsAdmitted(model, fixture: fixture) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count == 2 * (JarvisRetryPolicy.gfnPC.maxRetries + 1)) + } + } + + @Test(arguments: [false, true]) + func signingOutAfterAnOfflineSwitchRevokesDetachedCatalogAccess(_ isMembershipLoaded: Bool) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + installOfflineRefreshResponse() + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + try fixture.expireIDTokens() + try await restoreOfflineSession(fixture.detachedAccount, fixture: fixture) + try await restoreOfflineSession(fixture.mainAccount, fixture: fixture) + let service = CatalogAccountServiceStub() + let model = makeDetachedCatalog(fixture: fixture, service: service) + model.loadAccount() + try #require(service.subscriptionRequestCount == 1) + if isMembershipLoaded { + service.completeSubscription(at: 0, subscription: membership()) + #expect(model.currentLaunchEntitlements.audioChannelCount == 8) + } + let membershipBeforeSignOut = model.account.membershipTier + + await fixture.login.signOutAccount(fixture.detachedAccount) + #expect(fixture.auth.endedEmails == [fixture.detachedEmail]) + #expect(fixture.detachedSession.accessToken.isEmpty && fixture.detachedSession.idToken.isEmpty) + #expect(fixture.detachedSession.refreshToken.isEmpty && fixture.detachedSession.clientToken.isEmpty) + #expect(!model.configureCatalogService()) + if !isMembershipLoaded { service.completeSubscription(at: 0, subscription: membership()) } + #expect(model.account.membershipTier == membershipBeforeSignOut) + #expect(model.currentLaunchEntitlements == .none) + model.activeHomeSession = resumableSession() + model.resumeActiveHomeSession() + #expect(!fixture.registry.hasSessions) + #expect(model.launchErrorMessage == "Sign in again before launching a game.") + model.loadAccount() + #expect(service.subscriptionRequestCount == 1) + #expect(model.dlssMembershipState == .signedOut) + #expect(fixture.login.activeSession === fixture.mainSession) + } + } + + private func installOfflineRefreshResponse() { + SessionManagerURLProtocol.install(host: "login.nvidia.com") { _ in + SessionManagerURLProtocol.response(json: ["error": "temporarily_unavailable"], status: 503) + } + } + + private func restoreOfflineSession(_ account: LoginAccount, fixture: DetachedAccountTestFixture) async throws { + let session = try #require(fixture.login.sessions.first { $0.accountEmail == account.email }) + let originalTokens = fixture.tokens(for: session) + try #require(JarvisSessionParser.idTokenExpiry(session.idToken) == 1_000) + try #require(session.canContinueOffline && !session.isExpired) + let previousRequestCount = SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count + try #require(await fixture.login.restoreAccountSession(account)) + try #require(fixture.login.successMessage.contains("offline session")) + let refreshRequests = SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").dropFirst(previousRequestCount) + try #require(refreshRequests.count == JarvisRetryPolicy.gfnPC.maxRetries + 1) + #expect(refreshRequests.allSatisfy { $0.httpMethod == "POST" && $0.url?.path == "/token" }) + #expect(fixture.tokens(for: session) == originalTokens) + #expect(fixture.login.activeAccount === account) + } + + private func makeDetachedCatalog(fixture: DetachedAccountTestFixture, service: CatalogAccountServiceStub) -> CatalogViewModel { + CatalogViewModel( + account: fixture.detachedAccount, session: fixture.detachedSession, gameService: service, + discordPresence: OfflineCatalogPresence(), sessionRegistry: fixture.registry, + sessionResultStore: OPNGameSessionResultStore(), onRefreshAuth: { false } + ) + } + + private func credentials(for session: LoginSession) -> CatalogAccountServiceStub.SessionCredentials { + .init(accessToken: session.accessToken, idToken: session.idToken, userID: session.userId) + } + + private func membership() -> OPNSubscriptionInfo { + var subscription = OPNSubscriptionInfo() + subscription.membershipTier = "Ultimate" + subscription.entitledAudioChannelCount = 8 + subscription.isInGameSettingsPersistenceEntitled = true + subscription.dlssEntitlements = DlssEntitlements(superResolution: DlssCapability(isEntitled: true)) + return subscription + } + + private func resumableSession() -> OPNActiveSessionObject { + OPNActiveSessionObject(sessionId: "offline-owner-seat", appId: 123, status: 2, serverIp: "owner-seat.nvidiagrid.net", streamingBaseUrl: OPNGameService.defaultStreamingBaseUrl, signalingUrl: "") + } + + private func expectDetachedResumeIsAdmitted(_ model: CatalogViewModel, fixture: DetachedAccountTestFixture) throws { + try withPreservedPreferences([OPNSessionReadyAction.modeKey]) { + OPNSessionReadyAction.mode = .off + model.activeHomeSession = resumableSession() + model.resumeActiveHomeSession() + let ownerID = try #require(fixture.detachedAccount.resolveStableAccountID()) + let owned = try #require(fixture.registry.session(ownedBy: ownerID)) + defer { owned.cancelStreamLaunch() } + let configuration = try #require(owned.configuration) + #expect(configuration.accessToken == fixture.detachedSession.idToken) + #expect(configuration.owningAccountID == ownerID.rawValue) + #expect(configuration.launchEntitlements == OPNLaunchEntitlements(subscription: membership())) + #expect(configuration.resumeSessionID == "offline-owner-seat") + #expect(model.launchErrorMessage.isEmpty) + } + } +} diff --git a/Tests/GFN/Jarvis/JarvisTests.swift b/Tests/GFN/Jarvis/JarvisTests.swift index 5799752aa..36af0bcdd 100644 --- a/Tests/GFN/Jarvis/JarvisTests.swift +++ b/Tests/GFN/Jarvis/JarvisTests.swift @@ -187,6 +187,88 @@ private func jsonBody(_ request: URLRequest) throws -> [String: Any] { #expect(await authService.status == .authorizationError) } +@Test func jarvisAuthServiceFallsBackToTheRefreshTokenWhenTheClientTokenIsRejected() async throws { + let session = JarvisSession( + accessToken: "access", + refreshToken: "refresh", + userId: "user", + isAuthenticated: true, + clientToken: "client", + clientTokenExpiry: JarvisSession.currentEpochMs() + 3_600_000, + clientTokenExpiryLength: 3_600_000, + accessTokenExpiry: JarvisSession.currentEpochMs() - 1_000 + ) + let transport = SequencedJarvisTransport([ + .success((status: 401, json: ["error": "unauthorized"])), + .success((status: 200, json: ["access_token": "new-access", "refresh_token": "new-refresh", "expires_in": 3600])), + .success((status: 200, json: ["client_token": "new-client", "expires_in": 240])), + ]) + let service = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: transport, session: session) + let refreshed = try await service.refreshSession(force: true) + #expect(refreshed.accessToken == "new-access") + #expect(refreshed.clientToken == "new-client") + #expect(await transport.requestCount == 3) + + let rejecting = SequencedJarvisTransport([ + .success((status: 401, json: ["error": "unauthorized"])), + .success((status: 401, json: ["error": "unauthorized"])), + ]) + let deadService = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: rejecting, session: session) + await #expect(throws: JarvisAuthError.httpStatus(401)) { + _ = try await deadService.refreshSession(force: true) + } + #expect(await rejecting.requestCount == 2) + + let invalidGrant = SequencedJarvisTransport([ + .success((status: 400, json: ["error": "invalid_grant"])), + .success((status: 200, json: ["access_token": "new-access", "refresh_token": "new-refresh", "expires_in": 3600])), + .success((status: 200, json: ["client_token": "new-client", "expires_in": 240])), + ]) + let grantService = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: invalidGrant, session: session) + let regranted = try await grantService.refreshSession(force: true) + #expect(regranted.accessToken == "new-access") + #expect(await invalidGrant.requestCount == 3) +} + +private actor AccountSwitchingJarvisTransport: JarvisHTTPTransport { + private(set) var count = 0 + var onFirstRequest: (@Sendable () async -> Void)? + + func setOnFirstRequest(_ action: @escaping @Sendable () async -> Void) { onFirstRequest = action } + + func send(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) { + count += 1 + if count == 1 { await onFirstRequest?() } + let data = try JSONSerialization.data(withJSONObject: ["error": "unauthorized"]) + return (data, HTTPURLResponse(url: request.url ?? URL(string: "https://login.nvidia.com")!, statusCode: 401, httpVersion: nil, headerFields: nil)!) + } +} + +@Test func jarvisAuthServiceDoesNotFallBackOntoAnotherAccountAfterASwitchMidRefresh() async throws { + func makeSession(_ id: String) -> JarvisSession { + JarvisSession( + accessToken: "access-\(id)", + refreshToken: "refresh-\(id)", + userId: id, + isAuthenticated: true, + clientToken: "client-\(id)", + clientTokenExpiry: JarvisSession.currentEpochMs() + 3_600_000, + clientTokenExpiryLength: 3_600_000, + accessTokenExpiry: JarvisSession.currentEpochMs() - 1_000 + ) + } + let transport = AccountSwitchingJarvisTransport() + let service = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: transport, session: makeSession("a")) + let other = makeSession("b") + await transport.setOnFirstRequest { await service.setSession(other) } + await #expect(throws: CancellationError.self) { + _ = try await service.refreshSession(force: true) + } + // Account B keeps its client token and no refresh request was spent on it. + #expect(await service.session.clientToken == "client-b") + #expect(await transport.count == 1) +} + @Test func jarvisOAuthCallbackValidationMatchesVendorStateFlow() async throws { let service = JarvisAuthService(transport: MockJarvisTransport { _ in [:] }) let callback = try await service.parseCallback(query: "code=abc&state=expected", expectedState: "expected") diff --git a/Tests/Games/CatalogServiceIsolationTests.swift b/Tests/Games/CatalogServiceIsolationTests.swift new file mode 100644 index 000000000..9f55ff582 --- /dev/null +++ b/Tests/Games/CatalogServiceIsolationTests.swift @@ -0,0 +1,204 @@ +import Foundation +import Testing +@testable import OpenNOW + +private final class CatalogRetryCounter: @unchecked Sendable { + private let lock = NSLock() + private var attempts = 0 + + var count: Int { lock.withLock { attempts } } + + func next() -> Int { + lock.withLock { + attempts += 1 + return attempts + } + } +} + +@MainActor +@Suite struct CatalogServiceIsolationTests { + @Test func loadingAnotherCatalogKeepsEveryRequestOwnedByTheInitiatingAccount() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let counter = CatalogRetryCounter() + SessionManagerURLProtocol.install(host: "*", paths: ["/graphql", "/v2/serverInfo"]) { request in + Self.catalogResponse(request, counter: counter) + } + defer { SessionManagerURLProtocol.uninstall(host: "*") } + let first = makeCatalog(email: "catalog-a@example.test", userID: "user-a-\(UUID().uuidString)") + let second = makeCatalog(email: "catalog-b@example.test", userID: "user-b-\(UUID().uuidString)") + defer { + first.session.purgeTokens() + second.session.purgeTokens() + } + let firstService = try #require(first.gameService as? OPNGameService) + let secondService = try #require(second.gameService as? OPNGameService) + let firstBridge = try #require(first.launchBridge as? OPNGameLaunchBridge) + #expect((firstBridge.gameService as? OPNGameService) === firstService) + #expect(firstService !== secondService) + #expect(firstService !== OPNGameService.shared) + firstService.graphqlURL = "https://catalog-a.example.test/graphql" + firstService.persistedGraphqlURL = firstService.graphqlURL + firstService.providerStreamingBaseUrl = "https://provider-a.nvidiagrid.net/" + secondService.providerStreamingBaseUrl = "https://provider-b.nvidiagrid.net/" + first.configureCatalogService() + let favoritesSucceeded = await withCheckedContinuation { continuation in + firstService.fetchFavoriteGameObjects { success, _, _ in continuation.resume(returning: success) } + second.configureCatalogService() + first.selectGame(Self.game()) + first.toggleFavoriteSelectedGame() + first.confirmSelectedVariantOwned() + } + #expect(favoritesSucceeded) + try await waitUntil { first.errorMessage == "Ownership rejected for isolation test" && !first.isLoadingFavorites && counter.count == 3 } + let requests = SessionManagerURLProtocol.recordedRequests(host: "*") + let graphql = requests.filter { $0.url?.path == "/graphql" } + #expect(graphql.count >= 4) + #expect(graphql.allSatisfy { $0.url?.host == "catalog-a.example.test" }) + #expect(graphql.allSatisfy { $0.value(forHTTPHeaderField: "Authorization") == "GFNJWT \(first.session.idToken)" }) + let queries = SessionManagerURLProtocol.recordedJSONBodies(host: "*").compactMap { $0["query"] as? String } + #expect(queries.contains { $0.contains("AddFavoriteApp") }) + #expect(queries.contains { $0.contains("AddOwnedVariant") }) + #expect(requests.filter { $0.url?.path == "/v2/serverInfo" }.contains { $0.url?.host == "provider-a.nvidiagrid.net" }) + #expect(requests.filter { $0.url?.path == "/v2/serverInfo" }.contains { $0.url?.host == "provider-b.nvidiagrid.net" }) + #expect(firstService.providerStreamingBaseURL() == "https://provider-a.nvidiagrid.net/") + #expect(secondService.providerStreamingBaseURL() == "https://provider-b.nvidiagrid.net/") + } + } + + @Test func coalescedProviderCompletionsConfigureEveryRequestingService() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let providerID = "coalesced-\(UUID().uuidString)" + SessionManagerURLProtocol.install(host: "pcs.geforcenow.com") { _ in + Self.providerResponse(firstID: providerID, secondID: "unused") + } + defer { SessionManagerURLProtocol.uninstall(host: "pcs.geforcenow.com") } + let first = OPNGameService() + let second = OPNGameService() + var outcomes: [Bool] = [] + first.fetchProviderInfo(idpId: providerID) { success, _, _, _ in outcomes.append(success) } + second.fetchProviderInfo(idpId: providerID) { success, _, _, _ in outcomes.append(success) } + try await waitUntil { outcomes.count == 2 } + #expect(outcomes == [true, true]) + #expect(first.providerStreamingBaseURL() == "https://provider-a.nvidiagrid.net/") + #expect(second.providerStreamingBaseURL() == first.providerStreamingBaseURL()) + #expect(SessionManagerURLProtocol.recordedRequests(host: "pcs.geforcenow.com").count == 1) + } + } + + @Test func aLateProviderCompletionCannotReplaceANewerProviderContext() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let firstID = "provider-a-\(UUID().uuidString)" + let secondID = "provider-b-\(UUID().uuidString)" + SessionManagerURLProtocol.install(host: "pcs.geforcenow.com") { _ in + Self.providerResponse(firstID: firstID, secondID: secondID) + } + defer { SessionManagerURLProtocol.uninstall(host: "pcs.geforcenow.com") } + let changed = OPNGameService() + let unchanged = OPNGameService() + var outcomes: [Bool] = [] + changed.fetchProviderInfo(idpId: firstID) { success, _, _, _ in outcomes.append(success) } + changed.fetchProviderInfo(idpId: secondID) { success, _, _, _ in outcomes.append(success) } + unchanged.fetchProviderInfo(idpId: firstID) { success, _, _, _ in outcomes.append(success) } + try await waitUntil { outcomes.count == 3 } + #expect(outcomes.filter { !$0 }.count == 1) + #expect(changed.providerStreamingBaseURL() == "https://provider-b.nvidiagrid.net/") + #expect(unchanged.providerStreamingBaseURL() == "https://provider-a.nvidiagrid.net/") + let cached = OPNGameService() + cached.fetchProviderInfo(idpId: firstID) { success, _, _, _ in outcomes.append(success) } + try await waitUntil { outcomes.count == 4 } + #expect(outcomes.last == true) + #expect(cached.providerStreamingBaseURL() == "https://provider-a.nvidiagrid.net/") + #expect(SessionManagerURLProtocol.recordedRequests(host: "pcs.geforcenow.com").count == 2) + } + } + + @Test func launchPrefetchResolvesItsOwnProviderBeforeSendingAccountRequests() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let providerID = "prefetch-\(UUID().uuidString)" + SessionManagerURLProtocol.install(host: "*", paths: ["/v1/serviceUrls", "/v2/serverInfo", "/graphql"]) { request in + if request.url?.path == "/v1/serviceUrls" { + return Self.providerResponse(firstID: "unused", secondID: providerID) + } + if request.url?.path == "/v2/serverInfo" { + return SessionManagerURLProtocol.response(json: ["requestStatus": ["serverId": "GFN-PC"]]) + } + return SessionManagerURLProtocol.response(json: ["data": [ + "panels": [], + "apps": ["items": [], "pageInfo": ["hasNextPage": false]], + "filterGroupDefinitions": [], + "sortOrderDefinitions": [["id": "a_to_z", "orderBy": "sortName:ASC", "label": "A-Z"]], + ]]) + } + defer { SessionManagerURLProtocol.uninstall(host: "*") } + let prefetch = CatalogLaunchPrefetch() + prefetch.gameService.graphqlURL = "https://prefetch.example.test/graphql" + prefetch.gameService.persistedGraphqlURL = prefetch.gameService.graphqlURL + let userID = "prefetch-user-\(UUID().uuidString)" + var events: Set = [] + prefetch.start(accountIdentifier: userID, accessToken: "prefetch-access", idToken: "prefetch-id", idpId: providerID) + _ = prefetch.attach(accountIdentifier: userID) { event in + switch event { + case .panels(let kind, _), .panelsFailed(let kind, _): events.insert(kind.rawValue) + case .games(let kind, _), .gamesFailed(let kind, _): events.insert(kind.rawValue) + } + } + try await waitUntil { events.count == 4 } + prefetch.recordLaunchResultsAdopted(accountIdentifier: userID) + let requests = SessionManagerURLProtocol.recordedRequests(host: "*") + let streamingRequests = requests.filter { $0.url?.path == "/v2/serverInfo" } + #expect(!streamingRequests.isEmpty) + #expect(streamingRequests.allSatisfy { $0.url?.host == "provider-b.nvidiagrid.net" }) + #expect(streamingRequests.allSatisfy { $0.value(forHTTPHeaderField: "Authorization") == "GFNJWT prefetch-id" }) + #expect(prefetch.gameService !== OPNGameService.shared) + } + } + + private func makeCatalog(email: String, userID: String) -> CatalogViewModel { + let account = makeLoginAccountForTesting(email: email, userId: userID) + let session = makeLoginSessionForTesting(accountEmail: email, userId: userID) + return CatalogViewModel(account: account, session: session, onRefreshAuth: { false }) + } + + private func waitUntil(_ predicate: () -> Bool) async throws { + for _ in 0..<300 { + if predicate() { return } + try await Task.sleep(for: .milliseconds(10)) + } + try #require(predicate(), "The scripted catalog response did not arrive") + } + + nonisolated private static func catalogResponse(_ request: URLRequest, counter: CatalogRetryCounter) -> (Int, Data) { + if request.url?.path == "/v2/serverInfo" { + return SessionManagerURLProtocol.response(json: ["requestStatus": ["serverId": "GFN-PC"]]) + } + let query = SessionManagerURLProtocol.bodyData(from: request).flatMap { + (try? JSONSerialization.jsonObject(with: $0)) as? [String: Any] + }?["query"] as? String ?? "" + if query.contains("AddOwnedVariant") { + return SessionManagerURLProtocol.response(json: ["errors": [["message": "Ownership rejected for isolation test"]]]) + } + if query.contains("AddFavoriteApp") { + return SessionManagerURLProtocol.response(json: ["data": ["addFavoriteApp": ["app": ["id": "game-a"]]]]) + } + if counter.next() == 1 { + return SessionManagerURLProtocol.response(json: ["errors": [["message": "PersistedQueryNotFound", "extensions": ["code": "PERSISTED_QUERY_NOT_FOUND"]]]], status: 400) + } + return SessionManagerURLProtocol.response(json: ["data": ["panels": []]]) + } + + nonisolated private static func providerResponse(firstID: String, secondID: String) -> (Int, Data) { + SessionManagerURLProtocol.response(json: ["gfnServiceInfo": ["gfnServiceEndpoints": [ + ["idpId": firstID, "loginProvider": "Provider A", "streamingServiceUrl": "https://provider-a.nvidiagrid.net/"], + ["idpId": secondID, "loginProvider": "Provider B", "streamingServiceUrl": "https://provider-b.nvidiagrid.net/"], + ]]]) + } + + private static func game() -> OPNCatalogGameObject { + var game = OPNGameInfo() + game.id = "game-a" + game.title = "Account A Game" + game.variants = [OPNGameVariant(id: "123", appStore: "STEAM")] + return OPNCatalogGameObject(game: game) + } +} diff --git a/Tests/Games/DlssOverridesTests.swift b/Tests/Games/DlssOverridesTests.swift index d75910dcd..22386c2c4 100644 --- a/Tests/Games/DlssOverridesTests.swift +++ b/Tests/Games/DlssOverridesTests.swift @@ -342,7 +342,7 @@ import Testing frameGenerationMultipliers: [3, 4, 5, 6] ) ) - let settings: [String: Any] = ["dlssOverrides": [ + let settings: [String: Any] = [OPNLaunchEntitlements.settingsKey: OPNLaunchEntitlements(dlss: OPNStreamPreferences.loadDlssEntitlements()).storedDictionary, "dlssOverrides": [ "srMode": 6, "srModel": 2, "srScale": 80, @@ -382,7 +382,8 @@ import Testing rayReconstruction: DlssCapability(isEntitled: false, isUpsell: true) ) ) - let features = OPNSessionManager().requestedStreamingFeatures([:], hdrEnabled: false) + let settings: [String: Any] = [OPNLaunchEntitlements.settingsKey: OPNLaunchEntitlements(dlss: OPNStreamPreferences.loadDlssEntitlements()).storedDictionary] + let features = OPNSessionManager().requestedStreamingFeatures(settings, hdrEnabled: false) let overrides = try #require(features["dlssOverrides"] as? [String: Any]) #expect(overrides["srMode"] as? Int == DlssSuperResolutionMode.dlaa.rawValue) #expect(overrides["fgMode"] as? Int == 0) diff --git a/Tests/Games/SessionManagerTests.swift b/Tests/Games/SessionManagerTests.swift index f533625dd..16be0fb04 100644 --- a/Tests/Games/SessionManagerTests.swift +++ b/Tests/Games/SessionManagerTests.swift @@ -63,6 +63,7 @@ func expectReleaseCloudMatchRequestBody(_ requestData: [String: Any], metadata: var settings = minimalSettings() settings["networkTestSessionId"] = "stale-session-id" settings["enablePersistingInGameSettings"] = true + settings[OPNLaunchEntitlements.settingsKey] = OPNLaunchEntitlements(isInGameSettingsPersistenceEntitled: true).storedDictionary settings["partnerCustomData"] = "partner-data" settings["userAge"] = 21 settings["streamingQualityProfile"] = 4 diff --git a/Tests/Services/MainWindowAccountHandoffTests.swift b/Tests/Services/MainWindowAccountHandoffTests.swift new file mode 100644 index 000000000..ebf0f5c08 --- /dev/null +++ b/Tests/Services/MainWindowAccountHandoffTests.swift @@ -0,0 +1,90 @@ +import AppKit +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized) struct MainWindowAccountHandoffTests { + @Test func signInRisesAboveEveryObstructionUntilTheHandoffEnds() { + let main = makeWindow() + let detached = makeWindow() + let covering = NSWindow.Level(rawValue: NSWindow.Level.mainMenu.rawValue + 20) + detached.level = NSWindow.Level(rawValue: covering.rawValue + 4) + let handoff = OPNMainWindowAccountHandoff(mainWindow: { main }, coveringLevels: { [covering] }) + defer { + handoff.end() + main.close() + detached.close() + } + + handoff.begin(from: detached) + + #expect(main.level.rawValue > covering.rawValue) + #expect(main.level.rawValue > detached.level.rawValue) + #expect(main.isVisible) + handoff.end() + #expect(main.level == .normal) + #expect(detached.level.rawValue == covering.rawValue + 4) + } + + @Test func anAbsentMainSceneReceivesThePendingElevationWhenItAttaches() { + let main = makeWindow() + let covering = NSWindow.Level(rawValue: NSWindow.Level.mainMenu.rawValue + 8) + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [covering] }) + defer { + handoff.end() + main.close() + } + + handoff.begin(from: nil) + #expect(main.level == .normal) + handoff.attach(main) + + #expect(main.level.rawValue == covering.rawValue + 1) + handoff.end() + #expect(main.level == .normal) + } + + @Test func coveringLevelChangesAreObservedOnlyForTheLifetimeOfTheHandoff() { + let main = makeWindow() + var covering = NSWindow.Level(rawValue: NSWindow.Level.mainMenu.rawValue + 3) + let handoff = OPNMainWindowAccountHandoff(mainWindow: { main }, coveringLevels: { [covering] }) + defer { + handoff.end() + main.close() + } + handoff.begin(from: nil) + covering = NSWindow.Level(rawValue: covering.rawValue + 10) + NotificationCenter.default.post(name: OPNStreamBorderless.coveringStateDidChange, object: nil) + #expect(main.level.rawValue == covering.rawValue + 1) + + handoff.end() + NotificationCenter.default.post(name: OPNStreamBorderless.coveringStateDidChange, object: nil) + + #expect(main.level == .normal) + } + + @Test func ordinaryPresentationPreservesTheOriginalLevelAcrossRepeatedRequests() { + let main = makeWindow() + main.level = .floating + let handoff = OPNMainWindowAccountHandoff(mainWindow: { main }, coveringLevels: { [] }) + defer { + handoff.end() + main.close() + } + + handoff.begin(from: nil) + handoff.begin(from: nil) + #expect(main.level == .floating) + handoff.end() + + #expect(main.level == .floating) + } + + private func makeWindow() -> NSWindow { + _ = NSApplication.shared + let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 640, height: 480), + styleMask: [.titled, .closable], backing: .buffered, defer: false) + window.isReleasedWhenClosed = false + return window + } +} diff --git a/Tests/Settings/DetachedSessionModelTests.swift b/Tests/Settings/DetachedSessionModelTests.swift new file mode 100644 index 000000000..8a7018fff --- /dev/null +++ b/Tests/Settings/DetachedSessionModelTests.swift @@ -0,0 +1,123 @@ +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct DetachedSessionModelTests { + private func candidate(_ email: String, id: String?, hasSession: Bool = true) -> DetachedAccountCandidate { + DetachedAccountCandidate( + email: email, + displayName: email, + membershipTier: "Free", + accountID: id.flatMap { OPNAccountID(rawValue: $0) }, + hasSavedSession: hasSession + ) + } + + private let accountA = OPNAccountID(providerIdpId: "idp", vendorSubject: "user-a", localFallbackSubject: "a@example.com") + private let accountB = OPNAccountID(providerIdpId: "idp", vendorSubject: "user-b", localFallbackSubject: "b@example.com") + + @Test func anAccountThatOwnsAGameIsBusyAndOneWithoutASessionNeedsSignIn() throws { + let idA = try #require(accountA) + let idB = try #require(accountB) + let rows = DetachedSessionModel.rows( + candidates: [ + candidate("a@example.com", id: idA.rawValue), + candidate("b@example.com", id: idB.rawValue), + candidate("c@example.com", id: idB.rawValue, hasSession: false), + candidate("d@example.com", id: nil), + ], + ownedAccountIDs: [idA] + ) + #expect(rows.map(\.availability) == [.runningGame, .available, .signInRequired, .signInRequired]) + #expect(rows.map(\.availability.isSelectable) == [false, true, false, false]) + } + + @Test func theControllerChoiceBecomesThePresetAndTheKeptPads() { + let selection = DetachedSessionModel.selection(disabled: ["pad-a"], takesPadsFromOtherStreams: true) + #expect(selection.disabled == ["pad-a"]) + #expect(selection.takesPadsFromOtherStreams) + #expect(DetachedSessionModel.keptControllers(connected: ["pad-a", "pad-b"], disabled: ["pad-a"]) == ["pad-b"]) + } + + @Test func confirmingStoresTheSelectionUnderTheAccountAndMovesToTheCatalog() throws { + let idB = try #require(accountB) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: true) + viewModel.setController("pad-a", enabled: false) + let row = DetachedAccountRow(email: "b@example.com", displayName: "B", membershipTier: "Free", availability: .available) + + viewModel.confirm(row: row, accountID: idB) + + #expect(viewModel.phase == .catalog(email: "b@example.com")) + #expect(preset.peek(forAccount: idB.rawValue) == OPNStreamControllerSelection(disabled: ["pad-a"], takesPadsFromOtherStreams: true)) + } + + @Test func aBusyAccountCannotBeConfirmed() throws { + let idA = try #require(accountA) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: false) + let row = DetachedAccountRow(email: "a@example.com", displayName: "A", membershipTier: "Free", availability: .runningGame) + + viewModel.confirm(row: row, accountID: idA) + + #expect(viewModel.phase == .setup) + #expect(preset.peek(forAccount: idA.rawValue) == OPNStreamControllerSelection()) + } + + @Test func goingBackDropsTheSelectionForAStreamThatNeverStarted() throws { + let idB = try #require(accountB) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: false) + viewModel.setController("pad-a", enabled: false) + let row = DetachedAccountRow(email: "b@example.com", displayName: "B", membershipTier: "Free", availability: .available) + viewModel.confirm(row: row, accountID: idB) + + viewModel.backToSetup() + + #expect(viewModel.phase == .setup) + #expect(preset.peek(forAccount: idB.rawValue) == OPNStreamControllerSelection()) + } + + @Test func theHandOffToggleStartsOnOnlyWhenAnotherStreamIsRunning() { + #expect(DetachedSessionViewModel(preset: OPNStreamControllerPreset(), hasOtherStreams: true).takesPadsFromOtherStreams) + #expect(!DetachedSessionViewModel(preset: OPNStreamControllerPreset(), hasOtherStreams: false).takesPadsFromOtherStreams) + } + + @Test func changingAccountsDropsThePreviousAccountsUnconsumedControllerChoice() throws { + let idA = try #require(accountA) + let idB = try #require(accountB) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: false, registry: OPNGameSessionRegistry()) + viewModel.setController("pad-a", enabled: false) + viewModel.confirm(row: .init(email: "a@example.com", displayName: "A", membershipTier: "Free", availability: .available), accountID: idA) + + viewModel.confirm(row: .init(email: "b@example.com", displayName: "B", membershipTier: "Free", availability: .available), accountID: idB) + + #expect(preset.peek(forAccount: idA.rawValue) == OPNStreamControllerSelection()) + #expect(preset.peek(forAccount: idB.rawValue).disabled == ["pad-a"]) + #expect(viewModel.selectedEmail == "b@example.com") + viewModel.discardPendingSelection() + #expect(preset.peek(forAccount: idB.rawValue) == OPNStreamControllerSelection()) + } + + @Test func discardingAChoiceDoesNotEraseAReplacementMadeByAnotherWindow() throws { + let idB = try #require(accountB) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: false, registry: OPNGameSessionRegistry()) + viewModel.setController("pad-a", enabled: false) + viewModel.confirm(row: .init(email: "b@example.com", displayName: "B", membershipTier: "Free", availability: .available), accountID: idB) + let replacement = OPNStreamControllerSelection(disabled: ["pad-b"], takesPadsFromOtherStreams: true) + preset.store(replacement, forAccount: idB.rawValue) + + viewModel.discardPendingSelection() + + #expect(preset.peek(forAccount: idB.rawValue) == replacement) + } + + @Test func theCouchCoopFlagIsRegisteredAndOffByDefault() { + #expect(OPNLabs.flags.contains(OPNLabs.couchCoop)) + #expect(OPNLabs.couchCoop.id == "couchCoop") + #expect(OPNLabs.couchCoop.storageKey == "OpenNOW.Labs.couchCoop") + #expect(!OPNLabs.isEnabled(id: "a-flag-that-was-removed")) + } +} diff --git a/Tests/Settings/DetachedSessionPresentationTests.swift b/Tests/Settings/DetachedSessionPresentationTests.swift new file mode 100644 index 000000000..951b68239 --- /dev/null +++ b/Tests/Settings/DetachedSessionPresentationTests.swift @@ -0,0 +1,360 @@ +import AppKit +import SwiftUI +import Testing +import Vision +@testable import OpenNOW + +@MainActor +@Suite(.serialized) struct DetachedSessionPresentationTests { + @Test func dismissingAnUnrelatedConfirmationDoesNotDismissTheCurrentOwner() { + let presentation = OPNConfirmationPresentation() + let owner = UUID() + var dismissals = 0 + presentation.present(request, ownerID: owner, onDismiss: { dismissals += 1 }) + + presentation.dismiss(ownerID: UUID()) + + #expect(presentation.request != nil) + #expect(dismissals == 0) + presentation.dismiss(ownerID: owner) + #expect(presentation.request == nil) + #expect(dismissals == 1) + } + + @Test func replacingAConfirmationRetiresOnlyThePreviousOwnersState() { + let presentation = OPNConfirmationPresentation() + let previousOwner = UUID() + let currentOwner = UUID() + var previousDismissals = 0 + var currentDismissals = 0 + presentation.present(request, ownerID: previousOwner, onDismiss: { previousDismissals += 1 }) + presentation.present(request, ownerID: currentOwner, onDismiss: { currentDismissals += 1 }) + + presentation.dismiss(ownerID: previousOwner) + + #expect(previousDismissals == 1) + #expect(currentDismissals == 0) + #expect(presentation.request != nil) + presentation.dismiss(ownerID: currentOwner) + #expect(currentDismissals == 1) + } + + @Test func aDescendantConfirmationUsesItsDetachedRootsPresenter() async throws { + _ = NSApplication.shared + let confirmation = OPNConfirmationPresentation() + let title = "Detached confirmation \(UUID().uuidString)" + let hosting = NSHostingView(rootView: DetachedConfirmationFixture(title: title) + .modifier(DetachedSessionModalHost(confirmation: confirmation, reportIssue: OPNReportIssuePresentation())) + .environment(\.opnUIScale, 1.25)) + let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1100, height: 680), + styleMask: [.titled], backing: .buffered, defer: false) + window.isReleasedWhenClosed = false + window.contentView = hosting + window.orderFront(nil) + defer { + confirmation.dismiss() + if OPNConfirmationPresentation.shared.request?.title == title { OPNConfirmationPresentation.shared.dismiss() } + window.close() + } + for _ in 0..<100 { + if confirmation.request != nil { break } + try await Task.sleep(for: .milliseconds(10)) + } + + #expect(confirmation.request?.title == title) + #expect(OPNConfirmationPresentation.shared.request?.title != title) + } + + @Test(arguments: [CGFloat(1.25), CGFloat(1.5)]) + func aDetachedConfirmationDrawsOnlyInItsOwnRootAtLargerScales(_ scale: CGFloat) throws { + OPNDesign.applyTheme(accent: .cloudGreen, appearance: .dark, systemColorScheme: .dark) + let detached = OPNConfirmationPresentation() + detached.present(request, onDismiss: {}) + let other = OPNConfirmationPresentation() + let detachedCapture = try render( + confirmation: detached, report: OPNReportIssuePresentation(), scale: scale, + name: "detached-confirmation-\(scale).png" + ) + let otherCapture = try render( + confirmation: other, report: OPNReportIssuePresentation(), scale: scale, + name: "other-window-no-confirmation-\(scale).png" + ) + + #expect(detachedCapture.greenDominantPixelCount(in: GlassCapture.wholeImage, threshold: 40) > 500) + #expect(otherCapture.greenDominantPixelCount(in: GlassCapture.wholeImage, threshold: 40) == 0) + #expect(detachedCapture.greenDominantPixelCount(in: CGRect(x: 0, y: 0, width: 0.15, height: 1), threshold: 40) == 0) + #expect(detachedCapture.greenDominantPixelCount(in: CGRect(x: 0.85, y: 0, width: 0.15, height: 1), threshold: 40) == 0) + detached.dismiss() + } + + /// Offscreen evidence covers scoped modal chrome; the hosted test below verifies the form contents. + @Test(arguments: [CGFloat(1.25), CGFloat(1.5)]) + func aDetachedReportDrawsOnlyInItsOwnRootAtLargerScales(_ scale: CGFloat) throws { + OPNDesign.applyTheme(accent: .cloudGreen, appearance: .dark, systemColorScheme: .dark) + let detached = OPNReportIssuePresentation() + let other = OPNReportIssuePresentation() + detached.present(context: OPNIssueReportContext()) + let detachedCapture = try render( + confirmation: OPNConfirmationPresentation(), report: detached, scale: scale, + name: "detached-report-\(scale).png" + ) + let otherCapture = try render( + confirmation: OPNConfirmationPresentation(), report: other, scale: scale, + name: "other-window-no-report-\(scale).png" + ) + + #expect(detached.isPresented) + #expect(!other.isPresented) + #expect(detachedCapture.greenDominantPixelCount(in: GlassCapture.wholeImage, threshold: 40) > 500) + #expect(otherCapture.greenDominantPixelCount(in: GlassCapture.wholeImage, threshold: 40) == 0) + #expect(detachedCapture.greenDominantPixelCount(in: CGRect(x: 0, y: 0, width: 1, height: 0.01), threshold: 40) == 0) + #expect(detachedCapture.greenDominantPixelCount(in: CGRect(x: 0, y: 0.99, width: 1, height: 0.01), threshold: 40) == 0) + detached.dismiss() + } + + @Test( + .enabled(if: GlassEvidence.captureDirectory != nil), + .disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason)), + arguments: [CGFloat(1.25), CGFloat(1.5)] + ) + func aHostedDetachedReportExposesEditableFieldsAtLargerScales(_ scale: CGFloat) async throws { + _ = NSApplication.shared + OPNDesign.applyTheme(accent: .cloudGreen, appearance: .dark, systemColorScheme: .dark) + let report = OPNReportIssuePresentation() + report.present(context: OPNIssueReportContext()) + report.draft.category = .bug + report.draft.summary = HostedReportFixture.summary + report.draft.details = HostedReportFixture.details + report.draft.includesDiagnostics = false + defer { report.dismiss() } + + for viewport in HostedReportViewport.allCases { + let name = "detached-report-hosted-\(scale)-\(viewport.rawValue).png" + let capture = try await captureHostedReport(report, scale: scale, viewport: viewport, name: name) + try verifyCapturedReport(capture, viewport: viewport, name: name) + } + } + + private func captureHostedReport( + _ report: OPNReportIssuePresentation, scale: CGFloat, viewport: HostedReportViewport, name: String + ) async throws -> GlassCapture { + let suiteName = "DetachedSessionHostedReportTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suiteName)) + let probe = HostedReportWindowProbe() + defer { + probe.close() + defaults.removePersistentDomain(forName: suiteName) + } + let content = OPNDesign.Surface.app + .modifier(DetachedSessionModalHost(confirmation: OPNConfirmationPresentation(), reportIssue: report)) + .background(HostedReportWindowReader(probe: probe)) + .environment(\.opnUIScale, scale) + .environment(\.opnLiquidGlass, .off) + .environment(\.colorScheme, .dark) + .environment(\.locale, Locale(identifier: "en_US_POSIX")) + .transaction { $0.disablesAnimations = true } + .defaultAppStorage(defaults) + let captureTask = Task { + try await GlassEvidence.captureInWindow( + content, over: OPNDesign.Surface.app, + size: CGSize(width: 1100, height: 680), named: name + ) + } + do { + let form = try await probe.waitForNativeForm() + try form.reveal(viewport) + try form.verifyVisibleField(in: viewport) + let capture = try await captureTask.value + #expect(report.draft.summary == HostedReportFixture.summary) + #expect(report.draft.details == HostedReportFixture.details) + return capture + } catch { + captureTask.cancel() + _ = await captureTask.result + throw error + } + } + + private func verifyCapturedReport(_ capture: GlassCapture, viewport: HostedReportViewport, name: String) throws { + let request = VNRecognizeTextRequest() + request.recognitionLevel = .accurate + request.recognitionLanguages = ["en-US"] + request.usesLanguageCorrection = false + try VNImageRequestHandler(cgImage: capture.image, options: [:]).perform([request]) + let lines = (request.results ?? []).compactMap { $0.topCandidates(1).first?.string } + let recognized = lines.joined(separator: " ").lowercased().filter { $0.isLetter || $0.isNumber } + let expectedField = viewport == .top ? HostedReportFixture.summary : HostedReportFixture.details + let expected = expectedField.lowercased().filter { $0.isLetter || $0.isNumber } + #expect(recognized.contains(expected), "\(name) omitted its seeded field value. Recognized text: \(lines)") + #expect(recognized.contains("sendreport"), "\(name) lost its pinned footer. Recognized text: \(lines)") + if viewport == .bottom { + #expect(recognized.contains("includediagnostics"), "\(name) did not expose the bottom of the form. Recognized text: \(lines)") + } + } + + private var request: OPNConfirmationPresentation.Request { + .init(eyebrow: "DETACHED ACCOUNT", title: "Sign out of this account?", + message: "Only this saved account is signed out. The account in the main window stays connected.", + actions: [OPNConfirmationAction("CANCEL", role: .cancel) {}, OPNConfirmationAction("SIGN OUT", role: .destructive) {}]) + } + + private func render(confirmation: OPNConfirmationPresentation, report: OPNReportIssuePresentation, scale: CGFloat, name: String) throws -> GlassCapture { + let suiteName = "DetachedSessionPresentationTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suiteName)) + defer { defaults.removePersistentDomain(forName: suiteName) } + let renderer = ImageRenderer(content: OPNDesign.Surface.app + .modifier(DetachedSessionModalHost(confirmation: confirmation, reportIssue: report)) + .frame(width: 1100, height: 680) + .environment(\.opnUIScale, scale) + .environment(\.opnLiquidGlass, .off) + .transaction { $0.disablesAnimations = true } + .defaultAppStorage(defaults)) + renderer.scale = 2 + let image = try #require(renderer.cgImage) + if let directory = ProcessInfo.processInfo.environment["OPN_SNAPSHOT_DIR"] { + let bitmap = NSBitmapImageRep(cgImage: image) + let png = try #require(bitmap.representation(using: .png, properties: [:])) + try png.write(to: URL(fileURLWithPath: directory).appendingPathComponent(name)) + } + return GlassCapture(image: image) + } +} + +private struct DetachedConfirmationFixture: View { + let title: String + @State private var isPresented = true + + var body: some View { + OPNDesign.Surface.app + .opnConfirmation( + isPresented: $isPresented, eyebrow: "DETACHED", title: title, message: "Scoped to this window", + actions: [OPNConfirmationAction("CLOSE", role: .cancel) { isPresented = false }] + ) + } +} + +private enum HostedReportFixture { + static let summary = "Detached summary evidence" + static let details = "Detached description evidence" +} + +private enum HostedReportViewport: String, CaseIterable { + case top + case bottom +} + +@MainActor +private struct HostedReportNativeForm { + let summary: NSView + let details: NSView + let scrollView: NSScrollView + + func reveal(_ viewport: HostedReportViewport) throws { + let document = try #require(scrollView.documentView) + let clipView = scrollView.contentView + #expect(document.bounds.height > clipView.bounds.height, "The fixture must exercise vertical form scrolling") + let start = document.bounds.minY + let end = max(start, document.bounds.maxY - clipView.bounds.height) + let isShowingDocumentEnd = viewport == .bottom + let originY = isShowingDocumentEnd == document.isFlipped ? end : start + clipView.scroll(to: NSPoint(x: clipView.bounds.minX, y: originY)) + scrollView.reflectScrolledClipView(clipView) + scrollView.window?.contentView?.layoutSubtreeIfNeeded() + scrollView.window?.displayIfNeeded() + } + + func verifyVisibleField(in viewport: HostedReportViewport) throws { + #expect(HostedReportWindowProbe.editableValue(in: summary) == HostedReportFixture.summary) + #expect(HostedReportWindowProbe.editableValue(in: details) == HostedReportFixture.details) + let window = try #require(scrollView.window) + #expect(window.isVisible) + let field = viewport == .top ? summary : details + #expect(field.window === window) + #expect(!field.isHiddenOrHasHiddenAncestor) + #expect(!field.visibleRect.isEmpty, "The seeded field is outside the form viewport") + #expect(field.visibleRect.width >= field.bounds.width * 0.95, "The form clips the native field horizontally") + } +} + +@MainActor +private final class HostedReportWindowProbe { + private var window: NSWindow? + + func attach(_ window: NSWindow?) { + guard let window else { return } + window.isReleasedWhenClosed = false + self.window = window + } + + func close() { + guard let window else { return } + self.window = nil + window.orderOut(nil) + window.contentView = nil + window.close() + } + + func waitForNativeForm() async throws -> HostedReportNativeForm { + for _ in 0..<100 { + if let form = nativeForm() { return form } + try await Task.sleep(for: .milliseconds(10)) + } + return try #require(nativeForm(), "The hosted report did not install editable Summary/Description controls inside a scroll view") + } + + static func editableValue(in view: NSView) -> String? { + if let field = view as? NSTextField, field.isEditable { return field.stringValue } + if let editor = view as? NSTextView, editor.isEditable { return editor.string } + return nil + } + + private func nativeForm() -> HostedReportNativeForm? { + guard let content = window?.contentView else { return nil } + content.layoutSubtreeIfNeeded() + let inputs = editableInputs(in: content) + guard let summary = inputs.first(where: { Self.editableValue(in: $0) == HostedReportFixture.summary }), + let details = inputs.first(where: { Self.editableValue(in: $0) == HostedReportFixture.details }), + !summary.bounds.isEmpty, !details.bounds.isEmpty, + let scrollView = formScrollView(containing: summary, details: details) else { return nil } + return HostedReportNativeForm(summary: summary, details: details, scrollView: scrollView) + } + + private func editableInputs(in view: NSView) -> [NSView] { + let descendants = view.subviews.flatMap { editableInputs(in: $0) } + guard Self.editableValue(in: view) != nil else { return descendants } + return [view] + descendants + } + + private func formScrollView(containing summary: NSView, details: NSView) -> NSScrollView? { + var ancestor = summary.superview + while let view = ancestor { + if let scrollView = view as? NSScrollView, let document = scrollView.documentView, + details.isDescendant(of: document) { return scrollView } + ancestor = view.superview + } + return nil + } +} + +private struct HostedReportWindowReader: NSViewRepresentable { + let probe: HostedReportWindowProbe + + func makeNSView(context: Context) -> ProbeView { + let view = ProbeView(frame: .zero) + view.probe = probe + return view + } + + func updateNSView(_ nsView: ProbeView, context: Context) { + probe.attach(nsView.window) + } + + final class ProbeView: NSView { + weak var probe: HostedReportWindowProbe? + + override func viewDidMoveToWindow() { + super.viewDidMoveToWindow() + probe?.attach(window) + } + } +} diff --git a/Tests/Stream/ControllerBindingOutputLedgerTests.swift b/Tests/Stream/ControllerBindingOutputLedgerTests.swift index 211695446..b831ee711 100644 --- a/Tests/Stream/ControllerBindingOutputLedgerTests.swift +++ b/Tests/Stream/ControllerBindingOutputLedgerTests.swift @@ -69,4 +69,30 @@ import Testing let release = keyboard(ledger.events(for: key("plain", code: 0, down: false))) #expect(release.map(\.modifiers) == [.shift]) } + + @Test func focusReleaseClearsSharedDesktopOwnersWithoutTouchingGamepads() { + var ledger = ControllerBindingOutputLedger() + _ = ledger.events(for: key("second", code: 49, down: true, modifiers: .shift)) + _ = ledger.events(for: key("first", code: 49, down: true, modifiers: .control)) + _ = ledger.events(for: key("second", code: 0, down: true)) + let mousePress = UserInputEvent.mouse(.button(deviceID: "first", button: .left, isPressed: true, + timestamp: MediaTimestamp(nanoseconds: 1))) + _ = ledger.events(for: mousePress) + _ = ledger.events(for: .mouse(.button(deviceID: "second", button: .left, isPressed: true, + timestamp: MediaTimestamp(nanoseconds: 1)))) + let stamp = MediaTimestamp(nanoseconds: 2) + + let releases = ledger.releaseHeldOutputs(timestamp: stamp) + + #expect(keyboard(releases).map(\.keyCode) == [0, 49]) + #expect(keyboard(releases).allSatisfy { !$0.isPressed && $0.modifiers.isEmpty }) + #expect(releases.last == .mouse(.button(deviceID: "first", button: .left, isPressed: false, timestamp: stamp))) + #expect(releases.count == 3) + #expect(ledger.releaseHeldOutputs(timestamp: stamp).isEmpty) + #expect(ledger.events(for: key("first", down: false)).isEmpty) + #expect(ledger.events(for: key("second", down: false)).isEmpty) + let pad = UserInputEvent.gamepad(GamepadState(deviceID: "first", playerIndex: 0, buttons: .south, timestamp: stamp)) + #expect(ledger.events(for: pad) == [pad]) + #expect(ledger.events(for: mousePress) == [mousePress]) + } } diff --git a/Tests/Stream/CouchCoopBorderlessTests.swift b/Tests/Stream/CouchCoopBorderlessTests.swift new file mode 100644 index 000000000..55aca7d64 --- /dev/null +++ b/Tests/Stream/CouchCoopBorderlessTests.swift @@ -0,0 +1,182 @@ +import AppKit +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized) struct CouchCoopBorderlessTests { + private let screens = [ + NSRect(x: 0, y: 0, width: 1920, height: 1080), + NSRect(x: 1920, y: 0, width: 2560, height: 1440), + ] + + @Test func aWindowThatFillsAScreenCoversTheMenuBar() { + #expect(OPNStreamBorderless.coversWholeScreen(screens[1], screenFrames: screens)) + #expect(OPNStreamBorderless.level(for: screens[0], screenFrames: screens) == OPNStreamBorderless.coveringLevel) + #expect(OPNStreamBorderless.coveringLevel.rawValue > NSWindow.Level.mainMenu.rawValue) + } + + @Test func aTileThatSharesAScreenStaysAnOrdinaryWindow() { + let half = NSRect(x: 0, y: 0, width: 960, height: 1080) + #expect(!OPNStreamBorderless.coversWholeScreen(half, screenFrames: screens)) + #expect(OPNStreamBorderless.level(for: half, screenFrames: screens) == .normal) + } + + @Test func aReachableWindowSitsAboveTheCoveringStreams() { + #expect(OPNStreamBorderless.aboveCoveringLevel.rawValue > OPNStreamBorderless.coveringLevel.rawValue) + } + + @Test func borderlessAndTiledWindowsLetterboxAndNeverEnterNativeFullScreenOrPersistAFrame() { + let state = OPNStreamWindowLayoutState() + #expect(!state.releasesAspectLock && !state.suppressesNativeFullScreen && state.persistsFrame) + + state.isBorderless = true + #expect(state.releasesAspectLock && state.suppressesNativeFullScreen && !state.persistsFrame) + + state.isBorderless = false + state.isAutoTiled = true + #expect(state.releasesAspectLock && state.suppressesNativeFullScreen && !state.persistsFrame) + + state.isAutoTiled = false + #expect(!state.releasesAspectLock && !state.suppressesNativeFullScreen && state.persistsFrame) + } + + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func enteringBorderlessHidesTheButtonsKeepsTheStyleMaskAndLeavingRestoresTheWindow() async throws { + let suite = "OpenNOWTests.Borderless.\(UUID().uuidString)" + let window = OPNStreamWindowFactory.make(sessionID: UUID(), defaults: UserDefaults(suiteName: suite) ?? .standard) + defer { UserDefaults(suiteName: suite)?.removePersistentDomain(forName: suite) } + let before = window.frame + let level = window.level + let styleMask = window.styleMask + let target = NSRect(x: 100, y: 100, width: 800, height: 450) + + OPNStreamBorderless.enter(window, frame: target, screenFrames: screens) + try await Task.sleep(for: .milliseconds(200)) + + #expect(window.layoutState.isBorderless) + #expect(window.styleMask == styleMask) + #expect(window.standardWindowButton(.closeButton)?.isHidden == true) + #expect(window.frame == target) + #expect(window.level == .normal) + + OPNStreamBorderless.exit(window) + try await Task.sleep(for: .milliseconds(200)) + + #expect(!window.layoutState.isBorderless) + #expect(window.standardWindowButton(.closeButton)?.isHidden == false) + #expect(window.level == level) + #expect(window.frame == before) + #expect(window.borderlessRestoreState == nil) + } + + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func aBorderlessWindowDoesNotWriteItsFrameToThePlacementStore() async throws { + let suite = "OpenNOWTests.BorderlessStore.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + defer { defaults.removePersistentDomain(forName: suite) } + let window = OPNStreamWindowFactory.make(sessionID: UUID(), defaults: defaults) + OPNStreamWindowFrameStore.forget(isPictureInPicture: false, defaults: defaults) + + window.layoutState.isAutoTiled = true + window.setFrame(NSRect(x: 10, y: 10, width: 700, height: 400), display: false) + try await Task.sleep(for: .milliseconds(200)) + + #expect(OPNStreamWindowFrameStore.rememberedFrame(isPictureInPicture: false, defaults: defaults) == nil) + window.stopPersistingFrame() + } + + @Test func aWindowMovedAwayFromItsLayoutFrameIsNotAtIt() { + let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) + #expect(!window.isAtLayoutFrame) + window.setFrameFromLayout(NSRect(x: 50, y: 50, width: 640, height: 360)) + #expect(window.isAtLayoutFrame) + window.setFrame(NSRect(x: 80, y: 50, width: 640, height: 360), display: false) + #expect(!window.isAtLayoutFrame) + window.clearLayoutFrame() + #expect(!window.isAtLayoutFrame) + } + + @Test func tileCoverageDividesTheVisibleAreaOrTheWholeFrame() { + let frames = [NSRect(x: 0, y: 0, width: 1920, height: 1080)] + let visible = [NSRect(x: 0, y: 70, width: 1920, height: 985)] + #expect(OPNStreamTileCoverage.visibleArea.areas(frames: frames, visibleFrames: visible) == visible) + #expect(OPNStreamTileCoverage.fullScreen.areas(frames: frames, visibleFrames: visible) == frames) + #expect(OPNStreamTileCoverage.visibleArea.flipped == .fullScreen) + #expect(OPNStreamTileCoverage.fullScreen.flipped == .visibleArea) + } + + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func aTileThatCoversTheMenuBarIsRaisedEvenWhenItSharesAScreen() async throws { + let suite = "OpenNOWTests.BorderlessCover.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + defer { defaults.removePersistentDomain(forName: suite) } + let window = OPNStreamWindowFactory.make(sessionID: UUID(), defaults: defaults) + let half = NSRect(x: 0, y: 0, width: 960, height: 1080) + + OPNStreamBorderless.enter(window, frame: half, coversMenuBar: true, screenFrames: screens) + try await Task.sleep(for: .milliseconds(200)) + #expect(window.level == OPNStreamBorderless.coveringLevel) + + OPNStreamBorderless.enter(window, frame: half, coversMenuBar: false, screenFrames: screens) + try await Task.sleep(for: .milliseconds(200)) + #expect(window.level == .normal) + OPNStreamBorderless.exit(window) + try await Task.sleep(for: .milliseconds(200)) + } + + @Test func enteringAndLeavingBorderlessAnnouncesTheCoveringStateChange() async throws { + let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) + var posts = 0 + let token = NotificationCenter.default.addObserver(forName: OPNStreamBorderless.coveringStateDidChange, object: window, queue: .main) { _ in + MainActor.assumeIsolated { posts += 1 } + } + defer { NotificationCenter.default.removeObserver(token) } + + OPNStreamBorderless.enter(window, frame: NSRect(x: 0, y: 0, width: 640, height: 360), screenFrames: screens) + try await Task.sleep(for: .milliseconds(200)) + OPNStreamBorderless.exit(window) + try await Task.sleep(for: .milliseconds(200)) + + #expect(posts == 2) + } + + /// A tile whose placement is still waiting on the geometry gate has no layout frame yet. A move + /// that lands in that gap is not the reader taking the window. + @Test func aMoveBeforeTheFirstTileLandsKeepsTheWindowTiled() { + let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) + window.layoutState.isAutoTiled = true + + #expect(!OPNStreamWindowPresenter.noteFrameChange(window)) + + #expect(window.layoutState.isAutoTiled) + #expect(!window.layoutState.isManuallyPlaced) + } + + @Test func aMoveAwayFromAPlacedTileHandsTheWindowToTheReader() { + let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) + window.layoutState.isAutoTiled = true + window.setFrameFromLayout(NSRect(x: 50, y: 50, width: 640, height: 360)) + + #expect(!OPNStreamWindowPresenter.noteFrameChange(window)) + #expect(window.layoutState.isAutoTiled) + + window.setFrame(NSRect(x: 90, y: 50, width: 640, height: 360), display: false) + #expect(OPNStreamWindowPresenter.noteFrameChange(window)) + + #expect(!window.layoutState.isAutoTiled) + #expect(window.layoutState.isManuallyPlaced) + } + + @Test func theRelaunchHelperWaitsForThisProcessAndOpensTheBundleFromAnArgument() { + let arguments = OPNAppRelaunch.helperArguments(pid: 4242) + #expect(arguments.first == "-c") + #expect(arguments[1].contains("kill -0 4242")) + #expect(arguments[1].contains("open \"$0\"")) + #expect(arguments.last == Bundle.main.bundlePath) + } + + @Test func onlyTheCouchCoopFlagAsksForARestart() { + #expect(OPNLabs.couchCoop.requiresRestart) + #expect(!OPNLabs.clipboardCapture.requiresRestart) + } +} diff --git a/Tests/Stream/CouchCoopInputRoutingTests.swift b/Tests/Stream/CouchCoopInputRoutingTests.swift new file mode 100644 index 000000000..3e5e3b06d --- /dev/null +++ b/Tests/Stream/CouchCoopInputRoutingTests.swift @@ -0,0 +1,46 @@ +import Testing +@testable import OpenNOW + +@MainActor @Suite struct CouchCoopInputRoutingTests { + private let gamepad = UserInputEvent.gamepad(GamepadState(deviceID: "pad", playerIndex: 0, timestamp: MediaTimestamp(nanoseconds: 0))) + private let keyboard = UserInputEvent.keyboard(KeyboardEvent(deviceID: "keyboard", keyCode: 0, scanCode: 0, modifiers: [], isPressed: true, timestamp: MediaTimestamp(nanoseconds: 0))) + private let mouse = UserInputEvent.mouse(.button(deviceID: "mouse", button: .left, isPressed: true, timestamp: MediaTimestamp(nanoseconds: 0))) + + @Test func padsPlayInTheBackgroundOnlyWithTheFlagAndASecondStream() { + #expect(!OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: false, liveStreamCount: 2)) + #expect(!OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: true, liveStreamCount: 1)) + #expect(!OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: true, liveStreamCount: 0)) + #expect(OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: true, liveStreamCount: 2)) + #expect(OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: true, liveStreamCount: 4)) + } + + @Test func aBackgroundWindowAcceptsItsPadsButNeverTheKeyboardOrMouse() { + #expect(NativeNVSTHostViewModel.acceptsWhileNotFrontmost(gamepad, isPictureInPictureMode: false, allowsBackgroundGamepad: true)) + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(keyboard, isPictureInPictureMode: false, allowsBackgroundGamepad: true)) + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(mouse, isPictureInPictureMode: false, allowsBackgroundGamepad: true)) + } + + @Test func withoutCouchCoopOrPictureInPictureNothingPassesWhileNotFrontmost() { + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(gamepad, isPictureInPictureMode: false, allowsBackgroundGamepad: false)) + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(gamepad, isPictureInPictureMode: false)) + #expect(NativeNVSTHostViewModel.acceptsWhileNotFrontmost(gamepad, isPictureInPictureMode: true)) + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(keyboard, isPictureInPictureMode: true, allowsBackgroundGamepad: true)) + } + + @Test func mappingsFollowABackgroundPadWhileTheOverlayAndRemoteGatesStillApply() { + for overlay in [false, true] { + for remote in [false, true] { + let allowed = ControllerMappingFocusPolicy.allowsMappings( + appIsActive: false, windowIsKey: false, remoteInputEnabled: remote, + overlayCapturesInput: overlay, allowsBackgroundPads: true + ) + #expect(allowed == (remote && !overlay)) + let withoutBackground = ControllerMappingFocusPolicy.allowsMappings( + appIsActive: false, windowIsKey: false, remoteInputEnabled: remote, + overlayCapturesInput: overlay, allowsBackgroundPads: false + ) + #expect(!withoutBackground) + } + } + } +} diff --git a/Tests/Stream/CouchCoopNativeInputFixture.swift b/Tests/Stream/CouchCoopNativeInputFixture.swift new file mode 100644 index 000000000..5a676672c --- /dev/null +++ b/Tests/Stream/CouchCoopNativeInputFixture.swift @@ -0,0 +1,225 @@ +import AppKit +import GameController +import os +import Testing +@testable import OpenNOW + +@MainActor +final class CouchCoopInputFocus { + var isApplicationActive = true + var isWindowKey = true +} + +@MainActor +final class CouchCoopFocusReleaseRecorder { + private(set) var sequence: [String] = [] + private(set) var releaseCaptureStates: [Bool] = [] + + init(view: NativeStreamView) { + let inputHandler = view.onInputEvent + view.onInputEvent = { [weak self, weak view] event in + switch event { + case .keyboard(let key) where !key.isPressed: self?.sequence.append("key-release") + case .mouse(.button(_, _, false, _)): self?.sequence.append("mouse-release") + default: break + } + if NativeNVSTInputDispatcher.isNeutralizing(event) { self?.releaseCaptureStates.append(view?.isPointerLocked == true) } + inputHandler?(event) + } + let pointerHandler = view.onPointerLockChanged + view.onPointerLockChanged = { [weak self] isLocked in + if !isLocked { self?.sequence.append("unlock") } + pointerHandler?(isLocked) + } + } +} + +actor CouchCoopNativeInputRecorder { + private var inputs: [UserInputEvent] = [] + private var barrierSnapshots: [String: [UserInputEvent]] = [:] + private var barrierWaiters: [String: CheckedContinuation<[UserInputEvent], Error>] = [:] + + func append(_ input: NativeNVSTInput) { + guard case .event(let event) = input else { return } + if case .text("input-test-barrier", let marker, _) = event { + guard let waiter = barrierWaiters.removeValue(forKey: marker) else { + barrierSnapshots[marker] = inputs + return + } + waiter.resume(returning: inputs) + return + } + inputs.append(event) + } + + func snapshot(at marker: String) async throws -> [UserInputEvent] { + try Task.checkCancellation() + if let snapshot = barrierSnapshots.removeValue(forKey: marker) { return snapshot } + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { continuation in + guard !Task.isCancelled else { + continuation.resume(throwing: CancellationError()) + return + } + barrierWaiters[marker] = continuation + } + } onCancel: { + Task { await self.cancelBarrier(marker) } + } + } + + private func cancelBarrier(_ marker: String) { + barrierSnapshots.removeValue(forKey: marker) + barrierWaiters.removeValue(forKey: marker)?.resume(throwing: CancellationError()) + } +} + +@MainActor +final class CouchCoopNativeInputFixture { + let focus: CouchCoopInputFocus + let window: NSWindow + let view: NativeStreamView + let model: NativeNVSTHostViewModel + let store: ControllerMappingStore + let dispatcher: NativeNVSTInputDispatcher + private let recorder: CouchCoopNativeInputRecorder + private let nativePollState = NativeGamepadPollState() + private let defaults: UserDefaults + private let defaultsDomain: String + private(set) var cursorAssociations: [Bool] = [] + + init() throws { + let defaultsDomain = "CouchCoopNativeInputTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: defaultsDomain)) + let store = ControllerMappingStore(defaults: defaults) + for family in [ControllerFamily.steam, .generic] { + var profile = store.createProfile(named: "Input ownership", family: family) + profile.bindings[.faceA] = .mouseButton(.left) + profile.bindings[.faceB] = .keyboardKey(keyCode: 49, modifiers: .shift) + profile.bindings[.faceX] = .gamepadChord(ControllerButtonChord(buttons: .north)) + profile.bindings[.faceY] = .streamCommand(.togglePointerCapture) + store.updateProfile(profile) + } + let window = NSWindow(contentRect: NSRect(x: 100, y: 100, width: 1280, height: 720), + styleMask: .borderless, backing: .buffered, defer: false) + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [] }, steamDeviceIDs: { [] })) + let view = NativeStreamView(frame: NSRect(x: 0, y: 0, width: 1280, height: 720), + mappingProvider: store, controllerRegistry: registry) + let model = NativeNVSTHostViewModel( + configuration: StreamLaunchConfiguration(title: "Input ownership", applicationID: "input-test", + accessToken: "unused", accountLinked: true, selectedStore: "steam"), + sessionProvider: StubNativeNVSTSessionProvider(), preventDisplaySleep: false, onProgress: nil, + onEnd: { _, _, _ in } + ) + let recorder = CouchCoopNativeInputRecorder() + self.focus = CouchCoopInputFocus() + self.window = window + self.view = view + self.model = model + self.store = store + self.recorder = recorder + self.defaults = defaults + self.defaultsDomain = defaultsDomain + self.dispatcher = NativeNVSTInputDispatcher { input in await recorder.append(input) } + configureInputOwnership() + } + + private func configureInputOwnership() { + window.isReleasedWhenClosed = false + view.inputFocusProvider = { [focus] in (focus.isApplicationActive, focus.isWindowKey) } + view.hidesCursorWhilePointerLocked = false + view.cursorLocationProvider = { CGPoint(x: 740, y: 460) } + view.cursorWarpHandler = { _ in } + window.contentView = view + view.gamepadMonitor.stop() + view.gamepadMonitor.pollingAllowed = true + view.allowsBackgroundGamepadInput = true + view.updateControllerMappingFocus() + model.nativeView = view + model.path = NativeNVSTStreamingPath(sessionProvider: StubNativeNVSTSessionProvider(), transport: RecordingNativeTransport()) + model.isConnected = true + model.inputDispatcher = dispatcher + model.configureInput(for: view) + view.onPointerLockChanged = { [weak model] in model?.pointerLocked = $0 } + view.cursorAssociationHandler = { [weak self] isAssociated in + self?.cursorAssociations.append(isAssociated) + return .success + } + } + + func configureNativeControllers(_ controllers: [(deviceID: InputDeviceID, controller: GCController, profile: ControllerMappingProfile?)]) { + let monitor = view.gamepadMonitor + let state = nativePollState + monitor.pollingQueue.sync { + state.cachedControllers = controllers.map { $0.controller } + state.controllerSlots = Dictionary(uniqueKeysWithValues: controllers.enumerated().map { + (ObjectIdentifier($0.element.controller), $0.offset) + }) + let configurations = Dictionary(uniqueKeysWithValues: controllers.enumerated().map { + (ObjectIdentifier($0.element.controller), NativeControllerMappingConfiguration( + deviceID: $0.element.deviceID, playerIndex: $0.offset, profile: $0.element.profile + )) + }) + _ = state.configureMappings(configurations) + state.hidSnapshots = { [:] } + } + } + + func pollNativeControllers() { + let monitor = view.gamepadMonitor + let state = nativePollState + let captured = OSAllocatedUnfairLock(initialState: [UserInputEvent]()) + let commands = monitor.pollingQueue.sync { + state.pollAndEmit(onEvents: { events in captured.withLock { $0 += events } }, onBatteryChange: { _ in }) + monitor.pollState.pendingEvents.append(contentsOf: captured.withLock { $0 }) + return state.takePendingCommands() + } + for command in commands { monitor.onStreamCommand?(command) } + monitor.dispatchPendingInput() + } + + var routingDiagnostics: String { + let monitor = view.gamepadMonitor + let state = nativePollState + let slots = monitor.pollingQueue.sync { + "scriptedSlots=\(state.controllerSlots.values.sorted()) liveSlots=\(monitor.pollState.controllerSlots.values.sorted())" + } + let active = view.activeGamepadStates.keys.sorted().compactMap { playerIndex -> String? in + guard let pad = view.activeGamepadStates[playerIndex] else { return nil } + return "\(playerIndex):\(pad.deviceID.rawValue):buttons=\(pad.buttons.rawValue):leftX=\(pad.leftStickX)" + } + return "\(slots) active=\(active) mappings=\(monitor.mappingsEnabled) polling=\(monitor.pollingAllowed)" + } + + func sendSteam(_ snapshot: ControllerInputSnapshot) { + view.gamepadMonitor.processSteamSnapshot(deviceID: "steam-input-test", playerIndex: 2, snapshot: snapshot) + } + + func recordedInputs() async throws -> [UserInputEvent] { + let marker = UUID().uuidString + dispatcher.enqueue(.text(deviceID: "input-test-barrier", value: marker, timestamp: MediaTimestamp(nanoseconds: 0))) + do { + return try await recorder.snapshot(at: marker) + } catch { + Issue.record("Input barrier cancelled: pending=\(dispatcher.pendingInputCount) \(routingDiagnostics)") + throw error + } + } + + func close() { + view.remoteInputEnabled = false + view.gamepadMonitor.stop() + view.setPointerLocked(false) + view.onInputEvent = nil + view.onCommand = nil + model.cancelNativeShortcutTasks() + model.isConnected = false + model.nativeView = nil + model.path = nil + model.inputDispatcher = nil + dispatcher.cancel() + window.contentView = nil + window.close() + defaults.removePersistentDomain(forName: defaultsDomain) + } +} diff --git a/Tests/Stream/CouchCoopNativeInputTests.swift b/Tests/Stream/CouchCoopNativeInputTests.swift new file mode 100644 index 000000000..c01726311 --- /dev/null +++ b/Tests/Stream/CouchCoopNativeInputTests.swift @@ -0,0 +1,303 @@ +import AppKit +import GameController +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized, .streamLifecycleExclusive, .timeLimit(.minutes(1))) +struct CouchCoopNativeInputTests { + @Test func focusLossReleasesSharedMappedDesktopHoldsBeforeUnlockWithoutResettingPads() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + let mapped = GCController.withExtendedGamepad() + let physical = GCController.withExtendedGamepad() + let mappedPad = try #require(mapped.extendedGamepad) + let physicalPad = try #require(physical.extendedGamepad) + fixture.configureNativeControllers([ + ("mapped-native", mapped, fixture.store.profile(for: .generic)), + ("physical-native", physical, nil), + ]) + fixture.view.setPointerLocked(true) + mappedPad.buttonA.setValue(1) + mappedPad.buttonB.setValue(1) + mappedPad.buttonX.setValue(1) + physicalPad.buttonA.setValue(1) + physicalPad.leftThumbstick.xAxis.setValue(0.5) + fixture.pollNativeControllers() + fixture.sendSteam(ControllerInputSnapshot(buttons: [.south, .east, .west])) + fixture.view.gamepadMonitor.refreshInputState() + let initial = try await fixture.recordedInputs() + #expect(desktopInputs(initial).count == 2) + #expect(gamepads(initial).count == 3, "\(fixture.routingDiagnostics); received=\(gamepads(initial))") + #expect(fixture.view.pressedMouseButtons.isEmpty) + #expect(fixture.view.pressedKeyboardEvents.isEmpty) + + let releases = CouchCoopFocusReleaseRecorder(view: fixture.view) + fixture.focus.isWindowKey = false + fixture.view.handleFocusLoss() + fixture.view.handleFocusLoss() + + let released = try await fixture.recordedInputs() + #expect(releases.sequence == ["key-release", "mouse-release", "unlock"]) + #expect(releases.releaseCaptureStates == [true, true]) + #expect(desktopInputs(Array(released.dropFirst(initial.count))).count == 2) + #expect(gamepads(released) == gamepads(initial)) + #expect(fixture.view.activeGamepadStates[0]?.buttons == .north) + #expect(fixture.view.activeGamepadStates[1]?.leftStickX == 0.5) + #expect(fixture.view.activeGamepadStates[2]?.buttons == .north) + #expect(fixture.view.gamepadMonitor.mappingsEnabled) + + fixture.view.gamepadMonitor.refreshInputState() + physicalPad.leftThumbstick.xAxis.setValue(0.75) + fixture.pollNativeControllers() + fixture.sendSteam(ControllerInputSnapshot(buttons: [.south, .east, .west], leftTrigger: 0.75)) + let background = Array(try await fixture.recordedInputs().dropFirst(released.count)) + #expect(desktopInputs(background).isEmpty) + #expect(gamepads(background).contains { $0.deviceID == "physical-native" && $0.leftStickX == 0.75 }, "\(fixture.routingDiagnostics)") + #expect(gamepads(background).contains { $0.deviceID == "mapped-native" && $0.buttons == .north }, "\(fixture.routingDiagnostics)") + #expect(gamepads(background).contains { $0.deviceID == "steam-input-test" && $0.buttons == .north }) + #expect(gamepads(background).allSatisfy { !$0.buttons.isEmpty }) + } + + @Test func returningFocusDoesNotReplayDesktopPressesHeldInTheBackground() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + let held = ControllerInputSnapshot(buttons: [.south, .east, .west]) + fixture.view.setPointerLocked(true) + fixture.sendSteam(held) + fixture.focus.isWindowKey = false + fixture.view.handleFocusLoss() + fixture.sendSteam(ControllerInputSnapshot()) + fixture.sendSteam(held) + let background = try await fixture.recordedInputs() + #expect(desktopInputs(background).count == 4) + + fixture.focus.isWindowKey = true + fixture.view.locksPointerWhenRelativeModeSelected = true + fixture.view.handleFocusGain() + fixture.sendSteam(held) + let restored = try await fixture.recordedInputs() + #expect(desktopInputs(restored) == desktopInputs(background)) + #expect(fixture.view.isPointerLocked) + fixture.sendSteam(ControllerInputSnapshot()) + fixture.sendSteam(held) + let pressedAgain = try await fixture.recordedInputs() + #expect(desktopInputs(Array(pressedAgain.dropFirst(restored.count))).count == 2) + } + + @Test func aMappedRelativeReleaseReachesTheHostAfterUnlockWithoutRecapturing() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + fixture.view.setPointerLocked(true) + fixture.sendSteam(ControllerInputSnapshot(buttons: [.south, .east])) + let pressed = try await fixture.recordedInputs() + #expect(desktopInputs(pressed).count == 2) + fixture.view.setPointerLocked(false) + fixture.focus.isWindowKey = false + fixture.sendSteam(ControllerInputSnapshot()) + let released = Array(try await fixture.recordedInputs().dropFirst(pressed.count)) + + #expect(desktopInputs(released).count == 2) + #expect(desktopInputs(released).allSatisfy(NativeNVSTInputDispatcher.isNeutralizing)) + #expect(fixture.view.mouseInputMode == .relative) + #expect(!fixture.view.isPointerLocked) + #expect(fixture.cursorAssociations == [false, true]) + } + + @Test(arguments: BlockingOverlay.allCases) + func overlayTransitionsReleaseMappedHoldsButRejectNewGameInput(_ overlay: BlockingOverlay) async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + fixture.view.setPointerLocked(true) + fixture.sendSteam(ControllerInputSnapshot(buttons: [.south, .east, .west])) + let initial = try await fixture.recordedInputs() + switch overlay { + case .hud: + fixture.model.unifiedHUDVisible = true + fixture.view.remoteInputEnabled = false + case .quitMenu: + fixture.model.streamControlsVisible = true + fixture.view.remoteInputEnabled = false + case .keyboard: + fixture.view.localOverlayCapturesInput = true + fixture.view.setPointerLocked(false) + } + let transition = Array(try await fixture.recordedInputs().dropFirst(initial.count)) + #expect(desktopInputs(transition).count == 2) + #expect(desktopInputs(transition).allSatisfy(NativeNVSTInputDispatcher.isNeutralizing)) + #expect(!fixture.view.gamepadMonitor.mappingsEnabled) + let stamp = MediaTimestamp(nanoseconds: 1) + fixture.model.routeInputEvent(.mouse(.button(deviceID: "blocked", button: .right, isPressed: true, timestamp: stamp)), view: fixture.view) + fixture.model.routeInputEvent(.keyboard(KeyboardEvent(deviceID: "blocked", keyCode: 0, scanCode: 0, isPressed: true, timestamp: stamp)), view: fixture.view) + fixture.model.routeInputEvent(.gamepad(GamepadState(deviceID: "blocked", playerIndex: 3, buttons: .south, timestamp: stamp)), view: fixture.view) + let blocked = try await fixture.recordedInputs() + #expect(blocked.count == initial.count + transition.count) + #expect(!fixture.view.isPointerLocked) + #expect(fixture.cursorAssociations == [false, true]) + } + + @Test func backgroundSteamAndNativeCaptureCommandsNeverTakeTheOtherStreamsPointer() async throws { + let background = try CouchCoopNativeInputFixture() + let foreground = try CouchCoopNativeInputFixture() + defer { + background.close() + foreground.close() + } + background.focus.isWindowKey = false + background.view.handleFocusLoss() + foreground.view.setPointerLocked(true) + let native = GCController.withExtendedGamepad() + let pad = try #require(native.extendedGamepad) + background.configureNativeControllers([("native-command", native, background.store.profile(for: .generic))]) + background.sendSteam(ControllerInputSnapshot(buttons: [.north, .west])) + pad.buttonY.setValue(1) + pad.buttonX.setValue(1) + background.pollNativeControllers() + background.model.handleNativeCommand(.togglePointerCapture) + background.view.setRemoteCursorVisible(false) + background.view.cursorPolicy = .stream + + #expect(!background.view.isCursorCaptured) + #expect(!background.view.manualPointerCaptureOverride) + #expect(background.cursorAssociations.isEmpty) + #expect(foreground.view.isPointerLocked) + #expect(foreground.cursorAssociations == [false]) + let admitted = gamepads(try await background.recordedInputs()) + #expect(admitted.contains { $0.deviceID == "steam-input-test" && $0.buttons == .north }) + #expect(admitted.contains { $0.deviceID == "native-command" && $0.buttons == .north }) + + background.sendSteam(ControllerInputSnapshot(buttons: .quickAccess)) + #expect(background.model.unifiedHUDVisible) + #expect(!background.view.remoteInputEnabled) + #expect(foreground.view.isPointerLocked) + #expect(background.cursorAssociations.isEmpty) + let afterQuickAccess = gamepads(try await background.recordedInputs()) + #expect(afterQuickAccess.allSatisfy { !$0.buttons.contains(.quickAccess) }) + } + + @Test(arguments: CaptureBlocker.allCases) + func everyCaptureEntryPointHonorsOwnershipWhileUnlockRemainsPossible(_ blocker: CaptureBlocker) throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + fixture.view.setPointerLocked(true) + switch blocker { + case .inactiveApplication: fixture.focus.isApplicationActive = false + case .otherKeyWindow: fixture.focus.isWindowKey = false + case .remoteInputDisabled: fixture.view.remoteInputEnabled = false + case .keyboardOverlay: fixture.view.localOverlayCapturesInput = true + case .pictureInPicture: fixture.view.isPictureInPictureMode = true + case .actualNonKeyWindow: + fixture.view.inputFocusProvider = nil + #expect(!fixture.window.isKeyWindow) + } + fixture.view.setPointerLocked(false) + #expect(!fixture.view.isCursorCaptured) + #expect(fixture.cursorAssociations == [false, true]) + fixture.view.setPointerLocked(true) + fixture.view.enablePointerLock() + fixture.view.setManualPointerCapture(true) + fixture.view.setRemoteCursorVisible(false) + fixture.view.restoreInputFocus() + fixture.view.mouseInputMode = .absolute + fixture.view.confinesCursorToWindowInAbsoluteMode = true + fixture.view.captureAbsoluteCursorIfNeeded() + + #expect(fixture.cursorAssociations == [false, true]) + #expect(!fixture.view.isCursorCaptured) + #expect(!fixture.view.manualPointerCaptureOverride) + } + + @Test func aFrontmostRealInputRetriesRejectedCaptureAndNativeCommandsStillToggleIt() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + var associations: [Bool] = [] + fixture.view.cursorAssociationHandler = { isAssociated in + associations.append(isAssociated) + return associations.count == 1 ? .failure : .success + } + fixture.view.locksPointerWhenRelativeModeSelected = true + fixture.view.handleFocusGain() + #expect(!fixture.view.isPointerLocked) + let motion = UserInputEvent.mouse(.moved(deviceID: "mouse", deltaX: 3, deltaY: -2, timestamp: MediaTimestamp(nanoseconds: 1))) + fixture.model.routeInputEvent(motion, view: fixture.view) + + #expect(fixture.view.isPointerLocked) + #expect(associations == [false, false]) + #expect(try await fixture.recordedInputs() == [motion]) + fixture.model.handleNativeCommand(.togglePointerCapture) + #expect(!fixture.view.isPointerLocked) + fixture.model.handleNativeCommand(.togglePointerCapture) + #expect(fixture.view.isPointerLocked) + #expect(fixture.view.manualPointerCaptureOverride) + } + + @Test func pictureInPictureStillAdmitsAnUnfocusedPhysicalPadWithoutCapturing() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + fixture.focus.isWindowKey = false + fixture.view.allowsBackgroundGamepadInput = false + fixture.view.isPictureInPictureMode = true + fixture.model.isPictureInPicture = true + #expect(try await fixture.recordedInputs().isEmpty) + let native = GCController.withExtendedGamepad() + let pad = try #require(native.extendedGamepad) + fixture.configureNativeControllers([("pip-pad", native, nil)]) + pad.buttonA.setValue(1) + pad.leftThumbstick.xAxis.setValue(0.75) + fixture.pollNativeControllers() + fixture.model.handleNativeCommand(.togglePointerCapture) + + let admitted = gamepads(try await fixture.recordedInputs()) + #expect(admitted.count == 1) + #expect(admitted.first?.buttons == .south) + #expect(admitted.first?.leftStickX == 0.75) + #expect(!fixture.view.isCursorCaptured) + #expect(fixture.cursorAssociations.isEmpty) + } + + @Test func aRecordingBarrierReturnsItsOrderedPrefixEvenWhenTheReaderRunsLater() async throws { + let recorder = CouchCoopNativeInputRecorder() + let stamp = MediaTimestamp(nanoseconds: 1) + let first = UserInputEvent.gamepad(GamepadState(deviceID: "first", playerIndex: 0, buttons: .south, timestamp: stamp)) + let later = UserInputEvent.gamepad(GamepadState(deviceID: "later", playerIndex: 1, buttons: .east, timestamp: stamp)) + await recorder.append(.event(first)) + await recorder.append(.event(.text(deviceID: "input-test-barrier", value: "first-prefix", timestamp: stamp))) + await recorder.append(.event(later)) + await recorder.append(.event(.text(deviceID: "input-test-barrier", value: "second-prefix", timestamp: stamp))) + + #expect(try await recorder.snapshot(at: "first-prefix") == [first]) + #expect(try await recorder.snapshot(at: "second-prefix") == [first, later]) + } + + @Test func cancellingARecordingBarrierPropagatesCancellation() async { + let recorder = CouchCoopNativeInputRecorder() + let waiter = Task { try await recorder.snapshot(at: "cancelled-prefix") } + waiter.cancel() + + await #expect(throws: CancellationError.self) { try await waiter.value } + } + + private func desktopInputs(_ inputs: [UserInputEvent]) -> [UserInputEvent] { + inputs.filter { + switch $0 { + case .keyboard, .mouse: true + default: false + } + } + } + + private func gamepads(_ inputs: [UserInputEvent]) -> [GamepadState] { + inputs.compactMap { + guard case .gamepad(let state) = $0 else { return nil } + return state + } + } + + enum BlockingOverlay: CaseIterable, Sendable { + case hud, quitMenu, keyboard + } + + enum CaptureBlocker: CaseIterable, Sendable { + case inactiveApplication, otherKeyWindow, remoteInputDisabled, keyboardOverlay, pictureInPicture, actualNonKeyWindow + } +} diff --git a/Tests/Stream/DlssStatsHUDRowTests.swift b/Tests/Stream/DlssStatsHUDRowTests.swift index 1ffb9c2ec..6eb7ae9a7 100644 --- a/Tests/Stream/DlssStatsHUDRowTests.swift +++ b/Tests/Stream/DlssStatsHUDRowTests.swift @@ -2,7 +2,9 @@ // starts, and - once the seat's `DlssState` message arrives - what the running session actually // has. The request is never presented as an activation. +import AppKit import Foundation +import SwiftUI import Testing @testable import OpenNOW @@ -11,9 +13,8 @@ import Testing @Test func theStatsHUDReportsTheNextSessionRequest() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - let rows = makeOverlay().nativeStatsDlssGroup.rows + let rows = makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup.rows #expect(rows.map(\.label) == ["Super Resolution", "Frame Generation", "Ray Reconstruction"]) #expect(rows.map(\.value) == ["DLAA 100%", "Dynamic up to 4×", "Preset D"]) } @@ -22,16 +23,14 @@ import Testing @Test func theDlssGroupSaysTheValuesAreForTheNextSession() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - #expect(makeOverlay().nativeStatsDlssGroup.label == "DLSS · NEXT SESSION") + #expect(makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup.label == "DLSS · NEXT SESSION") } } @Test func theDlssGroupNeverClaimsAnOverrideIsActive() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - let group = makeOverlay().nativeStatsDlssGroup + let group = makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup let text = ([group.label] + group.rows.flatMap { [$0.label, $0.value, $0.detail ?? ""] }).joined(separator: " ").lowercased() let words = Set(text.split { !$0.isLetter }.map(String.init)) for claim in ["active", "enabled", "unsupported", "on"] { @@ -53,8 +52,7 @@ import Testing @Test func gameSettingsReadAsInGame() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(.useGameSettings) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - let rows = makeOverlay().nativeStatsDlssGroup.rows + let rows = makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup.rows #expect(rows.map(\.value) == ["In-game", "In-game", "In-game"]) } } @@ -71,7 +69,6 @@ import Testing @Test func theSeatsReportReplacesTheRequestOnceItArrives() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) let group = makeOverlay(applied: appliedState).nativeStatsDlssGroup #expect(group.label == "DLSS · THIS SESSION") @@ -85,7 +82,6 @@ import Testing @Test func aSeatThatOverrodeNothingReadsAsInGame() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) let group = makeOverlay(applied: NvstDlssStateNotification( isSuperResolutionOverridden: false, @@ -104,7 +100,6 @@ import Testing @Test func theFrameGenerationRowPrefersTheMultiplierTheSeatIsRunning() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) let state = NvstDlssStateNotification( isSuperResolutionOverridden: appliedState.isSuperResolutionOverridden, @@ -140,13 +135,58 @@ import Testing @Test func theHudRowAndTheCardSummaryAgree() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - let overrides = OPNStreamPreferences.effectiveDlssOverrides() - let rows = makeOverlay().nativeStatsDlssGroup.rows + let overrides = requestedOverrides.applyingAccess(grantedEntitlements) + let rows = makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup.rows #expect(overrides.nextSessionRequestSummary == "SR \(rows[0].value) · FG \(rows[1].value) · RR \(rows[2].value)") } } + @Test func concurrentLaunchSnapshotsIgnoreChangesToTheActiveAccountEntitlementMirror() { + withDlssPreferencesPreserved { + OPNStreamPreferences.saveDlssOverrides(requestedOverrides) + let entitled = makeOverlay(entitlements: grantedEntitlements) + let unentitled = makeOverlay() + + OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) + #expect(entitled.nativeStatsDlssGroup.rows.map(\.value) == ["DLAA 100%", "Dynamic up to 4×", "Preset D"]) + #expect(unentitled.nativeStatsDlssGroup.rows.map(\.value) == ["In-game", "In-game", "In-game"]) + + OPNStreamPreferences.clearDlssEntitlements() + #expect(entitled.nativeStatsDlssGroup.rows.map(\.value) == ["DLAA 100%", "Dynamic up to 4×", "Preset D"]) + #expect(unentitled.nativeStatsDlssGroup.rows.map(\.value) == ["In-game", "In-game", "In-game"]) + } + } + + @Test(arguments: [CGFloat(1.25), CGFloat(1.5)]) + func eachLaunchMembershipRendersAtNonDefaultInterfaceScales(_ scale: CGFloat) throws { + try withDlssPreferencesPreserved { + OPNStreamPreferences.saveDlssOverrides(requestedOverrides) + let overlays = [ + ("entitled", makeOverlay(entitlements: grantedEntitlements)), + ("unentitled", makeOverlay()), + ] + for (name, overlay) in overlays { + let width = (NativeNVSTStatsPanel.width + 24) * scale + let height = 220 * scale + let content = NativeNVSTStatsPanel(transport: "NATIVE NVST", heroes: [], groups: [overlay.nativeStatsDlssGroup]) + .padding(12) + .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) + .environment(\.opnUIScale, scale) + .opnInterfaceScale(scale) + .frame(width: width, height: height) + .background(Color.black) + .transaction { $0.disablesAnimations = true } + let renderer = ImageRenderer(content: content) + renderer.scale = 2 + let image = try #require(renderer.nsImage) + let pixelTolerance = 1 / renderer.scale + #expect(abs(image.size.width - width) < pixelTolerance) + #expect(abs(image.size.height - height) < pixelTolerance) + try writeSnapshot(image, name: "hud-dlss-\(name)-\(Int(scale * 100)).png") + } + } + } + private var requestedOverrides: DlssOverrides { DlssOverrides( superResolutionMode: DlssSuperResolutionMode.dlaa.rawValue, @@ -168,13 +208,25 @@ import Testing ) } - private func makeOverlay(applied: NvstDlssStateNotification? = nil) -> NativeNVSTStatsOverlay { - NativeNVSTStatsOverlay( + private func makeOverlay(entitlements: DlssEntitlements = .none, applied: NvstDlssStateNotification? = nil) -> NativeNVSTStatsOverlay { + let configuration = StreamLaunchConfiguration( + title: "Game", applicationID: "100", accessToken: "test-token", accountLinked: true, selectedStore: "steam" + ).snapshottingEntitlements(OPNLaunchEntitlements(dlss: entitlements), providerStreamingBaseURL: OPNGameService.defaultStreamingBaseUrl) + return NativeNVSTStatsOverlay( stats: NativeNVSTStreamStatsModel(), - applicationID: "100", + applicationID: configuration.applicationID, detailLevel: .advanced, hdrModeText: "", - appliedDlssState: applied + appliedDlssState: applied, + dlssEntitlements: configuration.launchEntitlements.dlss ) } + + private func writeSnapshot(_ image: NSImage, name: String) throws { + guard let directory = ProcessInfo.processInfo.environment["OPN_SNAPSHOT_DIR"] else { return } + let tiff = try #require(image.tiffRepresentation) + let bitmap = try #require(NSBitmapImageRep(data: tiff)) + let png = try #require(bitmap.representation(using: .png, properties: [:])) + try png.write(to: URL(fileURLWithPath: directory).appendingPathComponent(name)) + } } diff --git a/Tests/Stream/LaunchEntitlementIsolationTests.swift b/Tests/Stream/LaunchEntitlementIsolationTests.swift new file mode 100644 index 000000000..a51103804 --- /dev/null +++ b/Tests/Stream/LaunchEntitlementIsolationTests.swift @@ -0,0 +1,133 @@ +import Foundation +import Testing +@testable import OpenNOW + +@MainActor +private final class LaunchEntitlementPresenceStub: DiscordPresenceServing { + var isEnabled = false + private(set) var lastState = DiscordPresenceState.idle + + func update(_ state: DiscordPresenceState) { lastState = state } +} + +@MainActor +@Suite struct LaunchEntitlementIsolationTests { + @Test func eachAppOwnedLaunchKeepsItsMembershipThroughFinalPayloadResolution() throws { + try withPreservedPreferences(["OpenNOW.Stream.DlssEntitlements", "OpenNOW.Stream.DlssOverrides", "OpenNOW.Stream.EntitledAudioChannelCount", "OpenNOW.Stream.EntitledInGameSettingsPersistence", "OpenNOW.Stream.PersistInGameSettings", OPNSessionReadyAction.modeKey]) { + OPNSessionReadyAction.mode = .off + let granted = OPNLaunchEntitlements( + dlss: DlssEntitlements(superResolution: DlssCapability(isEntitled: true)), + audioChannelCount: 8, + isInGameSettingsPersistenceEntitled: true + ) + let firstService = OPNGameService() + let first = try makeOwnedSession(email: "launch-a@example.test", entitlements: granted, provider: "https://provider-a.nvidiagrid.net/", gameService: firstService) + let second = try makeOwnedSession(email: "launch-b@example.test", entitlements: .none, provider: "https://provider-b.nvidiagrid.net/") + defer { + first.cancelStreamLaunch() + second.cancelStreamLaunch() + first.session.purgeTokens() + second.session.purgeTokens() + } + let firstConfiguration = try #require(first.configuration) + let secondConfiguration = try #require(second.configuration) + #expect(firstConfiguration.launchEntitlements == granted) + #expect(secondConfiguration.launchEntitlements == .none) + #expect(firstConfiguration.metadata[OPNLaunchEntitlements.providerMetadataKey] == "https://provider-a.nvidiagrid.net/") + #expect(secondConfiguration.resumesExistingSession) + firstService.providerStreamingBaseUrl = "https://provider-changed.nvidiagrid.net/" + #expect(first.configuration?.metadata[OPNLaunchEntitlements.providerMetadataKey] == "https://provider-a.nvidiagrid.net/") + + OPNStreamPreferences.clearDlssEntitlements() + OPNStreamPreferences.saveEntitledAudioChannelCount(2) + OPNStreamPreferences.saveEntitledInGameSettingsPersistence(false) + OPNStreamPreferences.savePersistInGameSettings(true) + let coordinator = OPNStreamSessionCoordinator() + var firstSettings = coordinator.makeSettings(configuration: firstConfiguration) + #expect(OPNLaunchEntitlements(settings: firstSettings) == granted) + #expect(firstSettings["enablePersistingInGameSettings"] as? Bool == true) + OPNStreamPreferences.saveDlssEntitlements(granted.dlss) + OPNStreamPreferences.saveEntitledAudioChannelCount(8) + OPNStreamPreferences.saveEntitledInGameSettingsPersistence(true) + var secondSettings = coordinator.makeSettings(configuration: secondConfiguration) + #expect(secondSettings["enablePersistingInGameSettings"] as? Bool == false) + firstSettings["surroundMode"] = "7.1" + secondSettings["surroundMode"] = "7.1" + var overrides = DlssOverrides.useGameSettings + overrides.superResolutionModel = 1 + overrides.superResolutionMode = DlssSuperResolutionMode.quality.rawValue + firstSettings["dlssOverrides"] = overrides.storedDictionary + secondSettings["dlssOverrides"] = overrides.storedDictionary + try expectPayloadEntitlements(firstSettings: firstSettings, secondSettings: secondSettings) + } + } + + @Test func aLaunchWithoutAnAccountSnapshotCannotBorrowGlobalMembership() { + withPreservedPreferences(["OpenNOW.Stream.DlssEntitlements", "OpenNOW.Stream.DlssOverrides"]) { + OPNStreamPreferences.saveDlssEntitlements(DlssEntitlements(superResolution: DlssCapability(isEntitled: true))) + var overrides = DlssOverrides.useGameSettings + overrides.superResolutionModel = 1 + overrides.superResolutionMode = DlssSuperResolutionMode.quality.rawValue + let settings: [String: Any] = ["dlssOverrides": overrides.storedDictionary] + let features = OPNSessionManager().requestedStreamingFeatures(settings, hdrEnabled: false) + #expect((features["dlssOverrides"] as? [String: Any])?["srMode"] as? Int == 0) + #expect(OPNLaunchEntitlements(metadata: [OPNLaunchEntitlements.metadataKey: "not-json"]) == .none) + } + } + + @Test func automaticRegionSelectionUsesTheLaunchingProviderUnlessTheReaderChoseARegion() { + withPreservedPreferences(["OpenNOW.Stream.RegionUrl"]) { + OPNStreamPreferences.saveSelectedRegionUrl("") + #expect(OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: "https://provider-a.nvidiagrid.net/") == "https://provider-a.nvidiagrid.net/") + #expect(OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: "https://provider-b.nvidiagrid.net/") == "https://provider-b.nvidiagrid.net/") + OPNStreamPreferences.saveSelectedRegionUrl("https://chosen-region.nvidiagrid.net/") + #expect(OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: "https://provider-a.nvidiagrid.net/") == "https://chosen-region.nvidiagrid.net/") + #expect(OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: "https://provider-b.nvidiagrid.net/") == "https://chosen-region.nvidiagrid.net/") + } + } + + private func expectPayloadEntitlements(firstSettings: [String: Any], secondSettings: [String: Any]) throws { + let coordinator = OPNStreamSessionCoordinator() + var cloudVariables = OPNStreamCloudVariables() + cloudVariables.entitledAudioChannelCount = 2 + let firstCloudSettings = coordinator.settingsByApplyingCloudVariables(firstSettings, variables: cloudVariables) + cloudVariables.entitledAudioChannelCount = 8 + let secondCloudSettings = coordinator.settingsByApplyingCloudVariables(secondSettings, variables: cloudVariables) + let manager = OPNSessionManager() + var capabilities = OPNStreamDeviceCapabilities() + capabilities.audioOutputChannelCount = 8 + let firstPayloadSettings = manager.settingsByApplyingCloudVariables(firstCloudSettings, capabilities: capabilities) + let secondPayloadSettings = manager.settingsByApplyingCloudVariables(secondCloudSettings, capabilities: capabilities) + let appID = try #require(OPNLaunchAppId.resolve("123")) + let firstPayload = manager.sessionRequestData(launchAppId: appID, internalTitle: "Game A", settings: firstPayloadSettings, capabilities: capabilities, hdrEnabled: false, deviceId: "device-a", selectedStore: "STEAM") + let secondPayload = manager.sessionRequestData(launchAppId: appID, internalTitle: "Game B", settings: secondPayloadSettings, capabilities: capabilities, hdrEnabled: false, deviceId: "device-b", selectedStore: "STEAM") + #expect(firstPayload["requestedAudioFormat"] as? Int == 3) + #expect(secondPayload["requestedAudioFormat"] as? Int == 1) + #expect(firstPayload["enablePersistingInGameSettings"] as? Bool == true) + #expect(secondPayload["enablePersistingInGameSettings"] as? Bool == false) + #expect(firstPayload[OPNLaunchEntitlements.settingsKey] == nil) + let firstFeatures = try #require(firstPayload["requestedStreamingFeatures"] as? [String: Any]) + let secondFeatures = try #require(secondPayload["requestedStreamingFeatures"] as? [String: Any]) + #expect((firstFeatures["dlssOverrides"] as? [String: Any])?["srMode"] as? Int == DlssSuperResolutionMode.quality.rawValue) + #expect((secondFeatures["dlssOverrides"] as? [String: Any])?["srMode"] as? Int == 0) + } + + private func makeOwnedSession(email: String, entitlements: OPNLaunchEntitlements, provider: String, gameService: OPNGameService = OPNGameService()) throws -> OPNGameSession { + let account = makeLoginAccountForTesting(email: email, userId: email) + let session = makeLoginSessionForTesting(accountEmail: email, userId: email) + let registry = OPNGameSessionRegistry() + let owned = try #require(registry.begin( + account: account, + session: session, + gameService: gameService, + launchBridge: OPNGameLaunchBridge(gameService: gameService), + discordPresence: LaunchEntitlementPresenceStub(), + streamProfile: OPNStreamPreferenceProfile(), + launchEntitlements: entitlements, + providerStreamingBaseURL: provider, + results: OPNGameSessionResultStore() + )) + owned.beginResume(title: "Game", applicationID: "123", sessionID: "resume-\(email)", server: "seat.nvidiagrid.net", game: nil) + return owned + } +} diff --git a/Tests/Stream/NativeNVSTHUDFocusEntryParityTests.swift b/Tests/Stream/NativeNVSTHUDFocusEntryParityTests.swift index d0c60a13c..3cfe34efc 100644 --- a/Tests/Stream/NativeNVSTHUDFocusEntryParityTests.swift +++ b/Tests/Stream/NativeNVSTHUDFocusEntryParityTests.swift @@ -28,6 +28,26 @@ struct NativeNVSTHUDFocusEntryParityTests { #expect(focusIDs(in: "input", on: model) == surface.nativeHUDInputTiles.map(\.id)) } + /// The borderless tile exists only behind the Couch Co-op flag, and when it does a pad reaches it + /// the same way the pointer does. + @Test func theBorderlessTileAndItsFocusEntryAppearTogetherBehindTheFlag() { + let previous = OPNLabs.isCouchCoopEnabled + defer { OPNLabs.setEnabled(OPNLabs.couchCoop, previous) } + + OPNLabs.setEnabled(OPNLabs.couchCoop, false) + let (offSurface, offModel) = makeHUDSurface() + #expect(!offSurface.nativeHUDDisplayTiles.map(\.id).contains("borderless-full-screen")) + #expect(!offModel.hudFocusEntries.map(\.id).contains("borderless-full-screen")) + + OPNLabs.setEnabled(OPNLabs.couchCoop, true) + let (onSurface, onModel) = makeHUDSurface() + #expect(onSurface.nativeHUDDisplayTiles.map(\.id).contains("borderless-full-screen")) + #expect(focusIDs(in: "display", on: onModel) == onSurface.nativeHUDDisplayTiles.map(\.id)) + let tile = onSurface.nativeHUDDisplayTiles.first { $0.id == "borderless-full-screen" } + let entry = onModel.hudFocusEntries.first { $0.id == "borderless-full-screen" } + #expect(tile?.isDisabled == entry?.isDisabled) + } + /// The AUDIO panel has no tiles any more: the mute tiles were replaced by the volume sliders' /// leading icons, so its rows are hand-listed here in draw order — output above microphone, each /// volume a two-entry row of its mute icon and its slider. diff --git a/Tests/Stream/NativeNVSTInputParityTests.swift b/Tests/Stream/NativeNVSTInputParityTests.swift index a6a80f7e2..09ae505ca 100644 --- a/Tests/Stream/NativeNVSTInputParityTests.swift +++ b/Tests/Stream/NativeNVSTInputParityTests.swift @@ -271,8 +271,17 @@ private actor ControlledNativeInputRecorder { @Test @MainActor func focusLossNeutralizesEveryActiveDeviceBeforePointerUnlock() throws { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) + view.inputFocusProvider = { (true, true) } + view.cursorAssociationHandler = { _ in .success } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false + window.isReleasedWhenClosed = false window.contentView = view + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } var sequence: [String] = [] view.onInputEvent = { event in switch event { diff --git a/Tests/Stream/NativeStreamViewCaptureLifecycleTests.swift b/Tests/Stream/NativeStreamViewCaptureLifecycleTests.swift index 5a938856a..6739d1c1c 100644 --- a/Tests/Stream/NativeStreamViewCaptureLifecycleTests.swift +++ b/Tests/Stream/NativeStreamViewCaptureLifecycleTests.swift @@ -13,6 +13,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.captureAbsoluteCursorIfNeeded() try #require(view.isAbsoluteCursorConfined) @@ -54,6 +55,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.captureAbsoluteCursorIfNeeded() try #require(view.isAbsoluteCursorConfined) @@ -79,6 +81,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.captureAbsoluteCursorIfNeeded() view.setRemoteCursorVisible(true) @@ -94,6 +97,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.remoteInputEnabled = false view.locksPointerWhenRelativeModeSelected = true @@ -110,7 +114,9 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } + view.inputFocusProvider = nil try #require(!window.isKeyWindow) view.locksPointerWhenRelativeModeSelected = true view.mouseInputMode = .relative @@ -125,6 +131,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.localOverlayCapturesInput = true view.locksPointerWhenRelativeModeSelected = true diff --git a/Tests/Stream/NativeStreamViewCursorFixtures.swift b/Tests/Stream/NativeStreamViewCursorFixtures.swift index ff63b3149..64767c187 100644 --- a/Tests/Stream/NativeStreamViewCursorFixtures.swift +++ b/Tests/Stream/NativeStreamViewCursorFixtures.swift @@ -17,6 +17,8 @@ import AppKit defer: false ) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) + window.isReleasedWhenClosed = false + view.inputFocusProvider = { (true, true) } let center = CGPoint(x: window.frame.midX, y: window.frame.midY) view.cursorLocationProvider = { center } view.cursorWarpHandler = { _ in } diff --git a/Tests/Stream/NativeStreamViewCursorPolicyTests.swift b/Tests/Stream/NativeStreamViewCursorPolicyTests.swift index b1a845ac2..370641c86 100644 --- a/Tests/Stream/NativeStreamViewCursorPolicyTests.swift +++ b/Tests/Stream/NativeStreamViewCursorPolicyTests.swift @@ -80,10 +80,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.setManualPointerCapture(true) @@ -118,10 +125,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.setManualPointerCapture(true) // Teardown and in-place reconnect both drop remote input, which releases the pointer. @@ -155,10 +169,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } // The game is in mouselook, so the client followed it into relative mode. view.setRemoteCursorVisible(false) @@ -188,10 +209,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.setManualPointerCapture(true) view.setRemoteCursorVisible(false) @@ -282,10 +310,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 480, height: 270), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .relative + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } // The game is in mouselook, so the client is holding the pointer. view.setRemoteCursorVisible(false) diff --git a/Tests/Stream/NativeStreamViewMouseTests.swift b/Tests/Stream/NativeStreamViewMouseTests.swift index 5106db784..d2b3705e6 100644 --- a/Tests/Stream/NativeStreamViewMouseTests.swift +++ b/Tests/Stream/NativeStreamViewMouseTests.swift @@ -53,8 +53,16 @@ private struct MouseButtonTransition: Equatable { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false + window.isReleasedWhenClosed = false window.contentView = view + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } var events: [UserInputEvent] = [] var releaseObservedWhileLocked = false view.onInputEvent = { event in @@ -83,13 +91,20 @@ private struct MouseButtonTransition: Equatable { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false + window.isReleasedWhenClosed = false window.contentView = view var events: [UserInputEvent] = [] view.onInputEvent = { events.append($0) } let mouseDown = try #require(makeMouseEvent(type: .leftMouseDown)) let mouseUp = try #require(makeMouseEvent(type: .leftMouseUp)) - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.mouseDown(with: mouseDown) view.mouseUp(with: mouseUp) @@ -108,10 +123,17 @@ private struct MouseButtonTransition: Equatable { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.setRemoteCursorVisible(false) @@ -187,7 +209,8 @@ private struct MouseButtonTransition: Equatable { let (window, view) = makeAbsoluteCursorCaptureWindow() defer { view.setPointerLocked(false) - withExtendedLifetime(window) {} + window.contentView = nil + window.close() } var events: [UserInputEvent] = [] view.onInputEvent = { events.append($0) } diff --git a/Tests/Stream/OPNStreamControllerPresetTests.swift b/Tests/Stream/OPNStreamControllerPresetTests.swift new file mode 100644 index 000000000..090ee11f9 --- /dev/null +++ b/Tests/Stream/OPNStreamControllerPresetTests.swift @@ -0,0 +1,104 @@ +import Foundation +import GameController +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct OPNStreamControllerPresetTests { + private func makeStore() throws -> (store: ControllerMappingStore, defaults: UserDefaults, suite: String) { + let suite = "OPNStreamControllerPresetTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + return (ControllerMappingStore(defaults: defaults), defaults, suite) + } + + @Test func aStoredSelectionIsHandedOverOnceUnderItsAccount() { + let preset = OPNStreamControllerPreset() + let selection = OPNStreamControllerSelection(disabled: ["pad-a"], takesPadsFromOtherStreams: true) + preset.store(selection, forAccount: "account-a") + #expect(preset.peek(forAccount: "account-a") == selection) + #expect(preset.peek(forAccount: "account-b") == OPNStreamControllerSelection()) + #expect(preset.take(forAccount: "account-a") == selection) + #expect(preset.take(forAccount: "account-a") == OPNStreamControllerSelection()) + } + + @Test func anEmptySelectionClearsWhatWasStored() { + let preset = OPNStreamControllerPreset() + preset.store(OPNStreamControllerSelection(disabled: ["pad-a"]), forAccount: "account-a") + preset.store(OPNStreamControllerSelection(), forAccount: "account-a") + #expect(preset.peek(forAccount: "account-a") == OPNStreamControllerSelection()) + } + + @Test func theHandOffIsTheConnectedPadsThisStreamKeepsAndOnlyWhenAsked() { + let connected: [InputDeviceID] = ["pad-a", "pad-b", "pad-c"] + let off = OPNStreamControllerSelection(disabled: ["pad-a"], takesPadsFromOtherStreams: false) + let on = OPNStreamControllerSelection(disabled: ["pad-a"], takesPadsFromOtherStreams: true) + #expect(off.handedOff(connected: connected).isEmpty) + #expect(on.handedOff(connected: connected) == ["pad-b", "pad-c"]) + } + + @Test func disabledPadsAreOutOfAStreamBeforeItsFirstSlotIsPublished() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let first = GCController.withExtendedGamepad() + let second = GCController.withExtendedGamepad() + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [first, second] }, steamDeviceIDs: { [] })) + let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) + let firstID = try #require(registry.id(for: first)) + let secondID = try #require(registry.id(for: second)) + + monitor.disableForStream([firstID]) + + #expect(monitor.topology.playerIndices == [0]) + #expect(monitor.streamControllerRows.first { $0.id == firstID }?.state == .disabled) + #expect(monitor.streamControllerRows.first { $0.id == secondID }?.playerIndex == 0) + monitor.stop() + } + + @Test func otherStreamsLoseThePadsTheNewStreamKeeps() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let first = GCController.withExtendedGamepad() + let second = GCController.withExtendedGamepad() + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [first, second] }, steamDeviceIDs: { [] })) + let running = NativeGamepadMonitor(mappingProvider: store, registry: registry) + let firstID = try #require(registry.id(for: first)) + let secondID = try #require(registry.id(for: second)) + let selection = OPNStreamControllerSelection(disabled: [firstID], takesPadsFromOtherStreams: true) + + running.disableForStream(selection.handedOff(connected: registry.orderedDevices.map(\.id))) + + #expect(running.streamControllerRows.first { $0.id == firstID }?.isEnabled == true) + #expect(running.streamControllerRows.first { $0.id == secondID }?.isEnabled == false) + running.stop() + } + + @Test func aReconnectedPadKeepsItsStreamState() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let first = GCController.withExtendedGamepad() + var attached = [first] + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { attached }, steamDeviceIDs: { [] })) + let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) + let firstID = try #require(registry.id(for: first)) + monitor.disableForStream([firstID]) + + attached = [] + registry.refresh() + attached = [first] + registry.refresh() + monitor.setStreamEnabled(false, for: firstID) + + #expect(monitor.streamControllerRows.first { $0.id == firstID }?.isEnabled == false) + monitor.stop() + } + + @Test func disablingAnUnknownPadChangesNothing() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [] }, steamDeviceIDs: { [] })) + let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) + monitor.disableForStream(["not-connected"]) + #expect(monitor.streamControllerRows.isEmpty) + monitor.stop() + } +} diff --git a/Tests/Stream/OPNStreamPictureInPictureTests.swift b/Tests/Stream/OPNStreamPictureInPictureTests.swift index e758e408d..2a1779c0e 100644 --- a/Tests/Stream/OPNStreamPictureInPictureTests.swift +++ b/Tests/Stream/OPNStreamPictureInPictureTests.swift @@ -196,8 +196,6 @@ struct OPNStreamPictureInPictureTests { window.makeKeyAndOrderFront(nil) await settleRunLoop() - // What the close button sends. `performClose(_:)` itself is not driven here: on macOS 26 it - // ends the test host's process once the run loop turns, taking the rest of the run with it. #expect(window.delegate?.windowShouldClose?(window) == false) await settleRunLoop() diff --git a/Tests/Stream/OPNStreamTileLayoutTests.swift b/Tests/Stream/OPNStreamTileLayoutTests.swift new file mode 100644 index 000000000..4077cb569 --- /dev/null +++ b/Tests/Stream/OPNStreamTileLayoutTests.swift @@ -0,0 +1,118 @@ +import CoreGraphics +import Testing +@testable import OpenNOW + +@Suite struct OPNStreamTileLayoutTests { + private let landscape = CGRect(x: 0, y: 0, width: 1920, height: 1080) + private let secondScreen = CGRect(x: 1920, y: 0, width: 2560, height: 1440) + private let portrait = CGRect(x: 0, y: 0, width: 1080, height: 1920) + + @Test func twoWindowsOnTwoScreensFillOneScreenEach() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape, secondScreen], windowCount: 2, orientation: .auto) + #expect(tiles == [ + OPNStreamTile(frame: landscape, screenIndex: 0), + OPNStreamTile(frame: secondScreen, screenIndex: 1), + ]) + } + + @Test func twoWindowsOnOneLandscapeScreenSplitSideBySideInAutomatic() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 2, orientation: .auto) + #expect(tiles.map(\.frame) == [ + CGRect(x: 0, y: 0, width: 960, height: 1080), + CGRect(x: 960, y: 0, width: 960, height: 1080), + ]) + #expect(tiles.allSatisfy { $0.screenIndex == 0 }) + } + + @Test func twoWindowsOnOnePortraitScreenSplitTopAndBottomInAutomatic() { + let tiles = OPNStreamTileLayout.tiles(screens: [portrait], windowCount: 2, orientation: .auto) + #expect(tiles.map(\.frame) == [ + CGRect(x: 0, y: 960, width: 1080, height: 960), + CGRect(x: 0, y: 0, width: 1080, height: 960), + ]) + } + + @Test func explicitOrientationOverridesTheScreenShape() { + let stacked = OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 2, orientation: .topAndBottom) + #expect(stacked.map(\.frame) == [ + CGRect(x: 0, y: 540, width: 1920, height: 540), + CGRect(x: 0, y: 0, width: 1920, height: 540), + ]) + let columns = OPNStreamTileLayout.tiles(screens: [portrait], windowCount: 2, orientation: .sideBySide) + #expect(columns.map(\.frame) == [ + CGRect(x: 0, y: 0, width: 540, height: 1920), + CGRect(x: 540, y: 0, width: 540, height: 1920), + ]) + } + + @Test func threeWindowsOnOneScreenShareItInOneStrip() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 3, orientation: .sideBySide) + #expect(tiles.map(\.frame.width) == [640, 640, 640]) + #expect(tiles.map(\.frame.minX) == [0, 640, 1280]) + } + + @Test func fourWindowsOnOneScreenFormAGrid() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 4, orientation: .auto) + #expect(tiles.map(\.frame) == [ + CGRect(x: 0, y: 540, width: 960, height: 540), + CGRect(x: 960, y: 540, width: 960, height: 540), + CGRect(x: 0, y: 0, width: 960, height: 540), + CGRect(x: 960, y: 0, width: 960, height: 540), + ]) + } + + @Test func threeWindowsOnTwoScreensGiveTheFirstScreenTheExtraWindow() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape, secondScreen], windowCount: 3, orientation: .auto) + #expect(tiles.map(\.screenIndex) == [0, 0, 1]) + #expect(tiles[2].frame == secondScreen) + #expect(tiles[0].frame.width + tiles[1].frame.width == landscape.width) + } + + @Test func oddScreenSizesLeaveNoSeamAndNoOverlap() { + let odd = CGRect(x: 0, y: 0, width: 1367, height: 769) + for count in 2...7 { + for orientation in OPNStreamTileOrientation.allCases { + let tiles = OPNStreamTileLayout.tiles(screens: [odd], windowCount: count, orientation: orientation) + #expect(tiles.count == count) + let covered = tiles.reduce(0) { $0 + $1.frame.width * $1.frame.height } + #expect(covered == odd.width * odd.height, "\(count) windows, \(orientation)") + for (index, tile) in tiles.enumerated() { + #expect(odd.contains(tile.frame)) + for other in tiles[(index + 1)...] { + #expect(tile.frame.intersection(other.frame).isEmpty || tile.frame.intersection(other.frame).width * tile.frame.intersection(other.frame).height == 0) + } + } + } + } + } + + @Test func screensOfDifferentSizesKeepTheirOwnTiles() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape, secondScreen], windowCount: 4, orientation: .sideBySide) + let first = tiles.filter { $0.screenIndex == 0 } + let second = tiles.filter { $0.screenIndex == 1 } + #expect(first.count == 2 && second.count == 2) + #expect(first.allSatisfy { landscape.contains($0.frame) }) + #expect(second.allSatisfy { secondScreen.contains($0.frame) }) + #expect(second.map(\.frame.width) == [1280, 1280]) + } + + @Test func nothingToPlaceProducesNoTiles() { + #expect(OPNStreamTileLayout.tiles(screens: [], windowCount: 2, orientation: .auto).isEmpty) + #expect(OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 0, orientation: .auto).isEmpty) + } + + @Test func oneWindowFillsItsScreen() { + #expect(OPNStreamTileLayout.tiles(screens: [landscape, secondScreen], windowCount: 1, orientation: .auto) == [ + OPNStreamTile(frame: landscape, screenIndex: 0), + ]) + } + + @Test func theOrientationPreferenceRoundTripsThroughStorage() { + let original = OPNStreamTileOrientation.stored + defer { OPNStreamTileOrientation.store(original) } + for orientation in OPNStreamTileOrientation.allCases { + OPNStreamTileOrientation.store(orientation) + #expect(OPNStreamTileOrientation.stored == orientation) + } + } +} diff --git a/Tests/Stream/StreamPreflightProviderRoutingTests.swift b/Tests/Stream/StreamPreflightProviderRoutingTests.swift new file mode 100644 index 000000000..bfcd6b912 --- /dev/null +++ b/Tests/Stream/StreamPreflightProviderRoutingTests.swift @@ -0,0 +1,134 @@ +import Foundation +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct StreamPreflightProviderRoutingTests { + enum RegionSelection: CaseIterable, Sendable { + case automaticUncached, automaticCachedElsewhere, explicit + + var selectedURL: String { + switch self { + case .automaticUncached, .automaticCachedElsewhere: "" + case .explicit: "https://preflight-chosen.nvidiagrid.net/" + } + } + + var cachedRegions: [OPNStreamRegionOption] { + switch self { + case .automaticUncached: [] + case .automaticCachedElsewhere: + [OPNStreamRegionOption(name: "Another provider's cached region", url: "https://preflight-stale.nvidiagrid.net/", latencyMs: 10)] + case .explicit: + [OPNStreamRegionOption(name: "Chosen region", url: selectedURL, latencyMs: 10)] + } + } + } + + enum NetworkOutcome: CaseIterable, Sendable { + case completed, provisioned, unavailable, malformed + + var isSessionAvailable: Bool { self == .completed || self == .provisioned } + } + + @Test(arguments: RegionSelection.allCases, NetworkOutcome.allCases) + func preflightKeepsEachOwnersRouteThroughFinalAllocation(_ region: RegionSelection, _ outcome: NetworkOutcome) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + SessionManagerURLProtocol.install(host: "*", paths: ["/v2/serverInfo", "/v2/nettestsession", "/v2/session"]) { request in + Self.response(to: request, outcome: outcome) + } + defer { SessionManagerURLProtocol.uninstall(host: "*") } + let storage = OPNAppPreferenceStorage.standard + let activeSessionKey = OPNSessionManager.persistedActiveSessionIdKey + let savedActiveSessionID = withExclusivePreferenceDomain { storage.string(forKey: activeSessionKey) } + defer { + withExclusivePreferenceDomain { + if let savedActiveSessionID { storage.set(savedActiveSessionID, forKey: activeSessionKey) } + if savedActiveSessionID == nil { storage.removeObject(forKey: activeSessionKey) } + storage.synchronize() + } + } + + for owner in ["a", "b"] { + let providerURL = "https://preflight-provider-\(owner).nvidiagrid.net/" + let configuration = StreamLaunchConfiguration( + title: "Owner \(owner)", applicationID: "123", accessToken: "owner-\(owner)-token", + accountLinked: true, selectedStore: "Steam", + metadata: [OPNLaunchEntitlements.providerMetadataKey: providerURL] + ) + let providerSnapshot = try #require(configuration.metadata[OPNLaunchEntitlements.providerMetadataKey]) + let expectedURL = region.selectedURL.isEmpty ? providerURL : region.selectedURL + let expectedHost = try #require(URL(string: expectedURL)?.host) + let requestOffset = SessionManagerURLProtocol.recordedRequests(host: "*").count + let preflight = await runPreflight(configuration: configuration, providerURL: providerSnapshot, region: region) + #expect(preflight.streamingBaseUrl == expectedURL) + #expect(preflight.networkTestSessionId == (outcome.isSessionAvailable ? "nettest-\(expectedHost)" : "")) + #expect(preflight.serverReportedWarning == (outcome == .provisioned)) + if outcome == .completed { #expect(preflight.latencyMs == 12) } + + var settings = minimalSettings() + settings["networkTestSessionId"] = preflight.networkTestSessionId + let launch = PreparedStreamLaunch(settings: settings, streamingBaseUrl: preflight.streamingBaseUrl) + let coordinator = OPNStreamSessionCoordinator(sessionManager: OPNSessionManager(), deviceId: "owner-\(owner)-device") + let allocated = try await coordinator.allocateSession(configuration: configuration, launch: launch) + #expect(allocated.isReady) + try await waitForRegionRequest(after: requestOffset) + let requests = Array(SessionManagerURLProtocol.recordedRequests(host: "*").dropFirst(requestOffset)) + let networkRequests = requests.filter { $0.url?.path == "/v2/nettestsession" } + let allocationRequests = requests.filter { $0.url?.path == "/v2/session" } + #expect(networkRequests.count == 1) + #expect(allocationRequests.count == 1) + #expect((networkRequests + allocationRequests).allSatisfy { $0.url?.host == expectedHost && $0.httpMethod == "POST" }) + #expect(requests.allSatisfy { $0.value(forHTTPHeaderField: "Authorization") == "GFNJWT \(configuration.accessToken)" }) + #expect(requests.filter { $0.url?.path == "/v2/serverInfo" }.allSatisfy { $0.url?.absoluteString == providerURL + "v2/serverInfo" }) + let allocationPayloads = SessionManagerURLProtocol.recordedJSONBodies(host: "*").compactMap { $0["sessionRequestData"] as? [String: Any] } + let payload = try #require(allocationPayloads.last) + #expect(payload["deviceHashId"] as? String == "owner-\(owner)-device") + #expect((payload["networkTestSessionId"] as? String ?? "") == preflight.networkTestSessionId) + #expect(configuration.metadata[OPNLaunchEntitlements.providerMetadataKey] == providerSnapshot) + } + #expect(SessionManagerURLProtocol.strayRequestSummaries().isEmpty) + } + } + + private func runPreflight(configuration: StreamLaunchConfiguration, providerURL: String, region: RegionSelection) async -> OPNStreamNetworkPreflightResult { + await withCheckedContinuation { continuation in + withPreservedPreferences(["OpenNOW.Stream.RegionUrl", "OpenNOW.Stream.CachedRegions"]) { + OPNStreamPreferences.saveSelectedRegionUrl(region.selectedURL) + OPNStreamPreferences.saveCachedRegions(region.cachedRegions) + OPNStreamPreferences.runNetworkPreflight(token: configuration.accessToken, providerStreamingBaseUrl: providerURL, requestedMaxBitrateMbps: 50) { + continuation.resume(returning: $0) + } + } + } + } + + private func waitForRegionRequest(after offset: Int) async throws { + for _ in 0..<300 { + let requests = SessionManagerURLProtocol.recordedRequests(host: "*").dropFirst(offset) + if requests.contains(where: { $0.url?.path == "/v2/serverInfo" }) { return } + try await Task.sleep(for: .milliseconds(10)) + } + Issue.record("The provider region refresh did not reach the scripted transport") + } + + nonisolated private static func response(to request: URLRequest, outcome: NetworkOutcome) -> (Int, Data) { + guard request.url?.path != "/v2/serverInfo" else { + return SessionManagerURLProtocol.response(json: [:], status: 404) + } + guard request.url?.path != "/v2/session" else { + return SessionManagerURLProtocol.response(json: sessionResponse(statusCode: 1, sessionStatus: 2)) + } + guard outcome != .unavailable else { return SessionManagerURLProtocol.response(json: [:], status: 404) } + guard outcome != .malformed else { return SessionManagerURLProtocol.response(json: [:]) } + let host = request.url?.host ?? "missing-host" + var response: [String: Any] = ["netTestSession": [ + "sessionId": "nettest-\(host)", + "connectionInfo": [["ip": "measurement-endpoint.example.test", "port": 443, "appLevelProtocol": 5]], + ]] + if outcome == .completed { + response["testResult"] = ["status": "COMPLETED", "latencyMs": 12, "maxPacketSize": 1400] + } + return SessionManagerURLProtocol.response(json: response) + } +} diff --git a/Tests/Stream/StreamSessionIdentityTests.swift b/Tests/Stream/StreamSessionIdentityTests.swift index a8bb4191d..85251e532 100644 --- a/Tests/Stream/StreamSessionIdentityTests.swift +++ b/Tests/Stream/StreamSessionIdentityTests.swift @@ -38,11 +38,13 @@ private final class RecordingDiscordPresence: DiscordPresenceServing { let snapshotted = configuration.snapshottingSession( id: sessionID, ownerDisplayName: "Account A", + ownerAccountID: "account-a", mappingGameIdentity: "game-1" ) #expect(snapshotted.id == sessionID) #expect(snapshotted.owningAccountDisplayName == "Account A") + #expect(snapshotted.owningAccountID == "account-a") #expect(snapshotted.mappingGameIdentity == "game-1") #expect(snapshotted.applicationID == configuration.applicationID) #expect(snapshotted.title == configuration.title) diff --git a/Tests/Stream/StreamWindowConcurrencyTests.swift b/Tests/Stream/StreamWindowConcurrencyTests.swift index 35b6a25bc..81ea7832e 100644 --- a/Tests/Stream/StreamWindowConcurrencyTests.swift +++ b/Tests/Stream/StreamWindowConcurrencyTests.swift @@ -69,6 +69,116 @@ import Testing #expect(OPNStreamWindowPresenter.shared.windows[game.id] == nil) } + /// With couch co-op on, two streams are tiled borderless and share the screens; with the flag off + /// nothing is ever placed; ending one hands the survivor back its own placement. + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func couchCoopTilesTwoStreamsAndReleasesTheSurvivor() async throws { + let previousFlag = OPNLabs.isCouchCoopEnabled + defer { OPNLabs.setEnabled(OPNLabs.couchCoop, previousFlag) } + let registry = OPNGameSessionRegistry.shared + let accountA = makeLoginAccountForTesting(email: "a@example.com", userId: "user-a") + let accountB = makeLoginAccountForTesting(email: "b@example.com", userId: "user-b", isActive: false) + let sessionA = makeLoginSessionForTesting(accountEmail: accountA.email, id: "tile-a", userId: "user-a") + let sessionB = makeLoginSessionForTesting(accountEmail: accountB.email, id: "tile-b", userId: "user-b") + defer { + sessionA.purgeTokens() + sessionB.purgeTokens() + } + let gameA = try #require(begin(registry: registry, account: accountA, session: sessionA)) + let gameB = try #require(begin(registry: registry, account: accountB, session: sessionB)) + defer { + OPNStreamWindowPresenter.shared.dismiss(gameA.id) + OPNStreamWindowPresenter.shared.dismiss(gameB.id) + registry.end(gameA) + registry.end(gameB) + } + gameA.configuration = Self.configuration(title: "Cyberpunk 2077") + gameB.configuration = Self.configuration(title: "Manor Lords") + + OPNLabs.setEnabled(OPNLabs.couchCoop, false) + OPNStreamWindowPresenter.shared.syncWithOwnedSessions() + let windowA = try #require(OPNStreamWindowPresenter.shared.windows[gameA.id]) + let windowB = try #require(OPNStreamWindowPresenter.shared.windows[gameB.id]) + #expect(!windowA.layoutState.isAutoTiled && !windowB.layoutState.isAutoTiled) + #expect(!windowA.allowsBackgroundGamepadInput) + + OPNLabs.setEnabled(OPNLabs.couchCoop, true) + OPNStreamWindowPresenter.shared.retileWindows() + try await Task.sleep(for: .milliseconds(300)) + #expect(windowA.layoutState.isAutoTiled && windowB.layoutState.isAutoTiled) + #expect(windowA.layoutState.isBorderless && windowB.layoutState.isBorderless) + #expect(windowA.frame != windowB.frame) + + OPNStreamWindowPresenter.shared.dismiss(gameB.id) + try await Task.sleep(for: .milliseconds(300)) + #expect(!windowA.layoutState.isAutoTiled) + #expect(!windowA.layoutState.isBorderless) + } + + /// The reader taking one tiled window hands the layout back: the survivor is the only placeable + /// window left, which is not a layout, so it goes back to its own placement too. + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func movingOneTiledWindowRetilesTheOther() async throws { + try await withTiledPair { windowA, windowB in + windowA.setFrame(windowA.frame.offsetBy(dx: 40, dy: -40), display: false) + try await Task.sleep(for: .milliseconds(300)) + + #expect(windowA.layoutState.isManuallyPlaced) + #expect(!windowA.layoutState.isAutoTiled) + #expect(!windowB.layoutState.isAutoTiled) + #expect(!windowB.layoutState.isBorderless) + } + } + + /// Sending a tiled window to Picture in Picture releases it the way the HUD does, and the other + /// window is retiled rather than left holding a half-screen frame. + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func releasingATiledWindowToPictureInPictureRetilesTheOther() async throws { + try await withTiledPair { windowA, windowB in + OPNStreamPictureInPicture.enter(windowA, aspectRatio: 16.0 / 9.0) + try await Task.sleep(for: .milliseconds(300)) + + #expect(windowA.isPictureInPicture) + #expect(!windowA.layoutState.isAutoTiled) + #expect(!windowB.layoutState.isAutoTiled) + #expect(!windowB.layoutState.isBorderless) + } + } + + /// Runs `body` with two streams tiled by couch co-op, and puts the flag and sessions back after. + private func withTiledPair(_ body: @MainActor (OPNStreamWindow, OPNStreamWindow) async throws -> Void) async throws { + let previousFlag = OPNLabs.isCouchCoopEnabled + defer { OPNLabs.setEnabled(OPNLabs.couchCoop, previousFlag) } + let registry = OPNGameSessionRegistry.shared + let accountA = makeLoginAccountForTesting(email: "a@example.com", userId: "user-a") + let accountB = makeLoginAccountForTesting(email: "b@example.com", userId: "user-b", isActive: false) + let sessionA = makeLoginSessionForTesting(accountEmail: accountA.email, id: "retile-a", userId: "user-a") + let sessionB = makeLoginSessionForTesting(accountEmail: accountB.email, id: "retile-b", userId: "user-b") + defer { + sessionA.purgeTokens() + sessionB.purgeTokens() + } + let gameA = try #require(begin(registry: registry, account: accountA, session: sessionA)) + let gameB = try #require(begin(registry: registry, account: accountB, session: sessionB)) + defer { + OPNStreamWindowPresenter.shared.dismiss(gameA.id) + OPNStreamWindowPresenter.shared.dismiss(gameB.id) + registry.end(gameA) + registry.end(gameB) + } + gameA.configuration = Self.configuration(title: "Cyberpunk 2077") + gameB.configuration = Self.configuration(title: "Manor Lords") + + OPNLabs.setEnabled(OPNLabs.couchCoop, true) + OPNStreamWindowPresenter.shared.syncWithOwnedSessions() + let windowA = try #require(OPNStreamWindowPresenter.shared.windows[gameA.id]) + let windowB = try #require(OPNStreamWindowPresenter.shared.windows[gameB.id]) + try await Task.sleep(for: .milliseconds(300)) + try #require(windowA.layoutState.isAutoTiled && windowB.layoutState.isAutoTiled) + + try await body(windowA, windowB) + } + private static func configuration(title: String) -> StreamLaunchConfiguration { StreamLaunchConfiguration( title: title, diff --git a/Tests/Stream/StreamWindowLayoutMembershipTests.swift b/Tests/Stream/StreamWindowLayoutMembershipTests.swift new file mode 100644 index 000000000..ece29a484 --- /dev/null +++ b/Tests/Stream/StreamWindowLayoutMembershipTests.swift @@ -0,0 +1,201 @@ +import AppKit +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized, .streamLifecycleExclusive, + .disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) +struct StreamWindowLayoutMembershipTests { + @Test func draggingOneOfTwoTilesKeepsTheReadersPlacementAndReleasesTheSurvivor() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let moved = fixture.windows[0] + let survivor = fixture.windows[1] + #expect(moved.layoutState.isAutoTiled && survivor.layoutState.isAutoTiled) + + moved.setFrame(moved.frame.offsetBy(dx: 28, dy: 32), display: true) + let readerFrame = moved.frame + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(moved.frame == readerFrame) + #expect(moved.layoutState.isManuallyPlaced && !moved.layoutState.isAutoTiled) + #expect(!moved.layoutState.isBorderless && moved.level == .normal) + #expect(moved.standardWindowButton(.closeButton)?.isHidden == false) + #expect(survivor.frame == fixture.originalFrames[1]) + #expect(!survivor.layoutState.isAutoTiled && !survivor.layoutState.isBorderless) + #expect(!survivor.layoutState.isManuallyPlaced && survivor.level == .normal) + #expect(!moved.hasPendingLayoutChange && !survivor.hasPendingLayoutChange) + } + + @Test func resizingOneOfThreeTilesRetilesOnlyTheSurvivorsWithoutAProgrammaticFeedbackLoop() async throws { + let fixture = try StreamWindowLayoutFixture(count: 3) + defer { fixture.close() } + await settleStreamWindowChanges() + let resized = fixture.windows[0] + let survivors = Array(fixture.windows.dropFirst()) + var changes = 0 + let center = NotificationCenter.default + let tokens = survivors.flatMap { window in + [NSWindow.didMoveNotification, NSWindow.didResizeNotification].map { name in + center.addObserver(forName: name, object: window, queue: .main) { _ in + MainActor.assumeIsolated { changes += 1 } + } + } + } + defer { for token in tokens { center.removeObserver(token) } } + var readerFrame = resized.frame + readerFrame.size.height -= 60 + resized.setFrame(readerFrame, display: true) + readerFrame = resized.frame + await settleStreamWindowChanges() + let settledChanges = changes + let expected = fixture.expectedTiles(count: 2) + await settleStreamWindowChanges() + + #expect(resized.frame == readerFrame && resized.layoutState.isManuallyPlaced) + #expect(!resized.layoutState.isAutoTiled && resized.level == .normal) + #expect(survivors.map(\.frame) == expected) + #expect(survivors.allSatisfy { $0.layoutState.isAutoTiled && !$0.layoutState.isManuallyPlaced }) + #expect(survivors.allSatisfy { $0.level == OPNStreamBorderless.coveringLevel && !$0.hasPendingLayoutChange }) + #expect(changes == settledChanges) + } + + @Test func enteringPictureInPictureNotifiesTheOwningPresenterBeforeQueuedRestorationLands() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let picture = fixture.windows[0] + let survivor = fixture.windows[1] + OPNStreamPictureInPicture.enter(picture, aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + + #expect(picture.isPictureInPicture && picture.frame.size == NSSize(width: 640, height: 360)) + #expect(picture.windowedState?.frame == fixture.originalFrames[0]) + #expect(survivor.frame == fixture.originalFrames[1]) + #expect(!survivor.layoutState.isAutoTiled && !survivor.layoutState.isBorderless) + #expect(survivor.level == .normal) + OPNStreamPictureInPicture.exit(picture) + await settleStreamWindowChanges() + + #expect(picture.frame == fixture.originalFrames[0]) + #expect(picture.layoutState.isManuallyPlaced && !picture.layoutState.isAutoTiled) + #expect(survivor.frame == fixture.originalFrames[1] && !survivor.layoutState.isAutoTiled) + } + + @Test func theReadersBorderlessToggleReleasesTheOtherTileThroughItsOwner() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let window = fixture.windows[0] + let survivor = fixture.windows[1] + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + + #expect(window.frame == fixture.originalFrames[0] && window.layoutState.isManuallyPlaced) + #expect(survivor.frame == fixture.originalFrames[1]) + #expect(!survivor.layoutState.isAutoTiled && survivor.level == .normal) + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + + #expect(window.layoutState.isBorderless && !window.layoutState.isAutoTiled) + #expect(window.frame == OPNStreamBorderless.targetFrame(for: window)) + #expect(survivor.frame == fixture.originalFrames[1] && !survivor.layoutState.isAutoTiled) + } + + @Test func nativeFullScreenNotificationsReleaseSurvivorsWithoutRestoringOverTheTransition() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let transitioning = fixture.windows[0] + let tileFrame = transitioning.frame + let survivor = fixture.windows[1] + let center = NotificationCenter.default + center.post(name: NSWindow.willEnterFullScreenNotification, object: transitioning) + fixture.presenter.retileWindows() + await settleStreamWindowChanges() + + #expect(transitioning.frame == tileFrame) + #expect(transitioning.layoutState.isManuallyPlaced && !transitioning.hasPendingLayoutChange) + #expect(!transitioning.layoutState.isBorderless && !transitioning.layoutState.isAutoTiled) + #expect(survivor.frame == fixture.originalFrames[1] && survivor.level == .normal) + center.post(name: NSWindow.didEnterFullScreenNotification, object: transitioning) + center.post(name: NSWindow.willExitFullScreenNotification, object: transitioning) + center.post(name: NSWindow.didExitFullScreenNotification, object: transitioning) + await settleStreamWindowChanges() + + #expect(transitioning.frame == tileFrame && !transitioning.layoutState.isAutoTiled) + #expect(survivor.frame == fixture.originalFrames[1] && !survivor.layoutState.isAutoTiled) + } + + @Test func minimizingATileDoesNotBringItBackWhenTheSurvivorIsReleased() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let minimized = fixture.windows[0] + minimized.miniaturize(nil) + await settleStreamWindowChanges() + fixture.presenter.retileWindows() + await settleStreamWindowChanges() + + #expect(minimized.isMiniaturized) + #expect(minimized.layoutState.isManuallyPlaced && !minimized.layoutState.isAutoTiled) + #expect(fixture.windows[1].frame == fixture.originalFrames[1]) + #expect(!fixture.windows[1].layoutState.isAutoTiled && fixture.windows[1].level == .normal) + minimized.deminiaturize(nil) + await settleStreamWindowChanges() + #expect(!minimized.layoutState.isAutoTiled && !fixture.windows[1].layoutState.isAutoTiled) + } + + @Test func coverageAndOrientationChangesRemainProgrammaticPlacements() async throws { + let fixture = try StreamWindowLayoutFixture(count: 3) + defer { fixture.close() } + await settleStreamWindowChanges() + fixture.presenter.setTileCoverage(.visibleArea) + OPNStreamTileOrientation.store(.topAndBottom) + fixture.presenter.retileWindows() + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(fixture.windows.map(\.frame) == fixture.expectedTiles(count: 3)) + #expect(fixture.windows.allSatisfy { $0.layoutState.isAutoTiled && !$0.layoutState.isManuallyPlaced }) + #expect(fixture.windows.allSatisfy { $0.level == .normal && !$0.hasPendingLayoutChange }) + } + + @Test func flagOffLeavesOrdinaryWindowsUntiledAcrossPlacementAndModeChanges() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2, isCouchCoopEnabled: false) + defer { fixture.close() } + await settleStreamWindowChanges() + let moved = fixture.windows[0] + moved.setFrame(moved.frame.offsetBy(dx: 20, dy: 0), display: true) + let readerFrame = moved.frame + OPNStreamPictureInPicture.enter(fixture.windows[1], aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + OPNStreamPictureInPicture.exit(fixture.windows[1]) + await settleStreamWindowChanges() + + #expect(moved.frame == readerFrame) + #expect(fixture.windows[1].frame == fixture.originalFrames[1]) + #expect(fixture.windows.allSatisfy { !$0.layoutState.isAutoTiled && !$0.layoutState.isBorderless }) + #expect(fixture.windows.allSatisfy { $0.level == .normal }) + } + + @Test func dismissingDuringDeferredPictureInPictureCancelsItsGeometryAndReleasesTheSurvivor() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let ending = fixture.windows[0] + fixture.fixtures[0].geometryView.isResizeHeld = true + OPNStreamPictureInPicture.enter(ending, aspectRatio: 16.0 / 9.0) + fixture.presenter.dismiss(fixture.sessions[0]) + fixture.fixtures[0].geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(fixture.presenter.windows[fixture.sessions[0]] == nil) + #expect(!ending.isVisible && ending.contentView == nil) + #expect(!ending.isPictureInPicture && !ending.isPictureInPicturePending && !ending.hasPendingLayoutChange) + #expect(fixture.windows[1].frame == fixture.originalFrames[1]) + #expect(!fixture.windows[1].layoutState.isAutoTiled && fixture.windows[1].level == .normal) + } +} diff --git a/Tests/Stream/StreamWindowModeTransitionTests.swift b/Tests/Stream/StreamWindowModeTransitionTests.swift new file mode 100644 index 000000000..28910a8e2 --- /dev/null +++ b/Tests/Stream/StreamWindowModeTransitionTests.swift @@ -0,0 +1,388 @@ +import AppKit +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized, .streamLifecycleExclusive, + .disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) +struct StreamWindowModeTransitionTests { + private let tile = NSRect(x: 100, y: 100, width: 800, height: 450) + + @Test func theHostRestoresBorderlessBeforePictureInPictureCapturesItsReturnPlacement() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + let originalStyle = window.styleMask + let originalBehavior = window.collectionBehavior + let carrier = makeCarrier(for: window) + let model = makeHost(in: window) + let configuredAspectRatio = CGFloat( + OPNStreamPreferences.launchProfile(forGame: model.configuration.applicationID, + capabilities: OPNStreamPreferences.loadDeviceCapabilities()).aspectRatio + ) + let expectedSize = OPNStreamPictureInPicture.contentSize(aspectRatio: configuredAspectRatio) + window.contentAspectRatio = NSSize(width: configuredAspectRatio, height: 1) + OPNStreamBorderless.enter(window, frame: tile, coversMenuBar: true) + await settleStreamWindowChanges() + #expect(window.frame == tile) + + model.togglePictureInPicture() + #expect(window.isPictureInPicturePending) + await settleStreamWindowChanges() + + #expect(window.isPictureInPicture && model.isPictureInPicture) + #expect(model.nativeView?.isPictureInPictureMode == true) + #expect(abs(window.frame.width - expectedSize.width) < 1) + #expect(abs(window.frame.height - expectedSize.height) < 1) + #expect(window.windowedState?.frame == original) + #expect(window.windowedState?.level == .normal) + #expect(window.level == .floating) + #expect(window.collectionBehavior == [.canJoinAllSpaces]) + #expect(carrier.collectionBehavior.contains(.canJoinAllSpaces)) + #expect(carrier.level.rawValue >= window.level.rawValue) + #expect(window.standardWindowButton(.closeButton)?.isHidden == true) + #expect(window.styleMask == originalStyle) + #expect(!window.isPictureInPicturePending && !window.hasPendingLayoutChange) + #expect(OPNStreamWindowFrameStore.rememberedFrame(isPictureInPicture: false, defaults: fixture.defaults) == original) + + model.togglePictureInPicture() + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !model.isPictureInPicture) + #expect(window.frame == original) + #expect(window.level == .normal && window.collectionBehavior == originalBehavior) + #expect(window.standardWindowButton(.closeButton)?.isHidden == false) + #expect(!carrier.collectionBehavior.contains(.canJoinAllSpaces)) + #expect(carrier.collectionBehavior.contains(.ignoresCycle)) + #expect(!carrier.collectionBehavior.contains(.fullScreenPrimary)) + } + + @Test func pictureInPictureWaitsForLiveResizeWithoutChangingTheWindowOrLosingItsOriginalFrame() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.enter(window, frame: tile, coversMenuBar: true) + await settleStreamWindowChanges() + fixture.geometryView.isResizeHeld = true + var isCompleted = false + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0, completion: { isCompleted = true }) + + await settleStreamWindowChanges() + + #expect(!isCompleted && !window.isPictureInPicture) + #expect(window.isPictureInPicturePending && window.hasPendingLayoutChange) + #expect(window.frame == tile && window.level == OPNStreamBorderless.coveringLevel) + #expect(window.borderlessRestoreState?.frame == original) + fixture.geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(isCompleted && window.isPictureInPicture) + #expect(window.frame.size == NSSize(width: 640, height: 360)) + #expect(window.windowedState?.frame == original) + #expect(!window.hasPendingLayoutChange) + } + + @Test func aSupersededTileCannotOverwritePictureInPictureAfterQueuedWorkLands() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.enter(window, frame: tile) + OPNStreamBorderless.enter(window, frame: tile.offsetBy(dx: 50, dy: 0), coversMenuBar: true) + var entryKeyWindow: NSWindow? + OPNStreamPictureInPicture.enter(window, aspectRatio: 4.0 / 3.0, prepareForEntry: { + entryKeyWindow = NSApp.keyWindow + return true + }, completion: { + #expect(NSApp.keyWindow === entryKeyWindow) + #expect(window.canBecomeKey) + }) + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(window.frame.size == NSSize(width: 640, height: 480)) + #expect(window.windowedState?.frame == original) + #expect(window.level == .floating && !window.layoutState.isBorderless) + #expect(window.resizeIncrements == NSSize(width: 1, height: 1)) + #expect(window.contentResizeIncrements == NSSize(width: 1, height: 1)) + OPNStreamPictureInPicture.exit(window) + await settleStreamWindowChanges() + #expect(window.frame == original) + } + + @Test func aNewBorderlessRequestSupersedesPendingPictureInPictureWithoutLosingTheRestoreState() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.enter(window, frame: tile) + await settleStreamWindowChanges() + fixture.geometryView.isResizeHeld = true + var isObsoleteEntryCompleted = false + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0, completion: { isObsoleteEntryCompleted = true }) + let replacement = tile.offsetBy(dx: 30, dy: 0) + OPNStreamBorderless.enter(window, frame: replacement, coversMenuBar: true) + fixture.geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(!isObsoleteEntryCompleted && !window.isPictureInPicturePending) + #expect(!window.isPictureInPicture && window.layoutState.isBorderless) + #expect(window.frame == replacement) + #expect(window.borderlessRestoreState?.frame == original) + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + #expect(window.isPictureInPicture && window.windowedState?.frame == original) + } + + @Test func cancellingADeferredEntryRestoresWindowedModeAndAllowsTheNextEntry() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.enter(window, frame: tile) + await settleStreamWindowChanges() + fixture.geometryView.isResizeHeld = true + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0) + OPNStreamPictureInPicture.exit(window) + fixture.geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !window.isPictureInPicturePending) + #expect(window.frame == original && !window.layoutState.isBorderless) + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + #expect(window.isPictureInPicture && window.windowedState?.frame == original) + } + + @Test func rapidBorderlessTogglesReverseThePendingExitWithoutReplacingTheOriginalReturnFrame() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + OPNStreamBorderless.toggleByReader(window) + #expect(window.isBorderlessExitPending) + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + + #expect(window.layoutState.isBorderless && !window.isBorderlessExitPending) + #expect(window.frame == OPNStreamBorderless.targetFrame(for: window)) + #expect(window.borderlessRestoreState?.frame == original) + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + #expect(window.frame == original && !window.layoutState.isBorderless) + } + + @Test(arguments: [false, true]) + func closingOrEndingTheSessionCancelsADeferredEntry(isClosing: Bool) async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + OPNStreamBorderless.enter(window, frame: tile) + await settleStreamWindowChanges() + fixture.geometryView.isResizeHeld = true + var isCompleted = false + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0, completion: { isCompleted = true }) + if isClosing { window.close() } + if !isClosing { window.endPresentation() } + fixture.geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(!isCompleted && !window.isPictureInPicture && !window.isPictureInPicturePending) + #expect(!window.hasPendingLayoutChange && window.frame == tile) + #expect(window.windowedState == nil) + } + + @Test func anInvalidatedHostCannotEnterAfterItsBorderlessRestorationCompletes() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + let model = makeHost(in: window) + OPNStreamBorderless.enter(window, frame: tile) + await settleStreamWindowChanges() + model.togglePictureInPicture() + model.isEnding = true + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !model.isPictureInPicture) + #expect(!window.isPictureInPicturePending && window.frame == original) + } + + @Test func cancellingTheHostsFullScreenSequenceDoesNotEnterPictureInPictureLater() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let model = makeHost(in: window) + let original = window.frame + model.isFullScreenTransitioning = true + model.togglePictureInPicture() + #expect(window.isPictureInPicturePending) + model.togglePictureInPicture() + model.isFullScreenTransitioning = false + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !model.isPictureInPicture) + #expect(!window.isPictureInPicturePending && window.frame == original) + } + + @Test func aNativeFullScreenPictureInPictureRequestSurvivesRetilingAfterASecondStreamLaunches() async throws { + let fixture = try StreamWindowFullScreenFixture() + defer { fixture.close() } + let window = fixture.fullScreen.window + let survivor = fixture.windowed.window + let survivorFrame = survivor.frame + let model = makeHost(in: window) + #expect(fixture.isNativeFullScreen && !window.isVisible) + #expect(!window.styleMask.contains(.fullScreen)) + #expect(!window.layoutState.isManuallyPlaced && !window.layoutState.isAutoTiled) + #expect(!survivor.layoutState.isAutoTiled) + model.isFullScreenTransitioning = true + model.togglePictureInPicture() + let requestID = window.pictureInPictureRequestID + #expect(requestID != nil) + fixture.beginExit() + fixture.landExit() + await settleStreamWindowChanges() + fixture.layout.presenter.retileWindows() + await settleStreamWindowChanges() + + #expect(window.isPictureInPicturePending && window.pictureInPictureRequestID == requestID) + #expect(!fixture.isNativeFullScreen && !window.styleMask.contains(.fullScreen)) + #expect(!window.layoutState.isManuallyPlaced && !window.layoutState.isAutoTiled) + #expect(window.frame == fixture.returnFrame && !window.hasPendingLayoutChange) + #expect(survivor.frame == survivorFrame && !survivor.layoutState.isAutoTiled) + model.isFullScreenTransitioning = false + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + let expectedSize = OPNStreamPictureInPicture.contentSize(aspectRatio: CGFloat( + OPNStreamPreferences.launchProfile(forGame: model.configuration.applicationID, + capabilities: OPNStreamPreferences.loadDeviceCapabilities()).aspectRatio + )) + #expect(window.isPictureInPicture && model.isPictureInPicture) + #expect(model.nativeView?.isPictureInPictureMode == true) + #expect(window.windowedState?.frame == fixture.returnFrame) + #expect(abs(window.frame.width - expectedSize.width) < 1) + #expect(abs(window.frame.height - expectedSize.height) < 1) + #expect(window.level == .floating && window.collectionBehavior == [.canJoinAllSpaces]) + #expect(!window.isPictureInPicturePending && !window.layoutState.isAutoTiled && !window.hasPendingLayoutChange) + #expect(survivor.frame == survivorFrame && survivor.level == .normal) + #expect(!survivor.layoutState.isAutoTiled && !survivor.layoutState.isManuallyPlaced) + } + + @Test(arguments: [false, true]) + func anAbortedFullScreenPictureInPictureRequestReturnsItsPlacementToThePresenter(isHostInvalidated: Bool) async throws { + let fixture = try StreamWindowFullScreenFixture() + defer { fixture.close() } + let window = fixture.fullScreen.window + let survivor = fixture.windowed.window + let model = makeHost(in: window) + model.isFullScreenTransitioning = true + model.togglePictureInPicture() + fixture.beginExit() + fixture.landExit() + await settleStreamWindowChanges() + #expect(window.isPictureInPicturePending && !window.layoutState.isManuallyPlaced) + + if isHostInvalidated { model.nativeView = nil } + if !isHostInvalidated { model.togglePictureInPicture() } + model.isFullScreenTransitioning = false + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !model.isPictureInPicture && !window.isPictureInPicturePending) + #expect(!window.layoutState.isManuallyPlaced && !survivor.layoutState.isManuallyPlaced) + #expect(window.layoutState.isAutoTiled && survivor.layoutState.isAutoTiled) + #expect([window.frame, survivor.frame] == fixture.layout.expectedTiles(count: 2)) + #expect(!window.hasPendingLayoutChange && !survivor.hasPendingLayoutChange) + } + + @Test func anUnconfirmedFullScreenExitReleasesItsPlacementWhenPictureInPictureRetriesExpire() async throws { + let fixture = try StreamWindowFullScreenFixture() + defer { fixture.close() } + let window = fixture.fullScreen.window + let survivor = fixture.windowed.window + let model = makeHost(in: window) + model.isFullScreenTransitioning = true + model.togglePictureInPicture() + fixture.beginExit() + fixture.landExit() + await settleStreamWindowChanges() + #expect(window.isPictureInPicturePending && !window.layoutState.isAutoTiled) + + for _ in 0..<(NativeNVSTHostViewModel.sessionReadyFullScreenAttemptLimit * 2) { + guard window.isPictureInPicturePending else { break } + try await Task.sleep(for: NativeNVSTHostViewModel.sessionReadyFullScreenRetryDelay) + } + model.isFullScreenTransitioning = false + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicturePending && !window.isPictureInPicture && !model.isPictureInPicture) + #expect(!window.layoutState.isManuallyPlaced && !survivor.layoutState.isManuallyPlaced) + #expect(window.layoutState.isAutoTiled && survivor.layoutState.isAutoTiled) + #expect([window.frame, survivor.frame] == fixture.layout.expectedTiles(count: 2)) + #expect(!window.hasPendingLayoutChange && !survivor.hasPendingLayoutChange) + } + + @Test func theAccountHandoffFollowsSettledCoveringLevelsThroughEntryPictureInPictureAndClosure() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let main = NSWindow(contentRect: tile, styleMask: [.titled], backing: .buffered, defer: false) + main.isReleasedWhenClosed = false + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { + window.isVisible && window.layoutState.isBorderless ? [window.level] : [] + }) + defer { + handoff.end() + main.close() + } + handoff.begin(from: nil) + handoff.attach(main) + OPNStreamBorderless.enter(window, frame: tile, coversMenuBar: true) + await settleStreamWindowChanges() + + #expect(window.level == OPNStreamBorderless.coveringLevel) + #expect(main.level.rawValue > window.level.rawValue) + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + #expect(window.isPictureInPicture && main.level == .normal) + OPNStreamPictureInPicture.exit(window) + OPNStreamBorderless.enter(window, frame: tile, coversMenuBar: true) + await settleStreamWindowChanges() + #expect(main.level.rawValue > window.level.rawValue) + window.close() + await settleStreamWindowChanges() + + #expect(!window.isVisible && main.level == .normal) + } + + private func makeHost(in window: OPNStreamWindow) -> NativeNVSTHostViewModel { + let model = NativeNVSTHostViewModel( + configuration: StreamLaunchConfiguration(title: "Geometry", applicationID: "100", accessToken: "token", accountLinked: true, selectedStore: "steam"), + sessionProvider: StubNativeNVSTSessionProvider(), preventDisplaySleep: false, + onProgress: nil, onEnd: { _, _, _ in } + ) + let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) + view.cursorAssociationHandler = { _ in .success } + view.cursorWarpHandler = { _ in } + view.hidesCursorWhilePointerLocked = false + window.contentView = view + model.nativeView = view + return model + } + + private func makeCarrier(for window: OPNStreamWindow) -> NSWindow { + let carrier = NSWindow(contentRect: tile, styleMask: .borderless, backing: .buffered, defer: false) + carrier.isReleasedWhenClosed = false + carrier.alphaValue = 0 + carrier.ignoresMouseEvents = true + window.addChildWindow(carrier, ordered: .above) + return carrier + } +} diff --git a/Tests/Stream/StreamWindowTransitionFixtures.swift b/Tests/Stream/StreamWindowTransitionFixtures.swift new file mode 100644 index 000000000..78bf38e4d --- /dev/null +++ b/Tests/Stream/StreamWindowTransitionFixtures.swift @@ -0,0 +1,168 @@ +import AppKit +import GameController +import Testing +@testable import OpenNOW + +@MainActor +final class StreamWindowGeometryDeferralView: NSView { + var isResizeHeld = false + + override var inLiveResize: Bool { isResizeHeld || super.inLiveResize } +} + +@MainActor +final class StreamWindowTransitionFixture { + let window: OPNStreamWindow + let geometryView: StreamWindowGeometryDeferralView + let defaults: UserDefaults + private let suite: String + + init(isShown: Bool = true) throws { + _ = NSApplication.shared + let suite = "OpenNOWTests.WindowTransition.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + let window = OPNStreamWindowFactory.make(sessionID: UUID(), defaults: defaults) + let geometryView = StreamWindowGeometryDeferralView(frame: window.contentView?.bounds ?? .zero) + self.suite = suite + self.defaults = defaults + self.window = window + self.geometryView = geometryView + window.contentView = geometryView + window.alphaValue = 0 + if isShown { window.orderFront(nil) } + } + + func close() { + window.stopPersistingFrame() + for child in window.childWindows ?? [] { + window.removeChildWindow(child) + child.close() + } + window.close() + defaults.removePersistentDomain(forName: suite) + } +} + +@MainActor +final class StreamWindowLayoutFixture { + let presenter: OPNStreamWindowPresenter + let sessions: [UUID] + let fixtures: [StreamWindowTransitionFixture] + let originalFrames: [NSRect] + private let previousFlag = OPNLabs.isCouchCoopEnabled + private let previousCoverage = OPNStreamTileCoverage.stored + private let previousOrientation = OPNStreamTileOrientation.stored + private let wasMonitoringBackgroundEvents = GCController.shouldMonitorBackgroundEvents + + var windows: [OPNStreamWindow] { fixtures.map(\.window) } + + init(count: Int, isCouchCoopEnabled: Bool = true, + isNativeFullScreen: @escaping @MainActor (OPNStreamWindow) -> Bool = { $0.styleMask.contains(.fullScreen) }) throws { + presenter = OPNStreamWindowPresenter(isNativeFullScreen: isNativeFullScreen) + #expect(!NSScreen.screens.isEmpty) + let fixtures = try (0.. [NSRect] { + OPNStreamTileLayout.tiles( + screens: OPNStreamTileCoverage.stored.areas( + frames: NSScreen.screens.map(\.frame), visibleFrames: NSScreen.screens.map(\.visibleFrame) + ), + windowCount: count, + orientation: OPNStreamTileOrientation.stored + ).map(\.frame) + } +} + +@MainActor +final class StreamWindowFullScreenFixture { + let layout: StreamWindowLayoutFixture + let fullScreen: StreamWindowTransitionFixture + let windowed: StreamWindowTransitionFixture + let returnFrame: NSRect + private let fullScreenSessionID: UUID + private let windowedSessionID: UUID + private let fullScreenStatus: StreamWindowFullScreenStatus + + var isNativeFullScreen: Bool { fullScreenStatus.isFullScreen } + + init() throws { + let fullScreen = try StreamWindowTransitionFixture(isShown: false) + let fullScreenStatus = StreamWindowFullScreenStatus() + let layout = try StreamWindowLayoutFixture(count: 0, isNativeFullScreen: { window in + if window === fullScreen.window { return fullScreenStatus.isFullScreen } + return window.styleMask.contains(.fullScreen) + }) + let fullScreenSessionID = UUID() + let windowedSessionID = UUID() + let returnFrame = fullScreen.window.frame + #expect(!fullScreen.window.isVisible) + #expect(!fullScreen.window.styleMask.contains(.fullScreen)) + layout.presenter.register(fullScreen.window, for: fullScreenSessionID) + NotificationCenter.default.post(name: NSWindow.willEnterFullScreenNotification, object: fullScreen.window) + NotificationCenter.default.post(name: NSWindow.didEnterFullScreenNotification, object: fullScreen.window) + let windowed = try StreamWindowTransitionFixture() + layout.presenter.register(windowed.window, for: windowedSessionID) + layout.presenter.retileWindows() + self.layout = layout + self.fullScreen = fullScreen + self.windowed = windowed + self.returnFrame = returnFrame + self.fullScreenSessionID = fullScreenSessionID + self.windowedSessionID = windowedSessionID + self.fullScreenStatus = fullScreenStatus + } + + func beginExit() { + NotificationCenter.default.post(name: NSWindow.willExitFullScreenNotification, object: fullScreen.window) + } + + func landExit() { + #expect(!fullScreen.window.isVisible) + #expect(!fullScreen.window.styleMask.contains(.fullScreen)) + fullScreenStatus.isFullScreen = false + NotificationCenter.default.post(name: NSWindow.didExitFullScreenNotification, object: fullScreen.window) + } + + func close() { + if isNativeFullScreen { + beginExit() + landExit() + } + layout.presenter.dismiss(fullScreenSessionID) + layout.presenter.dismiss(windowedSessionID) + fullScreen.close() + windowed.close() + layout.close() + } +} + +@MainActor +private final class StreamWindowFullScreenStatus { + var isFullScreen = true +} + +@MainActor +func settleStreamWindowChanges() async { + try? await Task.sleep(for: .milliseconds(200)) + await withCheckedContinuation { continuation in + DispatchQueue.main.async { continuation.resume() } + } +} diff --git a/View/Catalog/CatalogTopBarViews.swift b/View/Catalog/CatalogTopBarViews.swift index 67af37c03..6842636ca 100644 --- a/View/Catalog/CatalogTopBarViews.swift +++ b/View/Catalog/CatalogTopBarViews.swift @@ -14,6 +14,7 @@ struct CatalogTopBar: View { let onForget: (LoginAccount) -> Void @Environment(\.opnUIScale) private var uiScale @Environment(\.openWindow) private var openWindow + @Environment(\.opnReportIssuePresentation) private var reportIssuePresentation @AppStorage(OPNInterfacePreferences.controllerModeEnabledKey) private var controllerModeEnabled = false @ObservedObject private var keybindings = OPNKeybindingsObserver.shared @@ -98,7 +99,7 @@ struct CatalogTopBar: View { .opnTooltip("Search games (\(keybindings.combo(for: .openSearch).label))") .matchedGeometryEffect(id: Self.searchGeometryID, in: searchTransition) } - Button { OPNReportIssuePresentation.shared.present(context: .current(viewModel: viewModel)) } label: { + Button { (reportIssuePresentation ?? .shared).present(context: .current(viewModel: viewModel)) } label: { CatalogTopBarIconLabel(systemName: "exclamationmark.bubble") } .buttonStyle(.opnPressable(scale: 0.90)) diff --git a/View/Catalog/CatalogView.swift b/View/Catalog/CatalogView.swift index c9062b55f..f8439ec3c 100644 --- a/View/Catalog/CatalogView.swift +++ b/View/Catalog/CatalogView.swift @@ -138,6 +138,9 @@ struct CatalogView: View { let onForget: (LoginAccount) -> Void let onRefreshAuth: () async -> Bool let onWindowTitleChange: (String?) -> Void + /// A catalog hosted in a Detach New Session window. It belongs to one account and one window, so + /// it never takes over the menu bar's session source or its account list from the main window. + private let isDetached: Bool @Binding private var pendingGameShortcut: GFNGameShortcut? @@ -269,8 +272,10 @@ struct CatalogView: View { onSignOut: @escaping (LoginAccount) -> Void, onForget: @escaping (LoginAccount) -> Void, onRefreshAuth: @escaping () async -> Bool, - onWindowTitleChange: @escaping (String?) -> Void + onWindowTitleChange: @escaping (String?) -> Void, + isDetached: Bool = false ) { + self.isDetached = isDetached self.accounts = accounts self.signedOutAccountEmails = signedOutAccountEmails self.onSwitch = onSwitch @@ -454,7 +459,7 @@ struct CatalogView: View { await viewModel.awaitAccountScopedState() // Bound for as long as this window is on screen: the menu bar surface follows the // launch flow through it, and can hand a launch back to it while it exists. - viewModel.attachMenuBarSurface() + if !isDetached { viewModel.attachMenuBarSurface() } } // Reported from the view rather than from the fetch callbacks: the point of the signal is // that a frame carrying real content has been built, not that bytes arrived. @@ -464,6 +469,7 @@ struct CatalogView: View { } .onChange(of: pendingGameShortcut) { @MainActor _, _ in consumePendingGameShortcut() } .onChange(of: menuBarAccountsSignature, initial: true) { @MainActor _, _ in + guard !isDetached else { return } viewModel.updateMenuBarAccounts(accounts, signedOutAccountEmails: signedOutAccountEmails) } .onChange(of: viewModel.activeStreamConfiguration) { @MainActor _, _ in @@ -494,7 +500,7 @@ struct CatalogView: View { .onChange(of: cloudSync.pendingConflicts) { @MainActor _, _ in refreshSyncConflictAlert() } .onAppear { @MainActor in refreshSyncConflictAlert() } .onDisappear { @MainActor in - viewModel.detachMenuBarSurface() + if !isDetached { viewModel.detachMenuBarSurface() } onWindowTitleChange(nil) } .opnConfirmation( diff --git a/View/Components/OPNConfirmationModal.swift b/View/Components/OPNConfirmationModal.swift index 7176d9941..d5ec4741d 100644 --- a/View/Components/OPNConfirmationModal.swift +++ b/View/Components/OPNConfirmationModal.swift @@ -43,17 +43,26 @@ final class OPNConfirmationPresentation: ObservableObject { @Published private(set) var request: Request? private var onDismiss: (() -> Void)? + private var ownerID: UUID? - func present(_ request: Request, onDismiss: @escaping () -> Void) { + func present(_ request: Request, ownerID: UUID = UUID(), onDismiss: @escaping () -> Void) { + if self.ownerID != ownerID { dismiss() } + self.ownerID = ownerID self.request = request self.onDismiss = onDismiss } + func dismiss(ownerID: UUID) { + guard self.ownerID == ownerID else { return } + dismiss() + } + /// Clears the modal and asks whoever presented it to clear its own state. Safe to call when /// nothing is presented. func dismiss() { guard request != nil else { return } request = nil + ownerID = nil let dismissHandler = onDismiss onDismiss = nil dismissHandler?() @@ -97,7 +106,7 @@ struct OPNConfirmationPanel: View { /// Hosts the shared confirmation modal at the app root. The full-cover scrim sits behind the /// centered panel; the scrim, close control, Escape, and `.cancel` all dismiss. struct OPNConfirmationOverlay: View { - @ObservedObject private var presentation = OPNConfirmationPresentation.shared + @ObservedObject var presentation: OPNConfirmationPresentation = .shared var body: some View { ZStack { @@ -115,6 +124,10 @@ struct OPNConfirmationOverlay: View { } } +extension EnvironmentValues { + @Entry var opnConfirmationPresentation: OPNConfirmationPresentation? +} + /// App-shell confirmation dialog following the modal spec: Panel background, 1px Stroke Regular, /// 2px accent top bar, modal shadow, App Bar header with eyebrow and 20pt bold title, and a footer /// of square buttons. Replaces the native `.confirmationDialog` / `.alert`, which render rounded @@ -252,6 +265,8 @@ extension View { } private struct OPNConfirmationPresenter: ViewModifier { + @Environment(\.opnConfirmationPresentation) private var scopedPresentation + @State private var ownerID = UUID() let isPresented: Binding let eyebrow: String let title: String @@ -262,21 +277,24 @@ private struct OPNConfirmationPresenter: ViewModifier { content .onAppear { syncPresentation(isPresented.wrappedValue) } .onChange(of: isPresented.wrappedValue) { _, presented in syncPresentation(presented) } - .onDisappear { OPNConfirmationPresentation.shared.dismiss() } + .onDisappear { presentation.dismiss(ownerID: ownerID) } } + private var presentation: OPNConfirmationPresentation { scopedPresentation ?? .shared } + private func syncPresentation(_ presented: Bool) { guard presented else { - OPNConfirmationPresentation.shared.dismiss() + presentation.dismiss(ownerID: ownerID) return } - OPNConfirmationPresentation.shared.present( + presentation.present( OPNConfirmationPresentation.Request( eyebrow: eyebrow, title: title, message: message, actions: actions ), + ownerID: ownerID, onDismiss: { isPresented.wrappedValue = false } ) } diff --git a/View/Components/OPNReportIssueModal.swift b/View/Components/OPNReportIssueModal.swift index 8c6fdadd1..4bbbb5583 100644 --- a/View/Components/OPNReportIssueModal.swift +++ b/View/Components/OPNReportIssueModal.swift @@ -3,7 +3,7 @@ import SwiftUI /// Window-level host for the Report an Issue modal. Mounted once at the app root so the form /// reaches the catalog, the login wall, and Settings through the same path. struct OPNReportIssueOverlay: View { - @ObservedObject private var presentation = OPNReportIssuePresentation.shared + @ObservedObject var presentation: OPNReportIssuePresentation = .shared @Environment(\.opnUIScale) private var uiScale var body: some View { @@ -31,6 +31,14 @@ struct OPNReportIssueOverlay: View { } } +extension EnvironmentValues { + @Entry var opnReportIssuePresentation: OPNReportIssuePresentation? +} + +extension FocusedValues { + @Entry var opnReportIssuePresentation: OPNReportIssuePresentation? +} + /// App-shell support form following the modal spec: Panel background, 1px Stroke Regular, 2px accent /// top bar, modal shadow, App Bar header, and a square-button footer. /// diff --git a/View/ContentView.swift b/View/ContentView.swift index 005421794..b5f292668 100644 --- a/View/ContentView.swift +++ b/View/ContentView.swift @@ -114,6 +114,7 @@ struct ContentView: View { .environment(\.opnTileDensity, tileDensity) .opnObservingCornerStyle() .opnObservingLiquidGlass() + .focusedSceneValue(\.opnReportIssuePresentation, OPNReportIssuePresentation.shared) .onDisappear { root.unbind() } // Binding happens inside the bootstrap, not in an `onAppear`: SwiftUI starts a `.task` // before it calls `onAppear`, so the two would race. @@ -123,6 +124,10 @@ struct ContentView: View { .onChange(of: accounts.count) { _, _ in syncViewModel() } .onChange(of: sessions.count) { _, _ in syncViewModel() } .onChange(of: devices.count) { _, _ in syncViewModel() } + .onChange(of: viewModel.signInRequest) { previous, current in + guard previous != nil, current == nil else { return } + OPNMainWindowAccountHandoff.shared.end() + } .onOpenURL { url in root.handleOpenURL(url) } } @@ -199,6 +204,7 @@ private struct WindowTitleConfigurator: NSViewRepresentable { if #available(macOS 11.0, *) { window.titlebarSeparatorStyle = .none } + OPNMainWindowAccountHandoff.shared.attach(window) } } diff --git a/View/DetachedSession/DetachedSessionView.swift b/View/DetachedSession/DetachedSessionView.swift new file mode 100644 index 000000000..1d1aecddb --- /dev/null +++ b/View/DetachedSession/DetachedSessionView.swift @@ -0,0 +1,454 @@ +// The Detach New Session window: one more account streaming beside the ones already running. +// +// Browse and launch from an account-owned catalog without activating it in the main window. +// Explicit account-management actions use the main window's login flow. + +import AppKit +import SwiftData +import SwiftUI + +struct DetachedSessionView: View { + @Environment(\.modelContext) private var modelContext + @Environment(\.openWindow) private var openWindow + @Query(sort: \LoginAccount.lastLoginAt, order: .reverse) private var accounts: [LoginAccount] + @Query(sort: \LoginSession.issuedAt, order: .reverse) private var sessions: [LoginSession] + @AppStorage(OPNInterfacePreferences.uiScaleKey) private var uiScale = OPNInterfacePreferences.defaultUIScale + @AppStorage(OPNThemePreferences.accentColorKey) private var accentColorRawValue = OPNThemePreferences.AccentColor.cloudGreen.rawValue + @AppStorage(OPNThemePreferences.appearanceKey) private var appearanceRawValue = OPNThemePreferences.Appearance.dark.rawValue + @EnvironmentObject private var systemAppearance: OPNSystemAppearance + @ObservedObject private var devices = ControllerMappingDevices.shared + @State private var viewModel = DetachedSessionViewModel() + @State private var windowTitle = "Detached Session" + // swiftlint:disable:next view_constructs_service -- window-owned UI state, not a service; DESIGN.md Confirmation Modal + @StateObject private var confirmation = OPNConfirmationPresentation() + // swiftlint:disable:next view_constructs_service -- window-owned UI state, not a service; DESIGN.md Report an Issue Modal + @StateObject private var reportIssue = OPNReportIssuePresentation() + @State private var windowReference = DetachedSessionWindowReference() + + private let registry = OPNGameSessionRegistry.shared + + private var rows: [DetachedAccountRow] { + DetachedSessionModel.rows(candidates: candidates, ownedAccountIDs: Set(registry.sessions.map(\.accountID))) + } + + private var candidates: [DetachedAccountCandidate] { + accounts.map { account in + DetachedAccountCandidate( + email: account.email, + displayName: account.displayName, + membershipTier: account.membershipTier, + accountID: account.resolveStableAccountID(), + hasSavedSession: savedSession(for: account) != nil + ) + } + } + + private func savedSession(for account: LoginAccount) -> LoginSession? { + sessions.first { $0.accountEmail == account.email && !$0.accessToken.isEmpty } + } + + private var preferredColorScheme: ColorScheme? { + switch OPNThemePreferences.Appearance(rawValue: appearanceRawValue) ?? .dark { + case .system: nil + case .dark: .dark + case .light: .light + } + } + + var body: some View { + let _ = OPNDesign.applyTheme( + accent: OPNThemePreferences.AccentColor(rawValue: accentColorRawValue) ?? .cloudGreen, + appearance: OPNThemePreferences.Appearance(rawValue: appearanceRawValue) ?? .dark, + systemColorScheme: systemAppearance.colorScheme + ) + Group { + switch viewModel.phase { + case .setup: + setup + case .catalog(let email): + catalog(for: email) + } + } + .modifier(DetachedSessionModalHost(confirmation: confirmation, reportIssue: reportIssue)) + .frame(minWidth: 640, minHeight: 480) + .ignoresSafeArea() + .background(OPNDesign.Surface.app) + .background(DetachedSessionWindowConfigurator(title: windowTitle, reference: windowReference)) + .background(OPNInterfaceScaleDensityBooster(scale: uiScale)) + .environment(\.opnUIScale, uiScale) + .opnObservingCornerStyle() + .opnObservingLiquidGlass() + .preferredColorScheme(preferredColorScheme) + .onAppear { devices.refresh() } + .onChange(of: viewModel.phase) { _, phase in + guard phase == .setup else { return } + windowTitle = "Detached Session" + } + .onDisappear { + viewModel.discardPendingSelection() + confirmation.dismiss() + } + } + + private func requestMainAccountAction(_ request: OPNMainAccountActions.Request) { + OPNMainWindowAccountHandoff.shared.begin(from: windowReference.window) + if OPNMainWindow.needsPresentation { + OPNDockIconController.showDockIcon() + openWindow(id: OPNMainWindow.identifier) + } + OPNMainAccountActions.shared.request(request) + } + + private func requestAccountMutation(_ account: LoginAccount, request: (OPNMainAccountActions.Target) -> OPNMainAccountActions.Request) { + guard let storedAccounts = try? modelContext.fetch(FetchDescriptor()), + let currentAccount = storedAccounts.first(where: { $0 === account }), + !modelContext.deletedModelsArray.contains(where: { $0 === account }) else { + confirmation.present( + .init(eyebrow: "ACCOUNT", title: "Account action unavailable", + message: "This saved account changed or was removed. Choose the account again before continuing.", + actions: [OPNConfirmationAction("BACK") { [weak confirmation = confirmation] in confirmation?.dismiss() }]), + onDismiss: viewModel.backToSetup + ) + return + } + requestMainAccountAction(request(.init(account: currentAccount))) + } + + private var selectedAccount: LoginAccount? { + guard let email = viewModel.selectedEmail else { return nil } + return accounts.first { $0.email == email } + } + + // MARK: - Setup + + private var setup: some View { + ScrollView { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.large(scale: uiScale)) { + header + accountSection + controllerSection + footer + } + .padding(.horizontal, OPNDesign.Spacing.pageHorizontal(scale: uiScale)) + .padding(.top, OPNDesign.Spacing.xxLarge(scale: uiScale)) + .padding(.bottom, OPNDesign.Spacing.xLarge(scale: uiScale)) + .frame(maxWidth: 720 * uiScale) + .frame(maxWidth: .infinity) + } + } + + private var header: some View { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.xSmall(scale: uiScale)) { + Text("DETACH NEW SESSION") + .font(.settingsFont(size: 11 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.accentInk) + Text("Stream with another account") + .font(.settingsFont(size: 24 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Text("This window streams for the account you pick, beside the games already running. Each account plays its own session.") + .font(.settingsFont(size: 13 * uiScale)) + .foregroundStyle(OPNDesign.Text.secondary) + .fixedSize(horizontal: false, vertical: true) + } + } + + private var accountSection: some View { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.small(scale: uiScale)) { + sectionTitle("1 Account") + if rows.isEmpty { + note("No saved accounts yet. Add one to start.") + } + ForEach(rows) { row in + accountRow(row) + } + Button { + requestMainAccountAction(.addAccount) + } label: { + Label("Add account…", systemImage: "person.badge.plus") + } + .buttonStyle(OPNModalSecondaryButtonStyle(uiScale: uiScale)) + .help("Signs in another account in the main window without signing out the others.") + } + } + + private func accountRow(_ row: DetachedAccountRow) -> some View { + let isSelected = viewModel.selectedEmail == row.email + return Button { + guard row.availability.isSelectable else { return } + viewModel.selectedEmail = row.email + } label: { + HStack(spacing: OPNDesign.Spacing.small(scale: uiScale)) { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.xxSmall(scale: uiScale)) { + Text(row.displayName.isEmpty ? row.email : row.displayName) + .font(.settingsFont(size: 14 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + .lineLimit(1) + Text("\(row.email) · \(row.availability.detail)") + .font(.settingsFont(size: 11 * uiScale)) + .foregroundStyle(OPNDesign.Text.tertiary) + .lineLimit(1) + } + Spacer(minLength: 0) + if isSelected { + Image(systemName: "checkmark.circle.fill") + .foregroundStyle(OPNDesign.accentInk) + } + } + .padding(OPNDesign.Spacing.small(scale: uiScale)) + .frame(maxWidth: .infinity, alignment: .leading) + .background(OPNCornerShape(role: .card, scale: uiScale).fill(OPNDesign.Surface.panel)) + .overlay { + OPNCornerShape(role: .card, scale: uiScale) + .strokeBorder(isSelected ? OPNDesign.accent : OPNDesign.Stroke.subtle, lineWidth: isSelected ? 2 : 1) + } + .opacity(row.availability.isSelectable ? 1 : 0.5) + } + .buttonStyle(.plain) + .disabled(!row.availability.isSelectable) + .accessibilityLabel(row.displayName) + .accessibilityValue(row.availability.detail) + } + + private var controllerSection: some View { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.small(scale: uiScale)) { + sectionTitle("2 Controllers") + if devices.orderedDevices.isEmpty { + note("No controllers connected. Connect one and it appears here.") + } + ForEach(devices.orderedDevices) { device in + controllerRow(device) + } + if !devices.orderedDevices.isEmpty { + handOffRow + } + } + } + + private func controllerRow(_ device: ControllerMappingDevice) -> some View { + HStack(spacing: OPNDesign.Spacing.small(scale: uiScale)) { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.xxSmall(scale: uiScale)) { + Text(device.name) + .font(.settingsFont(size: 14 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + .lineLimit(1) + Text(viewModel.isControllerEnabled(device.id) ? "Plays in this stream" : "Disabled in this stream") + .font(.settingsFont(size: 11 * uiScale)) + .foregroundStyle(OPNDesign.Text.tertiary) + } + Spacer(minLength: 0) + Text("Disable in this stream") + .font(.settingsFont(size: 11 * uiScale)) + .foregroundStyle(OPNDesign.Text.secondary) + Toggle( + isOn: Binding( + get: { !viewModel.isControllerEnabled(device.id) }, + set: { viewModel.setController(device.id, enabled: !$0) } + ), + uiScale: uiScale + ) + .accessibilityLabel("Disable \(device.name) in this stream") + } + .padding(OPNDesign.Spacing.small(scale: uiScale)) + .background(OPNCornerShape(role: .card, scale: uiScale).fill(OPNDesign.Surface.panel)) + .overlay { OPNCornerShape(role: .card, scale: uiScale).strokeBorder(OPNDesign.Stroke.subtle, lineWidth: 1) } + } + + private var handOffRow: some View { + HStack(spacing: OPNDesign.Spacing.small(scale: uiScale)) { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.xxSmall(scale: uiScale)) { + Text("Take these controllers from the other streams") + .font(.settingsFont(size: 13 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Text("One tap sets a controller per player. Turn it back on from each stream's Controllers panel.") + .font(.settingsFont(size: 11 * uiScale)) + .foregroundStyle(OPNDesign.Text.tertiary) + .fixedSize(horizontal: false, vertical: true) + } + Spacer(minLength: 0) + Toggle(isOn: Binding(get: { viewModel.takesPadsFromOtherStreams }, set: { viewModel.takesPadsFromOtherStreams = $0 }), uiScale: uiScale) + .accessibilityLabel("Take these controllers from the other streams") + } + .padding(OPNDesign.Spacing.small(scale: uiScale)) + } + + private var footer: some View { + HStack { + Spacer() + Button("CHOOSE A GAME") { + guard let account = selectedAccount, let accountID = account.resolveStableAccountID(), + let row = rows.first(where: { $0.email == account.email }) else { return } + viewModel.confirm(row: row, accountID: accountID) + } + .buttonStyle(VendorGetInButtonStyle(uiScale: uiScale)) + .disabled(selectedAccount == nil) + .opacity(selectedAccount == nil ? 0.46 : 1) + } + } + + private func sectionTitle(_ title: String) -> some View { + Text(title.uppercased()) + .font(.settingsFont(size: 11 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.secondary) + } + + private func note(_ text: String) -> some View { + Text(text) + .font(.settingsFont(size: 12 * uiScale)) + .foregroundStyle(OPNDesign.Text.tertiary) + } + + // MARK: - Catalog + + @ViewBuilder + private func catalog(for email: String) -> some View { + if let account = accounts.first(where: { $0.email == email }), let session = savedSession(for: account) { + CatalogView( + account: account, + session: session, + accounts: accounts, + signedOutAccountEmails: Set(accounts.filter { savedSession(for: $0) == nil }.map(\.email)), + pendingGameShortcut: .constant(nil), + onSwitch: switchDetachedAccount, + onAddAccount: { requestMainAccountAction(.addAccount) }, + onPresentAccountChooser: viewModel.backToSetup, + onSignOut: { requestAccountMutation($0, request: OPNMainAccountActions.Request.signOut) }, + onForget: { requestAccountMutation($0, request: OPNMainAccountActions.Request.forget) }, + onRefreshAuth: { await DetachedSessionRefresher(modelContext: modelContext).refresh(account: account, session: session) }, + onWindowTitleChange: { windowTitle = $0 ?? account.displayName }, + isDetached: true + ) + .id(session.id) + .onAppear { windowTitle = account.displayName } + } else { + VStack(spacing: OPNDesign.Spacing.medium(scale: uiScale)) { + note("This account has no saved session. Sign in again from the main window.") + Button("SIGN IN AGAIN") { + guard let account = accounts.first(where: { $0.email == email }) else { + viewModel.backToSetup() + return + } + requestMainAccountAction(.reauthenticate(.init(account: account))) + } + .buttonStyle(VendorGetInButtonStyle(uiScale: uiScale)) + Button("BACK", action: viewModel.backToSetup) + .buttonStyle(OPNModalSecondaryButtonStyle(uiScale: uiScale)) + } + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + } + + private func switchDetachedAccount(_ account: LoginAccount) { + viewModel.backToSetup() + windowTitle = "Detached Session" + guard let storedAccounts = try? modelContext.fetch(FetchDescriptor()), + let currentAccount = storedAccounts.first(where: { $0 === account }), + let row = rows.first(where: { $0.email == currentAccount.email }) else { return } + guard savedSession(for: currentAccount) != nil else { + requestMainAccountAction(.reauthenticate(.init(account: currentAccount))) + return + } + guard row.availability.isSelectable, let accountID = currentAccount.storedAccountID else { return } + viewModel.confirm(row: row, accountID: accountID) + } +} + +struct DetachedSessionModalHost: ViewModifier { + @ObservedObject var confirmation: OPNConfirmationPresentation + @ObservedObject var reportIssue: OPNReportIssuePresentation + + func body(content: Content) -> some View { + ZStack { + content + OPNReportIssueOverlay(presentation: reportIssue) + .zIndex(92) + OPNConfirmationOverlay(presentation: confirmation) + .zIndex(95) + } + .environment(\.opnConfirmationPresentation, confirmation) + .environment(\.opnReportIssuePresentation, reportIssue) + .focusedSceneValue(\.opnReportIssuePresentation, reportIssue) + } +} + +@MainActor +private final class DetachedSessionWindowReference { + weak var window: NSWindow? +} + +/// A detached window is an ordinary titled window with the catalog's transparent titlebar, raised +/// above the borderless stream windows that cover the screens it is opened from. +private struct DetachedSessionWindowConfigurator: NSViewRepresentable { + let title: String + let reference: DetachedSessionWindowReference + + func makeNSView(context: Context) -> ConfigurationView { + let view = ConfigurationView(frame: .zero) + view.reference = reference + return view + } + + func updateNSView(_ view: ConfigurationView, context: Context) { + view.title = title + view.apply() + } + + static func dismantleNSView(_ view: ConfigurationView, coordinator: ()) { + view.unbind() + } + + final class ConfigurationView: NSView { + var title = "" + weak var reference: DetachedSessionWindowReference? + private var levelObserver: NSObjectProtocol? + + override func viewDidMoveToWindow() { + super.viewDidMoveToWindow() + reference?.window = window + guard window != nil else { + unbind() + return + } + apply() + // The level only matters when a stream window takes or gives up the covering level, and + // the stream windows say so. Reading it then keeps this window reachable without a timer. + if levelObserver == nil { + // swiftlint:disable:next view_owns_long_lived_effect -- removed when the view leaves its window; an NSView has no SwiftUI lifetime to tie it to + levelObserver = NotificationCenter.default.addObserver( + forName: OPNStreamBorderless.coveringStateDidChange, object: nil, queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.applyLevel() } + } + } + } + + func unbind() { + if let levelObserver { NotificationCenter.default.removeObserver(levelObserver) } + levelObserver = nil + reference?.window = nil + } + + func apply() { + guard let window else { return } + if !window.styleMask.contains(.fullSizeContentView) { + window.styleMask.insert(.fullSizeContentView) + } + window.titleVisibility = .hidden + window.titlebarAppearsTransparent = true + window.isOpaque = true + window.backgroundColor = .black + applyLevel() + if window.title != title { window.title = title } + } + + private func applyLevel() { + guard let window else { return } + let hasCoveringStream = OPNStreamWindowPresenter.shared.windows.values.contains { $0.layoutState.isBorderless } + let level = hasCoveringStream ? OPNStreamBorderless.aboveCoveringLevel : NSWindow.Level.normal + if window.level != level { window.level = level } + } + } +} + +enum OPNDetachedSessionScene { + static let id = "detached-session" +} diff --git a/View/Settings/SettingsLabsViews.swift b/View/Settings/SettingsLabsViews.swift index afba7a3ed..6eb6e2ad5 100644 --- a/View/Settings/SettingsLabsViews.swift +++ b/View/Settings/SettingsLabsViews.swift @@ -28,6 +28,70 @@ struct LabsSettingsPage: View { } } .settingsSection("labs") + if isCouchCoopEnabled { + CouchCoopTilingCard(uiScale: uiScale) + } + } + } + + @AppStorage(OPNLabs.couchCoop.storageKey) private var isCouchCoopEnabled = false +} + +/// How two or more stream windows share the screens. Shown only while Couch Co-op is on, since +/// nothing else reads it. A change places the windows that are already open at once. +struct CouchCoopTilingCard: View { + let uiScale: CGFloat + + @State private var orientation = OPNStreamTileOrientation.auto + @State private var coverage = OPNStreamTileCoverage.visibleArea + + var body: some View { + SettingsCard(title: "Couch Co-op Layout", badge: .experimental, uiScale: uiScale) { + Text("With two accounts on two screens each stream fills its own screen. When streams share a screen they split it, and this decides how.") + .font(.settingsFont(size: 12 * uiScale, weight: .medium)) + .foregroundStyle(OPNDesign.Text.tertiary) + .fixedSize(horizontal: false, vertical: true) + SettingsDivider(uiScale: uiScale) + dropdownRow("Split shared screens", selection: orientation.title, items: OPNStreamTileOrientation.allCases.map { option in + OPNDropdownItem(id: option.id, title: option.title, isSelected: option == orientation) { + orientation = option + OPNStreamTileOrientation.store(option) + OPNStreamWindowPresenter.shared.retileWindows() + } + }) + SettingsDivider(uiScale: uiScale) + dropdownRow("Stream windows sit", selection: coverage.title, items: OPNStreamTileCoverage.allCases.map { option in + OPNDropdownItem(id: option.id, title: option.title, isSelected: option == coverage) { + coverage = option + OPNStreamWindowPresenter.shared.setTileCoverage(option) + } + }) + } + .onAppear { + orientation = OPNStreamTileOrientation.stored + coverage = OPNStreamTileCoverage.stored + } + } + + private func dropdownRow(_ title: String, selection: String, items: [OPNDropdownItem]) -> some View { + HStack { + Text(title) + .font(.settingsFont(size: 13 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Spacer(minLength: 0) + OPNDropdownMenu(items: items) { + HStack(spacing: OPNDesign.Spacing.xSmall(scale: uiScale)) { + Text(selection) + .font(.settingsFont(size: 12 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Image(systemName: "chevron.down") + .font(.settingsFont(size: 9 * uiScale, weight: .bold)) + } + .padding(.horizontal, OPNDesign.Spacing.controlRow(scale: uiScale)) + .frame(height: 30 * uiScale) + .background(OPNCornerShape(role: .control, scale: uiScale).fill(OPNDesign.Fill.neutral(0.075))) + .overlay { OPNCornerShape(role: .control, scale: uiScale).strokeBorder(OPNDesign.Stroke.regular, lineWidth: 1) } + } } } } @@ -37,6 +101,7 @@ struct LabsFlagRow: View { let uiScale: CGFloat @State private var isEnabled = false + @State private var isRestartPromptPresented = false var body: some View { SettingsToggleRow( @@ -47,8 +112,22 @@ struct LabsFlagRow: View { ) { enabled in isEnabled = enabled OPNLabs.setEnabled(flag, enabled) + if flag.requiresRestart { isRestartPromptPresented = true } } .onAppear { isEnabled = OPNLabs.isEnabled(flag) } + .opnConfirmation( + isPresented: $isRestartPromptPresented, + eyebrow: "LABS", + title: "Restart to apply \(flag.title)?", + message: "\(flag.title) takes full effect when OpenNOW starts. Restarting ends any stream that is running.", + actions: [ + OPNConfirmationAction("LATER", role: .cancel) { isRestartPromptPresented = false }, + OPNConfirmationAction("RESTART NOW") { + isRestartPromptPresented = false + OPNAppRelaunch.relaunch() + } + ] + ) } } diff --git a/View/Settings/SettingsReportViews.swift b/View/Settings/SettingsReportViews.swift index 79eb99fc8..b1d2f3c34 100644 --- a/View/Settings/SettingsReportViews.swift +++ b/View/Settings/SettingsReportViews.swift @@ -2,6 +2,7 @@ import SwiftUI /// The support entry beside Support Diagnostics: one button into the two-channel report form. struct ReportIssueSettingsPage: View { + @Environment(\.opnReportIssuePresentation) private var reportIssuePresentation let viewModel: CatalogViewModel let uiScale: CGFloat @@ -13,7 +14,7 @@ struct ReportIssueSettingsPage: View { SettingsCard(title: "Report an Issue", uiScale: uiScale) { HStack(alignment: .top, spacing: 10 * uiScale) { SettingsActionButton(title: "REPORT AN ISSUE", uiScale: uiScale) { - OPNReportIssuePresentation.shared.present(context: .current(viewModel: viewModel)) + (reportIssuePresentation ?? .shared).present(context: .current(viewModel: viewModel)) } Text("Open a pre-filled OpenNOW client report on GitHub, or send a stream quality issue to NVIDIA with a copyable session summary.") .font(.settingsFont(size: 12 * uiScale, weight: .medium)) diff --git a/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift b/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift index b4d112db2..9cb86deaa 100644 --- a/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift +++ b/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift @@ -38,6 +38,20 @@ extension NativeNVSTMediaStreamSurface { } var nativeHUDDisplayTiles: [NativeHUDTile] { + let tiles = nativeHUDBaseDisplayTiles + guard model.isBorderlessTileAvailable else { return tiles } + return tiles + [ + NativeHUDTile(id: "borderless-full-screen", + title: model.borderlessTileTitle, + subtitle: model.borderlessTileSubtitle, + systemName: "rectangle.dashed", + isActive: model.isBorderlessTileActive, + isDisabled: model.isBorderlessTileDisabled, + action: model.toggleBorderlessFullScreen), + ] + } + + private var nativeHUDBaseDisplayTiles: [NativeHUDTile] { [ NativeHUDTile(id: "floating-stats", title: model.nativeStatsVisible ? "Hide Floating Stats" : "Show Floating Stats", diff --git a/View/Stream/NativeNVSTStreamStatsHUD.swift b/View/Stream/NativeNVSTStreamStatsHUD.swift index d4d4e0748..b8064562a 100644 --- a/View/Stream/NativeNVSTStreamStatsHUD.swift +++ b/View/Stream/NativeNVSTStreamStatsHUD.swift @@ -227,6 +227,7 @@ struct NativeNVSTStatsOverlay: View { let hdrModeText: String /// The seat's applied DLSS override state, nil until it reports one. let appliedDlssState: NvstDlssStateNotification? + let dlssEntitlements: DlssEntitlements var body: some View { NativeNVSTStatsPanel( @@ -285,7 +286,7 @@ struct NativeNVSTStatsOverlay: View { /// The request the next session will carry, which is all there is to report until the seat /// sends its own state. It composes the same labels the settings card's summary does. private var nativeStatsNextSessionDlssGroup: NativeNVSTStatsPanel.Group { - let overrides = OPNStreamPreferences.effectiveDlssOverrides() + let overrides = OPNStreamPreferences.loadDlssOverrides().applyingAccess(dlssEntitlements) return NativeNVSTStatsPanel.Group(label: "DLSS · NEXT SESSION", rows: [ NativeNVSTStatsPanel.Row(label: "Super Resolution", value: overrides.superResolutionRequestLabel), NativeNVSTStatsPanel.Row(label: "Frame Generation", value: overrides.frameGenerationRequestLabel), @@ -608,7 +609,8 @@ extension NativeNVSTMediaStreamSurface { applicationID: configuration.applicationID, detailLevel: model.statsDetail, hdrModeText: model.nativeHdrModeText, - appliedDlssState: model.nativeDlssState + appliedDlssState: model.nativeDlssState, + dlssEntitlements: model.configuration.launchEntitlements.dlss ) .opnTransition(.scale(scale: 0.94, anchor: model.statsPosition.transitionAnchor).combined(with: .opacity)) .streamStatsHUDPosition(model.statsPosition, isSidebarVisible: model.unifiedHUDVisible) diff --git a/View/Stream/OPNStreamWindowPresenter.swift b/View/Stream/OPNStreamWindowPresenter.swift index a7261f840..15a5f8cd4 100644 --- a/View/Stream/OPNStreamWindowPresenter.swift +++ b/View/Stream/OPNStreamWindowPresenter.swift @@ -10,6 +10,7 @@ // import AppKit +import GameController import SwiftUI @MainActor @@ -19,10 +20,18 @@ final class OPNStreamWindowPresenter { /// One window per session, keyed by the session's own id: two accounts can stream at once, and /// each window belongs to the session that opened it. private(set) var windows: [UUID: OPNStreamWindow] = [:] + /// Creation order, which is the order the couch co-op layout deals windows out in. + private var windowOrder: [UUID] = [] private var presentedConfigurationIDs: [UUID: UUID] = [:] private var hostingViews: [UUID: NSHostingView] = [:] /// Held so the observation is not dropped the moment it is installed. private var sessionChangeObserver: NSObjectProtocol? + private var screenChangeObserver: NSObjectProtocol? + private let isNativeFullScreen: @MainActor (OPNStreamWindow) -> Bool + + init(isNativeFullScreen: @escaping @MainActor (OPNStreamWindow) -> Bool = { $0.styleMask.contains(.fullScreen) }) { + self.isNativeFullScreen = isNativeFullScreen + } /// Per-window full-screen bookkeeping, tracked from `present` so a dismissal that lands /// mid-enter is recognised before AppKit sets `.fullScreen`. @@ -113,6 +122,14 @@ final class OPNStreamWindowPresenter { ) { [weak self] _ in MainActor.assumeIsolated { self?.syncWithOwnedSessions() } } + // swiftlint:disable:next view_owns_long_lived_effect -- application-lifetime AppKit presenter, installed once by the app delegate + screenChangeObserver = NotificationCenter.default.addObserver( + forName: NSApplication.didChangeScreenParametersNotification, + object: nil, + queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.retileWindows() } + } } /// Brings every owned session's window up to date: one window per session that has a stream, and @@ -139,7 +156,7 @@ final class OPNStreamWindowPresenter { // the first and read as a replacement. Cascaded clear, the way macOS does it. let cascaded = Self.cascadedFrame(for: window.frame, taken: windows.values.map(\.frame)) window.setFrame(OPNStreamWindowFrameStore.onScreen(cascaded), display: false) - let hostingView = NSHostingView(rootView: OPNStreamWindowRootView(configuration: configuration, session: session)) + let hostingView = NSHostingView(rootView: OPNStreamWindowRootView(configuration: configuration, session: session, layoutState: window.layoutState)) window.contentView = hostingView // The game's title belongs to the window the game is in; the catalog window keeps its own. window.title = Self.windowTitle(for: configuration) @@ -150,11 +167,9 @@ final class OPNStreamWindowPresenter { } OPNStreamWindowCloseGuard.install(on: window) - windows[session.id] = window + register(window, for: session.id) hostingViews[session.id] = hostingView presentedConfigurationIDs[session.id] = configuration.id - // Tracked for the window's whole life so a mid-enter dismissal sees the transition. - track(window) // A launch is not a "show me" action. `OPNMainWindow.present(activating:)` sets the rule the // whole app follows - a session launch leaves the app where it is, and the Session Ready // preference keeps the only say over whether OpenNOW comes forward - and an unconditional @@ -165,20 +180,38 @@ final class OPNStreamWindowPresenter { case .takeKey: window.makeKeyAndOrderFront(nil) case .orderFrontWithoutActivating: window.orderFront(nil) } + retileWindows() + updateBackgroundGamepadInput() OPNLog.info(.launch, "Stream window presented for \(configuration.applicationID) session=\(session.id)") } + func register(_ window: OPNStreamWindow, for sessionID: UUID) { + guard windows[sessionID] !== window else { return } + dismiss(sessionID) + windows[sessionID] = window + windowOrder.append(sessionID) + track(window) + window.layoutMembershipDidChange = { [weak self, weak window] in + guard let self, let window, self.windows[sessionID] === window else { return } + self.retileWindows() + } + } + /// Takes one session's window away. A close button or a cancellation can both land here for the /// same ending; the second call has nothing left to do. func dismiss(_ sessionID: UUID) { guard let window = windows[sessionID] else { return } + window.endPresentation() presentedConfigurationIDs[sessionID] = nil hostingViews[sessionID] = nil windows[sessionID] = nil + windowOrder.removeAll { $0 == sessionID } OPNStreamWindowCloseGuard.uninstall(from: window) window.closeRequestHandler = nil window.sessionSurface = nil dismissWindow(window) + retileWindows() + updateBackgroundGamepadInput() } /// Decides whether this window can go out now or has to wait for a full-screen transition. @@ -253,6 +286,76 @@ final class OPNStreamWindowPresenter { styleMask.contains(.fullScreen) } + /// Takes `deviceIDs` out of every stream that is already running, so the pads a new window plays + /// with stop answering in the others. Streams that have not resolved their surface yet have + /// nothing to take them from. + func takePads(_ deviceIDs: Set) { + guard !deviceIDs.isEmpty else { return } + for window in windows.values { window.sessionSurface?.disableControllersForStream(deviceIDs) } + } + + /// Lets pads keep playing in the background exactly while couch co-op has two or more streams up, + /// and tells GameController to deliver their values while the app is not frontmost. + private func updateBackgroundGamepadInput() { + let isAllowed = OPNCouchCoopInputPolicy.allowsBackgroundGamepads( + isFlagEnabled: OPNLabs.isCouchCoopEnabled, + liveStreamCount: windows.count + ) + GCController.shouldMonitorBackgroundEvents = isAllowed + for window in windows.values { window.allowsBackgroundGamepadInput = isAllowed } + } + + /// Which windows the couch co-op layout places, and where. A window the reader placed, a window + /// in Picture in Picture and a window in its own full-screen Space are not the layout's to move. + /// Fewer than two placeable windows is not a layout at all: tiled windows go back to where they + /// were, and with the flag off nothing is ever tiled. + func retileWindows() { + let ordered = windowOrder.compactMap { windows[$0] } + let placeable = OPNLabs.isCouchCoopEnabled + ? ordered.filter { isPlaceableByLayout($0) } + : [] + for window in ordered where window.layoutState.isAutoTiled && !placeable.contains(where: { $0 === window }) { + releaseTile(window) + } + guard placeable.count >= 2 else { + for window in ordered where window.layoutState.isAutoTiled { releaseTile(window) } + return + } + let coverage = OPNStreamTileCoverage.stored + let tiles = OPNStreamTileLayout.tiles( + screens: coverage.areas(frames: NSScreen.screens.map(\.frame), visibleFrames: NSScreen.screens.map(\.visibleFrame)), + windowCount: placeable.count, + orientation: OPNStreamTileOrientation.stored + ) + guard tiles.count == placeable.count else { return } + for (window, tile) in zip(placeable, tiles) { + window.layoutState.isAutoTiled = true + OPNStreamBorderless.enter(window, frame: tile.frame, coversMenuBar: coverage == .fullScreen) + } + } + + /// Switches tiled windows between sitting below the menu bar and filling their screens, and + /// places them again at once. + func setTileCoverage(_ coverage: OPNStreamTileCoverage) { + OPNStreamTileCoverage.store(coverage) + retileWindows() + } + + var hasTiledWindows: Bool { windows.values.contains { $0.layoutState.isAutoTiled } } + + private func isPlaceableByLayout(_ window: OPNStreamWindow) -> Bool { + let windowPresentation = presentations[ObjectIdentifier(window)] + return !window.layoutState.isManuallyPlaced && !window.isPictureInPicture && !window.isPictureInPicturePending && + !window.isMiniaturized && !window.isPresentationEnding && !isNativeFullScreen(window) && + windowPresentation?.transition == FullScreenTransition.none + } + + private func releaseTile(_ window: OPNStreamWindow) { + window.layoutState.isAutoTiled = false + OPNStreamBorderless.exit(window) + window.clearLayoutFrame() + } + /// Observers live on the `WindowPresentation`, never on the presenter, so two windows at once /// route to the right one. @discardableResult @@ -276,13 +379,49 @@ final class OPNStreamWindowPresenter { self?.endTransition(.exiting, for: id) } } - presentation.observerTokens = [willEnter, didEnter, willExit, didExit] + let layoutTokens = [NSWindow.didMoveNotification, NSWindow.didResizeNotification].map { name in + center.addObserver(forName: name, object: window, queue: .main) { [weak window] _ in + MainActor.assumeIsolated { + guard let window else { return } + OPNStreamWindowPresenter.noteFrameChange(window) + } + } + } + let willMiniaturizeObserver = center.addObserver(forName: NSWindow.willMiniaturizeNotification, object: window, queue: .main) { [weak window] _ in + MainActor.assumeIsolated { + guard let window, !window.isApplyingLayoutChange else { return } + OPNStreamBorderless.takePlacement(window) + } + } + let miniaturizationObserverTokens = [NSWindow.didMiniaturizeNotification, NSWindow.didDeminiaturizeNotification].map { name in + center.addObserver(forName: name, object: window, queue: .main) { [weak self] _ in + MainActor.assumeIsolated { self?.retileWindows() } + } + } + presentation.observerTokens = [willEnter, didEnter, willExit, didExit, willMiniaturizeObserver] + layoutTokens + miniaturizationObserverTokens presentations[id] = presentation return presentation } + /// The owner callback retiles survivors; the observer must not issue a second retile. + @discardableResult + static func noteFrameChange(_ window: OPNStreamWindow) -> Bool { + // No layout frame yet means the placement is still waiting on the geometry gate, so a move + // that lands before it is not the reader's. + guard !window.isApplyingLayoutChange, window.layoutState.isAutoTiled, + window.layoutFrame != nil, !window.isAtLayoutFrame else { return false } + OPNStreamBorderless.takePlacement(window) + return true + } + private func setTransition(_ transition: FullScreenTransition, for id: ObjectIdentifier) { - presentations[id]?.transition = transition + guard let presentation = presentations[id] else { return } + presentation.transition = transition + let window = presentation.window + if window.layoutState.isAutoTiled || window.layoutState.isBorderless { + OPNStreamBorderless.takePlacement(window) + } + retileWindows() } private func endTransition(_ transition: FullScreenTransition, for id: ObjectIdentifier) { @@ -290,6 +429,7 @@ final class OPNStreamWindowPresenter { guard presentation.transition == transition else { return } presentation.transition = .none advanceDismissal(presentation) + retileWindows() } private func removeTransitionObservers(from presentation: WindowPresentation) { @@ -310,11 +450,13 @@ final class OPNStreamWindowPresenter { private func tearDownWindow(_ window: OPNStreamWindow) { window.stopPersistingFrame() window.orderOut(nil) + OPNStreamBorderless.postCoveringStateChange(for: window) window.contentView = nil // A late re-presentation must not leave the contentless window on screen. Task { @MainActor [weak window] in guard let window, window.isVisible else { return } window.orderOut(nil) + OPNStreamBorderless.postCoveringStateChange(for: window) } } diff --git a/View/Stream/OPNStreamWindowRootView.swift b/View/Stream/OPNStreamWindowRootView.swift index 961f75749..d26aa36cc 100644 --- a/View/Stream/OPNStreamWindowRootView.swift +++ b/View/Stream/OPNStreamWindowRootView.swift @@ -17,6 +17,7 @@ import SwiftUI struct OPNStreamWindowRootView: View { let configuration: StreamLaunchConfiguration let session: OPNGameSession + let layoutState: OPNStreamWindowLayoutState @AppStorage(OPNInterfacePreferences.uiScaleKey) private var uiScale = OPNInterfacePreferences.defaultUIScale @AppStorage(OPNThemePreferences.accentColorKey) private var accentColorRawValue = OPNThemePreferences.AccentColor.cloudGreen.rawValue @AppStorage(OPNThemePreferences.appearanceKey) private var appearanceRawValue = OPNThemePreferences.Appearance.dark.rawValue @@ -45,7 +46,7 @@ struct OPNStreamWindowRootView: View { GeometryReader { proxy in StreamStageLayout( viewport: proxy.size, - topInset: windowTopInset, + topInset: layoutState.isBorderless ? 0 : windowTopInset, aspectRatio: CGFloat(session.streamProfile.aspectRatio) ) { _ in StreamHostView( @@ -75,7 +76,7 @@ struct OPNStreamWindowRootView: View { } .ignoresSafeArea(edges: .all) .background(Color.black) - .background(StreamWindowAspectConfigurator(aspectRatio: session.streamProfile.aspectRatio, isLocked: true)) + .background(StreamWindowAspectConfigurator(aspectRatio: session.streamProfile.aspectRatio, isLocked: !layoutState.releasesAspectLock)) .environment(\.opnUIScale, uiScale) .opnObservingCornerStyle() .opnObservingLiquidGlass() diff --git a/View/Stream/StreamHost.swift b/View/Stream/StreamHost.swift index 905dd22d9..475be3ba2 100644 --- a/View/Stream/StreamHost.swift +++ b/View/Stream/StreamHost.swift @@ -86,6 +86,7 @@ struct NativeNVSTMediaStreamSurface: View { // so arriving a turn later is safe. NativeNVSTStreamHostView( mappingGameIdentity: configuration.mappingGameIdentity, + owningAccountID: configuration.owningAccountID, onResolve: { view in Task { @MainActor in model.nativeView = view @@ -172,11 +173,12 @@ private struct NativeNVSTStreamHostView: NSViewRepresentable { /// The game this surface is streaming, so its pad mappings resolve from its own overrides rather /// than from whichever game's session started last. let mappingGameIdentity: String + let owningAccountID: String let onResolve: @MainActor (NativeStreamView) -> Void var onWindowChanged: @MainActor (NSWindow?) -> Void = { _ in } func makeNSView(context: Context) -> NativeNVSTSurfaceContainerView { - let view = NativeNVSTSurfaceContainerView(frame: .zero, mappingGameIdentity: mappingGameIdentity) + let view = NativeNVSTSurfaceContainerView(frame: .zero, mappingGameIdentity: mappingGameIdentity, owningAccountID: owningAccountID) view.onResolve = onResolve view.onWindowChanged = onWindowChanged return view @@ -208,8 +210,16 @@ private struct NativeNVSTStreamHostView: NSViewRepresentable { var onWindowChanged: (@MainActor (NSWindow?) -> Void)? private var didResolve = false - init(frame frameRect: NSRect, mappingGameIdentity: String) { - streamView = NativeStreamView(frame: .zero, mappingProvider: ControllerMappingGameProvider(gameIdentity: mappingGameIdentity)) + init(frame frameRect: NSRect, mappingGameIdentity: String, owningAccountID: String) { + let selection = OPNStreamControllerPreset.shared.take(forAccount: owningAccountID) + streamView = NativeStreamView( + frame: .zero, + mappingProvider: ControllerMappingGameProvider(gameIdentity: mappingGameIdentity), + initiallyDisabledControllers: selection.disabled + ) + OPNStreamWindowPresenter.shared.takePads( + selection.handedOff(connected: ControllerMappingDevices.shared.orderedDevices.map(\.id)) + ) super.init(frame: frameRect) wantsLayer = true layer?.backgroundColor = NSColor.black.cgColor diff --git a/ViewModel/AppRootViewModel.swift b/ViewModel/AppRootViewModel.swift index 31bacfa03..eb52fb2c1 100644 --- a/ViewModel/AppRootViewModel.swift +++ b/ViewModel/AppRootViewModel.swift @@ -27,6 +27,7 @@ final class AppRootViewModel: ObservableObject { func bind(login: LoginViewModel) { self.login = login + OPNMainAccountActions.shared.bind(login: login) guard fileOpenObserver == nil else { return } fileOpenObserver = NotificationCenter.default .publisher(for: .opnDidOpenFile) @@ -38,6 +39,7 @@ final class AppRootViewModel: ObservableObject { func unbind() { fileOpenObserver = nil + if let login { OPNMainAccountActions.shared.unbind(login: login) } login = nil } diff --git a/ViewModel/CatalogAccountRequestIdentity.swift b/ViewModel/CatalogAccountRequestIdentity.swift new file mode 100644 index 000000000..dc1ded72d --- /dev/null +++ b/ViewModel/CatalogAccountRequestIdentity.swift @@ -0,0 +1,49 @@ +import Foundation +import SwiftData + +struct CatalogAccountRequestIdentity: Equatable, Sendable { + private let accountEmail: String + private let accountID: String + private let accountUserID: String + private let accountProviderID: String + private let lastLoginAt: Date + private let sessionID: String + private let sessionEmail: String + private let userID: String + private let providerID: String + private let accessToken: String + private let idToken: String + private let refreshToken: String + private let issuedAt: Date + private let isAccountStored: Bool + private let isSessionStored: Bool + + @MainActor + init(account: LoginAccount, session: LoginSession) { + accountEmail = account.email + accountID = account.stableAccountID + accountUserID = account.userId + accountProviderID = account.providerIdpId + lastLoginAt = account.lastLoginAt + sessionID = session.id + sessionEmail = session.accountEmail + userID = session.userId + providerID = session.idpId + accessToken = session.accessToken + idToken = session.idToken + refreshToken = session.refreshToken + issuedAt = session.issuedAt + isAccountStored = account.modelContext != nil + isSessionStored = session.modelContext != nil + } + + @MainActor + func isCurrent(account: LoginAccount, session: LoginSession) -> Bool { + guard !account.isDeleted, !session.isDeleted, + !isAccountStored || account.modelContext != nil, + !isSessionStored || session.modelContext != nil, + account.email == session.accountEmail, + !session.accessToken.isEmpty || !session.idToken.isEmpty else { return false } + return self == Self(account: account, session: session) + } +} diff --git a/ViewModel/CatalogBrowsing.swift b/ViewModel/CatalogBrowsing.swift index 700e132e3..785bc537d 100644 --- a/ViewModel/CatalogBrowsing.swift +++ b/ViewModel/CatalogBrowsing.swift @@ -152,6 +152,7 @@ extension CatalogViewModel { } func browseCatalog(forceRefresh: Bool) { + guard configureCatalogService() else { return } browseGeneration += 1 let generation = browseGeneration let browseStartTime = CFAbsoluteTimeGetCurrent() @@ -159,7 +160,6 @@ extension CatalogViewModel { isLoadingMoreCatalog = false catalogEndCursor = "" errorMessage = "" - configureCatalogService() let query = searchQuery.trimmed let resultCount = catalogGames.count gameService.browseCatalogObject( diff --git a/ViewModel/CatalogHomeSections.swift b/ViewModel/CatalogHomeSections.swift index 19b20845d..aea3830d2 100644 --- a/ViewModel/CatalogHomeSections.swift +++ b/ViewModel/CatalogHomeSections.swift @@ -268,6 +268,33 @@ extension CatalogViewModel { // rather than in the view model's class body: they are read from many bodies, and a cached getter // reads its inputs on the cached path so Observation still registers the dependency. extension CatalogViewModel { + var marqueeGames: [OPNCatalogGameObject] { + _ = marqueePanels + if let cachedMarqueeGames { return cachedMarqueeGames } + var games: [OPNCatalogGameObject] = [] + var seen = Set() + for panel in marqueePanels { + for section in panel.sections { + for game in section.games { + let key = Self.identity(for: game) + guard !key.isEmpty, !seen.contains(key) else { continue } + seen.insert(key) + games.append(game) + } + } + } + cachedMarqueeGames = games + return games + } + + var heroRotationGames: [OPNCatalogGameObject] { + _ = marqueePanels + if let cachedHeroRotationGames { return cachedHeroRotationGames } + let games = Self.dedupedByTitleGrouping(marqueeGames.filter(Self.hasMarqueeHeroArtwork)) + cachedHeroRotationGames = games + return games + } + var visibleFilterGroups: [OPNCatalogFilterGroupObject] { _ = filterGroups if let cachedVisibleFilterGroups { return cachedVisibleFilterGroups } diff --git a/ViewModel/CatalogLaunchFlow.swift b/ViewModel/CatalogLaunchFlow.swift index 6b4c2c0f3..0f1d11ca5 100644 --- a/ViewModel/CatalogLaunchFlow.swift +++ b/ViewModel/CatalogLaunchFlow.swift @@ -103,7 +103,7 @@ extension CatalogViewModel { } func openGameShortcut(_ shortcut: GFNGameShortcut) { - configureCatalogService() + guard configureCatalogService() else { return } let title = shortcut.lookupTitle.isEmpty ? shortcut.displayName : shortcut.lookupTitle OPNLog.info(.shortcut, "CatalogViewModel resolving shortcut cmsId=\(shortcut.cmsId) shortName=\(shortcut.shortName) parentGameId=\(shortcut.parentGameId) title=\(title)") setActionMessage("Opening \(title.isEmpty ? "GeForce NOW shortcut" : title)...") @@ -182,6 +182,10 @@ extension CatalogViewModel { /// and before any window is replaced, or reports why it could not. Another account's game is not /// in the way: the limit is one game per account. private func beginOwnedSession() -> OPNGameSession? { + guard CatalogAccountRequestIdentity(account: account, session: session).isCurrent(account: account, session: session) else { + reportLaunchFailure("Sign in again before launching a game.") + return nil + } guard let accountID = account.resolveStableAccountID() else { reportLaunchFailure("This account has no saved identity to start a game with. Sign in again.") return nil @@ -192,7 +196,9 @@ extension CatalogViewModel { gameService: gameService, launchBridge: launchBridge, discordPresence: discordPresence, - streamProfile: streamProfile + streamProfile: streamProfile, + launchEntitlements: currentLaunchEntitlements, + providerStreamingBaseURL: gameService.providerStreamingBaseURL() ) else { reportLaunchFailure(refusalMessage(for: accountID)) return nil @@ -264,7 +270,7 @@ extension CatalogViewModel { guard launchFlowState == .idle, activeStreamConfiguration == nil, !isCheckingHomeSession else { return } isCheckingHomeSession = true let token = launchToken - let streamingBaseUrl = OPNStreamPreferences.loadSelectedStreamingBaseUrl() + let streamingBaseUrl = OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: gameService.providerStreamingBaseURL()) OPNActiveSessionService.fetchActiveSessions(accessToken: token, streamingBaseUrl: streamingBaseUrl, deviceId: resolveCloudmatchDeviceId()) { [weak self] ok, sessions, _ in guard let self else { return } self.isCheckingHomeSession = false diff --git a/ViewModel/CatalogMenuBarSurface.swift b/ViewModel/CatalogMenuBarSurface.swift index e08eea795..0a2998fab 100644 --- a/ViewModel/CatalogMenuBarSurface.swift +++ b/ViewModel/CatalogMenuBarSurface.swift @@ -222,7 +222,7 @@ extension CatalogViewModel: OPNMenuBarSessionSource { /// reopened from the menu bar starts fetching as it appears — so an unknown game is resolved by /// browsing for its title, the same way an unresolved shortcut is. func launchGame(_ game: OPNMenuBarGame) { - configureCatalogService() + guard configureCatalogService() else { return } let title = game.title.trimmingCharacters(in: .whitespacesAndNewlines) OPNLog.info(.launch, "Menu bar launching game appId=\(game.appId) title=\(title)") if let match = knownGame(matching: game, title: title) { diff --git a/ViewModel/CatalogOwnership.swift b/ViewModel/CatalogOwnership.swift index f9967c85b..568b8d43f 100644 --- a/ViewModel/CatalogOwnership.swift +++ b/ViewModel/CatalogOwnership.swift @@ -55,7 +55,7 @@ extension CatalogViewModel { } func toggleFavoriteSelectedGame() { - guard let selectedGame else { return } + guard let selectedGame, configureCatalogService() else { return } let appId = Self.favoriteAppId(for: selectedGame) let identity = Self.identity(for: selectedGame) guard !appId.isEmpty, !identity.isEmpty else { return } @@ -223,7 +223,8 @@ extension CatalogViewModel { } func autoMarkFreeToPlaySelectedVariantThenLaunch() { - guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty else { return } + guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty, + configureCatalogService() else { return } let gameIdentity = Self.identity(for: selectedGame) let variantId = variant.id let variantIndex = selectedVariantIndex @@ -247,7 +248,7 @@ extension CatalogViewModel { } func beginMarkSelectedVariantOwnedFlow() { - guard let selectedGame, selectedVariant(in: selectedGame) != nil else { return } + guard let selectedGame, selectedVariant(in: selectedGame) != nil, configureCatalogService() else { return } presentedModal = .storePicker(stage: .resyncing) ownershipFlowMessage = syncingOwnershipMessage(for: selectedGame) let stores = CatalogAccountParsing.uniqueNonEmpty(selectedGame.variants.map(\.appStore)) @@ -274,7 +275,8 @@ extension CatalogViewModel { } func confirmSelectedVariantOwned() { - guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty else { return } + guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty, + configureCatalogService() else { return } let gameIdentity = Self.identity(for: selectedGame) let variantId = variant.id let title = selectedGame.title.isEmpty ? "game" : selectedGame.title @@ -298,7 +300,8 @@ extension CatalogViewModel { } func removeSelectedVariantOwned() { - guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty else { return } + guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty, + configureCatalogService() else { return } let gameIdentity = Self.identity(for: selectedGame) let variantId = variant.id let title = selectedGame.title.isEmpty ? "game" : selectedGame.title @@ -316,7 +319,7 @@ extension CatalogViewModel { } func selectOwnedVariant(_ variant: OPNCatalogGameVariantObject) { - guard !variant.id.isEmpty else { return } + guard !variant.id.isEmpty, configureCatalogService() else { return } let variantId = variant.id gameService.selectOwnedVariant(variantId) { [weak self] success, error in guard let self else { return } @@ -336,7 +339,7 @@ extension CatalogViewModel { } func syncStoreAccount(_ store: String) { - guard !store.isEmpty else { return } + guard !store.isEmpty, configureCatalogService() else { return } setActionMessage("Syncing \(displayName(forStore: store)) account...") gameService.syncAccountProvider(store: store) { [weak self] success, error in guard let self else { return } @@ -358,7 +361,7 @@ extension CatalogViewModel { } func linkStoreAccount(_ store: String) { - guard !store.isEmpty else { return } + guard !store.isEmpty, configureCatalogService() else { return } setActionMessage("Opening \(displayName(forStore: store)) account linking...") gameService.startAccountLinking(store: store) { [weak self] success, error in guard let self else { return } diff --git a/ViewModel/CatalogPanelLoading.swift b/ViewModel/CatalogPanelLoading.swift index bc8ece87e..485b789ed 100644 --- a/ViewModel/CatalogPanelLoading.swift +++ b/ViewModel/CatalogPanelLoading.swift @@ -57,10 +57,10 @@ extension CatalogViewModel { } func loadPanels() { + guard configureCatalogService() else { return } isLoadingPanels = true isLoadingMarquee = true errorMessage = "" - configureCatalogService() // The launch prefetch may already own (or have finished) these queries; // adopt whatever it has instead of asking for the same panels twice. @@ -190,7 +190,7 @@ extension CatalogViewModel { } func loadLibrary() { - configureCatalogService() + guard configureCatalogService() else { return } isLoadingLibrary = true // See `loadFavorites()`: the launch prefetch fires this alongside the home panels, under // the splash screen, so it has often already arrived by the time this gate lets it through. @@ -221,7 +221,7 @@ extension CatalogViewModel { } func loadFavorites() { - configureCatalogService() + guard configureCatalogService() else { return } isLoadingFavorites = true // The launch prefetch fires this alongside the home panels, under the splash screen, so // by the time the panels rail unblocks this call it has often already arrived (or arrives @@ -240,12 +240,12 @@ extension CatalogViewModel { /// Reloads a list the user has just changed. The launch snapshot is dropped first, so this /// goes to the network instead of adopting the state that existed before the change. func reloadFavoritesAfterChange() { - CatalogLaunchPrefetch.shared.invalidate(.favorites) + CatalogLaunchPrefetch.shared.invalidate(.favorites, accountIdentifier: catalogAccountIdentifier) loadFavorites() } func reloadLibraryAfterChange() { - CatalogLaunchPrefetch.shared.invalidate(.library) + CatalogLaunchPrefetch.shared.invalidate(.library, accountIdentifier: catalogAccountIdentifier) loadLibrary() } @@ -300,9 +300,20 @@ extension CatalogViewModel { } func loadAccount() { + let identity = CatalogAccountRequestIdentity(account: account, session: session) + entitlementIdentity = nil + let requestID = UUID() + subscriptionRequestID = requestID + guard identity.isCurrent(account: account, session: session) else { + subscriptionStatus = .unavailable + dlssMembershipState = .signedOut + launchEntitlements = .none + return + } configureCatalogService() gameService.fetchUserAccount { [weak self] success, account, error in - guard let self else { return } + guard let self, self.subscriptionRequestID == requestID, + identity.isCurrent(account: self.account, session: self.session) else { return } if success { self.accountStores = CatalogAccountParsing.parseStoreAccounts(account) self.accountSubscriptions = CatalogAccountParsing.parseAccountSubscriptions(account) @@ -315,28 +326,25 @@ extension CatalogViewModel { guard !userId.isEmpty else { subscriptionStatus = .unavailable dlssMembershipState = .signedOut - OPNStreamPreferences.clearDlssEntitlements() + launchEntitlements = .none return } dlssMembershipState = .loading - // The stored copy is what every session request is filtered against, so it is cleared before - // the fetch rather than left holding the previous account's membership. - OPNStreamPreferences.clearDlssEntitlements() + launchEntitlements = .none gameService.fetchSubscriptionInfo(userId: userId) { [weak self] success, subscription, error in - // A switch or sign-out can complete before this answers, and the stored entitlements are - // global: a stale success would filter the next account's requests with this one's. - guard let self, self.session.isActive else { return } + guard let self, self.subscriptionRequestID == requestID, + identity.isCurrent(account: self.account, session: self.session) else { return } guard success else { - if self.refreshAuthIfNeeded(error: error) { self.subscriptionStatus = .unavailable } + self.subscriptionStatus = .unavailable + _ = self.refreshAuthIfNeeded(error: error) self.dlssMembershipState = .unavailable - OPNStreamPreferences.clearDlssEntitlements() + self.launchEntitlements = .none return } self.subscriptionStatus = CatalogSubscriptionStatus(subscription: subscription) - OPNStreamPreferences.saveEntitledAudioChannelCount(subscription.entitledAudioChannelCount) - OPNStreamPreferences.saveEntitledInGameSettingsPersistence(subscription.isInGameSettingsPersistenceEntitled) - // Re-evaluated on every account change. The saved preference is kept whatever this says. - OPNStreamPreferences.saveDlssEntitlements(subscription.dlssEntitlements) + self.launchEntitlements = OPNLaunchEntitlements(subscription: subscription) + self.entitlementIdentity = identity + self.updateActiveAccountEntitlementMirror(subscription) self.dlssMembershipState = .loaded(subscription.dlssEntitlements) let membershipTier = subscription.membershipTier.trimmingCharacters(in: .whitespacesAndNewlines) guard !membershipTier.isEmpty else { return } @@ -344,8 +352,21 @@ extension CatalogViewModel { } } + var currentLaunchEntitlements: OPNLaunchEntitlements { + guard let entitlementIdentity, + entitlementIdentity.isCurrent(account: account, session: session) else { return .none } + return launchEntitlements + } + + private func updateActiveAccountEntitlementMirror(_ subscription: OPNSubscriptionInfo) { + guard account.isActive, session.isActive else { return } + OPNStreamPreferences.saveEntitledAudioChannelCount(subscription.entitledAudioChannelCount) + OPNStreamPreferences.saveEntitledInGameSettingsPersistence(subscription.isInGameSettingsPersistenceEntitled) + OPNStreamPreferences.saveDlssEntitlements(subscription.dlssEntitlements) + } + func loadStores() { - configureCatalogService() + guard configureCatalogService() else { return } gameService.fetchStoreDefinitions { [weak self] success, definitions, _ in guard let self else { return } if success { self.storeDefinitions = definitions.map(CatalogAccountParsing.parseStoreDefinition) } diff --git a/ViewModel/CatalogPatchingAndState.swift b/ViewModel/CatalogPatchingAndState.swift index 6047cfa0c..de4cdcceb 100644 --- a/ViewModel/CatalogPatchingAndState.swift +++ b/ViewModel/CatalogPatchingAndState.swift @@ -304,9 +304,12 @@ extension CatalogViewModel { } } - func configureCatalogService() { + @discardableResult + func configureCatalogService() -> Bool { + guard CatalogAccountRequestIdentity(account: account, session: session).isCurrent(account: account, session: session) else { return false } let userId = session.userId.isEmpty ? account.userId : session.userId gameService.configureCatalogSession(accessToken: session.accessToken, idToken: session.idToken, userId: userId) + return true } func refreshAuthIfNeeded(error: String) -> Bool { diff --git a/ViewModel/CatalogViewModel+Settings.swift b/ViewModel/CatalogViewModel+Settings.swift index 3f8cff677..44dbaf406 100644 --- a/ViewModel/CatalogViewModel+Settings.swift +++ b/ViewModel/CatalogViewModel+Settings.swift @@ -634,7 +634,7 @@ extension CatalogViewModel { /// default output device and the membership's entitlement. var surroundModeSubtitle: String { let deviceChannels = streamCapabilities.audioOutputChannelCount - let entitled = OPNStreamPreferences.loadEntitledAudioChannelCount() + let entitled = currentLaunchEntitlements.audioChannelCount let negotiated = StreamSettingsResolver.audioChannelCount(surroundMode: streamProfile.surroundMode.value, deviceOutputChannels: deviceChannels, entitledChannels: entitled) let layout = negotiated >= 8 ? "7.1" : (negotiated >= 6 ? "5.1" : "stereo") var text = "Output device has \(deviceChannels) channel\(deviceChannels == 1 ? "" : "s"); next session streams \(layout)." diff --git a/ViewModel/CatalogViewModel.swift b/ViewModel/CatalogViewModel.swift index 0a8d4e2c5..1c927e461 100644 --- a/ViewModel/CatalogViewModel.swift +++ b/ViewModel/CatalogViewModel.swift @@ -327,6 +327,9 @@ final class CatalogViewModel { } } var subscriptionStatus = CatalogSubscriptionStatus.unavailable + var launchEntitlements = OPNLaunchEntitlements.none + var entitlementIdentity: CatalogAccountRequestIdentity? + var subscriptionRequestID = UUID() /// The saved global cloud DLSS override, mirrored here so the card renders from state. var dlssOverrides = DlssOverrides.useGameSettings /// What the account's membership includes, kept apart from the entitlements so a membership that @@ -438,11 +441,11 @@ final class CatalogViewModel { /// Whether that work has landed; `startupContentGate` holds the splash until it has. var isAccountScopedStateLoaded = false - init(account: LoginAccount, session: LoginSession, gameService: any CatalogGameServing = OPNGameService.shared, launchBridge: any GameLaunchBridging = OPNGameLaunchBridge.shared, imageCache: any CatalogImageServing = CatalogImageCache.shared, discordPresence: any DiscordPresenceServing = DiscordRichPresence.shared, systemIntegration: any SystemIntegrationServing = AppKitSystemIntegration(), sessionRegistry: OPNGameSessionRegistry = .shared, sessionResultStore: OPNGameSessionResultStore = .shared, dlssCompatibilityService: OPNDlssCompatibilityService = .shared, dlssAnnouncer: OPNDlssCompatibilityAnnouncer = OPNDlssCompatibilityAnnouncer(), onSwitchAccount: @escaping (LoginAccount) -> Void = { _ in }, onAddAccount: @escaping () -> Void = {}, onPresentAccountChooser: @escaping () -> Void = {}, onRefreshAuth: @escaping () async -> Bool) { + init(account: LoginAccount, session: LoginSession, gameService: any CatalogGameServing = OPNGameService(), launchBridge: (any GameLaunchBridging)? = nil, imageCache: any CatalogImageServing = CatalogImageCache.shared, discordPresence: any DiscordPresenceServing = DiscordRichPresence.shared, systemIntegration: any SystemIntegrationServing = AppKitSystemIntegration(), sessionRegistry: OPNGameSessionRegistry = .shared, sessionResultStore: OPNGameSessionResultStore = .shared, dlssCompatibilityService: OPNDlssCompatibilityService = .shared, dlssAnnouncer: OPNDlssCompatibilityAnnouncer = OPNDlssCompatibilityAnnouncer(), onSwitchAccount: @escaping (LoginAccount) -> Void = { _ in }, onAddAccount: @escaping () -> Void = {}, onPresentAccountChooser: @escaping () -> Void = {}, onRefreshAuth: @escaping () async -> Bool) { self.account = account self.session = session self.gameService = gameService - self.launchBridge = launchBridge + self.launchBridge = launchBridge ?? OPNGameLaunchBridge(gameService: (gameService as? any GameLaunchServiceConfiguring) ?? OPNGameService()) self.imageCache = imageCache self.discordPresence = discordPresence self.systemIntegration = systemIntegration @@ -527,33 +530,6 @@ final class CatalogViewModel { cachedVisibleFilterGroups = nil } - var marqueeGames: [OPNCatalogGameObject] { - _ = marqueePanels - if let cachedMarqueeGames { return cachedMarqueeGames } - var games: [OPNCatalogGameObject] = [] - var seen = Set() - for panel in marqueePanels { - for section in panel.sections { - for game in section.games { - let key = Self.identity(for: game) - guard !key.isEmpty, !seen.contains(key) else { continue } - seen.insert(key) - games.append(game) - } - } - } - cachedMarqueeGames = games - return games - } - - var heroRotationGames: [OPNCatalogGameObject] { - _ = marqueePanels - if let cachedHeroRotationGames { return cachedHeroRotationGames } - let games = Self.dedupedByTitleGrouping(marqueeGames.filter(Self.hasMarqueeHeroArtwork)) - cachedHeroRotationGames = games - return games - } - var isBrowseMode: Bool { !selectedFilterIds.isEmpty || searchQuery.contains { !$0.isWhitespace } } @@ -593,13 +569,22 @@ final class CatalogViewModel { func refresh() { // An explicit refresh must hit the network, not re-adopt the panels the // launch prefetch already handed over. - CatalogLaunchPrefetch.shared.invalidate() + CatalogLaunchPrefetch.shared.invalidate(accountIdentifier: catalogAccountIdentifier) loadCatalogDataAfterProviderConfiguration(forceCatalogRefresh: true) } func loadCatalogDataAfterProviderConfiguration(forceCatalogRefresh: Bool = false) { - configureCatalogService() - Task { await configureCatalogProviderEndpoint() } + let identity = CatalogAccountRequestIdentity(account: account, session: session) + guard identity.isCurrent(account: account, session: session) else { return } + Task { [weak self] in + guard let self else { return } + await configureCatalogProviderEndpoint() + guard identity.isCurrent(account: account, session: session) else { return } + loadConfiguredCatalogData(forceCatalogRefresh: forceCatalogRefresh) + } + } + + private func loadConfiguredCatalogData(forceCatalogRefresh: Bool) { // Claim the library and favorites rail slots up front rather than when their (deferred) // fetches start. Setting them later meant the rails appeared mid-load and pushed // everything under them down the page. This has to run before `loadPanels()`, whose @@ -640,7 +625,7 @@ final class CatalogViewModel { self.loadFavorites() // Every launch attach point has now run, so the prefetch's copy of the panels and the // game lists is a duplicate from here on. - CatalogLaunchPrefetch.shared.recordLaunchResultsAdopted() + CatalogLaunchPrefetch.shared.recordLaunchResultsAdopted(accountIdentifier: self.catalogAccountIdentifier) self.loadAccount() self.loadStores() } diff --git a/ViewModel/DetachedSession/DetachedSessionModel.swift b/ViewModel/DetachedSession/DetachedSessionModel.swift new file mode 100644 index 000000000..3dcb507db --- /dev/null +++ b/ViewModel/DetachedSession/DetachedSessionModel.swift @@ -0,0 +1,71 @@ +// What the Detach New Session window decides, as plain values. +// +// The window picks an account for a new stream and the controllers that stream leaves out. Which +// accounts can be picked, and what the controller choice becomes, do not need a window or a store, +// so they live here where a test can reach them. + +import Foundation + +struct DetachedAccountCandidate: Equatable, Sendable { + let email: String + let displayName: String + let membershipTier: String + let accountID: OPNAccountID? + let hasSavedSession: Bool +} + +enum DetachedAccountAvailability: Equatable, Sendable { + case available + case runningGame + case signInRequired + + var isSelectable: Bool { self == .available } + + var detail: String { + switch self { + case .available: "Ready to stream" + case .runningGame: "Already streaming in another window" + case .signInRequired: "Sign in required" + } + } +} + +struct DetachedAccountRow: Identifiable, Equatable, Sendable { + let email: String + let displayName: String + let membershipTier: String + let availability: DetachedAccountAvailability + + var id: String { email } +} + +enum DetachedSessionModel { + /// An account that already owns a game cannot start a second one, and one with no saved session + /// has nothing to stream with, so neither is offered as a choice. + static func rows(candidates: [DetachedAccountCandidate], ownedAccountIDs: Set) -> [DetachedAccountRow] { + candidates.map { candidate in + DetachedAccountRow( + email: candidate.email, + displayName: candidate.displayName, + membershipTier: candidate.membershipTier, + availability: availability(of: candidate, ownedAccountIDs: ownedAccountIDs) + ) + } + } + + static func availability(of candidate: DetachedAccountCandidate, ownedAccountIDs: Set) -> DetachedAccountAvailability { + guard candidate.hasSavedSession, let accountID = candidate.accountID else { return .signInRequired } + return ownedAccountIDs.contains(accountID) ? .runningGame : .available + } + + /// The pads the reader switched off for the new stream become its preset, and the hand-off + /// toggle says whether the pads it keeps are taken out of the streams already running. + static func selection(disabled: Set, takesPadsFromOtherStreams: Bool) -> OPNStreamControllerSelection { + OPNStreamControllerSelection(disabled: disabled, takesPadsFromOtherStreams: takesPadsFromOtherStreams) + } + + /// The pads a stream would keep, in the order the reader sees them. + static func keptControllers(connected: [InputDeviceID], disabled: Set) -> [InputDeviceID] { + connected.filter { !disabled.contains($0) } + } +} diff --git a/ViewModel/DetachedSession/DetachedSessionRefresher.swift b/ViewModel/DetachedSession/DetachedSessionRefresher.swift new file mode 100644 index 000000000..621522ddd --- /dev/null +++ b/ViewModel/DetachedSession/DetachedSessionRefresher.swift @@ -0,0 +1,156 @@ +// Refreshes a saved account's tokens without making it the active account. +// +// `LoginViewModel` refreshes through one auth service whose session is the active account's, and +// re-activates the account it restores. A detached window's account is not the active one, so it +// gets a service of its own and writes the refreshed tokens back to that account's stored session +// only, leaving every `isActive` flag where it was. + +import Foundation +import SwiftData + +@MainActor +struct DetachedSessionRefresher { + let modelContext: ModelContext + private let refreshSession: @Sendable (JarvisSession) async throws -> JarvisSession + + init( + modelContext: ModelContext, + refreshSession: @escaping @Sendable (JarvisSession) async throws -> JarvisSession = { session in + let service = JarvisAuthService(transport: JarvisURLSessionTransport(), session: session) + return try await service.refreshSession(force: !session.isIdTokenValid) + } + ) { + self.modelContext = modelContext + self.refreshSession = refreshSession + } + + func refresh(account: LoginAccount, session storedSession: LoginSession) async -> Bool { + guard contains(account: account, session: storedSession) else { return false } + let ownership = RefreshOwnership(account: account, session: storedSession) + guard isCurrent(ownership, account: account, session: storedSession) else { return false } + var jarvisSession = JarvisSession( + accessToken: storedSession.accessToken, + idToken: storedSession.idToken, + refreshToken: storedSession.refreshToken, + userId: storedSession.userId.isEmpty ? account.userId : storedSession.userId, + displayName: account.displayName, + email: account.email, + membershipTier: account.membershipTier, + idpId: storedSession.idpId.isEmpty ? account.providerIdpId : storedSession.idpId, + expiresAt: Int64(storedSession.expiresAt.timeIntervalSince1970), + isAuthenticated: true, + clientToken: storedSession.clientToken, + clientTokenExpiry: Int64(storedSession.clientTokenExpiresAt.timeIntervalSince1970 * 1000.0), + clientTokenExpiryLength: 0, + accessTokenExpiry: Int64(storedSession.expiresAt.timeIntervalSince1970 * 1000.0) + ) + if jarvisSession.idTokenExpiry == 0 { + jarvisSession.idTokenExpiry = JarvisSessionParser.idTokenExpiry(storedSession.idToken) + } + do { + let refreshed = try await refreshSession(jarvisSession) + guard !Task.isCancelled, isCurrent(ownership, account: account, session: storedSession) else { return false } + guard !refreshed.accessToken.isEmpty, + jarvisSession.userId.isEmpty || refreshed.userId.isEmpty || refreshed.userId == jarvisSession.userId else { return false } + try store(refreshed, in: storedSession) + OPNLog.info(.auth, "Detached session refreshed account=\(ownership.email)") + return true + } catch is CancellationError { + return false + } catch { + OPNLog.warning(.auth, "Detached session refresh failed account=\(ownership.email) error=\(error.localizedDescription)") + guard !Task.isCancelled, isCurrent(ownership, account: account, session: storedSession), + (error as? JarvisAuthError)?.category != .authorization else { return false } + return storedSession.canContinueOffline && !storedSession.isExpired + } + } + + private func contains(account: LoginAccount, session: LoginSession) -> Bool { + guard !modelContext.deletedModelsArray.contains(where: { $0 === account || $0 === session }), + let accounts = try? modelContext.fetch(FetchDescriptor()), + let sessions = try? modelContext.fetch(FetchDescriptor()) else { return false } + return accounts.contains { $0 === account } && sessions.contains { $0 === session } + } + + private func isCurrent(_ ownership: RefreshOwnership, account: LoginAccount, session: LoginSession) -> Bool { + guard !ownership.accessToken.isEmpty, + !modelContext.deletedModelsArray.contains(where: { + $0.persistentModelID == ownership.accountIdentifier || $0.persistentModelID == ownership.sessionIdentifier + }) else { return false } + let email = ownership.email + let sessionID = ownership.sessionID + guard let currentAccount = try? modelContext.fetch(FetchDescriptor(predicate: #Predicate { $0.email == email })).first, + let currentSession = try? modelContext.fetch(FetchDescriptor(predicate: #Predicate { $0.id == sessionID })).first, + currentAccount === account, currentSession === session else { return false } + return currentAccount.persistentModelID == ownership.accountIdentifier + && currentSession.persistentModelID == ownership.sessionIdentifier + && currentAccount.stableAccountID == ownership.accountID + && currentAccount.userId == ownership.accountUserID + && currentAccount.providerIdpId == ownership.providerID + && currentSession.accountEmail == email + && currentSession.userId == ownership.userID + && currentSession.idpId == ownership.sessionProviderID + && currentSession.issuedAt == ownership.issuedAt + && currentSession.accessToken == ownership.accessToken + && currentSession.idToken == ownership.idToken + && currentSession.refreshToken == ownership.refreshToken + && currentSession.clientToken == ownership.clientToken + } + + private func store(_ refreshed: JarvisSession, in storedSession: LoginSession) throws { + let now = Date() + let expiry = Date(timeIntervalSince1970: TimeInterval(refreshed.expiresAt > 0 ? refreshed.expiresAt : Int64(now.addingTimeInterval(86_400).timeIntervalSince1970))) + let clientExpiry = refreshed.clientTokenExpiry > 0 ? Date(timeIntervalSince1970: TimeInterval(refreshed.clientTokenExpiry) / 1000.0) : expiry + storedSession.updateAuthentication( + accountEmail: storedSession.accountEmail, + authMethod: storedSession.authMethod, + accessToken: refreshed.accessToken, + clientToken: refreshed.clientToken, + idToken: refreshed.idToken, + refreshToken: refreshed.refreshToken, + userId: refreshed.userId.isEmpty ? storedSession.userId : refreshed.userId, + idpId: storedSession.idpId, + deviceId: storedSession.deviceId, + issuedAt: now, + expiresAt: expiry, + clientTokenExpiresAt: clientExpiry, + isActive: storedSession.isActive, + canContinueOffline: storedSession.canContinueOffline + ) + try modelContext.save() + } + + private struct RefreshOwnership { + let accountIdentifier: PersistentIdentifier + let sessionIdentifier: PersistentIdentifier + let accountID: String + let accountUserID: String + let providerID: String + let email: String + let sessionID: String + let userID: String + let sessionProviderID: String + let issuedAt: Date + let accessToken: String + let idToken: String + let refreshToken: String + let clientToken: String + + init(account: LoginAccount, session: LoginSession) { + accountIdentifier = account.persistentModelID + sessionIdentifier = session.persistentModelID + accountID = account.stableAccountID + accountUserID = account.userId + providerID = account.providerIdpId + email = account.email + sessionID = session.id + userID = session.userId + sessionProviderID = session.idpId + issuedAt = session.issuedAt + accessToken = session.accessToken + idToken = session.idToken + refreshToken = session.refreshToken + clientToken = session.clientToken + } + } +} diff --git a/ViewModel/DetachedSession/DetachedSessionViewModel.swift b/ViewModel/DetachedSession/DetachedSessionViewModel.swift new file mode 100644 index 000000000..34b616825 --- /dev/null +++ b/ViewModel/DetachedSession/DetachedSessionViewModel.swift @@ -0,0 +1,73 @@ +// State of one Detach New Session window: which account it streams for and which controllers it +// leaves out, until the reader moves on to the catalog. + +import Foundation +import Observation + +@MainActor +@Observable +final class DetachedSessionViewModel { + enum Phase: Equatable { + case setup + case catalog(email: String) + } + + private(set) var phase: Phase = .setup + var selectedEmail: String? + private(set) var disabledControllers: Set = [] + var takesPadsFromOtherStreams: Bool + + private let preset: OPNStreamControllerPreset + private let registry: OPNGameSessionRegistry + private var pendingAccountID: OPNAccountID? + private var pendingSelection: OPNStreamControllerSelection? + + init(preset: OPNStreamControllerPreset = .shared, hasOtherStreams: Bool = OPNGameSessionRegistry.shared.hasSessions, registry: OPNGameSessionRegistry = .shared) { + self.preset = preset + self.registry = registry + takesPadsFromOtherStreams = hasOtherStreams + } + + func setController(_ id: InputDeviceID, enabled: Bool) { + if enabled { + disabledControllers.remove(id) + } else { + disabledControllers.insert(id) + } + } + + func isControllerEnabled(_ id: InputDeviceID) -> Bool { + !disabledControllers.contains(id) + } + + /// Stores the controller choice under the account, where the stream finds it when it starts, and + /// moves on to the catalog the account will launch from. + func confirm(row: DetachedAccountRow, accountID: OPNAccountID) { + guard row.availability.isSelectable else { return } + discardPendingSelection() + let controllerSelection = DetachedSessionModel.selection(disabled: disabledControllers, takesPadsFromOtherStreams: takesPadsFromOtherStreams) + preset.store(controllerSelection, forAccount: accountID.rawValue) + pendingAccountID = accountID + pendingSelection = controllerSelection + selectedEmail = row.email + phase = .catalog(email: row.email) + } + + func backToSetup() { + discardPendingSelection() + selectedEmail = nil + phase = .setup + } + + /// A choice made for a stream that never started must not reach a later launch by the same + /// account. Once the account owns a game the stream has taken it, so there is nothing to drop. + func discardPendingSelection() { + let selectionAccountID = pendingAccountID + let controllerSelection = pendingSelection + pendingAccountID = nil + pendingSelection = nil + guard let selectionAccountID, let controllerSelection, !registry.isOwned(by: selectionAccountID), + preset.peek(forAccount: selectionAccountID.rawValue) == controllerSelection else { return } + preset.store(OPNStreamControllerSelection(), forAccount: selectionAccountID.rawValue) + } +} diff --git a/ViewModel/LoginAccountChooser.swift b/ViewModel/LoginAccountChooser.swift index 7c34e674e..28f3b672e 100644 --- a/ViewModel/LoginAccountChooser.swift +++ b/ViewModel/LoginAccountChooser.swift @@ -4,6 +4,37 @@ import Foundation extension LoginViewModel { + func invalidateLoginIntent() { + loginLaunchGeneration += 1 + authService.invalidatePendingAuthentication() + isLaunchingOAuth = false + isAuthenticating = false + isRequestingDeviceCode = false + deviceCodeUserCode = "" + deviceCodeVerificationURI = "" + } + + @discardableResult + func restoreAccountSelection(_ account: LoginAccount) -> Task { + invalidateLoginIntent() + let generation = loginLaunchGeneration + return Task { + guard generation == loginLaunchGeneration else { return } + let outcome = await restoreAccountSessionOutcome(account) + guard generation == loginLaunchGeneration else { return } + switch outcome { + case .restored: + announceRunningGameContinues(with: account) + case .rejected: + beginReauthentication(for: account, because: "\(account.displayName)'s saved session was rejected. Sign in again to switch to it.") + case .unavailable: + showAccountSwitchNotice(validationMessage) + case .cancelled: + return + } + } + } + /// A fresh launch asks which account to browse with, when the reader asked to be asked and there /// is more than one saved account to choose between. Nothing is signed out or re-selected to ask. func presentStartupAccountChooser() { @@ -51,7 +82,12 @@ extension LoginViewModel { let games = owners.count == 1 ? "Your current game continues using \(owners[0])." : "Your current games continue using \(owners.formatted(.list(type: .and)))." - accountSwitchNotice = "Browsing now uses \(account.displayName). \(games)" + showAccountSwitchNotice("Browsing now uses \(account.displayName). \(games)") + } + + /// Shows `text` in the switch notice and clears it after a while. + func showAccountSwitchNotice(_ text: String) { + accountSwitchNotice = text accountSwitchNoticeTask?.cancel() accountSwitchNoticeTask = Task { [weak self] in try? await Task.sleep(for: .seconds(12)) diff --git a/ViewModel/LoginOAuthFlow.swift b/ViewModel/LoginOAuthFlow.swift index 5290b86dc..0e6dea1f2 100644 --- a/ViewModel/LoginOAuthFlow.swift +++ b/ViewModel/LoginOAuthFlow.swift @@ -164,8 +164,24 @@ extension LoginViewModel { } } + /// Why a restore did not leave the account signed in. Only `rejected` says the saved tokens are + /// dead; the others must not send the user through a fresh sign-in. + enum AccountRestoreOutcome: Equatable { + case restored + /// No saved session, or the auth server refused the saved grants. + case rejected + /// The refresh failed for a reason that says nothing about the tokens, such as no network. + case unavailable + /// A sign-out or cancellation ran during the restore. + case cancelled + } + @discardableResult func restoreAccountSession(_ account: LoginAccount) async -> Bool { + await restoreAccountSessionOutcome(account) == .restored + } + + func restoreAccountSessionOutcome(_ account: LoginAccount) async -> AccountRestoreOutcome { validationMessage = "" successMessage = "" email = account.email @@ -175,64 +191,78 @@ extension LoginViewModel { guard let storedSession = sessions.first(where: { $0.accountEmail == account.email && !$0.accessToken.isEmpty }) else { OPNLog.warning(.auth, "Session restore failed because no saved session exists for account=\(account.email)") validationMessage = "No saved session exists for this account. Sign in again." - return false + return .rejected } - isAuthenticating = true - defer { isAuthenticating = false } // Captured before the network round trips. A sign-out or cancellation during them advances // this, and the restore then discards its result instead of reactivating the account. let generation = loginLaunchGeneration - - var jarvisSession = JarvisSession( - accessToken: storedSession.accessToken, - idToken: storedSession.idToken, - refreshToken: storedSession.refreshToken, - userId: storedSession.userId, - displayName: account.displayName, - email: account.email, - membershipTier: account.membershipTier, - idpId: storedSession.idpId.isEmpty ? account.providerIdpId : storedSession.idpId, - expiresAt: Int64(storedSession.expiresAt.timeIntervalSince1970), - isAuthenticated: true, - clientToken: storedSession.clientToken, - clientTokenExpiry: Int64(storedSession.clientTokenExpiresAt.timeIntervalSince1970 * 1000.0), - clientTokenExpiryLength: 0, - accessTokenExpiry: Int64(storedSession.expiresAt.timeIntervalSince1970 * 1000.0) - ) - if jarvisSession.idTokenExpiry == 0 { - jarvisSession.idTokenExpiry = JarvisSessionParser.idTokenExpiry(storedSession.idToken) + isAuthenticating = true + defer { + if generation == loginLaunchGeneration { isAuthenticating = false } } + let jarvisSession = restorableJarvisSession(for: account, stored: storedSession) + do { OPNLog.info(.auth, "Refreshing saved session account=\(account.email)") await jarvisAuthService.setSession(jarvisSession) + guard generation == loginLaunchGeneration else { return .cancelled } let refreshed = try await jarvisAuthService.refreshSession(force: !jarvisSession.isIdTokenValid) guard generation == loginLaunchGeneration else { OPNLog.info(.auth, "Discarded a session refresh that completed after sign-out account=\(account.email)") - return false + return .cancelled } persistSignedInSession(session: refreshed, userInfo: nil, authMethod: Jarvis.Operation.getSessionToken.rawValue) successMessage = "Session refreshed for \(account.displayName)." OPNLog.info(.auth, "Session refreshed account=\(account.email)") - return true + return .restored + } catch is CancellationError { + return .cancelled } catch { - if storedSession.canContinueOffline && !storedSession.isExpired { - guard generation == loginLaunchGeneration else { return false } + guard generation == loginLaunchGeneration else { return .cancelled } + if (error as? JarvisAuthError)?.category != .authorization && storedSession.canContinueOffline && !storedSession.isExpired { markActive(accountEmail: account.email) trySave() refreshSignedOutAccounts() successMessage = "Using saved offline session for \(account.displayName)." OPNLog.warning(.auth, "Using offline saved session account=\(account.email) refreshError=\(error.localizedDescription)") - return true + return .restored } else { - validationMessage = "Saved session expired. Sign in again." OPNLog.warning(.auth, "Session restore failed account=\(account.email) error=\(error.localizedDescription)") - return false + if (error as? JarvisAuthError)?.category == .authorization { + validationMessage = "Saved session expired. Sign in again." + return .rejected + } + validationMessage = "Couldn't refresh the saved session for \(account.displayName). Check your connection and try again." + return .unavailable } } } + private func restorableJarvisSession(for account: LoginAccount, stored storedSession: LoginSession) -> JarvisSession { + var jarvisSession = JarvisSession( + accessToken: storedSession.accessToken, + idToken: storedSession.idToken, + refreshToken: storedSession.refreshToken, + userId: storedSession.userId, + displayName: account.displayName, + email: account.email, + membershipTier: account.membershipTier, + idpId: storedSession.idpId.isEmpty ? account.providerIdpId : storedSession.idpId, + expiresAt: Int64(storedSession.expiresAt.timeIntervalSince1970), + isAuthenticated: true, + clientToken: storedSession.clientToken, + clientTokenExpiry: Int64(storedSession.clientTokenExpiresAt.timeIntervalSince1970 * 1000.0), + clientTokenExpiryLength: 0, + accessTokenExpiry: Int64(storedSession.expiresAt.timeIntervalSince1970 * 1000.0) + ) + if jarvisSession.idTokenExpiry == 0 { + jarvisSession.idTokenExpiry = JarvisSessionParser.idTokenExpiry(storedSession.idToken) + } + return jarvisSession + } + func signOutAccount(_ account: LoginAccount) async { // Re-read here rather than when the confirmation was drawn: a session that started in // between still blocks. Signing out takes the keychain copy the running game uses. @@ -244,8 +274,7 @@ extension LoginViewModel { OPNLog.info(.auth, "Signing out account=\(account.email)") // Invalidate anything already in flight before the purge below, so a refresh or restore that // completes afterwards cannot recreate the account or write its tokens back. - loginLaunchGeneration += 1 - authService.invalidatePendingAuthentication() + invalidateLoginIntent() let wasActive = account.isActive let userId = account.userId let email = account.email diff --git a/ViewModel/LoginViewModel.swift b/ViewModel/LoginViewModel.swift index 4da88dbba..a5cc11078 100644 --- a/ViewModel/LoginViewModel.swift +++ b/ViewModel/LoginViewModel.swift @@ -187,16 +187,7 @@ final class LoginViewModel: ObservableObject { func cancelPendingLogin() { guard isLaunchingOAuth || isAuthenticating else { return } - loginLaunchGeneration += 1 - // Invalidate the in-flight request at its source too: the generation check in the completion - // runs after a request that may still persist credentials, so cancellation has to reach the - // service before that persistence. - authService.invalidatePendingAuthentication() - isLaunchingOAuth = false - isAuthenticating = false - isRequestingDeviceCode = false - deviceCodeUserCode = "" - deviceCodeVerificationURI = "" + invalidateLoginIntent() validationMessage = "Sign-in cancelled. Choose GET IN to try again." OPNLog.info(.auth, "User cancelled pending sign-in") } @@ -241,10 +232,7 @@ final class LoginViewModel: ObservableObject { beginReauthentication(for: account) return } - Task { - guard await restoreAccountSession(account) else { return } - announceRunningGameContinues(with: account) - } + restoreAccountSelection(account) } func hasUsableSession(for account: LoginAccount) -> Bool { @@ -270,11 +258,12 @@ final class LoginViewModel: ObservableObject { /// Another account is still signed in, so cancelling the re-sign-in has a session to return to. var canCancelReauthentication: Bool { activeSession != nil } - func beginReauthentication(for account: LoginAccount) { + func beginReauthentication(for account: LoginAccount, because reason: String? = nil) { + invalidateLoginIntent() selectRememberedAccount(account) refreshLoginProviders() successMessage = "" - validationMessage = "\(account.displayName) is signed out. Sign in again to switch to it." + validationMessage = reason ?? "\(account.displayName) is signed out. Sign in again to switch to it." signInRequest = .reauthenticate(email: account.email) OPNLog.info(.auth, "Account switch needs re-authentication account=\(account.email)") } @@ -291,14 +280,19 @@ final class LoginViewModel: ObservableObject { beginSignInAgain(for: account) return } + invalidateLoginIntent() + let generation = loginLaunchGeneration Task { + guard generation == loginLaunchGeneration else { return } guard await restoreAccountSession(account) else { return } + guard generation == loginLaunchGeneration else { return } announceRunningGameContinues(with: account) } } /// Picks the account's provider and starts the browser leg, which is what its row promises. func beginSignInAgain(for account: LoginAccount) { + invalidateLoginIntent() selectRememberedAccount(account) refreshLoginProviders() successMessage = "" @@ -313,6 +307,7 @@ final class LoginViewModel: ObservableObject { /// Signs in an additional account. The account that is signed in keeps its tokens, so it stays /// in the list and switchable once the new one is added. func beginAddAccount() { + invalidateLoginIntent() dismissAccountChooser() email = "" selectedProvider = providers.first ?? LoginProvider.nvidia diff --git a/ViewModel/Stream/NativeNVSTHostInput.swift b/ViewModel/Stream/NativeNVSTHostInput.swift index 7fc1d2bb5..e1771a183 100644 --- a/ViewModel/Stream/NativeNVSTHostInput.swift +++ b/ViewModel/Stream/NativeNVSTHostInput.swift @@ -57,6 +57,7 @@ extension NativeNVSTHostViewModel { func attachStreamWindow(_ window: NSWindow?) { guard let window = window as? OPNStreamWindow, !didEnd else { return } window.sessionSurface = self + nativeView?.allowsBackgroundGamepadInput = window.allowsBackgroundGamepadInput } /// The HUD's full-screen tile reads `streamWindowIsFullScreen` rather than the style mask, so it @@ -145,10 +146,13 @@ extension NativeNVSTHostViewModel { } return } - guard path != nil, isConnected, !unifiedHUDVisible, !streamControlsVisible, !isEnding, !didEnd else { return } - if view.remoteInputEnabled, !NativeNVSTInputDispatcher.isNeutralizing(event), - !Self.acceptsWhileNotFrontmost(event, isPictureInPictureMode: view.isPictureInPictureMode) { - guard NSApplication.shared.isActive, view.window?.isKeyWindow == true else { return } + guard path != nil, isConnected, !isEnding, !didEnd else { return } + let isNeutralizing = NativeNVSTInputDispatcher.isNeutralizing(event) + guard isNeutralizing || (view.remoteInputEnabled && !view.localOverlayCapturesInput && !unifiedHUDVisible && !streamControlsVisible) else { return } + if !isNeutralizing, + !Self.acceptsWhileNotFrontmost(event, isPictureInPictureMode: view.isPictureInPictureMode, + allowsBackgroundGamepad: view.allowsBackgroundGamepadInput) { + guard view.isFrontmostInputTarget else { return } } lastAcceptedStreamInputAt = Date() // The left button's edges bracket a text selection: everything between press and release is @@ -157,35 +161,25 @@ extension NativeNVSTHostViewModel { if case .mouse(.button(_, let button, let isPressed, _)) = event, button == .left { clipboard.pointerSelection.noteLeftButton(isPressed: isPressed, at: Date()) } - if case .mouse = event { - if view.mouseInputMode == .relative, !view.isPointerLocked { - // The seat asked for mouselook but the capture is not held — an association macOS - // refused, or a focus change that released it. Dropping the event here is a mouse - // that does nothing at all, so a real user event retries the capture: a transient - // failure heals itself instead of stranding the session until the seat changes its - // mind. A neutralizing release, background input, or the HUD must never retake it. - let canRetakeCapture = view.remoteInputEnabled - && view.isFrontmostInputTarget - && !NativeNVSTInputDispatcher.isNeutralizing(event) - guard canRetakeCapture else { return } - view.setPointerLocked(true) - guard view.isPointerLocked else { return } - } - inputDispatcher?.enqueue(event) - return + if case .mouse = event, view.mouseInputMode == .relative, !view.isPointerLocked, !isNeutralizing { + // Real mouse input retries transient capture failures; neutral releases must never recapture. + guard view.canAcquirePointerCapture else { return } + view.setPointerLocked(true) + guard view.isPointerLocked else { return } } inputDispatcher?.enqueue(event) } /// Whether an event reaches the game without this window being frontmost. /// - /// Only the gamepad, and only in PiP. PiP is a small floating surface that never activates the + /// Only the gamepad, and only in PiP or while couch co-op has several streams running. PiP is a small floating surface that never activates the /// app, so a game pad - a device the system delivers to the process rather than to a window - /// has to keep working while the user is in another app; that is the mode's whole point. The /// keyboard and mouse stay gated: typing or clicking in the app the user moved to must not reach /// the game. - static func acceptsWhileNotFrontmost(_ event: UserInputEvent, isPictureInPictureMode: Bool) -> Bool { - guard isPictureInPictureMode else { return false } + static func acceptsWhileNotFrontmost(_ event: UserInputEvent, isPictureInPictureMode: Bool, + allowsBackgroundGamepad: Bool = false) -> Bool { + guard isPictureInPictureMode || allowsBackgroundGamepad else { return false } if case .gamepad = event { return true } return false } diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift b/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift new file mode 100644 index 000000000..cbfb21ba2 --- /dev/null +++ b/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift @@ -0,0 +1,60 @@ +// The Borderless Full Screen tile and what it does to a stream window: a single window toggles its +// own borderless mode, and a window the couch co-op layout places flips every tile between sitting +// below the menu bar and filling the screens. +// +// AppKit is imported for the same reason as the main file: the window is what these controls act on. +// +// swiftlint:disable:next no_appkit_in_view_model +import AppKit +import Foundation + +@MainActor +extension NativeNVSTHostViewModel { + private var streamLayoutState: OPNStreamWindowLayoutState? { + (nativeView?.window as? OPNStreamWindow)?.layoutState + } + + var isBorderlessWindow: Bool { streamLayoutState?.isBorderless ?? false } + + /// A window the couch co-op layout places. Its tile switches every tiled window between sitting + /// below the menu bar and filling the screens, rather than taking this one window out. + private var isTiledWindow: Bool { streamLayoutState?.isAutoTiled ?? false } + + var isBorderlessTileActive: Bool { + isTiledWindow ? OPNStreamTileCoverage.stored == .fullScreen : isBorderlessWindow + } + + var borderlessTileTitle: String { + if isTiledWindow { return OPNStreamTileCoverage.stored == .fullScreen ? "Tiles Below Menu Bar" : "Tiles Fill Screen" } + return isBorderlessWindow ? "Leave Borderless" : "Borderless Full Screen" + } + + var borderlessTileSubtitle: String { + if isTiledWindow { return OPNStreamTileCoverage.stored == .fullScreen ? "Filling every screen" : "Below the menu bar" } + return isBorderlessWindow ? "Borderless" : "Fill the screen, keep the Spaces" + } + + /// Borderless is a couch co-op mode, so the tile exists only behind the flag, and like full + /// screen it has nothing to act on in a small floating window or in a native full-screen Space. + var isBorderlessTileAvailable: Bool { OPNLabs.isCouchCoopEnabled } + + var isBorderlessTileDisabled: Bool { + nativeView?.window == nil || isPictureInPicture || streamWindowIsFullScreen || isFullScreenTransitioning + } + + /// The reader's own choice, so the layout stops placing this window: it stays where the toggle + /// left it, borderless or not. + func toggleBorderlessFullScreen() { + guard isBorderlessTileAvailable, !isBorderlessTileDisabled, !isEnding, !didEnd, + let window = nativeView?.window as? OPNStreamWindow else { return } + if window.layoutState.isAutoTiled { + let coverage = OPNStreamTileCoverage.stored.flipped + OPNStreamWindowPresenter.shared.setTileCoverage(coverage) + showNativeTransientStreamMessage(coverage == .fullScreen ? "Tiles fill the screen" : "Tiles below the menu bar") + return + } + let isLeavingBorderless = window.layoutState.isBorderless && !window.isBorderlessExitPending + OPNStreamBorderless.toggleByReader(window) + showNativeTransientStreamMessage(isLeavingBorderless ? "Leaving borderless full screen" : "Borderless full screen") + } +} diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift index a1dc87e62..3e62216af 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift @@ -361,9 +361,6 @@ extension NativeNVSTHostViewModel { // resuming into a session that cannot receive them - until the next path update. nativeView?.remoteInputEnabled = networkPathAvailable nativeView?.restoreInputFocus() - // Same gate as the line above, and for the same reason: `enablePointerLock` does not - // consult `remoteInputEnabled`, so an unguarded restore would take the pointer back - // during an outage, behind the recovery overlay. if restoreManualCapture, networkPathAvailable, nativeView?.isFrontmostInputTarget == true { nativeView?.setManualPointerCapture(true) } @@ -392,7 +389,7 @@ extension NativeNVSTHostViewModel { /// The full-screen tile and its focus entry share this rather than each writing the condition /// out: a PiP window is small and floating, and there is no full screen to enter from it. - var isFullScreenTileDisabled: Bool { nativeView?.window == nil || isPictureInPicture } + var isFullScreenTileDisabled: Bool { nativeView?.window == nil || isPictureInPicture || isBorderlessWindow } /// The window owns the transition: nothing here touches the style mask, collection behaviour, /// aspect ratio or frame - `WindowFitting` grants `.fullScreenPrimary` before the window @@ -401,7 +398,7 @@ extension NativeNVSTHostViewModel { func toggleNativeFullScreen() { // There is no full screen to enter from a small floating window; the tile is disabled in // PiP and this is the same answer for the menu bar and the shortcut. - guard let window = nativeView?.window, !isFullScreenTransitioning, !isPictureInPicture else { return } + guard let window = nativeView?.window, !isFullScreenTransitioning, !isPictureInPicture, !isBorderlessWindow else { return } guard !StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { return } // Read before the toggle: `.fullScreen` is only inserted once the transition finishes. let isEnteringFullScreen = !window.styleMask.contains(.fullScreen) @@ -419,86 +416,6 @@ extension NativeNVSTHostViewModel { ) } - /// PiP is a mode of the stream window, so this is a window change and nothing else: one surface, - /// one session, never a second window and never a reparented view. - /// - /// Pressed while the window is full screen it does what the user meant - leave full screen, then - /// enter PiP - because the exit is animated and the mode change would otherwise land mid - /// animation, which is the same reason `enterNativeFullScreenWhenSessionReady` waits and retries. - func togglePictureInPicture() { - guard let window = nativeView?.window as? OPNStreamWindow, !isEnding, !didEnd else { return } - guard !StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { return } - switch OPNStreamPictureInPicture.request( - isPictureInPicture: isPictureInPicture, - isFullScreen: window.styleMask.contains(.fullScreen), - isFullScreenTransitioning: isFullScreenTransitioning - ) { - case .leave: leavePictureInPicture(window) - case .leaveFullScreenThenEnter: leaveFullScreenThenEnterPictureInPicture(window, attempt: 0) - case .enter: enterPictureInPicture(window) - } - } - - /// The window is small and floating from here on, so every surface anchored to the window's top - /// corners has to go. The dock alone is 344pt wide in a 640pt window. - private func enterPictureInPicture(_ window: OPNStreamWindow) { - unifiedHUDVisible = false - isShortcutsHelpVisible = false - isHUDCustomizeVisible = false - nativeStatsVisible = false - closeHUDDropdown() - hudFocusID = nil - isHUDGamepadFocusVisible = false - if onScreenKeyboardVisible { setOnScreenKeyboardVisible(false) } - // Input stays live. The picture is in the game, so the mouse and keyboard have to keep - // reaching it while the window is small - the same gate the HUD uses on its way out, which - // is `networkPathAvailable` rather than an unconditional `true`: the network monitor blocks - // input while the path is down and `isConnected` stays true through a drop. - // - // The mode itself, though, must not hold the pointer: PiP is a cursor-sized picture, there - // is nothing to aim with, and a captured cursor is the one thing the user cannot use to - // reach the strip or whatever else they moved to. Setting the mode releases it, and keeps - // the controller alive while the window is not frontmost. - nativeView?.isPictureInPictureMode = true - nativeView?.remoteInputEnabled = networkPathAvailable - nativeView?.restoreInputFocus() - let aspectRatio = CGFloat(OPNStreamPreferences.launchProfile(forGame: configuration.applicationID, capabilities: OPNStreamPreferences.loadDeviceCapabilities()).aspectRatio) - OPNStreamPictureInPicture.enter(window, aspectRatio: aspectRatio) - isPictureInPicture = true - showNativeTransientStreamMessage("Picture in Picture") - OPNStreamTelemetry.capture("nvst.ui.pip.enter", level: .info, message: "Native NVST stream entered Picture in Picture.", attributes: ["applicationID": configuration.applicationID]) - } - - private func leavePictureInPicture(_ window: OPNStreamWindow) { - OPNStreamPictureInPicture.exit(window) - isPictureInPicture = false - nativeView?.isPictureInPictureMode = false - nativeView?.remoteInputEnabled = networkPathAvailable - nativeView?.restoreInputFocus() - OPNStreamTelemetry.capture("nvst.ui.pip.exit", level: .info, message: "Native NVST stream left Picture in Picture.", attributes: ["applicationID": configuration.applicationID]) - } - - /// Full screen first, PiP second, one retry beat at a time. Mirrors the session-ready helper - /// below: the exit is animated and AppKit refuses a second style-mask change while it runs. - private func leaveFullScreenThenEnterPictureInPicture(_ window: OPNStreamWindow, attempt: Int) { - guard attempt < Self.sessionReadyFullScreenAttemptLimit else { return } - Task { @MainActor [weak self] in - try? await Task.sleep(for: NativeNVSTHostViewModel.sessionReadyFullScreenRetryDelay) - guard let self, !self.isEnding, !self.didEnd else { return } - guard let window = self.nativeView?.window as? OPNStreamWindow else { return } - if self.isFullScreenTransitioning || StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) { - self.leaveFullScreenThenEnterPictureInPicture(window, attempt: attempt + 1) - return - } - if window.styleMask.contains(.fullScreen) { - window.toggleFullScreen(nil) - self.leaveFullScreenThenEnterPictureInPicture(window, attempt: attempt + 1) - return - } - self.enterPictureInPicture(window) - } - } - func startNativeStatsPolling(path: NativeNVSTStreamingPath) { nativeStatsTask?.cancel() nativeStatsTask = Task { diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+FullScreenEntry.swift b/ViewModel/Stream/NativeNVSTHostViewModel+FullScreenEntry.swift index 29d19ee2c..c8fea83c5 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+FullScreenEntry.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+FullScreenEntry.swift @@ -35,6 +35,7 @@ extension NativeNVSTHostViewModel { for attempt in 1...Self.sessionReadyFullScreenAttemptLimit { try? await Task.sleep(for: Self.sessionReadyFullScreenRetryDelay) guard !Task.isCancelled else { return } + if let window = nativeView?.window as? OPNStreamWindow, window.layoutState.suppressesNativeFullScreen { return } attemptCount = attempt switch nativeFullScreenEntryAttempt() { case .stop, .alreadyFullScreen: diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+HUDSections.swift b/ViewModel/Stream/NativeNVSTHostViewModel+HUDSections.swift index 1474d68e7..322623395 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+HUDSections.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+HUDSections.swift @@ -141,6 +141,14 @@ extension NativeNVSTHostViewModel { static let clipboardCaptureModeFocusID = "clipboard-capture-mode" private var displayFocusEntries: [StreamHUDFocusEntry] { + let entries = baseDisplayFocusEntries + guard isBorderlessTileAvailable else { return entries } + return entries + [ + StreamHUDFocusEntry(id: "borderless-full-screen", isDisabled: isBorderlessTileDisabled, group: "display", columns: 4, action: toggleBorderlessFullScreen), + ] + } + + private var baseDisplayFocusEntries: [StreamHUDFocusEntry] { [ StreamHUDFocusEntry(id: "floating-stats", isDisabled: !sidebarCapabilities.supports(.floatingStats), group: "display", columns: 4, action: toggleNativeStatsHUD), StreamHUDFocusEntry(id: "full-screen", isDisabled: isFullScreenTileDisabled, group: "display", columns: 4, action: toggleNativeFullScreen), diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift b/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift new file mode 100644 index 000000000..2d9e95b41 --- /dev/null +++ b/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift @@ -0,0 +1,98 @@ +// swiftlint:disable:next no_appkit_in_view_model +import AppKit +import Foundation + +@MainActor +extension NativeNVSTHostViewModel { + func togglePictureInPicture() { + guard let window = nativeView?.window as? OPNStreamWindow, !isEnding, !didEnd else { return } + if window.isPictureInPicturePending { + OPNStreamPictureInPicture.exit(window) + return + } + guard !StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { return } + switch OPNStreamPictureInPicture.request( + isPictureInPicture: isPictureInPicture, + isFullScreen: window.styleMask.contains(.fullScreen), + isFullScreenTransitioning: isFullScreenTransitioning + ) { + case .leave: leavePictureInPicture(window) + case .leaveFullScreenThenEnter: + let requestID = UUID() + window.pictureInPictureRequestID = requestID + window.layoutMembershipDidChange?() + leaveFullScreenThenEnterPictureInPicture(window, requestID: requestID, attempt: 0) + case .enter: enterPictureInPicture(window) + } + } + + private func enterPictureInPicture(_ window: OPNStreamWindow) { + let aspectRatio = CGFloat(OPNStreamPreferences.launchProfile(forGame: configuration.applicationID, capabilities: OPNStreamPreferences.loadDeviceCapabilities()).aspectRatio) + OPNStreamPictureInPicture.enter(window, aspectRatio: aspectRatio, prepareForEntry: { [weak self, weak window] in + guard let self, let window, !self.isEnding, !self.didEnd, self.nativeView?.window === window else { return false } + self.preparePictureInPictureEntry() + return true + }, completion: { [weak self] in self?.completePictureInPictureEntry() }) + } + + private func preparePictureInPictureEntry() { + unifiedHUDVisible = false + isShortcutsHelpVisible = false + isHUDCustomizeVisible = false + nativeStatsVisible = false + closeHUDDropdown() + hudFocusID = nil + isHUDGamepadFocusVisible = false + if onScreenKeyboardVisible { setOnScreenKeyboardVisible(false) } + nativeView?.isPictureInPictureMode = true + nativeView?.remoteInputEnabled = networkPathAvailable + nativeView?.restoreInputFocus() + } + + private func completePictureInPictureEntry() { + isPictureInPicture = true + showNativeTransientStreamMessage("Picture in Picture") + OPNStreamTelemetry.capture("nvst.ui.pip.enter", level: .info, message: "Native NVST stream entered Picture in Picture.", attributes: ["applicationID": configuration.applicationID]) + } + + private func leavePictureInPicture(_ window: OPNStreamWindow) { + OPNStreamPictureInPicture.exit(window) + isPictureInPicture = false + nativeView?.isPictureInPictureMode = false + nativeView?.remoteInputEnabled = networkPathAvailable + nativeView?.restoreInputFocus() + OPNStreamTelemetry.capture("nvst.ui.pip.exit", level: .info, message: "Native NVST stream left Picture in Picture.", attributes: ["applicationID": configuration.applicationID]) + } + + private func leaveFullScreenThenEnterPictureInPicture(_ window: OPNStreamWindow, requestID: UUID, attempt: Int) { + guard attempt < Self.sessionReadyFullScreenAttemptLimit else { + guard window.pictureInPictureRequestID == requestID else { return } + Self.cancelPictureInPictureRequest(in: window) + return + } + Task { @MainActor [weak self, weak window] in + try? await Task.sleep(for: NativeNVSTHostViewModel.sessionReadyFullScreenRetryDelay) + guard let window, window.pictureInPictureRequestID == requestID else { return } + guard let self, !self.isEnding, !self.didEnd, !window.isPresentationEnding, + self.nativeView?.window === window else { + NativeNVSTHostViewModel.cancelPictureInPictureRequest(in: window) + return + } + if self.isFullScreenTransitioning || StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) { + self.leaveFullScreenThenEnterPictureInPicture(window, requestID: requestID, attempt: attempt + 1) + return + } + if window.styleMask.contains(.fullScreen) { + window.toggleFullScreen(nil) + self.leaveFullScreenThenEnterPictureInPicture(window, requestID: requestID, attempt: attempt + 1) + return + } + self.enterPictureInPicture(window) + } + } + + private static func cancelPictureInPictureRequest(in window: OPNStreamWindow) { + window.pictureInPictureRequestID = nil + window.layoutMembershipDidChange?() + } +} diff --git a/ViewModel/Stream/StreamControllerSelectionContext.swift b/ViewModel/Stream/StreamControllerSelectionContext.swift index ea5cc8490..906390497 100644 --- a/ViewModel/Stream/StreamControllerSelectionContext.swift +++ b/ViewModel/Stream/StreamControllerSelectionContext.swift @@ -22,3 +22,14 @@ extension NativeNVSTHostViewModel { return StreamControllerSelectionContext(monitor: nativeView.gamepadMonitor) } } + +@MainActor +extension NativeNVSTHostViewModel { + func setBackgroundGamepadInput(_ isAllowed: Bool) { + nativeView?.allowsBackgroundGamepadInput = isAllowed + } + + func disableControllersForStream(_ deviceIDs: Set) { + nativeView?.gamepadMonitor.disableForStream(deviceIDs) + } +}