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

test.describe("cross-session late authority", () => {
  test.use({ scenarioId: "cross-session-late-events" });

  test("late A realtime events do not pollute B active session", async ({ page }, testInfo) => {
    await page.goto("/workbench/sessions/ses_late_B");
    await expect(page.locator(selectors.workspace)).toBeVisible();
    await expect(page.locator(sessionTab("ses_late_B"))).toBeVisible();
    await expect(page.locator(sessionTab("ses_late_B"))).toHaveAttribute("data-active", "true");
    await page.waitForTimeout(1_200);

    expect(page.url()).toMatch(/\/workbench\/sessions\/ses_late_B$/u);
    expect(await page.locator(sessionTab("ses_late_B")).getAttribute("data-active")).toBe("true");
    const conversationText = await page.locator(selectors.conversationList).textContent();
    expect(conversationText ?? "").not.toContain("写一个python脚本测试性能");
    expect(conversationText ?? "").not.toContain("perf_test.py");
    expect(await page.locator(`${selectors.messageCard}[data-role="user"]`).count()).toBe(1);

    await expect(page.locator(selectors.commandInput)).toBeVisible();
    await saveScreenshot(page, testInfo, "cross-session-late-events-b-active");
  });
});
