71 lines
3.9 KiB
TypeScript
71 lines
3.9 KiB
TypeScript
import { test } from '@japa/runner'
|
|
import { deflateRawSync } from 'node:zlib'
|
|
import { decodeSpotBinary, parseSpotArticleHeaders, parseSpotHeader, parseSpotXml, verifySpotHeader } from '#lib/spotnet/index'
|
|
|
|
// A real spot from free.pt (self-signed, key 7), 2026-09-28.
|
|
const SPOT = {"from": "Bart <0RnzK11s9B4jqZdfc0UHj1qpSrgBw7nKaYBPckvJuk8sKchVXvdofY6qcKkZE-s9x.Fbc6Dm09v32bUgXpDT88QADBHELZ7wP4sMIMHKpQYxBIlK-pZveUFu0aMYUx-pUf6Z@17a09b03c07c10d17z00.21651233204.20.1790619886.1.NL.Uyf3JHAAlVvgAg9HKIu3veFDGC4DX9-sgTnchacVAkC-p3USalOe-sacdamKdgXirL6>", "subject": "The.Incredible.Shrinking.Woman.1981.1080p.BluRay.REMUX.AVC.FLAC.2.0-MuGi | Bart", "id": "<jWCbaAa4f1A7LC6agmOEx@spot.net>", "date": "Mon, 28 Sep 2026 18:24:45 GMT"}
|
|
|
|
test.group('spotnet / headers', () => {
|
|
test('parses a spot header', ({ assert }) => {
|
|
const spot = parseSpotHeader(SPOT.from, SPOT.subject, SPOT.id, SPOT.date)!
|
|
assert.include(spot, {
|
|
poster: 'Bart',
|
|
title: 'The.Incredible.Shrinking.Woman.1981.1080p.BluRay.REMUX.AVC.FLAC.2.0-MuGi',
|
|
tag: 'Bart',
|
|
category: 0,
|
|
keyId: 7,
|
|
size: 21651233204,
|
|
messageId: 'jWCbaAa4f1A7LC6agmOEx@spot.net',
|
|
})
|
|
assert.deepEqual(spot.subcats, { a: [9], b: [3], c: [7, 10], d: [17], z: [0] })
|
|
})
|
|
|
|
test('verifies its signatures, and rejects another message id', ({ assert }) => {
|
|
const spot = parseSpotHeader(SPOT.from, SPOT.subject, SPOT.id, SPOT.date)!
|
|
assert.isTrue(verifySpotHeader(spot))
|
|
assert.isFalse(verifySpotHeader({ ...spot, messageId: 'other@spot.net' }))
|
|
assert.isNull(parseSpotHeader('someone <x@y>', 'subject', '<a@b>'))
|
|
})
|
|
})
|
|
|
|
test.group('spotnet / xml and nzb', () => {
|
|
test('reads the NZB segments and category from the X-XML lines', ({ assert }) => {
|
|
const raw = [
|
|
'From: Bart <x@y>',
|
|
'X-XML: <Spotnet><Posting><Key>7</Key><Title>A & B</Title><Category>01<Sub>01a09</Sub>',
|
|
'X-XML: <Sub>01z00</Sub></Category><NZB><Segment>abc@spot.net</Segment><Segment>def@spot.net</Segment></NZB>',
|
|
'X-XML: <Size>123</Size></Posting></Spotnet>',
|
|
].join('\r\n')
|
|
const { headers, xml } = parseSpotArticleHeaders(raw)
|
|
assert.equal(headers.from, 'Bart <x@y>')
|
|
const spot = parseSpotXml(xml)!
|
|
assert.deepEqual(spot.nzbSegments, ['abc@spot.net', 'def@spot.net'])
|
|
assert.include(spot, { title: 'A & B', category: 0, size: 123 })
|
|
assert.deepEqual(spot.subcats, ['01a09', '01z00'])
|
|
})
|
|
|
|
test('decodes an escaped, deflated NZB split over articles', ({ assert }) => {
|
|
const nzb = '<?xml version="1.0"?>\n<nzb>\r\n=\0</nzb>'.repeat(20)
|
|
const escaped = deflateRawSync(Buffer.from(nzb, 'latin1'))
|
|
.toString('latin1')
|
|
.replace(/[=\n\r\0]/g, (c) => ({ '=': '=D', '\n': '=C', '\r': '=B', '\0': '=A' })[c]!)
|
|
// Posted as lines, dot-stuffed, over two articles.
|
|
const lines = escaped.match(/.{1,40}/gs)!.map((l) => (l.startsWith('.') ? '.' + l : l))
|
|
const half = Math.ceil(lines.length / 2)
|
|
const bodies = [lines.slice(0, half), lines.slice(half)].map((ls) => Buffer.from(ls.join('\r\n'), 'latin1'))
|
|
assert.equal(decodeSpotBinary(bodies).toString('latin1'), nzb)
|
|
})
|
|
})
|
|
|
|
test.group('spotnet / moderation', () => {
|
|
test('recognises delete and dispose commands from key 2 only', async ({ assert }) => {
|
|
const { moderationTarget, spotterId } = await import('#lib/spotnet/index')
|
|
assert.equal(moderationTarget({ keyId: 2, title: 'DISPOSE 7IzfA0aCFTkOne6agvY06@spot.net - The Voice DE S15E07 (1080p)' }), '7IzfA0aCFTkOne6agvY06@spot.net')
|
|
assert.equal(moderationTarget({ keyId: 2, title: 'delete <abc@spot.net>' }), 'abc@spot.net')
|
|
assert.isNull(moderationTarget({ keyId: 7, title: 'DISPOSE abc@spot.net' }))
|
|
assert.isNull(moderationTarget({ keyId: 2, title: 'The Disposal 2024' }))
|
|
// Spotweb: base64 of the little-endian CRC32 of the key, without / + =.
|
|
assert.match(spotterId('0RnzK11s9B4jqZdfc0UHj1qpSrgBw7nKaYBPckvJuk8sKchVXvdofY6qcKkZE/9x'), /^[A-Za-z0-9]{6}$/)
|
|
})
|
|
})
|