Files
usenet-indexer/tests/functional/staging.spec.ts
T
Daan MeijerandClaude Opus 5.5 be8a3d4580 Spotnet import, header backfill, staging and counter grouping, faster search
Spotnet (CONCEPT D29):
- Index free.pt fully and import spots with their descriptions, categories
  and NZBs; a spot renames its release, since Spotnet names are more
  trustworthy than names derived from subjects.
- Find passwords in spot descriptions too, and tell "needs a password we
  have" from "needs a password we don't have".

Content inspection:
- List the contents of 7z (.7z.001) archives, SFV sets, nicovideo uploads
  and other archive-like files; mark yEncBin .bin containers as encrypted.
- Take real names from yEnc headers for randomly named files, and merge
  uploads that were split across posts.

Storage (D31, D32):
- Store segment lists zstd-compressed behind the FileStore accessor.
- group:backfill indexes older headers at low priority and pauses when
  the NVMe or HDD part of the database gets too large.

Grouping (D35, D36):
- Lone header segments wait in staged_segments until a second segment of
  their file arrives, which drops the per-article junk binaries.
- Counter grouping joins random-subject uploads by poster, [i/M] counter,
  part count and day.

Search and maintenance (D33, D34):
- A trigram-indexed search_text column, smaller indexes, estimated stats.
- Passes that clean up nonsense names; weekly index maintenance; GC budgets
  per kind.
- Web UI: grey out stale results while a search runs and cancel superseded
  requests. Count only up to 1,000 matches for the page, which made filter
  clicks take seconds (Newznab keeps the 10,000 cap).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 19:59:23 +02:00

71 lines
3.6 KiB
TypeScript

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'
const segment = (name: string, k: number, n: number, id = `${name}-${k}@x`) =>
segmentFromHeader(
{ 'subject': `"${name}" yEnc (${k}/${n})`, 'from': 'poster@x', 'message-id': `<${id}>`, 'date': 'Thu, 01 Oct 2026 12:00:00 GMT', ':bytes': '700000' },
'alt.binaries.test',
k
)!
async function binary(name: string) {
return db.from('binaries').where('name', name).first()
}
test.group('Staging', (group) => {
group.each.setup(() => testUtils.db().withGlobalTransaction())
test('a lone segment waits; its second segment brings both in', async ({ assert }) => {
let r = await StagingService.filter([segment('rnd1.bin', 3, 50)])
assert.equal(r.staged, 1)
assert.lengthOf(r.ingest, 0)
await IngestService.ingest(r.ingest)
assert.isNull(await binary('rnd1.bin'))
// The same article again (a re-fetched range) is no second segment.
r = await StagingService.filter([segment('rnd1.bin', 3, 50)])
assert.lengthOf(r.ingest, 0)
// Nor is the same article crossposted to another group (other group, other article number).
const crosspost = { ...segment('rnd1.bin', 3, 50), groups: ['alt.binaries.boneless'], articleNumber: 160154327571 }
r = await StagingService.filter([crosspost])
assert.lengthOf(r.ingest, 0)
r = await StagingService.filter([segment('dup.bin', 2, 9), { ...segment('dup.bin', 2, 9), groups: ['alt.binaries.boneless'] }])
assert.equal(r.staged, 1)
r = await StagingService.filter([segment('rnd1.bin', 4, 50)])
assert.deepEqual(r.ingest.map((s) => s.number).sort(), [3, 4])
await IngestService.ingest(r.ingest)
const b = await binary('rnd1.bin')
assert.equal(Number((await db.from('parts').where('binary_id', b.id).count('* as n'))[0].n), 2)
assert.lengthOf(await db.from('staged_segments').where('file_key', segment('rnd1.bin', 3, 50).fileKey), 0)
// Later segments of a known file go straight in.
r = await StagingService.filter([segment('rnd1.bin', 9, 50)])
assert.lengthOf(r.ingest, 1)
})
test('single-part files and files with two segments in the batch go straight in; staged ones join them', async ({ assert }) => {
// Part 1 arrived alone at the end of the previous batch.
await StagingService.filter([segment('real.part01.rar', 1, 3)])
const r = await StagingService.filter([segment('real.part01.rar', 2, 3), segment('real.part01.rar', 3, 3), segment('one.nfo', 1, 1)])
assert.equal(r.staged, 0)
assert.sameMembers(r.ingest.map((s) => `${s.name}:${s.number}`), ['real.part01.rar:1', 'real.part01.rar:2', 'real.part01.rar:3', 'one.nfo:1'])
})
test('GC adopts segments whose file got a binary, and drops old ones', async ({ assert }) => {
await StagingService.filter([segment('late.bin', 7, 10)])
await StagingService.filter([segment('old.bin', 2, 10)])
await db.from('staged_segments').where('file_key', segment('old.bin', 2, 10).fileKey).update({ staged_at: new Date(Date.now() - 3 * 3600_000) })
// Another job created the binary meanwhile.
await IngestService.ingest([segment('late.bin', 1, 10), segment('late.bin', 2, 10)])
const result = await StagingService.sweep(2 * 3600, Date.now() + 10_000)
assert.deepEqual(result, { adopted: 1, expired: 1 })
const b = await binary('late.bin')
assert.equal(Number((await db.from('parts').where('binary_id', b.id).count('* as n'))[0].n), 3)
})
})