Skip to content

Open source personal finance application

test: add unit tests for pure utility functions and validators

Ilya Komichev8cfe2c1dd222
diff --git a/app/components/date/statDateParams.test.ts b/app/components/date/statDateParams.test.ts
new file mode 100644
index 00000000..09110ce7
--- /dev/null
+++ b/app/components/date/statDateParams.test.ts
@@ -0,0 +1,204 @@
+import { describe, expect, it } from 'vitest'
+
+import type { Range, StatDateParams } from '~/components/date/types'
+
+import { computeDateRange, defaultStatDateParams, parseStatDateQueryParams } from './statDateParams'
+
+// --- parseStatDateQueryParams ---
+
+describe('parseStatDateQueryParams', () => {
+ const base: StatDateParams = { ...defaultStatDateParams }
+
+ it('returns current params on invalid query', () => {
+ const result = parseStatDateQueryParams({ rangeBy: 'invalid' as any }, base)
+ expect(result).toEqual(base)
+ })
+
+ it('merges valid rangeBy and rangeDuration', () => {
+ const result = parseStatDateQueryParams(
+ { rangeBy: 'month', rangeDuration: '3' },
+ base,
+ )
+ expect(result.rangeBy).toBe('month')
+ expect(result.rangeDuration).toBe(3)
+ expect(result.intervalsBy).toBe(base.intervalsBy)
+ })
+
+ it('merges intervalsBy and intervalsDuration', () => {
+ const result = parseStatDateQueryParams(
+ { intervalsBy: 'week', intervalsDuration: '2' },
+ base,
+ )
+ expect(result.intervalsBy).toBe('week')
+ expect(result.intervalsDuration).toBe(2)
+ })
+
+ it('merges intervalSelected', () => {
+ const result = parseStatDateQueryParams(
+ { intervalSelected: '5' },
+ base,
+ )
+ expect(result.intervalSelected).toBe(5)
+ })
+
+ it('merges rangeOffset', () => {
+ const result = parseStatDateQueryParams(
+ { rangeOffset: '-2' },
+ base,
+ )
+ expect(result.rangeOffset).toBe(-2)
+ })
+
+ it('merges boolean isShowMaxRange', () => {
+ const result = parseStatDateQueryParams(
+ { isShowMaxRange: 'true' },
+ base,
+ )
+ expect(result.isShowMaxRange).toBe(true)
+ })
+
+ it('merges boolean isSkipEmpty', () => {
+ const result = parseStatDateQueryParams(
+ { isSkipEmpty: 'true' },
+ base,
+ )
+ expect(result.isSkipEmpty).toBe(true)
+ })
+
+ it('does not mutate input params', () => {
+ const original = { ...base }
+ parseStatDateQueryParams({ rangeBy: 'year' }, base)
+ expect(base).toEqual(original)
+ })
+
+ it('ignores non-integer duration strings', () => {
+ const result = parseStatDateQueryParams(
+ { rangeDuration: '3.5' },
+ base,
+ )
+ expect(result).toEqual(base)
+ })
+
+ it('skips zero values for numeric fields', () => {
+ const result = parseStatDateQueryParams(
+ { rangeDuration: '0' },
+ base,
+ )
+ // 0 is falsy, so the if-check skips it
+ expect(result.rangeDuration).toBe(base.rangeDuration)
+ })
+})
+
+// --- computeDateRange ---
+
+describe('computeDateRange', () => {
+ const now = new Date('2024-06-15T12:00:00').getTime()
+ const maxRange: Range = {
+ end: new Date('2024-06-15').getTime(),
+ start: new Date('2024-01-01').getTime(),
+ }
+
+ it('returns customDate when set', () => {
+ const customDate: Range = {
+ end: new Date('2024-03-31').getTime(),
+ start: new Date('2024-03-01').getTime(),
+ }
+ const params: StatDateParams = {
+ ...defaultStatDateParams,
+ customDate,
+ }
+
+ const result = computeDateRange(params, maxRange, now)
+ expect(result).toEqual(customDate)
+ })
+
+ it('returns maxRange when isShowMaxRange + isSkipEmpty', () => {
+ const params: StatDateParams = {
+ ...defaultStatDateParams,
+ isShowMaxRange: true,
+ isSkipEmpty: true,
+ }
+
+ const result = computeDateRange(params, maxRange, now)
+ expect(result.start).toBe(maxRange.start)
+ expect(result.end).toBe(maxRange.end)
+ })
+
+ it('returns maxRange.start to end-of-period when isShowMaxRange only', () => {
+ const params: StatDateParams = {
+ ...defaultStatDateParams,
+ isShowMaxRange: true,
+ isSkipEmpty: false,
+ rangeBy: 'month',
+ }
+
+ const result = computeDateRange(params, maxRange, now)
+ expect(result.start).toBe(maxRange.start)
+ // end should be end of current month, not maxRange.end
+ expect(result.end).toBeGreaterThanOrEqual(now)
+ })
+
+ it('calculates range from offset when no custom/max flags', () => {
+ const params: StatDateParams = {
+ ...defaultStatDateParams,
+ customDate: false,
+ isShowMaxRange: false,
+ rangeBy: 'day',
+ rangeDuration: 7,
+ rangeOffset: 0,
+ }
+
+ const result = computeDateRange(params, maxRange, now)
+ expect(result.start).toBeDefined()
+ expect(result.end).toBeDefined()
+ expect(result.end).toBeGreaterThanOrEqual(result.start)
+ })
+
+ it('applies rangeOffset for navigation', () => {
+ const params0: StatDateParams = {
+ ...defaultStatDateParams,
+ rangeBy: 'month',
+ rangeDuration: 1,
+ rangeOffset: 0,
+ }
+ const params1: StatDateParams = {
+ ...params0,
+ rangeOffset: 1,
+ }
+
+ const range0 = computeDateRange(params0, maxRange, now)
+ const range1 = computeDateRange(params1, maxRange, now)
+
+ // positive offset subtracts time (goes back)
+ expect(range1.start).toBeLessThan(range0.start)
+ expect(range1.end).toBeLessThan(range0.end)
+ })
+
+ it('priority: customDate overrides isShowMaxRange', () => {
+ const customDate: Range = {
+ end: new Date('2024-02-28').getTime(),
+ start: new Date('2024-02-01').getTime(),
+ }
+ const params: StatDateParams = {
+ ...defaultStatDateParams,
+ customDate,
+ isShowMaxRange: true,
+ isSkipEmpty: true,
+ }
+
+ const result = computeDateRange(params, maxRange, now)
+ expect(result).toEqual(customDate)
+ })
+})
+
+// --- defaultStatDateParams ---
+
+describe('defaultStatDateParams', () => {
+ it('has expected defaults', () => {
+ expect(defaultStatDateParams.customDate).toBe(false)
+ expect(defaultStatDateParams.rangeBy).toBe('day')
+ expect(defaultStatDateParams.rangeDuration).toBe(14)
+ expect(defaultStatDateParams.intervalSelected).toBe(-1)
+ expect(defaultStatDateParams.rangeOffset).toBe(0)
+ })
+})
diff --git a/app/components/offline/replayHelpers.test.ts b/app/components/offline/replayHelpers.test.ts
new file mode 100644
index 00000000..7da7cd12
--- /dev/null
+++ b/app/components/offline/replayHelpers.test.ts
@@ -0,0 +1,231 @@
+import { describe, expect, it } from 'vitest'
+
+import type { OfflineOp } from '~/components/offline/types'
+import type { TrnItem } from '~/components/trns/types'
+
+import { TrnType } from '~/components/trns/types'
+
+import { extractRemaps, groupOpsByEntity, isTrnOrphaned, remapTrnIds, splitCategoryOps } from './replayHelpers'
+
+function makeOp(entity: OfflineOp['entity'], id: string, type: OfflineOp['type'] = 'create', data?: Record<string, unknown>): OfflineOp {
+ return { data, entity, id, timestamp: Date.now(), type }
+}
+
+// --- remapTrnIds ---
+
+describe('remapTrnIds', () => {
+ const remap = new Map([['local_w1', 'convex_w1'], ['local_c1', 'convex_c1']])
+
+ it('remaps walletId and categoryId for transaction', () => {
+ const trn = {
+ amount: 100,
+ categoryId: 'local_c1',
+ date: 1000,
+ type: TrnType.Expense,
+ updatedAt: 1000,
+ walletId: 'local_w1',
+ } as TrnItem
+
+ const result = remapTrnIds(trn, remap)
+ expect(result.walletId).toBe('convex_w1')
+ expect(result.categoryId).toBe('convex_c1')
+ })
+
+ it('remaps expenseWalletId and incomeWalletId for transfer', () => {
+ const remap2 = new Map([['local_w1', 'convex_w1'], ['local_w2', 'convex_w2']])
+ const trn = {
+ categoryId: 'transfer',
+ date: 1000,
+ expenseAmount: 100,
+ expenseWalletId: 'local_w1',
+ incomeAmount: 100,
+ incomeWalletId: 'local_w2',
+ type: TrnType.Transfer,
+ updatedAt: 1000,
+ } as TrnItem
+
+ const result = remapTrnIds(trn, remap2)
+ expect(result.expenseWalletId).toBe('convex_w1')
+ expect(result.incomeWalletId).toBe('convex_w2')
+ })
+
+ it('keeps original IDs when no remap exists', () => {
+ const trn = {
+ amount: 50,
+ categoryId: 'food',
+ date: 1000,
+ type: TrnType.Income,
+ updatedAt: 1000,
+ walletId: 'real_w1',
+ } as TrnItem
+
+ const result = remapTrnIds(trn, remap)
+ expect(result.walletId).toBe('real_w1')
+ expect(result.categoryId).toBe('food')
+ })
+
+ it('does not mutate original', () => {
+ const trn = {
+ amount: 100,
+ categoryId: 'local_c1',
+ date: 1000,
+ type: TrnType.Expense,
+ updatedAt: 1000,
+ walletId: 'local_w1',
+ } as TrnItem
+
+ remapTrnIds(trn, remap)
+ expect(trn.walletId).toBe('local_w1')
+ })
+})
+
+// --- isTrnOrphaned ---
+
+describe('isTrnOrphaned', () => {
+ const wallets = { w1: {}, w2: {} }
+ const categories = { food: {}, transport: {} }
+
+ it('returns false for valid transaction', () => {
+ const trn = { categoryId: 'food', type: TrnType.Expense, walletId: 'w1' } as TrnItem
+ expect(isTrnOrphaned(trn, wallets, categories)).toBe(false)
+ })
+
+ it('returns true for missing wallet', () => {
+ const trn = { categoryId: 'food', type: TrnType.Expense, walletId: 'missing' } as TrnItem
+ expect(isTrnOrphaned(trn, wallets, categories)).toBe(true)
+ })
+
+ it('returns true for missing category', () => {
+ const trn = { categoryId: 'missing', type: TrnType.Expense, walletId: 'w1' } as TrnItem
+ expect(isTrnOrphaned(trn, wallets, categories)).toBe(true)
+ })
+
+ it('returns false for valid transfer', () => {
+ const trn = {
+ expenseWalletId: 'w1',
+ incomeWalletId: 'w2',
+ type: TrnType.Transfer,
+ } as TrnItem
+ expect(isTrnOrphaned(trn, wallets, categories)).toBe(false)
+ })
+
+ it('returns true for transfer with missing wallet', () => {
+ const trn = {
+ expenseWalletId: 'w1',
+ incomeWalletId: 'missing',
+ type: TrnType.Transfer,
+ } as TrnItem
+ expect(isTrnOrphaned(trn, wallets, categories)).toBe(true)
+ })
+})
+
+// --- groupOpsByEntity ---
+
+describe('groupOpsByEntity', () => {
+ it('groups operations by entity type', () => {
+ const ops = [
+ makeOp('wallets', 'w1'),
+ makeOp('trns', 't1'),
+ makeOp('categories', 'c1'),
+ makeOp('wallets', 'w2'),
+ makeOp('userSettings', 's1'),
+ makeOp('trns', 't2'),
+ ]
+
+ const result = groupOpsByEntity(ops)
+ expect(result.walletOps).toHaveLength(2)
+ expect(result.categoryOps).toHaveLength(1)
+ expect(result.trnOps).toHaveLength(2)
+ expect(result.settingsOps).toHaveLength(1)
+ })
+
+ it('returns empty arrays for empty input', () => {
+ const result = groupOpsByEntity([])
+ expect(result.walletOps).toEqual([])
+ expect(result.categoryOps).toEqual([])
+ expect(result.trnOps).toEqual([])
+ expect(result.settingsOps).toEqual([])
+ })
+
+ it('preserves op references', () => {
+ const op = makeOp('trns', 't1')
+ const result = groupOpsByEntity([op])
+ expect(result.trnOps[0]).toBe(op)
+ })
+})
+
+// --- splitCategoryOps ---
+
+describe('splitCategoryOps', () => {
+ it('splits parent and child categories', () => {
+ const ops = [
+ makeOp('categories', 'c1', 'create', { name: 'Food', parentId: 0 }),
+ makeOp('categories', 'c2', 'create', { name: 'Groceries', parentId: 'c1' }),
+ makeOp('categories', 'c3', 'create', { name: 'Transport', parentId: 0 }),
+ ]
+
+ const result = splitCategoryOps(ops)
+ expect(result.parentOps).toHaveLength(2)
+ expect(result.childOps).toHaveLength(1)
+ expect(result.childOps[0].id).toBe('c2')
+ })
+
+ it('puts delete ops in parentOps', () => {
+ const ops = [
+ makeOp('categories', 'c1', 'delete'),
+ makeOp('categories', 'c2', 'create', { name: 'Child', parentId: 'c1' }),
+ ]
+
+ const result = splitCategoryOps(ops)
+ expect(result.parentOps).toHaveLength(1)
+ expect(result.parentOps[0].type).toBe('delete')
+ expect(result.childOps).toHaveLength(1)
+ })
+
+ it('treats no parentId as root', () => {
+ const ops = [
+ makeOp('categories', 'c1', 'create', { name: 'Root' }),
+ ]
+
+ const result = splitCategoryOps(ops)
+ expect(result.parentOps).toHaveLength(1)
+ expect(result.childOps).toHaveLength(0)
+ })
+
+ it('returns empty arrays for empty input', () => {
+ const result = splitCategoryOps([])
+ expect(result.parentOps).toEqual([])
+ expect(result.childOps).toEqual([])
+ })
+})
+
+// --- extractRemaps ---
+
+describe('extractRemaps', () => {
+ it('extracts localId/convexId pairs', () => {
+ const results = [
+ { convexId: 'convex_1', localId: 'local_1' },
+ { convexId: 'convex_2', localId: 'local_2' },
+ ]
+ const remaps = extractRemaps(results)
+ expect(remaps).toHaveLength(2)
+ expect(remaps[0]).toEqual({ convexId: 'convex_1', localId: 'local_1' })
+ })
+
+ it('skips non-remap results', () => {
+ const results = [
+ undefined,
+ null,
+ 'string',
+ 42,
+ { other: 'data' },
+ { convexId: 'c1', localId: 'l1' },
+ ]
+ const remaps = extractRemaps(results)
+ expect(remaps).toHaveLength(1)
+ })
+
+ it('returns empty for empty results', () => {
+ expect(extractRemaps([])).toEqual([])
+ })
+})
diff --git a/app/components/stat/chart/utils.test.ts b/app/components/stat/chart/utils.test.ts
new file mode 100644
index 00000000..6d5b2032
--- /dev/null
+++ b/app/components/stat/chart/utils.test.ts
@@ -0,0 +1,57 @@
+import { describe, expect, it } from 'vitest'
+
+import { getCompactAmount, getLocalAmount, markArea } from './utils'
+
+describe('stat chart utils', () => {
+ describe('markArea', () => {
+ it('returns echarts markArea config for given value', () => {
+ const result = markArea(3)
+ expect(result.data).toEqual([[{ xAxis: '3' }, { xAxis: '3' }]])
+ expect(result.itemStyle.color).toBe('var(--chart-line)')
+ expect(result.itemStyle.opacity).toBe(1)
+ })
+
+ it('converts numeric value to string for xAxis', () => {
+ expect(markArea(0).data[0][0].xAxis).toBe('0')
+ expect(markArea(-1).data[0][0].xAxis).toBe('-1')
+ })
+ })
+
+ describe('getCompactAmount', () => {
+ it('formats small numbers as-is', () => {
+ expect(getCompactAmount(0)).toBe('0')
+ expect(getCompactAmount(100)).toBe('100')
+ expect(getCompactAmount(999)).toBe('999')
+ })
+
+ it('formats thousands with K', () => {
+ expect(getCompactAmount(1000)).toBe('1K')
+ expect(getCompactAmount(1500)).toBe('1.5K')
+ expect(getCompactAmount(10000)).toBe('10K')
+ })
+
+ it('formats millions with M', () => {
+ expect(getCompactAmount(1000000)).toBe('1M')
+ expect(getCompactAmount(2500000)).toBe('2.5M')
+ })
+
+ it('formats billions with B', () => {
+ expect(getCompactAmount(1000000000)).toBe('1B')
+ })
+ })
+
+ describe('getLocalAmount', () => {
+ it('formats with Russian locale (space as separator)', () => {
+ const result = getLocalAmount(1000000)
+ // Russian locale uses non-breaking space as thousand separator
+ expect(result.replace(/\s/g, ' ')).toBe('1 000 000')
+ })
+
+ it('formats without decimal places', () => {
+ expect(getLocalAmount(42)).toBe('42')
+ // Should round, not show decimals
+ const result = getLocalAmount(1234.56)
+ expect(result.replace(/\s/g, ' ')).toBe('1 235')
+ })
+ })
+})
diff --git a/app/components/stat/statConfig.test.ts b/app/components/stat/statConfig.test.ts
new file mode 100644
index 00000000..2c0bdb2e
--- /dev/null
+++ b/app/components/stat/statConfig.test.ts
@@ -0,0 +1,135 @@
+import { describe, expect, it } from 'vitest'
+
+import type { MiniItemConfig } from '~/components/stat/useStatConfig'
+
+import { ConfigSchema } from '~/components/stat/useStatConfig'
+
+import { mergeConfigUpdate } from './statConfig'
+
+const defaultConfig: MiniItemConfig = {
+ catsList: {
+ isGrouped: true,
+ isItemsBg: false,
+ isLines: true,
+ isRoundIcon: true,
+ isShow: true,
+ },
+ catsRound: {
+ isGrouped: false,
+ isShow: true,
+ },
+ catsView: 'list',
+ chart: {
+ isShowAverage: false,
+ },
+ chartShow: true,
+ chartType: 'bar',
+ chartView: 'full',
+ date: {
+ isShowQuick: false,
+ },
+ isShowEmptyCategories: false,
+ statAverage: {
+ count: 10,
+ isShow: false,
+ },
+ vertical: {
+ isGrouped: false,
+ isShow: false,
+ },
+ wallets: {
+ count: 6,
+ isShow: false,
+ },
+}
+
+describe('configSchema', () => {
+ it('validates default config', () => {
+ expect(ConfigSchema.safeParse(defaultConfig).success).toBe(true)
+ })
+
+ it('rejects invalid chartType', () => {
+ const invalid = { ...defaultConfig, chartType: 'invalid' }
+ expect(ConfigSchema.safeParse(invalid).success).toBe(false)
+ })
+
+ it('rejects invalid catsView', () => {
+ const invalid = { ...defaultConfig, catsView: 'grid' }
+ expect(ConfigSchema.safeParse(invalid).success).toBe(false)
+ })
+
+ it('rejects missing required field', () => {
+ const { chartShow: _, ...incomplete } = defaultConfig
+ expect(ConfigSchema.safeParse(incomplete).success).toBe(false)
+ })
+
+ it('rejects non-boolean in nested object', () => {
+ const invalid = { ...defaultConfig, catsList: { ...defaultConfig.catsList, isShow: 'yes' } }
+ expect(ConfigSchema.safeParse(invalid).success).toBe(false)
+ })
+
+ it('rejects non-number wallets.count', () => {
+ const invalid = { ...defaultConfig, wallets: { ...defaultConfig.wallets, count: 'six' } }
+ expect(ConfigSchema.safeParse(invalid).success).toBe(false)
+ })
+})
+
+describe('mergeConfigUpdate', () => {
+ it('updates a primitive value', () => {
+ const result = mergeConfigUpdate(defaultConfig, 'chartShow', false as any)
+ expect(result).not.toBeNull()
+ expect(result!.chartShow).toBe(false)
+ })
+
+ it('updates chartType', () => {
+ const result = mergeConfigUpdate(defaultConfig, 'chartType', 'line' as any)
+ expect(result).not.toBeNull()
+ expect(result!.chartType).toBe('line')
+ })
+
+ it('updates chartView', () => {
+ const result = mergeConfigUpdate(defaultConfig, 'chartView', 'half' as any)
+ expect(result).not.toBeNull()
+ expect(result!.chartView).toBe('half')
+ })
+
+ it('deep-merges nested object', () => {
+ const result = mergeConfigUpdate(defaultConfig, 'catsList', { isGrouped: false })
+ expect(result).not.toBeNull()
+ expect(result!.catsList.isGrouped).toBe(false)
+ // Other fields preserved via defu
+ expect(result!.catsList.isShow).toBe(true)
+ expect(result!.catsList.isLines).toBe(true)
+ })
+
+ it('deep-merges wallets count', () => {
+ const result = mergeConfigUpdate(defaultConfig, 'wallets', { count: 12 })
+ expect(result).not.toBeNull()
+ expect(result!.wallets.count).toBe(12)
+ expect(result!.wallets.isShow).toBe(false) // preserved
+ })
+
+ it('returns null for invalid value', () => {
+ const result = mergeConfigUpdate(defaultConfig, 'chartType', 'invalid' as any)
+ expect(result).toBeNull()
+ })
+
+ it('returns null for invalid nested value', () => {
+ const result = mergeConfigUpdate(defaultConfig, 'wallets', { count: 'bad' } as any)
+ expect(result).toBeNull()
+ })
+
+ it('does not mutate original config', () => {
+ const original = { ...defaultConfig, chartShow: true }
+ mergeConfigUpdate(original, 'chartShow', false as any)
+ expect(original.chartShow).toBe(true)
+ })
+
+ it('preserves other keys when updating one', () => {
+ const result = mergeConfigUpdate(defaultConfig, 'isShowEmptyCategories', true as any)
+ expect(result).not.toBeNull()
+ expect(result!.isShowEmptyCategories).toBe(true)
+ expect(result!.chartType).toBe('bar')
+ expect(result!.catsList).toEqual(defaultConfig.catsList)
+ })
+})
diff --git a/convex/shared.test.ts b/convex/shared.test.ts
new file mode 100644
index 00000000..de6c420c
--- /dev/null
+++ b/convex/shared.test.ts
@@ -0,0 +1,72 @@
+import { describe, expect, it } from 'vitest'
+
+import { AMOUNT_MAX, CREDIT_LIMIT_MAX, DATE_MAX, DATE_MIN, validateNumberRange, validateStringLength } from './shared'
+
+describe('convex/shared validators', () => {
+ describe('validateStringLength', () => {
+ it('does nothing for undefined', () => {
+ expect(() => validateStringLength(undefined, 10, 'Test')).not.toThrow()
+ })
+
+ it('does nothing for string within limit', () => {
+ expect(() => validateStringLength('hello', 10, 'Test')).not.toThrow()
+ })
+
+ it('does nothing for string at exact limit', () => {
+ expect(() => validateStringLength('a'.repeat(100), 100, 'Name')).not.toThrow()
+ })
+
+ it('throws for string exceeding limit', () => {
+ expect(() => validateStringLength('a'.repeat(101), 100, 'Name'))
+ .toThrow('Name must be at most 100 characters')
+ })
+
+ it('throws with correct field name', () => {
+ expect(() => validateStringLength('too long', 5, 'Description'))
+ .toThrow('Description must be at most 5 characters')
+ })
+ })
+
+ describe('validateNumberRange', () => {
+ it('does nothing for undefined', () => {
+ expect(() => validateNumberRange(undefined, 0, 100, 'Amount')).not.toThrow()
+ })
+
+ it('does nothing for value within range', () => {
+ expect(() => validateNumberRange(50, 0, 100, 'Amount')).not.toThrow()
+ })
+
+ it('does nothing for value at boundaries', () => {
+ expect(() => validateNumberRange(0, 0, 100, 'Amount')).not.toThrow()
+ expect(() => validateNumberRange(100, 0, 100, 'Amount')).not.toThrow()
+ })
+
+ it('throws for value below min', () => {
+ expect(() => validateNumberRange(-1, 0, 100, 'Amount'))
+ .toThrow('Amount must be between 0 and 100')
+ })
+
+ it('throws for value above max', () => {
+ expect(() => validateNumberRange(101, 0, 100, 'Amount'))
+ .toThrow('Amount must be between 0 and 100')
+ })
+ })
+
+ describe('constants', () => {
+ it('DATE_MIN is 2000-01-01', () => {
+ expect(new Date(DATE_MIN).toISOString()).toBe('2000-01-01T00:00:00.000Z')
+ })
+
+ it('DATE_MAX is 2100-01-01', () => {
+ expect(new Date(DATE_MAX).toISOString()).toBe('2100-01-01T00:00:00.000Z')
+ })
+
+ it('AMOUNT_MAX is 1 trillion', () => {
+ expect(AMOUNT_MAX).toBe(1_000_000_000_000)
+ })
+
+ it('CREDIT_LIMIT_MAX is 1 billion', () => {
+ expect(CREDIT_LIMIT_MAX).toBe(1_000_000_000)
+ })
+ })
+})
diff --git a/convex/validators.test.ts b/convex/validators.test.ts
new file mode 100644
index 00000000..702dc839
--- /dev/null
+++ b/convex/validators.test.ts
@@ -0,0 +1,29 @@
+import { describe, expect, it } from 'vitest'
+
+import { TrnType, walletTypes } from './validators'
+
+describe('convex/validators', () => {
+ describe('walletTypes', () => {
+ it('contains all expected wallet types', () => {
+ expect(walletTypes).toEqual(['cash', 'credit', 'cashless', 'deposit', 'crypto', 'debt'])
+ })
+
+ it('is readonly', () => {
+ expect(Object.isFrozen(walletTypes)).toBe(true)
+ })
+ })
+
+ describe('TrnType enum', () => {
+ it('has correct numeric values', () => {
+ expect(TrnType.Expense).toBe(0)
+ expect(TrnType.Income).toBe(1)
+ expect(TrnType.Transfer).toBe(2)
+ })
+
+ it('has reverse mapping', () => {
+ expect(TrnType[0]).toBe('Expense')
+ expect(TrnType[1]).toBe('Income')
+ expect(TrnType[2]).toBe('Transfer')
+ })
+ })
+})
diff --git a/i18n/ruPluralization.test.ts b/i18n/ruPluralization.test.ts
new file mode 100644
index 00000000..ef78a07f
--- /dev/null
+++ b/i18n/ruPluralization.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from 'vitest'
+
+import { ruPluralization } from './ruPluralization'
+
+describe('ruPluralization', () => {
+ it('returns 0 for choice === 0', () => {
+ expect(ruPluralization(0, 4)).toBe(0)
+ })
+
+ it('returns 1 for singular (ends with 1, not teen)', () => {
+ expect(ruPluralization(1, 4)).toBe(1)
+ expect(ruPluralization(21, 4)).toBe(1)
+ expect(ruPluralization(31, 4)).toBe(1)
+ expect(ruPluralization(101, 4)).toBe(1)
+ })
+
+ it('returns 2 for few (ends with 2-4, not teen)', () => {
+ expect(ruPluralization(2, 4)).toBe(2)
+ expect(ruPluralization(3, 4)).toBe(2)
+ expect(ruPluralization(4, 4)).toBe(2)
+ expect(ruPluralization(22, 4)).toBe(2)
+ expect(ruPluralization(33, 4)).toBe(2)
+ expect(ruPluralization(104, 4)).toBe(2)
+ })
+
+ it('returns 3 for many (teens and ends with 5-9, 0)', () => {
+ expect(ruPluralization(5, 4)).toBe(3)
+ expect(ruPluralization(10, 4)).toBe(3)
+ expect(ruPluralization(11, 4)).toBe(3)
+ expect(ruPluralization(12, 4)).toBe(3)
+ expect(ruPluralization(14, 4)).toBe(3)
+ expect(ruPluralization(15, 4)).toBe(3)
+ expect(ruPluralization(19, 4)).toBe(3)
+ expect(ruPluralization(25, 4)).toBe(3)
+ expect(ruPluralization(100, 4)).toBe(3)
+ })
+
+ it('falls back to 2 when choicesLength < 4', () => {
+ expect(ruPluralization(5, 3)).toBe(2)
+ expect(ruPluralization(11, 3)).toBe(2)
+ expect(ruPluralization(100, 3)).toBe(2)
+ })
+})