-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add messages and connections controller
- Loading branch information
1 parent
833cfdb
commit 372a7ee
Showing
2 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
packages/nestjs-client/src/connections/connection.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
100
packages/nestjs-client/src/messages/message.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
) | ||
}) | ||
}) | ||
}) |