import { readFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
  DEFAULT_FLOCK_QUOTE_URL,
  formatFlockRecordingChecklist,
} from "@/workers/rpa/flock/demo-input";
import {
  formatFlockLoggedInRecordingChecklist,
  parseFlockRecordArgs,
  resolveFlockOutputBasename,
  resolveFlockStoragePath,
} from "@/scripts/record-flock";

const SH_PATH = join(process.cwd(), "scripts", "record-flock.sh");
const TS_PATH = join(process.cwd(), "scripts", "record-flock.ts");
const VISUAL_PATH = join(process.cwd(), "scripts", "visual-flock-human-assist.ts");
const SOP_PATH = join(process.cwd(), "docs", "rpa-flock-logged-in-sop.md");

describe("record-flock scripts", () => {
  it("record-flock.sh 存在且包含 codegen + logged-in", () => {
    expect(existsSync(SH_PATH)).toBe(true);
    const content = readFileSync(SH_PATH, "utf8");
    expect(content).toContain("codegen");
    expect(content).toContain("get-a-quote");
    expect(content).toContain("--logged-in");
    expect(content).toContain("flock-logged-in");
  });

  it("record-flock.ts 存在且包含 headed 与 canonical 路径", () => {
    expect(existsSync(TS_PATH)).toBe(true);
    const content = readFileSync(TS_PATH, "utf8");
    expect(content).toContain("codegen");
    expect(content).toContain("FLOCK_CANONICAL_PATHS");
    expect(content).toContain("DEFAULT_FLOCK_QUOTE_URL");
    expect(content).toContain("--logged-in");
    expect(content).toContain("flock-logged-in-");
    expect(content).toContain("flock-logged-in-storage.json");
  });

  it("登录后 SOP 存在", () => {
    expect(existsSync(SOP_PATH)).toBe(true);
    const content = readFileSync(SOP_PATH, "utf8");
    expect(content).toContain("flock-logged-in-");
    expect(content).toContain("Let's build a quote");
  });

  it("visual-flock-human-assist 含分步 pause", () => {
    expect(existsSync(VISUAL_PATH)).toBe(true);
    const content = readFileSync(VISUAL_PATH, "utf8");
    expect(content).toContain("page.pause");
    expect(content).toContain("flock-human-");
  });

  it("录制清单包含关键步骤", () => {
    const text = formatFlockRecordingChecklist({
      pickupDateDisplay: "07/14/2026",
      pickupZip: "90001",
      deliveryZip: "75201",
      palletCount: 6,
      totalWeightLb: 3500,
      lengthIn: 48,
      widthIn: 40,
      heightIn: 48,
    });
    expect(text).toContain("Next");
    expect(text).toContain("Create your free account");
    expect(text).toContain("Complete your order");
  });

  it("登录后清单提示手动粘贴账密与 Generate my quote", () => {
    const text = formatFlockLoggedInRecordingChecklist();
    expect(text).toContain("FLOCK_LOGIN_EMAIL");
    expect(text).toContain("Generate my quote");
    expect(text).toContain("90001");
  });
});

describe("parseFlockRecordArgs / storage 隔离", () => {
  it("默认匿名；--logged-in / --login 切换模式", () => {
    expect(parseFlockRecordArgs(["node", "record-flock.ts"]).mode).toBe(
      "anonymous",
    );
    expect(
      parseFlockRecordArgs(["node", "record-flock.ts", "--logged-in"]).mode,
    ).toBe("logged-in");
    expect(
      parseFlockRecordArgs(["node", "record-flock.ts", "--login"]).mode,
    ).toBe("logged-in");
  });

  it("解析起始 URL 与 help / list", () => {
    const withUrl = parseFlockRecordArgs([
      "node",
      "x",
      "--logged-in",
      "https://app.flockfreight.com/login",
    ]);
    expect(withUrl.mode).toBe("logged-in");
    expect(withUrl.startUrl).toBe("https://app.flockfreight.com/login");
    expect(parseFlockRecordArgs(["node", "x", "-h"]).help).toBe(true);
    expect(parseFlockRecordArgs(["node", "x", "list"]).list).toBe(true);
  });

  it("登录态产物前缀与 storage 路径隔离", () => {
    expect(resolveFlockOutputBasename("logged-in", "20260715-120000")).toBe(
      "flock-logged-in-20260715-120000.js",
    );
    expect(resolveFlockOutputBasename("anonymous", "20260715-120000")).toBe(
      "flock-manual-20260715-120000.js",
    );
    expect(resolveFlockStoragePath("logged-in")).toContain(
      "flock-logged-in-storage.json",
    );
    expect(resolveFlockStoragePath("anonymous")).toContain("flock-storage.json");
    expect(DEFAULT_FLOCK_QUOTE_URL).toContain("get-a-quote");
  });
});
