import { describe, expect, test } from "bun:test"
import fs from "fs/promises"
import path from "path"
import { SaeeolSessionPrompt } from "../../src/overlay/session/prompt"
import { tmpdir } from "../fixture/fixture"

describe("SaeeolSessionPrompt.ensurePlanDir", () => {
  test("creates a missing plan directory", async () => {
    await using tmp = await tmpdir({})
    const dir = path.join(tmp.path, ".saeeol", "plans")
    await SaeeolSessionPrompt.ensurePlanDir(dir)
    const stat = await fs.stat(dir)
    expect(stat.isDirectory()).toBe(true)
  })

  test("is idempotent when the directory already exists", async () => {
    await using tmp = await tmpdir({})
    const dir = path.join(tmp.path, ".saeeol", "plans")
    await fs.mkdir(dir, { recursive: true })
    await expect(SaeeolSessionPrompt.ensurePlanDir(dir)).resolves.toBeUndefined()
    const stat = await fs.stat(dir)
    expect(stat.isDirectory()).toBe(true)
  })

  test("creates intermediate parent directories", async () => {
    await using tmp = await tmpdir({})
    const dir = path.join(tmp.path, "deep", "nested", ".saeeol", "plans")
    await SaeeolSessionPrompt.ensurePlanDir(dir)
    const stat = await fs.stat(dir)
    expect(stat.isDirectory()).toBe(true)
  })
})
