eletrotupi / tcc/ api/tests/insights/mood-trend.unit.test.ts7ff50aa722f838fa59d0afb52d1c2e2b96bf3e22
2.2 KB | 86 lines Raw
1
import { describe, it, expect } from 'vitest'
2
import { BaseMoodOption } from '@prisma/client'
3
import {
4
  computeMoodTrend,
5
  moodTrendBody,
6
} from '@app/lib/queue/processors/insights/moodTrend'
7

8
const mood = (selectedMood: BaseMoodOption, energyLevel: number) => ({
9
  selectedMood,
10
  energyLevel,
11
})
12

13
describe('computeMoodTrend', () => {
14
  it('averages each half, scales the delta, and picks the dominant mood', () => {
15
    const trend = computeMoodTrend([
16
      mood('SAD', 6),
17
      mood('NEUTRAL', 6),
18
      mood('GREAT', 6),
19
      mood('GREAT', 6),
20
      mood('GREAT', 6),
21
    ])
22

23
    expect(trend).toEqual({
24
      delta: 100,
25
      avgFirst: 2.5,
26
      avgSecond: 5,
27
      dominantMood: 'GREAT',
28
      avgEnergy: 6,
29
    })
30
  })
31

32
  it('reports a negative delta when the second half dips', () => {
33
    const trend = computeMoodTrend([
34
      mood('GREAT', 6),
35
      mood('GREAT', 6),
36
      mood('SAD', 6),
37
      mood('SAD', 6),
38
      mood('SAD', 6),
39
    ])
40

41
    expect(trend.delta).toBe(-60)
42
    expect(trend.dominantMood).toBe('SAD')
43
  })
44

45
  it('reports a zero delta and the mean energy for a flat period', () => {
46
    const trend = computeMoodTrend([
47
      mood('GOOD', 3),
48
      mood('GOOD', 9),
49
      mood('GOOD', 6),
50
    ])
51

52
    expect(trend.delta).toBe(0)
53
    expect(trend.avgEnergy).toBe(6)
54
  })
55
})
56

57
describe('moodTrendBody', () => {
58
  it('celebrates an improving delta', () => {
59
    expect(moodTrendBody(100)).toBe(
60
      'Seu humor melhorou 100% em relação ao início do período.',
61
    )
62
  })
63

64
  it('names a worsening delta by its magnitude', () => {
65
    expect(moodTrendBody(-60)).toBe(
66
      'Seu humor caiu 60% em relação ao início do período.',
67
    )
68
  })
69

70
  it('calls a small delta stable', () => {
71
    expect(moodTrendBody(5)).toBe(
72
      'Seu humor se manteve estável durante o período.',
73
    )
74
  })
75

76
  it('treats a delta of exactly ten as stable and eleven as a change', () => {
77
    expect(moodTrendBody(10)).toBe(
78
      'Seu humor se manteve estável durante o período.',
79
    )
80
    expect(moodTrendBody(-10)).toBe(
81
      'Seu humor se manteve estável durante o período.',
82
    )
83
    expect(moodTrendBody(11)).toContain('melhorou')
84
    expect(moodTrendBody(-11)).toContain('caiu')
85
  })
86
})