import { describe, it, expect } from 'vitest';
import { parseFrameName, generateTemplateMarkdown, computeSyncActions, } from '../template-sync.js';
describe('pencil/template-sync', () => {
    describe('parseFrameName()', () => {
        it('parses "Template 01 - Hero Banner"', () => {
            const result = parseFrameName('Template 01 - Hero Banner');
            expect(result).toEqual({ number: 1, name: 'Hero Banner', slug: 'hero-banner' });
        });
        it('parses "Template 12 - Call to Action"', () => {
            const result = parseFrameName('Template 12 - Call to Action');
            expect(result).toEqual({ number: 12, name: 'Call to Action', slug: 'call-to-action' });
        });
        it('returns null for non-template frame names', () => {
            expect(parseFrameName('Random Frame')).toBeNull();
            expect(parseFrameName('Background')).toBeNull();
        });
    });
    describe('generateTemplateMarkdown()', () => {
        it('generates markdown with CSS from node properties', () => {
            const frame = {
                id: 'frame1',
                name: 'Template 01 - Hero Banner',
                width: 1080,
                height: 1350,
                children: [
                    {
                        id: 'child1',
                        name: 'Header',
                        type: 'frame',
                        fill: '#FFFFFF',
                        layout: 'vertical',
                        padding: [20, 24, 20, 24],
                    },
                ],
            };
            const md = generateTemplateMarkdown(frame);
            expect(md).toContain('# Template 01 — Hero Banner');
            expect(md).toContain('1080x1350');
            expect(md).toContain('background: #FFFFFF');
            expect(md).toContain('flex-direction: column');
        });
    });
    describe('computeSyncActions()', () => {
        it('detects new templates (frame without .md)', () => {
            const frames = [
                { id: 'f1', name: 'Template 01 - Hero', width: 1080, height: 1350, children: [] },
            ];
            const existingMds = [];
            const actions = computeSyncActions(frames, existingMds);
            expect(actions).toHaveLength(1);
            expect(actions[0].type).toBe('create');
            expect(actions[0].filename).toBe('template-01-hero.md');
        });
        it('detects orphaned templates (.md without frame)', () => {
            const frames = [];
            const existingMds = ['template-01-hero.md'];
            const actions = computeSyncActions(frames, existingMds);
            expect(actions).toHaveLength(1);
            expect(actions[0].type).toBe('orphan');
            expect(actions[0].filename).toBe('template-01-hero.md');
        });
        it('detects existing templates that may need update', () => {
            const frames = [
                { id: 'f1', name: 'Template 01 - Hero', width: 1080, height: 1350, children: [] },
            ];
            const existingMds = ['template-01-hero.md'];
            const actions = computeSyncActions(frames, existingMds);
            expect(actions).toHaveLength(1);
            expect(actions[0].type).toBe('update');
        });
        it('handles mix of new, existing, and orphaned', () => {
            const frames = [
                { id: 'f1', name: 'Template 01 - Hero', width: 1080, height: 1350, children: [] },
                { id: 'f2', name: 'Template 03 - Footer', width: 1080, height: 1350, children: [] },
            ];
            const existingMds = ['template-01-hero.md', 'template-02-sidebar.md'];
            const actions = computeSyncActions(frames, existingMds);
            const create = actions.filter(a => a.type === 'create');
            const update = actions.filter(a => a.type === 'update');
            const orphan = actions.filter(a => a.type === 'orphan');
            expect(update).toHaveLength(1); // template-01 exists
            expect(create).toHaveLength(1); // template-03 is new
            expect(orphan).toHaveLength(1); // template-02 has no frame
        });
        it('skips non-template frames', () => {
            const frames = [
                { id: 'f1', name: 'Background', width: 1920, height: 1080, children: [] },
                { id: 'f2', name: 'Template 01 - Hero', width: 1080, height: 1350, children: [] },
            ];
            const actions = computeSyncActions(frames, []);
            expect(actions).toHaveLength(1);
            expect(actions[0].filename).toBe('template-01-hero.md');
        });
    });
});
