From 3a090c5d8a89e8552fcf461b8663e1c5fa7f530f Mon Sep 17 00:00:00 2001
From: Julia Friesel <julia.friesel@gmail.com>
Date: Sat, 30 Jun 2018 14:57:51 +0200
Subject: [PATCH] For temperature, add 2 passing test for simple happy path

---
 lib/sensiplan.js                  | 39 ++++++++++++++++++++
 lib/sensiplan/index.js            |  7 ----
 test/fixtures/lower-temps.json    | 31 ++++++++++++++++
 test/fixtures/regular-cycles.json |  1 -
 test/fixtures/temp-shift.json     | 61 +++++++++++++++++++++++++++++++
 test/sensiplan.spec.js            | 24 ++++++++----
 6 files changed, 148 insertions(+), 15 deletions(-)
 create mode 100644 lib/sensiplan.js
 delete mode 100644 lib/sensiplan/index.js
 create mode 100644 test/fixtures/lower-temps.json
 delete mode 100644 test/fixtures/regular-cycles.json
 create mode 100644 test/fixtures/temp-shift.json

diff --git a/lib/sensiplan.js b/lib/sensiplan.js
new file mode 100644
index 00000000..1539f3aa
--- /dev/null
+++ b/lib/sensiplan.js
@@ -0,0 +1,39 @@
+function getTemperatureStatus(targetDateString, previousDaysInCycle) {
+  const tempValues = previousDaysInCycle
+    .filter(day => day.temperature)
+    .map(day => !day.temperature.exclude && rounded(day.temperature.value, 0.05))
+
+  return tempValues.reduce((acc, curr) => {
+    // if we don't yet have 6 lower temps, we just collect
+    // if no shift has been detected, we collect low temps
+    // after the shift has been detected, we count them as part
+    // of the higher temperature phase
+    if (acc.low.length < 6 || (!acc.shiftDetected && curr <= acc.ltl)) {
+      acc.low.push(curr)
+      acc.ltl = Math.max(...acc.low.slice(-6))
+    } else {
+      acc.high.push(curr)
+    }
+
+    // we round the difference because of JS decimal weirdness
+    if (acc.high.length === 3 && rounded(curr - acc.ltl, 0.01) >= 0.2) {
+      acc.shiftDetected = true
+    }
+
+    return acc
+  }, {
+    low: [],
+    high: [],
+    ltl: null,
+    shiftDetected: false
+  })
+}
+
+function rounded(val, step) {
+  const inverted = 1 / step
+  return Math.round(val * inverted) / inverted
+}
+
+export {
+  getTemperatureStatus
+}
\ No newline at end of file
diff --git a/lib/sensiplan/index.js b/lib/sensiplan/index.js
deleted file mode 100644
index 0d733f43..00000000
--- a/lib/sensiplan/index.js
+++ /dev/null
@@ -1,7 +0,0 @@
-function getTemperatureStatus (targetDate) {
-  return '42'
-}
-
-export {
-  getTemperatureStatus
-}
\ No newline at end of file
diff --git a/test/fixtures/lower-temps.json b/test/fixtures/lower-temps.json
new file mode 100644
index 00000000..20ae0884
--- /dev/null
+++ b/test/fixtures/lower-temps.json
@@ -0,0 +1,31 @@
+[{
+  "date": "2018-06-04",
+  "temperature": {
+    "value": 36.7,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-05",
+  "temperature": {
+    "value": 36.57,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-06",
+  "temperature": {
+    "value": 36.47,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-07",
+  "temperature": {
+    "value": 36.49,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-09",
+  "temperature": {
+    "value": 36.57,
+    "exclude": null
+  }
+}]
\ No newline at end of file
diff --git a/test/fixtures/regular-cycles.json b/test/fixtures/regular-cycles.json
deleted file mode 100644
index 38b534ce..00000000
--- a/test/fixtures/regular-cycles.json
+++ /dev/null
@@ -1 +0,0 @@
-[{"date":"2018-04-30","bleeding":{"value":2}},{"date":"2018-05-08","temperature":{"value":36.48,"exclude":false}},{"date":"2018-05-09","temperature":{"value":36.46,"exclude":false}},{"date":"2018-05-10","temperature":{"value":36.4,"exclude":false}},{"date":"2018-05-11","temperature":{"value":36.38,"exclude":false}},{"date":"2018-05-12","temperature":{"value":36.57,"exclude":false},"bleeding":{"value":4}},{"date":"2018-05-13","temperature":{"value":36.51,"exclude":false},"bleeding":{"value":4}},{"date":"2018-05-14","temperature":{"value":36.64,"exclude":false},"bleeding":{"value":4}},{"date":"2018-05-15","temperature":{"value":36.5,"exclude":false},"bleeding":{"value":3}},{"date":"2018-05-16","temperature":{"value":36.74,"exclude":false}},{"date":"2018-05-17","temperature":{"value":36.74,"exclude":false}},{"date":"2018-05-18","temperature":{"value":36.85,"exclude":false}},{"date":"2018-05-29","bleeding":{"value":2}},{"date":"2018-06-04","temperature":{"value":36.7,"exclude":false}},{"date":"2018-06-05","temperature":{"value":36.57,"exclude":false}},{"date":"2018-06-06","temperature":{"value":36.47,"exclude":false}},{"date":"2018-06-07","temperature":{"value":36.49,"exclude":false}},{"date":"2018-06-08","bleeding":{"value":4}},{"date":"2018-06-09","temperature":{"value":36.57,"exclude":false},"bleeding":{"value":4}},{"date":"2018-06-10","temperature":{"value":36.62,"exclude":false},"bleeding":{"value":4}},{"date":"2018-06-11","temperature":{"value":36.55,"exclude":false},"bleeding":{"value":3}},{"date":"2018-06-12","temperature":{"value":36.8,"exclude":false}},{"date":"2018-06-13","temperature":{"value":36.86,"exclude":false}},{"date":"2018-06-14","temperature":{"value":36.82,"exclude":false}},{"date":"2018-06-24","bleeding":{"value":2}}]
\ No newline at end of file
diff --git a/test/fixtures/temp-shift.json b/test/fixtures/temp-shift.json
new file mode 100644
index 00000000..74bd5598
--- /dev/null
+++ b/test/fixtures/temp-shift.json
@@ -0,0 +1,61 @@
+[{
+  "date": "2018-06-04",
+  "temperature": {
+    "value": 36.7,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-05",
+  "temperature": {
+    "value": 36.57,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-06",
+  "temperature": {
+    "value": 36.47,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-07",
+  "temperature": {
+    "value": 36.49,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-09",
+  "temperature": {
+    "value": 36.57,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-10",
+  "temperature": {
+    "value": 36.62,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-11",
+  "temperature": {
+    "value": 36.55,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-12",
+  "temperature": {
+    "value": 36.8,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-13",
+  "temperature": {
+    "value": 36.86,
+    "exclude": null
+  }
+}, {
+  "date": "2018-06-14",
+  "temperature": {
+    "value": 36.8,
+    "exclude": null
+  }
+}]
\ No newline at end of file
diff --git a/test/sensiplan.spec.js b/test/sensiplan.spec.js
index cd36646c..3ca639e6 100644
--- a/test/sensiplan.spec.js
+++ b/test/sensiplan.spec.js
@@ -1,18 +1,28 @@
 import chai from 'chai'
-import * as sensiplan from '../lib/sensiplan'
-import cycleDaysFixtures from './fixtures/regular-cycles.json'
+import { getTemperatureStatus } from '../lib/sensiplan'
+import tempShift from './fixtures/temp-shift.json'
+import lowerTempDays from './fixtures/lower-temps.json'
 
 const expect = chai.expect
 
-describe('sensiplan', () => {
+describe.only('sensiplan', () => {
   describe('getTemperatureStatus', () => {
+    it('reports lower temperature status before shift', function () {
+      const status = getTemperatureStatus('2018-06-09', lowerTempDays)
+      expect(status).to.eql({
+        low: [36.7, 36.55, 36.45, 36.5, 36.55],
+        ltl: 36.7,
+        high: [],
+        shiftDetected: false
+      })
+    })
+
     it('detects temperature shift', function () {
-      const targetDate = '2018-06-14'
-      const status = sensiplan.getTemperatureStatus(targetDate, cycleDaysFixtures)
+      const status = getTemperatureStatus('2018-06-14', tempShift)
       expect(status).to.eql({
-        lowerTemps: [36.6, 36.5, 36.5, 36.6, 36.6, 36.6],
+        low: [36.7, 36.55, 36.45, 36.5, 36.55, 36.6, 36.55],
         ltl: 36.6,
-        higherTemps: [36.8, 36.9, 36.8],
+        high: [36.8, 36.85, 36.8],
         shiftDetected: true
       })
     })
-- 
GitLab