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