| 1 | import { describe, it, expect } from 'vitest'
|
| 2 | import {
|
| 3 | computeDailyEnergy,
|
| 4 | dailyEnergyBody,
|
| 5 | } from '@app/lib/queue/processors/insights/dailyEnergy'
|
| 6 |
|
| 7 | const mood = (energyLevel: number, stressLevel: number, anxietyLevel: number) => ({
|
| 8 | energyLevel,
|
| 9 | stressLevel,
|
| 10 | anxietyLevel,
|
| 11 | })
|
| 12 |
|
| 13 | describe('computeDailyEnergy', () => {
|
| 14 | it('averages the levels, finds the peaks, and trends the day', () => {
|
| 15 | const { metadata, dayTrend } = computeDailyEnergy(
|
| 16 | [mood(8, 2, 1), mood(4, 4, 3), mood(6, 6, 5)],
|
| 17 | [],
|
| 18 | )
|
| 19 |
|
| 20 | expect(metadata).toEqual({
|
| 21 | avgEnergy: 6,
|
| 22 | avgStress: 4,
|
| 23 | avgAnxiety: 3,
|
| 24 | peak: 8,
|
| 25 | trough: 4,
|
| 26 | dayTrend: -37.5,
|
| 27 | entryCount: 3,
|
| 28 | yesterdayAvg: null,
|
| 29 | diffEnergy: null,
|
| 30 | windowLabel: 'hoje',
|
| 31 | })
|
| 32 | expect(dayTrend).toBe(-37.5)
|
| 33 | })
|
| 34 |
|
| 35 | it('rounds the averages to two decimals', () => {
|
| 36 | const { metadata } = computeDailyEnergy(
|
| 37 | [mood(7, 7, 7), mood(8, 8, 8), mood(8, 8, 8)],
|
| 38 | [],
|
| 39 | )
|
| 40 |
|
| 41 | expect(metadata).toMatchObject({
|
| 42 | avgEnergy: 7.67,
|
| 43 | avgStress: 7.67,
|
| 44 | avgAnxiety: 7.67,
|
| 45 | peak: 8,
|
| 46 | trough: 7,
|
| 47 | })
|
| 48 | })
|
| 49 |
|
| 50 | it('has no day trend with a single entry', () => {
|
| 51 | const { metadata, dayTrend } = computeDailyEnergy([mood(5, 1, 1)], [])
|
| 52 |
|
| 53 | expect(dayTrend).toBeNull()
|
| 54 | expect(metadata).toMatchObject({
|
| 55 | dayTrend: null,
|
| 56 | entryCount: 1,
|
| 57 | peak: 5,
|
| 58 | trough: 5,
|
| 59 | })
|
| 60 | })
|
| 61 |
|
| 62 | it('subtracts yesterday from the rounded today average, either way', () => {
|
| 63 | const up = computeDailyEnergy(
|
| 64 | [mood(6, 1, 1), mood(6, 1, 1)],
|
| 65 | [{ energyLevel: 4 }, { energyLevel: 4 }],
|
| 66 | ).metadata
|
| 67 |
|
| 68 | const down = computeDailyEnergy(
|
| 69 | [mood(4, 1, 1), mood(4, 1, 1)],
|
| 70 | [{ energyLevel: 9 }, { energyLevel: 9 }],
|
| 71 | ).metadata
|
| 72 |
|
| 73 | expect(up).toMatchObject({
|
| 74 | avgEnergy: 6,
|
| 75 | yesterdayAvg: 4,
|
| 76 | diffEnergy: 2,
|
| 77 | dayTrend: 0,
|
| 78 | })
|
| 79 | expect(down).toMatchObject({
|
| 80 | avgEnergy: 4,
|
| 81 | yesterdayAvg: 9,
|
| 82 | diffEnergy: -5,
|
| 83 | })
|
| 84 | })
|
| 85 | })
|
| 86 |
|
| 87 | describe('dailyEnergyBody', () => {
|
| 88 | it('picks the level from the average energy', () => {
|
| 89 | expect(dailyEnergyBody(7, null)).toBe('Energia alta hoje.')
|
| 90 | expect(dailyEnergyBody(4, null)).toBe('Energia moderada hoje.')
|
| 91 | expect(dailyEnergyBody(3, null)).toBe('Energia baixa hoje.')
|
| 92 | })
|
| 93 |
|
| 94 | it('adds the day trend when there is one', () => {
|
| 95 | expect(dailyEnergyBody(5, 20)).toBe(
|
| 96 | 'Energia moderada hoje, com melhora ao longo do dia.',
|
| 97 | )
|
| 98 | expect(dailyEnergyBody(5, -20)).toBe(
|
| 99 | 'Energia moderada hoje, com queda ao longo do dia.',
|
| 100 | )
|
| 101 | expect(dailyEnergyBody(5, 0)).toBe(
|
| 102 | 'Energia moderada e estável ao longo do dia.',
|
| 103 | )
|
| 104 | })
|
| 105 |
|
| 106 | it('treats a trend of exactly ten as stable', () => {
|
| 107 | expect(dailyEnergyBody(5, 10)).toBe(
|
| 108 | 'Energia moderada e estável ao longo do dia.',
|
| 109 | )
|
| 110 | expect(dailyEnergyBody(5, -10)).toBe(
|
| 111 | 'Energia moderada e estável ao longo do dia.',
|
| 112 | )
|
| 113 | expect(dailyEnergyBody(5, 11)).toBe(
|
| 114 | 'Energia moderada hoje, com melhora ao longo do dia.',
|
| 115 | )
|
| 116 | expect(dailyEnergyBody(5, -11)).toBe(
|
| 117 | 'Energia moderada hoje, com queda ao longo do dia.',
|
| 118 | )
|
| 119 | })
|
| 120 | })
|