jsonb can't hold \u0000, and ON CONFLICT can't take a row twice in one statement: either failed the whole flush, its outcomes lost and its candidates taken until a restart. NULs are dropped from strings; the probe_cache insert takes each message-id once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
2.9 KiB
TypeScript
46 lines
2.9 KiB
TypeScript
import { test } from '@japa/runner'
|
|
import db from '@adonisjs/lucid/services/db'
|
|
import testUtils from '@adonisjs/core/services/test_utils'
|
|
import { Prober } from '#services/prober'
|
|
|
|
test.group('Prober', (group) => {
|
|
group.each.setup(() => testUtils.db().withGlobalTransaction())
|
|
|
|
test('a claim takes each tier: the readable tiers also take unreadable candidates, a kind with only one of them too', async ({ assert }) => {
|
|
const [row] = await db
|
|
.table('releases')
|
|
.insert({ release_key: 'pb'.padEnd(40, '0'), identity_key: 'pb'.padEnd(40, '0'), name: 'pb', source: 'subject', groups: '[]', files: 3 })
|
|
.returning('id')
|
|
const candidate = (kind: string, unreadable: boolean, ready = true) => ({
|
|
release_id: row.id, kind, message_id: `<${kind}-${unreadable}-${ready}@x>`, file: kind, ready, unreadable,
|
|
})
|
|
// One per kind, so each tier's id range is a single id (the slices' starts aren't random then).
|
|
await db.table('probe_candidates').multiInsert([candidate('lone', true), candidate('par2', true), candidate('archive', false), candidate('7z-tail', false, false)])
|
|
|
|
const prober = new Prober() as any
|
|
await prober.claimTiers()
|
|
assert.sameMembers(prober.queue.map((c: { kind: string }) => c.kind), ['lone', 'par2', 'archive'])
|
|
const status = Object.fromEntries((await db.from('probe_candidates').select('kind', 'status')).map((c) => [c.kind, c.status]))
|
|
assert.deepEqual(status, { 'lone': 'taken', 'par2': 'taken', 'archive': 'taken', '7z-tail': 'waiting' })
|
|
})
|
|
|
|
test('a flush stores a name with a NUL in it, and one article that two releases have as a candidate', async ({ assert }) => {
|
|
const release = async (key: string) =>
|
|
Number((await db.table('releases').insert({ release_key: key.padEnd(40, '0'), identity_key: key.padEnd(40, '0'), name: key, source: 'subject', groups: '[]', files: 1 }).returning('id'))[0].id)
|
|
const a = await release('fa')
|
|
const b = await release('fb')
|
|
const candidate = async (releaseId: number) =>
|
|
Number((await db.table('probe_candidates').insert({ release_id: releaseId, kind: 'archive', message_id: '<same@x>', file: 'x.rar', status: 'taken' }).returning('id'))[0].id)
|
|
const inspection = { yencName: 'x.rar', size: 1000, type: null, names: ['Movie\u0000.mkv'], entries: 1, passworded: false }
|
|
const prober = new Prober() as any
|
|
for (const releaseId of [a, b]) {
|
|
prober.outcomes.push({ candidate: { id: await candidate(releaseId), releaseId, kind: 'archive', messageId: '<same@x>' }, status: 'done', inspection })
|
|
}
|
|
await prober.flush()
|
|
assert.deepEqual((await db.from('probe_candidates').select('status')).map((c) => c.status), ['done', 'done'])
|
|
const cached = await db.from('probe_cache').where('message_id', '<same@x>').firstOrFail()
|
|
assert.deepEqual(cached.result.names, ['Movie.mkv'])
|
|
assert.equal((await db.from('release_inspections').count('* as n'))[0].n, '2')
|
|
})
|
|
})
|