codeburn/mac/Tests/CodeBurnMenubarTests/ProviderSettingsEditorStateTests.swift
Aditya Vikram Singh 84fea9d550
feat(menubar): add native Capacity Dock (#1167)
* feat(menubar): add native capacity dock

* fix(menubar): harden dock interaction boundaries

* feat(menubar): polish native Capacity Dock for launch

- Flush top-edge docking via NSHostingView safe-area opt-out; shielding
  window level so the rail necks into the physical top like the system notch
- Liquid Glass legibility scrim so labels stay readable on light wallpapers
- Weekly-else-monthly headline window; unified severity colour bands
  (green <50 / yellow / orange / red) across rings and detail bars
- Squircle gauge + graphite theme defaults; auto-show connected
  subscriptions (up to 5) until the user edits the set, then respect it
- Capacity Dock Settings menu item; re-centre the settings window; info
  glyph for the About row
- Trim dead provider-credential override fields down to apiKey only
- Reconcile and extend tests for all of the above

---------

Co-authored-by: iamtoruk <hello@agentseal.org>
2026-08-29 00:37:30 -07:00

147 lines
5.2 KiB
Swift

import Foundation
import Testing
@testable import CodeBurnMenubar
@Suite("Provider settings editor state")
struct ProviderSettingsEditorStateTests {
@Test("switching providers replaces a draft secret with the destination provider's stored credential")
func switchingProvidersDoesNotLeakDraftSecret() {
var editor = ProviderSettingsEditorState.load(
providerID: "clinepass",
stored: CapacityDockProviderCredential(sourceMode: "api", apiKey: "synthetic-clinepass-key")
)
editor.credential.apiKey = "synthetic-unsaved-clinepass-key"
editor.localError = "temporary"
editor.applyProviderChange(
to: "opencodego",
stored: CapacityDockProviderCredential()
)
#expect(editor.providerID == "opencodego")
#expect(editor.credential == CapacityDockProviderCredential())
#expect(editor.savedCredential == CapacityDockProviderCredential())
#expect(editor.localError == nil)
}
@Test("reloading the same provider keeps an unsaved draft")
func sameProviderReloadKeepsDraft() {
var editor = ProviderSettingsEditorState.load(
providerID: "clinepass",
stored: CapacityDockProviderCredential()
)
editor.credential.apiKey = "synthetic-unsaved-clinepass-key"
editor.applyProviderChange(
to: "clinepass",
stored: CapacityDockProviderCredential()
)
#expect(editor.credential.apiKey == "synthetic-unsaved-clinepass-key")
}
@Test("switching onto a provider with a stored credential loads that credential")
func destinationStoredCredentialReplacesDraft() {
var editor = ProviderSettingsEditorState.load(
providerID: "clinepass",
stored: CapacityDockProviderCredential(sourceMode: "api", apiKey: "synthetic-clinepass-key")
)
editor.credential.apiKey = "synthetic-unsaved-clinepass-key"
let stored = CapacityDockProviderCredential(sourceMode: "api", apiKey: "synthetic-opencodego-key")
editor.applyProviderChange(to: "opencodego", stored: stored)
#expect(editor.credential == stored)
#expect(editor.savedCredential == stored)
}
@Test("a late Keychain result cannot populate a newly selected provider")
func staleAsyncCredentialResultIsIgnored() {
var editor = ProviderSettingsEditorState.load(
providerID: "clinepass",
stored: CapacityDockProviderCredential()
)
editor.beginLoading(providerID: "opencodego")
editor.applyLoadedCredential(
CapacityDockProviderCredential(
sourceMode: "api",
apiKey: "synthetic-stale-clinepass-key"
),
for: "clinepass"
)
#expect(editor.providerID == "opencodego")
#expect(editor.credential == CapacityDockProviderCredential())
#expect(editor.savedCredential == CapacityDockProviderCredential())
}
@Test("the current provider accepts its own asynchronous Keychain result")
func currentAsyncCredentialResultIsApplied() {
var editor = ProviderSettingsEditorState.load(
providerID: "",
stored: CapacityDockProviderCredential()
)
editor.beginLoading(providerID: "clinepass")
let stored = CapacityDockProviderCredential(
sourceMode: "api",
apiKey: "synthetic-clinepass-key"
)
editor.applyLoadedCredential(stored, for: "clinepass")
#expect(editor.credential == stored)
#expect(editor.savedCredential == stored)
}
@Test("a stalled Keychain operation times out without blocking the caller")
func stalledCredentialOperationTimesOut() async {
let semaphore = DispatchSemaphore(value: 0)
defer { semaphore.signal() }
do {
let _: Int = try await CapacityDockProviderCredentialStore.performAsync(
timeout: 0.05
) {
semaphore.wait()
return 1
}
Issue.record("Expected the stalled credential operation to time out")
} catch let error as CapacityDockProviderCredentialStoreError {
#expect(error == .timedOut)
} catch {
Issue.record("Unexpected timeout error: \(error)")
}
}
@Test("credential mutations await their real Keychain completion")
func credentialMutationsDoNotTimeOutInTheBackground() async throws {
let semaphore = DispatchSemaphore(value: 0)
let completion = LockedCompletionFlag()
let task = Task {
try await CapacityDockProviderCredentialStore.performBlocking {
semaphore.wait()
completion.setCompleted()
return 7
}
}
try await Task.sleep(for: .milliseconds(70))
#expect(!completion.isCompleted)
semaphore.signal()
#expect(try await task.value == 7)
#expect(completion.isCompleted)
}
}
private final class LockedCompletionFlag: @unchecked Sendable {
private let lock = NSLock()
private var completed = false
var isCompleted: Bool {
lock.withLock { completed }
}
func setCompleted() {
lock.withLock { completed = true }
}
}