"use strict";
/**
 * E2E Test: Enhanced Analytics Configuration Generator
 *
 * Verifies that the Analytics generator writes all expected files to disk with correct content.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
require("../generators/index");
const generation_orchestrator_1 = require("../generators/generation-orchestrator");
describe('E2E: Enhanced Analytics Configuration Generator', () => {
    const testOutputDir = path.join(__dirname, '../../.e2e-output/enhanced-analytics');
    let orchestrator;
    beforeEach(() => {
        if (fs.existsSync(testOutputDir)) {
            fs.rmSync(testOutputDir, { recursive: true, force: true });
        }
        orchestrator = new generation_orchestrator_1.GenerationOrchestrator();
    });
    afterEach(() => {
        if (fs.existsSync(testOutputDir)) {
            fs.rmSync(testOutputDir, { recursive: true, force: true });
        }
    });
    it('should generate Mixpanel provider files', async () => {
        const workflow = {
            workflow: { id: 'analytics-test', version: '1.0.0', name: 'Analytics Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const mixpanelPath = path.join(testOutputDir, 'analytics/src/providers/mixpanel.ts');
        expect(fs.existsSync(mixpanelPath)).toBe(true);
        const mixpanel = fs.readFileSync(mixpanelPath, 'utf-8');
        expect(mixpanel).toContain('MixpanelProvider');
        expect(mixpanel).toContain('mixpanel.init');
        expect(mixpanel).toContain('track_pageview: true');
    });
    it('should generate Amplitude provider files', async () => {
        const workflow = {
            workflow: { id: 'amplitude-test', version: '1.0.0', name: 'Amplitude Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'amplitude',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const amplitudePath = path.join(testOutputDir, 'analytics/src/providers/amplitude.ts');
        expect(fs.existsSync(amplitudePath)).toBe(true);
        const amplitude = fs.readFileSync(amplitudePath, 'utf-8');
        expect(amplitude).toContain('AmplitudeProvider');
        expect(amplitude).toContain('sessions: true');
        expect(amplitude).toContain('pageViews: true');
    });
    it('should generate Segment provider files', async () => {
        const workflow = {
            workflow: { id: 'segment-test', version: '1.0.0', name: 'Segment Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'segment',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const segmentPath = path.join(testOutputDir, 'analytics/src/providers/segment.ts');
        expect(fs.existsSync(segmentPath)).toBe(true);
        const segment = fs.readFileSync(segmentPath, 'utf-8');
        expect(segment).toContain('SegmentProvider');
        expect(segment).toContain('AnalyticsBrowser.load');
    });
    it('should generate Google Analytics provider files', async () => {
        const workflow = {
            workflow: { id: 'ga-test', version: '1.0.0', name: 'GA Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'google_analytics',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const gaPath = path.join(testOutputDir, 'analytics/src/providers/google-analytics.ts');
        expect(fs.existsSync(gaPath)).toBe(true);
        const ga = fs.readFileSync(gaPath, 'utf-8');
        expect(ga).toContain('GoogleAnalyticsProvider');
        expect(ga).toContain('ReactGA.initialize');
    });
    it('should generate unified analytics service', async () => {
        const workflow = {
            workflow: { id: 'service-test', version: '1.0.0', name: 'Service Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const servicePath = path.join(testOutputDir, 'analytics/src/services/analytics.ts');
        expect(fs.existsSync(servicePath)).toBe(true);
        const service = fs.readFileSync(servicePath, 'utf-8');
        expect(service).toContain('AnalyticsService');
        expect(service).toContain('MixpanelProvider');
    });
    it('should generate event definitions', async () => {
        const workflow = {
            workflow: { id: 'events-test', version: '1.0.0', name: 'Events Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
                tracking: {
                    custom_events: [
                        {
                            name: 'ButtonClicked',
                            properties: ['buttonId', 'label'],
                        },
                    ],
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const definitionsPath = path.join(testOutputDir, 'analytics/src/events/definitions.ts');
        expect(fs.existsSync(definitionsPath)).toBe(true);
        const definitions = fs.readFileSync(definitionsPath, 'utf-8');
        expect(definitions).toContain('ButtonClickedEventProperties');
        expect(definitions).toContain('EventName');
    });
    it('should generate event tracker', async () => {
        const workflow = {
            workflow: { id: 'tracker-test', version: '1.0.0', name: 'Tracker Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
                tracking: {
                    custom_events: [{ name: 'TestEvent', properties: [] }],
                },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const trackerPath = path.join(testOutputDir, 'analytics/src/events/tracker.ts');
        expect(fs.existsSync(trackerPath)).toBe(true);
        const tracker = fs.readFileSync(trackerPath, 'utf-8');
        expect(tracker).toContain('EventTracker');
        expect(tracker).toContain('track');
    });
    it('should generate funnel definitions', async () => {
        const workflow = {
            workflow: { id: 'funnels-test', version: '1.0.0', name: 'Funnels Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
                funnels: [
                    {
                        name: 'SignupFlow',
                        steps: [{ event: 'signup_started' }, { event: 'email_verified' }],
                    },
                ],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const funnelsPath = path.join(testOutputDir, 'analytics/src/funnels/definitions.ts');
        expect(fs.existsSync(funnelsPath)).toBe(true);
        const funnels = fs.readFileSync(funnelsPath, 'utf-8');
        expect(funnels).toContain('SignupFlowFunnel');
        expect(funnels).toContain('signup_started');
    });
    it('should generate funnel tracker', async () => {
        const workflow = {
            workflow: { id: 'funnel-tracker-test', version: '1.0.0', name: 'Funnel Tracker Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
                funnels: [{ name: 'TestFunnel', steps: [] }],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const trackerPath = path.join(testOutputDir, 'analytics/src/funnels/tracker.ts');
        expect(fs.existsSync(trackerPath)).toBe(true);
        const tracker = fs.readFileSync(trackerPath, 'utf-8');
        expect(tracker).toContain('FunnelTracker');
        expect(tracker).toContain('startFunnel');
        expect(tracker).toContain('completeFunnel');
    });
    it('should generate cohort definitions', async () => {
        const workflow = {
            workflow: { id: 'cohorts-test', version: '1.0.0', name: 'Cohorts Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
                cohorts: [
                    {
                        name: 'PowerUsers',
                        criteria: 'events_count > 100',
                    },
                ],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const cohortsPath = path.join(testOutputDir, 'analytics/src/cohorts/definitions.ts');
        expect(fs.existsSync(cohortsPath)).toBe(true);
        const cohorts = fs.readFileSync(cohortsPath, 'utf-8');
        expect(cohorts).toContain('PowerUsersCohort');
    });
    it('should generate cohort analyzer', async () => {
        const workflow = {
            workflow: { id: 'cohort-analyzer-test', version: '1.0.0', name: 'Cohort Analyzer Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
                cohorts: [{ name: 'TestCohort', criteria: 'test_property = "value"' }],
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const analyzerPath = path.join(testOutputDir, 'analytics/src/cohorts/analyzer.ts');
        expect(fs.existsSync(analyzerPath)).toBe(true);
        const analyzer = fs.readFileSync(analyzerPath, 'utf-8');
        expect(analyzer).toContain('CohortAnalyzer');
        expect(analyzer).toContain('addUserToCohort');
    });
    it('should generate consent manager with GDPR compliance', async () => {
        const workflow = {
            workflow: { id: 'consent-test', version: '1.0.0', name: 'Consent Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
                ...{ privacy: { gdpr_compliance: true } },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const consentPath = path.join(testOutputDir, 'analytics/src/privacy/consent.ts');
        expect(fs.existsSync(consentPath)).toBe(true);
        const consent = fs.readFileSync(consentPath, 'utf-8');
        expect(consent).toContain('ConsentManager');
        expect(consent).toContain('grantConsent');
        expect(consent).toContain('revokeConsent');
    });
    it('should generate frontend analytics hooks', async () => {
        const workflow = {
            workflow: { id: 'hooks-test', version: '1.0.0', name: 'Hooks Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const analyticsHookPath = path.join(testOutputDir, 'web/src/hooks/useAnalytics.ts');
        expect(fs.existsSync(analyticsHookPath)).toBe(true);
        const analyticsHook = fs.readFileSync(analyticsHookPath, 'utf-8');
        expect(analyticsHook).toContain('useAnalytics');
        expect(analyticsHook).toContain('trackEvent');
        const trackEventHookPath = path.join(testOutputDir, 'web/src/hooks/useTrackEvent.ts');
        expect(fs.existsSync(trackEventHookPath)).toBe(true);
        const trackPageHookPath = path.join(testOutputDir, 'web/src/hooks/useTrackPage.ts');
        expect(fs.existsSync(trackPageHookPath)).toBe(true);
    });
    it('should generate Analytics Provider component', async () => {
        const workflow = {
            workflow: { id: 'provider-test', version: '1.0.0', name: 'Provider Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const providerPath = path.join(testOutputDir, 'web/src/components/AnalyticsProvider.tsx');
        expect(fs.existsSync(providerPath)).toBe(true);
        const provider = fs.readFileSync(providerPath, 'utf-8');
        expect(provider).toContain('AnalyticsProvider');
        expect(provider).toContain('AnalyticsContext');
    });
    it('should generate backend analytics service', async () => {
        const workflow = {
            workflow: { id: 'backend-test', version: '1.0.0', name: 'Backend Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const servicePath = path.join(testOutputDir, 'api/src/services/analytics.ts');
        expect(fs.existsSync(servicePath)).toBe(true);
        const service = fs.readFileSync(servicePath, 'utf-8');
        expect(service).toContain('BackendAnalyticsService');
        expect(service).toContain('trackEvent');
    });
    it('should generate analytics middleware', async () => {
        const workflow = {
            workflow: { id: 'middleware-test', version: '1.0.0', name: 'Middleware Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const middlewarePath = path.join(testOutputDir, 'api/src/middleware/analytics.ts');
        expect(fs.existsSync(middlewarePath)).toBe(true);
        const middleware = fs.readFileSync(middlewarePath, 'utf-8');
        expect(middleware).toContain('analyticsMiddleware');
        expect(middleware).toContain('api_request');
    });
    it('should generate user properties service', async () => {
        const workflow = {
            workflow: { id: 'user-props-test', version: '1.0.0', name: 'User Props Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const servicePath = path.join(testOutputDir, 'analytics/src/services/user-properties.ts');
        expect(fs.existsSync(servicePath)).toBe(true);
        const service = fs.readFileSync(servicePath, 'utf-8');
        expect(service).toContain('UserPropertiesService');
        expect(service).toContain('setProperty');
    });
    it('should generate analytics types', async () => {
        const workflow = {
            workflow: { id: 'types-test', version: '1.0.0', name: 'Types Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const typesPath = path.join(testOutputDir, 'analytics/src/types/analytics.ts');
        expect(fs.existsSync(typesPath)).toBe(true);
        const types = fs.readFileSync(typesPath, 'utf-8');
        expect(types).toContain('AnalyticsProvider');
        expect(types).toContain('TrackEventParams');
    });
    it('should generate analytics configuration', async () => {
        const workflow = {
            workflow: { id: 'config-test', version: '1.0.0', name: 'Config Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
                ...{ sampling: { rate: 0.5, enabled: true } },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const configPath = path.join(testOutputDir, 'analytics/src/config/analytics.ts');
        expect(fs.existsSync(configPath)).toBe(true);
        const config = fs.readFileSync(configPath, 'utf-8');
        expect(config).toContain('analyticsConfig');
    });
    it('should generate analytics documentation', async () => {
        const workflow = {
            workflow: { id: 'docs-test', version: '1.0.0', name: 'Docs Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        const docsPath = path.join(testOutputDir, 'analytics/README.md');
        expect(fs.existsSync(docsPath)).toBe(true);
        const docs = fs.readFileSync(docsPath, 'utf-8');
        expect(docs).toContain('# Analytics Infrastructure');
    });
    it('should generate complete analytics infrastructure', async () => {
        const workflow = {
            workflow: { id: 'complete-test', version: '1.0.0', name: 'Complete Test' },
            stack: { api: { type: 'backend', technology: 'typescript/express' } },
            analytics: {
                provider: 'mixpanel',
                tracking: {
                    custom_events: [
                        {
                            name: 'ButtonClicked',
                            properties: ['buttonId'],
                        },
                    ],
                },
                funnels: [
                    {
                        name: 'SignupFlow',
                        steps: [{ event: 'signup_started' }],
                    },
                ],
                cohorts: [
                    {
                        name: 'PowerUsers',
                        criteria: 'engagement_score > 80',
                    },
                ],
                ...{ privacy: { gdpr_compliance: true } },
            },
            stages: { local: { infrastructure: { databases: {}, services: {} } } },
        };
        const _result = await orchestrator.generate(workflow, {
            outputDirectory: testOutputDir,
            dryRun: false,
            verbose: false,
        });
        // Verify all key files exist
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/providers/mixpanel.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/services/analytics.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/events/definitions.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/events/tracker.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/funnels/definitions.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/funnels/tracker.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/cohorts/definitions.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/cohorts/analyzer.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/privacy/consent.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/services/user-properties.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'web/src/hooks/useAnalytics.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'web/src/hooks/useTrackEvent.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'web/src/hooks/useTrackPage.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'web/src/components/AnalyticsProvider.tsx'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/services/analytics.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'api/src/middleware/analytics.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/types/analytics.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/src/config/analytics.ts'))).toBe(true);
        expect(fs.existsSync(path.join(testOutputDir, 'analytics/README.md'))).toBe(true);
    });
});
//# sourceMappingURL=enhanced-analytics.e2e.test.js.map