import { describe, it, expect, beforeEach } from 'vitest';
import { CommandQueue } from '../queue/command-queue.js';
describe('CommandQueue', () => {
let queue: CommandQueue;
beforeEach(() => {
queue = new CommandQueue();
});
it('should enqueue and dequeue commands', () => {
const id = queue.enqueue('create_rect', { x: 0, y: 0, width: 100, height: 100 });
expect(id).toBeTruthy();
const commands = queue.dequeue();
expect(commands).toHaveLength(1);
expect(commands[0].id).toBe(id);
expect(commands[0].type).toBe('create_rect');
});
it('should mark commands as processing on dequeue', () => {
queue.enqueue('create_rect', { x: 0, y: 0, width: 100, height: 100 });
const commands = queue.dequeue();
expect(commands[0].status).toBe('processing');
});
it('should not return already processing commands', () => {
queue.enqueue('create_rect', { x: 0, y: 0, width: 100, height: 100 });
queue.dequeue(); // First dequeue marks as processing
const commands = queue.dequeue(); // Second dequeue should be empty
expect(commands).toHaveLength(0);
});
it('should store and retrieve results', () => {
const id = queue.enqueue('create_rect', { x: 0, y: 0, width: 100, height: 100 });
queue.setResult(id, { nodeId: '123:456', success: true });
const result = queue.getResult(id);
expect(result).toEqual({ nodeId: '123:456', success: true });
});
it('should track queue size', () => {
expect(queue.getQueueSize()).toBe(0);
queue.enqueue('create_rect', { x: 0, y: 0 });
queue.enqueue('create_text', { x: 0, y: 0, text: 'Hello' });
expect(queue.getQueueSize()).toBe(2);
});
it('should track pending count', () => {
queue.enqueue('create_rect', { x: 0, y: 0 });
queue.enqueue('create_text', { x: 0, y: 0, text: 'Hello' });
expect(queue.getPendingCount()).toBe(2);
queue.dequeue();
expect(queue.getPendingCount()).toBe(2); // Now processing, still counted
});
});