Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/api/student/resume/confirm/route.theme-contrast.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { describe, it, expect, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest';
import { POST } from './route';

vi.mock('@/lib/github-owner-verification', () => ({
verifyGitHubOwner: vi.fn().mockResolvedValue({
verified: true,
status: 200,
message: 'OK',
}),
}));

vi.mock('@/lib/mongodb', () => ({
default: vi.fn(),
}));
Expand Down
19 changes: 4 additions & 15 deletions components/dashboard/StatsCard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import StatsCard from './StatsCard';

// Mock framer-motion

vi.mock('framer-motion', () => ({
motion: {
div: ({
children,
whileHover,
whileTap,
whileInView,
initial,
animate,
exit,
transition,
viewport,
layoutId,
...props
}: any) => (
<div {...props} data-testid="motion-div">
div: ({ children, ...rest }: any) => (
<div {...rest} data-testid="motion-div">
{children}
</div>
),
Expand Down
35 changes: 28 additions & 7 deletions lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ type CacheItem<T> = {
value: T;
expiresAt: number;
};

/**
* A Simple in-memory TTL(Time To Live) cache.
*
Expand All @@ -18,16 +17,21 @@ type CacheItem<T> = {
* @typeParam T - Type of values stored in the cache.
*/
export class TTLCache<T> {
//private store = new Map<string, CacheItem<T>>();

private store = new Map<string, CacheItem<T | Buffer>>();

private cleanupInterval: ReturnType<typeof setInterval> | null = null;
private readonly maxSize?: number;

private static assertValidKey(key: unknown): asserts key is string {
if (typeof key !== 'string') {
throw new TypeError('Cache key must be a string');
}
}

if (key.trim().length === 0) {
throw new TypeError('Cache key cannot be empty');
}
}
/**
* Creates a new TTL cache instance.
*
Expand Down Expand Up @@ -109,7 +113,14 @@ export class TTLCache<T> {
* const user = cache.get("user:1");
*/
get(key: string): T | null {
TTLCache.assertValidKey(key);
//TTLCache.assertValidKey(key);
if (key === null || key === undefined) {
throw new TypeError('Cache key must be a string');
}

if (typeof key !== 'string') {
throw new TypeError('Cache key must be a string');
}

const hit = this.store.get(key);
if (!hit) return null;
Expand All @@ -136,7 +147,14 @@ export class TTLCache<T> {
* }
*/
has(key: string): boolean {
TTLCache.assertValidKey(key);
//TTLCache.assertValidKey(key);
if (key === null || key === undefined) {
throw new TypeError('Cache key must be a string');
}

if (typeof key !== 'string') {
throw new TypeError('Cache key must be a string');
}

const hit = this.store.get(key);
if (!hit) return false;
Expand Down Expand Up @@ -203,8 +221,11 @@ export class TTLCache<T> {
}

set(key: string, value: T, ttlMs: number): void {
TTLCache.assertValidKey(key);
if (key === '') throw new Error('Cache key cannot be empty');
//TTLCache.assertValidKey(key);
if (typeof key !== 'string' || key.trim().length === 0) {
throw new TypeError('Cache key cannot be empty');
}

if (ttlMs <= 0) throw new RangeError(`ttlMs must be positive, got ${ttlMs}`);
if (Number.isNaN(ttlMs)) ttlMs = 60_000;

Expand Down
Loading