|
| 1 | +import * as domain from 'node:domain'; |
| 2 | +import { strictEqual, throws, ok } from 'node:assert'; |
| 3 | +import { EventEmitter } from 'node:events'; |
| 4 | + |
| 5 | +export const domainTest = { |
| 6 | + test() { |
| 7 | + // Note: test was generated by claude with manual tweaks |
| 8 | + |
| 9 | + // Test that Domain class exists and is a constructor |
| 10 | + strictEqual(typeof domain.Domain, 'function'); |
| 11 | + |
| 12 | + // Test that createDomain function exists |
| 13 | + strictEqual(typeof domain.createDomain, 'function'); |
| 14 | + |
| 15 | + // Test that create export exists (should be a Domain instance) |
| 16 | + ok(domain.create instanceof domain.Domain); |
| 17 | + |
| 18 | + // Test that active is null (domains are non-operational) |
| 19 | + strictEqual(domain.active, null); |
| 20 | + |
| 21 | + // Test creating a new domain |
| 22 | + const d = domain.createDomain(); |
| 23 | + ok(d instanceof domain.Domain); |
| 24 | + ok(d instanceof EventEmitter); // Domain extends EventEmitter |
| 25 | + |
| 26 | + // Test that Domain has expected properties |
| 27 | + strictEqual(d.members, undefined); |
| 28 | + |
| 29 | + // Test domain methods exist and are functions |
| 30 | + strictEqual(typeof d.enter, 'function'); |
| 31 | + strictEqual(typeof d.exit, 'function'); |
| 32 | + strictEqual(typeof d.add, 'function'); |
| 33 | + strictEqual(typeof d.remove, 'function'); |
| 34 | + strictEqual(typeof d.run, 'function'); |
| 35 | + strictEqual(typeof d.intercept, 'function'); |
| 36 | + strictEqual(typeof d.bind, 'function'); |
| 37 | + strictEqual(typeof d._errorHandler, 'function'); |
| 38 | + |
| 39 | + // Test enter() method - should be no-op (not throw) |
| 40 | + d.enter(); |
| 41 | + |
| 42 | + // Test exit() method - should be no-op (not throw) |
| 43 | + d.exit(); |
| 44 | + |
| 45 | + // Test add() method - should be no-op (not throw) |
| 46 | + const ee = new EventEmitter(); |
| 47 | + d.add(ee); |
| 48 | + d.add(null); |
| 49 | + d.add(undefined); |
| 50 | + d.add('string'); |
| 51 | + d.add(123); |
| 52 | + |
| 53 | + // Test remove() method - should be no-op (not throw) |
| 54 | + d.remove(ee); |
| 55 | + d.remove(null); |
| 56 | + d.remove(undefined); |
| 57 | + d.remove('string'); |
| 58 | + d.remove(123); |
| 59 | + |
| 60 | + // Test run() method - should execute function and return result |
| 61 | + let callCount = 0; |
| 62 | + const testFn = function (...args) { |
| 63 | + callCount++; |
| 64 | + strictEqual(this, d); // this should be the domain |
| 65 | + return args.reduce((a, b) => a + b, 0); |
| 66 | + }; |
| 67 | + |
| 68 | + const result = d.run(testFn, 1, 2, 3, 4); |
| 69 | + strictEqual(result, 10); |
| 70 | + strictEqual(callCount, 1); |
| 71 | + |
| 72 | + // Test run() with function that throws |
| 73 | + const errorFn = function () { |
| 74 | + throw new Error('test error'); |
| 75 | + }; |
| 76 | + throws(() => { |
| 77 | + d.run(errorFn); |
| 78 | + }, /test error/); |
| 79 | + |
| 80 | + // Test run() with no arguments |
| 81 | + const noArgFn = function () { |
| 82 | + return 'no args'; |
| 83 | + }; |
| 84 | + strictEqual(d.run(noArgFn), 'no args'); |
| 85 | + |
| 86 | + // Test intercept() method - should return the callback directly |
| 87 | + const callback1 = function () { |
| 88 | + return 'callback1'; |
| 89 | + }; |
| 90 | + const intercepted = d.intercept(callback1); |
| 91 | + strictEqual(intercepted, callback1); |
| 92 | + strictEqual(intercepted(), 'callback1'); |
| 93 | + |
| 94 | + // Test bind() method - should return the callback directly |
| 95 | + const callback2 = function () { |
| 96 | + return 'callback2'; |
| 97 | + }; |
| 98 | + const bound = d.bind(callback2); |
| 99 | + strictEqual(bound, callback2); |
| 100 | + strictEqual(bound(), 'callback2'); |
| 101 | + |
| 102 | + // Test _errorHandler() method - should throw the error passed to it |
| 103 | + const testError = new Error('test error handler'); |
| 104 | + throws(() => { |
| 105 | + d._errorHandler(testError); |
| 106 | + }, testError); |
| 107 | + |
| 108 | + throws(() => { |
| 109 | + d._errorHandler('string error'); |
| 110 | + }, /string error/); |
| 111 | + |
| 112 | + throws(() => { |
| 113 | + d._errorHandler(null); |
| 114 | + }); |
| 115 | + |
| 116 | + // Test that multiple domains can be created independently |
| 117 | + const d1 = domain.createDomain(); |
| 118 | + const d2 = domain.createDomain(); |
| 119 | + ok(d1 !== d2); |
| 120 | + ok(d1 instanceof domain.Domain); |
| 121 | + ok(d2 instanceof domain.Domain); |
| 122 | + |
| 123 | + // Test that domains inherit from EventEmitter properly |
| 124 | + let emitted = false; |
| 125 | + d1.on('test', () => { |
| 126 | + emitted = true; |
| 127 | + }); |
| 128 | + d1.emit('test'); |
| 129 | + strictEqual(emitted, true); |
| 130 | + |
| 131 | + // Test domain with async function (should work normally) |
| 132 | + let asyncResult = null; |
| 133 | + const asyncFn = async function (value) { |
| 134 | + return Promise.resolve(value * 2); |
| 135 | + }; |
| 136 | + |
| 137 | + const asyncPromise = d.run(asyncFn, 5); |
| 138 | + ok(asyncPromise instanceof Promise); |
| 139 | + |
| 140 | + // Test that EventEmitter.usingDomains is set to false |
| 141 | + strictEqual(EventEmitter.usingDomains, false); |
| 142 | + |
| 143 | + // Test default export |
| 144 | + strictEqual(typeof domain.default, 'object'); |
| 145 | + strictEqual(domain.default.Domain, domain.Domain); |
| 146 | + strictEqual(domain.default.active, domain.active); |
| 147 | + strictEqual(domain.default.createDomain, domain.createDomain); |
| 148 | + strictEqual(domain.default.create, domain.create); |
| 149 | + |
| 150 | + // Test edge cases with run() |
| 151 | + // Test with null function (should throw) |
| 152 | + throws(() => { |
| 153 | + d.run(null); |
| 154 | + }); |
| 155 | + |
| 156 | + throws(() => { |
| 157 | + d.run(undefined); |
| 158 | + }); |
| 159 | + |
| 160 | + throws(() => { |
| 161 | + d.run('not a function'); |
| 162 | + }); |
| 163 | + |
| 164 | + // Test intercept with non-function (should still return it) |
| 165 | + const nonFunction = 'not a function'; |
| 166 | + strictEqual(d.intercept(nonFunction), nonFunction); |
| 167 | + |
| 168 | + // Test bind with non-function (should still return it) |
| 169 | + strictEqual(d.bind(nonFunction), nonFunction); |
| 170 | + |
| 171 | + // Test that domain methods can be called multiple times safely |
| 172 | + d.enter(); |
| 173 | + d.enter(); |
| 174 | + d.exit(); |
| 175 | + d.exit(); |
| 176 | + |
| 177 | + // Test members property remains undefined after operations |
| 178 | + d.add(new EventEmitter()); |
| 179 | + d.remove(new EventEmitter()); |
| 180 | + d.enter(); |
| 181 | + d.exit(); |
| 182 | + strictEqual(d.members, undefined); |
| 183 | + |
| 184 | + console.log('All domain module tests passed!'); |
| 185 | + }, |
| 186 | +}; |
0 commit comments