DynamicISFEnableTests.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import CoreData
  2. import Foundation
  3. import Swinject
  4. import Testing
  5. @testable import Trio
  6. @Suite("Dynamic ISF Enable Logic Tests", .serialized) struct DynamicISFEnableTests {
  7. var coreDataStack: CoreDataStack!
  8. var context: NSManagedObjectContext!
  9. init() async throws {
  10. // In-memory Core Data for tests
  11. coreDataStack = try await CoreDataStack.createForTests()
  12. context = coreDataStack.newTaskContext()
  13. }
  14. func testEnableLogic(percentSamples: Double) async throws -> Bool {
  15. let numberOfSamples = Int(288 * 7 * percentSamples)
  16. let now = Date() // internal function uses Date()
  17. try await context.perform {
  18. for index in 0 ..< numberOfSamples {
  19. let timeDelta = Double(index * 5 * 60)
  20. let tdd = TDDStored(context: context)
  21. tdd.date = now - timeDelta
  22. tdd.total = 30
  23. tdd.bolus = 15
  24. tdd.tempBasal = 15
  25. tdd.scheduledBasal = 0
  26. }
  27. try context.save()
  28. }
  29. return try await BaseTDDStorage.hasSufficientTDD(context: context)
  30. }
  31. @Test("Confirm samples from last 7 days enables Dynamic ISF") func testPercentSamplesEnablingLogic() async throws {
  32. let enabled = try await testEnableLogic(percentSamples: 0.8)
  33. #expect(enabled)
  34. }
  35. @Test("Confirm insufficient samples from last 7 days disables Dynamic ISF") func testPercentSamplesDisablesLogic() async throws {
  36. let enabled = try await testEnableLogic(percentSamples: 0.7)
  37. #expect(!enabled)
  38. }
  39. }