import test from "node:test";
import assert from "node:assert/strict";
import { Readable } from "node:stream";
import { resolveWorkspaceRoute } from "./workspace-routes.js";

function request(method, body) {
  const req = Readable.from(body === undefined ? [] : [JSON.stringify(body)]);
  req.method = method;
  return req;
}

test("GET /project-folders returns safe folder choices", async () => {
  const result = await resolveWorkspaceRoute(request("GET"), new URL("http://localhost/project-folders"), {
    config: {},
    listProjectFolders: async () => [{ name: "Alpha" }]
  });
  assert.deepEqual(result, { status: 200, body: { projects: [{ name: "Alpha" }] } });
});

test("POST /workspaces creates or registers a project workspace", async () => {
  let input;
  const result = await resolveWorkspaceRoute(request("POST", { name: "Alpha", create: true }), new URL("http://localhost/workspaces"), {
    config: {},
    configPath: "config.local.json",
    createProjectWorkspace: async (_config, _path, value) => {
      input = value;
      return { workspace: { id: "alpha", label: "Alpha" }, workspaces: [{ id: "alpha", label: "Alpha" }] };
    }
  });
  assert.deepEqual(input, { name: "Alpha", create: true });
  assert.equal(result.status, 201);
});

test("GET /folder-browser returns opaque folder choices without paths", async () => {
  const result = await resolveWorkspaceRoute(request("GET"), new URL("http://localhost/folder-browser?folderId=root-a"), {
    config: {},
    browseFolders: async (_config, folderId) => ({ current: "Projects", parentId: "parent-a", folders: [{ id: "child-a", name: "Alpha" }] })
  });
  assert.deepEqual(result, { status: 200, body: { current: "Projects", parentId: "parent-a", folders: [{ id: "child-a", name: "Alpha" }] } });
  assert.equal(JSON.stringify(result).includes(":" + "\\"), false);
});

test("POST /workspaces registers a folder-browser selection", async () => {
  let selectedId;
  const result = await resolveWorkspaceRoute(request("POST", { folderId: "opaque-a" }), new URL("http://localhost/workspaces"), {
    config: {},
    configPath: "config.local.json",
    registerBrowsedWorkspace: async (_config, _path, folderId) => {
      selectedId = folderId;
      return { workspace: { id: "alpha", label: "Alpha" }, workspaces: [{ id: "alpha", label: "Alpha" }] };
    }
  });
  assert.equal(selectedId, "opaque-a");
  assert.equal(result.status, 201);
});

test("POST /folder-browser/folders creates a child below an opaque parent selection", async () => {
  let input;
  const result = await resolveWorkspaceRoute(request("POST", { parentId: "opaque-parent", name: "CRUBLY" }), new URL("http://localhost/folder-browser/folders"), {
    config: {},
    createBrowsedFolder: async (_config, parentId, name) => {
      input = { parentId, name };
      return { current: "CRUBLY", currentId: "opaque-child", folders: [] };
    }
  });

  assert.deepEqual(input, { parentId: "opaque-parent", name: "CRUBLY" });
  assert.deepEqual(result, { status: 201, body: { current: "CRUBLY", currentId: "opaque-child", folders: [] } });
});

test("workspace router ignores unrelated routes", async () => {
  assert.equal(await resolveWorkspaceRoute(request("GET"), new URL("http://localhost/threads"), {}), null);
});
