import { test } from '@japa/runner' import db from '@adonisjs/lucid/services/db' import testUtils from '@adonisjs/core/services/test_utils' import Par2Service from '#services/Par2Service' import type { Par2Set } from '#lib/par2/index' const VOLUME = 10_000_952 // a standard split-volume size that recurs across posts async function release(key: string, name: string) { const now = new Date() const [row] = await db .table('releases') .insert({ release_key: key.padEnd(40, '0'), name, source: 'weak-stem', groups: '[]', posted_at: now, first_seen_at: now, last_seen_at: now }) .returning('id') return Number(row.id) } async function binary(key: string, fields: Record) { const now = new Date() const [row] = await db .table('binaries') .insert({ file_key: key.padEnd(40, '0'), poster: 'x', subject: '"x" yEnc', name: 'Xq8r2KdLm3Zw91PqA7', total_parts: 14, groups: '[]', posted_at: now, first_seen_at: now, last_seen_at: now, ...fields }) .returning(['id', 'poster', 'groups', 'posted_at']) return row } function set(id: string, files: { name: string; md5_16k: string }[]): Par2Set { return { recoverySetId: id.padEnd(32, '0'), files: files.map((f, i) => ({ fileId: `${id}${i}`.padEnd(32, '0'), md5: '0'.repeat(32), md5_16k: f.md5_16k, length: VOLUME, name: f.name })), } } test.group('Par2Service.attach', (group) => { group.each.setup(() => testUtils.db().withGlobalTransaction()) test('a set does not join another post just because a size matches', async ({ assert }) => { const other = await release('other', 'OtherPost') await binary('other-vol', { size: VOLUME, md5_16k: 'a'.repeat(32), release_id: other }) const par2 = await binary('par2-b', { size: 50_000, total_parts: 1 }) const result = await Par2Service.attach(par2, set('b', [{ name: 'PostB.part1.rar', md5_16k: 'b'.repeat(32) }])) assert.notEqual(result.releaseId, other) assert.equal(await db.from('release_files').where('release_id', other).count('* as n').first().then((r) => Number(r.n)), 0) }) test('an exactly matching file pulls the set into its release and gets its real name', async ({ assert }) => { const stemRelease = await release('stem', 'Xq8r2KdLm3Zw91PqA7') const vol = await binary('vol', { size: VOLUME, md5_16k: 'c'.repeat(32), release_id: stemRelease }) const par2 = await binary('par2-c', { size: 50_000, total_parts: 1 }) const result = await Par2Service.attach(par2, set('c', [{ name: 'Real.Name.part1.rar', md5_16k: 'c'.repeat(32) }])) assert.equal(result.releaseId, stemRelease) assert.equal((await db.from('binaries').where('id', vol.id).first()).real_name, 'Real.Name.part1.rar') }) })