OpenAPSFixed.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. func sortPumpHistory(pumpHistory: JSON) throws -> JSON {
  10. let pumpHistorySwift = try JSONBridge.pumpHistory(from: pumpHistory)
  11. return try JSONBridge.to(pumpHistorySwift.sorted(by: { $0.timestamp > $1.timestamp }))
  12. }
  13. private func middlewareScript(name: String) -> Script? {
  14. if let url = Foundation.Bundle.main.url(forResource: "javascript/\(name)", withExtension: "") {
  15. do {
  16. let body = try String(contentsOf: url)
  17. return Script(name: name, body: body)
  18. } catch {
  19. debug(.openAPS, "Failed to load script \(name): \(error)")
  20. }
  21. }
  22. return nil
  23. }
  24. func determineBasalJavascript(
  25. glucose: JSON,
  26. currentTemp: JSON,
  27. iob: JSON,
  28. profile: JSON,
  29. autosens: JSON,
  30. meal: JSON,
  31. microBolusAllowed: Bool,
  32. reservoir: JSON,
  33. pumpHistory: JSON,
  34. preferences: JSON,
  35. basalProfile: JSON,
  36. trioCustomOrefVariables: JSON,
  37. clock: Date
  38. ) async throws -> OrefFunctionResult {
  39. do {
  40. let jsWorker = JavaScriptWorker(poolSize: 1)
  41. let testBundle = Bundle(for: OpenAPSFixed.self)
  42. let result = try await withCheckedThrowingContinuation { continuation in
  43. jsWorker.inCommonContext { worker in
  44. worker.evaluateBatch(scripts: [
  45. Script(name: "prepare/log.js"),
  46. Script.fromTestingBundle(name: "determine-basal-prepare.js", bundle: testBundle),
  47. Script.fromTestingBundle(name: "basal-set-temp.js", bundle: testBundle),
  48. Script.fromTestingBundle(name: "glucose-get-last.js", bundle: testBundle),
  49. Script.fromTestingBundle(name: "determine-basal.js", bundle: testBundle)
  50. ])
  51. if let middleware = self.middlewareScript(name: OpenAPS.Middleware.determineBasal) {
  52. worker.evaluate(script: middleware)
  53. }
  54. let result = worker.call(function: "generate", with: [
  55. iob,
  56. currentTemp,
  57. glucose,
  58. profile,
  59. autosens,
  60. meal,
  61. microBolusAllowed,
  62. reservoir,
  63. clock,
  64. pumpHistory,
  65. preferences,
  66. basalProfile,
  67. trioCustomOrefVariables
  68. ])
  69. continuation.resume(returning: result)
  70. }
  71. }
  72. return .success(result)
  73. } catch {
  74. return .failure(error)
  75. }
  76. }
  77. func iobHistory(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON, zeroTempDuration: JSON) async throws -> JSON {
  78. let jsWorker = JavaScriptWorker(poolSize: 1)
  79. let testBundle = Bundle(for: OpenAPSFixed.self)
  80. let pumphistory: JSON = try! sortPumpHistory(pumpHistory: pumphistory)
  81. let result = try await withCheckedThrowingContinuation { continuation in
  82. jsWorker.inCommonContext { worker in
  83. worker.evaluateBatch(scripts: [
  84. Script(name: "prepare/log.js"),
  85. Script.fromTestingBundle(name: "iob-history.js", bundle: testBundle),
  86. Script.fromTestingBundle(name: "iob-history-prepare.js", bundle: testBundle)
  87. ])
  88. let result = worker.call(function: "generate", with: [
  89. pumphistory,
  90. profile,
  91. clock,
  92. autosens,
  93. zeroTempDuration
  94. ])
  95. continuation.resume(returning: result)
  96. }
  97. }
  98. return result
  99. }
  100. func mealJavascript(
  101. pumphistory: JSON,
  102. profile: JSON,
  103. basalProfile: JSON,
  104. clock: JSON,
  105. carbs: JSON,
  106. glucose: JSON
  107. ) async -> OrefFunctionResult {
  108. let jsWorker = JavaScriptWorker(poolSize: 1)
  109. let testBundle = Bundle(for: OpenAPSFixed.self)
  110. do {
  111. let result = try await withCheckedThrowingContinuation { continuation in
  112. jsWorker.inCommonContext { worker in
  113. worker.evaluateBatch(scripts: [
  114. Script(name: "prepare/log.js"),
  115. Script.fromTestingBundle(name: "meal.js", bundle: testBundle),
  116. Script(name: "prepare/meal.js")
  117. ])
  118. let result = worker.call(function: "generate", with: [
  119. pumphistory,
  120. profile,
  121. clock,
  122. glucose,
  123. basalProfile,
  124. carbs
  125. ])
  126. continuation.resume(returning: result)
  127. }
  128. }
  129. return .success(result)
  130. } catch {
  131. return .failure(error)
  132. }
  133. }
  134. func autosenseJavascript(
  135. glucose: JSON,
  136. pumpHistory: JSON,
  137. basalprofile: JSON,
  138. profile: JSON,
  139. carbs: JSON,
  140. temptargets: JSON,
  141. clock: JSON
  142. ) async -> OrefFunctionResult {
  143. do {
  144. let result = try await withCheckedThrowingContinuation { continuation in
  145. let jsWorker = JavaScriptWorker(poolSize: 1)
  146. let testBundle = Bundle(for: OpenAPSFixed.self)
  147. jsWorker.inCommonContext { worker in
  148. worker.evaluateBatch(scripts: [
  149. Script(name: "prepare/log.js"),
  150. Script.fromTestingBundle(name: "autosens.js", bundle: testBundle),
  151. Script.fromTestingBundle(name: "autosens-prepare.js", bundle: testBundle)
  152. ])
  153. let result = worker.call(function: "generate", with: [
  154. glucose,
  155. pumpHistory,
  156. basalprofile,
  157. profile,
  158. carbs,
  159. temptargets,
  160. clock
  161. ])
  162. continuation.resume(returning: result)
  163. }
  164. }
  165. return .success(result)
  166. } catch {
  167. return .failure(error)
  168. }
  169. }
  170. func iobJavascript(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON) async -> OrefFunctionResult {
  171. do {
  172. let testBundle = Bundle(for: OpenAPSFixed.self)
  173. let pumphistory: JSON = try! sortPumpHistory(pumpHistory: pumphistory)
  174. let result = try await withCheckedThrowingContinuation { continuation in
  175. let jsWorker = JavaScriptWorker(poolSize: 1)
  176. jsWorker.inCommonContext { worker in
  177. worker.evaluateBatch(scripts: [
  178. Script(name: "prepare/log.js"),
  179. Script.fromTestingBundle(name: "iob.js", bundle: testBundle),
  180. Script(name: "prepare/iob.js")
  181. ])
  182. let result = worker.call(function: "generate", with: [
  183. pumphistory,
  184. profile,
  185. clock,
  186. autosens
  187. ])
  188. continuation.resume(returning: result)
  189. }
  190. }
  191. return .success(result)
  192. } catch {
  193. return .failure(error)
  194. }
  195. }
  196. }
  197. extension Script {
  198. static func fromTestingBundle(name: String, bundle: Bundle) -> Script {
  199. let body: String
  200. if let url = bundle.url(forResource: "\(name)", withExtension: "") {
  201. do {
  202. body = try String(contentsOf: url)
  203. } catch {
  204. print("Error loading script: \(error.localizedDescription)")
  205. body = "Error loading script"
  206. }
  207. } else {
  208. print("Resource not found: javascript/\(name)")
  209. testPrintAllJSFiles(testBundle: bundle)
  210. body = "Resource not found"
  211. }
  212. return Script(name: name, body: body)
  213. }
  214. static func testPrintAllJSFiles(testBundle: Bundle) {
  215. // Get all .js files in the bundle
  216. if let jsURLs = testBundle.urls(forResourcesWithExtension: "js", subdirectory: nil) {
  217. print("JavaScript files in test bundle:")
  218. for jsURL in jsURLs {
  219. print("- \(jsURL.lastPathComponent)")
  220. print(" Full path: \(jsURL.path)")
  221. }
  222. print("Total JS files found: \(jsURLs.count)")
  223. } else {
  224. print("No JavaScript files found in test bundle")
  225. }
  226. }
  227. }