basic.test.ts•2.33 kB
/**
* Basic tests for the API that don't require actual database connectivity
* These tests use in-line mocks to avoid the need for Prisma initialization
*/
import { describe, test, expect, jest, beforeEach } from '@jest/globals';
import { cacheService } from '../utils/cache';
// Mock the entire database module
jest.mock('../utils/db', () => {
return {
db: {
connect: jest.fn(),
disconnect: jest.fn()
},
prisma: {
article: {
findMany: jest.fn().mockResolvedValue([]),
findUnique: jest.fn().mockResolvedValue(null)
},
source: {
findMany: jest.fn().mockResolvedValue([])
}
},
connectDb: jest.fn(),
disconnectDb: jest.fn(),
Database: jest.fn(),
DatabaseConfig: jest.fn()
};
});
describe('Cache Service', () => {
beforeEach(() => {
// Clear the cache before each test
cacheService.clear();
});
test('should have cache service defined', () => {
expect(cacheService).toBeDefined();
});
test('should set and get values from cache', () => {
const key = 'testKey';
const value = { test: 'data' };
cacheService.set(key, value);
const retrieved = cacheService.get(key);
expect(retrieved).toEqual(value);
});
test('should clear the cache', () => {
// Set some values in the cache
cacheService.set('key1', 'value1');
cacheService.set('key2', 'value2');
// Verify values are in the cache
expect(cacheService.get('key1')).toBe('value1');
expect(cacheService.get('key2')).toBe('value2');
// Clear the cache
cacheService.clear();
// Verify values are no longer in the cache
expect(cacheService.get('key1')).toBeUndefined();
expect(cacheService.get('key2')).toBeUndefined();
});
test('should provide cache statistics', () => {
// Set a value
cacheService.set('key1', 'value1');
// Get the value (this should count as a hit)
cacheService.get('key1');
// Try to get a non-existent value (this should count as a miss)
cacheService.get('nonexistent');
// Get statistics
const stats = cacheService.getStats();
// Verify statistics
expect(stats).toBeDefined();
expect(stats.hits).toBeGreaterThanOrEqual(1);
expect(stats.misses).toBeGreaterThanOrEqual(1);
});
});