-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-log-parser-service.ts
152 lines (126 loc) · 5.68 KB
/
ios-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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { assert } from "chai";
import { CONNECTED_STATUS, DEBUGGER_PORT_FOUND_EVENT_NAME, DEVICE_LOG_EVENT_NAME } from "../../lib/common/constants";
import { IOSDeviceOperations } from "../../lib/common/mobile/ios/device/ios-device-operations";
import { IOSLogParserService } from "../../lib/services/ios-log-parser-service";
import { IOSSimulatorLogProvider } from "../../lib/common/mobile/ios/simulator/ios-simulator-log-provider";
import { Yok } from "../../lib/common/yok";
const appId = "org.nativescript.test";
const deviceId = "fbece8e562ac63749a1018a9f1ea57614c5c953a";
const device = <Mobile.IDevice>{
deviceInfo: {
identifier: deviceId,
status: CONNECTED_STATUS
}
};
function createTestInjector() {
const injector = new Yok();
injector.register("deviceLogProvider", {
setProjectNameForDevice: () => ({})
});
injector.register("iosDeviceOperations", IOSDeviceOperations);
injector.register("iOSLogParserService", IOSLogParserService);
injector.register("iOSProjectService", {
getFrameworkVersion: () => "4.1.0"
});
injector.register("iOSSimResolver", () => ({}));
injector.register("iOSSimulatorLogProvider", IOSSimulatorLogProvider);
injector.register("logger", {
trace: () => ({})
});
injector.register("processService", {
attachToProcessExitSignals: () => ({})
});
injector.register("projectDataService", {
getProjectData: (projectDir: string) => ({
projectName: "test",
projectId: appId
})
});
return injector;
}
function getDebuggerPortMessage(port: number) {
return `NativeScript debugger has opened inspector socket on port ${port} for ${appId}.`;
}
describe("iOSLogParserService", () => {
let injector: IInjector, iOSLogParserService: IIOSLogParserService, iosDeviceOperations: IIOSDeviceOperations;
beforeEach(() => {
injector = createTestInjector();
iOSLogParserService = injector.resolve("iOSLogParserService");
iosDeviceOperations = injector.resolve("iosDeviceOperations");
iosDeviceOperations.startDeviceLog = () => ({});
});
function emitDeviceLog(message: string) {
iosDeviceOperations.emit(DEVICE_LOG_EVENT_NAME, {
deviceId: device.deviceInfo.identifier,
message: message
});
}
function attachOnDebuggerFoundEvent(emittedMessagesCount: number): Promise<IIOSDebuggerPortData[]> {
const receivedData: IIOSDebuggerPortData[] = [];
return new Promise((resolve, reject) => {
iOSLogParserService.on(DEBUGGER_PORT_FOUND_EVENT_NAME, (data: IIOSDebuggerPortData) => {
receivedData.push(data);
if (receivedData.length === emittedMessagesCount) {
resolve(receivedData);
}
});
});
}
const mockProjectNameObj: IProjectName = {
projectName: "projectName"
};
describe("startLookingForDebuggerPort", () => {
it(`should emit ${DEBUGGER_PORT_FOUND_EVENT_NAME} event`, async () => {
const emittedMessagesCount = 1;
const promise = attachOnDebuggerFoundEvent(emittedMessagesCount);
await iOSLogParserService.startParsingLog(device, mockProjectNameObj);
emitDeviceLog("test message");
emitDeviceLog(getDebuggerPortMessage(18181));
const data = await promise;
assert.deepEqual(data.length, emittedMessagesCount);
assert.deepEqual(data[0], { port: 18181, deviceId: deviceId, appId: appId });
});
it("should receive all log data when cache logs are emitted in case when default port is not available", async () => {
const emittedMessagesCount = 5;
const promise = attachOnDebuggerFoundEvent(emittedMessagesCount);
await iOSLogParserService.startParsingLog(device, mockProjectNameObj);
emitDeviceLog(getDebuggerPortMessage(18181));
emitDeviceLog(getDebuggerPortMessage(18181));
emitDeviceLog(getDebuggerPortMessage(18181));
emitDeviceLog(getDebuggerPortMessage(18181));
emitDeviceLog(getDebuggerPortMessage(64087));
const data = await promise;
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 receive all log data when cache logs are emitted in case when default port is available", async () => {
const emittedMessagesCount = 5;
const promise = attachOnDebuggerFoundEvent(emittedMessagesCount);
await iOSLogParserService.startParsingLog(device, mockProjectNameObj);
emitDeviceLog(getDebuggerPortMessage(45898));
emitDeviceLog(getDebuggerPortMessage(1809));
emitDeviceLog(getDebuggerPortMessage(65072));
emitDeviceLog(getDebuggerPortMessage(12345));
emitDeviceLog(getDebuggerPortMessage(18181));
const data = await promise;
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 receive ${DEBUGGER_PORT_FOUND_EVENT_NAME} event when debugger port message is not emitted`, async () => {
let isDebuggedPortFound = false;
iOSLogParserService.on(DEBUGGER_PORT_FOUND_EVENT_NAME, (data: IIOSDebuggerPortData) => isDebuggedPortFound = true);
await iOSLogParserService.startParsingLog(device, mockProjectNameObj);
emitDeviceLog("some test message");
emitDeviceLog("another test message");
assert.isFalse(isDebuggedPortFound);
});
});
});