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

test.describe("submit session authority", () => {
  test.use({ scenarioId: "submit-authority-race" });

  test("optimistic submit immediately projects running state to tab and composer without legacy session detail", async ({ page }) => {
    await gotoWorkbench(page, "/workbench/sessions/ses_running");
    await page.locator(selectors.commandInput).fill("workbench submit race probe redacted: 请只回复 OK。");
    await expect(page.locator(selectors.commandSend)).toBeEnabled();
    await page.locator(selectors.commandSend).click();
    await page.waitForTimeout(250);

    await expect(page.locator(sessionTab("ses_running"))).toHaveAttribute("data-running", "true");
    await expect(page.locator(sessionTab("ses_running"))).toHaveAttribute("data-status", /^(running|pending)$/u);
    await expect(page.locator(`${selectors.messageCard}[data-role="user"]`).last()).toContainText("workbench submit race probe redacted");
    await expect(page.locator(`${selectors.messageCard}[data-role="agent"][data-status="running"]`).last()).toBeVisible();
    await expect(page.locator(selectors.commandSend)).toHaveAttribute("data-action", "cancel");

    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([]);
  });

  test("message page empty user text is diagnostic, not repaired from list preview", async ({ page }) => {
    await gotoWorkbench(page, "/workbench/sessions/ses_submit_split");
    await expect(page.locator(sessionTab("ses_submit_split"))).toContainText("workbench submit race probe redacted");
    const userCard = page.locator(`${selectors.messageCard}[data-role="user"]`).first();
    await expect(userCard).not.toContainText("workbench submit race probe redacted");
    await expect(userCard).toContainText("Workbench read model 未返回 user text");
    await expect(page.locator(`${selectors.messageCard}[data-role="agent"][data-status="running"]`)).toBeVisible();

    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([]);
  });
});

test.describe("submit admission failure", () => {
  test.use({ scenarioId: "admission-unavailable" });

  test("not-admitted chat failure restores draft and does not add preflight routes", async ({ page }) => {
    await gotoWorkbench(page, "/workbench/sessions/ses_completed");
    const draft = "admission unavailable draft must survive";
    await page.locator(selectors.commandInput).fill(draft);
    await page.locator(selectors.commandSend).click();
    await expect(page.locator(selectors.commandInput)).toHaveValue(draft);
    await expect(page.locator(`${selectors.messageCard}[data-role="agent"][data-status="failed"]`).last()).toContainText("admission_unavailable");

    const state = await fakeServerState(page);
    expect(state.chatRequests).toHaveLength(1);
    const forbiddenPreflights = (state.requestLedger as Array<{ method?: string; path?: string }>).filter((item) => /billing|readiness|preflight|agentrun/iu.test(item.path ?? ""));
    expect(forbiddenPreflights).toEqual([]);
  });
});
