Cotton code
48 lines · 2.5 KB · SwiftRaw
// A ring over the chat socket: the Worker's frame (`calls.ts` `frameTo`) read as a ring or its stop, and nothing else.import CottonKitimport Foundationimport Testing@Suite struct CallRingFrameTests {    private let room = "0123456789abcdef0123456789abcdef"    private func frame(_ json: String) throws -> JSONValue {        try JSONDecoder().decode(JSONValue.self, from: Data(json.utf8))    }    @Test func aRingWithItsBoxAndVideo() throws {        let ring = CallRingFrame(frame: try frame(#"{"t":"ring","kind":"ring","room":"\#(room)","video":"1","box":"sealed"}"#))        #expect(ring == .ring(room: room, video: true, box: "sealed"))    }    @Test func aVoiceRingWithoutABox() throws {        let ring = CallRingFrame(frame: try frame(#"{"t":"ring","kind":"ring","room":"\#(room)","video":"0"}"#))        #expect(ring == .ring(room: room, video: false, box: nil))    }    @Test func aStopNamesItsReason() throws {        let stop = CallRingFrame(frame: try frame(#"{"t":"ring","kind":"stop","room":"\#(room)","stop":"answered"}"#))        #expect(stop == .stop(room: room, reason: "answered", box: nil))    }    @Test func aMissedStopCarriesWhoCalledSealed() throws {        let stop = CallRingFrame(frame: try frame(#"{"t":"ring","kind":"stop","room":"\#(room)","stop":"missed","box":"sealed"}"#))        #expect(stop == .stop(room: room, reason: "missed", box: "sealed"))    }    // RULE (CALLS FIX 2): a missed call told as the socket opens is its own kind, with the ring's times; it never rings.    @Test func aMissedCallToldLaterCarriesItsTimes() throws {        let told = CallRingFrame(frame: try frame(#"{"t":"ring","kind":"missed","room":"\#(room)","stop":"missed","box":"sealed","at":"1790000000000","ended":"1790000045000"}"#))        #expect(told == .missed(room: room, reason: "missed", box: "sealed", times: CallTimes(at: 1_790_000_000_000, ended: 1_790_000_045_000)))        let stop = CallRingFrame(frame: try frame(#"{"t":"ring","kind":"stop","room":"\#(room)","stop":"missed","box":"sealed","at":"5","ended":"4"}"#))        #expect(stop == .stop(room: room, reason: "missed", box: "sealed"), "times out of order are none")        #expect(stop?.room == room)    }    @Test func otherFramesAreNotRings() throws {        #expect(CallRingFrame(frame: try frame(#"{"t":"send"}"#)) == nil)        #expect(CallRingFrame(frame: try frame(#"{"t":"envelope","item":{}}"#)) == nil)        #expect(CallRingFrame(frame: try frame(#"{"t":"ring","kind":"ring"}"#)) == nil, "no room, no ring")    }}