forked from slackapi/bolt-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversation-store.spec.ts
283 lines (254 loc) · 10.6 KB
/
conversation-store.spec.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import 'mocha';
import { assert, AssertionError } from 'chai';
import sinon, { SinonSpy } from 'sinon';
import rewiremock from 'rewiremock';
import { Logger } from '@slack/logger';
import { WebClient } from '@slack/web-api';
import { Override, createFakeLogger, delay } from './test-helpers';
import { ConversationStore } from './conversation-store';
import { AnyMiddlewareArgs, NextFn, Context } from './types';
describe('conversationContext middleware', () => {
it('should forward events that have no conversation ID', async () => {
// Arrange
// conversationId property is omitted from return value
const fakeGetTypeAndConversation = sinon.fake.returns({});
const fakeStore = createFakeStore();
const fakeLogger = createFakeLogger();
const fakeNext = sinon.fake();
const dummyContext: DummyContext<void> = {};
const { conversationContext } = await importConversationStore(
withGetTypeAndConversation(fakeGetTypeAndConversation),
);
const fakeArgs = {
body: {},
context: dummyContext,
next: fakeNext,
logger: fakeLogger,
} as unknown as MiddlewareArgs;
// Act
const middleware = conversationContext(fakeStore);
await middleware(fakeArgs);
// Assert
assert(fakeLogger.debug.called);
assert(fakeNext.called);
assert.notProperty(dummyContext, 'updateConversation');
assert.notProperty(dummyContext, 'conversation');
});
it('should add to the context for events within a conversation that was not previously stored and pass expiresAt', async () => {
// Arrange
const dummyConversationState = Symbol();
const dummyConversationId = 'CONVERSATION_ID';
const dummyStoreSetResult = Symbol();
const dummyExpiresAt = 1234;
const fakeGetTypeAndConversation = sinon.fake.returns({ conversationId: dummyConversationId });
const fakeStore = createFakeStore(
sinon.fake.rejects(new Error('Test conversation missing')),
sinon.fake.resolves(dummyStoreSetResult),
);
const fakeLogger = createFakeLogger();
const fakeNext = sinon.fake();
const dummyContext: DummyContext<symbol> = {};
const { conversationContext } = await importConversationStore(
withGetTypeAndConversation(fakeGetTypeAndConversation),
);
const fakeArgs = {
body: {},
context: dummyContext,
next: fakeNext,
logger: fakeLogger,
} as unknown as MiddlewareArgs;
// Act
const middleware = conversationContext(fakeStore);
await middleware(fakeArgs);
assert(fakeNext.called);
assert.notProperty(dummyContext, 'conversation');
if (dummyContext.updateConversation === undefined) {
assert.fail();
}
assert.equal(await dummyContext.updateConversation(dummyConversationState, dummyExpiresAt), dummyStoreSetResult);
assert(fakeStore.set.calledOnce);
assert(fakeStore.set.calledWith(dummyConversationId, dummyConversationState, dummyExpiresAt));
});
it('should add to the context for events within a conversation that was not previously stored', async () => {
// Arrange
const dummyConversationState = Symbol();
const dummyConversationId = 'CONVERSATION_ID';
const dummyStoreSetResult = Symbol();
const fakeGetTypeAndConversation = sinon.fake.returns({ conversationId: dummyConversationId });
const fakeStore = createFakeStore(
sinon.fake.rejects(new Error('Test conversation missing')),
sinon.fake.resolves(dummyStoreSetResult),
);
const fakeLogger = createFakeLogger();
const fakeNext = sinon.fake();
const dummyContext: DummyContext<symbol> = {};
const { conversationContext } = await importConversationStore(
withGetTypeAndConversation(fakeGetTypeAndConversation),
);
const fakeArgs = {
body: {},
context: dummyContext,
next: fakeNext,
logger: fakeLogger,
} as unknown as MiddlewareArgs;
// Act
const middleware = conversationContext(fakeStore);
await middleware(fakeArgs);
// Assert
assert(fakeNext.called);
assert.notProperty(dummyContext, 'conversation');
// NOTE: chai types do not offer assertion signatures yet, and neither do node's assert module types.
if (dummyContext.updateConversation === undefined) {
assert.fail();
}
assert.equal(await dummyContext.updateConversation(dummyConversationState), dummyStoreSetResult);
assert(fakeStore.set.calledOnce);
assert(fakeStore.set.calledWith(dummyConversationId, dummyConversationState));
});
it('should add to the context for events within a conversation that was previously stored', async () => {
// Arrange
const dummyConversationState = Symbol();
const dummyConversationId = 'CONVERSATION_ID';
const dummyStoreSetResult = Symbol();
const fakeGetTypeAndConversation = sinon.fake.returns({ conversationId: dummyConversationId });
const fakeStore = createFakeStore(
sinon.fake.resolves(dummyConversationState),
sinon.fake.resolves(dummyStoreSetResult),
);
const fakeLogger = createFakeLogger();
const fakeNext = sinon.fake();
const dummyContext: DummyContext<symbol> = {};
const { conversationContext } = await importConversationStore(
withGetTypeAndConversation(fakeGetTypeAndConversation),
);
const fakeArgs = {
body: {},
context: dummyContext,
next: fakeNext,
logger: fakeLogger,
} as unknown as MiddlewareArgs;
// Act
const middleware = conversationContext(fakeStore);
await middleware(fakeArgs);
// Assert
assert.equal(dummyContext.conversation, dummyConversationState);
// NOTE: chai types do not offer assertion signatures yet, and neither do node's assert module types.
if (dummyContext.updateConversation === undefined) {
assert.fail();
}
const newDummyConversationState = Symbol();
const result = await dummyContext.updateConversation(newDummyConversationState);
assert.equal(result, dummyStoreSetResult);
assert(fakeStore.set.calledOnce);
assert(fakeStore.set.calledWith(dummyConversationId, newDummyConversationState));
});
});
describe('MemoryStore', () => {
describe('constructor', () => {
it('should initialize successfully', async () => {
// Arrange
const { MemoryStore } = await importConversationStore();
// Act
const store = new MemoryStore();
// Assert
assert.isOk(store);
});
});
// NOTE: there's no good way to fetch the contents of the map that backs the state with an override, so instead we use
// the public API once again. as a consequence, this is not a pure unit test of a single method, but it does verify
// the expected behavior when looking at set and get as one unit.
describe('#set and #get', () => {
it('should store conversation state', async () => {
// Arrange
const dummyConversationState = Symbol();
const dummyConversationId = 'CONVERSATION_ID';
const { MemoryStore } = await importConversationStore();
// Act
const store = new MemoryStore();
await store.set(dummyConversationId, dummyConversationState);
const actualConversationState = await store.get(dummyConversationId);
// Assert
assert.equal(actualConversationState, dummyConversationState);
});
it('should reject lookup of conversation state when the conversation is not stored', async () => {
// Arrange
const { MemoryStore } = await importConversationStore();
// Act
const store = new MemoryStore();
try {
await store.get('CONVERSATION_ID');
assert.fail();
} catch (error: any) {
// Assert
assert.instanceOf(error, Error);
assert.notInstanceOf(error, AssertionError);
}
});
it('should reject lookup of conversation state when the conversation is expired', async () => {
// Arrange
const dummyConversationId = 'CONVERSATION_ID';
const dummyConversationState = Symbol();
const expiresInMs = 5;
const { MemoryStore } = await importConversationStore();
// Act
const store = new MemoryStore();
await store.set(dummyConversationId, dummyConversationState, Date.now() + expiresInMs);
await delay(expiresInMs * 2);
try {
await store.get(dummyConversationId);
assert.fail();
} catch (error: any) {
// Assert
assert.instanceOf(error, Error);
assert.notInstanceOf(error, AssertionError);
}
});
});
});
/* Testing Harness */
type MiddlewareArgs = AnyMiddlewareArgs & {
next: NextFn;
context: Context;
logger: Logger;
client: WebClient;
};
interface DummyContext<ConversationState> {
conversation?: ConversationState;
updateConversation?: (c: ConversationState, expiresAt?: number) => Promise<unknown>;
}
// Loading the system under test using overrides
async function importConversationStore(overrides: Override = {}): Promise<typeof import('./conversation-store')> {
return rewiremock.module(() => import('./conversation-store'), overrides);
}
// Composable overrides
function withGetTypeAndConversation(spy: SinonSpy): Override {
return {
'./helpers': {
getTypeAndConversation: spy,
},
};
}
// Fakes
interface FakeStore extends ConversationStore {
set: SinonSpy<Parameters<ConversationStore['set']>, ReturnType<ConversationStore['set']>>;
get: SinonSpy<Parameters<ConversationStore['get']>, ReturnType<ConversationStore['get']>>;
}
function createFakeStore(
getSpy: SinonSpy = sinon.fake.resolves(undefined),
setSpy: SinonSpy = sinon.fake.resolves({}),
): FakeStore {
return {
// NOTE (Nov 2019): We had to convert to 'unknown' first due to the following error:
// src/conversation-store.spec.ts:223:10 - error TS2352: Conversion of type 'SinonSpy<any[], any>' to
// type 'SinonSpy<[string, any, (number | undefined)?], Promise<unknown>>' may be a mistake because neither type
// sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
// Types of property 'firstCall' are incompatible.
// Type 'SinonSpyCall<any[], any>' is not comparable to type 'SinonSpyCall<[string, any, (number | undefined)?],
// Promise<unknown>>'.
// Type 'any[]' is not comparable to type '[string, any, (number | undefined)?]'.
// 223 set: setSpy as SinonSpy<Parameters<ConversationStore['set']>, ReturnType<ConversationStore['set']>>,
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set: setSpy as unknown as SinonSpy<Parameters<ConversationStore['set']>, ReturnType<ConversationStore['set']>>,
get: getSpy as unknown as SinonSpy<Parameters<ConversationStore['get']>, ReturnType<ConversationStore['get']>>,
};
}