| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import EquipoKit |
| 4 | |
| 5 | @MainActor @Suite struct MarkdownStreamTests { |
| 6 | func fixture() throws -> (EquipoSession, Snapshot, String) { |
| 7 | let accounts = DeviceAccounts(store: MemorySecretStore()) |
| 8 | let transport = ScriptedTransport() |
| 9 | let account = DeviceAccount.new(origin: transport.origin, label: "Test") |
| 10 | let session = EquipoSession(account: account, accounts: accounts, transport: transport) |
| 11 | var snapshot: Snapshot = try Fixture.decode("thin/snapshot") |
| 12 | var turn = try #require(snapshot.turns?.first) |
| 13 | turn.state = "running"; turn.attempt = 1; turn.replyId = "" |
| 14 | snapshot.turns = [turn]; snapshot.messages = [] |
| 15 | session.apply(snapshot) |
| 16 | return (session, snapshot, turn.id) |
| 17 | } |
| 18 | func delta(_ session: EquipoSession, _ id: String, _ offset: Int, _ text: String, attempt: Int = 1, resource: String = "turn") async throws { |
| 19 | let data: [String: Any] = ["type": "delta", "resource": resource, "id": id, "turn_id": id, "attempt": attempt, |
| 20 | "data": ["turn_id": id, "offset": offset, "text": text]] |
| 21 | await session.handle("data: " + String(decoding: try JSONSerialization.data(withJSONObject: data), as: UTF8.self)) |
| 22 | } |
| 23 | @Test func draftsUseByteOffsetsAndRejectDuplicatesGapsAndOldAttempts() async throws { |
| 24 | let (session, _, id) = try fixture() |
| 25 | try await delta(session, id, 0, "**é") |
| 26 | try await delta(session, id, 3, "wrong character offset") |
| 27 | try await delta(session, id, 0, "duplicate") |
| 28 | try await delta(session, id, 9, "gap") |
| 29 | #expect(session.drafts[id]?.text == "**é") |
| 30 | try await delta(session, id, 4, "**") |
| 31 | #expect(session.drafts[id]?.text == "**é**") |
| 32 | try await delta(session, id, 0, "new", attempt: 2) |
| 33 | try await delta(session, id, 3, "stale", attempt: 1) |
| 34 | try await delta(session, id, 0, "old restart", attempt: 1) |
| 35 | try await delta(session, id, 0, "other resource", attempt: 3, resource: "question") |
| 36 | #expect(session.drafts[id]?.text == "new") |
| 37 | #expect(session.drafts[id]?.attempt == 2) |
| 38 | let reply = try #require(Feed.build(session).compactMap(\.reply).first) |
| 39 | #expect(reply.draft) |
| 40 | #expect(reply.text == "new") |
| 41 | } |
| 42 | @Test func committedReplyReplacesDraftWithTheSameIdentityAndIgnoresLateDeltas() async throws { |
| 43 | let (session, original, id) = try fixture() |
| 44 | try await delta(session, id, 0, "```sh\nls\n") |
| 45 | let draft = try #require(Feed.build(session).first { $0.reply != nil }) |
| 46 | var snapshot = original |
| 47 | let message = ThinMessage(id: "m-finished", revision: 100, role: "coordinator", text: "```sh\nls\n```\n", turnId: id, createdAt: Date()) |
| 48 | snapshot.messages = [message] |
| 49 | snapshot.turns?[0].replyId = message.id |
| 50 | snapshot.turns?[0].state = "replied" |
| 51 | snapshot.turns?[0].revision += 1 |
| 52 | session.apply(snapshot) |
| 53 | #expect(session.drafts[id] == nil) |
| 54 | let finished = try #require(Feed.build(session).first { $0.reply != nil }) |
| 55 | #expect(finished.id == draft.id) |
| 56 | #expect(finished.reply?.draft == false) |
| 57 | try await delta(session, id, 0, "late", attempt: 2) |
| 58 | #expect(session.drafts[id] == nil) |
| 59 | } |
| 60 | @Test func aSnapshotStartedBeforeTheTurnDoesNotLoseItsFirstChunks() async throws { |
| 61 | let (session, original, _) = try fixture() |
| 62 | let id = "turn-not-yet-in-snapshot" |
| 63 | try await delta(session, id, 0, "first") |
| 64 | session.apply(original) |
| 65 | #expect(session.drafts[id]?.text == "first") |
| 66 | var newer = original |
| 67 | var turn = try #require(original.turns?.first) |
| 68 | turn.id = id |
| 69 | newer.turns?.append(turn) |
| 70 | session.apply(newer) |
| 71 | try await delta(session, id, 5, " second") |
| 72 | #expect(session.drafts[id]?.text == "first second") |
| 73 | } |
| 74 | @Test func signOutClearsAnActiveDraftAndIgnoresLateChunks() async throws { |
| 75 | let (session, _, id) = try fixture() |
| 76 | try await delta(session, id, 0, "still streaming") |
| 77 | #expect(!session.drafts.isEmpty) |
| 78 | session.invalidate() |
| 79 | #expect(session.drafts.isEmpty) |
| 80 | try await delta(session, id, 15, "late") |
| 81 | try await delta(session, "unknown", 0, "another turn") |
| 82 | #expect(session.drafts.isEmpty) |
| 83 | } |
| 84 | @Test func terminalTurnsNewAttemptsAndSignOutDiscardDrafts() async throws { |
| 85 | let (session, original, id) = try fixture() |
| 86 | try await delta(session, id, 0, "first") |
| 87 | var snapshot = original |
| 88 | snapshot.turns?[0].revision += 1 |
| 89 | snapshot.turns?[0].attempt = 2 |
| 90 | session.apply(snapshot) |
| 91 | #expect(session.drafts[id] == nil) |
| 92 | try await delta(session, id, 0, "stale", attempt: 1) |
| 93 | #expect(session.drafts[id] == nil) |
| 94 | try await delta(session, id, 0, "next", attempt: 2) |
| 95 | #expect(session.drafts[id]?.text == "next") |
| 96 | snapshot.turns?[0].revision += 1 |
| 97 | snapshot.turns?[0].state = "cancelled" |
| 98 | session.apply(snapshot) |
| 99 | #expect(session.drafts[id] == nil) |
| 100 | try await delta(session, id, 0, "after cancel", attempt: 2) |
| 101 | #expect(session.drafts[id] == nil) |
| 102 | session.invalidate() |
| 103 | try await delta(session, "unknown", 0, "after sign-out") |
| 104 | #expect(session.drafts.isEmpty) |
| 105 | } |
| 106 | } |
| 107 | |