import { describe, expect, it } from 'bun:test'; import { __financialTaxonomyInternals } from './financial-taxonomy'; import type { FilingTaxonomySnapshotRecord } from './repos/filing-taxonomy'; import type { FinancialStatementKind, TaxonomyStatementRow } from '@/lib/types'; function createRow(periodIds: string[]): TaxonomyStatementRow { return { key: 'us-gaap:RevenueFromContractWithCustomerExcludingAssessedTax', label: 'Revenue From Contract With Customer Excluding Assessed Tax', conceptKey: 'us-gaap:RevenueFromContractWithCustomerExcludingAssessedTax', qname: 'us-gaap:RevenueFromContractWithCustomerExcludingAssessedTax', namespaceUri: 'http://fasb.org/us-gaap/2021-01-31', localName: 'RevenueFromContractWithCustomerExcludingAssessedTax', isExtension: false, statement: 'income', roleUri: 'income', order: 1, depth: 0, parentKey: null, values: Object.fromEntries(periodIds.map((periodId, index) => [periodId, 100 + index])), units: Object.fromEntries(periodIds.map((periodId) => [periodId, 'iso4217:USD'])), hasDimensions: false, sourceFactIds: periodIds.map((_, index) => index + 1) }; } function createSnapshot(input: { filingId: number; filingType: '10-K' | '10-Q'; filingDate: string; periods: Array<{ id: string; periodStart: string | null; periodEnd: string; periodLabel: string; }>; statement: FinancialStatementKind; }) { const row = createRow(input.periods.map((period) => period.id)); return { id: input.filingId, filing_id: input.filingId, ticker: 'MSFT', filing_date: input.filingDate, filing_type: input.filingType, parse_status: 'ready', parse_error: null, source: 'xbrl_instance', periods: input.periods.map((period) => ({ id: period.id, filingId: input.filingId, accessionNumber: `0000-${input.filingId}`, filingDate: input.filingDate, periodStart: period.periodStart, periodEnd: period.periodEnd, filingType: input.filingType, periodLabel: period.periodLabel })), statement_rows: { income: input.statement === 'income' ? [row] : [], balance: input.statement === 'balance' ? [{ ...row, statement: 'balance' }] : [], cash_flow: [], equity: [], comprehensive_income: [] }, derived_metrics: null, validation_result: null, facts_count: 0, concepts_count: 0, dimensions_count: 0, created_at: input.filingDate, updated_at: input.filingDate } satisfies FilingTaxonomySnapshotRecord; } describe('financial taxonomy internals', () => { it('selects the primary quarter duration for 10-Q income statements', () => { const snapshot = createSnapshot({ filingId: 1, filingType: '10-Q', filingDate: '2026-01-28', statement: 'income', periods: [ { id: 'instant', periodStart: null, periodEnd: '2025-12-31', periodLabel: 'Instant' }, { id: 'quarter', periodStart: '2025-10-01', periodEnd: '2025-12-31', periodLabel: '2025-10-01 to 2025-12-31' }, { id: 'ytd', periodStart: '2025-07-01', periodEnd: '2025-12-31', periodLabel: '2025-07-01 to 2025-12-31' } ] }); const selection = __financialTaxonomyInternals.selectPrimaryPeriods([snapshot], 'income'); expect(selection.periods).toHaveLength(1); expect(selection.periods[0]?.id).toBe('quarter'); }); it('selects the latest instant for balance sheets', () => { const snapshot = createSnapshot({ filingId: 2, filingType: '10-K', filingDate: '2025-07-30', statement: 'balance', periods: [ { id: 'prior', periodStart: null, periodEnd: '2024-06-30', periodLabel: 'Instant' }, { id: 'current', periodStart: null, periodEnd: '2025-06-30', periodLabel: 'Instant' } ] }); const selection = __financialTaxonomyInternals.selectPrimaryPeriods([snapshot], 'balance'); expect(selection.periods).toHaveLength(1); expect(selection.periods[0]?.id).toBe('current'); }); it('builds one reporting period per filing for the selected statement', () => { const annual = createSnapshot({ filingId: 10, filingType: '10-K', filingDate: '2025-07-30', statement: 'income', periods: [ { id: 'annual', periodStart: '2024-07-01', periodEnd: '2025-06-30', periodLabel: '2024-07-01 to 2025-06-30' }, { id: 'quarter', periodStart: '2025-04-01', periodEnd: '2025-06-30', periodLabel: '2025-04-01 to 2025-06-30' } ] }); const quarterly = createSnapshot({ filingId: 11, filingType: '10-Q', filingDate: '2025-10-29', statement: 'income', periods: [ { id: 'instant', periodStart: null, periodEnd: '2025-09-30', periodLabel: 'Instant' }, { id: 'quarter', periodStart: '2025-07-01', periodEnd: '2025-09-30', periodLabel: '2025-07-01 to 2025-09-30' }, { id: 'ytd', periodStart: '2025-01-01', periodEnd: '2025-09-30', periodLabel: '2025-01-01 to 2025-09-30' } ] }); const periods = __financialTaxonomyInternals.buildPeriods([annual, quarterly], 'income'); expect(periods.map((period) => period.id)).toEqual(['annual', 'quarter']); }); });