|
| 1 | +import { mockFunction } from '../../../tests/helpers' |
| 2 | +import { ConnectionInvitationMessage } from '../../modules/connections' |
| 3 | +import { JsonTransformer } from '../../utils/JsonTransformer' |
| 4 | +import { IndyStorageService } from '../IndyStorageService' |
| 5 | +import { DidCommMessageRecord, DidCommMessageRepository, DidCommMessageRole } from '../didcomm' |
| 6 | + |
| 7 | +jest.mock('../IndyStorageService') |
| 8 | + |
| 9 | +const StorageMock = IndyStorageService as unknown as jest.Mock<IndyStorageService<DidCommMessageRecord>> |
| 10 | + |
| 11 | +const invitationJson = { |
| 12 | + '@type': 'https://didcomm.org/connections/1.0/invitation', |
| 13 | + '@id': '04a2c382-999e-4de9-a1d2-9dec0b2fa5e4', |
| 14 | + recipientKeys: ['recipientKeyOne', 'recipientKeyTwo'], |
| 15 | + serviceEndpoint: 'https://example.com', |
| 16 | + label: 'test', |
| 17 | +} |
| 18 | + |
| 19 | +describe('Repository', () => { |
| 20 | + let repository: DidCommMessageRepository |
| 21 | + let storageMock: IndyStorageService<DidCommMessageRecord> |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + storageMock = new StorageMock() |
| 25 | + repository = new DidCommMessageRepository(storageMock) |
| 26 | + }) |
| 27 | + |
| 28 | + const getRecord = ({ id }: { id?: string } = {}) => { |
| 29 | + return new DidCommMessageRecord({ |
| 30 | + id, |
| 31 | + message: invitationJson, |
| 32 | + role: DidCommMessageRole.Receiver, |
| 33 | + associatedRecordId: '16ca6665-29f6-4333-a80e-d34db6bfe0b0', |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + describe('getAgentMessage()', () => { |
| 38 | + it('should get the record using the storage service', async () => { |
| 39 | + const record = getRecord({ id: 'test-id' }) |
| 40 | + mockFunction(storageMock.findByQuery).mockReturnValue(Promise.resolve([record])) |
| 41 | + |
| 42 | + const invitation = await repository.getAgentMessage({ |
| 43 | + messageClass: ConnectionInvitationMessage, |
| 44 | + associatedRecordId: '04a2c382-999e-4de9-a1d2-9dec0b2fa5e4', |
| 45 | + }) |
| 46 | + |
| 47 | + expect(storageMock.findByQuery).toBeCalledWith(DidCommMessageRecord, { |
| 48 | + associatedRecordId: '04a2c382-999e-4de9-a1d2-9dec0b2fa5e4', |
| 49 | + messageType: 'https://didcomm.org/connections/1.0/invitation', |
| 50 | + }) |
| 51 | + expect(invitation).toBeInstanceOf(ConnectionInvitationMessage) |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + describe('saveAgentMessage()', () => { |
| 56 | + it('should transform and save the agent message', async () => { |
| 57 | + await repository.saveAgentMessage({ |
| 58 | + role: DidCommMessageRole.Receiver, |
| 59 | + agentMessage: JsonTransformer.fromJSON(invitationJson, ConnectionInvitationMessage), |
| 60 | + associatedRecordId: '04a2c382-999e-4de9-a1d2-9dec0b2fa5e4', |
| 61 | + }) |
| 62 | + |
| 63 | + expect(storageMock.save).toBeCalledWith( |
| 64 | + expect.objectContaining({ |
| 65 | + role: DidCommMessageRole.Receiver, |
| 66 | + message: invitationJson, |
| 67 | + associatedRecordId: '04a2c382-999e-4de9-a1d2-9dec0b2fa5e4', |
| 68 | + }) |
| 69 | + ) |
| 70 | + }) |
| 71 | + }) |
| 72 | +}) |
0 commit comments