mirror of
https://github.com/tiennm99/ccs.git
synced 2026-09-06 04:16:27 +00:00
fix(dashboard): respect account last-used timeline base
This commit is contained in:
@@ -47,10 +47,15 @@ export function generateConnectionEvents(accounts: AccountData[]): ConnectionEve
|
|||||||
// Use a shared base time so events from all accounts interleave in the timeline.
|
// Use a shared base time so events from all accounts interleave in the timeline.
|
||||||
// Without this, accounts with more recent lastUsedAt dominate the sorted output.
|
// Without this, accounts with more recent lastUsedAt dominate the sorted output.
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const sharedBaseTime = accounts.reduce((latest, a) => {
|
const latestLastUsedAt = accounts.reduce<number | undefined>((latest, account) => {
|
||||||
const t = a.lastUsedAt ? new Date(a.lastUsedAt).getTime() : now;
|
if (!account.lastUsedAt) return latest;
|
||||||
return Math.max(latest, isNaN(t) ? now : t);
|
|
||||||
}, now);
|
const timestamp = new Date(account.lastUsedAt).getTime();
|
||||||
|
if (Number.isNaN(timestamp)) return latest;
|
||||||
|
|
||||||
|
return latest === undefined ? timestamp : Math.max(latest, timestamp);
|
||||||
|
}, undefined);
|
||||||
|
const sharedBaseTime = latestLastUsedAt ?? now;
|
||||||
|
|
||||||
accounts.forEach((account) => {
|
accounts.forEach((account) => {
|
||||||
const lastUsed = new Date(sharedBaseTime);
|
const lastUsed = new Date(sharedBaseTime);
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ describe('generateConnectionEvents()', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
vi.useRealTimers();
|
||||||
vi.restoreAllMocks();
|
vi.restoreAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -78,6 +79,33 @@ describe('generateConnectionEvents()', () => {
|
|||||||
expect(emails.has('older@example.com')).toBe(true);
|
expect(emails.has('older@example.com')).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('bases shared timelines on the latest actual lastUsedAt instead of current time', () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
vi.setSystemTime(new Date('2026-05-29T00:00:00.000Z'));
|
||||||
|
|
||||||
|
const latestActualLastUsedAt = new Date('2021-01-01T00:00:00.000Z').getTime();
|
||||||
|
const accounts: AccountData[] = [
|
||||||
|
makeAccount({
|
||||||
|
id: 'old2020',
|
||||||
|
email: 'old2020@example.com',
|
||||||
|
successCount: 1,
|
||||||
|
lastUsedAt: '2020-01-01T00:00:00.000Z',
|
||||||
|
}),
|
||||||
|
makeAccount({
|
||||||
|
id: 'old2021',
|
||||||
|
email: 'old2021@example.com',
|
||||||
|
successCount: 1,
|
||||||
|
lastUsedAt: '2021-01-01T00:00:00.000Z',
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
const events = generateConnectionEvents(accounts);
|
||||||
|
const mostRecentTimestamp = Math.max(...events.map((event) => event.timestamp.getTime()));
|
||||||
|
|
||||||
|
expect(mostRecentTimestamp).toBe(latestActualLastUsedAt);
|
||||||
|
expect(mostRecentTimestamp).toBeLessThan(Date.now() - 365 * 24 * 60 * 60 * 1000);
|
||||||
|
});
|
||||||
|
|
||||||
it('returns at most MAX_TIMELINE_EVENTS events (cap respected by caller slice)', () => {
|
it('returns at most MAX_TIMELINE_EVENTS events (cap respected by caller slice)', () => {
|
||||||
// generateConnectionEvents returns all events unsorted-by-cap; cap is applied by caller.
|
// generateConnectionEvents returns all events unsorted-by-cap; cap is applied by caller.
|
||||||
// But we verify total output does not exceed successCount + failureCount across accounts.
|
// But we verify total output does not exceed successCount + failureCount across accounts.
|
||||||
|
|||||||
Reference in New Issue
Block a user