PodCommsSessionTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // PodCommsSessionTests.swift
  3. // OmniKitTests
  4. //
  5. // Created by Pete Schwamb on 3/25/19.
  6. // Copyright © 2019 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. import XCTest
  10. @testable import OmniKit
  11. class MockMessageTransport: MessageTransport {
  12. var delegate: MessageTransportDelegate?
  13. var messageNumber: Int
  14. var responseMessageBlocks = [MessageBlock]()
  15. public var sentMessages = [Message]()
  16. var address: UInt32
  17. var sentMessageHandler: ((Message) -> Void)?
  18. init(address: UInt32, messageNumber: Int) {
  19. self.address = address
  20. self.messageNumber = messageNumber
  21. }
  22. func sendMessage(_ message: Message) throws -> Message {
  23. sentMessages.append(message)
  24. if responseMessageBlocks.isEmpty {
  25. throw PodCommsError.noResponse
  26. }
  27. return Message(address: address, messageBlocks: [responseMessageBlocks.removeFirst()], sequenceNum: messageNumber)
  28. }
  29. func addResponse(_ messageBlock: MessageBlock) {
  30. responseMessageBlocks.append(messageBlock)
  31. }
  32. func assertOnSessionQueue() {
  33. // Do nothing in tests
  34. }
  35. }
  36. class PodCommsSessionTests: XCTestCase, PodCommsSessionDelegate {
  37. var lastPodStateUpdate: PodState?
  38. func podCommsSession(_ podCommsSession: PodCommsSession, didChange state: PodState) {
  39. lastPodStateUpdate = state
  40. }
  41. func testNonceResync() {
  42. // From https://raw.githubusercontent.com/wiki/openaps/openomni/Full-life-of-a-pod-(omni-flo).md
  43. // 2018-05-25T13:03:51.765792 pod Message(ffffffff seq:01 [OmniKitPacketParser.VersionResponse(blockType: OmniKitPacketParser.MessageBlockType.versionResponse, lot: 43620, tid: 560313, address: Optional(521580830), pmVersion: 2.7.0, piVersion: 2.7.0, data: 23 bytes)])
  44. let podState = PodState(address: 521580830, piVersion: "2.7.0", pmVersion: "2.7.0", lot: 43620, tid: 560313, insulinType: .novolog)
  45. let messageTransport = MockMessageTransport(address: podState.address, messageNumber: 5)
  46. do {
  47. // 2018-05-26T09:11:08.580347 pod Message(1f16b11e seq:06 [OmniKitPacketParser.ErrorResponse(blockType: OmniKitPacketParser.MessageBlockType.errorResponse, errorReponseType: OmniKitPacketParser.ErrorResponse.ErrorReponseType.badNonce, nonceSearchKey: 43492, data: 5 bytes)])
  48. messageTransport.addResponse(try ErrorResponse(encodedData: Data(hexadecimalString: "060314a9e403f5")!))
  49. messageTransport.addResponse(try StatusResponse(encodedData: Data(hexadecimalString: "1d5800d1a8140012e3ff8018")!))
  50. } catch (let error) {
  51. XCTFail("message decoding threw error: \(error)")
  52. return
  53. }
  54. let session = PodCommsSession(podState: podState, transport: messageTransport, delegate: self)
  55. // 2018-05-26T09:11:07.984983 pdm Message(1f16b11e seq:05 [SetInsulinScheduleCommand(nonce:2232447658, bolus(units: 1.0, timeBetweenPulses: 2.0)), OmniKitPacketParser.BolusExtraCommand(blockType: OmniKitPacketParser.MessageBlockType.bolusExtra, completionBeep: false, programReminderInterval: 0.0, units: 1.0, timeBetweenPulses: 2.0, squareWaveUnits: 0.0, squareWaveDuration: 0.0)])
  56. let bolusDelivery = SetInsulinScheduleCommand.DeliverySchedule.bolus(units: 1.0, timeBetweenPulses: 2.0)
  57. let sentCommand = SetInsulinScheduleCommand(nonce: 2232447658, deliverySchedule: bolusDelivery)
  58. do {
  59. let status: StatusResponse = try session.send([sentCommand])
  60. XCTAssertEqual(2, messageTransport.sentMessages.count)
  61. let bolusTry1 = messageTransport.sentMessages[0].messageBlocks[0] as! SetInsulinScheduleCommand
  62. XCTAssertEqual(2232447658, bolusTry1.nonce)
  63. let bolusTry2 = messageTransport.sentMessages[1].messageBlocks[0] as! SetInsulinScheduleCommand
  64. XCTAssertEqual(1521036535, bolusTry2.nonce)
  65. XCTAssert(status.deliveryStatus.bolusing)
  66. } catch (let error) {
  67. XCTFail("message sending error: \(error)")
  68. }
  69. // Try sending another bolus command: nonce should be 676940027
  70. XCTAssertEqual(545302454, lastPodStateUpdate!.currentNonce)
  71. let _ = session.bolus(units: 2, automatic: false)
  72. let bolusTry3 = messageTransport.sentMessages[2].messageBlocks[0] as! SetInsulinScheduleCommand
  73. XCTAssertEqual(545302454, bolusTry3.nonce)
  74. }
  75. }