Files
2026-10-22 23:10:45 +11:00

156 lines
4.5 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { setup, teardown, cleanup } = require('../helpers');
test('10.5 GET / → 200 HTML landing page', async () => {
const base = await setup();
try {
const res = await fetch(base, {
headers: { Accept: 'text/html' },
});
assert.equal(res.status, 200);
const html = await res.text();
assert.ok(html.includes('<!DOCTYPE html>'), 'should be valid HTML');
assert.ok(html.includes('Emberfall'), 'should contain the game title');
assert.ok(html.includes('Login'), 'should have a Login link');
assert.ok(html.includes('Register'), 'should have a Register link');
} finally {
await teardown();
cleanup();
}
});
test('10.5 GET / logged-in with hero → shows Play button', async () => {
const base = await setup();
const jar = { cookies: [] };
function jarSet(res) {
const cookies = res.headers['set-cookie'];
if (cookies && cookies.length) {
jar.cookies = cookies.map((c) => c.split(';')[0]);
}
}
function jarGet() {
return jar.cookies.join('; ');
}
try {
// Register
const regRes = await fetch(base + '/api/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'landinguser', password: 'password123' }),
});
assert.equal(regRes.status, 201);
jarSet(regRes);
// Create a hero
await fetch(base + '/api/character', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Cookie: jarGet(),
},
body: JSON.stringify({ name: 'LandingHero', class: 'warrior' }),
});
// GET / with session cookie
const res = await fetch(base, {
headers: {
Accept: 'text/html',
Cookie: jarGet(),
},
});
assert.equal(res.status, 200);
const html = await res.text();
assert.ok(html.includes('Play'), 'should show Play button when logged in with hero');
assert.ok(html.includes('hidden'), 'Play button should use hidden class logic');
} finally {
await teardown();
cleanup();
}
});
test('10.5 GET / logged-in without hero → shows Login/Register, no Play', async () => {
const base = await setup();
const jar = { cookies: [] };
function jarSet(res) {
const cookies = res.headers['set-cookie'];
if (cookies && cookies.length) {
jar.cookies = cookies.map((c) => c.split(';')[0]);
}
}
function jarGet() {
return jar.cookies.join('; ');
}
try {
// Register (no hero)
const regRes = await fetch(base + '/api/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'landingnom', password: 'password123' }),
});
assert.equal(regRes.status, 201);
jarSet(regRes);
// GET / with session cookie but no hero
const res = await fetch(base, {
headers: {
Accept: 'text/html',
Cookie: jarGet(),
},
});
assert.equal(res.status, 200);
const html = await res.text();
assert.ok(html.includes('Login'), 'should show Login when no hero');
assert.ok(html.includes('Register'), 'should show Register when no hero');
} finally {
await teardown();
cleanup();
}
});
test('10.5 GET / anonymous → shows Login/Register, no Play', async () => {
const base = await setup();
try {
const res = await fetch(base, {
headers: { Accept: 'text/html' },
});
assert.equal(res.status, 200);
const html = await res.text();
assert.ok(html.includes('Login'), 'should show Login to anonymous users');
assert.ok(html.includes('Register'), 'should show Register to anonymous users');
} finally {
await teardown();
cleanup();
}
});
test('10.5 404/500 pages have working "Return to Emberfall" link to /', async () => {
const base = await setup();
try {
// Check 404 page
const res404 = await fetch(base + '/nonexistent-page', {
headers: { Accept: 'text/html' },
});
assert.equal(res404.status, 404);
const html404 = await res404.text();
assert.ok(html404.includes('href="/"'), '404 page should link to /');
// Check 500 page
const res500 = await fetch(base + '/nonexistent-page', {
headers: { Accept: 'text/html' },
});
assert.equal(res500.status, 404);
const html500 = await res500.text();
assert.ok(html500.includes('href="/"'), '500 page should link to /');
} finally {
await teardown();
cleanup();
}
});