import { expect, test } from 'bun:test'; import { createRenderQueue, RenderBusyError } from './queue'; const result = new Uint8Array([1, 2, 3]); test('render queue bounds global concurrency and shares only the same owner and content', async () => { const queue = createRenderQueue({ concurrency: 2, maxPending: 4, maxPerUser: 2 }); let active = 0, peak = 0, calls = 0; const run = async () => { calls++; active++; peak = Math.max(peak, active); await Bun.sleep(5); active--; return result; }; const first = queue('alice', 'one', run); expect(queue('alice', 'one', run)).toBe(first); const all = [first, queue('alice', 'two', run), queue('bob', 'one', run), queue('carol', 'one', run)]; await Promise.all(all); expect(calls).toBe(4); expect(peak).toBe(2); expect(await queue('alice', 'one', run)).toBe(result); expect(calls).toBe(5); }); test('render admission enforces per-user and global capacity and recovers after failures', async () => { const queue = createRenderQueue({ concurrency: 1, maxPending: 2, maxPerUser: 1 }); let release!: () => void; const first = queue('alice', 'a', async () => { await new Promise(resolve => { release = resolve; }); throw new Error('render failed'); }); const failure = first.catch(error => error); await expect(queue('alice', 'b', async () => result)).rejects.toBeInstanceOf(RenderBusyError); const second = queue('bob', 'b', async () => result); await expect(queue('carol', 'c', async () => result)).rejects.toBeInstanceOf(RenderBusyError); while (!release) await Bun.sleep(1); release(); expect((await failure).message).toBe('render failed'); await second; expect(await queue('alice', 'a', async () => result)).toBe(result); });