Files
MangaCircus/scraper/test/weebcentral-volumes.test.js
T

70 lines
3.9 KiB
JavaScript

'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const { parseChapters, discoverWeebCatalog, discoverWeebChapter } = require('../adapters/weebcentral');
const { parseSpecialFolder } = require('../../chapter-identity');
const { selectByScope } = require('../lib/scope');
const { publishManifest } = require('../lib/publisher');
const { scanLibrary, shouldDownload } = require('../lib/library-inventory');
const reader = require('../../library');
const { Canvas } = require('../../tools/png');
const ids = ['01JHVPNS8PNMSNF4H18ERX3SMK', '01JHVPNS8PNSDS66BEPVHH536F'];
const seriesUrl = 'https://weebcentral.com/series/01J76XY8CF6MA6W5VJWZDNWESZ/Akira';
test('whole-volume releases keep their source IDs and do not masquerade as chapters', async t => {
const html = `<a href="/chapters/${ids[0]}">Volume 1</a><a href="/chapters/${ids[1]}">Volume 2</a>`;
const refs = parseChapters(html, seriesUrl);
assert.equal(refs.length, 2);
assert.deepEqual(refs.map(ref => ref.volume), ['1', '2']);
assert.ok(refs.every(ref => ref.special && ref.releaseKind === 'volume'));
assert.equal(parseSpecialFolder(refs[0].number + ' [WeebCentral]').releaseKind, 'volume');
assert.equal(selectByScope(refs, { mode: 'volume', volume: '2' })[0].id, ids[1]);
assert.equal(selectByScope(refs, { mode: 'chapter', chapter: '1' }).length, 0);
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'mc-volume-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const page = path.join(root, '.fixture.png');
await fs.writeFile(page, new Canvas(10, 20, [1, 2, 3]).png());
const manifest = { version: 1, series: { title: 'Volume fixture', direction: 'rtl', sources: [{ name: 'WeebCentral', url: seriesUrl }] },
chapters: [refs[0], { number: '1', source: { name: 'Other', url: 'https://example.org/1' } }].map(ref => ({
...ref, source: ref.source || { name: 'WeebCentral', url: ref.url }, pages: [{ path: page }],
})) };
await publishManifest(manifest, { libraryRoot: root });
const scanned = (await reader.scanLibrary(root)).index.mangas[0];
assert.equal(scanned.chapters.length, 2);
assert.ok(scanned.chapters.some(ch => ch.label === 'Volume 1'));
assert.ok(scanned.chapters.some(ch => ch.label === 'Ch. 1'));
const manga = (await scanLibrary(root)).mangas[0];
assert.equal(shouldDownload(manga, refs[0], 'weebcentral'), false);
assert.equal(shouldDownload(manga, refs[1], 'weebcentral'), true);
assert.equal(shouldDownload(manga, refs[0], 'weebcentral', 'add-source'), false);
const numberedOnly = { ...manga, chapters: manga.chapters.filter(chapter => chapter.releaseKind !== 'volume') };
assert.equal(shouldDownload(numberedOnly, refs[0], 'weebcentral'), false);
assert.equal(shouldDownload(numberedOnly, refs[0], 'weebcentral', 'add-source'), true);
});
test('catalog ordering and chapter discovery preserve whole-volume metadata', async () => {
const html = `<h1>Volume fixture</h1><a href="/chapters/${ids[0]}">Volume 1</a>`
+ `<a href="/chapters/${ids[1]}">Chapter 1</a>`;
const http = { async get(url) {
const body = url === seriesUrl ? html
: url === `https://weebcentral.com/chapters/${ids[0]}` ? '<time datetime="2026-01-01"></time>'
: '<img src="https://images.example/1.jpg">';
return { async text() { return body; } };
} };
const catalog = await discoverWeebCatalog(seriesUrl, http);
assert.equal(catalog.chapters.length, 2);
assert.equal(catalog.chapters[0].number, '1');
assert.equal(catalog.chapters[1].label, 'Volume 1');
const chapter = await discoverWeebChapter(catalog.chapters[1], http);
assert.equal(chapter.releaseKind, 'volume');
assert.equal(chapter.sourceId, ids[0]);
assert.equal(chapter.sourceNumber, '1');
assert.equal(chapter.book.number, 1);
assert.equal(chapter.pages.length, 1);
});