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

test.use({ scenarioId: "deep-link" });

test("deep link hydrates the same authority path as tab selection", async ({ page }, testInfo) => {
  await gotoWorkbench(page, "/workbench/sessions/ses_failed");
  await expect(page.locator(sessionTab("ses_failed"))).toHaveAttribute("data-active", "true");
  await expect(page.locator(`${selectors.messageCard}[data-role="agent"][data-status="failed"]`)).toContainText("缺少受控依赖");
  await expect(page.locator(`${selectors.traceTimeline}[data-status="failed"]`)).toBeVisible();
  await expect(page).toHaveURL(/\/workbench\/sessions\/ses_failed$/u);
  await saveScreenshot(page, testInfo, "deep-link-failed");
});

test.describe("deep link stale session authority", () => {
  test.use({ scenarioId: "deep-link-stale-session-authority" });

  test("deep link persists route session over stale fallback state", async ({ page }) => {
    await gotoWorkbench(page, "/workbench/sessions/ses_failed");
    await expect(page.locator(sessionTab("ses_failed"))).toHaveAttribute("data-active", "true");
    await expect(page.locator(`${selectors.messageCard}[data-role="agent"][data-status="failed"]`)).toContainText("缺少受控依赖");
    await expect(page).toHaveURL(/\/workbench\/sessions\/ses_failed$/u);
    const state = await fakeServerState(page);
    expect((state.legacyRequestLedger as unknown[]).length).toBe(0);
  });
});

test.describe("legacy cnv deep link canonical authority", () => {
  test.use({ scenarioId: "legacy-cnv-deeplink-canonical" });

  test("cnv route resolves through backend to canonical session without stale active fallback", async ({ page }) => {
    await gotoWorkbench(page, "/workbench/sessions/cnv_c5e1330558c94d8e");
    await expect(page).toHaveURL(/\/workbench\/sessions\/ses_c5e1330558c94d8e$/u);
    await expect(page.locator(sessionTab("ses_c5e1330558c94d8e"))).toHaveAttribute("data-active", "true");
    await expect(page.locator(sessionTab("ses_running"))).not.toHaveAttribute("data-active", "true");
    await expect(page.locator(`${selectors.messageCard}[data-role="agent"][data-status="completed"]`)).toContainText("新增 Python benchmark 脚本");
    await expect(page.locator(`${selectors.traceTimeline}[data-status="completed"]`)).toBeVisible();
    const state = await fakeServerState(page);
    const detailRequests = (state.requestLedger as Array<{ path?: string }>).filter((item) => /^\/v1\/workbench\/sessions\/[^/]+$/u.test(item.path ?? ""));
    expect(detailRequests[0]?.path).toBe("/v1/workbench/sessions/cnv_c5e1330558c94d8e");
    expect(detailRequests.some((item) => item.path === "/v1/workbench/sessions/ses_running")).toBe(false);
    expect((state.legacyRequestLedger as unknown[]).length).toBe(0);
  });

  test("cnv route reflects canonical URL as soon as canonical content is visible", async ({ page }) => {
    await gotoWorkbench(page, "/workbench/sessions/cnv_c5e1330558c94d8e");
    await page.locator(selectors.sessionDetailToggle).click();
    await expect(page.locator(selectors.sessionDetailDialog)).toContainText("ses_c5e1330558c94d8e");
    await expect(page).toHaveURL(/\/workbench\/sessions\/ses_c5e1330558c94d8e$/u, { timeout: 500 });
    const state = await fakeServerState(page);
    const legacyGets = (state.requestLedger as Array<{ method?: string; path?: string }>).filter((item) => item.method === "GET" && /^\/v1\/agent\/sessions\/[^/]+$/u.test(item.path ?? ""));
    expect(legacyGets).toEqual([]);
  });
});
