import { describe, expect, it } from "vitest"; import { filterPeriodsByHistoryWindow, financialHistoryLimit, } from "@/lib/financials/history-window"; import type { FinancialStatementPeriod } from "@/lib/types"; function createPeriod(year: number, month = 12, day = 31) { const suffix = `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`; return { id: suffix, filingId: year, accessionNumber: `0000-${year}`, filingDate: `${year + 1}-02-01`, periodStart: `${year}-01-01`, periodEnd: suffix, filingType: "10-K", periodLabel: `FY ${year}`, } satisfies FinancialStatementPeriod; } describe("financialHistoryLimit", () => { it("scales fetch size for annual and ltm cadences", () => { expect(financialHistoryLimit("annual", 3)).toBe(5); expect(financialHistoryLimit("ltm", 10)).toBe(12); }); it("fetches a buffered number of quarterly periods", () => { expect(financialHistoryLimit("quarterly", 3)).toBe(16); expect(financialHistoryLimit("quarterly", 5)).toBe(24); expect(financialHistoryLimit("quarterly", 10)).toBe(44); }); }); describe("filterPeriodsByHistoryWindow", () => { it("keeps the latest three annual periods", () => { const periods = [ createPeriod(2018), createPeriod(2019), createPeriod(2020), createPeriod(2021), createPeriod(2022), createPeriod(2023), createPeriod(2024), ]; expect( filterPeriodsByHistoryWindow(periods, "annual", 3).map( (period) => period.id, ), ).toEqual(["2022-12-31", "2023-12-31", "2024-12-31"]); }); it("keeps the exact trailing annual count for longer windows", () => { const periods = Array.from({ length: 12 }, (_, index) => createPeriod(2013 + index), ); expect( filterPeriodsByHistoryWindow(periods, "annual", 10).map( (period) => period.id, ), ).toEqual([ "2015-12-31", "2016-12-31", "2017-12-31", "2018-12-31", "2019-12-31", "2020-12-31", "2021-12-31", "2022-12-31", "2023-12-31", "2024-12-31", ]); }); it("keeps the exact trailing quarterly count", () => { const periods = [ createPeriod(2022, 3, 31), createPeriod(2022, 6, 30), createPeriod(2022, 9, 30), createPeriod(2022, 12, 31), createPeriod(2023, 3, 31), createPeriod(2023, 6, 30), createPeriod(2023, 9, 30), createPeriod(2023, 12, 31), createPeriod(2024, 3, 31), createPeriod(2024, 6, 30), createPeriod(2024, 9, 30), createPeriod(2024, 12, 31), createPeriod(2025, 3, 31), ]; expect( filterPeriodsByHistoryWindow(periods, "quarterly", 3).map( (period) => period.id, ), ).toEqual([ "2022-06-30", "2022-09-30", "2022-12-31", "2023-03-31", "2023-06-30", "2023-09-30", "2023-12-31", "2024-03-31", "2024-06-30", "2024-09-30", "2024-12-31", "2025-03-31", ]); }); it("returns all periods when the list is shorter than the requested window", () => { const periods = [createPeriod(2024)]; expect(filterPeriodsByHistoryWindow(periods, "annual", 5)).toEqual(periods); }); });