| 1 | import Testing |
| 2 | @testable import Ed |
| 3 | |
| 4 | // The other tests only touch Swift types. This one crosses into Rust, so a |
| 5 | // framework built from different bindings fails here with UniFFI's checksum |
| 6 | // check instead of at the first call in an app. |
| 7 | @Test func newAgentReachesTheRustCore() async { |
| 8 | let agent = EdAgent() |
| 9 | #expect(await agent.isLoaded() == false) |
| 10 | |
| 11 | let info = await agent.info() |
| 12 | #expect(info.modelName == nil) |
| 13 | #expect(info.historyLength == 0) |
| 14 | } |
| 15 | |
| 16 | @Test func agentAcceptsAnAppIDAndStartsEmpty() async { |
| 17 | let agent = EdAgent(appID: "test-app") |
| 18 | #expect(await agent.history().isEmpty) |
| 19 | } |
| 20 | |
| 21 | // These need a loaded model to do anything, so without one they must be |
| 22 | // harmless rather than crash across the FFI boundary. |
| 23 | @Test func settersAndRestoreAreNoOpsWithoutAModel() async { |
| 24 | let agent = EdAgent(appID: nil) |
| 25 | await agent.setSystemPrompt("You are terse.") |
| 26 | await agent.clearSystemPrompt() |
| 27 | await agent.setSampling(EdSamplingConfiguration(temperature: 0.2, maxTokens: 64)) |
| 28 | await agent.restoreHistory([ |
| 29 | EdChatMessage(role: .user, content: "hi"), |
| 30 | EdChatMessage(role: .assistant, content: "hello"), |
| 31 | ]) |
| 32 | #expect(await agent.history().isEmpty) |
| 33 | } |
| 34 | |
| 35 | @Test func generateAndStreamFailCleanlyWithoutAModel() async { |
| 36 | let agent = EdAgent() |
| 37 | |
| 38 | await #expect(throws: (any Error).self) { |
| 39 | _ = try await agent.generate(messages: [EdChatMessage(role: .user, content: "hi")]) |
| 40 | } |
| 41 | |
| 42 | var deltas: [String] = [] |
| 43 | var failure: (any Error)? |
| 44 | do { |
| 45 | for try await delta in agent.stream("hi") { deltas.append(delta) } |
| 46 | } catch { |
| 47 | failure = error |
| 48 | } |
| 49 | #expect(deltas.isEmpty) |
| 50 | #expect(failure != nil) |
| 51 | } |
| 52 | |
| 53 | @Test func eventsStreamReportsStatusChanges() async { |
| 54 | let agent = EdAgent() |
| 55 | var events = agent.events().makeAsyncIterator() |
| 56 | |
| 57 | await agent.unload() |
| 58 | |
| 59 | guard case .statusChanged(let status, _, _)? = await events.next() else { |
| 60 | Issue.record("expected a status change") |
| 61 | return |
| 62 | } |
| 63 | #expect(status == .unloaded) |
| 64 | } |