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

test('Confirm usage of test API (may fail if you are running your own Time Safari)', async ({ page }, testInfo) => {
  // Load account view
  await page.goto('./account');
  await page.getByRole('heading', { name: 'Advanced' }).click();

  // look into the config file: if it starts Time Safari, it might say which server it should set by default
  const webServer = testInfo.config.webServer;
  const endorserWords = webServer?.command.split(' ');
  const ENDORSER_ENV_NAME = 'VITE_DEFAULT_ENDORSER_API_SERVER';
  const endorserTerm = endorserWords?.find(word => word.startsWith(ENDORSER_ENV_NAME + '='));
  const endorserTermInConfig = endorserTerm?.substring(ENDORSER_ENV_NAME.length + 1);

  const endorserServer = endorserTermInConfig || 'https://test-api.endorser.ch';
  await expect(page.getByRole('textbox').nth(1)).toHaveValue(endorserServer);
});

test('Check activity feed', async ({ page }) => {
  // Load app homepage
  await page.goto('./');

  // Check that initial 10 activities have been loaded
  await page.locator('ul#listLatestActivity li:nth-child(10)');

  // Scroll down a bit to trigger loading additional activities
  await page.locator('ul#listLatestActivity li:nth-child(50)').scrollIntoViewIfNeeded();
});

test('Check discover results', async ({ page }) => {
  // Load Discover view
  await page.goto('./discover');

  // Check that initial 10 projects have been loaded
  await page.locator('ul#listDiscoverResults li.border-b:nth-child(10)');

  // Scroll down a bit to trigger loading additional projects
  await page.locator('ul#listDiscoverResults li.border-b:nth-child(20)').scrollIntoViewIfNeeded();
});

test('Check no-ID messaging in homepage', async ({ page }) => {
  // Load app homepage
  await page.goto('./');

  // Check 'someone must register you' notice
  await expect(page.getByText('To share, someone must register you.')).toBeVisible();
});

test('Check no-ID messaging in account', async ({ page }) => {
  // Load account view
  await page.goto('./account');

  // Check 'someone must register you' notice
  await expect(page.locator('#noticeBeforeShare')).toBeVisible();

  // Check 'a friend needs to register you' notice
  await expect(page.locator('#noticeBeforeAnnounce')).toBeVisible();

  // Check that there is no ID
  await expect(page.locator('#sectionIdentityDetails code.truncate')).toBeEmpty();
});

test('Check ID generation', async ({ page }) => {
  // Load Account view
  await page.goto('./account');

  // Check that ID is empty
  await expect(page.locator('#sectionIdentityDetails code.truncate')).toBeEmpty();

  // Load homepage to trigger ID generation (?)
  await page.goto('./');

  // Wait for activity feed to start loading, as a delay
  await expect(page.locator('ul#listLatestActivity li:nth-child(10)')).toBeVisible();

  // Go back to Account view
  await page.goto('./account');

  // Check that ID is now generated
  await expect(page.locator('#sectionIdentityDetails code.truncate')).toContainText('did:ethr:');
});