forked from openwallet-foundation/credo-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.test.ts
More file actions
104 lines (84 loc) · 3.6 KB
/
agents.test.ts
File metadata and controls
104 lines (84 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// eslint-disable-next-line
// @ts-ignore
import { poll } from 'await-poll';
import { Subject } from 'rxjs';
import { Agent } from '..';
import { toBeConnectedWith, SubjectInboundTransporter, SubjectOutboundTransporter } from './helpers';
import { InitConfig, WireMessage } from '../types';
import indy from 'indy-sdk';
expect.extend({ toBeConnectedWith });
const aliceConfig: InitConfig = {
label: 'Alice',
walletConfig: { id: 'alice' },
walletCredentials: { key: '00000000000000000000000000000Test01' },
autoAcceptConnections: true,
};
const bobConfig: InitConfig = {
label: 'Bob',
walletConfig: { id: 'bob' },
walletCredentials: { key: '00000000000000000000000000000Test02' },
autoAcceptConnections: true,
};
describe('agents', () => {
let aliceAgent: Agent;
let bobAgent: Agent;
afterAll(async () => {
await aliceAgent.closeAndDeleteWallet();
await bobAgent.closeAndDeleteWallet();
});
test('make a connection between agents', async () => {
const aliceMessages = new Subject();
const bobMessages = new Subject();
const aliceAgentInbound = new SubjectInboundTransporter(aliceMessages);
const aliceAgentOutbound = new SubjectOutboundTransporter(bobMessages);
const bobAgentInbound = new SubjectInboundTransporter(bobMessages);
const bobAgentOutbound = new SubjectOutboundTransporter(aliceMessages);
aliceAgent = new Agent(aliceConfig, aliceAgentInbound, aliceAgentOutbound, indy);
await aliceAgent.init();
bobAgent = new Agent(bobConfig, bobAgentInbound, bobAgentOutbound, indy);
await bobAgent.init();
const aliceConnectionAtAliceBob = await aliceAgent.connections.createConnection();
if (!aliceConnectionAtAliceBob.invitation) {
throw new Error('There is no invitation in newly created connection!');
}
const bobConnectionAtBobAlice = await bobAgent.connections.receiveInvitation(
aliceConnectionAtAliceBob.invitation.toJSON()
);
const aliceConnectionRecordAtAliceBob = await aliceAgent.connections.returnWhenIsConnected(
aliceConnectionAtAliceBob.id
);
if (!aliceConnectionRecordAtAliceBob) {
throw new Error('Connection not found!');
}
const bobConnectionRecordAtBobAlice = await bobAgent.connections.returnWhenIsConnected(bobConnectionAtBobAlice.id);
if (!bobConnectionRecordAtBobAlice) {
throw new Error('Connection not found!');
}
expect(aliceConnectionRecordAtAliceBob).toBeConnectedWith(bobConnectionRecordAtBobAlice);
expect(bobConnectionRecordAtBobAlice).toBeConnectedWith(aliceConnectionRecordAtAliceBob);
});
test('send a message to connection', async () => {
const aliceConnections = await aliceAgent.connections.getAll();
console.log('aliceConnections', aliceConnections);
const bobConnections = await bobAgent.connections.getAll();
console.log('bobConnections', bobConnections);
// send message from Alice to Bob
const lastAliceConnection = aliceConnections[aliceConnections.length - 1];
console.log('lastAliceConnection\n', lastAliceConnection);
const message = 'hello, world';
await aliceAgent.basicMessages.sendMessage(lastAliceConnection, message);
const bobMessages = await poll(
async () => {
console.log(`Getting Bob's messages from Alice...`);
const messages = await bobAgent.basicMessages.findAllByQuery({
from: lastAliceConnection.did,
to: lastAliceConnection.theirDid,
});
return messages;
},
(messages: WireMessage[]) => messages.length < 1
);
const lastMessage = bobMessages[bobMessages.length - 1];
expect(lastMessage.content).toBe(message);
});
});