import { describe, expect, it } from "vitest";
import { renderRequest } from "../../../apps/web/src/app.js";

describe("I1 U1-U6 page journey render", () => {
  it("UI-E2E-001 renders the approved lifecycle identities without page-identity drift", () => {
    const u1 = renderRequest("/projects/project-i1");
    const u2 = renderRequest("/factory-runs/FR-001");
    const u3 = renderRequest("/factory-runs/FR-001?fixture=recovery", { fixtureMode: true });
    const u4 = renderRequest("/changesets/CS-001");
    const u5 = renderRequest("/changesets/CS-002");
    const u6 = renderRequest("/changesets/CS-002?fixture=merged", { fixtureMode: true });

    expect(u1).toContain("Queue / Next Up");
    expect(u2).toContain("FR-001");
    expect(u3).toContain("FR-001");
    expect(u4).toContain("ChangeSet / Review");
    expect(u4).toContain("CS-001");
    expect(u5).toContain("CS-002");
    expect(u6).toContain("CS-002");
    expect(u6).toContain("Trusted Merge completed successfully");
  });

  it("UI-E2E-002 exposes keyboard/focus/a11y text equivalents for critical state", () => {
    const project = renderRequest("/projects/project-i1");
    const queue = renderRequest("/projects/project-i1/queue?view=graph&task=task-ui");
    const recovery = renderRequest("/factory-runs/FR-001?fixture=recovery", { fixtureMode: true });
    const review = renderRequest("/changesets/CS-001?finding=cross-plan");

    expect(project).toContain("Skip to main content");
    expect(project).toContain('aria-label="Project tabs"');
    expect(queue).toContain("Accessible dependency relationships");
    expect(queue).toContain("Prerequisite → dependent");
    expect(recovery).toContain("WIP is safely persisted");
    expect(review).toContain("Blocking");
    expect(review).toContain("src/application/queue.ts:152");
  });

  it("UI-E2E-003 never invents an owner Merge approval gate", () => {
    const blocked = renderRequest("/changesets/CS-001");
    expect(blocked).toContain("MergeGate — not ready");
    expect(blocked).not.toMatch(/data-command="merge"/u);
    const ready = renderRequest("/changesets/CS-002");
    expect(ready).toContain("Trusted merge automatic");
    expect(ready).not.toMatch(/data-command="merge"/u);
  });

  it("UI-E2E-004 shows automatic reconciliation, not owner refresh or Merge, while checks are pending", () => {
    const verifying = renderRequest("/changesets/CS-001?fixture=verifying", { fixtureMode: true });
    expect(verifying).toContain("Required checks pending");
    expect(verifying).toContain("Checks reconcile automatically");
    expect(verifying).not.toMatch(/data-command="refresh-required-checks"/u);
    expect(verifying).not.toMatch(/data-command="merge"/u);
    expect(verifying).not.toMatch(/data-command="start-correction"/u);
  });
});
