import test from "ava";
import {random_user} from "../../lib/testing.js";
import { User, Payment } from "../../lib/models.js";


test('test user model basics work', async (t) => {
  let user = random_user();

  try {
    user.password_repeat = user.password;

    let test1 = await User.register(user);
    t.not(test1, undefined);
    t.not(test1.unsubkey, undefined);
    t.is(test1.full_name, user.full_name);
    // save the id to delete later
    user.id = test1.id;

    let test2 = await User.first({id: test1.id});
    t.is(test1.id, test2.id);
    t.not(test1.full_name, undefined);

    let count = await User.count({id: test2.id});
    t.is(count, 1);

    let all = await User.all({id: test1.id});
    t.is(all.length, 1);
    t.is(all[0].full_name, test1.full_name);

    let test_name = Payment.gen_internal_id();

    let res = await User.update({id: test1.id}, {full_name: test_name});
    t.is(res, 1);

    res = await User.first({full_name: test_name});
    t.is(res.id, test2.id);
    t.is(res.full_name, test_name);

    res = await User.delete({id: test2.id});
    t.is(res, 1);

    count = await User.count({id: test2.id});
    t.is(count, 0);

    all = await User.all({id: test2.id});
    t.is(all.length, 0);

  } catch (error) {
    console.log(error);
    t.fail(error.message);
  } finally {
    await User.delete({id: user.id});
  }
})
