import { parseTodoList, todoListToString, type TodoList } from "./todoList"
import { test, expect } from "bun:test"

test("todoListToString", () => {
  const todoList: TodoList = [
    {
      title: "# Today",
      todos: [
        { done: false, indent: "", text: "a task with a #tag", children: [] },
        { done: true, indent: "", text: "a #tag and date @2/2/25 in a task", children: [] },
      ],
    },
    {
      title: "# Tomorrow",
      todos: [
        {
          done: false,
          indent: "",
          text: "another task",
          children: [
            { done: false, indent: "  ", text: "a subtask", children: [] },
            { done: true, indent: "  ", text: "completed", children: [] },
          ],
        },
        { done: false, indent: "", text: "a task with a @1/2/25 date", children: [] },
      ],
    },
  ]

  const result = todoListToString(todoList)

  const expected = `# Today
[ ] a task with a #tag
[x] a #tag and date @2/2/25 in a task
# Tomorrow
[ ] another task
  [ ] a subtask
  [x] completed
[ ] a task with a @1/2/25 date
`

  expect(result).toEqual(expected)
})

test("parseTodoList", () => {
  const todoListString = `# Today
[ ] a task with a #tag
[x] a #tag and date @2/2/25 in a task

# Tomorrow
[ ] another task
  [ ] a subtask
  [x] completed
  
[ ] a task with a @1/2/25 date`

  const result = parseTodoList(todoListString)

  const expected: TodoList = [
    {
      title: "# Today",
      todos: [
        { done: false, indent: "", text: "a task with a #tag", children: [] },
        { done: true, indent: "", text: "a #tag and date @2/2/25 in a task", children: [] },
        "",
      ],
    },
    {
      title: "# Tomorrow",
      todos: [
        {
          done: false,
          indent: "",
          text: "another task",
          children: [
            { done: false, indent: "  ", text: "a subtask", children: [] },
            { done: true, indent: "  ", text: "completed", children: [] },
          ],
        },
        "  ",
        { done: false, indent: "", text: "a task with a @1/2/25 date", children: [] },
      ],
    },
  ]

  expect(JSON.parse(JSON.stringify(result))).toEqual(expected)
})
