Skip to content

Open source personal finance application

test: add tests for amount utils, date utils, and auth cookie

Ilya Komichev28b39d3618c9
diff --git a/app/components/amount/utils.test.ts b/app/components/amount/utils.test.ts
new file mode 100644
index 00000000..8f983e2f
--- /dev/null
+++ b/app/components/amount/utils.test.ts
@@ -0,0 +1,59 @@
+import { describe, expect, it } from 'vitest'
+
+import { formatAmount, formatByCurrency, getCurrencySymbol } from '~/components/amount/utils'
+
+describe('formatAmount', () => {
+ it('formats integer with default precision 0', () => {
+ expect(formatAmount(1234)).toBe('1 234')
+ })
+
+ it('uses space as thousands separator', () => {
+ expect(formatAmount(1000000)).toBe('1 000 000')
+ })
+
+ it('formats with explicit precision', () => {
+ expect(formatAmount(1234.567, undefined, { precision: 2 })).toBe('1 234.57')
+ })
+
+ it('formats negative amounts', () => {
+ expect(formatAmount(-1500)).toBe('-1 500')
+ })
+
+ it('formats zero', () => {
+ expect(formatAmount(0)).toBe('0')
+ })
+
+ it('formats small decimals', () => {
+ expect(formatAmount(0.99, undefined, { precision: 2 })).toBe('0.99')
+ })
+})
+
+describe('getCurrencySymbol', () => {
+ it('returns symbol for known currency', () => {
+ expect(getCurrencySymbol('USD')).toBe('$')
+ expect(getCurrencySymbol('EUR')).toBe('€')
+ expect(getCurrencySymbol('RUB')).toBe('₽')
+ })
+
+ it('returns code when no symbol defined', () => {
+ expect(getCurrencySymbol('AMD')).toBe('AMD')
+ })
+
+ it('returns empty string for undefined', () => {
+ expect(getCurrencySymbol(undefined)).toBe('')
+ })
+})
+
+describe('formatByCurrency', () => {
+ it('adds thousands separator', () => {
+ expect(formatByCurrency('1000000', ' ')).toBe('1 000 000')
+ })
+
+ it('does not affect small numbers', () => {
+ expect(formatByCurrency('999', ' ')).toBe('999')
+ })
+
+ it('works with different separators', () => {
+ expect(formatByCurrency('1000000', ',')).toBe('1,000,000')
+ })
+})
diff --git a/app/components/amount/utils.ts b/app/components/amount/utils.ts
index 3eb24c0f..00b38d4f 100644
--- a/app/components/amount/utils.ts
+++ b/app/components/amount/utils.ts
@@ -2,28 +2,36 @@ import type { CurrencyCode } from '~/components/currencies/types'
import { currencies } from '~/components/currencies/currencies'
+const currencyMap = new Map(currencies.map(c => [c.code, c]))
+const formatterCache = new Map<number, Intl.NumberFormat>()
+
+function getFormatter(precision: number) {
+ let fmt = formatterCache.get(precision)
+ if (!fmt) {
+ fmt = new Intl.NumberFormat('en-US', {
+ maximumFractionDigits: precision,
+ minimumFractionDigits: precision,
+ })
+ formatterCache.set(precision, fmt)
+ }
+ return fmt
+}
+
export function formatAmount(amount: number, currencyCode?: CurrencyCode, {
precision = 0,
} = {}) {
- const currencySettings = currencyCode
- ? currencies.find(c => c.code === currencyCode) ?? null
- : null
-
+ const currencySettings = currencyCode ? currencyMap.get(currencyCode) : undefined
const p = precision != null ? precision : currencySettings?.precision ?? 2
- return new Intl.NumberFormat('en-US', {
- maximumFractionDigits: p,
- minimumFractionDigits: p,
- }).format(amount).replace(/,/g, ' ')
+ return getFormatter(p).format(amount).replace(/,/g, ' ')
}
export function getCurrencySymbol(currencyCode?: CurrencyCode) {
- const currencySettings = currencies.find(c => c.code === currencyCode)
-
- if (currencySettings?.symbol)
- return currencySettings.symbol
+ if (!currencyCode)
+ return ''
- return currencyCode ?? ''
+ const currencySettings = currencyMap.get(currencyCode)
+ return currencySettings?.symbol ?? currencyCode
}
export function formatByCurrency(value: string, separator: string) {
diff --git a/app/components/date/utils.test.ts b/app/components/date/utils.test.ts
new file mode 100644
index 00000000..74c3e85f
--- /dev/null
+++ b/app/components/date/utils.test.ts
@@ -0,0 +1,93 @@
+import { describe, expect, it } from 'vitest'
+
+import { calculateBestIntervalsBy, getEndOf, getStartOf } from '~/components/date/utils'
+
+describe('calculateBestIntervalsBy', () => {
+ it('returns year for range > 400 days', () => {
+ const range = { end: new Date(2025, 5, 1).getTime(), start: new Date(2024, 0, 1).getTime() }
+ expect(calculateBestIntervalsBy(range)).toBe('year')
+ })
+
+ it('returns month at exactly 401 days', () => {
+ const start = new Date(2024, 0, 1)
+ const end = new Date(start)
+ end.setDate(end.getDate() + 401)
+ expect(calculateBestIntervalsBy({ end: end.getTime(), start: start.getTime() })).toBe('year')
+ })
+
+ it('returns month for range 81–400 days', () => {
+ const start = new Date(2025, 0, 1)
+ const end = new Date(2025, 6, 1) // ~181 days
+ expect(calculateBestIntervalsBy({ end: end.getTime(), start: start.getTime() })).toBe('month')
+ })
+
+ it('returns day for range <= 80 days', () => {
+ const start = new Date(2025, 0, 1)
+ const end = new Date(2025, 1, 15) // ~45 days
+ expect(calculateBestIntervalsBy({ end: end.getTime(), start: start.getTime() })).toBe('day')
+ })
+
+ it('returns month at boundary 400 days', () => {
+ const start = new Date(2024, 0, 1)
+ const end = new Date(start)
+ end.setDate(end.getDate() + 400)
+ expect(calculateBestIntervalsBy({ end: end.getTime(), start: start.getTime() })).toBe('month')
+ })
+
+ it('returns day at boundary 80 days', () => {
+ const start = new Date(2025, 0, 1)
+ const end = new Date(start)
+ end.setDate(end.getDate() + 80)
+ expect(calculateBestIntervalsBy({ end: end.getTime(), start: start.getTime() })).toBe('day')
+ })
+})
+
+describe('getStartOf', () => {
+ const date = new Date(2025, 2, 12, 15, 30) // Wed Mar 12 2025 15:30
+
+ it('year → Jan 1 00:00', () => {
+ expect(getStartOf(date, 'year')).toEqual(new Date(2025, 0, 1))
+ })
+
+ it('month → Mar 1 00:00', () => {
+ expect(getStartOf(date, 'month')).toEqual(new Date(2025, 2, 1))
+ })
+
+ it('week → Monday (weekStartsOn: 1)', () => {
+ expect(getStartOf(date, 'week')).toEqual(new Date(2025, 2, 10))
+ })
+
+ it('day → Mar 12 00:00', () => {
+ expect(getStartOf(date, 'day')).toEqual(new Date(2025, 2, 12))
+ })
+})
+
+describe('getEndOf', () => {
+ const date = new Date(2025, 2, 12, 15, 30) // Wed Mar 12 2025 15:30
+
+ it('year → Dec 31 23:59:59.999', () => {
+ const result = getEndOf(date, 'year')
+ expect(result.getFullYear()).toBe(2025)
+ expect(result.getMonth()).toBe(11)
+ expect(result.getDate()).toBe(31)
+ expect(result.getHours()).toBe(23)
+ })
+
+ it('month → Mar 31 23:59:59.999', () => {
+ const result = getEndOf(date, 'month')
+ expect(result.getDate()).toBe(31)
+ expect(result.getHours()).toBe(23)
+ })
+
+ it('week → Sunday (weekStartsOn: 1)', () => {
+ const result = getEndOf(date, 'week')
+ expect(result.getDay()).toBe(0) // Sunday
+ expect(result.getDate()).toBe(16)
+ })
+
+ it('day → Mar 12 23:59:59.999', () => {
+ const result = getEndOf(date, 'day')
+ expect(result.getDate()).toBe(12)
+ expect(result.getHours()).toBe(23)
+ })
+})
diff --git a/app/composables/useAuthCookie.test.ts b/app/composables/useAuthCookie.test.ts
new file mode 100644
index 00000000..24774244
--- /dev/null
+++ b/app/composables/useAuthCookie.test.ts
@@ -0,0 +1,72 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+describe('useAuthCookie', () => {
+ let cookieJar: string
+
+ beforeEach(() => {
+ cookieJar = ''
+ vi.stubGlobal('document', {
+ get cookie() { return cookieJar },
+ set cookie(val: string) { cookieJar = val },
+ })
+ vi.resetModules()
+ })
+
+ afterEach(() => {
+ vi.unstubAllGlobals()
+ })
+
+ describe('hasAuthCookie', () => {
+ it('returns true when cookie exists', async () => {
+ const { hasAuthCookie } = await import('~/composables/useAuthCookie')
+ cookieJar = 'other=1; finapp.localAuthUid=abc'
+ expect(hasAuthCookie()).toBe(true)
+ })
+
+ it('returns false when cookie absent', async () => {
+ const { hasAuthCookie } = await import('~/composables/useAuthCookie')
+ cookieJar = 'other=1'
+ expect(hasAuthCookie()).toBe(false)
+ })
+ })
+
+ describe('setAuthCookie', () => {
+ it('writes cookie with correct flags', async () => {
+ const { setAuthCookie } = await import('~/composables/useAuthCookie')
+ setAuthCookie('user-123')
+ expect(cookieJar).toBe('finapp.localAuthUid=user-123;path=/;max-age=31536000;SameSite=Lax')
+ })
+
+ it('encodes uid to prevent cookie injection', async () => {
+ const { setAuthCookie } = await import('~/composables/useAuthCookie')
+ setAuthCookie('a=b;c=d')
+ expect(cookieJar).toContain('finapp.localAuthUid=a%3Db%3Bc%3Dd')
+ })
+ })
+
+ describe('clearAuthCookie', () => {
+ it('flags match setAuthCookie with max-age=0', async () => {
+ const { clearAuthCookie } = await import('~/composables/useAuthCookie')
+ clearAuthCookie()
+ expect(cookieJar).toBe('finapp.localAuthUid=;path=/;max-age=0;SameSite=Lax')
+ })
+ })
+
+ describe('Secure flag', () => {
+ it('included on https', async () => {
+ vi.stubGlobal('location', { protocol: 'https:' })
+ vi.resetModules()
+ const { setAuthCookie } = await import('~/composables/useAuthCookie')
+ setAuthCookie('uid')
+ expect(cookieJar).toContain(';Secure')
+ })
+
+ it('omitted on http', async () => {
+ vi.stubGlobal('location', { protocol: 'http:' })
+ vi.resetModules()
+ const { setAuthCookie } = await import('~/composables/useAuthCookie')
+ setAuthCookie('uid')
+ expect(cookieJar).not.toContain('Secure')
+ })
+ })
+})