import Testing import Foundation @testable import CoreATProtocol @Suite("CoreATProtocol Environment Tests", .serialized) struct CoreATProtocolTests { @Test("Environment singleton is accessible") func testEnvironmentSingleton() async { // Clear state first await clearAuthenticationContext() // Just verify we can access the singleton let host = await APEnvironment.current.host #expect(host == nil) // Default state should have nil host } @Test("Setup configures environment correctly") func testSetup() async { // Clear previous state await clearAuthenticationContext() await setup( hostURL: "https://bsky.social", accessJWT: "test-access", refreshJWT: "test-refresh" ) let host = await APEnvironment.current.host let access = await APEnvironment.current.accessToken let refresh = await APEnvironment.current.refreshToken #expect(host == "https://bsky.social") #expect(access == "test-access") #expect(refresh == "test-refresh") // Clean up await clearAuthenticationContext() } @Test("Clear authentication context removes all tokens") func testClearContext() async { await setup( hostURL: "https://test.social", accessJWT: "access", refreshJWT: "refresh" ) await clearAuthenticationContext() let access = await APEnvironment.current.accessToken let refresh = await APEnvironment.current.refreshToken let login = await APEnvironment.current.login #expect(access == nil) #expect(refresh == nil) #expect(login == nil) } @Test("Update tokens modifies existing tokens") func testUpdateTokens() async { await setup(hostURL: nil, accessJWT: "old-access", refreshJWT: "old-refresh") await updateTokens(access: "new-access", refresh: "new-refresh") let access = await APEnvironment.current.accessToken let refresh = await APEnvironment.current.refreshToken #expect(access == "new-access") #expect(refresh == "new-refresh") await clearAuthenticationContext() } @Test("DPoP nonce update works correctly") func testDPoPNonceUpdate() async { await updateResourceDPoPNonce("test-nonce-123") let nonce = await APEnvironment.current.resourceServerNonce #expect(nonce == "test-nonce-123") await updateResourceDPoPNonce(nil) } @Test("hasValidSession returns false when no session") func testNoValidSession() async { await clearAuthenticationContext() let valid = await hasValidSession #expect(valid == false) } }