Repository Public
1// ORIX Sonata2// File: Packages/SonataUI/Tests/SonataUITests/LiveAnthropicTests.swift3// Description: Opt-in live test of Compose against the real Anthropic API, with the key stored4// in the app's Keychain item. It spends API credit, so it runs only when asked:5// SONATA_LIVE_ANTHROPIC=1 swift test --filter liveCompose6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import Testing11@testable import SonataUI1213@Test(.enabled(if: ProcessInfo.processInfo.environment["SONATA_LIVE_ANTHROPIC"] == "1"))14@MainActor func liveComposeWritesAPieceThatCompiles() async throws {15 let key = AssistantKeychain.key(for: .anthropic)16 try #require(!key.isEmpty, "no Anthropic key in the Keychain item ai.spboucher.orix.sonata.assistant / anthropic")17 let knowledge = LanguageKnowledge.load(from: composerRepositoryRoot, inRepository: true)18 #expect(knowledge.missing.isEmpty)19 let prompt = ProcessInfo.processInfo.environment["SONATA_LIVE_PROMPT"] ?? "A gentle lullaby for piano in F major, 3/4, about 30 seconds, with a simple left-hand accompaniment"20 let out = URL(fileURLWithPath: ProcessInfo.processInfo.environment["SONATA_LIVE_OUT"] ?? NSTemporaryDirectory()).appendingPathComponent("sonata-live")21 let provider = LoggingProvider(inner: AnthropicProvider(settings: ProviderSettings(kind: .anthropic), key: key))22 let started = Date()23 let piece = try await Composer.compose(prompt: prompt, provider: provider, knowledge: knowledge.text, renderDirectory: out) { status in print("LIVE status: \(status)") }24 try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)25 try Data(piece.source.utf8).write(to: out.appendingPathComponent("composed.sonata"))26 print("LIVE model=\(piece.model) requests=\(piece.attempts) seconds=\(String(format: "%.1f", Date().timeIntervalSince(started))) title=\(piece.title) tracks=\(piece.tracks) notes=\(piece.notes) duration=\(String(format: "%.2f", piece.durationSec)) peak=\(piece.peakDb.map { String(format: "%.2f", $0) } ?? "-") lufs=\(piece.lufs.map { String(format: "%.1f", $0) } ?? "-") warnings=\(piece.warnings.count) out=\(out.path)")27 #expect(piece.notes >= 8 && piece.durationSec > 4)28 #expect((piece.peakDb ?? -200) > -40)29}3031/// Prints the follow-up messages the app sends (compiler diagnostics, level reports) so a live32/// run shows what the model got wrong. Never prints the key or the system prompt.33struct LoggingProvider: AssistantProvider {34 let inner: AssistantProvider35 func send(system: String, turns: [AssistantTurn], tools: [AssistantToolSpec]) async throws -> AssistantReply {36 if turns.count > 1, case .user(let text) = turns[turns.count - 1] {37 print("LIVE follow-up: " + text.replacingOccurrences(of: "\n", with: " | ").prefix(700))38 }39 let reply = try await inner.send(system: system, turns: turns, tools: tools)40 print("LIVE usage: input=\(reply.inputTokens) output=\(reply.outputTokens) stop=\(reply.stop)")41 return reply42 }43}44