import { describe, expect, test } from "bun:test"
import { SaeeolRunAuto } from "../../src/overlay/cli/run-auto"

describe("SaeeolRunAuto", () => {
  test("tracks task child sessions without allowing unrelated sessions", () => {
    const state = SaeeolRunAuto.create("ses_root")

    expect(SaeeolRunAuto.allowed(state, "ses_root")).toBe(true)
    expect(SaeeolRunAuto.allowed(state, "ses_child")).toBe(false)

    SaeeolRunAuto.track(state, {
      type: "tool",
      tool: "task",
      sessionID: "ses_root",
      state: {
        metadata: {
          sessionId: "ses_child",
        },
      },
    })

    expect(SaeeolRunAuto.allowed(state, "ses_child")).toBe(true)
    expect(SaeeolRunAuto.allowed(state, "ses_other")).toBe(false)
  })

  test("ignores malformed or non-root task metadata", () => {
    const state = SaeeolRunAuto.create("ses_root")

    SaeeolRunAuto.track(state, {
      type: "tool",
      tool: "task",
      sessionID: "ses_root",
      state: {
        metadata: {
          sessionId: "",
        },
      },
    })
    SaeeolRunAuto.track(state, {
      type: "tool",
      tool: "task",
      sessionID: "ses_other",
      state: {
        metadata: {
          sessionId: "ses_wrong",
        },
      },
    })
    SaeeolRunAuto.track(state, {
      type: "text",
      sessionID: "ses_root",
      state: {},
    })

    expect(SaeeolRunAuto.allowed(state, "ses_wrong")).toBe(false)
    expect(SaeeolRunAuto.allowed(state, "")).toBe(false)
  })
})
