feat(m1): Ollama client, catalog, streaming chat and CLI (T1.1-T1.6)
Add endpoint policy and redirect-safe ephemeral HTTP client, error mapping, catalog cache and version checks, streaming chat with phase timeouts/cancellation, provisional Hy-MT3 prompt/language/request factory, warm-up policy, and sprachmittler-cli models/show/translate. Recapture the 7B fixtures; defer the 1.8B VERIFY items; document ADR-016. Also fix the signature checker's pipefail SIGPIPE issue.
This commit is contained in:
1 parent
7ca5c710ec
commit
26b493e7e5
93 files changed
+3732
-218
No files matched your search
@@ -0,0 +1,221 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import SprachmittlerOllama
|
||||
|
||||
/// HTTP transport tests share `StubURLProtocol`; the suite is serialized so the static stub
|
||||
/// cannot leak between tests.
|
||||
@Suite("Ollama HTTP transport", .serialized)
|
||||
struct OllamaTransportTests {
|
||||
@Test func sendsGETAndReturnsBody() async throws {
|
||||
StubURLProtocol.reset(.init(chunks: [Data(#"{"version":"0.40.18"}"#.utf8)]))
|
||||
defer { StubURLProtocol.reset() }
|
||||
|
||||
let client = try makeClient()
|
||||
let response = try await client.send(.init(path: "/api/version"))
|
||||
#expect(response.status == 200)
|
||||
#expect(String(data: response.body, encoding: .utf8) == #"{"version":"0.40.18"}"#)
|
||||
}
|
||||
|
||||
@Test func maps404ModelNotFound() async throws {
|
||||
StubURLProtocol.reset(.init(
|
||||
status: 404,
|
||||
chunks: [Data(#"{"error":"model 'missing' not found"}"#.utf8)]
|
||||
))
|
||||
defer { StubURLProtocol.reset() }
|
||||
|
||||
let client = try makeClient()
|
||||
do {
|
||||
_ = try await client.send(.init(path: "/api/chat"), model: "missing")
|
||||
Issue.record("expected modelNotInstalled")
|
||||
} catch let error as OllamaError {
|
||||
#expect(error == .modelNotInstalled("missing"))
|
||||
}
|
||||
}
|
||||
|
||||
@Test func maps400And500() async throws {
|
||||
let client = try makeClient()
|
||||
|
||||
StubURLProtocol.reset(.init(status: 400, chunks: [Data(#"{"error":"bad input"}"#.utf8)]))
|
||||
do {
|
||||
_ = try await client.send(.init(path: "/api/chat"))
|
||||
Issue.record("expected badRequest")
|
||||
} catch let error as OllamaError {
|
||||
#expect(error == .badRequest("bad input"))
|
||||
}
|
||||
|
||||
StubURLProtocol.reset(.init(status: 500, chunks: [Data(#"{"error":"out of memory"}"#.utf8)]))
|
||||
do {
|
||||
_ = try await client.send(.init(path: "/api/chat"))
|
||||
Issue.record("expected server")
|
||||
} catch let error as OllamaError {
|
||||
#expect(error == .server(status: 500, message: "out of memory"))
|
||||
}
|
||||
StubURLProtocol.reset()
|
||||
}
|
||||
|
||||
@Test func mapsTransportFailures() async throws {
|
||||
let client = try makeClient()
|
||||
|
||||
StubURLProtocol.reset(.init(error: URLError(.cannotConnectToHost)))
|
||||
do {
|
||||
_ = try await client.send(.init(path: "/api/version"))
|
||||
Issue.record("expected unreachable")
|
||||
} catch let error as OllamaError {
|
||||
guard case .unreachable = error else {
|
||||
Issue.record("expected unreachable, got \(error)")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
StubURLProtocol.reset(.init(error: URLError(.timedOut)))
|
||||
do {
|
||||
_ = try await client.send(.init(path: "/api/version"))
|
||||
Issue.record("expected timeout")
|
||||
} catch let error as OllamaError {
|
||||
#expect(error == .timeout(.connect))
|
||||
}
|
||||
StubURLProtocol.reset()
|
||||
}
|
||||
|
||||
@Test func decodesJSONAndRejectsMalformedJSON() async throws {
|
||||
StubURLProtocol.reset(.init(chunks: [Data(#"{"version":"0.40.18"}"#.utf8)]))
|
||||
let client = try makeClient()
|
||||
let version = try await client.sendJSON(.init(path: "/api/version"), as: VersionResponse.self)
|
||||
#expect(version.version == "0.40.18")
|
||||
|
||||
StubURLProtocol.reset(.init(chunks: [Data("not json".utf8)]))
|
||||
do {
|
||||
_ = try await client.sendJSON(.init(path: "/api/version"), as: VersionResponse.self)
|
||||
Issue.record("expected decoding error")
|
||||
} catch let error as OllamaError {
|
||||
guard case .decoding = error else {
|
||||
Issue.record("expected decoding, got \(error)")
|
||||
return
|
||||
}
|
||||
}
|
||||
StubURLProtocol.reset()
|
||||
}
|
||||
|
||||
@Test func joinsPathsWithoutDoubleSlash() throws {
|
||||
let client = try makeClient()
|
||||
#expect(
|
||||
try client.makeURL(for: "/api/version").absoluteString
|
||||
== "http://127.0.0.1:11434/api/version"
|
||||
)
|
||||
#expect(
|
||||
try client.makeURL(for: "api/tags").absoluteString
|
||||
== "http://127.0.0.1:11434/api/tags"
|
||||
)
|
||||
}
|
||||
|
||||
@Test func joinsPathsWithTrailingSlashAndBasePath() throws {
|
||||
let trailing = try makeClient(baseURL: "http://127.0.0.1:11434/")
|
||||
#expect(
|
||||
try trailing.makeURL(for: "/api/tags").absoluteString
|
||||
== "http://127.0.0.1:11434/api/tags"
|
||||
)
|
||||
|
||||
let basePath = try makeClient(baseURL: "http://127.0.0.1:11434/ollama/")
|
||||
#expect(
|
||||
try basePath.makeURL(for: "/api/tags").absoluteString
|
||||
== "http://127.0.0.1:11434/ollama/api/tags"
|
||||
)
|
||||
|
||||
let baseNoSlash = try makeClient(baseURL: "http://127.0.0.1:11434/ollama")
|
||||
#expect(
|
||||
try baseNoSlash.makeURL(for: "/api/tags").absoluteString
|
||||
== "http://127.0.0.1:11434/ollama/api/tags"
|
||||
)
|
||||
}
|
||||
|
||||
@Test func setsJSONHeadersOnPOST() async throws {
|
||||
StubURLProtocol.reset(.init(chunks: [Data(#"{"ok":true}"#.utf8)]))
|
||||
defer { StubURLProtocol.reset() }
|
||||
|
||||
let client = try makeClient()
|
||||
_ = try await client.send(.init(
|
||||
method: .post,
|
||||
path: "/api/show",
|
||||
body: Data(#"{"model":"x"}"#.utf8)
|
||||
))
|
||||
let request = try #require(StubURLProtocol.snapshot().lastRequest)
|
||||
#expect(request.httpMethod == "POST")
|
||||
#expect(request.value(forHTTPHeaderField: "Content-Type") == "application/json")
|
||||
#expect(request.value(forHTTPHeaderField: "Accept") == "application/json")
|
||||
}
|
||||
|
||||
@Test func delegatePassesNilToTheCompletionHandler() throws {
|
||||
let url = try #require(URL(string: "http://127.0.0.1:11434/redirect"))
|
||||
let delegate = RedirectRefusingDelegate()
|
||||
let session = URLSession(configuration: .ephemeral)
|
||||
let task = session.dataTask(with: url)
|
||||
let response = try #require(HTTPURLResponse(
|
||||
url: url,
|
||||
statusCode: 302,
|
||||
httpVersion: nil,
|
||||
headerFields: ["Location": "https://example.com/steal"]
|
||||
))
|
||||
let box = RedirectResultBox()
|
||||
|
||||
delegate.urlSession(
|
||||
session,
|
||||
task: task,
|
||||
willPerformHTTPRedirection: response,
|
||||
newRequest: URLRequest(url: url)
|
||||
) { request in
|
||||
box.record(request)
|
||||
}
|
||||
|
||||
#expect(box.wasCalled)
|
||||
#expect(box.request == nil)
|
||||
}
|
||||
|
||||
@Test func clientRefusesA302WithoutRequestingTheTarget() async throws {
|
||||
StubURLProtocol.reset(.init(
|
||||
status: 302,
|
||||
headers: ["Location": "https://example.com/steal"],
|
||||
chunks: []
|
||||
))
|
||||
defer { StubURLProtocol.reset() }
|
||||
|
||||
let client = try makeClient()
|
||||
do {
|
||||
_ = try await client.send(.init(path: "/api/version"))
|
||||
Issue.record("expected redirect rejection")
|
||||
} catch let error as OllamaError {
|
||||
#expect(error == .endpointRejected(.redirect))
|
||||
}
|
||||
|
||||
let snapshot = StubURLProtocol.snapshot()
|
||||
#expect(snapshot.count == 1)
|
||||
#expect(snapshot.lastRequest?.url?.host == "127.0.0.1")
|
||||
#expect(client.session.delegate is RedirectRefusingDelegate)
|
||||
}
|
||||
|
||||
private func makeClient(baseURL: String = "http://127.0.0.1:11434") throws -> OllamaHTTPClient {
|
||||
let url = try #require(URL(string: baseURL))
|
||||
let configuration = OllamaSessionFactory.makeConfiguration()
|
||||
configuration.protocolClasses = [StubURLProtocol.self]
|
||||
let session = URLSession(
|
||||
configuration: configuration,
|
||||
delegate: RedirectRefusingDelegate(),
|
||||
delegateQueue: nil
|
||||
)
|
||||
return try OllamaHTTPClient(baseURL: url, session: session)
|
||||
}
|
||||
}
|
||||
|
||||
private struct VersionResponse: Decodable {
|
||||
let version: String
|
||||
}
|
||||
|
||||
/// A synchronous result box for the delegate callback; the callback is not concurrent here.
|
||||
private final class RedirectResultBox: @unchecked Sendable {
|
||||
private(set) var wasCalled = false
|
||||
private(set) var request: URLRequest?
|
||||
|
||||
func record(_ request: URLRequest?) {
|
||||
wasCalled = true
|
||||
self.request = request
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user