Skip to content

Commit

Permalink
test: add messages and connections controller
Browse files Browse the repository at this point in the history
  • Loading branch information
lotharking committed Feb 4, 2025
1 parent 833cfdb commit 372a7ee
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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>(ConnectionsEventController)
service = module.get<ConnectionsEventService>(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',
)
})
})
})
100 changes: 100 additions & 0 deletions packages/nestjs-client/src/messages/message.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(MessageEventController)
messageService = module.get<MessageEventService>(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',
)
})
})
})

0 comments on commit 372a7ee

Please sign in to comment.