import assert from "node:assert/strict";
import { test } from "bun:test";

import { isAllowedProjectManagementProxyMethod } from "./project-management-proxy.ts";

test("project management proxy forwards public MDTODO write resources", () => {
  const allowed = [
    ["GET", "/v1/project-management/mdtodo/sources"],
    ["HEAD", "/v1/project-management/mdtodo/tasks"],
    ["POST", "/v1/project-management/workbench-links"],
    ["POST", "/v1/project-management/mdtodo/sources"],
    ["PATCH", "/v1/project-management/mdtodo/sources/d601-f103-v2-mdtodo"],
    ["DELETE", "/v1/project-management/mdtodo/sources/d601-f103-v2-mdtodo"],
    ["POST", "/v1/project-management/mdtodo/sources/d601-f103-v2-mdtodo/probe"],
    ["POST", "/v1/project-management/mdtodo/sources/d601-f103-v2-mdtodo/reindex"],
    ["PATCH", "/v1/project-management/mdtodo/files/file_demo/content"],
    ["POST", "/v1/project-management/mdtodo/tasks"],
    ["PATCH", "/v1/project-management/mdtodo/tasks/mdtodo%3Asource%3Afile%3AR1.1"],
    ["DELETE", "/v1/project-management/mdtodo/tasks/mdtodo%3Asource%3Afile%3AR1.1"]
  ];
  for (const [method, pathname] of allowed) {
    assert.equal(isAllowedProjectManagementProxyMethod(method, pathname), true, `${method} ${pathname}`);
  }
});

test("project management proxy keeps non-contract writes blocked", () => {
  const blocked = [
    ["PUT", "/v1/project-management/mdtodo/sources/d601-f103-v2-mdtodo"],
    ["POST", "/v1/project-management/mdtodo/sources/d601-f103-v2-mdtodo/delete"],
    ["POST", "/v1/project-management/mdtodo/files/file_demo/content"],
    ["PATCH", "/v1/project-management/mdtodo/tasks"],
    ["POST", "/v1/project-management/internal/rebuild"]
  ];
  for (const [method, pathname] of blocked) {
    assert.equal(isAllowedProjectManagementProxyMethod(method, pathname), false, `${method} ${pathname}`);
  }
});
