import { describe, test, expect, afterAll } from 'bun:test'; import { mkdtemp, rm, stat, utimes, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { JobBusyError, PutJobs } from '../src/put-jobs.js'; import { mediaGrantFrom } from '../src/grant.js'; import { randomBytes } from '../src/bytes.js'; const dirs: string[] = []; async function tempDir(): Promise { const d = await mkdtemp(join(tmpdir(), 'shade-jobs-')); dirs.push(d); return d; } afterAll(async () => { for (const d of dirs) await rm(d, { recursive: true, force: true }); }); const result = () => ({ mediaId: randomBytes(16), mediaKey: randomBytes(32), rootDigest: randomBytes(32), plaintextSize: 10, sealedSize: 100, segmentSize: 65536, segmentCount: 1, }); describe('PutJobs', () => { test('a job is the server, the file as it is now, and the options; a changed file is another job', async () => { const d = await tempDir(); const jobs = new PutJobs(join(d, 'jobs')); const film = join(d, 'film.mkv'); await writeFile(film, 'one'); const k = await jobs.key('http://relay/', film, 'web'); expect(await jobs.key('http://relay', film, 'web')).toBe(k); expect(await jobs.key('http://other', film, 'web')).not.toBe(k); expect(await jobs.key('http://relay', film, 'plain')).not.toBe(k); await writeFile(film, 'two!'); expect(await jobs.key('http://relay', film, 'web')).not.toBe(k); }); test('resumable only with a whole sealed copy; done keeps the grant and drops the copy; the key file is 0600', async () => { const d = await tempDir(); const jobs = new PutJobs(d); const key = 'a'.repeat(32); const release = await jobs.claim(key); const job = { result: result(), readToken: randomBytes(32), meta: { name: 'Film.mkv' } }; await jobs.save(key, '/films/Film.mkv', job); expect(await jobs.load(key)).toBeNull(); // no sealed copy yet await writeFile(jobs.sealedPath(key), new Uint8Array(99)); expect(await jobs.load(key)).toBeNull(); // cut short await writeFile(jobs.sealedPath(key), new Uint8Array(100)); const back = (await jobs.load(key))!; expect(back.result.mediaKey).toEqual(job.result.mediaKey); expect(back.readToken).toEqual(job.readToken); expect(((await stat(join(d, key, 'job.json'))).mode & 0o777).toString(8)).toBe('600'); const grant = mediaGrantFrom(job.result, { url: 'http://relay/v1/media/x', readToken: job.readToken, meta: job.meta }); await jobs.done(key, '/films/Film.mkv', job, grant); expect(await stat(jobs.sealedPath(key)).catch(() => null)).toBeNull(); expect((await jobs.load(key))!.grant!.mediaKey).toEqual(job.result.mediaKey); await release(); }); test('one run per job; a lock left by a dead run is taken over', async () => { const d = await tempDir(); const jobs = new PutJobs(d); const key = 'b'.repeat(32); const release = await jobs.claim(key); await expect(jobs.claim(key)).rejects.toBeInstanceOf(JobBusyError); await release(); await writeFile(join(d, key, 'lock'), '999999999'); // no such process const again = await jobs.claim(key); await again(); }); test('prune: jobs untouched for 30 days, and half-sealed leftovers after an hour', async () => { const d = await tempDir(); const jobs = new PutJobs(d); const old = 'c'.repeat(32); const fresh = 'd'.repeat(32); const half = 'e'.repeat(32); for (const k of [old, fresh]) { await (await jobs.claim(k))(); await jobs.save(k, '/f', { result: result(), readToken: null, meta: {} }); } await (await jobs.claim(half))(); const now = Date.now(); const daysAgo = (n: number) => new Date(now - n * 86400_000); await utimes(join(d, old, 'job.json'), daysAgo(31), daysAgo(31)); await utimes(join(d, half), daysAgo(0.1), daysAgo(0.1)); expect(await jobs.prune(now)).toBe(2); expect(await stat(join(d, fresh)).catch(() => null)).not.toBeNull(); expect(await stat(join(d, old)).catch(() => null)).toBeNull(); expect(await stat(join(d, half)).catch(() => null)).toBeNull(); }); });