import test from "ava";
import { Site } from "../../lib/models.js";

test('test site model basics work', async (t) => {
  try {
    let result = await Site.set("test_number", 1);
    t.not(result, undefined);

    let test_1 = await Site.get("test_number");
    t.is(test_1, 1);

    result = await Site.set("test_number", 100);
    t.not(result, undefined);
  } catch (error) {
    console.log(error);
    t.fail(error.message);
  } finally {
    await Site.delete({key: "test_number"});
  }
})

test('test site model works with complex types', async (t) => {
  try {
    let result = await Site.set("test_complex", {name: "Zed", age: 100});
    t.not(result, undefined);

    let test_2 = await Site.get("test_complex");
    t.is(test_2.name, "Zed");
    t.is(test_2.age, 100);

    result = await Site.set("test_complex", {name: "Zed", age: 1});
    t.not(result, undefined);

    let test_3 = await Site.get("test_complex");
    t.is(test_3.name, "Zed");
    t.is(test_3.age, 1);

  } catch (error) {
    console.log(error);
    t.fail(error.message);
  } finally {
    await Site.delete({key: "test_complex"});
  }
})

test('test increment and decrement counters', async (t) => {
  try {
    let result = await Site.set("test_inc", 0);
    t.not(result, undefined);

    result = await Site.increment("test_inc", 1);
    t.not(result, undefined);

    let test_2 = await Site.get("test_inc");
    t.is(test_2, 1);

    result = await Site.decrement("test_inc", 10);
    t.not(result, undefined);

    let test_3 = await Site.get("test_inc");
    t.is(test_3, -9);
  } catch (error) {
    console.log(error);
    t.fail(error.message);
  } finally {
    await Site.delete({key: "test_inc"});
  }
});
