import { test } from '@japa/runner' import db from '@adonisjs/lucid/services/db' import testUtils from '@adonisjs/core/services/test_utils' import NzbImportService, { nzbFileName } from '#services/NzbImportService' import SearchService from '#services/SearchService' const XML = ` Real.Name.2026.1080p.WEB-DL-GRPhunter2 alt.binaries.bloaf known-1@xknown-2@x alt.binaries.cores new-1@x ` async function file(fields: Record) { const now = new Date() const [row] = await db .table('files') .insert({ poster: 'p@x', date: now.getTime(), parts: 1, size: 100, groups: '[]', created_at: now, updated_at: now, ...fields }) .returning('*') return row } test.group('NzbImportService', (group) => { group.each.setup(() => testUtils.db().withGlobalTransaction()) test('names entries despite odd quoting', ({ assert }) => { assert.equal(nzbFileName('Wond40 "Nachbe" Nachchbee2025bebeSD.part1.rar" yEnc (0001/1849)'), 'Nachchbee2025bebeSD.part1.rar') assert.equal(nzbFileName('"x.par2" yEnc (1/1)'), 'x.par2') }) test('joins the release of known files, adds the rest, keeps name and password', async ({ assert }) => { const now = new Date() const [release] = await db .table('releases') .insert({ release_key: 'z'.repeat(40), name: 'Xq8r2KdLm3Zw91PqA7', source: 'stem', groups: '[]', first_seen_at: now, last_seen_at: now }) .returning('id') await file({ filename: 'Xq8r2KdLm3Zw91PqA7.part1.rar', release_id: release.id, message_ids: JSON.stringify({ '1': { id: 'known-1@x', size: 700 } }) }) const posted = await file({ filename: 'Xq8r2KdLm3Zw91PqA7.nzb', message_ids: JSON.stringify({ '1': { id: 'nzb-1@x', size: 2000 } }) }) const result = (await NzbImportService.import(posted, XML))! assert.equal(result.releaseId, Number(release.id)) assert.include(result, { listed: 2, known: 1, added: 1, password: true }) const updated = await db.from('releases').where('id', release.id).first() assert.equal(updated.name, 'Real.Name.2026.1080p.WEB-DL-GRP') assert.equal(updated.password, 'hunter2') assert.equal(updated.file_total, 2) const files = await db.from('files').where('release_id', release.id).orderBy('filename').select('filename', 'groups') assert.deepEqual(files.map((f) => f.filename), ['Xq8r2KdLm3Zw91PqA7.nzb', 'Xq8r2KdLm3Zw91PqA7.part1.rar', 'Xq8r2KdLm3Zw91PqA7.part2.rar']) // Our own NZB passes the password on. const nzb = await SearchService.nzb(Number(release.id)) assert.include(nzb!.xml, 'hunter2') // Importing again changes nothing. const again = (await NzbImportService.import(posted, XML))! assert.equal(again.added, 0) }) test('NZBs from one archive become separate releases named after their entries', async ({ assert }) => { const nzbXml = (id: string) => ` alt.binaries.x ${id}@x ` const archive = await file({ filename: 'JOCpLxSgyzOPhBWXL5riLy.rar', message_ids: JSON.stringify({ '1': { id: 'rar-1@x', size: 2000 } }) }) const a = (await NzbImportService.import(archive, nzbXml('a'), { entryName: 'The Kinks - Misfits-LP-1978.nzb', moveContainer: false }))! const b = (await NzbImportService.import(archive, nzbXml('b'), { entryName: 'The Kinks - Sleepwalker-LP-1977.nzb', moveContainer: false }))! assert.notEqual(a.releaseId, b.releaseId) assert.equal(a.releaseName, 'The Kinks - Misfits-LP-1978') assert.equal(b.releaseName, 'The Kinks - Sleepwalker-LP-1977') const container = await db.from('files').where('id', archive.id).first() assert.isNull(container.release_id) }) test('a posted NZB alone in its own release moves to the release it describes', async ({ assert }) => { const now = new Date() const insert = async (key: string, name: string) => (await db.table('releases').insert({ release_key: key.padEnd(40, 'r'), name, source: 'stem', groups: '[]', first_seen_at: now, last_seen_at: now }).returning('id'))[0].id const target = await insert('target', 'Backbeat.1994.1080p.BluRay.x264-OFT') const lonely = await insert('lonely', '.nzb') await file({ filename: 'x.part1.rar', release_id: target, message_ids: JSON.stringify({ '1': { id: 'known-1@x', size: 700 } }) }) const posted = await file({ filename: '.nzb', release_id: lonely, message_ids: JSON.stringify({ '1': { id: 'nzb-2@x', size: 2000 } }) }) await NzbImportService.import(posted, XML) assert.equal(Number((await db.from('files').where('id', posted.id).first()).release_id), Number(target)) assert.notExists(await db.from('releases').where('id', lonely).first()) }) })