"use strict";
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"));
const database_1 = require("../walls/06-memory/database/database");
const memory_event_store_1 = require("../walls/06-memory/stores/memory-event-store");
const context_pipeline_1 = require("../walls/06-memory/retrieval/context-pipeline");
const TEST_PROJECT = 'pipeline-test';
const TEST_DIR = path.join(__dirname, '__test_pipeline__');
function cleanup() {
    try {
        fs.rmSync(TEST_DIR, { recursive: true, force: true });
    }
    catch { /* ignore */ }
}
describe('Context Pipeline', () => {
    beforeAll(() => {
        (0, database_1.getDb)();
        cleanup();
        fs.mkdirSync(TEST_DIR, { recursive: true });
        // Init style files
        const styleDir = path.join(TEST_DIR, '.cell', 'memory', 'style');
        fs.mkdirSync(styleDir, { recursive: true });
        fs.writeFileSync(path.join(styleDir, 'developer-style.json'), JSON.stringify({
            communication: 'Hinglish, simple, direct',
            coding: 'small minimal changes',
            verification: ['npx tsc --noEmit'],
            privacy: ['never store secrets'],
            product_rules: ['OSS/local separate from cloud'],
            preferences: {},
        }));
        fs.writeFileSync(path.join(styleDir, 'project-style.json'), JSON.stringify({
            architecture: 'feature-based walls',
            naming: 'kebab-case files, camelCase functions',
            testing: 'jest with maxWorkers: 1',
            stack: ['TypeScript', 'Node.js', 'SQLite'],
            conventions: ['run tsc after every change'],
            entry_points: ['src/index.ts'],
            module_system: 'commonjs',
        }));
    });
    afterEach(() => {
        const db = (0, database_1.getDb)();
        db.prepare('DELETE FROM memory_events WHERE project = ?').run(TEST_PROJECT);
        db.prepare('DELETE FROM sessions WHERE project = ?').run(TEST_PROJECT);
    });
    afterAll(() => {
        cleanup();
    });
    it('buildContext returns context response', () => {
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'decision', summary: 'Use SQLite', importance: 9, topic: 'database' });
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'error', summary: 'FTS corruption', importance: 8 });
        const ctx = (0, context_pipeline_1.buildContext)({ project: TEST_PROJECT, projectDir: TEST_DIR });
        expect(ctx.project).toBe(TEST_PROJECT);
        expect(ctx.sources.length).toBeGreaterThan(0);
        expect(ctx.compactContext.length).toBeGreaterThan(0);
        expect(ctx.stats.totalSources).toBeGreaterThan(0);
    });
    it('buildContext with topic filter', () => {
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'decision', summary: 'Use zstd', importance: 7, topic: 'compression' });
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'decision', summary: 'Use AES', importance: 8, topic: 'encryption' });
        const ctx = (0, context_pipeline_1.buildContext)({ project: TEST_PROJECT, projectDir: TEST_DIR, topic: 'compression' });
        expect(ctx.sources.length).toBeGreaterThan(0);
    });
    it('getShortContext returns string', () => {
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'edit', summary: 'Edited file', importance: 3 });
        const result = (0, context_pipeline_1.getShortContext)(TEST_PROJECT, TEST_DIR);
        expect(typeof result).toBe('string');
        expect(result.length).toBeGreaterThan(0);
    });
    it('getTopicContext returns string', () => {
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'decision', summary: 'Topic test', importance: 6, topic: 'test-topic' });
        const result = (0, context_pipeline_1.getTopicContext)(TEST_PROJECT, TEST_DIR, 'test-topic');
        expect(typeof result).toBe('string');
        expect(result).toContain('test-topic');
    });
    it('buildContext mode=full has more sources', () => {
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'decision', summary: 'Dec 1', importance: 7 });
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'fix', summary: 'Fix 1', importance: 6 });
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'error', summary: 'Err 1', importance: 5 });
        const short = (0, context_pipeline_1.buildContext)({ project: TEST_PROJECT, projectDir: TEST_DIR, mode: 'short' });
        const full = (0, context_pipeline_1.buildContext)({ project: TEST_PROJECT, projectDir: TEST_DIR, mode: 'full' });
        expect(full.sources.length).toBeGreaterThanOrEqual(short.sources.length);
    });
});
//# sourceMappingURL=context-pipeline.test.js.map