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,89 @@
|
||||
//
|
||||
// OpenAIProviderTests.swift
|
||||
// oAITests
|
||||
//
|
||||
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
||||
// Copyright (C) 2026 Rune Olsen
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable import oAI
|
||||
|
||||
@Suite("OpenAIProvider request/response conversion")
|
||||
struct OpenAIProviderTests {
|
||||
|
||||
private var provider: OpenAIProvider { OpenAIProvider(apiKey: "test-key") }
|
||||
|
||||
// MARK: - buildURLRequest
|
||||
|
||||
@Test("Sets the Authorization header and JSON content type")
|
||||
func setsAuthAndContentTypeHeaders() throws {
|
||||
let request = ChatRequest(messages: [Message(role: .user, content: "hi")], model: "gpt-4o")
|
||||
let urlRequest = try provider.buildURLRequest(from: request, stream: false)
|
||||
#expect(urlRequest.value(forHTTPHeaderField: "Authorization") == "Bearer test-key")
|
||||
#expect(urlRequest.value(forHTTPHeaderField: "Content-Type") == "application/json")
|
||||
#expect(urlRequest.value(forHTTPHeaderField: "Accept") == nil)
|
||||
}
|
||||
|
||||
@Test("Streaming requests add an SSE Accept header and stream_options")
|
||||
func streamingAddsAcceptHeaderAndUsageOption() throws {
|
||||
let request = ChatRequest(messages: [Message(role: .user, content: "hi")], model: "gpt-4o")
|
||||
let urlRequest = try provider.buildURLRequest(from: request, stream: true)
|
||||
#expect(urlRequest.value(forHTTPHeaderField: "Accept") == "text/event-stream")
|
||||
|
||||
let body = try JSONSerialization.jsonObject(with: urlRequest.httpBody!) as! [String: Any]
|
||||
let streamOptions = body["stream_options"] as? [String: Any]
|
||||
#expect(streamOptions?["include_usage"] as? Bool == true)
|
||||
}
|
||||
|
||||
@Test("o1/o3 reasoning models omit temperature even when one is requested")
|
||||
func reasoningModelsOmitTemperature() throws {
|
||||
let request = ChatRequest(messages: [Message(role: .user, content: "hi")], model: "o1-preview", temperature: 0.7)
|
||||
let urlRequest = try provider.buildURLRequest(from: request, stream: false)
|
||||
let body = try JSONSerialization.jsonObject(with: urlRequest.httpBody!) as! [String: Any]
|
||||
#expect(body["temperature"] == nil)
|
||||
}
|
||||
|
||||
@Test("Non-reasoning models include the requested temperature")
|
||||
func nonReasoningModelsIncludeTemperature() throws {
|
||||
let request = ChatRequest(messages: [Message(role: .user, content: "hi")], model: "gpt-4o", temperature: 0.7)
|
||||
let urlRequest = try provider.buildURLRequest(from: request, stream: false)
|
||||
let body = try JSONSerialization.jsonObject(with: urlRequest.httpBody!) as! [String: Any]
|
||||
#expect(body["temperature"] as? Double == 0.7)
|
||||
}
|
||||
|
||||
@Test("A message with an image attachment produces multi-part vision-format content")
|
||||
func imageAttachmentProducesMultipartContent() throws {
|
||||
let attachment = FileAttachment(path: "photo.png", type: .image, data: Data([0x01]))
|
||||
let message = Message(role: .user, content: "look at this", attachments: [attachment])
|
||||
let request = ChatRequest(messages: [message], model: "gpt-4o")
|
||||
let urlRequest = try provider.buildURLRequest(from: request, stream: false)
|
||||
let body = try JSONSerialization.jsonObject(with: urlRequest.httpBody!) as! [String: Any]
|
||||
let messages = body["messages"] as? [[String: Any]]
|
||||
let content = messages?.first?["content"] as? [[String: Any]]
|
||||
#expect(content?.count == 2)
|
||||
#expect(content?.first?["type"] as? String == "text")
|
||||
#expect(content?.last?["type"] as? String == "image_url")
|
||||
}
|
||||
|
||||
// MARK: - convertToChatResponse
|
||||
|
||||
@Test("Returns an empty-content response when there are no choices, rather than throwing")
|
||||
func convertToChatResponseEmptyChoicesFallback() {
|
||||
let apiResponse = OpenRouterChatResponse(id: "x", model: "m", choices: [], usage: nil, created: 0)
|
||||
let response = provider.convertToChatResponse(apiResponse)
|
||||
#expect(response.content == "")
|
||||
#expect(response.role == "assistant")
|
||||
}
|
||||
|
||||
// MARK: - fallbackModels
|
||||
|
||||
@Test("Fallback models are non-empty, all support tools, and are sorted by name")
|
||||
func fallbackModelsAreWellFormed() {
|
||||
let models = provider.fallbackModels()
|
||||
#expect(!models.isEmpty)
|
||||
#expect(models.allSatisfy { $0.capabilities.tools == true })
|
||||
#expect(models.allSatisfy { $0.capabilities.online == false })
|
||||
#expect(models.map(\.name) == models.map(\.name).sorted())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user