import { expect, test } from '@playwright/test';

test('index page redirects to login page', async ({ page }) => {
	await page.goto('/');

	await page.waitForURL('/sign-in');

	await expect(page.url()).toContain('/sign-in');
});

test('sign-in page has a sign-up link that redirects to the sign-up page', async ({ page }) => {
	await page.goto('/sign-in');

	const link = await page.$('a[href*="/sign-up"]');

	await link?.click();

	await page.waitForURL('/sign-up');

	await expect(page.url()).toContain('/sign-up');
});

test('sign-up page has a sign-in link that redirects to the sign-in page', async ({ page }) => {
	await page.goto('/sign-up');

	const link = await page.$('a[href*="/sign-in"]');

	await link?.click();

	await page.waitForURL('/sign-in');

	await expect(page.url()).toContain('/sign-in');
});

test('dashboard page redirects to sign-in page if user is not logged in', async ({ page }) => {
	await page.goto('/dashboard');
	await expect(page.url()).toContain('/sign-in');
});

test('login form accepts valid credentials', async ({ page }) => {
	await page.context().clearCookies();

	await page.goto('/sign-in');

	await page.fill('input[name="pseudo"]', 'glazk0');
	await page.fill('input[name="passwd"]', 'Cookies Are #Miam42');

	await Promise.all([page.getByRole('button').click(), page.waitForURL('/dashboard')]);

	await expect(page.url()).toContain('/dashboard');
});
