From 372f03799332291958c91e3cbd28966db325d946 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 22 Mar 2026 09:34:35 +0000 Subject: [PATCH] test: add comprehensive tests for calculateTTL and calculateExpiration - Add src/lib/validation.test.ts using node:test - Test calculateTTL with future, past, and current timestamps - Test calculateExpiration with all expiration types - Use t.mock.timers for deterministic testing - Update re-exports in src/lib/validation.ts with .ts extensions for test runner compatibility Co-authored-by: instax-dutta <54683866+instax-dutta@users.noreply.github.com> --- src/lib/validation.test.ts | 70 ++++++++++++++++++++++++++++++++++++++ src/lib/validation.ts | 4 +-- 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/lib/validation.test.ts diff --git a/src/lib/validation.test.ts b/src/lib/validation.test.ts new file mode 100644 index 0000000..b9bfde1 --- /dev/null +++ b/src/lib/validation.test.ts @@ -0,0 +1,70 @@ +import { test } from 'node:test'; +import assert from 'node:assert'; +import { calculateExpiration, calculateTTL } from './validation.ts'; + +test('calculateTTL', async (t) => { + t.mock.timers.enable({ apis: ['Date'] }); + const now = 1000000; + t.mock.timers.setTime(now); + + await t.test('should return undefined when expiresAt is undefined', () => { + assert.strictEqual(calculateTTL(undefined), undefined); + }); + + await t.test('should return positive seconds for future timestamp', () => { + const future = now + 5000; + assert.strictEqual(calculateTTL(future), 5); + }); + + await t.test('should return 0 for past timestamp (edge case)', () => { + const past = now - 5000; + assert.strictEqual(calculateTTL(past), 0); + }); + + await t.test('should return 0 for current timestamp', () => { + assert.strictEqual(calculateTTL(now), 0); + }); + + await t.test('should floor the result', () => { + const future = now + 5500; + assert.strictEqual(calculateTTL(future), 5); + }); +}); + +test('calculateExpiration', async (t) => { + t.mock.timers.enable({ apis: ['Date'] }); + const now = 1000000; + t.mock.timers.setTime(now); + + await t.test('should return undefined for "never"', () => { + assert.strictEqual(calculateExpiration('never'), undefined); + }); + + await t.test('should return now + 5 min', () => { + assert.strictEqual(calculateExpiration('5min'), now + 5 * 60 * 1000); + }); + + await t.test('should return now + 1 hour', () => { + assert.strictEqual(calculateExpiration('1hour'), now + 60 * 60 * 1000); + }); + + await t.test('should return now + 1 day', () => { + assert.strictEqual(calculateExpiration('1day'), now + 24 * 60 * 60 * 1000); + }); + + await t.test('should return now + 7 days', () => { + assert.strictEqual(calculateExpiration('7days'), now + 7 * 24 * 60 * 60 * 1000); + }); + + await t.test('should return now + 30 days', () => { + assert.strictEqual(calculateExpiration('30days'), now + 30 * 24 * 60 * 60 * 1000); + }); + + await t.test('should return undefined for "views"', () => { + assert.strictEqual(calculateExpiration('views'), undefined); + }); + + await t.test('should return undefined for "burn"', () => { + assert.strictEqual(calculateExpiration('burn'), undefined); + }); +}); diff --git a/src/lib/validation.ts b/src/lib/validation.ts index e7504bc..c578ac7 100644 --- a/src/lib/validation.ts +++ b/src/lib/validation.ts @@ -138,9 +138,9 @@ export function calculateTTL(expiresAt?: number): number | undefined { /** * Re-export getClientIp from the dedicated IP utility module. */ -export { getClientIp } from './ip'; +export { getClientIp } from './ip.ts'; /** * Re-export paste size validation logic from the dedicated module. */ -export { MAX_PASTE_SIZE, validatePasteSize } from './paste-size'; +export { MAX_PASTE_SIZE, validatePasteSize } from './paste-size.ts';