import fs from "fs";
import os from "os";
import path from "path";
import { afterEach, describe, expect, it } from "vitest";
import { normalizePath, resolveProductDir, resolveServiceCwd } from "./paths.mjs";

const cleanups = [];

afterEach(() => {
  while (cleanups.length > 0) {
    cleanups.pop()();
  }
});

describe("config path helpers", () => {
  it("normalizes repo-relative paths consistently", () => {
    expect(normalizePath("./src/app/page.tsx")).toBe("src/app/page.tsx");
  });

  it("resolves product and service directories", () => {
    const productDir = fs.mkdtempSync(path.join(os.tmpdir(), "testkit-paths-"));
    cleanups.push(() => fs.rmSync(productDir, { recursive: true, force: true }));

    expect(resolveProductDir(productDir, ".")).toBe(productDir);
    expect(resolveServiceCwd(productDir, "apps/web")).toBe(path.join(productDir, "apps", "web"));
  });
});
