import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { inert, isTask, workers } from 'moroutine'; import type { Task } from 'moroutine'; import { checkPrime, upper, countUp } from './fixtures/is-task.ts'; describe('isTask', () => { it('exposes a stable id on the moroutine', () => { assert.equal(typeof checkPrime.id, 'string'); assert.ok(checkPrime.id.length > 0); assert.notEqual(checkPrime.id, upper.id); }); it('identifies tasks produced by a moroutine', () => { const t = inert(checkPrime(7)); assert.ok(isTask(checkPrime, t)); assert.ok(!isTask(upper, t)); }); it('works across value and streaming moroutines', () => { const v = inert(checkPrime(7)); const s = inert(countUp(3)); assert.ok(isTask(checkPrime, v)); assert.ok(!isTask(countUp, v)); assert.ok(isTask(countUp, s)); assert.ok(!isTask(checkPrime, s)); }); it('narrows task args for downstream dispatch', async () => { using run = workers(1); const tasks: Array | Task> = [ inert(checkPrime(5)), inert(upper('hi')), ]; const results: Array = []; for (const t of tasks) { if (isTask(checkPrime, t)) { // t is Task — args typed as [n: number] const [n] = t.args; assert.equal(typeof n, 'number'); results.push(await run(t)); } else { const [s] = t.args; assert.equal(typeof s, 'string'); results.push(await run(t)); } } assert.deepEqual(results, [true, 'HI']); }); });