import { describe, it, expect } from "vitest";

describe("Platform MCP Tools - Automated Validation Suite", () => {
  it("should successfully parse checkboxes without leading dashes/bullets", () => {
    const checklistText = `
[x] T001 [P] Remove legacy Drizzle ORM configuration.
[ ] T004 [P] Install bcryptjs.
- [x] T005 Update auth.ts.
- [ ] T006 Create server actions.
`;
    const regex = /^(\s*)(?:-\s*)?\[([ xX])\]\s+(.+)$/;
    const lines = checklistText.split("\n").map(l => l.trim()).filter(Boolean);

    const parsed = lines.map(line => {
      const match = line.match(regex);
      if (match) {
        return {
          isChecked: match[2].toLowerCase() === "x",
          text: match[3].trim()
        };
      }
      return null;
    }).filter(Boolean);

    expect(parsed.length).toBe(4);
    expect(parsed[0]?.isChecked).toBe(true);
    expect(parsed[0]?.text).toContain("T001");
    expect(parsed[1]?.isChecked).toBe(false);
    expect(parsed[1]?.text).toContain("T004");
    expect(parsed[2]?.isChecked).toBe(true);
    expect(parsed[2]?.text).toContain("T005");
    expect(parsed[3]?.isChecked).toBe(false);
    expect(parsed[3]?.text).toContain("T006");
  });

  it("should dynamically default empty path/cwd parameters to dot (.)", () => {
    const defaultCwd = (passedCwd: string | undefined | null) => {
      return passedCwd === undefined || passedCwd === null || passedCwd === "" ? "." : String(passedCwd);
    };
    expect(defaultCwd(undefined)).toBe(".");
    expect(defaultCwd(null)).toBe(".");
    expect(defaultCwd("")).toBe(".");
    expect(defaultCwd("src/app")).toBe("src/app");
  });

  it("should generate a POSIX-compliant pipeline for fs_edit instead of using bash herestrings <<<", () => {
    const b64 = "Y29udGVudA==";
    const pyB64 = "cHl0aG9uIGNvZGU=";
    const path = "src/auth.ts";
    const shq = (s: string) => `"'${s}'"`; // mock shq shell-quoter

    // The secure POSIX-compliant pipeline command we designed
    const cmd = `printf %s ${b64} | base64 -d | python3 -c "$(printf %s ${pyB64} | base64 -d)" && echo "---" && sha256sum ${path} | cut -d' ' -f1`;

    expect(cmd).not.toContain("<<<");
    expect(cmd).toContain("| base64 -d | python3 -c");
  });
});
