eletrotupi / tcc/ api/tests/insights/trigger-pattern.unit.test.ts7ff50aa722f838fa59d0afb52d1c2e2b96bf3e22
1.5 KB | 55 lines Raw
1
import { describe, it, expect } from 'vitest'
2
import { TriggerType } from '@prisma/client'
3
import {
4
  countTriggerCategories,
5
  triggerPatternBody,
6
} from '@app/lib/queue/processors/insights/triggerPattern'
7

8
const trigger = (category: TriggerType) => ({ category })
9

10
describe('countTriggerCategories', () => {
11
  it('counts each category and sorts the most frequent first', () => {
12
    const sorted = countTriggerCategories([
13
      trigger('WORK'),
14
      trigger('SOCIAL'),
15
      trigger('WORK'),
16
      trigger('OTHER'),
17
      trigger('WORK'),
18
    ])
19

20
    expect(sorted).toEqual([
21
      ['WORK', 3],
22
      ['SOCIAL', 1],
23
      ['OTHER', 1],
24
    ])
25
  })
26

27
  it('leads with the top category', () => {
28
    const [top] = countTriggerCategories([
29
      trigger('HEALTH'),
30
      trigger('HEALTH'),
31
      trigger('SOCIAL'),
32
    ])
33

34
    expect(top).toEqual(['HEALTH', 2])
35
  })
36

37
  it('returns an empty distribution for no triggers', () => {
38
    expect(countTriggerCategories([])).toEqual([])
39
  })
40
})
41

42
describe('triggerPatternBody', () => {
43
  it('names the category and its share of the period', () => {
44
    expect(triggerPatternBody('WORK', 3, 6)).toBe(
45
      'Gatilhos de trabalho foram os mais frequentes, representando 50% dos registros do período.',
46
    )
47
  })
48

49
  it('wraps an OTHER category in the generic wording', () => {
50
    expect(triggerPatternBody('OTHER', 2, 4)).toBe(
51
      'Gatilhos do tipo "outros" foram os mais frequentes, ' +
52
        'representando 50% dos registros do período.',
53
    )
54
  })
55
})