Skip to content
File

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

javascript155 lines
1// Copyright (c) 2025 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 trace_events from 'node:trace_events';
5import { strictEqual, throws, doesNotThrow } from 'node:assert';
6 
7export const traceEventsTest = {
8 test() {
9 // Note: test was generated by claude with manual tweaks
10 
11 // Test that required exports exist
12 strictEqual(typeof trace_events.createTracing, 'function');
13 strictEqual(typeof trace_events.getEnabledCategories, 'function');
14 
15 doesNotThrow(() => {
16 trace_events.createTracing({ categories: ['node'] });
17 });
18 
19 doesNotThrow(() => {
20 trace_events.createTracing({ categories: ['node', 'v8'] });
21 });
22 
23 throws(
24 () => {
25 trace_events.createTracing({ categories: [] });
26 },
27 { code: 'ERR_TRACE_EVENTS_CATEGORY_REQUIRED' }
28 );
29 
30 // Test createTracing options validation - options must be an object
31 for (const input of [
32 'invalid',
33 null,
34 123,
35 [],
36 { categories: 'invalid' },
37 { categories: {} },
38 { categories: 123 },
39 { categories: [123] },
40 { categories: ['valid', 123] },
41 { categories: ['valid', null] },
42 { categories: ['valid', {}] },
43 ]) {
44 throws(
45 () => {
46 trace_events.createTracing(input);
47 },
48 {
49 code: 'ERR_INVALID_ARG_TYPE',
50 }
51 );
52 }
53 
54 // Test getEnabledCategories - should return undefined
55 const enabledCategories = trace_events.getEnabledCategories();
56 strictEqual(enabledCategories, undefined);
57 
58 // Test getEnabledCategories with arguments (should ignore them)
59 const enabledCategories2 = trace_events.getEnabledCategories('ignored');
60 strictEqual(enabledCategories2, undefined);
61 
62 const enabledCategories3 = trace_events.getEnabledCategories(123, {});
63 strictEqual(enabledCategories3, undefined);
64 
65 // Test Tracing class through prototype (can't instantiate due to constructor throwing)
66 // We need to access the Tracing class through the internal module structure
67 // Since we can't instantiate it, we'll test what we can access
68 
69 // Test that createTracing validates properly before throwing
70 // Test with additional unknown properties (should be accepted)
71 doesNotThrow(() => {
72 trace_events.createTracing({
73 categories: ['node'],
74 unknownProperty: 'should be ignored',
75 });
76 });
77 
78 // Test edge cases with categories array
79 doesNotThrow(() => {
80 trace_events.createTracing({ categories: [''] }); // empty string should be valid
81 });
82 
83 doesNotThrow(() => {
84 trace_events.createTracing({ categories: ['a'.repeat(1000)] }); // very long string
85 });
86 
87 // Test that categories with special characters are accepted
88 doesNotThrow(() => {
89 trace_events.createTracing({
90 categories: ['node.fs', 'v8.gc', 'custom-category'],
91 });
92 });
93 
94 // Test with undefined categories (should pass validation)
95 throws(
96 () => {
97 trace_events.createTracing({ categories: undefined });
98 },
99 {
100 code: 'ERR_INVALID_ARG_TYPE', // undefined categories should fail validation
101 }
102 );
103 
104 // Test default export
105 strictEqual(typeof trace_events.default, 'object');
106 strictEqual(trace_events.default.createTracing, trace_events.createTracing);
107 strictEqual(
108 trace_events.default.getEnabledCategories,
109 trace_events.getEnabledCategories
110 );
111 
112 // Test that getEnabledCategories is consistent across calls
113 const categories1 = trace_events.getEnabledCategories();
114 const categories2 = trace_events.getEnabledCategories();
115 strictEqual(categories1, undefined);
116 strictEqual(categories2, undefined);
117 strictEqual(categories1, categories2);
118 
119 // Test various valid category names
120 const validCategoryNames = [
121 'node',
122 'v8',
123 'node.fs',
124 'node.http',
125 'v8.gc',
126 'custom',
127 'my-category',
128 'category_with_underscores',
129 'category.with.dots',
130 'category123',
131 '123category',
132 'a',
133 'very_long_category_name_that_should_still_be_valid',
134 ];
135 
136 for (const category of validCategoryNames) {
137 doesNotThrow(() => {
138 trace_events.createTracing({ categories: [category] });
139 });
140 }
141 
142 // Test with multiple valid categories
143 doesNotThrow(() => {
144 trace_events.createTracing({ categories: validCategoryNames });
145 });
146 
147 // Test with duplicate categories (should be valid)
148 doesNotThrow(() => {
149 trace_events.createTracing({
150 categories: ['node', 'node', 'v8', 'v8'],
151 });
152 });
153 },
154};