import test from 'ava';
import {login, expect, tid, playstart, playstop} from '../../lib/testing.js';
import { User } from "../../lib/models.js";
import { base_host } from "../../lib/config.js";

test.before(async t => t.context = await playstart(`${base_host}/client/#/login/`));

test.after(async t => {
  await playstop(t.context.browser, t.context.p);
});

test('test /login/ works', async (t) => {
  const {p} = t.context;
  let user;

  try {
    user = await login(t, p);

    await p.goto(`${base_host}/client/#/email/unsubscribe/${user.unsubkey}/`);
    await expect(t, p, tid("page-unsubscribe"));
    await expect(t, p, tid("unsubscribe-good"));

    await p.goto(`${base_host}/client/#/email/unsubscribe/deadbeef000/`);
    await p.reload();
    await expect(t, p, tid("page-unsubscribe"));
    await expect(t, p, tid("unsubscribe-fail"));
  } catch (error) {
    console.log(error);
    t.fail(error.message);
  } finally {
    if(user) await User.delete({id: user.id});
  }
});
