import { expect, test } from '@/e2e/helper';

test.describe('Acceptance | Dashboard', { tag: '@acceptance' }, () => {
  test('shows "page requires authentication" error when not logged in', async ({ page }) => {
    await page.goto('/dashboard');
    await expect(page).toHaveURL('/dashboard');
    await expect(page.locator('[data-test-title]')).toHaveText('This page requires authentication');
    await expect(page.locator('[data-test-login]')).toBeVisible();
  });

  test('shows the dashboard when logged in', async ({ page, msw, percy }) => {
    let user = await msw.db.user.create({
      login: 'johnnydee',
      name: 'John Doe',
      email: 'john@doe.com',
      avatar: 'https://avatars2.githubusercontent.com/u/1234567?v=4',
    });

    await msw.authenticateAs(user);

    {
      let crate = await msw.db.crate.create({ name: 'rand' });
      await msw.db.version.create({ crate, num: '0.5.0' });
      await msw.db.version.create({ crate, num: '0.6.0' });
      await msw.db.version.create({ crate, num: '0.7.0' });
      await msw.db.version.create({ crate, num: '0.7.1' });
      await msw.db.version.create({ crate, num: '0.7.2' });
      await msw.db.version.create({ crate, num: '0.7.3' });
      await msw.db.version.create({ crate, num: '0.8.0' });
      await msw.db.version.create({ crate, num: '0.8.1' });
      await msw.db.version.create({ crate, num: '0.9.0' });
      await msw.db.version.create({ crate, num: '1.0.0' });
      await msw.db.version.create({ crate, num: '1.1.0' });
      user = await msw.db.user.update(q => q.where({ id: user.id }), {
        data(u) {
          u.followedCrates = [...u.followedCrates, crate];
        },
      });
    }

    {
      let crate = await msw.db.crate.create({ name: 'nanomsg', downloads: 3892 });
      await msw.db.crateOwnership.create({ crate, user });
      await msw.db.version.create({ crate, num: '0.1.0' });
      user = await msw.db.user.update(q => q.where({ id: user.id }), {
        data(u) {
          u.followedCrates = [...u.followedCrates, crate];
        },
      });
    }

    await page.goto('/dashboard');
    await expect(page).toHaveURL('/dashboard');
    await expect(page.locator('[data-test-feed-list]')).toBeVisible();
    await percy.snapshot();
    await expect(page).toMatchAriaSnapshot({ name: 'aria.yml' });
  });
});
