Skip to content

Commit e0c13b2

Browse files
Fix observe options test
1 parent 8b340c1 commit e0c13b2

File tree

1 file changed

+104
-74
lines changed

1 file changed

+104
-74
lines changed

application/apps/rustcore/ts-bindings/spec/session.protocol.spec.ts

+104-74
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,77 @@ import * as proto from 'protocol';
1313
import * as $ from 'platform/types/observe';
1414
import * as convertor from '../src/util/convertor';
1515
import * as runners from './runners';
16+
import * as ty from '../src/protocol';
1617

1718
const config = readConfigurationFile().get().tests.protocol;
1819

20+
function deepEqualObj(a: any, b: any, depth = Infinity): boolean {
21+
if (depth < 1 || (typeof a !== 'object' && typeof b !== 'object')) {
22+
return a === b || (a == null && b == null);
23+
}
24+
if (a == null || b == null) {
25+
return a == null && b == null;
26+
}
27+
if (Array.isArray(a) && Array.isArray(b)) {
28+
if (a.length !== b.length) return false;
29+
return a.every((item, index) => deepEqualObj(item, b[index], depth - 1));
30+
}
31+
if (typeof a === 'object' && typeof b === 'object') {
32+
const keys1 = Object.keys(a);
33+
const keys2 = Object.keys(b);
34+
35+
if (keys1.length !== keys2.length) return false;
36+
if (!keys1.every((key) => keys2.includes(key))) return false;
37+
38+
return keys1.every((key) => deepEqualObj(a[key], b[key], depth - 1));
39+
}
40+
return a === b;
41+
}
42+
1943
describe('Protocol', function () {
2044
it(config.regular.list[1], function () {
2145
return runners.noSession(config.regular, 1, async (logger, done) => {
22-
{
23-
let origin = convertor.toObserveOptions({
46+
function check(origin: ty.ObserveOptions) {
47+
expect(
48+
deepEqualObj(
49+
proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin)),
50+
origin,
51+
),
52+
).toBe(true);
53+
}
54+
check(
55+
convertor.toObserveOptions({
2456
origin: { File: ['somefile', $.Types.File.FileType.Text, 'path_to_file'] },
2557
parser: { Text: null },
26-
});
27-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
28-
expect(origin).toEqual(decoded);
29-
}
30-
{
31-
let origin = convertor.toObserveOptions({
58+
}),
59+
);
60+
check(
61+
convertor.toObserveOptions({
3262
origin: {
3363
Stream: ['stream', { TCP: { bind_addr: '0.0.0.0' } }],
3464
},
3565
parser: { Text: null },
36-
});
37-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
38-
expect(origin).toEqual(decoded);
39-
}
40-
{
41-
let origin = convertor.toObserveOptions({
66+
}),
67+
);
68+
check(
69+
convertor.toObserveOptions({
4270
origin: {
4371
Stream: [
4472
'stream',
45-
{ Process: { command: 'command', cwd: 'cwd', envs: { one: 'one' } } },
73+
{
74+
Process: {
75+
command: 'command',
76+
cwd: 'cwd',
77+
envs: { one: 'one' },
78+
},
79+
},
4680
],
4781
},
4882
parser: { Text: null },
49-
});
50-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
51-
expect(origin).toEqual(decoded);
52-
}
53-
{
54-
let origin = convertor.toObserveOptions({
83+
}),
84+
);
85+
check(
86+
convertor.toObserveOptions({
5587
origin: {
5688
Concat: [
5789
['somefile1', $.Types.File.FileType.Text, 'path_to_file'],
@@ -60,13 +92,13 @@ describe('Protocol', function () {
6092
],
6193
},
6294
parser: { Text: null },
63-
});
64-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
65-
expect(origin).toEqual(decoded);
66-
}
67-
{
68-
let origin = convertor.toObserveOptions({
69-
origin: { File: ['somefile', $.Types.File.FileType.Binary, 'path_to_file'] },
95+
}),
96+
);
97+
check(
98+
convertor.toObserveOptions({
99+
origin: {
100+
File: ['somefile', $.Types.File.FileType.Binary, 'path_to_file'],
101+
},
70102
parser: {
71103
Dlt: {
72104
fibex_file_paths: ['path'],
@@ -75,13 +107,13 @@ describe('Protocol', function () {
75107
tz: 'zz',
76108
},
77109
},
78-
});
79-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
80-
// expect(origin).toEqual(decoded);
81-
}
82-
{
83-
let origin = convertor.toObserveOptions({
84-
origin: { File: ['somefile', $.Types.File.FileType.Binary, 'path_to_file'] },
110+
}),
111+
);
112+
check(
113+
convertor.toObserveOptions({
114+
origin: {
115+
File: ['somefile', $.Types.File.FileType.Binary, 'path_to_file'],
116+
},
85117
parser: {
86118
Dlt: {
87119
fibex_file_paths: [],
@@ -90,13 +122,13 @@ describe('Protocol', function () {
90122
tz: 'zz',
91123
},
92124
},
93-
});
94-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
95-
// expect(origin).toEqual(decoded);
96-
}
97-
{
98-
let origin = convertor.toObserveOptions({
99-
origin: { File: ['somefile', $.Types.File.FileType.Binary, 'path_to_file'] },
125+
}),
126+
);
127+
check(
128+
convertor.toObserveOptions({
129+
origin: {
130+
File: ['somefile', $.Types.File.FileType.Binary, 'path_to_file'],
131+
},
100132
parser: {
101133
Dlt: {
102134
fibex_file_paths: undefined,
@@ -105,13 +137,13 @@ describe('Protocol', function () {
105137
tz: 'zz',
106138
},
107139
},
108-
});
109-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
110-
// expect(origin).toEqual(decoded);
111-
}
112-
{
113-
let origin = convertor.toObserveOptions({
114-
origin: { File: ['somefile', $.Types.File.FileType.Binary, 'path_to_file'] },
140+
}),
141+
);
142+
check(
143+
convertor.toObserveOptions({
144+
origin: {
145+
File: ['somefile', $.Types.File.FileType.Binary, 'path_to_file'],
146+
},
115147
parser: {
116148
Dlt: {
117149
fibex_file_paths: ['path'],
@@ -127,46 +159,44 @@ describe('Protocol', function () {
127159
tz: 'zz',
128160
},
129161
},
130-
});
131-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
132-
// expect(origin).toEqual(decoded);
133-
}
134-
{
135-
let origin = convertor.toObserveOptions({
136-
origin: { File: ['somefile', $.Types.File.FileType.PcapNG, 'path_to_file'] },
162+
}),
163+
);
164+
check(
165+
convertor.toObserveOptions({
166+
origin: {
167+
File: ['somefile', $.Types.File.FileType.PcapNG, 'path_to_file'],
168+
},
137169
parser: {
138170
SomeIp: {
139171
fibex_file_paths: ['path'],
140172
},
141173
},
142-
});
143-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
144-
// expect(origin).toEqual(decoded);
145-
}
146-
{
147-
let origin = convertor.toObserveOptions({
148-
origin: { File: ['somefile', $.Types.File.FileType.PcapNG, 'path_to_file'] },
174+
}),
175+
);
176+
check(
177+
convertor.toObserveOptions({
178+
origin: {
179+
File: ['somefile', $.Types.File.FileType.PcapNG, 'path_to_file'],
180+
},
149181
parser: {
150182
SomeIp: {
151183
fibex_file_paths: [],
152184
},
153185
},
154-
});
155-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
156-
// expect(origin).toEqual(decoded);
157-
}
158-
{
159-
let origin = convertor.toObserveOptions({
160-
origin: { File: ['somefile', $.Types.File.FileType.PcapNG, 'path_to_file'] },
186+
}),
187+
);
188+
check(
189+
convertor.toObserveOptions({
190+
origin: {
191+
File: ['somefile', $.Types.File.FileType.PcapNG, 'path_to_file'],
192+
},
161193
parser: {
162194
SomeIp: {
163195
fibex_file_paths: undefined,
164196
},
165197
},
166-
});
167-
let decoded = proto.ObserveOptions.decode(proto.ObserveOptions.encode(origin));
168-
// expect(origin).toEqual(decoded);
169-
}
198+
}),
199+
);
170200
finish(undefined, done);
171201
});
172202
});

0 commit comments

Comments
 (0)