"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 os = __importStar(require("os"));
const TEST_PROJECT = 'test-automation';
const TEST_DIR = path.join(os.tmpdir(), '__test_automation_dir__');
function cleanup() {
    try {
        const configPath = path.join(TEST_DIR, '.cell', 'memory', 'automation.json');
        if (fs.existsSync(configPath))
            fs.unlinkSync(configPath);
        const cellDir = path.join(TEST_DIR, '.cell');
        if (fs.existsSync(cellDir))
            fs.rmSync(cellDir, { recursive: true, force: true });
        if (fs.existsSync(TEST_DIR))
            fs.rmSync(TEST_DIR, { recursive: true, force: true });
    }
    catch { }
}
describe('Memory Automation', () => {
    beforeAll(() => {
        cleanup();
        if (!fs.existsSync(TEST_DIR))
            fs.mkdirSync(TEST_DIR, { recursive: true });
    });
    afterAll(() => { cleanup(); });
    test('initAutomationConfig creates config', () => {
        const { initAutomationConfig, loadAutomationConfig } = require('../walls/06-memory/automation/memory-automation');
        const config = initAutomationConfig(TEST_PROJECT, TEST_DIR);
        expect(config.project).toBe(TEST_PROJECT);
        expect(config.projectDir).toBe(TEST_DIR);
        expect(config.autoRecordCommits).toBe(true);
        expect(config.autoRecordTestRuns).toBe(true);
        expect(config.maxCommitsPerSync).toBe(50);
        const loaded = loadAutomationConfig(TEST_DIR);
        expect(loaded).not.toBeNull();
        expect(loaded.project).toBe(TEST_PROJECT);
    });
    test('initAutomationConfig returns existing config', () => {
        const { initAutomationConfig, loadAutomationConfig } = require('../walls/06-memory/automation/memory-automation');
        // Delete existing config to start fresh
        const configPath = path.join(TEST_DIR, '.cell', 'memory', 'automation.json');
        if (fs.existsSync(configPath))
            fs.unlinkSync(configPath);
        const config1 = initAutomationConfig(TEST_PROJECT, TEST_DIR, { maxCommitsPerSync: 25 });
        expect(config1.maxCommitsPerSync).toBe(25);
        // Second call should return existing config (25), not override with 100
        const config2 = initAutomationConfig(TEST_PROJECT, TEST_DIR, { maxCommitsPerSync: 100 });
        expect(config2.maxCommitsPerSync).toBe(25);
    });
    test('isGitRepo returns false for non-git dir', () => {
        const { isGitRepo } = require('../walls/06-memory/automation/memory-automation');
        expect(isGitRepo(TEST_DIR)).toBe(false);
    });
    test('getCurrentBranch returns unknown for non-git', () => {
        const { getCurrentBranch } = require('../walls/06-memory/automation/memory-automation');
        expect(getCurrentBranch(TEST_DIR)).toBe('unknown');
    });
    test('getLatestCommitHash returns null for non-git', () => {
        const { getLatestCommitHash } = require('../walls/06-memory/automation/memory-automation');
        expect(getLatestCommitHash(TEST_DIR)).toBeNull();
    });
    test('getTopicFromCommit classifies correctly', () => {
        const { getTopicFromCommit } = require('../walls/06-memory/automation/memory-automation');
        expect(getTopicFromCommit({ subject: 'fix: null pointer', files: [] })).toBe('bugfix');
        expect(getTopicFromCommit({ subject: 'feat: add auth', files: [] })).toBe('feature');
        expect(getTopicFromCommit({ subject: 'refactor: cleanup utils', files: [] })).toBe('refactor');
        expect(getTopicFromCommit({ subject: 'add unit tests', files: [] })).toBe('feature');
        expect(getTopicFromCommit({ subject: 'docs: update readme', files: [] })).toBe('documentation');
        expect(getTopicFromCommit({ subject: 'security: patch vuln', files: [] })).toBe('security');
        expect(getTopicFromCommit({ subject: 'security: add auth guard', files: [] })).toBe('security');
        expect(getTopicFromCommit({ subject: 'chore: update deps', files: [] })).toBe('general');
    });
    test('getImportanceForCommit returns number', () => {
        const { getImportanceForCommit } = require('../walls/06-memory/automation/memory-automation');
        const imp = getImportanceForCommit({ subject: 'feat: add auth', files: ['src/auth.ts'], insertions: 100, deletions: 10 });
        expect(typeof imp).toBe('number');
        expect(imp).toBeGreaterThanOrEqual(1);
        expect(imp).toBeLessThanOrEqual(10);
    });
    test('detectFileTopics works', () => {
        const { detectFileTopics } = require('../walls/06-memory/automation/memory-automation');
        expect(detectFileTopics(['src/auth.test.ts'])).toContain('testing');
        expect(detectFileTopics(['README.md'])).toContain('documentation');
        expect(detectFileTopics(['package.json'])).toContain('configuration');
        expect(detectFileTopics(['Dockerfile'])).toContain('deployment');
        expect(detectFileTopics(['.env'])).toContain('configuration');
    });
    test('getAutomationStatus returns status', () => {
        const { getAutomationStatus } = require('../walls/06-memory/automation/memory-automation');
        const status = getAutomationStatus(TEST_DIR);
        expect(typeof status.isGitRepo).toBe('boolean');
        expect(typeof status.branch).toBe('string');
        expect(typeof status.configExists).toBe('boolean');
    });
    test('syncGitCommitsToMemory returns empty for non-git', () => {
        const { syncGitCommitsToMemory } = require('../walls/06-memory/automation/memory-automation');
        const result = syncGitCommitsToMemory(TEST_PROJECT, TEST_DIR);
        expect(result.synced).toBe(0);
    });
});
//# sourceMappingURL=memory-automation.test.js.map