Phase 2: tests for the provider layer and other exposed pure helpers
66 new tests across 8 files (93 total in the suite now): - GitSyncServiceTests: convertToSSH/injectCredentials/sanitizeFilename/ detectSecretsInText, including a below-threshold false-positive check on the secret regex. - ChatViewModelPureLogicTests: detectGoodbyePhrase, inferProvider, calculateCost -- including the cache-read (0.1x) / cache-write (1.25x) pricing multipliers, which is real billing-affecting logic. - EmbeddingServiceTests / ContextSelectionServiceTests: embedding (de)serialization round-trip, importance-score weighting, and the token-estimate fallback (content.count / 4) when no real count exists. - OpenRouterProviderTests / OllamaProviderTests / OpenAIProviderTests / AnthropicProviderTests: request-building (attachments, online mode, cache_control breakpoints, o1/o3 temperature omission, tool schema conversion) and response-parsing (text, tool_use blocks, empty-choices fallback behavior) for all four providers, with no network involved. Also marks calculateCost/inferProvider/detectGoodbyePhrase (ChatViewModel) and serializeEmbedding/deserializeEmbedding (EmbeddingService) as `nonisolated` -- discovered via the actual test failures, not speculation: the project's `SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor` setting isolates the classes that carry an explicit `@MainActor` (ChatViewModel), so calling their static members from a plain synchronous @Test needs the pure ones marked `nonisolated`. Matches the existing convention already used elsewhere in EmbeddingService (cosineSimilarity was already nonisolated before this change). Phase 2 of the test-suite rollout plan (peaceful-baking-kurzweil).
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// ContextSelectionServiceTests.swift
|
||||
// oAITests
|
||||
//
|
||||
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
||||
// Copyright (C) 2026 Rune Olsen
|
||||
|
||||
import Testing
|
||||
@testable import oAI
|
||||
|
||||
@Suite("ContextSelectionService pure scoring helpers")
|
||||
struct ContextSelectionServiceTests {
|
||||
|
||||
private var service: ContextSelectionService { ContextSelectionService.shared }
|
||||
|
||||
// MARK: - getImportanceScore
|
||||
|
||||
@Test("A short message with no cost or token data scores from length alone")
|
||||
func lowScoreForPlainShortMessage() {
|
||||
let message = Message(role: .user, content: "hi")
|
||||
#expect(service.getImportanceScore(message) < 0.01)
|
||||
}
|
||||
|
||||
@Test("Cost, length, and token factors combine to the exact weighted sum")
|
||||
func combinesWeightedFactors() {
|
||||
// cost 0.005 / 0.01 = 0.5 -> * 0.5 weight = 0.25
|
||||
// 1000 chars / 2000 = 0.5 -> * 0.3 weight = 0.15
|
||||
// 500 tokens / 1000 = 0.5 -> * 0.2 weight = 0.1
|
||||
// total = 0.5
|
||||
let message = Message(role: .assistant, content: String(repeating: "a", count: 1000), tokens: 500, cost: 0.005)
|
||||
#expect(abs(service.getImportanceScore(message) - 0.5) < 0.0001)
|
||||
}
|
||||
|
||||
@Test("Score is capped at 1.0 even when every factor maxes out and would otherwise overflow it")
|
||||
func scoreIsCappedAtOne() {
|
||||
let message = Message(
|
||||
role: .assistant,
|
||||
content: String(repeating: "a", count: 5000), // well past the 2000-char max
|
||||
tokens: 5000, // well past the 1000-token max
|
||||
cost: 1.0 // well past the $0.01 max
|
||||
)
|
||||
#expect(service.getImportanceScore(message) == 1.0)
|
||||
}
|
||||
|
||||
// MARK: - estimateTokens
|
||||
|
||||
@Test("Uses the message's actual token count when present")
|
||||
func usesActualTokenCountWhenAvailable() {
|
||||
let messages = [Message(role: .user, content: "irrelevant for this test", tokens: 42)]
|
||||
#expect(service.estimateTokens(messages) == 42)
|
||||
}
|
||||
|
||||
@Test("Falls back to content.count / 4 when tokens is nil")
|
||||
func fallsBackToCharacterEstimate() {
|
||||
let messages = [Message(role: .user, content: String(repeating: "x", count: 40))]
|
||||
#expect(service.estimateTokens(messages) == 10)
|
||||
}
|
||||
|
||||
@Test("Sums estimates across a mix of messages with and without token counts")
|
||||
func sumsAcrossMixedMessages() {
|
||||
let messages = [
|
||||
Message(role: .user, content: "ignored", tokens: 100),
|
||||
Message(role: .assistant, content: String(repeating: "y", count: 20)), // 20/4 = 5
|
||||
]
|
||||
#expect(service.estimateTokens(messages) == 105)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user