const test = require("node:test");
const assert = require("node:assert/strict");
const request = require("supertest");
const { app } = require("../src/server");

test("public pages load", async () => {
  for (const path of ["/", "/items", "/services", "/about-contact", "/healthz"]) {
    const response = await request(app).get(path);
    assert.equal(response.status < 500, true, `${path} returned ${response.status}`);
  }
});

test("admin redirects to setup or login", async () => {
  const response = await request(app).get("/admin");
  assert.equal([302, 303].includes(response.status), true);
});

test("contact form requires complete fields", async () => {
  const agent = request.agent(app);
  const page = await agent.get("/about-contact");
  const token = /name="_csrf" value="([^"]+)"/.exec(page.text)?.[1];
  const response = await agent
    .post("/contact")
    .send(`_csrf=${encodeURIComponent(token || "")}&name=&email=bad&subject=&message=`);
  assert.equal(response.status, 400);
});
