import { expect, gotoWorkbench, saveScreenshot, test } from "../fixtures/test";
import { selectors, sessionTab } from "../fixtures/selectors";

test("Code Agent message headers render canonical timing metadata", async ({ page }, testInfo) => {
  await page.clock.install({ time: new Date("2026-06-17T09:39:45.000Z") });
  await gotoWorkbench(page, "/workbench/sessions/ses_running");

  const running = page.locator(`${selectors.messageCard}[data-role="agent"][data-status="running"]`).first();
  await expect(running.locator(`.message-header-main ${selectors.messageDurationMeta}`)).toContainText("耗时 4 秒");
  await expect(running.locator(`.message-header-actions ${selectors.messageActivityMeta}`)).toContainText("最近 0 秒前");
  await expect(running.locator(selectors.messageDurationMeta)).toHaveAttribute("aria-label", "总耗时：4 秒");
  await expect(running.locator(selectors.messageActivityMeta)).toHaveAttribute("aria-label", "最近事件：0 秒前");
  await expect(running.locator(selectors.messageDurationMeta)).not.toContainText("最近");

  await page.clock.fastForward(5000);
  await expect(running.locator(selectors.messageDurationMeta)).toContainText("耗时 9 秒");
  await expect(running.locator(selectors.messageActivityMeta)).toContainText("最近 5 秒前");

  await page.clock.fastForward(60000);
  await expect(running.locator(selectors.messageDurationMeta)).toContainText("耗时 1 分 9 秒");
  await expect(running.locator(selectors.messageActivityMeta)).toContainText("最近 1 分 5 秒前");

  await page.locator(sessionTab("ses_completed")).click();
  const completed = page.locator(`${selectors.messageCard}[data-role="agent"][data-status="completed"]`).first();
  await expect(completed.locator(selectors.messageDurationMeta)).toContainText("耗时 6 秒");
  await expect(completed.locator(selectors.messageActivityMeta)).toHaveCount(0);
  await page.clock.fastForward(60000);
  await expect(completed.locator(selectors.messageDurationMeta)).toContainText("耗时 6 秒");

  await page.locator(sessionTab("ses_failed")).click();
  const failed = page.locator(`${selectors.messageCard}[data-role="agent"][data-status="failed"]`).first();
  await expect(failed.locator(selectors.messageDurationMeta)).toContainText("耗时 2 秒");
  await expect(failed.locator(selectors.messageActivityMeta)).toHaveCount(0);

  await page.locator(sessionTab("ses_canceled")).click();
  const canceled = page.locator(`${selectors.messageCard}[data-role="agent"][data-status="canceled"]`).first();
  await expect(canceled.locator(selectors.messageDurationMeta)).toContainText("耗时 0 秒");
  await expect(canceled.locator(selectors.messageActivityMeta)).toHaveCount(0);

  await expect(page.locator(`${selectors.messageCard}[data-role="user"] ${selectors.messageDurationMeta}`)).toHaveCount(0);
  await expect(page.locator(`${selectors.messageCard}[data-role="user"] ${selectors.messageActivityMeta}`)).toHaveCount(0);
  await saveScreenshot(page, testInfo, "message-timing-headers");
});
