fix(quota): ignore invalid reset timestamps (#1182)
* fix(quota): ignore invalid reset timestamps * fix(quota): guard reset-after timestamps --------- Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
This commit is contained in:
committed by
GitHub
co-authored by
Isaac Sanchez
parent
323347f50e
commit
b944bd812c
@@ -1,6 +1,10 @@
|
||||
export const formatResetTime = (timestamp) => {
|
||||
try {
|
||||
const resetDate = new Date(timestamp);
|
||||
if (!Number.isFinite(resetDate.getTime())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const isToday = resetDate.toDateString() === now.toDateString();
|
||||
|
||||
@@ -25,7 +29,9 @@ export const formatResetTime = (timestamp) => {
|
||||
|
||||
export const calculateResetAfterSeconds = (resetAt) => {
|
||||
if (!resetAt) return null;
|
||||
const delta = Math.floor((resetAt - Date.now()) / 1000);
|
||||
const resetAtTime = new Date(resetAt).getTime();
|
||||
if (!Number.isFinite(resetAtTime)) return null;
|
||||
const delta = Math.floor((resetAtTime - Date.now()) / 1000);
|
||||
return delta < 0 ? 0 : delta;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { calculateResetAfterSeconds, formatResetTime } from './formatters.js';
|
||||
|
||||
describe('formatResetTime', () => {
|
||||
it('returns null for invalid timestamps', () => {
|
||||
expect(formatResetTime('not-a-date')).toBeNull();
|
||||
expect(formatResetTime(NaN)).toBeNull();
|
||||
expect(formatResetTime(Infinity)).toBeNull();
|
||||
expect(formatResetTime(-Infinity)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculateResetAfterSeconds', () => {
|
||||
it('returns null for invalid timestamps', () => {
|
||||
expect(calculateResetAfterSeconds('not-a-date')).toBeNull();
|
||||
expect(calculateResetAfterSeconds(NaN)).toBeNull();
|
||||
expect(calculateResetAfterSeconds(Infinity)).toBeNull();
|
||||
expect(calculateResetAfterSeconds(-Infinity)).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user