import { expect } from 'e2e';
import { test, WEB } from './t3.ts';

// Run with the accompanying forge-fixture.mjs preloaded through NODE_OPTIONS.
// It supplies only external GitHub responses; the app itself runs unchanged.
test('Numeric PR search ranks exact number, then title, then description', WEB, async ({ t3, app, screen, browser }) => {
  await t3.project('Numeric search demo', { '.git/config': '[remote "origin"]\n  url = https://github.com/scenario/numeric-search.git\n  fetch = +refs/heads/*:refs/remotes/origin/*\n' });
  await t3.open('/pull-requests?state=open&involvement=all');
  const search = screen.getByLabel('Search pull requests');
  await search.fill('24');
  await browser.keyboard.press('Enter');
  await screen.getByText('fix(web): refresh pending review state', { exact: true }).waitFor({ state: 'visible' });
  await browser.locator('[data-pull-request-key]').filter({ hasText: '#29756' }).waitFor({ state: 'hidden' });
  await app.screenshot('numeric-24-description-matches');
  await search.fill('60');
  await browser.keyboard.press('Enter');
  await screen.getByText('fix(mobile): scheduled tasks due within the hour no longer read "Next run in 60 minutes"', { exact: true }).waitFor({ state: 'visible' });
  await screen.getByText('fix(web): refresh pending review state', { exact: true }).waitFor({ state: 'visible' });
  await screen.getByText('fix(server): preserve queued work', { exact: true }).waitFor({ state: 'visible' });
  await browser.keyboard.press('Tab');
  await app.screenshot('numeric-60-ranked-results');
  const actual = await browser.evaluate(() => [...document.querySelectorAll('[data-pull-request-key]')].map((row) => ({
    number: Number(row.textContent?.match(/#(\d+)/)?.[1]),
    descriptionBadge: row.textContent?.includes('matched in the description') ?? false,
  })));
  expect(actual).toEqual([
    { number: 60, descriptionBadge: false },
    { number: 29756, descriptionBadge: false },
    { number: 33274, descriptionBadge: true },
    { number: 33523, descriptionBadge: true },
  ]);
});
