| 1 | import { describe, it, expect } from 'vitest'
|
| 2 | import { avg, formatHours, formatDiff, paginate, timeToCron } from '@app/utils/calc'
|
| 3 |
|
| 4 | describe('avg', () => {
|
| 5 | it('averages a list of numbers', () => {
|
| 6 | expect(avg([2, 4, 6])).toBe(4)
|
| 7 | })
|
| 8 |
|
| 9 | it('yields NaN for an empty list', () => {
|
| 10 | expect(avg([])).toBeNaN()
|
| 11 | })
|
| 12 | })
|
| 13 |
|
| 14 | describe('formatHours', () => {
|
| 15 | it('drops the minutes for a whole hour', () => {
|
| 16 | expect(formatHours(7)).toBe('7h')
|
| 17 | })
|
| 18 |
|
| 19 | it('renders the remaining minutes', () => {
|
| 20 | expect(formatHours(7.5)).toBe('7h 30min')
|
| 21 | })
|
| 22 |
|
| 23 | it('carries a rounding of sixty minutes into the next hour', () => {
|
| 24 | expect(formatHours(7.999)).toBe('8h')
|
| 25 | })
|
| 26 | })
|
| 27 |
|
| 28 | describe('formatDiff', () => {
|
| 29 | it('signs a positive whole-hour difference', () => {
|
| 30 | expect(formatDiff(120)).toBe('+2h')
|
| 31 | })
|
| 32 |
|
| 33 | it('signs a negative whole-hour difference', () => {
|
| 34 | expect(formatDiff(-120)).toBe('-2h')
|
| 35 | })
|
| 36 |
|
| 37 | it('signs a positive difference with hours and minutes', () => {
|
| 38 | expect(formatDiff(90)).toBe('+1h 30min')
|
| 39 | })
|
| 40 |
|
| 41 | it('signs a negative difference with hours and minutes', () => {
|
| 42 | expect(formatDiff(-90)).toBe('-1h 30min')
|
| 43 | })
|
| 44 |
|
| 45 | it('renders a positive sub-hour difference in minutes only', () => {
|
| 46 | expect(formatDiff(30)).toBe('+30min')
|
| 47 | })
|
| 48 |
|
| 49 | it('renders a negative sub-hour difference in minutes only', () => {
|
| 50 | expect(formatDiff(-30)).toBe('-30min')
|
| 51 | })
|
| 52 | })
|
| 53 |
|
| 54 | describe('paginate', () => {
|
| 55 | it('points the first page at the next one', () => {
|
| 56 | expect(paginate(1, 10, 25)).toEqual({ page: 1, skip: 0, nextPage: 2 })
|
| 57 | })
|
| 58 |
|
| 59 | it('points a middle page at the next one', () => {
|
| 60 | expect(paginate(2, 10, 25)).toEqual({ page: 2, skip: 10, nextPage: 3 })
|
| 61 | })
|
| 62 |
|
| 63 | it('closes the chain on the last page', () => {
|
| 64 | expect(paginate(3, 10, 25)).toEqual({ page: 3, skip: 20, nextPage: null })
|
| 65 | })
|
| 66 |
|
| 67 | it('has no next page past the end', () => {
|
| 68 | expect(paginate(4, 10, 25)).toEqual({ page: 4, skip: 30, nextPage: null })
|
| 69 | })
|
| 70 |
|
| 71 | it('treats an exact division as the last page', () => {
|
| 72 | expect(paginate(2, 10, 20)).toEqual({ page: 2, skip: 10, nextPage: null })
|
| 73 | })
|
| 74 |
|
| 75 | it('has no next page for an empty result set', () => {
|
| 76 | expect(paginate(1, 10, 0)).toEqual({ page: 1, skip: 0, nextPage: null })
|
| 77 | })
|
| 78 | })
|
| 79 |
|
| 80 | describe('timeToCron', () => {
|
| 81 | it('puts the minute before the hour', () => {
|
| 82 | expect(timeToCron('23:59')).toBe('59 23 * * *')
|
| 83 | })
|
| 84 |
|
| 85 | it('drops the leading zero from the hour', () => {
|
| 86 | expect(timeToCron('07:05')).toBe('5 7 * * *')
|
| 87 | })
|
| 88 |
|
| 89 | it('maps midnight to zero hour and minute', () => {
|
| 90 | expect(timeToCron('00:00')).toBe('0 0 * * *')
|
| 91 | })
|
| 92 |
|
| 93 | it('maps midday to the twelfth hour', () => {
|
| 94 | expect(timeToCron('12:00')).toBe('0 12 * * *')
|
| 95 | })
|
| 96 | })
|