import { describe, expect, it } from 'bun:test'; import type { Filing } from '@/lib/types'; import { __taskProcessorInternals } from './task-processors'; function sampleFiling(): Filing { return { id: 1, ticker: 'AAPL', filing_type: '10-Q', filing_date: '2026-01-30', accession_number: '0000320193-26-000001', cik: '0000320193', company_name: 'Apple Inc.', filing_url: 'https://www.sec.gov/Archives/edgar/data/320193/000032019326000001/a10q.htm', submission_url: 'https://data.sec.gov/submissions/CIK0000320193.json', primary_document: 'a10q.htm', metrics: { revenue: 120_000_000_000, netIncome: 25_000_000_000, totalAssets: 410_000_000_000, cash: 70_000_000_000, debt: 98_000_000_000 }, analysis: null, created_at: '2026-01-30T00:00:00.000Z', updated_at: '2026-01-30T00:00:00.000Z' }; } describe('task processor extraction helpers', () => { it('parses strict extraction payloads', () => { const raw = JSON.stringify({ summary: 'Revenue growth remained resilient despite FX pressure.', keyPoints: ['Revenue up year-over-year'], redFlags: ['Debt service burden is rising'], followUpQuestions: ['Is margin guidance sustainable?'], portfolioSignals: ['Monitor leverage trend'], segmentSpecificData: ['Services segment outgrew hardware segment.'], geographicRevenueBreakdown: ['EMEA revenue grew faster than Americas.'], companySpecificData: ['Same-store sales increased 4.2%.'], secApiCrossChecks: ['Revenue from SEC API aligns with filing narrative.'], confidence: 0.72 }); const parsed = __taskProcessorInternals.parseExtractionPayload(raw); expect(parsed).not.toBeNull(); expect(parsed?.summary).toContain('Revenue growth'); expect(parsed?.confidence).toBe(0.72); }); it('rejects extraction payloads with extra keys', () => { const raw = JSON.stringify({ summary: 'ok', keyPoints: [], redFlags: [], followUpQuestions: [], portfolioSignals: [], segmentSpecificData: [], geographicRevenueBreakdown: [], companySpecificData: [], secApiCrossChecks: [], confidence: 0.2, extra: 'not-allowed' }); const parsed = __taskProcessorInternals.parseExtractionPayload(raw); expect(parsed).toBeNull(); }); it('builds deterministic extraction fallback from filing metadata', () => { const fallback = __taskProcessorInternals.deterministicExtractionFallback(sampleFiling()); expect(fallback.summary).toContain('Deterministic extraction fallback'); expect(fallback.keyPoints.length).toBeGreaterThan(0); expect(fallback.redFlags.length).toBeGreaterThan(0); expect(fallback.segmentSpecificData.length).toBeGreaterThan(0); expect(fallback.geographicRevenueBreakdown.length).toBeGreaterThan(0); expect(fallback.companySpecificData.length).toBeGreaterThan(0); expect(fallback.secApiCrossChecks.length).toBeGreaterThan(0); expect(fallback.confidence).toBe(0.2); }); it('treats only 10-K and 10-Q as financial metric filings', () => { expect(__taskProcessorInternals.isFinancialMetricsForm('10-K')).toBe(true); expect(__taskProcessorInternals.isFinancialMetricsForm('10-Q')).toBe(true); expect(__taskProcessorInternals.isFinancialMetricsForm('8-K')).toBe(false); }); });