import { describe, it, expect, beforeEach, afterEach } from "vitest";
import fs from "fs";
import os from "os";
import path from "path";
import { NasFileManager } from "../services/nas-file-manager";

describe("NasFileManager path traversal", () => {
  const nas = new NasFileManager();

  describe("deleteItem", () => {
    it("rejects empty path", async () => {
      const result = await nas.deleteItem("PRJ-001", "");
      expect(result).toContain("kořenovou složku");
    });

    it("rejects root path /", async () => {
      const result = await nas.deleteItem("PRJ-001", "/");
      expect(result).toContain("kořenovou složku");
    });

    it("rejects current directory .", async () => {
      const result = await nas.deleteItem("PRJ-001", ".");
      expect(result).toContain("kořenovou složku");
    });

    it("rejects current directory ./", async () => {
      const result = await nas.deleteItem("PRJ-001", "./");
      expect(result).toContain("kořenovou složku");
    });

    it("rejects path traversal ..", async () => {
      const result = await nas.deleteItem("PRJ-001", "../etc/passwd");
      expect(result).toContain("Neplatná cesta");
    });
  });

  describe("moveItem", () => {
    it("rejects empty fromPath", async () => {
      const result = await nas.moveItem("PRJ-001", "", "dest");
      expect(result).toContain("kořenovou složku");
    });

    it("rejects root fromPath /", async () => {
      const result = await nas.moveItem("PRJ-001", "/", "dest");
      expect(result).toContain("kořenovou složku");
    });

    it("rejects current directory .", async () => {
      const result = await nas.moveItem("PRJ-001", ".", "dest");
      expect(result).toContain("kořenovou složku");
    });

    it("rejects path traversal in fromPath", async () => {
      const result = await nas.moveItem("PRJ-001", "../secret", "dest");
      expect(result).toContain("Neplatná cesta");
    });
  });
});

/**
 * Positive-path coverage: a legitimate (non-traversal) path inside a real
 * project folder is ACCEPTED — it passes resolveProjectPath validation and
 * reaches the underlying fs op (success → null). Without this, a
 * "reject-everything" regression in the traversal guard would still pass every
 * rejection test above. Uses the constructor basePath seam + a temp dir so it
 * is deterministic even where the real NAS (Z:) is not mounted.
 */
describe("NasFileManager accepts legitimate paths", () => {
  let tmpBase: string;
  let nas: NasFileManager;
  const NUM = "29990001";

  beforeEach(() => {
    tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), "nas-accept-"));
    nas = new NasFileManager(tmpBase);
    // Create a real project folder <number>_<name> so findProjectFolder resolves.
    nas.createProjectFolder(NUM, "Test");
  });

  afterEach(() => {
    fs.rmSync(tmpBase, { recursive: true, force: true });
  });

  it("deletes a legitimate file inside the project folder (path accepted)", async () => {
    const folder = path.join(tmpBase, `${NUM}_Test`);
    fs.writeFileSync(path.join(folder, "doc.pdf"), "hello");
    expect(fs.existsSync(path.join(folder, "doc.pdf"))).toBe(true);

    const result = await nas.deleteItem(NUM, "doc.pdf");
    // null = success: the path was accepted and the file actually removed.
    expect(result).toBeNull();
    expect(fs.existsSync(path.join(folder, "doc.pdf"))).toBe(false);
  });

  it("moves a legitimate file to a legitimate destination (both accepted)", async () => {
    const folder = path.join(tmpBase, `${NUM}_Test`);
    fs.writeFileSync(path.join(folder, "from.pdf"), "data");

    const result = await nas.moveItem(NUM, "from.pdf", "to.pdf");
    expect(result).toBeNull();
    expect(fs.existsSync(path.join(folder, "from.pdf"))).toBe(false);
    expect(fs.existsSync(path.join(folder, "to.pdf"))).toBe(true);
  });

  it("a traversal delete is rejected AND deletes nothing (folder intact)", async () => {
    const folder = path.join(tmpBase, `${NUM}_Test`);
    fs.writeFileSync(path.join(folder, "keep.pdf"), "keep");

    const result = await nas.deleteItem(NUM, "../keep.pdf");
    expect(result).toContain("Neplatná cesta");
    // The guard rejected the path before any fs op — nothing was removed.
    expect(fs.existsSync(path.join(folder, "keep.pdf"))).toBe(true);
  });

  it("rejects a traversal DESTINATION even when the source is legitimate", async () => {
    // With a real project folder, the source `from.pdf` resolves cleanly, so
    // this isolates the DESTINATION check: `../escape.pdf` must be rejected and
    // the source must stay put (no move outside the project folder).
    const folder = path.join(tmpBase, `${NUM}_Test`);
    fs.writeFileSync(path.join(folder, "from.pdf"), "data");

    const result = await nas.moveItem(NUM, "from.pdf", "../escape.pdf");
    expect(result).toContain("Neplatná cesta");
    expect(fs.existsSync(path.join(folder, "from.pdf"))).toBe(true);
    // Nothing escaped the project folder.
    expect(fs.existsSync(path.join(tmpBase, "escape.pdf"))).toBe(false);
  });
});
