ResourceManager.test.ts.bak•3.62 kB
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { ResourceManager } from '../../../../infrastructure/resource/ResourceManager';
import {
Resource,
ResourceType,
ResourceStatus,
ResourceNotFoundError,
ResourceCacheOptions
} from '../../../../domain/resource-types';
// Mock the ResourceCache class
jest.mock('../../../../infrastructure/cache/ResourceCache', () => {
return {
ResourceCache: jest.fn().mockImplementation(() => {
return {
set: jest.fn().mockImplementation((id: string, value: Resource, options?: ResourceCacheOptions) => Promise.resolve()),
get: jest.fn().mockImplementation((id: string) => Promise.resolve(null)),
getByType: jest.fn().mockImplementation((type: ResourceType) => Promise.resolve([])),
getByTags: jest.fn().mockImplementation((tags: string[]) => Promise.resolve([])),
getByNamespace: jest.fn().mockImplementation((namespace: string) => Promise.resolve([])),
delete: jest.fn().mockImplementation((id: string) => Promise.resolve(true)),
clear: jest.fn().mockImplementation(() => Promise.resolve()),
invalidateByTags: jest.fn().mockImplementation((tags: string[]) => Promise.resolve(0)),
invalidateByType: jest.fn().mockImplementation((type: ResourceType) => Promise.resolve(0)),
invalidateByNamespace: jest.fn().mockImplementation((namespace: string) => Promise.resolve(0))
};
})
};
});
import { ResourceCache } from '../../../../infrastructure/cache/ResourceCache';
describe('ResourceManager', () => {
let resourceManager: ResourceManager;
let mockCache: any;
beforeEach(() => {
// Reset mocks before each test
jest.clearAllMocks();
// Create a new instance of the mocked ResourceCache
mockCache = new ResourceCache();
// Create resourceManager with mocked cache
resourceManager = new ResourceManager(mockCache);
});
it('should initialize correctly', () => {
expect(resourceManager).toBeDefined();
});
it('should create a resource successfully', async () => {
// Setup
const resourceData = {
name: 'Test Resource',
description: 'This is a test resource'
};
// Execute
const resource = await resourceManager.create(
ResourceType.PROJECT,
resourceData
);
// Verify
expect(resource).toBeDefined();
expect(resource.id).toBeDefined();
expect(resource.type).toBe(ResourceType.PROJECT);
expect(resource.status).toBe(ResourceStatus.ACTIVE);
expect(resource.name).toBe('Test Resource');
expect(resource.description).toBe('This is a test resource');
expect(mockCache.set).toHaveBeenCalledWith(
resource.id,
resource,
undefined
);
});
it('should retrieve a resource by ID', async () => {
// Setup
const mockResource = {
id: 'test-123',
type: ResourceType.PROJECT,
status: ResourceStatus.ACTIVE,
createdAt: new Date(),
name: 'Test Resource'
};
mockCache.get.mockResolvedValueOnce(mockResource);
// Execute
const resource = await resourceManager.get(ResourceType.PROJECT, 'test-123');
// Verify
expect(resource).toBe(mockResource);
expect(mockCache.get).toHaveBeenCalledWith('test-123');
});
it('should throw NotFoundError when resource is not found', async () => {
// Setup
mockCache.get.mockResolvedValueOnce(null);
// Execute & Verify
await expect(
resourceManager.get(ResourceType.PROJECT, 'non-existent')
).rejects.toThrow(ResourceNotFoundError);
});
});