// // ProviderCredentialTests.swift // MuseionTests // // ## Scenario: one credential resolution behind probe and readiness // From #181 // // Given a provider, its `.env` key (possibly empty), and the endpoint the // key would ride // When the ONE credential resolution runs — Settings' Test Connection and // pass readiness both call it // Then a pasted key always wins, a keyless provider on its official // endpoint borrows the shared Portal session key, and every other empty // case refuses honestly — the implicit session credential never rides a // user-set URL. import Foundation @testable import Museion import Testing @Suite("Provider credential resolution") struct ProviderCredentialTests { /// Writes a portal auth store carrying `agent_key` and answers its path. private func makeAuthFile(key: String) throws -> String { let dir = FileManager.default.temporaryDirectory .appendingPathComponent("cred-\(UUID().uuidString)") try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) let path = dir.appendingPathComponent("auth.json").path try #"{"providers":{"nous":{"agent_key":"\#(key)"}}}"#.write(toFile: path, atomically: true, encoding: .utf8) return path } /// A pasted key always wins — no provider's resolution touches the /// Portal auth store when its own key is set. @Test func `pasted key wins for every provider`() async { let missingAuth = FileManager.default.temporaryDirectory .appendingPathComponent("cred-missing-\(UUID().uuidString)").path for provider in ModelProvider.allCases { let resolved = await ProviderCredential.resolve( provider: provider, envKey: "sk-pasted", endpoint: provider.defaultEndpoint, portalAuthPath: missingAuth, ) #expect(resolved == .key("sk-pasted")) } } /// A keyed provider with no key refuses the paste path — the probe's /// old empty-key precondition, now the shared rail's. @Test func `keyed provider without a key refuses the paste path`() async { let resolved = await ProviderCredential.resolve( provider: .anthropic, envKey: "", endpoint: ModelProvider.anthropic.defaultEndpoint, portalAuthPath: "/nonexistent/auth.json", ) guard case let .refused(message) = resolved else { Issue.record("expected a refusal, got \(resolved)") return } #expect(message == "no API key set — paste the key first") } /// The keyless fallback: nous on the official endpoint (trailing slash /// tolerated) borrows the session's agent_key. @Test func `keyless provider borrows the portal session key on the official endpoint`() async throws { let authPath = try makeAuthFile(key: "ak-shared-123") for endpoint in [ ModelProvider.nous.defaultEndpoint, ModelProvider.nous.defaultEndpoint + "/", ] { let resolved = await ProviderCredential.resolve( provider: .nous, envKey: "", endpoint: endpoint, portalAuthPath: authPath, ) #expect(resolved == .key("ak-shared-123")) } } /// Finish-audit #175, on the shared rail: the implicit session /// credential never rides a user-set URL — a custom endpoint with an /// empty key refuses even though the login exists. @Test func `the session key never rides a custom endpoint`() async throws { let authPath = try makeAuthFile(key: "ak-shared-123") let resolved = await ProviderCredential.resolve( provider: .nous, envKey: "", endpoint: "http://proxy.example/v1", portalAuthPath: authPath, ) guard case let .refused(message) = resolved else { Issue.record("expected the custom-endpoint refusal, got \(resolved)") return } #expect(message.contains("paste an API key")) } /// Keyless with neither key nor login names the portal login path, /// not a bare "no key". @Test func `keyless without key or login names the portal path`() async { let resolved = await ProviderCredential.resolve( provider: .nous, envKey: "", endpoint: ModelProvider.nous.defaultEndpoint, portalAuthPath: "/nonexistent/auth.json", ) guard case let .refused(message) = resolved else { Issue.record("expected a refusal, got \(resolved)") return } #expect(message.contains("hermes auth add nous")) } }