/*
This file is part of the Notesnook project (https://notesnook.com/)

Copyright (C) 2023 Streetwriters (Private) Limited

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import { notebookTest, TEST_NOTEBOOK } from "./utils/index.ts";
import { test, expect } from "vitest";

test("add a notebook", () =>
  notebookTest().then(async ({ db, id }) => {
    expect(id).toBeDefined();
    const notebook = await db.notebooks.notebook(id);
    expect(notebook).toBeDefined();
    expect(notebook.title).toBe(TEST_NOTEBOOK.title);
  }));

test("get all notebooks", () =>
  notebookTest().then(async ({ db }) => {
    expect(await db.notebooks.all.count()).toBeGreaterThan(0);
  }));

test("pin a notebook", () =>
  notebookTest().then(async ({ db, id }) => {
    await db.notebooks.pin(true, id);
    const notebook = await db.notebooks.notebook(id);
    expect(notebook.pinned).toBe(true);
  }));

test("unpin a notebook", () =>
  notebookTest().then(async ({ db, id }) => {
    await db.notebooks.pin(false, id);
    const notebook = await db.notebooks.notebook(id);
    expect(notebook.pinned).toBe(false);
  }));

test("updating notebook with empty title should throw", () =>
  notebookTest().then(async ({ db, id }) => {
    await expect(db.notebooks.add({ id, title: "" })).rejects.toThrow();
  }));

test("parentId() returns parentId if notebook is a subnotebook", () =>
  notebookTest().then(async ({ db, id }) => {
    const subId = await db.notebooks.add({ title: "Sub", id });
    await db.relations.add(
      { type: "notebook", id },
      { type: "notebook", id, subId }
    );
    expect(await db.notebooks.parentId(subId)).toBe(id);
  }));

test("parentId() returns undefined if notebook is not a subnotebook", () =>
  notebookTest().then(async ({ db, id }) => {
    expect(await db.notebooks.parentId(id)).toBeUndefined();
  }));
