Skip to content

Commit 98f5f54

Browse files
fix(directline): add new changes from master
1 parent bf10377 commit 98f5f54

8 files changed

Lines changed: 211 additions & 137 deletions

File tree

.babelrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ module.exports = {
3737
'@babel/preset-typescript'
3838
],
3939
sourceMaps: 'inline'
40-
};
40+
};

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515

1616
## [Unreleased]
1717

18+
### Fixed
19+
20+
- Reverting PR [#171](https://github.com/Microsoft/BotFramework-DirectLineJS/pull/171) and PR [#172](https://github.com/Microsoft/BotFramework-DirectLineJS/pull/172), which caused infinite loop of reconnections, by [@compulim](https://github.com/compulim) in PR [#240](https://github.com/Microsoft/BotFramework-DirectLineJS/pull/240)
21+
22+
## [0.11.5] - 2019-09-30
23+
1824
### Changed
1925
- Update DirectLine to rxjs v6 pipeable operators, PR [#102](https://github.com/Microsoft/BotFramework-DirectLineJS/pull/102/files)
2026

@@ -64,15 +70,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6470
- [`webpack@4.39.3`](https://npmjs.com/package/webpack)
6571
- [`webpack-cli@3.3.8`](https://npmjs.com/package/webpack-cli)
6672

67-
<<<<<<< HEAD
73+
6874
### Added
6975
- Fix [#235](https://github.com/Microsoft/BotFramework-DirectLineJS/issues/235). Added metadata when uploading attachments, including `thumbnailUrl`, by [@compulim](https://github.com/compulim), in PR [#236](https://github.com/Microsoft/BotFramework-DirectLineJS/pull/236)
7076

7177
### Fixed
7278
- Avoid posting an error on intentional end, by [@orgads](https://github.com/orgads) in PR [#172](https://github.com/Microsoft/BotFramework-DirectLineJS/pull/172)
7379
- Surface Web Socket errors, by [@orgads](https://github.com/orgads) in PR [#171](https://github.com/Microsoft/BotFramework-DirectLineJS/pull/171)
74-
=======
75-
>>>>>>> fix(directline): add new changes from master
7680

7781
## [0.11.4] - 2019-03-04
7882
### Changed

__tests__/happy.uploadAttachments.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,4 @@ describe('Happy path', () => {
113113
]);
114114
});
115115
});
116-
});
116+
});

src/directLine.mock.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import * as DirectLineExport from "./directLine";
2-
import { TestScheduler, Observable } from "rxjs";
3-
import { AjaxCreationMethod, AjaxRequest, AjaxResponse } from "rxjs/observable/dom/AjaxObservable";
1+
import * as DirectLineExport from './directLine';
2+
import { Observable } from 'rxjs';
3+
import { TestScheduler } from 'rxjs/testing';
44
import { URL, URLSearchParams } from 'url';
5+
import { AjaxRequest, AjaxResponse } from 'rxjs/ajax';
6+
import { AjaxCreationMethod } from 'rxjs/internal/observable/dom/AjaxObservable';
57

68
// MOCK helpers
79

@@ -148,8 +150,7 @@ export const mockAjax = (server: Server, customAjax?: ajaxType): AjaxCreationMet
148150
}
149151

150152
return response as AjaxResponse;
151-
}
152-
else if (parts.length === 5) {
153+
} else if (parts.length === 5) {
153154
const responseToken = tokenResponse(server, urlOrRequest);
154155
if (responseToken !== null) {
155156
return responseToken;
@@ -179,8 +180,7 @@ export const mockAjax = (server: Server, customAjax?: ajaxType): AjaxCreationMet
179180
try {
180181
subscriber.next(jax(urlOrRequest));
181182
subscriber.complete();
182-
}
183-
catch (error) {
183+
} catch (error) {
184184
subscriber.error(error);
185185
}
186186
});
@@ -210,6 +210,11 @@ type EventHandler<E extends Event> = (this: WebSocket, ev: E) => any;
210210

211211
export const mockWebSocket = (server: Server): WebSocketConstructor =>
212212
class MockWebSocket implements WebSocket, ActivitySocket {
213+
static CLOSED = WebSocket.CLOSED;
214+
static CLOSING = WebSocket.CLOSING;
215+
static CONNECTING = WebSocket.CONNECTING;
216+
static OPEN = WebSocket.OPEN;
217+
213218
constructor(url: string, protocols?: string | string[]) {
214219

215220
server.scheduler.schedule(() => {
@@ -226,21 +231,6 @@ export const mockWebSocket = (server: Server): WebSocketConstructor =>
226231
});
227232
}
228233

229-
play(start: number, after: number) {
230-
231-
const { conversation: { history } } = server;
232-
const activities = history.slice(start, after);
233-
const watermark = history.length.toString();
234-
const activityGroup: DirectLineExport.ActivityGroup = {
235-
activities,
236-
watermark,
237-
}
238-
239-
const message = new MessageEvent('type', { data: JSON.stringify(activityGroup) });
240-
241-
this.onmessage(message);
242-
}
243-
244234
binaryType: BinaryType = 'arraybuffer';
245235
readonly bufferedAmount: number = 0;
246236
readonly extensions: string = '';
@@ -257,6 +247,21 @@ export const mockWebSocket = (server: Server): WebSocketConstructor =>
257247
onmessage: EventHandler<MessageEvent>;
258248
onopen: EventHandler<Event>;
259249

250+
play(start: number, after: number) {
251+
252+
const { conversation: { history } } = server;
253+
const activities = history.slice(start, after);
254+
const watermark = history.length.toString();
255+
const activityGroup: DirectLineExport.ActivityGroup = {
256+
activities,
257+
watermark,
258+
}
259+
260+
const message = new MessageEvent('type', { data: JSON.stringify(activityGroup) });
261+
262+
this.onmessage(message);
263+
}
264+
260265
close(code?: number, reason?: string): void {
261266
this.readyState = WebSocket.CLOSING;
262267
this.onclose(new CloseEvent('close'))
@@ -270,11 +275,6 @@ export const mockWebSocket = (server: Server): WebSocketConstructor =>
270275
addEventListener() { throw new Error(); }
271276
removeEventListener() { throw new Error(); }
272277
dispatchEvent(): boolean { throw new Error(); }
273-
274-
static CLOSED = WebSocket.CLOSED;
275-
static CLOSING = WebSocket.CLOSING;
276-
static CONNECTING = WebSocket.CONNECTING;
277-
static OPEN = WebSocket.OPEN;
278278
};
279279

280280
// MOCK services (top-level aggregation of all mocks)

src/directLine.test.ts

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import * as DirectLineExport from "./directLine";
22
import * as DirectLineMock from './directLine.mock';
3-
import { TestScheduler, Observable, Subscription, AjaxResponse } from "rxjs";
43
// @ts-ignore
5-
import { version } from "../package.json";
4+
import { version as packageVersion } from "../package.json";
5+
import { Observable, Subscription, timer, empty, scheduled } from "rxjs";
6+
import { TestScheduler } from "rxjs/testing";
7+
import { AjaxResponse } from "rxjs/ajax";
8+
import { catchError, observeOn } from "rxjs/operators";
69

710
declare var process: {
811
arch: string;
@@ -33,7 +36,7 @@ test("#setConnectionStatusFallback", () => {
3336
});
3437

3538
describe("#commonHeaders", () => {
36-
const botAgent = `DirectLine/3.0 (directlinejs; custom-bot-agent ${version})`;
39+
const botAgent = `DirectLine/3.0 (directlinejs; custom-bot-agent ${packageVersion})`;
3740
let botConnection;
3841

3942
beforeEach(() => {
@@ -78,16 +81,14 @@ describe('MockSuite', () => {
7881
const result = iterator.next();
7982
if (result.done === true) {
8083
subscriber.complete();
81-
}
82-
else {
84+
} else {
8385
inner = result.value.subscribe(
8486
value => subscriber.next(value),
8587
error => subscriber.error(error),
8688
pump
8789
);
8890
}
89-
}
90-
catch (error) {
91+
} catch (error) {
9192
subscriber.error(error);
9293
}
9394
};
@@ -108,7 +109,7 @@ describe('MockSuite', () => {
108109
let directline: DirectLineExport.DirectLine;
109110

110111
beforeEach(() => {
111-
scheduler = new TestScheduler((actual, expected) => expect(expected).toBe(actual));
112+
scheduler = new TestScheduler((actual, expectedValue) => expect(expectedValue).toBe(actual));
112113
scheduler.maxFrames = 60 * 1000;
113114
server = DirectLineMock.mockServer(scheduler);
114115
services = DirectLineMock.mockServices(server, scheduler);
@@ -132,14 +133,14 @@ describe('MockSuite', () => {
132133
// arrange
133134

134135
const scenario = function* (): IterableIterator<Observable<unknown>> {
135-
yield Observable.timer(200, scheduler);
136+
yield timer(200, scheduler);
136137
yield directline.postActivity(expected.x);
137-
yield Observable.timer(200, scheduler);
138+
yield timer(200, scheduler);
138139
yield directline.postActivity(expected.y);
139-
yield Observable.timer(200, scheduler);
140+
yield timer(200, scheduler);
140141
};
141142

142-
subscriptions.push(lazyConcat(scenario()).observeOn(scheduler).subscribe());
143+
subscriptions.push(lazyConcat(scenario()).pipe(observeOn(scheduler)).subscribe());
143144

144145
const actual: Array<DirectLineExport.Activity> = [];
145146
subscriptions.push(directline.activity$.subscribe(a => actual.push(a)));
@@ -157,15 +158,15 @@ describe('MockSuite', () => {
157158
// arrange
158159

159160
const scenario = function* (): IterableIterator<Observable<unknown>> {
160-
yield Observable.timer(200, scheduler);
161+
yield timer(200, scheduler);
161162
yield directline.postActivity(expected.x);
162163
DirectLineMock.injectClose(server);
163-
yield Observable.timer(200, scheduler);
164+
yield timer(200, scheduler);
164165
yield directline.postActivity(expected.y);
165-
yield Observable.timer(200, scheduler);
166+
yield timer(200, scheduler);
166167
};
167168

168-
subscriptions.push(lazyConcat(scenario()).observeOn(scheduler).subscribe());
169+
subscriptions.push(lazyConcat(scenario()).pipe(observeOn(scheduler)).subscribe());
169170

170171
const actual: Array<DirectLineExport.Activity> = [];
171172
subscriptions.push(directline.activity$.subscribe(a => actual.push(a)));
@@ -180,31 +181,30 @@ describe('MockSuite', () => {
180181
});
181182

182183
test('BotAgentWithMocks', () => {
183-
const expected: string = `DirectLine/3.0 (directlinejs ${version})`;
184+
const expectedValue: string = `DirectLine/3.0 (directlinejs ${packageVersion})`;
184185

185-
//@ts-ignore
186+
// @ts-ignore
186187
const actual: string = directline.commonHeaders()["x-ms-bot-agent"];
187-
expect(actual).toStrictEqual(expected)
188+
expect(actual).toStrictEqual(expectedValue)
188189
});
189190

190191
test('RetryAfterHeader', () => {
191192
services.ajax = DirectLineMock.mockAjax(server, (urlOrRequest) => {
192193

193-
if(typeof urlOrRequest === 'string'){
194+
if (typeof urlOrRequest === 'string') {
194195
throw new Error();
195196
}
196197

197-
if(urlOrRequest.url && urlOrRequest.url.indexOf(server.conversation.conversationId)>0){
198+
if (urlOrRequest.url && urlOrRequest.url.indexOf(server.conversation.conversationId) > 0) {
198199
const response: Partial<AjaxResponse> = {
199200
status: 429,
200-
xhr:{
201+
xhr: {
201202
getResponseHeader: (name) => "10"
202203
} as XMLHttpRequest
203204
};
204205
const error = new Error('Ajax Error');
205206
throw Object.assign(error, response);
206-
}
207-
else if(urlOrRequest.url && urlOrRequest.url.indexOf('/conversations') > 0){
207+
} else if (urlOrRequest.url && urlOrRequest.url.indexOf('/conversations') > 0) {
208208
// start conversation
209209
const response: Partial<AjaxResponse> = {
210210
response: server.conversation,
@@ -222,17 +222,16 @@ describe('MockSuite', () => {
222222
let startTime: number;
223223
let endTime: number;
224224
const scenario = function* (): IterableIterator<Observable<unknown>> {
225-
yield Observable.timer(200, scheduler);
225+
yield timer(200, scheduler);
226226
startTime = scheduler.now();
227227
yield directline.postActivity(expected.x);
228228
};
229229

230230
let actualError: Error;
231-
try{
232-
subscriptions.push(lazyConcat(scenario()).observeOn(scheduler).subscribe());
233-
scheduler.flush();
234-
}
235-
catch(error){
231+
try {
232+
subscriptions.push(lazyConcat(scenario()).pipe(observeOn(scheduler)).subscribe());
233+
scheduler.flush();
234+
} catch (error) {
236235
actualError = error;
237236
endTime = scheduler.now();
238237
}
@@ -243,22 +242,21 @@ describe('MockSuite', () => {
243242
});
244243

245244
test('SendDebugHeader', () => {
246-
let expectedBotId:string;
245+
let expectedBotId: string;
247246
const actual = 'botid';
248247
services.ajax = DirectLineMock.mockAjax(server, (urlOrRequest) => {
249-
if(typeof urlOrRequest === 'string'){
248+
if (typeof urlOrRequest === 'string') {
250249
throw new Error();
251250
}
252251

253-
if(urlOrRequest.url && urlOrRequest.url.indexOf(server.conversation.conversationId)>0){
252+
if (urlOrRequest.url && urlOrRequest.url.indexOf(server.conversation.conversationId) > 0) {
254253
const response: Partial<AjaxResponse> = {
255-
response: {id:'blah'},
254+
response: {id: 'blah'},
256255
status: 200
257256
};
258257
expectedBotId = urlOrRequest.headers['x-ms-botid'];
259258
return response as AjaxResponse;
260-
}
261-
else if(urlOrRequest.url && urlOrRequest.url.indexOf('/conversations') > 0){
259+
} else if (urlOrRequest.url && urlOrRequest.url.indexOf('/conversations') > 0) {
262260
// start conversation
263261
const response: Partial<AjaxResponse> = {
264262
response: server.conversation,
@@ -273,14 +271,14 @@ describe('MockSuite', () => {
273271
});
274272
directline = new DirectLineExport.DirectLine(services);
275273
const scenario = function* (): IterableIterator<Observable<unknown>> {
276-
yield Observable.timer(200, scheduler);
277-
yield directline.postActivity(expected.x).catch(() => Observable.empty(scheduler));
274+
yield timer(200, scheduler);
275+
yield directline.postActivity(expected.x).pipe(catchError(() => scheduled([], scheduler)));
278276
};
279277

280278
// lack of subscribe arguments means that the empty subscriber is used
281279
// the empty subscriber will propagate observable errors on the JS call stack
282280
// within the scheduler notification action handling loop because of the observeOn
283-
subscriptions.push(lazyConcat(scenario()).observeOn(scheduler).subscribe());
281+
subscriptions.push(lazyConcat(scenario()).pipe(observeOn(scheduler)).subscribe());
284282
scheduler.flush();
285283

286284
expect(expectedBotId).toBe(actual);

0 commit comments

Comments
 (0)