Files
Shade/packages/shade-media/tests/library.test.ts
Sterister 01f8471fb5 feat(media): a local library of uploads — media ls with names, media link; canonical grant encoding
The relay never knows names and a link is printed once, so a lost link
made an owned film unreachable. media put now appends each upload and its
grant to $XDG_CONFIG_HOME/shade/media-library.jsonl (0600, one JSON line
per change: concurrent uploads cannot lose entries, a torn line costs only
itself). media ls names uploads from it; media link <name|id> prints the
link again; media rm forgets it.

encodeGrant now writes meta in canonical (validateMeta) order, so one
grant is always one link; media.json vectors regenerated (album and
subtitle vectors only), Kotlin 82/0.

Verified: 82 TS tests (library: fold, find, 0600, torn line, 200
concurrent appends, stable encoding); CLI put → ls → link gives back the
identical link.

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

72 lines
3.2 KiB
TypeScript

import { describe, test, expect, afterAll } from 'bun:test';
import { appendFile, mkdtemp, rm, stat } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { MediaLibrary } from '../src/library.js';
import { encodeGrant, parseGrant } from '../src/index.js';
import type { MediaGrant } from '../src/index.js';
const dirs: string[] = [];
afterAll(async () => {
for (const d of dirs) await rm(d, { recursive: true, force: true });
});
async function libPath(): Promise<string> {
const d = await mkdtemp(join(tmpdir(), 'shade-lib-'));
dirs.push(d);
return join(d, 'nested', 'media-library.jsonl');
}
const grant = (n: number, name: string): MediaGrant => ({
kind: 'shade.media-grant/v1',
mediaId: new Uint8Array(16).fill(n),
mediaKey: new Uint8Array(32).fill(n),
rootDigest: new Uint8Array(32).fill(n),
url: `https://relay.example/v1/media/${n}`,
readToken: new Uint8Array(32).fill(n + 1),
meta: { name, mime: 'video/mp4' },
});
describe('media library', () => {
test('records uploads with their grants, finds them by name or id, forgets removals', async () => {
const path = await libPath();
const lib = await MediaLibrary.open(path);
await lib.add('https://relay.example/', grant(1, 'Sesong 1 — Episode 2.mkv'), 100);
await lib.add('https://relay.example', grant(2, 'Ferie.jpg'), 50);
await lib.add('https://relay.example', grant(3, 'Episode 10.mkv'), 70);
await lib.remove('02'.repeat(16));
const again = await MediaLibrary.open(path);
expect(again.entries().map((e) => e.name).sort()).toEqual(['Episode 10.mkv', 'Sesong 1 — Episode 2.mkv']);
expect(again.find('episode').length).toBe(2);
expect(again.find('sesong 1')[0]!.grant).toEqual(grant(1, 'Sesong 1 — Episode 2.mkv'));
expect(again.find('0303')[0]!.name).toBe('Episode 10.mkv');
expect(again.get('01'.repeat(16))!.server).toBe('https://relay.example');
// Grants are keys: the file is the owner's alone.
expect((await stat(path)).mode & 0o777).toBe(0o600);
});
test('a torn line costs that entry only', async () => {
const path = await libPath();
const lib = await MediaLibrary.open(path);
await lib.add('https://r', grant(1, 'a'), 1);
await appendFile(path, '{"op":"add","id":"x","serv');
await appendFile(path, '\n');
await lib.add('https://r', grant(2, 'b'), 1);
expect((await MediaLibrary.open(path)).entries().map((e) => e.name).sort()).toEqual(['a', 'b']);
});
test('writers at the same time do not lose each other\'s entries', async () => {
const path = await libPath();
const libs = await Promise.all(Array.from({ length: 8 }, () => MediaLibrary.open(path)));
await Promise.all(libs.flatMap((l, i) => Array.from({ length: 25 }, (_, j) => l.add('https://r', grant(i * 25 + j, `f${i}-${j}`), 1))));
expect((await MediaLibrary.open(path)).entries().length).toBe(200);
});
test('a grant encodes to the same link after a round trip, whatever its meta key order', () => {
const a = grant(7, 'x.mkv');
const b: MediaGrant = { ...a, meta: { mime: 'video/mp4', name: 'x.mkv' } };
expect(encodeGrant(a)).toBe(encodeGrant(b));
expect(encodeGrant(parseGrant(encodeGrant(a)))).toBe(encodeGrant(a));
});
});