// Copyright (c) 2023 Cloudflare, Inc. // Licensed under the Apache 2.0 license found in the LICENSE file or at: // https://opensource.org/licenses/Apache-2.0 import * as v8 from 'node:v8'; import { strictEqual, deepStrictEqual, throws } from 'node:assert'; export const v8Test = { test() { // Note: test was generated by claude with manual tweaks // Test cachedDataVersionTag function strictEqual(typeof v8.cachedDataVersionTag, 'function'); strictEqual(v8.cachedDataVersionTag(), 0); // Test getHeapStatistics - should return object with expected structure const heapStats = v8.getHeapStatistics(); strictEqual(typeof heapStats, 'object'); strictEqual(heapStats.total_heap_size, 0); strictEqual(heapStats.total_heap_size_executable, 0); strictEqual(heapStats.total_physical_size, 0); strictEqual(heapStats.total_available_size, 0); strictEqual(heapStats.used_heap_size, 0); strictEqual(heapStats.heap_size_limit, 0); strictEqual(heapStats.malloced_memory, 0); strictEqual(heapStats.peak_malloced_memory, 0); strictEqual(heapStats.does_zap_garbage, 0); strictEqual(heapStats.number_of_native_contexts, 0); strictEqual(heapStats.number_of_detached_contexts, 0); strictEqual(heapStats.total_global_handles_size, 0); strictEqual(heapStats.used_global_handles_size, 0); strictEqual(heapStats.external_memory, 0); // Test getHeapSpaceStatistics - should return empty array const heapSpaceStats = v8.getHeapSpaceStatistics(); deepStrictEqual(heapSpaceStats, []); // Test getHeapCodeStatistics - should return object with expected structure const heapCodeStats = v8.getHeapCodeStatistics(); strictEqual(typeof heapCodeStats, 'object'); strictEqual(heapCodeStats.code_and_metadata_size, 0); strictEqual(heapCodeStats.bytecode_and_metadata_size, 0); strictEqual(heapCodeStats.external_script_source_size, 0); strictEqual(heapCodeStats.cpu_profiler_metadata_size, 0); // Test getCppHeapStatistics with default 'detailed' type const cppHeapStats = v8.getCppHeapStatistics(); strictEqual(typeof cppHeapStats, 'object'); strictEqual(cppHeapStats.committed_size_bytes, 0); strictEqual(cppHeapStats.resident_size_bytes, 0); strictEqual(cppHeapStats.used_size_bytes, 0); deepStrictEqual(cppHeapStats.space_statistics, []); deepStrictEqual(cppHeapStats.type_names, []); strictEqual(cppHeapStats.detail_level, 'detailed'); // Test getCppHeapStatistics with 'brief' type const cppHeapStatsBrief = v8.getCppHeapStatistics('brief'); strictEqual(cppHeapStatsBrief.detail_level, 'brief'); // Test getCppHeapStatistics with invalid type throws( () => { v8.getCppHeapStatistics('invalid'); }, { code: 'ERR_INVALID_ARG_VALUE', } ); // Test writeHeapSnapshot - should throw ERR_METHOD_NOT_IMPLEMENTED throws( () => { v8.writeHeapSnapshot(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.writeHeapSnapshot('test.heapsnapshot'); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.writeHeapSnapshot('test.heapsnapshot', { exposeInternals: true }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test writeHeapSnapshot argument validation throws( () => { v8.writeHeapSnapshot(123); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.writeHeapSnapshot('test.heapsnapshot', 'invalid'); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.writeHeapSnapshot('test.heapsnapshot', { exposeInternals: 'invalid', }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.writeHeapSnapshot('test.heapsnapshot', { exposeNumericValues: 'invalid', }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test getHeapSnapshot - should throw ERR_METHOD_NOT_IMPLEMENTED throws( () => { v8.getHeapSnapshot(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.getHeapSnapshot({ exposeInternals: false }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test getHeapSnapshot argument validation throws( () => { v8.getHeapSnapshot('invalid'); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.getHeapSnapshot({ exposeInternals: 'invalid' }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test setFlagsFromString - should throw ERR_METHOD_NOT_IMPLEMENTED throws( () => { v8.setFlagsFromString('--expose-gc'); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test setFlagsFromString argument validation throws( () => { v8.setFlagsFromString(123); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.setFlagsFromString(); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test isStringOneByteRepresentation - should throw ERR_METHOD_NOT_IMPLEMENTED throws( () => { v8.isStringOneByteRepresentation('hello'); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test isStringOneByteRepresentation argument validation throws( () => { v8.isStringOneByteRepresentation(123); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.isStringOneByteRepresentation(); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test setHeapSnapshotNearHeapLimit - should throw ERR_METHOD_NOT_IMPLEMENTED throws( () => { v8.setHeapSnapshotNearHeapLimit(1); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test setHeapSnapshotNearHeapLimit argument validation throws( () => { v8.setHeapSnapshotNearHeapLimit(-1); }, { code: 'ERR_OUT_OF_RANGE', } ); throws( () => { v8.setHeapSnapshotNearHeapLimit('invalid'); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.setHeapSnapshotNearHeapLimit(); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test Serializer class throws( () => { new v8.Serializer(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test Deserializer class throws( () => { new v8.Deserializer(Buffer.from('test')); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test DefaultSerializer class throws( () => { new v8.DefaultSerializer(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test DefaultDeserializer class throws( () => { new v8.DefaultDeserializer(Buffer.from('test')); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test serialize function throws( () => { v8.serialize({ test: 'value' }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test deserialize function throws( () => { v8.deserialize(Buffer.from('test')); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test GCProfiler class const gcProfiler = new v8.GCProfiler(); throws( () => { gcProfiler.start(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { gcProfiler.stop(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test promiseHooks throws( () => { v8.promiseHooks.createHook(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.promiseHooks.onInit(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.promiseHooks.onBefore(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.promiseHooks.onAfter(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.promiseHooks.onSettled(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test queryObjects function throws( () => { v8.queryObjects(Object); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.queryObjects(Array, { format: 'count' }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.queryObjects(Array, { format: 'summary' }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test queryObjects argument validation throws( () => { v8.queryObjects('invalid'); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.queryObjects(); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.queryObjects(Object, 'invalid'); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { v8.queryObjects(Object, { format: 'invalid' }); }, { code: 'ERR_INVALID_ARG_VALUE', } ); // Test takeCoverage function throws( () => { v8.takeCoverage(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test stopCoverage function throws( () => { v8.stopCoverage(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test startupSnapshot methods throws( () => { v8.startupSnapshot.addSerializeCallback(() => {}, {}); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.startupSnapshot.addDeserializeCallback(() => {}, {}); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { v8.startupSnapshot.setDeserializeMainFunction(() => {}, {}); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test startupSnapshot.isBuildingSnapshot - should return false (not throw) strictEqual(v8.startupSnapshot.isBuildingSnapshot(), false); // Test default export exists and has expected properties strictEqual(typeof v8.default, 'object'); strictEqual(v8.default.cachedDataVersionTag, v8.cachedDataVersionTag); strictEqual(v8.default.getHeapStatistics, v8.getHeapStatistics); strictEqual(v8.default.Serializer, v8.Serializer); strictEqual(v8.default.Deserializer, v8.Deserializer); }, };