import { test } from '@japa/runner' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { gzipSync } from 'node:zlib' import coordinatorConfig from '#config/coordinator' import Corpus from '#services/corpus' test.group('Corpus', (group) => { let dir: string let before: string | undefined group.each.setup(async () => { dir = await mkdtemp(join(tmpdir(), 'corpus-')) before = coordinatorConfig.corpus.dir coordinatorConfig.corpus.dir = dir }) group.each.teardown(async () => { coordinatorConfig.corpus.dir = before await rm(dir, { recursive: true, force: true }) }) test('knows the chunks it holds, and a range once all are there', async ({ assert }) => { const range = { group: 'alt.binaries.test', start: 1, end: 200_000 } const first = { from: 1, to: 100_000 } const second = { from: 100_001, to: 200_000 } assert.isFalse(await Corpus.has(range, first)) await Corpus.write(range, first, gzipSync('1\tsubject\n'), 2) assert.isTrue(await Corpus.has(range, first)) assert.isFalse(await Corpus.complete(range)) // A reply cut short isn't kept. await Corpus.write(range, second, Buffer.from('not gzip'), 2) assert.isFalse(await Corpus.has(range, second)) await Corpus.write(range, second, gzipSync('2\tsubject\n'), 2) assert.isTrue(await Corpus.complete(range)) assert.equal((await Corpus.read(range, second))?.length, gzipSync('2\tsubject\n').length) }) })