"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"));
describe('Branch Snapshot (Phase 5)', () => {
    let testHome;
    let originalUserProfile;
    beforeAll(() => {
        originalUserProfile = process.env.USERPROFILE;
        testHome = path.join(os.tmpdir(), 'fivo-cell-branch-' + Date.now());
        process.env.USERPROFILE = testHome;
    });
    afterAll(() => {
        process.env.USERPROFILE = originalUserProfile;
        const { closeDb } = require('../walls/06-memory/database/database');
        try {
            closeDb();
        }
        catch { }
        if (fs.existsSync(testHome)) {
            try {
                fs.rmSync(testHome, { recursive: true, force: true });
            }
            catch { }
        }
    });
    describe('saveBranchSnapshot / loadBranchSnapshot', () => {
        it('saves and loads a branch snapshot', async () => {
            const { saveBranchSnapshot, loadBranchSnapshot } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            saveBranchSnapshot({
                project: 'test-proj',
                branch: 'feature-auth',
                goal: 'Refresh token redesign',
                filesTouched: ['auth.ts', 'jwt.ts'],
                pendingTasks: ['middleware rewrite'],
                recentErrors: ['token rotation test failing'],
                recentDecisions: ['switched to RS256'],
            });
            const loaded = loadBranchSnapshot('test-proj', 'feature-auth');
            expect(loaded).not.toBeNull();
            expect(loaded.branch).toBe('feature-auth');
            expect(loaded.goal).toBe('Refresh token redesign');
            expect(loaded.filesTouched).toEqual(['auth.ts', 'jwt.ts']);
            expect(loaded.pendingTasks).toEqual(['middleware rewrite']);
            expect(loaded.recentErrors).toEqual(['token rotation test failing']);
            expect(loaded.recentDecisions).toEqual(['switched to RS256']);
        });
        it('returns null for non-existent branch', async () => {
            const { loadBranchSnapshot } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            const result = loadBranchSnapshot('test-proj', 'nonexistent-branch');
            expect(result).toBeNull();
        });
        it('updates existing snapshot on re-save', async () => {
            const { saveBranchSnapshot, loadBranchSnapshot } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            saveBranchSnapshot({
                project: 'test-proj2',
                branch: 'fix-bug',
                goal: 'Fix login crash',
                filesTouched: ['login.ts'],
            });
            saveBranchSnapshot({
                project: 'test-proj2',
                branch: 'fix-bug',
                goal: 'Fix login crash (updated)',
                filesTouched: ['login.ts', 'auth.ts', 'session.ts'],
            });
            const loaded = loadBranchSnapshot('test-proj2', 'fix-bug');
            expect(loaded.goal).toBe('Fix login crash (updated)');
            expect(loaded.filesTouched.length).toBe(3);
        });
        it('handles empty arrays', async () => {
            const { saveBranchSnapshot, loadBranchSnapshot } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            saveBranchSnapshot({
                project: 'test-proj3',
                branch: 'chore-cleanup',
                filesTouched: [],
                pendingTasks: [],
                recentErrors: [],
                recentDecisions: [],
            });
            const loaded = loadBranchSnapshot('test-proj3', 'chore-cleanup');
            expect(loaded).not.toBeNull();
            expect(loaded.filesTouched).toEqual([]);
            expect(loaded.pendingTasks).toEqual([]);
            expect(loaded.recentErrors).toEqual([]);
            expect(loaded.recentDecisions).toEqual([]);
        });
        it('handles optional fields as undefined', async () => {
            const { saveBranchSnapshot, loadBranchSnapshot } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            saveBranchSnapshot({
                project: 'test-proj4',
                branch: 'minimal',
            });
            const loaded = loadBranchSnapshot('test-proj4', 'minimal');
            expect(loaded).not.toBeNull();
            expect(loaded.goal).toBeUndefined();
            expect(loaded.filesTouched).toEqual([]);
        });
    });
    describe('listBranchSnapshots', () => {
        it('lists all snapshots for a project', async () => {
            const { saveBranchSnapshot, listBranchSnapshots } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            saveBranchSnapshot({ project: 'list-test', branch: 'feature-a' });
            saveBranchSnapshot({ project: 'list-test', branch: 'fix-b' });
            saveBranchSnapshot({ project: 'list-test', branch: 'chore-c' });
            const list = listBranchSnapshots('list-test');
            expect(list.length).toBeGreaterThanOrEqual(3);
            const names = list.map(s => s.branch);
            expect(names).toContain('feature-a');
            expect(names).toContain('fix-b');
            expect(names).toContain('chore-c');
        });
        it('returns empty array for project with no snapshots', async () => {
            const { listBranchSnapshots } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            const list = listBranchSnapshots('nonexistent-project-xyz');
            expect(list).toEqual([]);
        });
        it('does not mix snapshots across projects', async () => {
            const { saveBranchSnapshot, listBranchSnapshots } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            saveBranchSnapshot({ project: 'proj-isolation-a', branch: 'main' });
            saveBranchSnapshot({ project: 'proj-isolation-b', branch: 'main' });
            const listA = listBranchSnapshots('proj-isolation-a');
            const listB = listBranchSnapshots('proj-isolation-b');
            expect(listA.length).toBe(1);
            expect(listB.length).toBe(1);
        });
    });
    describe('deleteBranchSnapshot', () => {
        it('deletes a branch snapshot', async () => {
            const { saveBranchSnapshot, loadBranchSnapshot, deleteBranchSnapshot } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            saveBranchSnapshot({ project: 'del-test', branch: 'to-delete' });
            expect(loadBranchSnapshot('del-test', 'to-delete')).not.toBeNull();
            const result = deleteBranchSnapshot('del-test', 'to-delete');
            expect(result).toBe(true);
            expect(loadBranchSnapshot('del-test', 'to-delete')).toBeNull();
        });
        it('returns false for non-existent snapshot', async () => {
            const { deleteBranchSnapshot } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/branch-snapshot')));
            const result = deleteBranchSnapshot('del-test', 'nonexistent');
            expect(result).toBe(false);
        });
    });
    describe('GitBranchPreGenerator fallback', () => {
        it('parses feature branch name', async () => {
            const { GitBranchPreGenerator } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/git-branch-pregenerator')));
            const gen = new GitBranchPreGenerator();
            const parsed = gen.parseBranchName('feature/auth-refresh');
            expect(parsed.type).toBe('feature');
            expect(parsed.name).toBe('auth-refresh');
            expect(parsed.intent).toBe('Auth Refresh');
        });
        it('parses fix branch name', async () => {
            const { GitBranchPreGenerator } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/git-branch-pregenerator')));
            const gen = new GitBranchPreGenerator();
            const parsed = gen.parseBranchName('fix/login-crash');
            expect(parsed.type).toBe('fix');
        });
        it('defaults to feature for plain name', async () => {
            const { GitBranchPreGenerator } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/git-branch-pregenerator')));
            const gen = new GitBranchPreGenerator();
            const parsed = gen.parseBranchName('my-branch');
            expect(parsed.type).toBe('feature');
        });
        it('generates expected files', async () => {
            const { GitBranchPreGenerator } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/git-branch-pregenerator')));
            const gen = new GitBranchPreGenerator();
            const files = gen.generateFiles('feature/auth', [], 'typescript');
            expect(files.length).toBeGreaterThan(0);
            expect(files.some(f => f.type === 'model')).toBe(true);
            expect(files.some(f => f.type === 'test')).toBe(true);
        });
        it('getIntentSummary returns readable string', async () => {
            const { GitBranchPreGenerator } = await Promise.resolve().then(() => __importStar(require('../walls/03-knowledge/decisions/git-branch-pregenerator')));
            const gen = new GitBranchPreGenerator();
            const summary = gen.getIntentSummary('feature/auth-refresh');
            expect(summary).toContain('New feature');
            expect(summary).toContain('Auth Refresh');
        });
    });
});
//# sourceMappingURL=branch-context.test.js.map