import { describe, expect, test } from 'bun:test'; import { readWorkflowRuntimeConfig } from '../src/runtime/config.js'; import { WorkflowScheduler } from '../src/runtime/scheduler.js'; const config = (overrides: Partial> = {}) => ({ ...readWorkflowRuntimeConfig({} as NodeJS.ProcessEnv), ...overrides, }); describe('WorkflowScheduler', () => { test('进程级资源上限覆盖不同根运行', async () => { const scheduler = new WorkflowScheduler(config({ modelConcurrency: 2 })); let active = 0; let peak = 0; await Promise.all( Array.from({ length: 12 }, (_, index) => scheduler.run('model', `root-${index % 3}`, async () => { active++; peak = Math.max(peak, active); await new Promise((resolve) => setTimeout(resolve, 4)); active--; }), ), ); expect(peak).toBe(2); }); test('根运行之间轮转,不让一个大队列饿死后来的根', async () => { const scheduler = new WorkflowScheduler(config({ toolConcurrency: 1 })); const first = await scheduler.acquire('tool', 'root-a'); const order: string[] = []; const queued = [ scheduler.run('tool', 'root-a', async () => void order.push('a1')), scheduler.run('tool', 'root-a', async () => void order.push('a2')), scheduler.run('tool', 'root-b', async () => void order.push('b1')), scheduler.run('tool', 'root-a', async () => void order.push('a3')), ]; first.release(); await Promise.all(queued); expect(order.indexOf('b1')).toBeLessThan(order.indexOf('a3')); }); test('取消会移除排队项且不占用槽', async () => { const scheduler = new WorkflowScheduler(config({ processConcurrency: 1 })); const first = await scheduler.acquire('process', 'root-a'); const controller = new AbortController(); const cancelled = scheduler.acquire('process', 'root-b', controller.signal); controller.abort(Object.assign(new Error('stop'), { name: 'AbortError' })); await expect(cancelled).rejects.toMatchObject({ name: 'AbortError' }); first.release(); const lease = await scheduler.acquire('process', 'root-c'); lease.release(); }); }); describe('workflow runtime environment', () => { test('非法值响亮报错,不静默回默认值', () => { expect(() => readWorkflowRuntimeConfig({ MINIMAL_AGENT_WORKFLOW_MODEL_CONCURRENCY: '0', } as NodeJS.ProcessEnv), ).toThrow(/MINIMAL_AGENT_WORKFLOW_MODEL_CONCURRENCY/); expect(() => readWorkflowRuntimeConfig({ MINIMAL_AGENT_WORKFLOW_PIPELINE_CONCURRENCY: '10', MINIMAL_AGENT_WORKFLOW_PIPELINE_MAX_CONCURRENCY: '5', } as NodeJS.ProcessEnv), ).toThrow(/不能大于/); }); });