import { describe, test, expect, afterEach } from 'bun:test'; import { Hono } from 'hono'; import { mkdtemp, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { createShade, type Shade } from '../src/index.js'; import { createPrekeyServer, MemoryPrekeyStore } from '@shade/server'; import { SubtleCryptoProvider } from '@shade/crypto-web'; import { MediaClient, OwnerKey, openGrant } from '@shade/media'; import type { Grant } from '@shade/media'; import { createMediaRoutes, FileMediaStore } from '@shade/media/server'; const crypto = new SubtleCryptoProvider(); /** * A film shared over a Shade session: Alice seals and uploads it, sends the * grant through the Double Ratchet, Bob plays it from the relay — which * never saw a key. */ describe('media grants over a Shade session', () => { const stops: Array<() => void | Promise> = []; afterEach(async () => { for (const s of stops.splice(0).reverse()) await s(); }); async function setup(): Promise<{ alice: Shade; bob: Shade; media: string }> { const prekeys = Bun.serve({ port: 0, fetch: createPrekeyServer({ crypto, store: new MemoryPrekeyStore(), disableRateLimit: true }).fetch }); stops.push(() => prekeys.stop(true)); const dir = await mkdtemp(join(tmpdir(), 'sdk-media-')); stops.push(() => rm(dir, { recursive: true, force: true })); const app = new Hono(); app.route('/', createMediaRoutes(new FileMediaStore(dir))); const media = Bun.serve({ port: 0, fetch: app.fetch }); stops.push(() => media.stop(true)); const alice = await createShade({ prekeyServer: `http://localhost:${prekeys.port}`, address: 'alice' }); const bob = await createShade({ prekeyServer: `http://localhost:${prekeys.port}`, address: 'bob' }); stops.push(() => alice.shutdown(), () => bob.shutdown()); return { alice, bob, media: `http://localhost:${media.port}` }; } test('shareMedia → onMediaGrant → play', async () => { const { alice, bob, media } = await setup(); const film = new Uint8Array(3 * 1024 * 1024 + 11).map((_, i) => (i * 7) & 0xff); const grant = await new MediaClient(media, OwnerKey.generate()).uploadBytes(film, { meta: { mime: 'video/mp4', name: 'Ferie.mp4' }, }); const got: Array<{ from: string; grant: Grant }> = []; const texts: string[] = []; bob.onMediaGrant((from, g) => void got.push({ from, grant: g })); bob.onMessage((_from, text) => void texts.push(text)); const env = await alice.shareMedia('bob', grant); expect(await bob.receive('alice', env)).toBe(''); expect(texts).toEqual([]); expect(got.length).toBe(1); expect(got[0]!.from).toBe('alice'); const g = got[0]!.grant; if (g.kind !== 'shade.media-grant/v1') throw new Error('expected a media grant'); const reader = await openGrant(g); expect(reader.meta.name).toBe('Ferie.mp4'); expect(await reader.read(2 * 1024 * 1024, 1024)).toEqual(film.subarray(2 * 1024 * 1024, 2 * 1024 * 1024 + 1024)); // Ordinary messages still reach onMessage. await bob.receive('alice', await alice.send('bob', 'hei')); expect(texts).toEqual(['hei']); }); test('with no grant handler, a grant arrives as plain text (unchanged behaviour)', async () => { const { alice, bob, media } = await setup(); const grant = await new MediaClient(media, OwnerKey.generate()).uploadBytes(new Uint8Array(10)); const texts: string[] = []; bob.onMessage((_f, t) => void texts.push(t)); await bob.receive('alice', await alice.shareMedia('bob', grant)); expect(texts.length).toBe(1); expect(texts[0]).toContain('shade.media-grant/v1'); }); });