Files
museion/MuseionTests/ProviderCredentialTests.swift
AlexKucera f49e4b6ca0
CI / ci (push) Canceled after 0s
refactor(settings): provider facts as data
#181: ModelProvider is the provider-descriptor table — label, effort
ladder, flagship, endpoint, env var joined by wire dialect (chat/models
paths + the SSE decoder selector), auth mode (Bearer vs Anthropic's
header pair), keyless (nous rides the Portal login), and the ONE
trailing-slash endpoint trim. The probe's URL builders, auth headers,
and stream-decoder picks select by data; the per-dialect bodies and
decoders stay separate functions, not switch clothing.

ProviderCredential.resolve is the ONE credential rail (provider key,
else the shared Portal session key for keyless providers — official
endpoint ONLY, never a user-set URL). Settings' Test Connection and the
new PassReadiness module both call it; the twice-implemented #175
fallback can no longer drift. One deliberate tightening, reviewer-
blessed: a nous pass profile on a custom base_url with no pasted key now
reads missingKey where the old session-model fallback said ready — the
old copy had no endpoint gate at all.

Pass readiness leaves the session model (PassReadiness.swift maps each
pass surface to ready/missingKey/unknown over the descriptor;
MentorSessionModel stores the verdicts and reads no keys or auth
files). runPass and blindRead run their guards through one shared rail
(guardOpenPieceRun) — identical refusal strings, blockers named
honestly. No app-side model-settings store (ADR-0014 red line).

Rider: the #179 mid-pass repoint pin polls the rig's hasHeldPrompt
before releasing the held turn — runningPass flips before the one-shot
engine parks its prompt, so firing into that window false-failed under
parallel-suite load.

Full suite 668/668 (xcresult executed count). Finish-audit: three
reviewers (ponytail/security/general), zero findings above LOW; two
ponytail trims and one pin tightening applied.

Closes #181
2026-11-10 10:58:29 +01:00

118 lines
4.1 KiB
Swift

//
// 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"))
}
}