import { describe, expect, test } from "bun:test";
import { renderPrCreateTable } from "./gh/pr-commands";

describe("gh pr create default Next", () => {
  test("created ready PR keeps bounded observe/review/preflight/status and suppresses manual delivery repair", () => {
    const rendered = renderPrCreateTable({
      ok: true,
      command: "pr create",
      repo: "pikasTech/unidesk",
      pr: {
        number: 1838,
        title: "Bounded PR create next",
        url: "https://github.com/pikasTech/unidesk/pull/1838",
        draft: false,
        head: { ref: "fix/1838-pr-create-next" },
        base: { ref: "master" },
      },
      request: {
        title: "Bounded PR create next",
        base: "master",
        head: "fix/1838-pr-create-next",
        draft: false,
      },
      next: {
        policy: "bounded-pr-create-next",
        defaultState: "waiting-pr-monitor",
        commands: {
          observe: "bun scripts/cli.ts gh pr view 1838 --repo pikasTech/unidesk",
          review: "bun scripts/cli.ts gh pr review-plan 1838 --repo pikasTech/unidesk",
          preflight: "bun scripts/cli.ts gh pr preflight 1838 --repo pikasTech/unidesk",
          status: "bun scripts/cli.ts gh pr view 1838 --repo pikasTech/unidesk --json headRefName,baseRefName,mergeable,mergeStateStatus,statusCheckRollup",
        },
      },
    });

    expect(rendered).toContain("nextState=waiting-pr-monitor");
    expect(rendered).toContain("observe: bun scripts/cli.ts gh pr view 1838 --repo pikasTech/unidesk");
    expect(rendered).toContain("review: bun scripts/cli.ts gh pr review-plan 1838 --repo pikasTech/unidesk");
    expect(rendered).toContain("preflight: bun scripts/cli.ts gh pr preflight 1838 --repo pikasTech/unidesk");
    expect(rendered).toContain("status: bun scripts/cli.ts gh pr view 1838 --repo pikasTech/unidesk --json headRefName,baseRefName,mergeable,mergeStateStatus,statusCheckRollup");
    expect(rendered).toContain("$unidesk-subagent review/preflight/merge policy");
    expect(rendered).not.toContain("gh pr merge 1838");
    expect(rendered).not.toContain("preflight 1838 --repo pikasTech/unidesk --full");
    expect(rendered).not.toContain("preflight 1838 --repo pikasTech/unidesk --raw");
    expect(rendered).not.toContain("PipelineRun");
    expect(rendered).not.toContain("Argo refresh");
    expect(rendered).not.toContain("mirror sync");
  });

  test("dry-run keeps create guidance without suggesting post-create merge", () => {
    const rendered = renderPrCreateTable({
      ok: true,
      command: "pr create",
      repo: "pikasTech/unidesk",
      dryRun: true,
      draft: false,
      request: {
        title: "Bounded PR create next",
        base: "master",
        head: "fix/1838-pr-create-next",
        draft: false,
      },
    });

    expect(rendered).toContain("rerun without --dry-run to create the PR");
    expect(rendered).toContain("observe: bun scripts/cli.ts gh pr view <pr-number> --repo pikasTech/unidesk");
    expect(rendered).not.toContain("after creation: bun scripts/cli.ts gh pr merge");
    expect(rendered).not.toContain("preflight <pr-number> --repo pikasTech/unidesk --full");
  });
});
