import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { workers, assign, channel } from 'moroutine'; import { identity, countUp } from './fixtures/worker-handle.ts'; describe('WorkerHandle', () => { it('run.workers is a frozen array matching pool size', () => { const run = workers(3); try { assert.equal(run.workers.length, 3); assert.ok(Object.isFrozen(run.workers)); } finally { run[Symbol.dispose](); } }); it('w.exec() dispatches a task to the specific worker', async () => { const run = workers(2); try { const result = await run.workers[0].exec(identity(42)); assert.equal(result, 42); } finally { run[Symbol.dispose](); } }); it('w.exec() dispatches a streaming task', async () => { const run = workers(1); try { const results: number[] = []; for await (const n of run.workers[0].exec(countUp(3))) { results.push(n); } assert.deepEqual(results, [0, 1, 2]); } finally { run[Symbol.dispose](); } }); it('assign() pins task to a specific worker via run()', async () => { const run = workers(2); try { const result = await run(assign(run.workers[1], identity(99))); assert.equal(result, 99); } finally { run[Symbol.dispose](); } }); it('assign() works in a batch', async () => { const run = workers(2); try { const results = await run([assign(run.workers[0], identity(1)), assign(run.workers[1], identity(2))]); assert.deepEqual(results, [1, 2]); } finally { run[Symbol.dispose](); } }); it('w.exec() rejects after dispose', async () => { const run = workers(1); run[Symbol.dispose](); await assert.rejects(() => run.workers[0].exec(identity(1)), { message: /disposed/ }); }); });