import { describe, it, expect } from 'vitest';
import { validateSnapshot, SNAPSHOT_VERSION } from '../core/canvas-serializer';

describe('canvas-serializer validation', () => {
	const validSnapshot = {
		version: SNAPSHOT_VERSION,
		exportedAt: new Date().toISOString(),
		nodes: [],
		edges: [],
		groups: [],
		viewport: { zoom: 1, pan: { x: 0, y: 0 } },
	};

	it('valid snapshot passes', () => {
		const result = validateSnapshot(validSnapshot);
		expect(result.valid).toBe(true);
		expect(result.errors).toHaveLength(0);
	});

	it('rejects null', () => {
		const result = validateSnapshot(null);
		expect(result.valid).toBe(false);
	});

	it('rejects wrong version', () => {
		const result = validateSnapshot({ ...validSnapshot, version: 999 });
		expect(result.valid).toBe(false);
		expect(result.errors.some((e) => e.includes('version'))).toBe(true);
	});

	it('rejects missing nodes', () => {
		const { nodes, ...rest } = validSnapshot;
		const result = validateSnapshot(rest);
		expect(result.valid).toBe(false);
		expect(result.errors.some((e) => e.includes('nodes'))).toBe(true);
	});

	it('rejects missing edges', () => {
		const { edges, ...rest } = validSnapshot;
		const result = validateSnapshot(rest);
		expect(result.valid).toBe(false);
		expect(result.errors.some((e) => e.includes('edges'))).toBe(true);
	});

	it('rejects missing viewport', () => {
		const { viewport, ...rest } = validSnapshot;
		const result = validateSnapshot(rest);
		expect(result.valid).toBe(false);
		expect(result.errors.some((e) => e.includes('viewport'))).toBe(true);
	});

	it('rejects invalid node (no id)', () => {
		const result = validateSnapshot({
			...validSnapshot,
			nodes: [{ position: { x: 0, y: 0 }, dimensions: { width: 200, height: 100 }, dbData: {} }],
		});
		expect(result.valid).toBe(false);
		expect(result.errors.some((e) => e.includes('id'))).toBe(true);
	});
});
