Skip to content
File

Blob: src/workerd/api/node/tests/v8-nodejs-test.js

javascript522 lines
1// Copyright (c) 2023 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4import * as v8 from 'node:v8';
5import { strictEqual, deepStrictEqual, throws } from 'node:assert';
6 
7export const v8Test = {
8 test() {
9 // Note: test was generated by claude with manual tweaks
10 
11 // Test cachedDataVersionTag function
12 strictEqual(typeof v8.cachedDataVersionTag, 'function');
13 strictEqual(v8.cachedDataVersionTag(), 0);
14 
15 // Test getHeapStatistics - should return object with expected structure
16 const heapStats = v8.getHeapStatistics();
17 strictEqual(typeof heapStats, 'object');
18 strictEqual(heapStats.total_heap_size, 0);
19 strictEqual(heapStats.total_heap_size_executable, 0);
20 strictEqual(heapStats.total_physical_size, 0);
21 strictEqual(heapStats.total_available_size, 0);
22 strictEqual(heapStats.used_heap_size, 0);
23 strictEqual(heapStats.heap_size_limit, 0);
24 strictEqual(heapStats.malloced_memory, 0);
25 strictEqual(heapStats.peak_malloced_memory, 0);
26 strictEqual(heapStats.does_zap_garbage, 0);
27 strictEqual(heapStats.number_of_native_contexts, 0);
28 strictEqual(heapStats.number_of_detached_contexts, 0);
29 strictEqual(heapStats.total_global_handles_size, 0);
30 strictEqual(heapStats.used_global_handles_size, 0);
31 strictEqual(heapStats.external_memory, 0);
32 
33 // Test getHeapSpaceStatistics - should return empty array
34 const heapSpaceStats = v8.getHeapSpaceStatistics();
35 deepStrictEqual(heapSpaceStats, []);
36 
37 // Test getHeapCodeStatistics - should return object with expected structure
38 const heapCodeStats = v8.getHeapCodeStatistics();
39 strictEqual(typeof heapCodeStats, 'object');
40 strictEqual(heapCodeStats.code_and_metadata_size, 0);
41 strictEqual(heapCodeStats.bytecode_and_metadata_size, 0);
42 strictEqual(heapCodeStats.external_script_source_size, 0);
43 strictEqual(heapCodeStats.cpu_profiler_metadata_size, 0);
44 
45 // Test getCppHeapStatistics with default 'detailed' type
46 const cppHeapStats = v8.getCppHeapStatistics();
47 strictEqual(typeof cppHeapStats, 'object');
48 strictEqual(cppHeapStats.committed_size_bytes, 0);
49 strictEqual(cppHeapStats.resident_size_bytes, 0);
50 strictEqual(cppHeapStats.used_size_bytes, 0);
51 deepStrictEqual(cppHeapStats.space_statistics, []);
52 deepStrictEqual(cppHeapStats.type_names, []);
53 strictEqual(cppHeapStats.detail_level, 'detailed');
54 
55 // Test getCppHeapStatistics with 'brief' type
56 const cppHeapStatsBrief = v8.getCppHeapStatistics('brief');
57 strictEqual(cppHeapStatsBrief.detail_level, 'brief');
58 
59 // Test getCppHeapStatistics with invalid type
60 throws(
61 () => {
62 v8.getCppHeapStatistics('invalid');
63 },
64 {
65 code: 'ERR_INVALID_ARG_VALUE',
66 }
67 );
68 
69 // Test writeHeapSnapshot - should throw ERR_METHOD_NOT_IMPLEMENTED
70 throws(
71 () => {
72 v8.writeHeapSnapshot();
73 },
74 {
75 code: 'ERR_METHOD_NOT_IMPLEMENTED',
76 }
77 );
78 
79 throws(
80 () => {
81 v8.writeHeapSnapshot('test.heapsnapshot');
82 },
83 {
84 code: 'ERR_METHOD_NOT_IMPLEMENTED',
85 }
86 );
87 
88 throws(
89 () => {
90 v8.writeHeapSnapshot('test.heapsnapshot', { exposeInternals: true });
91 },
92 {
93 code: 'ERR_METHOD_NOT_IMPLEMENTED',
94 }
95 );
96 
97 // Test writeHeapSnapshot argument validation
98 throws(
99 () => {
100 v8.writeHeapSnapshot(123);
101 },
102 {
103 code: 'ERR_INVALID_ARG_TYPE',
104 }
105 );
106 
107 throws(
108 () => {
109 v8.writeHeapSnapshot('test.heapsnapshot', 'invalid');
110 },
111 {
112 code: 'ERR_INVALID_ARG_TYPE',
113 }
114 );
115 
116 throws(
117 () => {
118 v8.writeHeapSnapshot('test.heapsnapshot', {
119 exposeInternals: 'invalid',
120 });
121 },
122 {
123 code: 'ERR_INVALID_ARG_TYPE',
124 }
125 );
126 
127 throws(
128 () => {
129 v8.writeHeapSnapshot('test.heapsnapshot', {
130 exposeNumericValues: 'invalid',
131 });
132 },
133 {
134 code: 'ERR_INVALID_ARG_TYPE',
135 }
136 );
137 
138 // Test getHeapSnapshot - should throw ERR_METHOD_NOT_IMPLEMENTED
139 throws(
140 () => {
141 v8.getHeapSnapshot();
142 },
143 {
144 code: 'ERR_METHOD_NOT_IMPLEMENTED',
145 }
146 );
147 
148 throws(
149 () => {
150 v8.getHeapSnapshot({ exposeInternals: false });
151 },
152 {
153 code: 'ERR_METHOD_NOT_IMPLEMENTED',
154 }
155 );
156 
157 // Test getHeapSnapshot argument validation
158 throws(
159 () => {
160 v8.getHeapSnapshot('invalid');
161 },
162 {
163 code: 'ERR_INVALID_ARG_TYPE',
164 }
165 );
166 
167 throws(
168 () => {
169 v8.getHeapSnapshot({ exposeInternals: 'invalid' });
170 },
171 {
172 code: 'ERR_INVALID_ARG_TYPE',
173 }
174 );
175 
176 // Test setFlagsFromString - should throw ERR_METHOD_NOT_IMPLEMENTED
177 throws(
178 () => {
179 v8.setFlagsFromString('--expose-gc');
180 },
181 {
182 code: 'ERR_METHOD_NOT_IMPLEMENTED',
183 }
184 );
185 
186 // Test setFlagsFromString argument validation
187 throws(
188 () => {
189 v8.setFlagsFromString(123);
190 },
191 {
192 code: 'ERR_INVALID_ARG_TYPE',
193 }
194 );
195 
196 throws(
197 () => {
198 v8.setFlagsFromString();
199 },
200 {
201 code: 'ERR_INVALID_ARG_TYPE',
202 }
203 );
204 
205 // Test isStringOneByteRepresentation - should throw ERR_METHOD_NOT_IMPLEMENTED
206 throws(
207 () => {
208 v8.isStringOneByteRepresentation('hello');
209 },
210 {
211 code: 'ERR_METHOD_NOT_IMPLEMENTED',
212 }
213 );
214 
215 // Test isStringOneByteRepresentation argument validation
216 throws(
217 () => {
218 v8.isStringOneByteRepresentation(123);
219 },
220 {
221 code: 'ERR_INVALID_ARG_TYPE',
222 }
223 );
224 
225 throws(
226 () => {
227 v8.isStringOneByteRepresentation();
228 },
229 {
230 code: 'ERR_INVALID_ARG_TYPE',
231 }
232 );
233 
234 // Test setHeapSnapshotNearHeapLimit - should throw ERR_METHOD_NOT_IMPLEMENTED
235 throws(
236 () => {
237 v8.setHeapSnapshotNearHeapLimit(1);
238 },
239 {
240 code: 'ERR_METHOD_NOT_IMPLEMENTED',
241 }
242 );
243 
244 // Test setHeapSnapshotNearHeapLimit argument validation
245 throws(
246 () => {
247 v8.setHeapSnapshotNearHeapLimit(-1);
248 },
249 {
250 code: 'ERR_OUT_OF_RANGE',
251 }
252 );
253 
254 throws(
255 () => {
256 v8.setHeapSnapshotNearHeapLimit('invalid');
257 },
258 {
259 code: 'ERR_INVALID_ARG_TYPE',
260 }
261 );
262 
263 throws(
264 () => {
265 v8.setHeapSnapshotNearHeapLimit();
266 },
267 {
268 code: 'ERR_INVALID_ARG_TYPE',
269 }
270 );
271 
272 // Test Serializer class
273 throws(
274 () => {
275 new v8.Serializer();
276 },
277 {
278 code: 'ERR_METHOD_NOT_IMPLEMENTED',
279 }
280 );
281 
282 // Test Deserializer class
283 throws(
284 () => {
285 new v8.Deserializer(Buffer.from('test'));
286 },
287 {
288 code: 'ERR_METHOD_NOT_IMPLEMENTED',
289 }
290 );
291 
292 // Test DefaultSerializer class
293 throws(
294 () => {
295 new v8.DefaultSerializer();
296 },
297 {
298 code: 'ERR_METHOD_NOT_IMPLEMENTED',
299 }
300 );
301 
302 // Test DefaultDeserializer class
303 throws(
304 () => {
305 new v8.DefaultDeserializer(Buffer.from('test'));
306 },
307 {
308 code: 'ERR_METHOD_NOT_IMPLEMENTED',
309 }
310 );
311 
312 // Test serialize function
313 throws(
314 () => {
315 v8.serialize({ test: 'value' });
316 },
317 {
318 code: 'ERR_METHOD_NOT_IMPLEMENTED',
319 }
320 );
321 
322 // Test deserialize function
323 throws(
324 () => {
325 v8.deserialize(Buffer.from('test'));
326 },
327 {
328 code: 'ERR_METHOD_NOT_IMPLEMENTED',
329 }
330 );
331 
332 // Test GCProfiler class
333 const gcProfiler = new v8.GCProfiler();
334 throws(
335 () => {
336 gcProfiler.start();
337 },
338 {
339 code: 'ERR_METHOD_NOT_IMPLEMENTED',
340 }
341 );
342 
343 throws(
344 () => {
345 gcProfiler.stop();
346 },
347 {
348 code: 'ERR_METHOD_NOT_IMPLEMENTED',
349 }
350 );
351 
352 // Test promiseHooks
353 throws(
354 () => {
355 v8.promiseHooks.createHook();
356 },
357 {
358 code: 'ERR_METHOD_NOT_IMPLEMENTED',
359 }
360 );
361 
362 throws(
363 () => {
364 v8.promiseHooks.onInit();
365 },
366 {
367 code: 'ERR_METHOD_NOT_IMPLEMENTED',
368 }
369 );
370 
371 throws(
372 () => {
373 v8.promiseHooks.onBefore();
374 },
375 {
376 code: 'ERR_METHOD_NOT_IMPLEMENTED',
377 }
378 );
379 
380 throws(
381 () => {
382 v8.promiseHooks.onAfter();
383 },
384 {
385 code: 'ERR_METHOD_NOT_IMPLEMENTED',
386 }
387 );
388 
389 throws(
390 () => {
391 v8.promiseHooks.onSettled();
392 },
393 {
394 code: 'ERR_METHOD_NOT_IMPLEMENTED',
395 }
396 );
397 
398 // Test queryObjects function
399 throws(
400 () => {
401 v8.queryObjects(Object);
402 },
403 {
404 code: 'ERR_METHOD_NOT_IMPLEMENTED',
405 }
406 );
407 
408 throws(
409 () => {
410 v8.queryObjects(Array, { format: 'count' });
411 },
412 {
413 code: 'ERR_METHOD_NOT_IMPLEMENTED',
414 }
415 );
416 
417 throws(
418 () => {
419 v8.queryObjects(Array, { format: 'summary' });
420 },
421 {
422 code: 'ERR_METHOD_NOT_IMPLEMENTED',
423 }
424 );
425 
426 // Test queryObjects argument validation
427 throws(
428 () => {
429 v8.queryObjects('invalid');
430 },
431 {
432 code: 'ERR_INVALID_ARG_TYPE',
433 }
434 );
435 
436 throws(
437 () => {
438 v8.queryObjects();
439 },
440 {
441 code: 'ERR_INVALID_ARG_TYPE',
442 }
443 );
444 
445 throws(
446 () => {
447 v8.queryObjects(Object, 'invalid');
448 },
449 {
450 code: 'ERR_INVALID_ARG_TYPE',
451 }
452 );
453 
454 throws(
455 () => {
456 v8.queryObjects(Object, { format: 'invalid' });
457 },
458 {
459 code: 'ERR_INVALID_ARG_VALUE',
460 }
461 );
462 
463 // Test takeCoverage function
464 throws(
465 () => {
466 v8.takeCoverage();
467 },
468 {
469 code: 'ERR_METHOD_NOT_IMPLEMENTED',
470 }
471 );
472 
473 // Test stopCoverage function
474 throws(
475 () => {
476 v8.stopCoverage();
477 },
478 {
479 code: 'ERR_METHOD_NOT_IMPLEMENTED',
480 }
481 );
482 
483 // Test startupSnapshot methods
484 throws(
485 () => {
486 v8.startupSnapshot.addSerializeCallback(() => {}, {});
487 },
488 {
489 code: 'ERR_METHOD_NOT_IMPLEMENTED',
490 }
491 );
492 
493 throws(
494 () => {
495 v8.startupSnapshot.addDeserializeCallback(() => {}, {});
496 },
497 {
498 code: 'ERR_METHOD_NOT_IMPLEMENTED',
499 }
500 );
501 
502 throws(
503 () => {
504 v8.startupSnapshot.setDeserializeMainFunction(() => {}, {});
505 },
506 {
507 code: 'ERR_METHOD_NOT_IMPLEMENTED',
508 }
509 );
510 
511 // Test startupSnapshot.isBuildingSnapshot - should return false (not throw)
512 strictEqual(v8.startupSnapshot.isBuildingSnapshot(), false);
513 
514 // Test default export exists and has expected properties
515 strictEqual(typeof v8.default, 'object');
516 strictEqual(v8.default.cachedDataVersionTag, v8.cachedDataVersionTag);
517 strictEqual(v8.default.getHeapStatistics, v8.getHeapStatistics);
518 strictEqual(v8.default.Serializer, v8.Serializer);
519 strictEqual(v8.default.Deserializer, v8.Deserializer);
520 },
521};