import { expect } from 'e2e';
import { execFileSync } from 'node:child_process';
import { test, WEB } from './t3.ts';

test('Pull request comment tooltips honor the Time format preference', WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project('clock-demo');
  execFileSync('git', ['remote', 'add', 'origin', 'https://github.com/example/clock-demo.git'], { cwd: project });
  const thread = await t3.thread({ project, title: 'Clock preference demo', messages: ['Show a synthetic review workspace.'] });
  await t3.open(thread);
  await app.open('/settings/general');
  await screen.getByRole('combobox', 'Timestamp format').tap();
  await screen.getByRole('option', '24-hour').tap();
  await app.screenshot('setting-24-hour');
  await app.open('/pull-requests?repository=example%2Fclock-demo&number=1&selectedUrl=https%3A%2F%2Fgithub.com%2Fexample%2Fclock-demo%2Fpull%2F1');
  await screen.getByRole('button', 'Summary').tap();
  await browser.locator('time[datetime="2026-11-12T18:05:32.000Z"]').hover();
  await browser.locator('[data-slot="tooltip-popup"]').textContent();
  await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 500)));
  await app.screenshot('comment-24-hour');
  const clock24 = await browser.locator('[data-slot="tooltip-popup"]').textContent();
  await app.open('/settings/general');
  await screen.getByRole('combobox', 'Timestamp format').tap();
  await screen.getByRole('option', '12-hour').tap();
  await app.screenshot('setting-12-hour');
  await app.open('/pull-requests?repository=example%2Fclock-demo&number=1&selectedUrl=https%3A%2F%2Fgithub.com%2Fexample%2Fclock-demo%2Fpull%2F1');
  await browser.locator('time[datetime="2026-11-12T18:05:32.000Z"]').hover();
  await browser.locator('[data-slot="tooltip-popup"]').textContent();
  await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 500)));
  await app.screenshot('comment-12-hour');
  const clock12 = await browser.locator('[data-slot="tooltip-popup"]').textContent();
  await app.open('/settings/general');
  await screen.getByRole('combobox', 'Timestamp format').tap();
  await screen.getByRole('option', 'System default').tap();
  await app.screenshot('setting-system-default');
  await app.open('/pull-requests?repository=example%2Fclock-demo&number=1&selectedUrl=https%3A%2F%2Fgithub.com%2Fexample%2Fclock-demo%2Fpull%2F1');
  await browser.locator('time[datetime="2026-11-12T18:05:32.000Z"]').hover();
  await browser.locator('[data-slot="tooltip-popup"]').textContent();
  await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 500)));
  await app.screenshot('comment-system-default');
  const clockSystem = await browser.locator('[data-slot="tooltip-popup"]').textContent();
  expect(clockSystem).toContain('PM');
  expect(clock12).toContain('PM');
  expect(clock24).toContain('18:05');
  expect(clock24).not.toMatch(/AM|PM/);
});
