OpenAPSFixed.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 mealJavascript(
  37. pumphistory: JSON,
  38. profile: JSON,
  39. basalProfile: JSON,
  40. clock: JSON,
  41. carbs: JSON,
  42. glucose: JSON
  43. ) async -> OrefFunctionResult {
  44. let testBundle = Bundle(for: OpenAPSFixed.self)
  45. do {
  46. let result = try await withCheckedThrowingContinuation { continuation in
  47. jsWorker.inCommonContext { worker in
  48. worker.evaluateBatch(scripts: [
  49. Script(name: "prepare/log.js"),
  50. Script.fromTestingBundle(name: "meal.js", bundle: testBundle),
  51. Script(name: "prepare/meal.js")
  52. ])
  53. let result = worker.call(function: "generate", with: [
  54. pumphistory,
  55. profile,
  56. clock,
  57. glucose,
  58. basalProfile,
  59. carbs
  60. ])
  61. continuation.resume(returning: result)
  62. }
  63. }
  64. return .success(result)
  65. } catch {
  66. return .failure(error)
  67. }
  68. }
  69. func autosenseJavascript(
  70. glucose: JSON,
  71. pumpHistory: JSON,
  72. basalprofile: JSON,
  73. profile: JSON,
  74. carbs: JSON,
  75. temptargets: JSON,
  76. clock: JSON
  77. ) async -> OrefFunctionResult {
  78. do {
  79. let result = try await withCheckedThrowingContinuation { continuation in
  80. let testBundle = Bundle(for: OpenAPSFixed.self)
  81. jsWorker.inCommonContext { worker in
  82. worker.evaluateBatch(scripts: [
  83. Script(name: "prepare/log.js"),
  84. Script.fromTestingBundle(name: "autosens.js", bundle: testBundle),
  85. Script.fromTestingBundle(name: "autosens-prepare.js", bundle: testBundle)
  86. ])
  87. let result = worker.call(function: "generate", with: [
  88. glucose,
  89. pumpHistory,
  90. basalprofile,
  91. profile,
  92. carbs,
  93. temptargets,
  94. clock
  95. ])
  96. continuation.resume(returning: result)
  97. }
  98. }
  99. return .success(result)
  100. } catch {
  101. return .failure(error)
  102. }
  103. }
  104. func iobJavascript(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON) async -> OrefFunctionResult {
  105. do {
  106. let testBundle = Bundle(for: OpenAPSFixed.self)
  107. let pumphistory: JSON = try! sortPumpHistory(pumpHistory: pumphistory)
  108. let result = try await withCheckedThrowingContinuation { continuation in
  109. jsWorker.inCommonContext { worker in
  110. worker.evaluateBatch(scripts: [
  111. Script(name: "prepare/log.js"),
  112. Script.fromTestingBundle(name: "iob.js", bundle: testBundle),
  113. Script(name: "prepare/iob.js")
  114. ])
  115. let result = worker.call(function: "generate", with: [
  116. pumphistory,
  117. profile,
  118. clock,
  119. autosens
  120. ])
  121. continuation.resume(returning: result)
  122. }
  123. }
  124. return .success(result)
  125. } catch {
  126. return .failure(error)
  127. }
  128. }
  129. }
  130. extension Script {
  131. static func fromTestingBundle(name: String, bundle: Bundle) -> Script {
  132. let body: String
  133. if let url = bundle.url(forResource: "\(name)", withExtension: "") {
  134. do {
  135. body = try String(contentsOf: url)
  136. } catch {
  137. print("Error loading script: \(error.localizedDescription)")
  138. body = "Error loading script"
  139. }
  140. } else {
  141. print("Resource not found: javascript/\(name)")
  142. testPrintAllJSFiles(testBundle: bundle)
  143. body = "Resource not found"
  144. }
  145. return Script(name: name, body: body)
  146. }
  147. static func testPrintAllJSFiles(testBundle: Bundle) {
  148. // Get all .js files in the bundle
  149. if let jsURLs = testBundle.urls(forResourcesWithExtension: "js", subdirectory: nil) {
  150. print("JavaScript files in test bundle:")
  151. for jsURL in jsURLs {
  152. print("- \(jsURL.lastPathComponent)")
  153. print(" Full path: \(jsURL.path)")
  154. }
  155. print("Total JS files found: \(jsURLs.count)")
  156. } else {
  157. print("No JavaScript files found in test bundle")
  158. }
  159. }
  160. }