100 lines
2.6 KiB
TypeScript
100 lines
2.6 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { createService, getServiceById, getAllServices, updateService, deleteService } from './repository.js';
|
|
|
|
// Mock the cosmos container
|
|
vi.mock('../../lib/cosmos-init.js', () => ({
|
|
getContainer: vi.fn(() => ({
|
|
items: {
|
|
create: vi.fn(),
|
|
upsert: vi.fn(),
|
|
read: vi.fn(),
|
|
query: vi.fn(),
|
|
delete: vi.fn(),
|
|
},
|
|
})),
|
|
}));
|
|
|
|
describe('Services Repository', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe('createService', () => {
|
|
it('should create a new service', async () => {
|
|
const serviceData = {
|
|
id: 'test-service',
|
|
name: 'Test Service',
|
|
scriptPath: '../deploy-test.sh',
|
|
healthUrl: 'https://test.example.com/health',
|
|
repoPath: '../test-repo',
|
|
};
|
|
|
|
const service = await createService(serviceData);
|
|
|
|
expect(service).toBeDefined();
|
|
expect(service.id).toBe('test-service');
|
|
expect(service.name).toBe('Test Service');
|
|
});
|
|
|
|
it('should include productId in created service', async () => {
|
|
const serviceData = {
|
|
id: 'test-service',
|
|
name: 'Test Service',
|
|
scriptPath: '../deploy-test.sh',
|
|
healthUrl: 'https://test.example.com/health',
|
|
repoPath: '../test-repo',
|
|
};
|
|
|
|
const service = await createService(serviceData);
|
|
|
|
expect(service.productId).toBe('devops-internal');
|
|
});
|
|
});
|
|
|
|
describe('getServiceById', () => {
|
|
it('should retrieve a service by id', async () => {
|
|
const service = await getServiceById('test-service');
|
|
|
|
expect(service).toBeDefined();
|
|
expect(service?.id).toBe('test-service');
|
|
});
|
|
|
|
it('should return null for non-existent service', async () => {
|
|
const service = await getServiceById('non-existent');
|
|
|
|
expect(service).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('getAllServices', () => {
|
|
it('should return all services', async () => {
|
|
const services = await getAllServices();
|
|
|
|
expect(Array.isArray(services)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('updateService', () => {
|
|
it('should update an existing service', async () => {
|
|
const updates = {
|
|
name: 'Updated Service Name',
|
|
};
|
|
|
|
const service = await updateService('test-service', updates);
|
|
|
|
expect(service).toBeDefined();
|
|
expect(service?.name).toBe('Updated Service Name');
|
|
});
|
|
});
|
|
|
|
describe('deleteService', () => {
|
|
it('should delete a service', async () => {
|
|
await deleteService('test-service');
|
|
|
|
// Verify deletion
|
|
const service = await getServiceById('test-service');
|
|
expect(service).toBeNull();
|
|
});
|
|
});
|
|
});
|