|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert'; |
| 3 | +import { Worker } from 'node:worker_threads'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { writeFileSync, unlinkSync } from 'node:fs'; |
| 7 | + |
| 8 | +describe('watch:worker event system', () => { |
| 9 | + it('should report worker files to parent process', async () => { |
| 10 | + const testDir = tmpdir(); |
| 11 | + const workerFile = join(testDir, `test-worker-${Date.now()}.js`); |
| 12 | + |
| 13 | + try { |
| 14 | + // Create a simple worker that reports itself |
| 15 | + writeFileSync(workerFile, ` |
| 16 | + const { Worker } = require('node:worker_threads'); |
| 17 | + module.exports = { test: true }; |
| 18 | + `); |
| 19 | + |
| 20 | + // Create a worker that requires the file |
| 21 | + const worker = new Worker(workerFile); |
| 22 | + |
| 23 | + await new Promise((resolve) => { |
| 24 | + worker.on('online', () => { |
| 25 | + worker.terminate(); |
| 26 | + resolve(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + } finally { |
| 30 | + try { unlinkSync(workerFile); } catch {} |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + it('should not report eval workers', (t, done) => { |
| 35 | + // Eval workers should be filtered out |
| 36 | + // This is a unit test that validates the condition logic |
| 37 | + const isInternal = false; |
| 38 | + const doEval = true; |
| 39 | + |
| 40 | + // Condition: !isInternal && doEval === false |
| 41 | + const shouldReport = !isInternal && doEval === false; |
| 42 | + assert.strictEqual(shouldReport, false, 'Eval workers should not be reported'); |
| 43 | + done(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should not report internal workers', (t, done) => { |
| 47 | + // Internal workers should be filtered out |
| 48 | + const isInternal = true; |
| 49 | + const doEval = false; |
| 50 | + |
| 51 | + // Condition: !isInternal && doEval === false |
| 52 | + const shouldReport = !isInternal && doEval === false; |
| 53 | + assert.strictEqual(shouldReport, false, 'Internal workers should not be reported'); |
| 54 | + done(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should report regular workers', (t, done) => { |
| 58 | + // Regular workers should be reported |
| 59 | + const isInternal = false; |
| 60 | + const doEval = false; |
| 61 | + |
| 62 | + // Condition: !isInternal && doEval === false |
| 63 | + const shouldReport = !isInternal && doEval === false; |
| 64 | + assert.strictEqual(shouldReport, true, 'Regular workers should be reported'); |
| 65 | + done(); |
| 66 | + }); |
| 67 | +}); |
0 commit comments