import { describe, it, expect, vi, beforeEach } from 'vitest'; import { AcceptSponsorshipRequestMutation } from './AcceptSponsorshipRequestMutation'; import { SponsorshipRequestsService } from '@/lib/services/sponsors/SponsorshipRequestsService'; import { Result } from '@/lib/contracts/Result'; // Mock dependencies vi.mock('@/lib/services/sponsors/SponsorshipRequestsService', () => { return { SponsorshipRequestsService: vi.fn(), }; }); describe('AcceptSponsorshipRequestMutation', () => { let mutation: AcceptSponsorshipRequestMutation; let mockServiceInstance: any; beforeEach(() => { vi.clearAllMocks(); mockServiceInstance = { acceptRequest: vi.fn(), }; // Use mockImplementation to return the instance (SponsorshipRequestsService as any).mockImplementation(function() { return mockServiceInstance; }); mutation = new AcceptSponsorshipRequestMutation(); }); describe('execute', () => { describe('happy paths', () => { it('should successfully accept sponsorship request with valid input', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: 'driver-456', }; mockServiceInstance.acceptRequest.mockResolvedValue(Result.ok(undefined)); // Act const result = await mutation.execute(command); // Assert expect(result.isOk()).toBe(true); expect(result.unwrap()).toBeUndefined(); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledWith(command); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledTimes(1); }); }); describe('failure modes', () => { it('should handle service failure during acceptance', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: 'driver-456', }; const serviceError = new Error('Service error'); mockServiceInstance.acceptRequest.mockRejectedValue(serviceError); // Act const result = await mutation.execute(command); // Assert expect(result.isErr()).toBe(true); expect(result.getError()).toBe('ACCEPT_SPONSORSHIP_REQUEST_FAILED'); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledTimes(1); }); it('should handle service returning error result', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: 'driver-456', }; const domainError = { type: 'serverError', message: 'Database connection failed' }; mockServiceInstance.acceptRequest.mockResolvedValue(Result.err(domainError)); // Act const result = await mutation.execute(command); // Assert expect(result.isErr()).toBe(true); expect(result.getError()).toBe('ACCEPT_SPONSORSHIP_REQUEST_FAILED'); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledTimes(1); }); it('should handle service returning validation error', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: 'driver-456', }; const domainError = { type: 'validationError', message: 'Invalid request ID' }; mockServiceInstance.acceptRequest.mockResolvedValue(Result.err(domainError)); // Act const result = await mutation.execute(command); // Assert expect(result.isErr()).toBe(true); expect(result.getError()).toBe('ACCEPT_SPONSORSHIP_REQUEST_FAILED'); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledTimes(1); }); it('should handle service returning notFound error', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: 'driver-456', }; const domainError = { type: 'notFound', message: 'Sponsorship request not found' }; mockServiceInstance.acceptRequest.mockResolvedValue(Result.err(domainError)); // Act const result = await mutation.execute(command); // Assert expect(result.isErr()).toBe(true); expect(result.getError()).toBe('ACCEPT_SPONSORSHIP_REQUEST_FAILED'); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledTimes(1); }); it('should handle service returning unauthorized error', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: 'driver-456', }; const domainError = { type: 'unauthorized', message: 'Insufficient permissions' }; mockServiceInstance.acceptRequest.mockResolvedValue(Result.err(domainError)); // Act const result = await mutation.execute(command); // Assert expect(result.isErr()).toBe(true); expect(result.getError()).toBe('ACCEPT_SPONSORSHIP_REQUEST_FAILED'); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledTimes(1); }); }); describe('error mapping', () => { it('should map various domain errors to mutation errors', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: 'driver-456', }; const testCases = [ { domainError: { type: 'notFound' }, expectedError: 'ACCEPT_SPONSORSHIP_REQUEST_FAILED' }, { domainError: { type: 'unauthorized' }, expectedError: 'ACCEPT_SPONSORSHIP_REQUEST_FAILED' }, { domainError: { type: 'validationError' }, expectedError: 'ACCEPT_SPONSORSHIP_REQUEST_FAILED' }, { domainError: { type: 'serverError' }, expectedError: 'ACCEPT_SPONSORSHIP_REQUEST_FAILED' }, { domainError: { type: 'networkError' }, expectedError: 'ACCEPT_SPONSORSHIP_REQUEST_FAILED' }, { domainError: { type: 'notImplemented' }, expectedError: 'ACCEPT_SPONSORSHIP_REQUEST_FAILED' }, { domainError: { type: 'unknown' }, expectedError: 'ACCEPT_SPONSORSHIP_REQUEST_FAILED' }, ]; for (const testCase of testCases) { mockServiceInstance.acceptRequest.mockResolvedValue(Result.err(testCase.domainError)); const result = await mutation.execute(command); expect(result.isErr()).toBe(true); expect(result.getError()).toBe(testCase.expectedError); } }); }); describe('input validation', () => { it('should accept valid command input', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: 'driver-456', }; mockServiceInstance.acceptRequest.mockResolvedValue(Result.ok(undefined)); // Act const result = await mutation.execute(command); // Assert expect(result.isOk()).toBe(true); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledTimes(1); }); it('should handle empty requestId gracefully', async () => { // Arrange const command = { requestId: '', actorDriverId: 'driver-456', }; mockServiceInstance.acceptRequest.mockResolvedValue(Result.ok(undefined)); // Act const result = await mutation.execute(command); // Assert expect(result.isOk()).toBe(true); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledWith(command); }); it('should handle empty actorDriverId gracefully', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: '', }; mockServiceInstance.acceptRequest.mockResolvedValue(Result.ok(undefined)); // Act const result = await mutation.execute(command); // Assert expect(result.isOk()).toBe(true); expect(mockServiceInstance.acceptRequest).toHaveBeenCalledWith(command); }); }); describe('service instantiation', () => { it('should create SponsorshipRequestsService instance', () => { // Arrange & Act const mutation = new AcceptSponsorshipRequestMutation(); // Assert expect(mutation).toBeInstanceOf(AcceptSponsorshipRequestMutation); }); }); describe('result shape', () => { it('should return void on success', async () => { // Arrange const command = { requestId: 'request-123', actorDriverId: 'driver-456', }; mockServiceInstance.acceptRequest.mockResolvedValue(Result.ok(undefined)); // Act const result = await mutation.execute(command); // Assert expect(result.isOk()).toBe(true); expect(result.unwrap()).toBeUndefined(); }); }); }); });