import Foundation import Testing @testable import EquipoKit @MainActor @Suite struct MarkdownStreamTests { func fixture() throws -> (EquipoSession, Snapshot, String) { let accounts = DeviceAccounts(store: MemorySecretStore()) let transport = ScriptedTransport() let account = DeviceAccount.new(origin: transport.origin, label: "Test") let session = EquipoSession(account: account, accounts: accounts, transport: transport) var snapshot: Snapshot = try Fixture.decode("thin/snapshot") var turn = try #require(snapshot.turns?.first) turn.state = "running"; turn.attempt = 1; turn.replyId = "" snapshot.turns = [turn]; snapshot.messages = [] session.apply(snapshot) return (session, snapshot, turn.id) } func delta(_ session: EquipoSession, _ id: String, _ offset: Int, _ text: String, attempt: Int = 1, resource: String = "turn") async throws { let data: [String: Any] = ["type": "delta", "resource": resource, "id": id, "turn_id": id, "attempt": attempt, "data": ["turn_id": id, "offset": offset, "text": text]] await session.handle("data: " + String(decoding: try JSONSerialization.data(withJSONObject: data), as: UTF8.self)) } @Test func draftsUseByteOffsetsAndRejectDuplicatesGapsAndOldAttempts() async throws { let (session, _, id) = try fixture() try await delta(session, id, 0, "**é") try await delta(session, id, 3, "wrong character offset") try await delta(session, id, 0, "duplicate") try await delta(session, id, 9, "gap") #expect(session.drafts[id]?.text == "**é") try await delta(session, id, 4, "**") #expect(session.drafts[id]?.text == "**é**") try await delta(session, id, 0, "new", attempt: 2) try await delta(session, id, 3, "stale", attempt: 1) try await delta(session, id, 0, "old restart", attempt: 1) try await delta(session, id, 0, "other resource", attempt: 3, resource: "question") #expect(session.drafts[id]?.text == "new") #expect(session.drafts[id]?.attempt == 2) let reply = try #require(Feed.build(session).compactMap(\.reply).first) #expect(reply.draft) #expect(reply.text == "new") } @Test func committedReplyReplacesDraftWithTheSameIdentityAndIgnoresLateDeltas() async throws { let (session, original, id) = try fixture() try await delta(session, id, 0, "```sh\nls\n") let draft = try #require(Feed.build(session).first { $0.reply != nil }) var snapshot = original let message = ThinMessage(id: "m-finished", revision: 100, role: "coordinator", text: "```sh\nls\n```\n", turnId: id, createdAt: Date()) snapshot.messages = [message] snapshot.turns?[0].replyId = message.id snapshot.turns?[0].state = "replied" snapshot.turns?[0].revision += 1 session.apply(snapshot) #expect(session.drafts[id] == nil) let finished = try #require(Feed.build(session).first { $0.reply != nil }) #expect(finished.id == draft.id) #expect(finished.reply?.draft == false) try await delta(session, id, 0, "late", attempt: 2) #expect(session.drafts[id] == nil) } @Test func aSnapshotStartedBeforeTheTurnDoesNotLoseItsFirstChunks() async throws { let (session, original, _) = try fixture() let id = "turn-not-yet-in-snapshot" try await delta(session, id, 0, "first") session.apply(original) #expect(session.drafts[id]?.text == "first") var newer = original var turn = try #require(original.turns?.first) turn.id = id newer.turns?.append(turn) session.apply(newer) try await delta(session, id, 5, " second") #expect(session.drafts[id]?.text == "first second") } @Test func signOutClearsAnActiveDraftAndIgnoresLateChunks() async throws { let (session, _, id) = try fixture() try await delta(session, id, 0, "still streaming") #expect(!session.drafts.isEmpty) session.invalidate() #expect(session.drafts.isEmpty) try await delta(session, id, 15, "late") try await delta(session, "unknown", 0, "another turn") #expect(session.drafts.isEmpty) } @Test func terminalTurnsNewAttemptsAndSignOutDiscardDrafts() async throws { let (session, original, id) = try fixture() try await delta(session, id, 0, "first") var snapshot = original snapshot.turns?[0].revision += 1 snapshot.turns?[0].attempt = 2 session.apply(snapshot) #expect(session.drafts[id] == nil) try await delta(session, id, 0, "stale", attempt: 1) #expect(session.drafts[id] == nil) try await delta(session, id, 0, "next", attempt: 2) #expect(session.drafts[id]?.text == "next") snapshot.turns?[0].revision += 1 snapshot.turns?[0].state = "cancelled" session.apply(snapshot) #expect(session.drafts[id] == nil) try await delta(session, id, 0, "after cancel", attempt: 2) #expect(session.drafts[id] == nil) session.invalidate() try await delta(session, "unknown", 0, "after sign-out") #expect(session.drafts.isEmpty) } }