aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/time/granularity.test.ts
blob: ce5142f53ba2e91787cba39424cd11056611641d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { describe, expect, test } from 'vitest'
import { TICK_UNIT_BY_LIFE_STAGE, lifeStageForAge, tickUnitForAge } from '@hollowdark/time/granularity'

describe('lifeStageForAge', () => {
  test('stage boundaries', () => {
    expect(lifeStageForAge(0)).toBe('infancy')
    expect(lifeStageForAge(2)).toBe('infancy')
    expect(lifeStageForAge(3)).toBe('early_childhood')
    expect(lifeStageForAge(6)).toBe('early_childhood')
    expect(lifeStageForAge(7)).toBe('middle_childhood')
    expect(lifeStageForAge(12)).toBe('middle_childhood')
    expect(lifeStageForAge(13)).toBe('adolescence')
    expect(lifeStageForAge(17)).toBe('adolescence')
    expect(lifeStageForAge(18)).toBe('young_adult')
    expect(lifeStageForAge(29)).toBe('young_adult')
    expect(lifeStageForAge(30)).toBe('middle_adult')
    expect(lifeStageForAge(59)).toBe('middle_adult')
    expect(lifeStageForAge(60)).toBe('late_adult')
    expect(lifeStageForAge(75)).toBe('late_adult')
    expect(lifeStageForAge(76)).toBe('elderly')
    expect(lifeStageForAge(100)).toBe('elderly')
  })

  test('rejects negative age', () => {
    expect(() => lifeStageForAge(-1)).toThrow()
  })
})

describe('tickUnitForAge', () => {
  test('infancy = year', () => {
    expect(tickUnitForAge(1)).toBe('year')
  })

  test('childhood = season', () => {
    expect(tickUnitForAge(5)).toBe('season')
    expect(tickUnitForAge(10)).toBe('season')
  })

  test('adolescence = month', () => {
    expect(tickUnitForAge(15)).toBe('month')
  })

  test('adult life = week', () => {
    expect(tickUnitForAge(25)).toBe('week')
    expect(tickUnitForAge(45)).toBe('week')
    expect(tickUnitForAge(70)).toBe('week')
  })

  test('elderly = month', () => {
    expect(tickUnitForAge(80)).toBe('month')
  })
})

describe('TICK_UNIT_BY_LIFE_STAGE has every stage', () => {
  test('every life stage has a tick unit', () => {
    const stages = [
      'infancy',
      'early_childhood',
      'middle_childhood',
      'adolescence',
      'young_adult',
      'middle_adult',
      'late_adult',
      'elderly'
    ] as const
    for (const s of stages) {
      expect(TICK_UNIT_BY_LIFE_STAGE[s]).toBeDefined()
    }
  })
})