"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 os = __importStar(require("os"));
const path = __importStar(require("path"));
const agents_md_1 = require("../walls/01-context/context/agents-md");
describe('agents-md (AGENTS.md + .cursorrules writers)', () => {
    let tmpDir;
    beforeEach(() => {
        tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cell-agents-md-'));
    });
    afterEach(() => {
        try {
            fs.rmSync(tmpDir, { recursive: true, force: true });
        }
        catch { }
    });
    describe('templates', () => {
        it('AGENTS_MD_TEMPLATE declares FIVO Cell rules and the @cell convention', () => {
            expect(agents_md_1.AGENTS_MD_TEMPLATE).toContain(agents_md_1.CELL_RULES_MARKER);
            expect(agents_md_1.AGENTS_MD_TEMPLATE).toContain('@cell');
            expect(agents_md_1.AGENTS_MD_TEMPLATE).toContain('cell_get_context');
            expect(agents_md_1.AGENTS_MD_TEMPLATE).toContain('cell_session_start');
            expect(agents_md_1.AGENTS_MD_TEMPLATE).toContain('path.basename(process.cwd())');
        });
        it('CURSORRULES_TEMPLATE mentions @cell and cell_get_context', () => {
            expect(agents_md_1.CURSORRULES_TEMPLATE).toContain(agents_md_1.CELL_RULES_MARKER);
            expect(agents_md_1.CURSORRULES_TEMPLATE).toContain('@cell');
            expect(agents_md_1.CURSORRULES_TEMPLATE).toContain('cell_get_context');
        });
        it('isCellRulesFile detects the marker in any file content', () => {
            expect((0, agents_md_1.isCellRulesFile)(agents_md_1.AGENTS_MD_TEMPLATE)).toBe(true);
            expect((0, agents_md_1.isCellRulesFile)('# User custom rules')).toBe(false);
            expect((0, agents_md_1.isCellRulesFile)('partial marker mention')).toBe(false);
            expect((0, agents_md_1.isCellRulesFile)('# fivo-cell:rules:v1\n# body')).toBe(true);
        });
    });
    describe('writeAgentsMd', () => {
        it('writes AGENTS.md to project root', () => {
            const result = (0, agents_md_1.writeAgentsMd)(tmpDir);
            expect(result).toBe('written');
            const filePath = path.join(tmpDir, agents_md_1.AGENTS_MD_FILENAME);
            expect(fs.existsSync(filePath)).toBe(true);
            const content = fs.readFileSync(filePath, 'utf8');
            expect(content).toBe(agents_md_1.AGENTS_MD_TEMPLATE);
        });
        it('does not overwrite existing AGENTS.md (idempotent — respects user edits)', () => {
            const filePath = path.join(tmpDir, agents_md_1.AGENTS_MD_FILENAME);
            const custom = '# My custom team rules\nDo not touch this file.\n';
            fs.writeFileSync(filePath, custom, 'utf8');
            const result = (0, agents_md_1.writeAgentsMd)(tmpDir);
            expect(result).toBe('exists');
            const content = fs.readFileSync(filePath, 'utf8');
            expect(content).toBe(custom);
        });
        it('overwrites existing AGENTS.md when overwrite: true', () => {
            const filePath = path.join(tmpDir, agents_md_1.AGENTS_MD_FILENAME);
            fs.writeFileSync(filePath, 'old content', 'utf8');
            const result = (0, agents_md_1.writeAgentsMd)(tmpDir, { overwrite: true });
            expect(result).toBe('written');
            const content = fs.readFileSync(filePath, 'utf8');
            expect(content).toBe(agents_md_1.AGENTS_MD_TEMPLATE);
        });
    });
    describe('writeCursorRules', () => {
        it('writes .cursorrules to project root', () => {
            const result = (0, agents_md_1.writeCursorRules)(tmpDir);
            expect(result).toBe('written');
            const filePath = path.join(tmpDir, agents_md_1.CURSORRULES_FILENAME);
            expect(fs.existsSync(filePath)).toBe(true);
        });
        it('does not overwrite existing .cursorrules', () => {
            const filePath = path.join(tmpDir, agents_md_1.CURSORRULES_FILENAME);
            fs.writeFileSync(filePath, 'user rules', 'utf8');
            const result = (0, agents_md_1.writeCursorRules)(tmpDir);
            expect(result).toBe('exists');
        });
    });
    describe('writeCellRules', () => {
        it('writes both files on a fresh project', () => {
            const r = (0, agents_md_1.writeCellRules)(tmpDir);
            expect(r.agentsMd).toBe('written');
            expect(r.cursorRules).toBe('written');
            expect(fs.existsSync(path.join(tmpDir, agents_md_1.AGENTS_MD_FILENAME))).toBe(true);
            expect(fs.existsSync(path.join(tmpDir, agents_md_1.CURSORRULES_FILENAME))).toBe(true);
        });
        it('returns exists for both when both files already present', () => {
            fs.writeFileSync(path.join(tmpDir, agents_md_1.AGENTS_MD_FILENAME), 'x', 'utf8');
            fs.writeFileSync(path.join(tmpDir, agents_md_1.CURSORRULES_FILENAME), 'y', 'utf8');
            const r = (0, agents_md_1.writeCellRules)(tmpDir);
            expect(r.agentsMd).toBe('exists');
            expect(r.cursorRules).toBe('exists');
        });
        it('skips .cursorrules when cursor: false', () => {
            const r = (0, agents_md_1.writeCellRules)(tmpDir, { cursor: false });
            expect(r.agentsMd).toBe('written');
            expect(r.cursorRules).toBe('skipped');
            expect(fs.existsSync(path.join(tmpDir, agents_md_1.CURSORRULES_FILENAME))).toBe(false);
        });
    });
    describe('filename constants', () => {
        it('exports the correct filenames', () => {
            expect(agents_md_1.AGENTS_MD_FILENAME).toBe('AGENTS.md');
            expect(agents_md_1.CURSORRULES_FILENAME).toBe('.cursorrules');
        });
    });
});
//# sourceMappingURL=agents-md.test.js.map