Files
Shade/packages/shade-media/tests/put-jobs.test.ts
Sterister 20c5ec07ab feat(media): media put resumes across runs, and a file already up gives its link again
A run already survives a dropped connection by retrying. This covers what one
run cannot survive: Ctrl-C, a laptop that sleeps, a relay that is gone for
longer than the retries wait.

Each file being put has a job under $XDG_CONFIG_HOME/shade/media-pending/,
keyed by server, path, size, mtime and options. The job holds:
- the sealed copy;
- job.json (0600: the seal result with the file key, the read token, and the
  meta including subtitle grants);
- a lock with the pid of the run working on it. A lock left by a dead run is
  taken over.

Running the same command again resumes with the same media id, so the relay
already has most of the file. Once the relay has stored it, the copy goes and
the grant stays. Putting the file again gives the same link while the relay
still serves it, so a season's put run again after it stopped re-sends only
what is missing. Jobs untouched for 30 days are removed.

Verified with real processes, a 300 MiB file and a file-store relay:
- Run 1 was SIGKILLed after 8 of 19 pieces.
- Run 2 resumed at 42% and finished.
- Run 3 printed "already on the relay, same link".
- `media get` returned the file byte-identical, and the library has one item.

tests/put-jobs.test.ts (4). media 118, cli 15. All 27 packages type-check.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-18 23:45:56 +02:00

99 lines
4.1 KiB
TypeScript

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<string> {
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();
});
});