import { test } from "node:test";import assert from "node:assert/strict";import { h, load } from "../../lib/harness.mjs";const { handleCreateUser } = await load();const JSON_CT = { "Content-Type": "application/json" };const ids = () => { let n = 0; const f = () => `u${++n}`; f.count = () => n; return f;};const call = (req, nextId = ids()) => h.call(() => handleCreateUser({ method: "POST", headers: JSON_CT, body: "{}", ...req }, nextId));const post = (obj, extra = {}) => call({ body: JSON.stringify(obj), ...extra });const field = (f) => ({ status: 422, body: { error: "invalid_field", field: f } });const good = { name: "Ada", email: "ada@example.com" };test("creates a user", () => { const nextId = ids(); assert.deepEqual(post({ ...good, age: 36 }, {}), { status: 201, body: { id: "u1", name: "Ada", email: "ada@example.com", age: 36 } }); assert.deepEqual(call({ body: JSON.stringify(good) }, nextId), { status: 201, body: { id: "u1", name: "Ada", email: "ada@example.com" } }); assert.equal(nextId.count(), 1);});test("age is omitted when absent or null, kept when 0", () => { const a = post(good); assert.equal("age" in a.body, false); const b = post({ ...good, age: null }); assert.equal(b.status, 201); assert.equal("age" in b.body, false); assert.ok(Object.values(b.body).every((v) => v !== null && v !== undefined)); assert.deepEqual(post({ ...good, age: 0 }), { status: 201, body: { id: "u1", name: "Ada", email: "ada@example.com", age: 0 } }); assert.equal(post({ ...good, age: 150 }).body.age, 150);});test("method must be POST", () => { const nextId = ids(); assert.deepEqual(call({ method: "GET" }, nextId), { status: 405, body: { error: "method_not_allowed" } }); assert.deepEqual(call({ method: "PUT", headers: {}, body: "nope" }), { status: 405, body: { error: "method_not_allowed" } }); assert.equal(nextId.count(), 0);});test("content type header is case-insensitive and may carry parameters", () => { const body = JSON.stringify(good); assert.equal(call({ body, headers: { "content-type": "application/json" } }).status, 201); assert.equal(call({ body, headers: { "CONTENT-TYPE": "Application/JSON; charset=utf-8" } }).status, 201); assert.equal(call({ body, headers: { "Content-type": "application/json;charset=UTF-8" } }).status, 201); assert.equal(call({ body, headers: { "x-other": "1", "content-type": " application/json " } }).status, 201);});test("wrong or missing content type is 415", () => { const body = JSON.stringify(good); const u = { status: 415, body: { error: "unsupported_media_type" } }; assert.deepEqual(call({ body, headers: {} }), u); assert.deepEqual(call({ body, headers: { "content-type": "text/plain" } }), u); assert.deepEqual(call({ body, headers: { "content-type": "application/jsonp" } }), u); assert.deepEqual(call({ body, headers: { "content-type": "" } }), u); assert.deepEqual(call({ body: "not json", headers: { accept: "application/json" } }), u);});test("unparseable body is 400", () => { const nextId = ids(); for (const body of ["", "{", "{'a':1}", "undefined", "{\"name\":\"x\",}"]) { assert.deepEqual(call({ body }, nextId), { status: 400, body: { error: "invalid_json" } }, body); } assert.equal(nextId.count(), 0);});test("body must be a JSON object", () => { for (const body of ["null", "[]", "[{\"name\":\"a\"}]", "42", "0", "\"str\"", "true", "false"]) { assert.deepEqual(call({ body }), field("body"), body); }});test("name must be a non-blank string and is trimmed", () => { assert.deepEqual(post({ email: good.email }), field("name")); assert.deepEqual(post({ name: "", email: good.email }), field("name")); assert.deepEqual(post({ name: " ", email: good.email }), field("name")); assert.deepEqual(post({ name: 7, email: good.email }), field("name")); assert.deepEqual(post({ name: null, email: good.email }), field("name")); assert.equal(post({ name: " Grace ", email: good.email }).body.name, "Grace");});test("email rule", () => { for (const email of ["a@b.co", "first.last@mail.example.org", "x@y.z"]) { assert.equal(post({ name: "n", email }).status, 201, email); } for (const email of ["", "ab.co", "a@b@c.co", "@b.co", "a@bco", "a@.co", "a@b.", "a b@c.co", "a@b.co ", 5, null]) { assert.deepEqual(post({ name: "n", email }), field("email"), String(email)); }});test("age must be an integer from 0 to 150", () => { for (const age of [-1, 151, 30.5, "30", true, [], {}]) { assert.deepEqual(post({ ...good, age }), field("age"), JSON.stringify(age)); }});test("fields are checked in order", () => { assert.deepEqual(post({ name: "", email: "bad", age: -1 }), field("name")); assert.deepEqual(post({ name: "n", email: "bad", age: -1 }), field("email")); assert.deepEqual(call({ method: "GET", headers: {}, body: "null" }).status, 405); assert.deepEqual(call({ headers: {}, body: "{" }).status, 415);});test("other fields are ignored and never echoed", () => { const nextId = ids(); const r = call({ body: JSON.stringify({ ...good, id: "evil", admin: true, age: 5 }) }, nextId); assert.deepEqual(r, { status: 201, body: { id: "u1", name: "Ada", email: "ada@example.com", age: 5 } }); assert.equal(nextId.count(), 1);});test("validation failures do not consume an id", () => { const nextId = ids(); call({ body: JSON.stringify({ name: "n", email: "bad" }) }, nextId); call({ body: JSON.stringify({ ...good, age: 999 }) }, nextId); assert.equal(nextId.count(), 0); assert.equal(call({ body: JSON.stringify(good) }, nextId).body.id, "u1");});