import { test } from '@japa/runner' import db from '@adonisjs/lucid/services/db' import testUtils from '@adonisjs/core/services/test_utils' import FileStore from '#services/FileStore' import ContentService from '#services/ContentService' let keys = 0 async function counterFile(name: string) { const now = new Date() const [r] = await db .table('releases') .insert({ release_key: `yenc${++keys}`.padEnd(40, 'y'), name, source: 'weak-stem', groups: '[]', first_seen_at: now, last_seen_at: now }) .returning('id') const [{ id }] = await FileStore.insert([ { filename: name, poster: 'bob@home.com', date: now.getTime(), parts: 2, size: 10, release_id: r.id, groups: '["alt.binaries.boneless"]', segments: [{ number: 1, id: `${name}@nyuu`, size: 10 }], created_at: now, updated_at: now }, ]) return db.from('files').where('id', id).first() } test.group('Names from the yEnc header', (group) => { group.each.setup(() => testUtils.db().withGlobalTransaction()) test('random-named files take the yEnc name and gather in its release', async ({ assert }) => { const a = await counterFile('P9BSPl59iDfQUVvXFeBVG4MDQaOTufds') const b = await counterFile('Faoq6nlEaVlTzYyNA27sSfFm2KvLHOvD') const nameFromYenc = (ContentService as any).nameFromYenc.bind(ContentService) assert.equal(await nameFromYenc(a, null, 'Deep.Strike.S01E16.1080p.WEB.h264-GRP.part01.rar'), 'Deep.Strike.S01E16.1080p.WEB.h264-GRP.part01.rar') await nameFromYenc(b, null, 'Deep.Strike.S01E16.1080p.WEB.h264-GRP.part02.rar') const [fa, fb] = await Promise.all([db.from('files').where('id', a.id).first(), db.from('files').where('id', b.id).first()]) assert.equal(fa.release_id, fb.release_id) assert.equal((await db.from('releases').where('id', fa.release_id).first()).name, 'Deep.Strike.S01E16.1080p.WEB.h264-GRP') // A random yEnc name, or a file that has a real name already, stays as it is. const c = await counterFile('Vg9QflfNLXVMPOWtK4KRJv474DadgTIF') assert.isUndefined(await nameFromYenc(c, null, 'qTcrIl1qTo4Bx6El5c4RtTA4ZSqwd6tz')) const d = await counterFile('Some.Release.part01.rar') assert.isUndefined(await nameFromYenc(d, null, 'Other.Name.part01.rar')) }) })