64 lines
2.9 KiB
JavaScript
64 lines
2.9 KiB
JavaScript
// Сценарии спеки deploy (TPTIP-6): сервер стенда и страница продукта.
|
|
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
const fs = require('node:fs');
|
|
const os = require('node:os');
|
|
const path = require('node:path');
|
|
|
|
const ROOT = path.join(__dirname, '..');
|
|
|
|
async function withServer(dir, fn) {
|
|
process.env.APP_DIR = dir;
|
|
delete require.cache[require.resolve('../server.js')];
|
|
const { createServer } = require('../server.js');
|
|
const srv = createServer().listen(0);
|
|
await new Promise((r) => srv.once('listening', r));
|
|
try { await fn(`http://127.0.0.1:${srv.address().port}`); } finally { srv.close(); delete process.env.APP_DIR; }
|
|
}
|
|
|
|
function tmpDir(files) {
|
|
const d = fs.mkdtempSync(path.join(os.tmpdir(), 'tptip6-'));
|
|
for (const [name, body] of Object.entries(files)) fs.writeFileSync(path.join(d, name), body);
|
|
return d;
|
|
}
|
|
|
|
const about = fs.readFileSync(path.join(ROOT, 'about.html'), 'utf8');
|
|
|
|
test('Сервер жив: GET /healthz → 200 «ok»', () => withServer(ROOT, async (base) => {
|
|
const r = await fetch(base + '/healthz');
|
|
assert.strictEqual(r.status, 200);
|
|
assert.strictEqual(await r.text(), 'ok');
|
|
}));
|
|
|
|
test('Неизвестный адрес: служебные файлы не отдаются (404)', () => withServer(ROOT, async (base) => {
|
|
for (const p of ['/.peppa/project.json', '/server.js', '/../AGENTS.md']) {
|
|
assert.strictEqual((await fetch(base + p)).status, 404, p);
|
|
}
|
|
}));
|
|
|
|
test('Приложение есть: «/» отдаёт index.html', () => withServer(tmpDir({ 'index.html': '<title>Трекер</title>', 'about.html': about }), async (base) => {
|
|
const r = await fetch(base + '/');
|
|
assert.strictEqual(r.status, 200);
|
|
assert.match(await r.text(), /<title>Трекер<\/title>/);
|
|
}));
|
|
|
|
test('Приложения ещё нет: «/» отдаёт страницу продукта', () => withServer(tmpDir({ 'about.html': about }), async (base) => {
|
|
const r = await fetch(base + '/');
|
|
assert.strictEqual(r.status, 200);
|
|
assert.match(r.headers.get('content-type'), /text\/html/);
|
|
assert.match(await r.text(), /data-list="check"/);
|
|
}));
|
|
|
|
test('Страница продукта на трёх языках: все ключи есть в ru, kk, en', () => {
|
|
const dict = JSON.parse(fs.readFileSync(path.join(ROOT, 'about.i18n.json'), 'utf8'));
|
|
assert.deepStrictEqual(Object.keys(dict).sort(), ['en', 'kk', 'ru']);
|
|
const keys = new Set([...about.matchAll(/data-(?:t|list)="([^"]+)"/g)].map((m) => m[1]));
|
|
for (const code of ['ru', 'kk', 'en']) for (const k of keys) assert.ok(dict[code][k], `${code}.${k}`);
|
|
assert.strictEqual(dict.ru.title, 'Трекер тренировок и питания');
|
|
assert.strictEqual(dict.en.title, 'Workout and nutrition tracker');
|
|
});
|
|
|
|
test('Неизвестный язык: страница выбирает русский', () => {
|
|
assert.match(about, /dict\[code\] \? code : 'ru'/);
|
|
});
|