Fix compare chart date alignment

This commit is contained in:
2026-03-13 00:25:20 -04:00
parent a3d4c97f4e
commit 54172f9e8b
2 changed files with 130 additions and 14 deletions

View File

@@ -0,0 +1,82 @@
import { describe, expect, it } from 'bun:test';
import { mergeDataSeries } from './chart-data-transformers';
type PricePoint = {
date: string;
price: number;
};
describe('mergeDataSeries', () => {
it('normalizes intraday timestamps onto the same trading day', () => {
const merged = mergeDataSeries<PricePoint>([
{
id: 'stock',
data: [
{ date: '2026-03-10T21:00:00.000Z', price: 100 },
{ date: '2026-03-11T21:00:00.000Z', price: 101 }
]
},
{
id: 'sp500',
data: [
{ date: '2026-03-10', price: 5000 },
{ date: '2026-03-11', price: 5050 }
]
}
]);
expect(merged).toEqual([
{ date: '2026-03-10', stock: 100, sp500: 5000 },
{ date: '2026-03-11', stock: 101, sp500: 5050 }
]);
});
it('fills a comparison series from the nearest prior trading day when dates are missing', () => {
const merged = mergeDataSeries<PricePoint>([
{
id: 'stock',
data: [
{ date: '2026-03-10', price: 100 },
{ date: '2026-03-11', price: 103 },
{ date: '2026-03-12', price: 104 }
]
},
{
id: 'sp500',
data: [
{ date: '2026-03-10', price: 5000 },
{ date: '2026-03-12', price: 5060 }
]
}
]);
expect(merged).toEqual([
{ date: '2026-03-10', stock: 100, sp500: 5000 },
{ date: '2026-03-11', stock: 103, sp500: 5000 },
{ date: '2026-03-12', stock: 104, sp500: 5060 }
]);
});
it('does not backfill dates before a series first appears', () => {
const merged = mergeDataSeries<PricePoint>([
{
id: 'stock',
data: [
{ date: '2026-03-10', price: 100 },
{ date: '2026-03-11', price: 103 }
]
},
{
id: 'sp500',
data: [
{ date: '2026-03-11', price: 5000 }
]
}
]);
expect(merged).toEqual([
{ date: '2026-03-10', stock: 100 },
{ date: '2026-03-11', stock: 103, sp500: 5000 }
]);
});
});