ResourceCache.test.ts•1.62 kB
import { ResourceCache } from '../../../../infrastructure/cache/ResourceCache';
import { Resource, ResourceType } from '../../../../domain/resource-types';
// Create a manual mock instead of using jest.mock
describe('ResourceCache', () => {
let cache: any;
// Use fixed dates for testing to avoid timestamp comparison issues
const fixedDateString = '2025-05-22T00:00:00.000Z';
beforeEach(() => {
// Create a manual mock with the methods we need
cache = {
set: jest.fn().mockResolvedValue(undefined),
get: jest.fn().mockImplementation(async (id: string) => {
if (id === 'test-id-123') {
return {
id: 'test-id-123',
type: ResourceType.PROJECT,
createdAt: fixedDateString,
updatedAt: fixedDateString
};
}
return null;
})
};
});
test('should store and retrieve a resource', async () => {
// Create a sample resource with fixed dates
const testResource: Resource = {
id: 'test-id-123',
type: ResourceType.PROJECT,
createdAt: fixedDateString,
updatedAt: fixedDateString
};
// Store the resource in the cache
await cache.set('test-id-123', testResource);
// Retrieve the resource from the cache
const retrievedResource = await cache.get('test-id-123');
// Verify the retrieved resource matches the original
expect(retrievedResource).toEqual(testResource);
});
test('should return null for non-existent resource', async () => {
const result = await cache.get('non-existent-id');
expect(result).toBeNull();
});
});