Files
Emberfall/tests/integration/error-pages.test.js
T
2026-10-21 10:21:11 +11:00

54 lines
1.6 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { setup, teardown, cleanup } = require('../helpers');
test('GET /nonexistent → serves 404.html for HTML requests', async () => {
const base = await setup();
try {
const res = await fetch(base + '/nonexistent-page', {
headers: { Accept: 'text/html' },
});
assert.equal(res.status, 404);
const html = await res.text();
assert.ok(html.includes('404'), '404 page should contain "404"');
assert.ok(html.includes('Page Not Found'), '404 page should have title');
assert.ok(html.includes('Emberfall'), '404 page should be branded');
} finally {
await teardown();
cleanup();
}
});
test('GET /nonexistent → JSON error for API requests', async () => {
const base = await setup();
try {
const res = await fetch(base + '/api/nonexistent', {
headers: { Accept: 'application/json' },
});
assert.equal(res.status, 404);
const data = await res.json();
assert.equal(data.ok, false);
assert.equal(data.error.code, 'NOT_FOUND');
assert.ok(typeof data.error.message === 'string');
} finally {
await teardown();
cleanup();
}
});
test('GET /api/heroes/nonexistent → 404.html (public route, unknown hero)', async () => {
const base = await setup();
try {
const res = await fetch(base + '/api/heroes/doesnotexist', {
headers: { Accept: 'text/html' },
});
// Public route exists, so it returns JSON 404 (not the page 404)
assert.equal(res.status, 404);
const data = await res.json();
assert.equal(data.ok, false);
} finally {
await teardown();
cleanup();
}
});