step 10.5: wip (not ticked)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const PUBLIC_DIR = path.resolve(__dirname, '../../public');
|
||||
|
||||
function extractLocalRefs(filePath) {
|
||||
const html = fs.readFileSync(filePath, 'utf-8');
|
||||
const refs = [];
|
||||
|
||||
// Extract href/src values from <a>, <link>, <script>, <img> tags
|
||||
const refRegex = /(?:href|src)\s*=\s*["']([^"']+)["']/gi;
|
||||
let match;
|
||||
while ((match = refRegex.exec(html)) !== null) {
|
||||
const value = match[1];
|
||||
// Skip external URLs
|
||||
if (/^https?:\/\//i.test(value) || value.startsWith('//')) continue;
|
||||
// Skip data URIs, javascript:, mailto:
|
||||
if (/^(data:|javascript:|mailto:)/i.test(value)) continue;
|
||||
// Skip hash-only links
|
||||
if (value.startsWith('#')) continue;
|
||||
// Skip API endpoints (not static files)
|
||||
if (value.startsWith('/api/')) continue;
|
||||
// All Emberfall HTML uses absolute paths (starting with /)
|
||||
if (value.startsWith('/')) {
|
||||
refs.push(value); // keep leading / for PUBLIC_DIR resolution
|
||||
}
|
||||
}
|
||||
|
||||
return refs;
|
||||
}
|
||||
|
||||
test('link-checker: all local href/src references in public/*.html resolve to existing files', async () => {
|
||||
const htmlFiles = fs.readdirSync(PUBLIC_DIR)
|
||||
.filter((f) => f.endsWith('.html'))
|
||||
.map((f) => path.join(PUBLIC_DIR, f));
|
||||
|
||||
const broken = [];
|
||||
|
||||
for (const file of htmlFiles) {
|
||||
const refs = extractLocalRefs(file);
|
||||
const relFile = path.relative(PUBLIC_DIR, file);
|
||||
|
||||
for (const ref of refs) {
|
||||
// ref starts with / → resolve relative to PUBLIC_DIR
|
||||
const target = path.join(PUBLIC_DIR, ref.slice(1));
|
||||
const exists = fs.existsSync(target);
|
||||
if (!exists) {
|
||||
broken.push(`${relFile} → ${ref} (expected ${target})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert.equal(broken.length, 0, `Broken links found:\n${broken.join('\n')}`);
|
||||
});
|
||||
@@ -0,0 +1,155 @@
|
||||
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();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user