|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import { ineeda } from 'ineeda';
|
3 |
| -import { spy } from 'sinon'; |
| 3 | +import { match, spy } from 'sinon'; |
4 | 4 |
|
5 | 5 | import { Bot } from '../../src/Bot';
|
6 | 6 | import { INJECT_BOT } from '../../src/BotService';
|
| 7 | +import { User } from '../../src/entity/auth/User'; |
| 8 | +import { Context } from '../../src/entity/Context'; |
7 | 9 | import { Message } from '../../src/entity/Message';
|
| 10 | +import { NotImplementedError } from '../../src/error/NotImplementedError'; |
8 | 11 | import { LoopbackListener } from '../../src/listener/LoopbackListener';
|
| 12 | +import { Service } from '../../src/Service'; |
| 13 | +import { TYPE_TEXT } from '../../src/utils/Mime'; |
9 | 14 | import { describeLeaks, itLeaks } from '../helpers/async';
|
10 | 15 | import { createService, createServiceContainer } from '../helpers/container';
|
11 | 16 |
|
| 17 | +const TEST_METADATA = { |
| 18 | + kind: 'loopback-listener', |
| 19 | + name: 'test-listener', |
| 20 | +}; |
| 21 | + |
| 22 | +const TEST_TARGET = { |
| 23 | + kind: 'test-target', |
| 24 | + name: 'test-target', |
| 25 | +}; |
| 26 | + |
12 | 27 | describeLeaks('loopback listener', async () => {
|
13 | 28 | itLeaks('should send messages to the bot', async () => {
|
| 29 | + const { container, services } = await createServiceContainer(); |
| 30 | + |
| 31 | + const svc = ineeda<Service>(TEST_TARGET); |
| 32 | + services.addService(svc); |
| 33 | + |
14 | 34 | const receive = spy();
|
15 |
| - const { container } = await createServiceContainer(); |
16 | 35 | const listener = await createService(container, LoopbackListener, {
|
17 | 36 | [INJECT_BOT]: ineeda<Bot>({
|
18 | 37 | receive,
|
19 | 38 | }),
|
20 | 39 | data: {
|
| 40 | + defaultTarget: TEST_TARGET, |
21 | 41 | filters: [],
|
22 | 42 | strict: false,
|
23 | 43 | },
|
24 |
| - metadata: { |
25 |
| - kind: 'loopback-listener', |
26 |
| - name: 'test-listener', |
27 |
| - }, |
| 44 | + metadata: TEST_METADATA, |
28 | 45 | });
|
| 46 | + await listener.start(); |
| 47 | + |
| 48 | + const ctxOptions = { |
| 49 | + name: 'test-user', |
| 50 | + uid: 'test-uid', |
| 51 | + }; |
| 52 | + const msgOptions = { |
| 53 | + body: 'test-body', |
| 54 | + type: TYPE_TEXT, |
| 55 | + }; |
29 | 56 |
|
30 |
| - const msg = ineeda<Message>(); |
| 57 | + const msg = new Message({ |
| 58 | + context: new Context({ |
| 59 | + channel: { |
| 60 | + id: '', |
| 61 | + thread: '', |
| 62 | + }, |
| 63 | + ...ctxOptions, |
| 64 | + }), |
| 65 | + labels: {}, |
| 66 | + reactions: [], |
| 67 | + ...msgOptions, |
| 68 | + }); |
31 | 69 | await listener.send(msg);
|
32 | 70 |
|
33 |
| - expect(receive).to.have.callCount(1).and.been.calledWith(msg); |
| 71 | + expect(receive).to.have.callCount(1) |
| 72 | + .and.been.calledWithMatch(match.has('body', msgOptions.body) |
| 73 | + .and(match.has('type', msgOptions.type))); |
| 74 | + }); |
| 75 | + |
| 76 | + itLeaks('should throw when fetching messages', async () => { |
| 77 | + const { container, services } = await createServiceContainer(); |
| 78 | + |
| 79 | + const svc = ineeda<Service>(TEST_TARGET); |
| 80 | + services.addService(svc); |
| 81 | + |
| 82 | + const listener = await createService(container, LoopbackListener, { |
| 83 | + data: { |
| 84 | + defaultTarget: TEST_TARGET, |
| 85 | + filters: [], |
| 86 | + strict: false, |
| 87 | + }, |
| 88 | + metadata: TEST_METADATA, |
| 89 | + }); |
| 90 | + await listener.start(); |
| 91 | + |
| 92 | + await expect(listener.fetch({ |
| 93 | + channel: '', |
| 94 | + })).to.eventually.be.rejectedWith(NotImplementedError); |
| 95 | + }); |
| 96 | + |
| 97 | + itLeaks('should throw when creating sessions', async () => { |
| 98 | + const { container, services } = await createServiceContainer(); |
| 99 | + |
| 100 | + const svc = ineeda<Service>(TEST_TARGET); |
| 101 | + services.addService(svc); |
| 102 | + |
| 103 | + const listener = await createService(container, LoopbackListener, { |
| 104 | + data: { |
| 105 | + defaultTarget: TEST_TARGET, |
| 106 | + filters: [], |
| 107 | + strict: false, |
| 108 | + }, |
| 109 | + metadata: TEST_METADATA, |
| 110 | + }); |
| 111 | + await listener.start(); |
| 112 | + |
| 113 | + await expect(listener.createSession('test', ineeda<User>())).to.eventually.be.rejectedWith(NotImplementedError); |
| 114 | + }); |
| 115 | + |
| 116 | + itLeaks('should not provide sessions', async () => { |
| 117 | + const { container, services } = await createServiceContainer(); |
| 118 | + |
| 119 | + const svc = ineeda<Service>(TEST_TARGET); |
| 120 | + services.addService(svc); |
| 121 | + |
| 122 | + const listener = await createService(container, LoopbackListener, { |
| 123 | + data: { |
| 124 | + defaultTarget: TEST_TARGET, |
| 125 | + filters: [], |
| 126 | + strict: false, |
| 127 | + }, |
| 128 | + metadata: TEST_METADATA, |
| 129 | + }); |
| 130 | + await listener.start(); |
| 131 | + |
| 132 | + await expect(listener.getSession('test')).to.eventually.equal(undefined); |
34 | 133 | });
|
35 | 134 | });
|
0 commit comments