import { test } from '@japa/runner' import db from '@adonisjs/lucid/services/db' import testUtils from '@adonisjs/core/services/test_utils' import StagingService from '#services/StagingService' import IngestService, { segmentFromHeader } from '#services/IngestService' import { counterFileKey } from '#lib/subject/index' let n = 0 /** An article of a random-subject upload: a new random name every time. */ const randomArticle = (k: number, total = 4, { counter = '[21/62]', date = 'Thu, 01 Oct 2026 12:00:00 GMT', id }: { counter?: string; date?: string; id?: string } = {}) => segmentFromHeader( { 'subject': `${counter} "Rnd${++n}xQ7kLm2Pz9Wv4Ty8Ab3Cd" yEnc (${k}/${total})`, 'from': 'bob@home.com', 'message-id': `<${id ?? `art${n}`}@nyuu>`, 'date': date, ':bytes': '700000' }, 'alt.binaries.boneless', k )! async function ingestBatch(segments: ReturnType[], counters = true) { const r = await StagingService.filter(segments, { counters }) await IngestService.ingest(r.ingest) return r } test.group('Counter grouping', (group) => { group.each.setup(() => testUtils.db().withGlobalTransaction()) test('articles with random subjects become one file by poster, counter and part count', async ({ assert }) => { const key = counterFileKey(randomArticle(1))! await ingestBatch([randomArticle(1)]) assert.isNull(await db.from('binaries').where('file_key', key).first()) await ingestBatch([randomArticle(2)]) const binary = await db.from('binaries').where('file_key', key).first() assert.isNotNull(binary) // Later articles join the file directly, until it completes. await ingestBatch([randomArticle(3)]) await ingestBatch([randomArticle(4)]) const parts = await db.from('parts').where('binary_id', binary.id).orderBy('number').select('number') assert.deepEqual(parts.map((p) => p.number), [1, 2, 3, 4]) assert.isNotNull((await db.from('binaries').where('id', binary.id).first()).completed_at) }) test('off by default; another day is another file; a repeated part number is a conflict', async ({ assert }) => { await ingestBatch([randomArticle(1, 9, { counter: '[05/10]' })], false) await ingestBatch([randomArticle(2, 9, { counter: '[05/10]' })], false) assert.isNull(await db.from('binaries').where('file_key', counterFileKey(randomArticle(1, 9, { counter: '[05/10]' }))!).first()) const other = randomArticle(1, 9, { counter: '[06/10]', date: 'Fri, 02 Oct 2026 12:00:00 GMT' }) assert.notEqual(counterFileKey(other), counterFileKey(randomArticle(1, 9, { counter: '[06/10]' }))) // Two uploads of the same shape on one day: part 1 twice, from different articles. await ingestBatch([randomArticle(1, 9, { counter: '[07/10]' })]) await ingestBatch([randomArticle(2, 9, { counter: '[07/10]' })]) await ingestBatch([randomArticle(1, 9, { counter: '[07/10]' })]) const binary = await db.from('binaries').where('file_key', counterFileKey(randomArticle(1, 9, { counter: '[07/10]' }))!).first() assert.isNotNull(binary.conflicted_at) }) test('a normal file with a counter stays a normal file', async ({ assert }) => { const normal = (k: number) => segmentFromHeader( { 'subject': `[07/45] - "Some.Release.part06.rar" yEnc (${k}/3)`, 'from': 'poster@x', 'message-id': ``, 'date': 'Thu, 01 Oct 2026 12:00:00 GMT', ':bytes': '700000' }, 'alt.binaries.test', k )! await ingestBatch([normal(1)]) await ingestBatch([normal(2), normal(3)]) const binary = await db.from('binaries').where('name', 'Some.Release.part06.rar').first() assert.equal(binary.file_key, normal(1).fileKey) assert.isNotNull(binary.completed_at) }) })