"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const database_1 = require("../walls/06-memory/database/database");
const TEST_PROJECT = 'test-ci';
describe('CI Reporter', () => {
    beforeAll(() => {
        const db = (0, database_1.getDb)();
        db.prepare('DELETE FROM memory_events WHERE project = ?').run(TEST_PROJECT);
    });
    test('reportCIBuild records success', () => {
        const { reportCIBuild } = require('../walls/06-memory/stores/ci-reporter');
        const id = reportCIBuild({
            project: TEST_PROJECT, branch: 'main', buildId: 'b1',
            status: 'success', duration: 5000, stage: 'build',
            commitHash: 'abc123', commitMessage: 'fix bug', author: 'test',
            timestamp: new Date().toISOString(), ciPlatform: 'github-actions',
        });
        expect(id).toBeGreaterThan(0);
        const db = (0, database_1.getDb)();
        const row = db.prepare('SELECT * FROM memory_events WHERE id = ?').get(id);
        expect(row.type).toBe('verification');
        expect(row.topic).toBe('ci-build');
    });
    test('reportCIBuild records failure as error', () => {
        const { reportCIBuild } = require('../walls/06-memory/stores/ci-reporter');
        const id = reportCIBuild({
            project: TEST_PROJECT, branch: 'dev', buildId: 'b2',
            status: 'failure', duration: 3000, stage: 'test',
            commitHash: 'def456', commitMessage: 'failing test', author: 'test',
            timestamp: new Date().toISOString(), logSnippet: 'Error: test failed',
            ciPlatform: 'gitlab-ci',
        });
        const db = (0, database_1.getDb)();
        const row = db.prepare('SELECT * FROM memory_events WHERE id = ?').get(id);
        expect(row.type).toBe('error');
        expect(row.topic).toBe('ci-build-failed');
    });
    test('reportCITest records test results', () => {
        const { reportCITest } = require('../walls/06-memory/stores/ci-reporter');
        const id = reportCITest({
            project: TEST_PROJECT, branch: 'main', buildId: 'b3',
            totalTests: 100, passed: 95, failed: 3, skipped: 2,
            duration: 10000, failedTests: ['test1', 'test2', 'test3'],
            coverage: 85, timestamp: new Date().toISOString(),
        });
        expect(id).toBeGreaterThan(0);
        const db = (0, database_1.getDb)();
        const row = db.prepare('SELECT * FROM memory_events WHERE id = ?').get(id);
        expect(row.type).toBe('error');
        expect(row.topic).toBe('ci-test-failed');
    });
    test('reportCITest all passed records verification', () => {
        const { reportCITest } = require('../walls/06-memory/stores/ci-reporter');
        const id = reportCITest({
            project: TEST_PROJECT, branch: 'main', buildId: 'b4',
            totalTests: 50, passed: 50, failed: 0, skipped: 0,
            duration: 5000, failedTests: [], timestamp: new Date().toISOString(),
        });
        const db = (0, database_1.getDb)();
        const row = db.prepare('SELECT * FROM memory_events WHERE id = ?').get(id);
        expect(row.type).toBe('verification');
        expect(row.topic).toBe('ci-test-passed');
    });
    test('reportCIDeploy records deployment', () => {
        const { reportCIDeploy } = require('../walls/06-memory/stores/ci-reporter');
        const id = reportCIDeploy({
            project: TEST_PROJECT, environment: 'production',
            status: 'success', version: 'v1.0.0', duration: 30000,
            timestamp: new Date().toISOString(),
        });
        const db = (0, database_1.getDb)();
        const row = db.prepare('SELECT * FROM memory_events WHERE id = ?').get(id);
        expect(row.type).toBe('publish');
        expect(row.topic).toBe('ci-deploy');
    });
    test('reportCIDeploy failure records error', () => {
        const { reportCIDeploy } = require('../walls/06-memory/stores/ci-reporter');
        const id = reportCIDeploy({
            project: TEST_PROJECT, environment: 'staging',
            status: 'failed', version: 'v1.0.0-rc1', duration: 10000,
            timestamp: new Date().toISOString(),
        });
        const db = (0, database_1.getDb)();
        const row = db.prepare('SELECT * FROM memory_events WHERE id = ?').get(id);
        expect(row.type).toBe('error');
        expect(row.topic).toBe('ci-deploy-failed');
    });
    test('getCISummary aggregates correctly', () => {
        const { getCISummary } = require('../walls/06-memory/stores/ci-reporter');
        const summary = getCISummary(TEST_PROJECT, 30);
        expect(summary.totalBuilds).toBeGreaterThanOrEqual(2);
        expect(summary.successRate).toBeGreaterThanOrEqual(0);
        expect(summary.totalTests).toBeGreaterThanOrEqual(150);
        expect(summary.testPassRate).toBeGreaterThanOrEqual(0);
    });
    test('detectCIFailurePatterns finds patterns', () => {
        const { detectCIFailurePatterns } = require('../walls/06-memory/stores/ci-reporter');
        const patterns = detectCIFailurePatterns(TEST_PROJECT, 30);
        expect(Array.isArray(patterns)).toBe(true);
    });
    test('formatCISummary produces markdown', () => {
        const { getCISummary, formatCISummary } = require('../walls/06-memory/stores/ci-reporter');
        const summary = getCISummary(TEST_PROJECT, 30);
        const formatted = formatCISummary(summary);
        expect(typeof formatted).toBe('string');
        expect(formatted).toContain('CI/CD Summary');
    });
});
//# sourceMappingURL=ci-reporter.test.js.map