'use strict'; const assert = require('node:assert/strict'); const fs = require('node:fs/promises'); const os = require('node:os'); const path = require('node:path'); const { buildLibrary, createServer } = require('../deploy/library-server.cjs'); (async () => { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'mangabom-server-test-')); let server; try { const folder = path.join(root, 'LORD_OF_CHEAP_LUNCH'); const chapter = path.join(folder, 'chapter-001'); await fs.mkdir(chapter, { recursive: true }); await fs.writeFile(path.join(folder, 'series.json'), JSON.stringify({ id: 'food', title: 'Повелитель дешёвого обеда' })); let library = await buildLibrary(root); assert.equal(library.series[0].id, 'food'); assert.equal(library.series[0].folder, 'LORD_OF_CHEAP_LUNCH'); assert.equal(library.series[0].chapters[0].ready, false); server = createServer(root); await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); const base = 'http://127.0.0.1:' + server.address().port; assert.equal((await fetch(base + '/healthz')).status, 200); for (let i = 1; i <= 15; i++) await fs.writeFile(path.join(chapter, String(i).padStart(2, '0') + '.png'), Buffer.from([137,80,78,71])); assert.equal((await (await fetch(base + '/library.json')).json()).series[0].chapters[0].ready, false); await fs.writeFile(path.join(chapter, '016.PNG'), Buffer.from([137,80,78,71])); library = await (await fetch(base + '/library.json')).json(); assert.equal(library.series[0].chapters[0].ready, true); assert.equal(library.series[0].chapters[0].pages.length, 16); assert.equal(library.series[0].chapters[0].pages[9].src, 'manga/LORD_OF_CHEAP_LUNCH/chapter-001/10.png'); assert.equal(library.series[0].chapters[0].pages[15].src, 'manga/LORD_OF_CHEAP_LUNCH/chapter-001/016.PNG'); assert.equal((await (await fetch(base + '/library.json')).json()).generatedAt, library.generatedAt); const head = await fetch(base + '/library.json', { method: 'HEAD' }); assert.equal(head.status, 200); assert.equal(await head.text(), ''); assert.equal((await fetch(base + '/library.json', { method: 'POST' })).status, 405); assert.equal((await fetch(base + '/anything-else')).status, 404); await fs.writeFile(path.join(chapter, '1.jpg'), Buffer.from([1])); assert.equal((await (await fetch(base + '/library.json')).json()).series[0].chapters[0].ready, false); await fs.unlink(path.join(chapter, '1.jpg')); await fs.writeFile(path.join(chapter, 'page-17.png'), Buffer.from([1])); assert.equal((await buildLibrary(root)).series[0].chapters[0].ready, false); await fs.unlink(path.join(chapter, 'page-17.png')); const second = path.join(folder, 'chapter-002'); await fs.mkdir(second); await fs.writeFile(path.join(second, 'chapter.json'), JSON.stringify({ title: 'Продолжение' })); assert.equal((await (await fetch(base + '/library.json')).json()).series[0].chapters[1].title, 'Продолжение'); const currentRoot = path.join(__dirname, '../dist/manga'); const live = await buildLibrary(currentRoot); const existing = JSON.parse(await fs.readFile(path.join(currentRoot, 'library.json'), 'utf8')); assert.deepEqual(live.series, existing.series, 'Server catalogue must match the existing Windows catalogue'); assert.equal(live.series.find(item => item.id === 'food').chapters[0].ready, true); console.log('PASS: server-side auto-indexing, real 16-page chapter, stable catalogue, HTTP methods, numeric sorting, duplicates and custom chapters.'); } finally { if (server) await new Promise(resolve => server.close(resolve)); if (!path.resolve(root).startsWith(path.resolve(os.tmpdir()) + path.sep + 'mangabom-server-test-')) throw Error('Unsafe cleanup path'); await fs.rm(root, { recursive: true, force: true }); } })().catch(error => { console.error(error); process.exitCode = 1; });