"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 activeTask_1 = require("../activeTask");
const lockfile_1 = require("../lockfile");
let tmpDir;
(0, node_test_1.before)(() => {
    tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'aidev-active-task-'));
});
(0, node_test_1.afterEach)(() => {
    try {
        fs.unlinkSync((0, activeTask_1.activeTaskPath)(tmpDir));
    }
    catch { /* ok */ }
    try {
        fs.unlinkSync((0, lockfile_1.lockfilePath)(tmpDir));
    }
    catch { /* ok */ }
});
(0, node_test_1.after)(() => {
    try {
        fs.rmdirSync(tmpDir);
    }
    catch { /* ok */ }
});
(0, node_test_1.describe)('activeTaskPath', () => {
    (0, node_test_1.it)('returns path ending with ACTIVE_TASK_FILENAME inside cwd', () => {
        strict_1.default.equal((0, activeTask_1.activeTaskPath)('/some/dir'), path.join('/some/dir', activeTask_1.ACTIVE_TASK_FILENAME));
    });
});
(0, node_test_1.describe)('writeActiveTask / readActiveTaskFile', () => {
    (0, node_test_1.it)('round-trips a task id', () => {
        (0, activeTask_1.writeActiveTask)(tmpDir, 'CU-123');
        strict_1.default.equal((0, activeTask_1.readActiveTaskFile)(tmpDir), 'CU-123');
    });
    (0, node_test_1.it)('trims whitespace on write', () => {
        (0, activeTask_1.writeActiveTask)(tmpDir, '  CU-456  ');
        strict_1.default.equal((0, activeTask_1.readActiveTaskFile)(tmpDir), 'CU-456');
    });
});
(0, node_test_1.describe)('readActiveTask', () => {
    (0, node_test_1.it)('returns null when lock is absent', () => {
        (0, activeTask_1.writeActiveTask)(tmpDir, 'CU-1');
        strict_1.default.equal((0, activeTask_1.readActiveTask)(tmpDir), null);
    });
    (0, node_test_1.it)('returns task id when lock is held by a live process', () => {
        strict_1.default.ok((0, lockfile_1.acquireLock)(tmpDir));
        (0, activeTask_1.writeActiveTask)(tmpDir, 'CU-live');
        strict_1.default.equal((0, activeTask_1.readActiveTask)(tmpDir), 'CU-live');
        (0, lockfile_1.releaseLock)(tmpDir);
    });
    (0, node_test_1.it)('clears stale active file when lock pid is dead', () => {
        fs.writeFileSync((0, lockfile_1.lockfilePath)(tmpDir), '999999999', 'utf8');
        (0, activeTask_1.writeActiveTask)(tmpDir, 'CU-stale');
        strict_1.default.equal((0, activeTask_1.readActiveTask)(tmpDir), null);
        strict_1.default.equal((0, activeTask_1.readActiveTaskFile)(tmpDir), null);
    });
});
(0, node_test_1.describe)('clearActiveTask', () => {
    (0, node_test_1.it)('removes the file when present', () => {
        (0, activeTask_1.writeActiveTask)(tmpDir, 'CU-x');
        (0, activeTask_1.clearActiveTask)(tmpDir);
        strict_1.default.equal((0, activeTask_1.readActiveTaskFile)(tmpDir), null);
    });
});
//# sourceMappingURL=activeTask.test.js.map