import { describe, expect, it } from "vitest";
import { buildGracefulShutdownPlan, shouldAutoCleanOrphanProcesses, shouldCleanupPersistedSessionsOnStartup, } from "./runtime-lifecycle-policy.js";
describe("runtime lifecycle policy", () => {
    it("keeps agent processes and persisted state during scheduler restart preservation", () => {
        expect(buildGracefulShutdownPlan(true)).toEqual({
            terminateSdkSessions: false,
            closePtySessions: false,
            terminateResidualProcesses: false,
            unregisterInstance: false,
            clearPersistedSessions: false,
        });
    });
    it("cleans runtime state for explicit bridge shutdown", () => {
        expect(buildGracefulShutdownPlan(false)).toEqual({
            terminateSdkSessions: true,
            closePtySessions: true,
            terminateResidualProcesses: true,
            unregisterInstance: true,
            clearPersistedSessions: true,
        });
    });
    it("does not destructively clean persisted sessions on startup unless explicitly enabled", () => {
        expect(shouldCleanupPersistedSessionsOnStartup(false, {})).toBe(false);
        expect(shouldCleanupPersistedSessionsOnStartup(false, {
            AWS_BRIDGE_CLEAN_ORPHANS_ON_STARTUP: "true",
        })).toBe(true);
        expect(shouldCleanupPersistedSessionsOnStartup(true, {
            AWS_BRIDGE_CLEAN_ORPHANS_ON_STARTUP: "true",
        })).toBe(false);
    });
    it("does not let orphan monitor terminate preserved sessions unless cleanup is explicit", () => {
        expect(shouldAutoCleanOrphanProcesses({})).toBe(false);
        expect(shouldAutoCleanOrphanProcesses({
            AWS_ORPHAN_AUTO_CLEAN_MODE: "auto",
        })).toBe(false);
        expect(shouldAutoCleanOrphanProcesses({
            AWS_ORPHAN_AUTO_CLEAN_MODE: "auto",
            AWS_BRIDGE_CLEAN_ORPHANS_ON_STARTUP: "true",
        })).toBe(true);
    });
});
