SPB Git forge

spb/hilmacorp

Public
1commits 1branches 0releases
29.2 MBsize
maindefault branch
20 days agolast push
TypeScript 93.3% JavaScript 4.4% CSS 2.3%
2.6 KB · 95 lines javascript
Raw Blame History
1/**2 * Author: Simon-Pierre Boucher3 * Contact: contact@spboucher.ai4 * Project: Hilmacorp.ai — Web Platform5 * File: tests/contact-validation.test.mjs6 * Description: Unit tests for the contact form validation used by the /api/contact route7 */89import { test } from "node:test";10import assert from "node:assert/strict";11import { validateContact } from "../lib/validate.ts";1213test("accepts a valid payload", () => {14  const result = validateContact({15    name: "Jane Doe",16    organization: "Acme Inc.",17    email: "jane@acme.com",18    message: "We would like to discuss an AI project.",19  });20  assert.equal(result.ok, true);21  assert.deepEqual(result.errors, {});22  assert.equal(result.data?.name, "Jane Doe");23});2425test("organization is optional", () => {26  const result = validateContact({27    name: "Jane Doe",28    organization: "",29    email: "jane@acme.com",30    message: "A perfectly valid message.",31  });32  assert.equal(result.ok, true);33});3435test("rejects a missing name", () => {36  const result = validateContact({37    name: "",38    email: "jane@acme.com",39    message: "A perfectly valid message.",40  });41  assert.equal(result.ok, false);42  assert.equal(result.errors.name, "invalid_name");43});4445test("rejects an invalid email", () => {46  for (const email of ["", "not-an-email", "a@b", "a b@c.com"]) {47    const result = validateContact({48      name: "Jane Doe",49      email,50      message: "A perfectly valid message.",51    });52    assert.equal(result.ok, false, `expected rejection for email: "${email}"`);53    assert.equal(result.errors.email, "invalid_email");54  }55});5657test("rejects a message that is too short", () => {58  const result = validateContact({59    name: "Jane Doe",60    email: "jane@acme.com",61    message: "hi",62  });63  assert.equal(result.ok, false);64  assert.equal(result.errors.message, "invalid_message");65});6667test("trims whitespace and handles non-string input safely", () => {68  const result = validateContact({69    name: "  Jane Doe  ",70    organization: 42,71    email: " jane@acme.com ",72    message: "  A perfectly valid message.  ",73  });74  assert.equal(result.ok, true);75  assert.equal(result.data?.name, "Jane Doe");76  assert.equal(result.data?.organization, "");77});7879test("handles null and garbage payloads without throwing", () => {80  for (const payload of [null, undefined, "string", 12, []]) {81    const result = validateContact(payload);82    assert.equal(result.ok, false);83  }84});8586test("caps oversized fields instead of rejecting them", () => {87  const result = validateContact({88    name: "Jane Doe",89    email: "jane@acme.com",90    message: "x".repeat(10000),91  });92  assert.equal(result.ok, true);93  assert.equal(result.data?.message.length, 5000);94});95