OpenAPSFixed.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import Combine
  2. import Foundation
  3. import JavaScriptCore
  4. @testable import Trio
  5. /// This class provides us with an implementation of trio-oref with a number of iob bugs that are fixed.
  6. /// We can use this during testing to confirm that for an input that generated an error that a corrected
  7. /// Javascript implementation would have produced the same results
  8. final class OpenAPSFixed {
  9. private let jsWorker = JavaScriptWorker()
  10. func sortPumpHistory(pumpHistory: JSON) throws -> JSON {
  11. let pumpHistorySwift = try JSONBridge.pumpHistory(from: pumpHistory)
  12. return try JSONBridge.to(pumpHistorySwift.sorted(by: { $0.timestamp > $1.timestamp }))
  13. }
  14. func iobHistory(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON, zeroTempDuration: JSON) async throws -> JSON {
  15. let testBundle = Bundle(for: OpenAPSFixed.self)
  16. let pumphistory: JSON = try! sortPumpHistory(pumpHistory: pumphistory)
  17. let result = try await withCheckedThrowingContinuation { continuation in
  18. jsWorker.inCommonContext { worker in
  19. worker.evaluateBatch(scripts: [
  20. Script(name: "prepare/log.js"),
  21. Script.fromTestingBundle(name: "iob-history.js", bundle: testBundle),
  22. Script.fromTestingBundle(name: "iob-history-prepare.js", bundle: testBundle)
  23. ])
  24. let result = worker.call(function: "generate", with: [
  25. pumphistory,
  26. profile,
  27. clock,
  28. autosens,
  29. zeroTempDuration
  30. ])
  31. continuation.resume(returning: result)
  32. }
  33. }
  34. return result
  35. }
  36. func iobJavascript(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON) async -> OrefFunctionResult {
  37. do {
  38. let testBundle = Bundle(for: OpenAPSFixed.self)
  39. let pumphistory: JSON = try! sortPumpHistory(pumpHistory: pumphistory)
  40. let result = try await withCheckedThrowingContinuation { continuation in
  41. jsWorker.inCommonContext { worker in
  42. worker.evaluateBatch(scripts: [
  43. Script(name: "prepare/log.js"),
  44. Script.fromTestingBundle(name: "iob.js", bundle: testBundle),
  45. Script(name: "prepare/iob.js")
  46. ])
  47. let result = worker.call(function: "generate", with: [
  48. pumphistory,
  49. profile,
  50. clock,
  51. autosens
  52. ])
  53. continuation.resume(returning: result)
  54. }
  55. }
  56. return .success(result)
  57. } catch {
  58. return .failure(error)
  59. }
  60. }
  61. }
  62. extension Script {
  63. static func fromTestingBundle(name: String, bundle: Bundle) -> Script {
  64. let body: String
  65. if let url = bundle.url(forResource: "\(name)", withExtension: "") {
  66. do {
  67. body = try String(contentsOf: url)
  68. } catch {
  69. print("Error loading script: \(error.localizedDescription)")
  70. body = "Error loading script"
  71. }
  72. } else {
  73. print("Resource not found: javascript/\(name)")
  74. body = "Resource not found"
  75. }
  76. return Script(name: name, body: body)
  77. }
  78. }