import { afterEach, describe, expect, it, vi } from 'vitest';
const isEngineSealedMock = vi.fn();
const loadInstalledPacksMock = vi.fn();
vi.mock('@mmnto/totem', async () => {
    const actual = await vi.importActual('@mmnto/totem');
    return {
        ...actual,
        isEngineSealed: () => isEngineSealedMock(),
        loadInstalledPacks: ((options) => loadInstalledPacksMock(options)),
    };
});
afterEach(() => {
    isEngineSealedMock.mockReset();
    loadInstalledPacksMock.mockReset();
});
function makeConfig(overrides = {}) {
    return {
        targets: [],
        totemDir: '.totem',
        ignorePatterns: [],
        ...overrides,
    };
}
describe('bootstrapEngine', () => {
    it('invokes loadInstalledPacks with projectRoot and config.totemDir when engine is unsealed', async () => {
        isEngineSealedMock.mockReturnValue(false);
        const { bootstrapEngine } = await import('./bootstrap-engine.js');
        await bootstrapEngine(makeConfig(), '/abs/repo-root');
        expect(loadInstalledPacksMock).toHaveBeenCalledTimes(1);
        expect(loadInstalledPacksMock).toHaveBeenCalledWith({
            projectRoot: '/abs/repo-root',
            totemDir: '.totem',
        });
    });
    it('short-circuits silently when engine is already sealed (test-harness re-entry)', async () => {
        isEngineSealedMock.mockReturnValue(true);
        const { bootstrapEngine } = await import('./bootstrap-engine.js');
        await bootstrapEngine(makeConfig(), '/abs/repo-root');
        expect(loadInstalledPacksMock).not.toHaveBeenCalled();
    });
    it('propagates pack-callback errors verbatim — does not catch or rewrap', async () => {
        isEngineSealedMock.mockReturnValue(false);
        const cause = new Error("Pack 'broken' registration callback threw.");
        loadInstalledPacksMock.mockImplementation(() => {
            throw cause;
        });
        const { bootstrapEngine } = await import('./bootstrap-engine.js');
        // .toBe(cause) instead of .toThrowError(cause) — the contract is
        // "propagate the original Error instance verbatim," not just "throw
        // an error with this message." A fresh Error with the same message
        // would slip past .toThrowError; .toBe pins the exact instance.
        await expect(bootstrapEngine(makeConfig(), '/abs/repo-root')).rejects.toBe(cause);
    });
    it('passes a non-default totemDir through unchanged (monorepo subpackage case)', async () => {
        isEngineSealedMock.mockReturnValue(false);
        const { bootstrapEngine } = await import('./bootstrap-engine.js');
        await bootstrapEngine(makeConfig({ totemDir: '.custom-totem' }), '/abs/repo-root/sub');
        expect(loadInstalledPacksMock).toHaveBeenCalledWith({
            projectRoot: '/abs/repo-root/sub',
            totemDir: '.custom-totem',
        });
    });
});
//# sourceMappingURL=bootstrap-engine.test.js.map