-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathlog-parser-service.ts
130 lines (111 loc) · 4.6 KB
/
log-parser-service.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
import { assert } from "chai";
import { CONNECTED_STATUS, DEVICE_LOG_EVENT_NAME } from "../../lib/common/constants";
import { LogParserService } from "../../lib/services/log-parser-service";
import { DevicePlatformsConstants } from "../../lib/common/mobile/device-platforms-constants";
import { IOSDebuggerPortService } from "../../lib/services/ios-debugger-port-service";
import { EventEmitter } from "events";
import { Yok } from "../../lib/common/yok";
const appId = "org.nativescript.test";
const deviceId = "fbece8e562ac63749a1018a9f1ea57614c5c953a";
const device = <Mobile.IDevice>{
deviceInfo: {
identifier: deviceId,
status: CONNECTED_STATUS,
platform: "ios"
}
};
class DeveiceLogProviderMock extends EventEmitter {
}
function createTestInjector() {
const injector = new Yok();
injector.register("previewAppLogProvider", { on: () => ({}) });
injector.register("deviceLogProvider", DeveiceLogProviderMock);
injector.register("previewSdkService", {
on: () => ({})
});
injector.register("devicePlatformsConstants", DevicePlatformsConstants);
injector.register("logParserService", LogParserService);
injector.register("devicesService", {
getDeviceByIdentifier: () => {
return device;
}
});
injector.register("errors", {});
return injector;
}
function getDebuggerPortMessage(port: number) {
return `NativeScript debugger has opened inspector socket on port ${port} for ${appId}.`;
}
describe("iOSLogParserService", () => {
let injector: IInjector, logParserService: LogParserService, deviceLogProvider: Mobile.IDeviceLogProvider;
beforeEach(() => {
injector = createTestInjector();
logParserService = injector.resolve("logParserService");
deviceLogProvider = injector.resolve("deviceLogProvider");
});
function emitDeviceLog(message: string) {
deviceLogProvider.emit(DEVICE_LOG_EVENT_NAME, message, device.deviceInfo.identifier);
}
function attachOnDebuggerFoundEvent(): IIOSDebuggerPortData[] {
const receivedData: any[] = [];
logParserService.addParseRule({
handler: (matches: RegExpMatchArray) => {
const data = {
port: parseInt(matches[1]),
appId: matches[2],
deviceId
};
receivedData.push(data);
},
regex: IOSDebuggerPortService.DEBUG_PORT_LOG_REGEX,
name: "testDebugPort"
});
return receivedData;
}
describe("addParseRule", () => {
it(`should execute handler if regex matches`, async () => {
const emittedMessagesCount = 1;
const data = attachOnDebuggerFoundEvent();
emitDeviceLog("test message");
emitDeviceLog(getDebuggerPortMessage(18181));
assert.deepEqual(data.length, emittedMessagesCount);
assert.deepEqual(data[0], { port: 18181, deviceId: deviceId, appId: appId });
});
it("should call handler for all mactches in order for same matches", async () => {
const emittedMessagesCount = 5;
const data = attachOnDebuggerFoundEvent();
emitDeviceLog(getDebuggerPortMessage(18181));
emitDeviceLog(getDebuggerPortMessage(18181));
emitDeviceLog(getDebuggerPortMessage(18181));
emitDeviceLog(getDebuggerPortMessage(18181));
emitDeviceLog(getDebuggerPortMessage(64087));
assert.deepEqual(data.length, emittedMessagesCount);
assert.deepEqual(data[0], { port: 18181, deviceId: deviceId, appId: appId });
assert.deepEqual(data[1], { port: 18181, deviceId: deviceId, appId: appId });
assert.deepEqual(data[2], { port: 18181, deviceId: deviceId, appId: appId });
assert.deepEqual(data[3], { port: 18181, deviceId: deviceId, appId: appId });
assert.deepEqual(data[4], { port: 64087, deviceId: deviceId, appId: appId });
});
it("should call handler for all matches in order for different matches", async () => {
const emittedMessagesCount = 5;
const data = attachOnDebuggerFoundEvent();
emitDeviceLog(getDebuggerPortMessage(45898));
emitDeviceLog(getDebuggerPortMessage(1809));
emitDeviceLog(getDebuggerPortMessage(65072));
emitDeviceLog(getDebuggerPortMessage(12345));
emitDeviceLog(getDebuggerPortMessage(18181));
assert.deepEqual(data.length, emittedMessagesCount);
assert.deepEqual(data[0], { port: 45898, deviceId: deviceId, appId: appId });
assert.deepEqual(data[1], { port: 1809, deviceId: deviceId, appId: appId });
assert.deepEqual(data[2], { port: 65072, deviceId: deviceId, appId: appId });
assert.deepEqual(data[3], { port: 12345, deviceId: deviceId, appId: appId });
assert.deepEqual(data[4], { port: 18181, deviceId: deviceId, appId: appId });
});
it(`should not execute handler if no match`, async () => {
const data = attachOnDebuggerFoundEvent();
emitDeviceLog("some test message");
emitDeviceLog("another test message");
assert.equal(data.length, 0);
});
});
});