Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130import Foundationimport Testing@testable import CoreATProtocol
@Suite("Jetstream frames")struct JetstreamTests { @Test("Commit frames decode with the record as JSON") func commit() throws { let text = """ {"$type":"message","payload":{"$type":"network.bsky.jetstream.subscribeEvents#commit","seq":12345,"did":"did:plc:abc","time":"2026-10-09T12:00:00.000001Z","witnessedAt":"2026-10-09T12:00:00.000002Z","rev":"3jzfcijpj2z2a","operation":"create","collection":"xyz.effem.feed.subscription","rkey":"3jx2abcdefghi","cid":"bafyreifhsti7kbd5wzjg5tvv35er5ilogdwifhbnt5go6ksxuesp7obeau","record":{"$type":"xyz.effem.feed.subscription","subject":{"uri":"at://did:web:catalog.effem.app/xyz.effem.feed.podcast/abc","cid":"bafyreifhsti7kbd5wzjg5tvv35er5ilogdwifhbnt5go6ksxuesp7obeau"},"createdAt":"2026-10-09T11:59:59.000Z"}}} """ guard case .message(let event) = try JetstreamFrame.decode(text), case .commit(let commit) = event else { Issue.record("expected a commit") return } #expect(commit.seq == 12345) #expect(commit.operation == .create) #expect(commit.collection == "xyz.effem.feed.subscription") #expect(commit.uri == "at://did:plc:abc/xyz.effem.feed.subscription/3jx2abcdefghi") #expect(commit.record?["subject"]?["uri"]?.string == "at://did:web:catalog.effem.app/xyz.effem.feed.podcast/abc") #expect(event.witnessedAt == "2026-10-09T12:00:00.000002Z") #expect(event.seq == 12345) }
@Test("Delete commits have no record or cid") func delete() throws { let text = #"{"$type":"message","payload":{"$type":"network.bsky.jetstream.subscribeEvents#commit","seq":2,"did":"did:plc:abc","time":"2026-10-09T12:00:00Z","rev":"3jzfcijpj2z2a","operation":"delete","collection":"xyz.effem.feed.comment","rkey":"3jx2abcdefghi"}}"# guard case .message(.commit(let commit)) = try JetstreamFrame.decode(text) else { Issue.record("expected a commit") return } #expect(commit.operation == .delete) #expect(commit.record == nil) #expect(commit.cid == nil) }
@Test("Identity, account, sync and info frames decode") func otherKinds() throws { let identity = #"{"$type":"message","payload":{"$type":"network.bsky.jetstream.subscribeEvents#identity","seq":3,"did":"did:plc:abc","time":"2026-10-09T12:00:00Z","identity":{"did":"did:plc:abc","handle":"alice.test","seq":99,"time":"2026-10-09T11:59:00Z"}}}"# guard case .message(.identity(let identityEvent)) = try JetstreamFrame.decode(identity) else { Issue.record("expected identity") return } #expect(identityEvent.identity.handle == "alice.test")
let account = #"{"$type":"message","payload":{"$type":"network.bsky.jetstream.subscribeEvents#account","seq":4,"did":"did:plc:abc","time":"2026-10-09T12:00:00Z","account":{"did":"did:plc:abc","active":false,"status":"takendown","seq":100,"time":"2026-10-09T11:59:00Z"}}}"# guard case .message(.account(let accountEvent)) = try JetstreamFrame.decode(account) else { Issue.record("expected account") return } #expect(accountEvent.account.active == false) #expect(accountEvent.account.status == "takendown")
let sync = #"{"$type":"message","payload":{"$type":"network.bsky.jetstream.subscribeEvents#sync","seq":5,"did":"did:plc:abc","time":"2026-10-09T12:00:00Z","sync":{"did":"did:plc:abc","rev":"3jzfcijpj2z2a","seq":101,"time":"2026-10-09T11:59:00Z"}}}"# guard case .message(.sync(let syncEvent)) = try JetstreamFrame.decode(sync) else { Issue.record("expected sync") return } #expect(syncEvent.sync.rev == "3jzfcijpj2z2a")
let info = #"{"$type":"message","payload":{"$type":"network.bsky.jetstream.subscribeEvents#info","name":"OutdatedCursor","message":"resumed from 42"}}"# guard case .message(let infoEvent) = try JetstreamFrame.decode(info), case .info(let payload) = infoEvent else { Issue.record("expected info") return } #expect(payload.name == JetstreamInfo.outdatedCursor) #expect(infoEvent.seq == nil) }
@Test("Error frames and unknown payloads are surfaced, missing envelopes rejected") func errorsAndUnknowns() throws { guard case .error(let error) = try JetstreamFrame.decode(#"{"$type":"error","error":"ConsumerTooSlow","message":"too slow"}"#) else { Issue.record("expected error frame") return } #expect(error.code == JetstreamStreamError.consumerTooSlow) guard case .message(.unknown(let type)) = try JetstreamFrame.decode(#"{"$type":"message","payload":{"$type":"network.bsky.jetstream.subscribeEvents#future"}}"#) else { Issue.record("expected unknown") return } #expect(type.hasSuffix("#future")) #expect(throws: JetstreamDecodingError.missingEnvelope) { try JetstreamFrame.decode(#"{"did":"did:plc:abc","time_us":1}"#) } }
@Test("A commit without a sequence number decodes, for hosts that don't assign them") func commitWithoutSeq() throws { let text = #"{"$type":"message","payload":{"$type":"network.bsky.jetstream.subscribeEvents#commit","did":"did:plc:abc","time":"2026-10-09T12:00:00Z","rev":"3jzfcijpj2z2a","operation":"delete","collection":"xyz.effem.feed.comment","rkey":"3jx2abcdefghi"}}"# guard case .message(let event) = try JetstreamFrame.decode(text), case .commit(let commit) = event else { Issue.record("expected a commit") return } #expect(commit.seq == nil) #expect(event.seq == nil) }
@Test("Events encode to their payload shape and decode back unchanged", arguments: [ #"{"$type":"network.bsky.jetstream.subscribeEvents#commit","seq":12345,"did":"did:plc:abc","time":"2026-10-09T12:00:00.000001Z","witnessedAt":"2026-10-09T12:00:00.000002Z","rev":"3jzfcijpj2z2a","operation":"create","collection":"xyz.effem.feed.subscription","rkey":"3jx2abcdefghi","cid":"bafyreifhsti7kbd5wzjg5tvv35er5ilogdwifhbnt5go6ksxuesp7obeau","record":{"$type":"xyz.effem.feed.subscription","createdAt":"2026-10-09T11:59:59.000Z","count":3}}"#, #"{"$type":"network.bsky.jetstream.subscribeEvents#commit","did":"did:plc:abc","time":"2026-10-09T12:00:00Z","rev":"3jzfcijpj2z2a","operation":"delete","collection":"xyz.effem.feed.comment","rkey":"3jx2abcdefghi"}"#, #"{"$type":"network.bsky.jetstream.subscribeEvents#identity","seq":3,"did":"did:plc:abc","time":"2026-10-09T12:00:00Z","identity":{"did":"did:plc:abc","handle":"alice.test","seq":99,"time":"2026-10-09T11:59:00Z"}}"#, #"{"$type":"network.bsky.jetstream.subscribeEvents#account","seq":4,"did":"did:plc:abc","time":"2026-10-09T12:00:00Z","account":{"did":"did:plc:abc","active":false,"status":"takendown","seq":100,"time":"2026-10-09T11:59:00Z"}}"#, #"{"$type":"network.bsky.jetstream.subscribeEvents#sync","seq":5,"did":"did:plc:abc","time":"2026-10-09T12:00:00Z","sync":{"did":"did:plc:abc","rev":"3jzfcijpj2z2a","seq":101,"time":"2026-10-09T11:59:00Z"}}"#, #"{"$type":"network.bsky.jetstream.subscribeEvents#info","name":"OutdatedCursor","message":"resumed from 42"}"#, #"{"$type":"network.bsky.jetstream.subscribeEvents#future"}"#, ]) func roundTrip(payload: String) throws { let event = try JSONDecoder().decode(JetstreamEvent.self, from: Data(payload.utf8)) let encoded = try JSONEncoder().encode(event) #expect(try JSONDecoder().decode(JetstreamEvent.self, from: encoded) == event) #expect(try JSONValue.parse(encoded) == JSONValue.parse(payload)) }
@Test("Subscription URLs carry kinds, collections and the cursor") func subscriptionURL() throws { var subscription = JetstreamSubscription(host: "wss://jetstream.us-east.bsky.network", kinds: [.commit, .account], collections: ["xyz.effem.feed.comment", "xyz.effem.feed.*"]) subscription.cursor = .seq(42) let url = try subscription.url() #expect(url.absoluteString == "wss://jetstream.us-east.bsky.network/xrpc/network.bsky.jetstream.subscribeEvents?kinds=commit&kinds=account&collections=xyz.effem.feed.comment&collections=xyz.effem.feed.*&cursor=42") subscription.cursor = .timestamp(microseconds: 1_760_000_000_000_000) #expect(try subscription.url().query?.contains("cursor=1760000000000000") == true) #expect(throws: JetstreamSubscription.ValidationError.invalidHost("https://example.com")) { try JetstreamSubscription(host: "https://example.com").url() } let tooMany = JetstreamSubscription(host: "wss://example.com", collections: Array(repeating: "a.b.c", count: 101)) #expect(throws: JetstreamSubscription.ValidationError.tooManyCollections(101)) { try tooMany.url() } }}