| 1 | import { describe, it, expect } from 'vitest'
|
| 2 | import { Insight, MedicineRegimen, SleepRecord, User } from '@prisma/client'
|
| 3 | import {
|
| 4 | insightTable,
|
| 5 | medicineRegimenTable,
|
| 6 | profileTable,
|
| 7 | sleepRecordTable,
|
| 8 | } from '@app/services/export/table.descriptors'
|
| 9 | import { CsvTable } from '@app/services/export/types'
|
| 10 |
|
| 11 | const columnValue = <Row>(table: CsvTable<Row>, header: string) => {
|
| 12 | const column = table.columns.find((column) => column.header === header)
|
| 13 |
|
| 14 | if (!column) throw new Error(`a tabela não tem a coluna ${header}`)
|
| 15 |
|
| 16 | return column.value
|
| 17 | }
|
| 18 |
|
| 19 | describe('boolean descriptor values', () => {
|
| 20 | it('renders true as Sim and false as Não', () => {
|
| 21 | const user = { active: true, pushEnabled: false } as User
|
| 22 |
|
| 23 | expect(columnValue(profileTable, 'Ativo')(user)).toBe('Sim')
|
| 24 | expect(columnValue(profileTable, 'Notificações por push')(user)).toBe('Não')
|
| 25 | })
|
| 26 |
|
| 27 | it('renders a regimen active flag the same way', () => {
|
| 28 | const regimen = { active: false } as MedicineRegimen
|
| 29 |
|
| 30 | expect(columnValue(medicineRegimenTable, 'Ativo')(regimen)).toBe('Não')
|
| 31 | })
|
| 32 | })
|
| 33 |
|
| 34 | describe('json descriptor values', () => {
|
| 35 | it('serializes metadata to a JSON string', () => {
|
| 36 | const insight = { metadata: { delta: -1, avgFirst: 5 } } as unknown as Insight
|
| 37 |
|
| 38 | expect(columnValue(insightTable, 'Metadados')(insight)).toBe(
|
| 39 | '{"delta":-1,"avgFirst":5}',
|
| 40 | )
|
| 41 | })
|
| 42 |
|
| 43 | it('keeps null metadata empty instead of the string "null"', () => {
|
| 44 | const insight = { metadata: null } as Insight
|
| 45 |
|
| 46 | expect(columnValue(insightTable, 'Metadados')(insight)).toBeNull()
|
| 47 | })
|
| 48 | })
|
| 49 |
|
| 50 | describe('date-only descriptor values', () => {
|
| 51 | it('drops the time from a sleep record date', () => {
|
| 52 | const record = { date: new Date('2026-10-17T15:45:30.000Z') } as SleepRecord
|
| 53 |
|
| 54 | expect(columnValue(sleepRecordTable, 'Data')(record)).toBe('2026-10-17')
|
| 55 | })
|
| 56 | })
|