"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_test_1 = require("node:test");
const strict_1 = __importDefault(require("node:assert/strict"));
const fs = __importStar(require("node:fs"));
const os = __importStar(require("node:os"));
const path = __importStar(require("node:path"));
const lockfile_1 = require("../lockfile");
// ─── helpers ──────────────────────────────────────────────────────────────────
let tmpDir;
function writeLock(cwd, pid) {
    fs.writeFileSync((0, lockfile_1.lockfilePath)(cwd), String(pid), 'utf8');
}
// ─── setup / teardown ─────────────────────────────────────────────────────────
(0, node_test_1.before)(() => {
    tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'aidev-lock-test-'));
});
(0, node_test_1.afterEach)(() => {
    // Clean up lock file between tests
    try {
        fs.unlinkSync((0, lockfile_1.lockfilePath)(tmpDir));
    }
    catch { /* ok */ }
});
(0, node_test_1.after)(() => {
    try {
        fs.rmdirSync(tmpDir);
    }
    catch { /* ok */ }
});
// ─── lockfilePath ─────────────────────────────────────────────────────────────
(0, node_test_1.describe)('lockfilePath', () => {
    (0, node_test_1.it)('returns path ending with LOCK_FILENAME inside cwd', () => {
        const p = (0, lockfile_1.lockfilePath)('/some/dir');
        strict_1.default.equal(p, path.join('/some/dir', lockfile_1.LOCK_FILENAME));
    });
});
// ─── isProcessAlive ───────────────────────────────────────────────────────────
(0, node_test_1.describe)('isProcessAlive', () => {
    (0, node_test_1.it)('returns true for the current process', () => {
        strict_1.default.equal((0, lockfile_1.isProcessAlive)(process.pid), true);
    });
    (0, node_test_1.it)('returns false for a PID that does not exist', () => {
        // PID 0 is the kernel — kill(0, 0) targets the current process group,
        // so use a very high number that is almost certainly unused.
        // We rely on errno ESRCH (no such process).
        strict_1.default.equal((0, lockfile_1.isProcessAlive)(999999999), false);
    });
});
// ─── readLock ─────────────────────────────────────────────────────────────────
(0, node_test_1.describe)('readLock', () => {
    (0, node_test_1.it)('returns null when no lock file exists', () => {
        strict_1.default.equal((0, lockfile_1.readLock)(tmpDir), null);
    });
    (0, node_test_1.it)('returns the PID stored in the lock file', () => {
        writeLock(tmpDir, 1234);
        strict_1.default.equal((0, lockfile_1.readLock)(tmpDir), 1234);
    });
    (0, node_test_1.it)('returns null for non-numeric content', () => {
        fs.writeFileSync((0, lockfile_1.lockfilePath)(tmpDir), 'not-a-pid', 'utf8');
        strict_1.default.equal((0, lockfile_1.readLock)(tmpDir), null);
    });
});
// ─── acquireLock ──────────────────────────────────────────────────────────────
(0, node_test_1.describe)('acquireLock', () => {
    (0, node_test_1.it)('succeeds when no lock file exists', () => {
        const ok = (0, lockfile_1.acquireLock)(tmpDir);
        strict_1.default.equal(ok, true);
        strict_1.default.equal((0, lockfile_1.readLock)(tmpDir), process.pid);
    });
    (0, node_test_1.it)('succeeds and overwrites a stale lock file (dead PID)', () => {
        writeLock(tmpDir, 999999999); // dead PID
        const ok = (0, lockfile_1.acquireLock)(tmpDir);
        strict_1.default.equal(ok, true);
        strict_1.default.equal((0, lockfile_1.readLock)(tmpDir), process.pid);
    });
    (0, node_test_1.it)('fails when a live process holds the lock', () => {
        writeLock(tmpDir, process.pid); // current process = alive
        const ok = (0, lockfile_1.acquireLock)(tmpDir);
        strict_1.default.equal(ok, false);
    });
});
// ─── releaseLock ──────────────────────────────────────────────────────────────
(0, node_test_1.describe)('releaseLock', () => {
    (0, node_test_1.it)('removes the lock file', () => {
        writeLock(tmpDir, process.pid);
        (0, lockfile_1.releaseLock)(tmpDir);
        strict_1.default.equal(fs.existsSync((0, lockfile_1.lockfilePath)(tmpDir)), false);
    });
    (0, node_test_1.it)('does not throw when the file is already absent', () => {
        strict_1.default.doesNotThrow(() => (0, lockfile_1.releaseLock)(tmpDir));
    });
});
// ─── stopProcess ──────────────────────────────────────────────────────────────
(0, node_test_1.describe)('stopProcess', () => {
    (0, node_test_1.it)('returns "no-lock" when no lock file exists', () => {
        strict_1.default.equal((0, lockfile_1.stopProcess)(tmpDir), 'no-lock');
    });
    (0, node_test_1.it)('returns "not-running" and removes a stale lock', () => {
        writeLock(tmpDir, 999999999); // dead PID
        const result = (0, lockfile_1.stopProcess)(tmpDir);
        strict_1.default.equal(result, 'not-running');
        strict_1.default.equal(fs.existsSync((0, lockfile_1.lockfilePath)(tmpDir)), false);
    });
    // Note: we cannot test 'killed' safely in a unit test without spawning a
    // real child process, so the live-kill path is exercised by integration tests.
});
//# sourceMappingURL=lockfile.test.js.map