#!/usr/bin/env node

/**
 * TDD Tests for getAllSections Method Recovery
 */

const { TestRunner } = require('./test_runner.js');
const runner = new TestRunner();

// Test getAllSections functionality
runner.describe('SectionManager getAllSections method', () => {

    runner.it('should have getAllSections method in SectionManager', async () => {
        // Load editor
        delete require.cache[require.resolve('/home/worsch/markitect_project/markitect/static/editor.js')];
        require('/home/worsch/markitect_project/markitect/static/editor.js');

        if (global.SectionManager) {
            const manager = new global.SectionManager();
            const hasGetAllSections = typeof manager.getAllSections === 'function';
            runner.expect(hasGetAllSections).toBeTruthy();
        }
    });

    runner.it('should return array of all sections', async () => {
        if (global.SectionManager) {
            const manager = new global.SectionManager();

            // Create some test sections
            const sections = manager.createSectionsFromMarkdown('# Test\n\nContent\n\n## Another\n\nMore content');

            // getAllSections should return an array
            const allSections = manager.getAllSections();
            runner.expect(Array.isArray(allSections)).toBeTruthy();
            runner.expect(allSections.length).toBe(sections.length);
        }
    });

    runner.it('should return all sections from the sections Map', async () => {
        if (global.SectionManager) {
            const manager = new global.SectionManager();

            // Create sections
            manager.createSectionsFromMarkdown('# Test\n\nContent');

            const allSections = manager.getAllSections();
            const mapSize = manager.sections.size;

            runner.expect(allSections.length).toBe(mapSize);
        }
    });

    runner.it('should return sections with proper properties', async () => {
        if (global.SectionManager) {
            const manager = new global.SectionManager();

            // Create sections
            manager.createSectionsFromMarkdown('# Test\n\nContent');

            const allSections = manager.getAllSections();

            if (allSections.length > 0) {
                const firstSection = allSections[0];
                runner.expect(firstSection.id).toBeTruthy();
                runner.expect(firstSection.currentMarkdown).toBeTruthy();
                runner.expect(typeof firstSection.hasChanges).toBe('function');
                runner.expect(typeof firstSection.isEditing).toBe('function');
                runner.expect(typeof firstSection.getStatus).toBe('function');
            }
        }
    });
});

// Run the tests
if (require.main === module) {
    console.log('📊 Running TDD Tests for getAllSections Method Recovery');
    runner.run().then(() => {
        console.log('✅ Test run complete - now implement getAllSections!');
    });
}

module.exports = runner;