import assert from "node:assert/strict"
import { hasPermission, permissionForRequest } from "./permissions.js"

assert.equal(hasPermission(["mm:servers:write"], "mm:servers:read"), true)
assert.equal(hasPermission(["mm:servers:admin"], "mm:servers:write"), true)
assert.equal(hasPermission(["mm:servers:read"], "mm:servers:write"), false)

assert.equal(
  permissionForRequest("GET", "/api/servers"),
  "mm:servers:read",
)
assert.equal(
  permissionForRequest("POST", "/api/servers"),
  "mm:servers:write",
)
assert.equal(
  permissionForRequest("GET", "/api/system/database/backup"),
  "mm:settings:admin",
)
assert.equal(
  permissionForRequest("GET", "/api/traffic/servers/1/live"),
  "mm:traffic:read",
)
assert.equal(
  permissionForRequest("POST", "/api/traffic/flow/purge"),
  "mm:traffic:write",
)
assert.equal(
  permissionForRequest("GET", "/api/unknown-thing"),
  "mm:dashboard:read",
)
assert.equal(
  permissionForRequest("GET", "/api/wireguard"),
  "mm:network:read",
)
assert.equal(
  permissionForRequest("POST", "/api/wireguard/interfaces"),
  "mm:network:write",
)
assert.equal(
  permissionForRequest("GET", "/api/firewall/all"),
  "mm:network:read",
)
assert.equal(
  permissionForRequest("GET", "/api/users"),
  "mm:users:read",
)
assert.equal(
  permissionForRequest("POST", "/api/users"),
  "mm:users:write",
)
assert.equal(hasPermission(["mm:settings:admin"], "mm:users:read"), true)
assert.equal(hasPermission(["mm:settings:admin"], "mm:users:write"), true)
assert.equal(hasPermission(["mm:dashboard:read"], "mm:users:write"), false)

console.log("permissions.test.ts: ok")
