forked from googleapis/cloud-debug-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-v8debugapi.ts
2330 lines (2176 loc) · 73.3 KB
/
test-v8debugapi.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {ResolvedDebugAgentConfig} from '../src/agent/config';
import {Debuglet} from '../src/agent/debuglet';
import {DebugApi} from '../src/agent/v8/debugapi';
import consoleLogLevel = require('console-log-level');
import * as stackdriver from '../src/types/stackdriver';
import {MockLogger} from './mock-logger';
// TODO(dominickramer): Have this actually implement Breakpoint
const breakpointInFoo: stackdriver.Breakpoint = {
id: 'fake-id-123',
// TODO(dominickramer): Determine if we should be restricting to only the
// build directory.
location: {path: 'build/test/test-v8debugapi-code.js', line: 5},
} as stackdriver.Breakpoint;
const MAX_INT = 2147483647; // Max signed int32.
import * as assert from 'assert';
import {after, afterEach, before, beforeEach, describe, it} from 'mocha';
import * as extend from 'extend';
import * as debugapi from '../src/agent/v8/debugapi';
import {defaultConfig} from '../src/agent/config';
import {StatusMessage} from '../src/client/stackdriver/status-message';
import {InspectorDebugApi} from '../src/agent/v8/inspector-debugapi';
import * as scanner from '../src/agent/io/scanner';
import * as SourceMapper from '../src/agent/io/sourcemapper';
import * as path from 'path';
import * as utils from '../src/agent/util/utils';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const code = require('./test-v8debugapi-code.js');
import {dist} from './test-v8debugapi-ts-code';
function stateIsClean(api: DebugApi): boolean {
assert.strictEqual(
api.numBreakpoints_(),
0,
'there should be no breakpoints active'
);
assert.strictEqual(
api.numListeners_(),
0,
'there should be no listeners active'
);
return true;
}
function validateVariable(variable: stackdriver.Variable | null): void {
assert.ok(variable);
if (variable) {
if (variable.name) {
assert.strictEqual(typeof variable.name, 'string');
}
if (variable.value) {
assert.strictEqual(typeof variable.value, 'string');
}
if (variable.type) {
assert.strictEqual(typeof variable.type, 'string');
}
if (variable.members) {
variable.members.forEach(validateVariable);
}
if (variable.varTableIndex) {
assert.ok(
Number.isInteger(variable.varTableIndex) &&
variable.varTableIndex >= 0 &&
variable.varTableIndex <= MAX_INT
);
}
}
}
function validateSourceLocation(location: stackdriver.SourceLocation): void {
if (location.path) {
assert.strictEqual(typeof location.path, 'string');
}
if (location.line) {
assert.ok(
Number.isInteger(location.line) &&
location.line >= 1 &&
location.line <= MAX_INT
);
}
}
function validateStackFrame(frame: stackdriver.StackFrame): void {
if (frame['function']) {
assert.strictEqual(typeof frame['function'], 'string');
}
if (frame.location) {
validateSourceLocation(frame.location);
}
if (frame.arguments) {
frame.arguments.forEach(validateVariable);
}
if (frame.locals) {
frame.locals.forEach(validateVariable);
}
}
function validateBreakpoint(breakpoint: stackdriver.Breakpoint): void {
if (!breakpoint) {
return;
}
if (breakpoint.variableTable) {
breakpoint.variableTable.forEach(validateVariable);
}
if (breakpoint.evaluatedExpressions) {
breakpoint.evaluatedExpressions.forEach(validateVariable);
}
if (breakpoint.stackFrames) {
breakpoint.stackFrames.forEach(validateStackFrame);
}
}
describe('debugapi selection', () => {
const config: ResolvedDebugAgentConfig = extend({}, defaultConfig, {
workingDirectory: __dirname,
forceNewAgent_: true,
});
const logger = consoleLogLevel({
level: Debuglet.logLevelToName(config.logLevel),
});
it('should use the correct debugapi and have appropriate warning', done => {
let api: DebugApi;
scanner
.scan(config.workingDirectory, /.js$|.js.map$/)
.then(async fileStats => {
assert.strictEqual(fileStats.errors().size, 0);
const jsStats = fileStats.selectStats(/.js$/);
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
const mapper = await SourceMapper.create(mapFiles, logger);
// TODO(dominickramer): Handle the case when mapper is undefined.
// TODO(dominickramer): Handle the case when v8debugapi.create
// returns null
api = debugapi.create(
logger,
config,
jsStats,
mapper as SourceMapper.SourceMapper
) as DebugApi;
assert.ok(api instanceof InspectorDebugApi);
done();
});
});
});
const describeFn = utils.satisfies(process.version, '>=10')
? describe
: describe.skip;
describeFn('debugapi selection on Node >=10', () => {
const config: ResolvedDebugAgentConfig = extend({}, defaultConfig, {
workingDirectory: __dirname,
forceNewAgent_: true,
});
const logger = consoleLogLevel({
level: Debuglet.logLevelToName(config.logLevel),
});
it('should always use the inspector api', done => {
let api: DebugApi;
scanner
.scan(config.workingDirectory, /.js$|.js.map$/)
.then(async fileStats => {
assert.strictEqual(fileStats.errors().size, 0);
const jsStats = fileStats.selectStats(/.js$/);
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
const mapper = await SourceMapper.create(mapFiles, logger);
assert(mapper);
api = debugapi.create(logger, config, jsStats, mapper!);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const inspectorapi = require('../src/agent/v8/inspector-debugapi');
assert.ok(api instanceof inspectorapi.InspectorDebugApi);
done();
});
});
});
describe('v8debugapi', () => {
const config: ResolvedDebugAgentConfig = extend({}, defaultConfig, {
workingDirectory: __dirname,
forceNewAgent_: true,
javascriptFileExtensions: ['.js', '.jsz'],
});
const logger = new MockLogger();
let api: DebugApi;
beforeEach(done => {
if (!api) {
scanner
.scan(config.workingDirectory, /.js$|.jsz$|.js.map$/)
.then(async fileStats => {
assert.strictEqual(fileStats.errors().size, 0);
const jsStats = fileStats.selectStats(/.js$|.jsz$/);
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
const mapper = await SourceMapper.create(mapFiles, logger);
// TODO(dominickramer): Handle the case when mapper is undefined.
// TODO(dominickramer): Handle the case when v8debugapi.create
// returns null
api = debugapi.create(
logger,
config,
jsStats,
mapper as SourceMapper.SourceMapper
) as DebugApi;
assert.ok(api, 'should be able to create the api');
// monkey-patch wait to add validation of the breakpoints.
const origWait = api.wait.bind(api);
api.wait = (bp, callback) => {
origWait(bp, (err2?: Error) => {
validateBreakpoint(bp);
callback(err2);
});
};
done();
});
} else {
assert(stateIsClean(api));
done();
}
});
afterEach(() => {
logger.clear();
assert(stateIsClean(api));
});
it('should be able to set and remove breakpoints', done => {
// clone a clean breakpointInFoo
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: breakpointInFoo.id,
location: breakpointInFoo.location,
} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
assert.strictEqual(api.numBreakpoints_(), 1);
api.clear(bp, err2 => {
assert.ifError(err2);
done();
});
});
});
it('should accept breakpoint with ids 0 as a valid breakpoint', done => {
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: 0,
location: breakpointInFoo.location,
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.clear(bp, err2 => {
assert.ifError(err2);
done();
});
});
});
it('should permit breakpoints on js files with non-standard extensions', done => {
require('./fixtures/hello.jsz');
const bp: stackdriver.Breakpoint = {
id: 0,
location: {line: 1, path: path.join('fixtures', 'hello.jsz')},
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.clear(bp, err2 => {
assert.ifError(err2);
done();
});
});
});
it('should set error for breakpoint in non-js files', done => {
require('./fixtures/key-bad.json');
// TODO(dominickramer): Have this actually implement Breakpoint
const bp = {
id: 0,
location: {line: 1, path: path.join('fixtures', 'key-bad.json')},
} as {} as stackdriver.Breakpoint;
api.set(bp, err => {
assert.ok(err, 'should return an error');
assert.ok(bp.status);
assert.ok(bp.status instanceof StatusMessage);
assert.strictEqual(bp.status!.refersTo, 'BREAKPOINT_SOURCE_LOCATION');
assert.ok(bp.status!.isError);
done();
});
});
it('should disambiguate incorrect path if filename is unique', done => {
require('./fixtures/foo.js');
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: 0,
location: {line: 1, path: path.join(path.sep, 'test', 'foo.js')},
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.clear(bp, err2 => {
assert.ifError(err2);
done();
});
});
});
it('should disambiguate incorrect path if partial path is unique', done => {
require('./fixtures/foo.js');
// hello.js is not unique but a/hello.js is.
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: 0,
location: {line: 1, path: path.join(path.sep, 'Server', 'a', 'hello.js')},
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.clear(bp, err2 => {
assert.ifError(err2);
done();
});
});
});
describe('invalid breakpoints', () => {
// TODO(dominickramer): Have this actually be a list of Breakpoints
const badBreakpoints: stackdriver.Breakpoint[] = [
{} as {} as stackdriver.Breakpoint,
{id: 'with no location'} as {} as stackdriver.Breakpoint,
{id: 'with bad location', location: {}} as {} as stackdriver.Breakpoint,
{
id: 'with no path',
location: {line: 4},
} as {} as stackdriver.Breakpoint,
{
id: 'with no line',
location: {path: 'foo.js'},
} as {} as stackdriver.Breakpoint,
{
id: 'with incomplete path',
location: {path: 'st-v8debugapi.js', line: 4},
} as {} as stackdriver.Breakpoint,
];
badBreakpoints.forEach((bp: stackdriver.Breakpoint) => {
it('should reject breakpoint ' + bp.id, done => {
api.set(bp, err => {
assert.ok(err, 'should return an error');
assert.ok(bp.status);
assert.ok(bp.status instanceof StatusMessage);
assert.ok(bp.status!.isError);
done();
});
});
});
it('should reject breakpoint when javascript file is ambiguous', done => {
require('./fixtures/a/hello.js');
require('./fixtures/b/hello.js');
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: 'ambiguous',
location: {line: 1, path: 'hello.js'},
} as {} as stackdriver.Breakpoint;
api.set(bp, err => {
assert.ok(err);
assert.ok(bp.status);
assert.ok(bp.status instanceof StatusMessage);
assert.ok(bp.status!.isError);
assert(
bp.status!.description.format === utils.messages.SOURCE_FILE_AMBIGUOUS
);
// Verify that a log message is emitted.
assert.strictEqual(
logger.warns.length,
1,
`Expected 1 warning log message, got ${logger.allCalls.length}`
);
const message = logger.warns[0].args[0];
let expectedSubstring = path.join('fixtures', 'a', 'hello.js');
assert.notStrictEqual(
message.indexOf(expectedSubstring),
-1,
`Missing text '${expectedSubstring}' in '${message}'`
);
expectedSubstring = path.join('fixtures', 'b', 'hello.js');
assert.notStrictEqual(
message.indexOf(expectedSubstring),
-1,
`Missing text '${expectedSubstring}' in '${message}'`
);
expectedSubstring = 'Unable to unambiguously find';
assert.notStrictEqual(
message.indexOf(expectedSubstring),
-1,
`Missing text '${expectedSubstring}' in '${message}'`
);
done();
});
});
it('should reject breakpoint when source mapping is ambiguous', done => {
const bp: stackdriver.Breakpoint = {
id: 'ambiguous',
location: {line: 1, path: 'in.ts'},
} as {} as stackdriver.Breakpoint;
api.set(bp, err => {
assert.ok(err);
assert.ok(bp.status);
assert.ok(bp.status instanceof StatusMessage);
assert.ok(bp.status!.isError);
assert(
bp.status!.description.format === utils.messages.SOURCE_FILE_AMBIGUOUS
);
// Verify that a warning log message is emitted.
assert.strictEqual(
logger.warns.length,
1,
`Expected 1 warning log message, got ${logger.allCalls.length}`
);
const message = logger.warns[0].args[0];
assert.notStrictEqual(message.indexOf('Multiple matches:'), -1);
done();
});
});
it('should reject breakpoint on non-existent line', done => {
require('./fixtures/foo.js');
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: 'non-existent line',
location: {path: path.join('fixtures', 'foo.js'), line: 500},
} as {} as stackdriver.Breakpoint;
api.set(bp, err => {
assert.ok(err);
assert.ok(bp.status);
assert.ok(bp.status instanceof StatusMessage);
assert.ok(bp.status!.isError);
assert(
bp.status!.description.format.match(
`${utils.messages.INVALID_LINE_NUMBER}.*foo.js:500`
)
);
done();
});
});
});
function conditionTests(
subject: string,
test: (err: Error | null) => void,
expressions: Array<string | null>
) {
describe(subject, () => {
expressions.forEach(expr => {
it('should validate breakpoint with condition "' + expr + '"', done => {
// make a clean copy of breakpointInFoo
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: breakpointInFoo.id,
location: breakpointInFoo.location,
condition: expr,
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
test(err1);
api.clear(bp, err2 => {
test(err2);
done();
});
});
});
});
});
}
conditionTests('invalid conditions', assert, [
// syntax errors
'*',
'j+',
'break',
':)',
// mutability
'x = 1',
'const x = 1;',
'console.log(1)',
'while (true) ;',
'return 3',
'throw new Error()',
'new Error()',
'try { 1 }',
'let me_pass = 1',
'debugger',
'function newfunction() { 1 }',
'{ f: fib(3) }',
'function () { 1 }',
'() => { 1 }',
'1, 2, 3, fib(), 4',
'!fib()',
'1+fib()',
'x++',
'[1, 2, 3, 4, x = 1, x == 1, x === 1]',
'[0].values()',
'new Object()',
]);
conditionTests(
'valid conditions',
err => {
assert.ifError(err);
},
[
null,
'',
';',
'x == 1',
'x === 1',
'global <= 1',
'this + 1',
'!this',
'this?this:1',
'{f: this?1:2}',
'{f: process.env}',
'1,2,3,{f:2},4',
'A[this?this:1]',
'[1, 2, 3, 4, x == 1, x === 1, null, undefined]',
'[0].values',
'[][0]',
'[0][' + MAX_INT + ']',
'"𠮷".length + (5| "𠮷")',
'/ٹوٹ بٹوٹ کے دو مُرغے تھے/',
]
);
if (utils.satisfies(process.version, '>=4.0')) {
conditionTests('invalid conditions Node 4+', assert, [
'[][Symbol.iterator]()',
'`${[][Symbol.iterator]()}`',
'`${let x = 1}`',
'`${JSON.parse("{x:1}")}`',
'`${try {1}}`',
]);
conditionTests(
'valid conditions Node 4+',
err => {
assert.ifError(err);
},
[
'[][Symbol.iterator]',
'[..."peanut butter"]',
'[0,...[1,2,"foo"]]',
'`${1}`',
'`${[][1+1]}`',
'0b10101010',
'0o70000',
// Disabled because of suspect acorn issues?
// https://tonicdev.com/575b00351a0e0a1300505d00/575b00351a0e0a1300505d01
// '{["foo"]: 1}',
// '{ foo (a,b) {}}'
]
);
}
describe('path normalization', () => {
// TODO(dominickramer): Have this actually be a list of Breakpoints
const breakpoints = [
{
id: 'path0',
location: {
line: 5,
path: path.join(path.sep, 'test', 'test-v8debugapi-code.js'),
},
} as {} as stackdriver.Breakpoint,
{
id: 'path1',
location: {line: 5, path: path.join('test', 'test-v8debugapi-code.js')},
} as {} as stackdriver.Breakpoint,
{
id: 'path2',
location: {
line: 5,
path:
// Usage the absolute path to `test-v8debugapi-code.js`.
__filename
.split(path.sep)
.slice(0, -1)
.concat('test-v8debugapi-code.js')
.join(path.sep),
},
} as {} as stackdriver.Breakpoint,
{
id: 'with . in path',
location: {
path: path.join('test', '.', 'test-v8debugapi-code.js'),
line: 5,
},
} as {} as stackdriver.Breakpoint,
{
id: 'with . in path',
location: {path: path.join('.', 'test-v8debugapi-code.js'), line: 5},
} as {} as stackdriver.Breakpoint,
{
id: 'with .. in path',
location: {
path: path.join('test', '..', 'test-v8debugapi-code.js'),
line: 5,
},
} as {} as stackdriver.Breakpoint,
{
id: 'with .. in path',
location: {
path: path.join('..', 'test', 'test-v8debugapi-code.js'),
line: 5,
},
} as {} as stackdriver.Breakpoint,
];
breakpoints.forEach((bp: stackdriver.Breakpoint) => {
it('should handle breakpoint as ' + bp.location!.path, done => {
api.set(bp, err1 => {
assert.ifError(err1);
api.wait(bp, err2 => {
assert.ifError(err2);
api.clear(bp, err3 => {
assert.ifError(err3);
done();
});
});
process.nextTick(() => {
code.foo(7);
});
});
});
});
});
describe('log', () => {
let oldLPS: number;
let oldDS: number;
before(() => {
oldLPS = config.log.maxLogsPerSecond;
oldDS = config.log.logDelaySeconds;
config.log.maxLogsPerSecond = 1;
config.log.logDelaySeconds = 1;
});
after(() => {
config.log.maxLogsPerSecond = oldLPS;
config.log.logDelaySeconds = oldDS;
assert(stateIsClean(api));
});
it('should throttle correctly', done => {
let completed = false;
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: breakpointInFoo.id,
location: breakpointInFoo.location,
action: 'LOG',
logMessageFormat: 'cat',
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
let transcript = '';
let runCount = 0;
assert.ifError(err1);
api.log(
bp,
fmt => {
transcript += fmt;
},
() => {
return completed;
}
);
const interval = setInterval(() => {
code.foo(1);
runCount++;
}, 100);
setTimeout(() => {
completed = true;
assert.strictEqual(transcript, 'catcat');
assert(runCount > 12);
clearInterval(interval);
api.clear(bp, err2 => {
assert.ifError(err2);
done();
});
}, 1500);
});
});
});
describe('InspectorDebugApi', () => {
let oldLPS: number;
let oldDS: number;
before(() => {
oldLPS = config.log.maxLogsPerSecond;
oldDS = config.log.logDelaySeconds;
config.log.maxLogsPerSecond = config.resetV8DebuggerThreshold * 3;
config.log.logDelaySeconds = 1;
});
after(() => {
config.log.maxLogsPerSecond = oldLPS;
config.log.logDelaySeconds = oldDS;
assert(stateIsClean(api));
});
it('should perform v8 breakpoints reset when meeting threshold', done => {
// The test is only eligible for the InspectorDebugApi test target.
if (!(api instanceof InspectorDebugApi)) {
done();
return;
}
const bp: stackdriver.Breakpoint = {
id: breakpointInFoo.id,
location: breakpointInFoo.location,
action: 'LOG',
logMessageFormat: 'cat',
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
let logpointEvaluatedTimes = 0;
assert.ifError(err1);
api.log(
bp,
() => {
logpointEvaluatedTimes += 1;
},
() => false
);
const inspectorDebugApi = api as InspectorDebugApi;
const v8BeforeReset = inspectorDebugApi.v8;
// The loop should trigger the breakpoints reset.
for (let i = 0; i < config.resetV8DebuggerThreshold; i++) {
code.foo(1);
}
// Expect the current v8 data is no longer the previous one.
assert.notStrictEqual(inspectorDebugApi.v8, v8BeforeReset);
// Make sure the logpoint is still triggered correctly after the second reset.
for (let i = 0; i < config.resetV8DebuggerThreshold + 1; i++) {
code.foo(1);
}
assert.strictEqual(
logpointEvaluatedTimes,
config.resetV8DebuggerThreshold * 2 + 1
);
api.clear(bp, err2 => {
assert.ifError(err2);
done();
});
});
});
});
describe('set and wait', () => {
it('should be possible to wait on a breakpoint', done => {
// clone a clean breakpointInFoo
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: breakpointInFoo.id,
location: breakpointInFoo.location,
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.wait(bp, err2 => {
assert.ifError(err2);
api.clear(bp, err3 => {
assert.ifError(err3);
done();
});
});
process.nextTick(() => {
code.foo(1);
});
});
});
it('should resolve actual line number hit rather than originally set for js files', done => {
const bp: stackdriver.Breakpoint = {
id: 'fake-id-124',
location: {path: 'build/test/test-v8debugapi-code.js', line: 4},
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.wait(bp, err2 => {
assert.ifError(err2);
assert.strictEqual(
(bp.location as stackdriver.SourceLocation).line,
5
);
api.clear(bp, err3 => {
assert.ifError(err3);
done();
});
});
process.nextTick(() => {
code.foo(1);
});
});
});
it('should not change line number when breakpoints hit for transpiled files', done => {
const bp: stackdriver.Breakpoint = {
id: 'fake-id-125',
location: {
path: path.join('test', 'test-v8debugapi-ts-code.ts'),
line: 10,
},
} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.wait(bp, err2 => {
assert.ifError(err2);
assert(bp.location);
assert.strictEqual(bp.location!.line, 10);
api.clear(bp, err3 => {
assert.ifError(err3);
done();
});
});
process.nextTick(() => {
dist({x: 1, y: 2}, {x: 3, y: 4});
});
});
});
it('should hit breakpoints in shorter transpiled files', done => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const someFunction = require('./fixtures/transpiled-shorter/in.js');
const bp: stackdriver.Breakpoint = {
id: 'fake-id-shorter-transpiled',
location: {
path: path.join(
'build',
'test',
'fixtures',
'transpiled-shorter',
'in.coffee'
),
// Note: The file `./fixtures/transpiled-shorter/in.js` was generated
// from
// transpiling `./fixtures/transpiled-shorter/in.coffee`, and
// `in.js` only has 44 lines. The purpose of this test is to
// ensure that if the line number specified below is larger than
// the number of lines in `in.js` but less than or equal to the
// number of lines in `in.coffee`, the breakpoint will still hit
// correctly.
line: 60,
},
} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.wait(bp, err2 => {
assert.ifError(err2);
assert(bp.location);
assert.strictEqual(bp.location!.line, 60);
api.clear(bp, err3 => {
assert.ifError(err3);
done();
});
});
process.nextTick(someFunction);
});
});
it('should work with multiply hit breakpoints', done => {
const oldWarn = logger.warn;
let logCount = 0;
// If an exception is thrown we will log
logger.warn = () => {
logCount++;
};
// clone a clean breakpointInFoo
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: breakpointInFoo.id,
location: breakpointInFoo.location,
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.wait(bp, err2 => {
assert.ifError(err2);
setTimeout(() => {
logger.warn = oldWarn;
assert.strictEqual(logCount, 0);
api.clear(bp, err3 => {
assert.ifError(err3);
done();
});
}, 100);
});
process.nextTick(() => {
code.foo(1);
});
setTimeout(() => {
code.foo(2);
}, 50);
});
});
it('should be possible to wait on a logpoint without expressions', done => {
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: breakpointInFoo.id,
action: 'LOG',
logMessageFormat: 'Hello World',
location: breakpointInFoo.location,
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.wait(bp, err2 => {
assert.ifError(err2);
api.clear(bp, err3 => {
assert.ifError(err3);
done();
});
});
process.nextTick(() => {
code.foo(1);
});
});
});
it('should capture state', done => {
// clone a clean breakpointInFoo
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: breakpointInFoo.id,
location: breakpointInFoo.location,
} as {} as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.wait(bp, err2 => {
assert.ifError(err2);
assert.ok(bp.stackFrames);
assert.ok(bp.variableTable);
const topFrame = bp.stackFrames[0];
assert.ok(topFrame);
assert.strictEqual(topFrame['function'], 'foo');
assert.strictEqual(topFrame.locals[0].name, 'n');
assert.strictEqual(topFrame.locals[0].value, '2');
assert.strictEqual(topFrame.locals[1].name, 'A');
assert.strictEqual(topFrame.locals[2].name, 'B');
api.clear(bp, err3 => {
assert.ifError(err3);
done();
});
});
process.nextTick(() => {
code.foo(2);
});
});
});
it('should resolve correct frame count', done => {
// clone a clean breakpointInFoo
// TODO(dominickramer): Have this actually implement Breakpoint
const bp: stackdriver.Breakpoint = {
id: breakpointInFoo.id,
location: breakpointInFoo.location,
} as {} as stackdriver.Breakpoint;
const oldCount = config.capture.maxExpandFrames;
config.capture.maxExpandFrames = 0;
api.set(bp, err1 => {
assert.ifError(err1);
api.wait(bp, err2 => {
assert.ifError(err2);
assert.ok(bp.stackFrames);
assert.ok(bp.variableTable);
const topFrame = bp.stackFrames[0];
assert.ok(topFrame);
assert.strictEqual(topFrame['function'], 'foo');
assert.strictEqual(topFrame.arguments.length, 1);
// TODO(dominickramer): Handle the case when
// topFrame.arguments[0].varTableIndex
// is undefined.