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

import { buildFileTree } from "./gitTree";

describe("buildFileTree", () => {
  it("folds paths into a nested directory tree", () => {
    const tree = buildFileTree([
      { path: "src/app/main.rs", staged: false },
      { path: "src/lib.rs", staged: true },
      { path: "README.md", staged: false },
    ]);

    // Directories first, then files, alphabetically: src/ then README.md.
    expect(tree.map((n) => n.name)).toEqual(["src", "README.md"]);

    const src = tree[0];
    expect(src.isDir).toBe(true);
    // Inside src: app/ (dir) then lib.rs (file).
    expect(src.children.map((n) => n.name)).toEqual(["app", "lib.rs"]);
    expect(src.children[0].isDir).toBe(true);
    expect(src.children[0].children[0].path).toBe("src/app/main.rs");
    expect(src.children[1].path).toBe("src/lib.rs");

    // The README leaf keeps its full path + staged flag.
    const readme = tree[1];
    expect(readme.isDir).toBe(false);
    expect(readme.path).toBe("README.md");
  });

  it("returns an empty array for no files", () => {
    expect(buildFileTree([])).toEqual([]);
  });
});
