import { describe, it, expect, beforeEach } from 'vitest';
import {
	pendingNodeMutations,
	getPendingState,
	clearPendingState,
	hasPendingMutations,
} from '../utils/mutation-queue';

describe('mutation-queue', () => {
	beforeEach(() => {
		pendingNodeMutations.clear();
	});

	describe('getPendingState', () => {
		it('creates default state for new node', () => {
			const state = getPendingState('n1');
			expect(state.inFlight).toBe(false);
			expect(state.queuedPosition).toBeNull();
			expect(state.queuedUiProperties).toBeNull();
			expect(state.graphId).toBeNull();
		});

		it('returns same reference on second call', () => {
			const s1 = getPendingState('n1');
			const s2 = getPendingState('n1');
			expect(s1).toBe(s2);
		});

		it('creates independent state per node', () => {
			const s1 = getPendingState('n1');
			const s2 = getPendingState('n2');
			s1.inFlight = true;
			expect(s2.inFlight).toBe(false);
		});
	});

	describe('clearPendingState', () => {
		it('removes state for node', () => {
			getPendingState('n1');
			expect(pendingNodeMutations.has('n1')).toBe(true);
			clearPendingState('n1');
			expect(pendingNodeMutations.has('n1')).toBe(false);
		});
	});

	describe('hasPendingMutations', () => {
		it('returns false when empty', () => {
			expect(hasPendingMutations()).toBe(false);
		});

		it('returns true when inFlight', () => {
			const state = getPendingState('n1');
			state.inFlight = true;
			expect(hasPendingMutations()).toBe(true);
		});

		it('returns true when queued position', () => {
			const state = getPendingState('n1');
			state.queuedPosition = { x: 10, y: 20 };
			expect(hasPendingMutations()).toBe(true);
		});

		it('returns false when state exists but idle', () => {
			getPendingState('n1');
			expect(hasPendingMutations()).toBe(false);
		});
	});
});
