import test from "node:test";
import assert from "node:assert/strict";
import * as projectPicker from "./project-picker.js";

const { filterProjectFolders, projectListErrorMessage, projectOperationError, projectPopoverPlacement, recentProjectFolders, upsertProjectFolder, validateProjectName } = projectPicker;

const projects = [
  { name: "Codex-remote", workspaceId: "remote" },
  { name: "Dashboard" },
  { name: "Context-Manager" }
];

test("project search filters folder names case-insensitively", () => {
  assert.deepEqual(filterProjectFolders(projects, "dash"), [{ name: "Dashboard" }]);
  assert.deepEqual(filterProjectFolders(projects, "MANAGER"), [{ name: "Context-Manager" }]);
});

test("empty project search preserves every folder", () => {
  assert.deepEqual(filterProjectFolders(projects, ""), projects);
});

test("new project popover opens away from the nearest viewport edge", () => {
  assert.equal(projectPopoverPlacement({ top: 120, height: 48 }, 844), "below");
  assert.equal(projectPopoverPlacement({ top: 600, height: 48 }, 844), "above");
  assert.equal(projectPopoverPlacement({ top: 398, height: 48 }, 844), "below");
});

test("recent project folders include only used registered workspaces in latest order", () => {
  const threads = [
    { id: "alpha-old", workspaceId: "alpha", workspaceLabel: "Alpha", updatedAt: "2026-07-14T01:00:00.000Z" },
    { id: "beta", workspaceId: "beta", workspaceLabel: "Beta", updatedAt: "2026-07-16T01:00:00.000Z" },
    { id: "alpha-new", workspaceId: "alpha", workspaceLabel: "Alpha", updatedAt: "2026-07-15T01:00:00.000Z" },
    { id: "stale", workspaceId: "missing", workspaceLabel: "Missing", updatedAt: "2026-07-17T01:00:00.000Z" }
  ];
  const workspaces = [
    { id: "alpha", label: "Alpha" },
    { id: "beta", label: "Beta" },
    { id: "unused", label: "Unused" }
  ];

  assert.deepEqual(recentProjectFolders(threads, workspaces), [
    { name: "Beta", workspaceId: "beta" },
    { name: "Alpha", workspaceId: "alpha" }
  ]);
});

test("a created project is immediately present and selected in the folder list", () => {
  assert.deepEqual(upsertProjectFolder(projects, { name: "CRUBLY", workspaceId: "crumbly-id" }), [
    { name: "Codex-remote", workspaceId: "remote" },
    { name: "Context-Manager" },
    { name: "CRUBLY", workspaceId: "crumbly-id" },
    { name: "Dashboard" }
  ]);
  assert.deepEqual(
    upsertProjectFolder([{ name: "CRUBLY" }], { name: "CRUBLY", workspaceId: "crumbly-id" }),
    [{ name: "CRUBLY", workspaceId: "crumbly-id" }]
  );
});

test("project name validation gives immediate feedback for invalid folders", () => {
  for (const name of ["", "   ", "..", "../secret", "C:\\Users\\person\\secret", "bad/name", "CON", "trailing."]) {
    assert.deepEqual(validateProjectName(name), {
      valid: false,
      message: "Use a simple folder name."
    });
  }
  assert.deepEqual(validateProjectName("Fresh App"), { valid: true, message: "" });
});

test("project operation errors expose only allowlisted codes", () => {
  assert.equal(typeof projectOperationError, "function");
  assert.equal(projectOperationError(new Error("project_create_failed")), "Could not create that project folder.");
  assert.equal(projectOperationError(new Error("folder_selection_expired")), "That folder selection expired. Browse to the folder again.");
  assert.equal(projectOperationError(new Error("folder_browse_failed")), "Could not open that folder.");
  assert.equal(projectOperationError(new Error("folder_create_failed")), "Could not create that folder.");
  assert.equal(projectOperationError(new Error("C:\\Users\\private\\project")), "Project update failed.");
});

test("project list errors explain projectsRoot without exposing paths", () => {
  assert.equal(typeof projectListErrorMessage, "function");
  assert.equal(
    projectListErrorMessage(new Error("projects_root_unavailable")),
    "Project folder discovery is unavailable. Set projectsRoot in local config to a parent folder; only direct child folders appear here."
  );
  assert.equal(projectListErrorMessage(new Error("C:\\Private\\Root")), "Project folder discovery failed.");
});
