diff --git a/packages/nestjs-client/src/connections/connection.controller.spec.ts b/packages/nestjs-client/src/connections/connection.controller.spec.ts new file mode 100644 index 0000000..3cffa1b --- /dev/null +++ b/packages/nestjs-client/src/connections/connection.controller.spec.ts @@ -0,0 +1,69 @@ +import { HttpUtils } from '@2060.io/service-agent-client' +import { ConnectionStateUpdated, ExtendedDidExchangeState } from '@2060.io/service-agent-model' +import { Logger } from '@nestjs/common' +import { Test, TestingModule } from '@nestjs/testing' + +import { ConnectionsEventController } from './connection.controller' +import { ConnectionsEventService } from './connection.service' + +jest.mock('@2060.io/service-agent-client', () => ({ + HttpUtils: { + handleException: jest.fn(), + }, +})) + +describe('ConnectionsEventController', () => { + let controller: ConnectionsEventController + let service: ConnectionsEventService + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [ConnectionsEventController], + providers: [ + { + provide: ConnectionsEventService, + useValue: { + update: jest.fn(), + }, + }, + ], + }).compile() + + controller = module.get(ConnectionsEventController) + service = module.get(ConnectionsEventService) + }) + + it('should be defined', () => { + expect(controller).toBeDefined() + }) + + describe('update', () => { + const mockBody = new ConnectionStateUpdated({ + connectionId: '123', + invitationId: '456', + state: ExtendedDidExchangeState.Completed, + }) + + it('should call service.update and return success message', async () => { + jest.spyOn(service, 'update').mockResolvedValue(undefined) + + const response = await controller.update(mockBody) + + expect(service.update).toHaveBeenCalledWith(mockBody) + expect(response).toEqual({ message: 'Connection state updated successfully' }) + }) + + it('should handle exceptions and call HttpUtils.handleException', async () => { + const error = new Error('Test error') + jest.spyOn(service, 'update').mockRejectedValue(error) + + await controller.update(mockBody) + + expect(HttpUtils.handleException).toHaveBeenCalledWith( + expect.any(Logger), + error, + 'Failed to update connection state', + ) + }) + }) +}) diff --git a/packages/nestjs-client/src/messages/message.controller.spec.ts b/packages/nestjs-client/src/messages/message.controller.spec.ts new file mode 100644 index 0000000..62097cf --- /dev/null +++ b/packages/nestjs-client/src/messages/message.controller.spec.ts @@ -0,0 +1,100 @@ +import { HttpUtils } from '@2060.io/service-agent-client' +import { MessageReceived, MessageStateUpdated, TextMessage } from '@2060.io/service-agent-model' +import { Logger } from '@nestjs/common' +import { Test, TestingModule } from '@nestjs/testing' +import { MessageState } from 'credo-ts-receipts' + +import { MessageEventController } from './message.controller' +import { MessageEventService } from './message.service' + +jest.mock('@2060.io/service-agent-client', () => ({ + HttpUtils: { + handleException: jest.fn(), + }, +})) + +describe('MessageEventController', () => { + let controller: MessageEventController + let messageService: MessageEventService + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [MessageEventController], + providers: [ + { + provide: MessageEventService, + useValue: { + received: jest.fn(), + updated: jest.fn(), + }, + }, + ], + }).compile() + + controller = module.get(MessageEventController) + messageService = module.get(MessageEventService) + }) + + describe('received', () => { + const mockBody = new MessageReceived({ + message: { + type: 'text', + connectionId: 'conn1', + threadId: 'thread1', + content: 'Hello', + } as TextMessage, + }) + it('should successfully process received message', async () => { + const result = await controller.received(mockBody) + + expect(messageService.received).toHaveBeenCalledWith(mockBody) + expect(result).toEqual({ message: 'Message received updated successfully' }) + }) + + it('should handle error in received message processing', async () => { + const error = new Error('Test error') + jest.spyOn(messageService, 'received').mockRejectedValue(error) + await controller.received(mockBody) + + expect(HttpUtils.handleException).toHaveBeenCalledWith( + expect.any(Logger), + error, + 'Failed to received message state', + ) + }) + }) + + describe('updated', () => { + it('should successfully process updated message state', async () => { + const mockBody = new MessageStateUpdated({ + connectionId: 'conn-1', + messageId: 'msg-1', + state: MessageState.Submitted, + }) + + const result = await controller.updated(mockBody) + + expect(messageService.updated).toHaveBeenCalled() + expect(result).toEqual({ message: 'Message state updated successfully' }) + }) + + it('should handle error in message state update', async () => { + const error = new Error('Test error') + jest.spyOn(messageService, 'updated').mockRejectedValue(error) + jest.spyOn(HttpUtils, 'handleException') + + const mockBody = new MessageStateUpdated({ + connectionId: 'conn-1', + messageId: 'msg-1', + state: MessageState.Submitted, + }) + await controller.updated(mockBody) + + expect(HttpUtils.handleException).toHaveBeenCalledWith( + expect.any(Logger), + error, + 'Failed to update message state', + ) + }) + }) +})