Files
Shade/packages/shade-sdk/tests/thumbnail.test.ts
Sterister 53ce776c02 test: stop guessing port numbers
Twenty test servers picked a port with `NNNN + Math.floor(Math.random() * 500)`
and hoped it was free. Several files guessed inside the *same* range —
sdk.test.ts and gates.test.ts both on 19500, three files on 22000, and
webrtc-integration.test.ts used 22000 twice within itself — so with test files
serving in parallel the collision was not unlikely, it was scheduled.

Found from the outside, in Nova, where Shade is vendored: across eight runs of
an unchanged tree, two had failures and six were clean. The visible error was

    Failed to start server. Is port 24287 in use?  EADDRINUSE

and then a second, louder one: `teardownRig` ran on a rig that setup had never
finished building and died on `rig.alice`, so the TypeError from cleanup is
what you read first and the real cause scrolled past above it.

`Bun.serve({ port: 0 })` lets the OS hand out a free port and `server.port`
reads it back. There is no range to collide inside.

teardownRig now tolerates a rig that was never built. Cleanup must never be the
loudest thing in a failing test.

A suite that fails a quarter of the time is worse than a suite that fails: it
teaches everyone to re-run it, and then a real regression looks like the flake.

Verified: 1198 pass / 0 fail here, and the same fix is going into Nova's
vendored copy so the next sync does not bring the guessing back.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014489bKUtUEY1Zgs9xN9mt7
2026-09-08 16:33:40 +02:00

286 lines
9.4 KiB
TypeScript

import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
import {
createShade,
ShadeThumbnailCache,
THUMBNAIL_MAX_BYTES,
type IncomingTransfer,
type Shade,
type TransferHandle,
type TransferResult,
} from '../src/index.js';
import {
createPrekeyServer,
MemoryPrekeyStore,
PrekeyServerEvents,
} from '@shade/server';
import { SubtleCryptoProvider } from '@shade/crypto-web';
import { sha256Once } from '@shade/streams';
const crypto = new SubtleCryptoProvider();
interface TestRig {
alice: Shade;
bob: Shade;
prekeyStop: () => void;
bobServerStop: () => void;
}
async function startPrekeyServer(): Promise<{ url: string; stop: () => void }> {
const events = new PrekeyServerEvents();
const server = createPrekeyServer({
crypto,
store: new MemoryPrekeyStore(),
disableRateLimit: true,
events,
});
// Port 0 lets the OS hand out a free one. This guessed inside a 500-wide
// range and hoped — and several files here guessed inside the SAME range,
// so with test files serving in parallel the collision arrived about a
// quarter of the time as EADDRINUSE.
const handle = Bun.serve({ port: 0, fetch: server.fetch });
const port = handle.port;
return { url: `http://localhost:${port}`, stop: () => handle.stop() };
}
async function setupRig(): Promise<TestRig> {
const prekey = await startPrekeyServer();
const alice = await createShade({ prekeyServer: prekey.url, address: 'alice' });
const bob = await createShade({ prekeyServer: prekey.url, address: 'bob' });
bob.configureTransfers({
resolveBaseUrl: async () => {
throw new Error('bob is receive-only');
},
});
const bobApp = await bob.transferRoute();
// Port 0 lets the OS hand out a free one. This guessed inside a 500-wide
// range and hoped — and several files here guessed inside the SAME range,
// so with test files serving in parallel the collision arrived about a
// quarter of the time as EADDRINUSE.
const bobServer = Bun.serve({ port: 0, fetch: bobApp.fetch });
const port = bobServer.port;
const bobBaseUrl = `http://localhost:${port}`;
alice.configureTransfers({
resolveBaseUrl: async (addr) => {
if (addr === 'bob') return bobBaseUrl;
throw new Error(`unknown peer ${addr}`);
},
});
return {
alice,
bob,
prekeyStop: prekey.stop,
bobServerStop: () => bobServer.stop(),
};
}
async function teardownRig(rig: TestRig): Promise<void> {
await rig.alice.shutdown();
await rig.bob.shutdown();
rig.bobServerStop();
rig.prekeyStop();
}
function fakeJpeg(size: number): Uint8Array {
// Synthetic "JPEG-shaped" bytes: SOI marker + filler. The SDK only
// hashes + ships these; the receiver-side validator we exercise is
// MIME + size, not actual decode-ability. The widget renderer feeds
// these to a real `<img>`; that's where format-correctness matters.
const buf = new Uint8Array(size);
buf[0] = 0xff;
buf[1] = 0xd8;
buf[2] = 0xff;
for (let i = 3; i < size; i++) buf[i] = i & 0xff;
return buf;
}
describe('V3.9 thumbnail roundtrip', () => {
let rig: TestRig;
beforeAll(async () => {
rig = await setupRig();
});
afterAll(async () => {
await teardownRig(rig);
});
test('upload with thumbnail attaches fileMetadata + ships separate stream', async () => {
const main = crypto.randomBytes(64 * 1024);
const thumb = fakeJpeg(4096);
const expectedHashB64 = bytesToBase64(sha256Once(thumb));
const incomings: IncomingTransfer[] = [];
const recvHandles: TransferHandle[] = [];
const unsub = await rig.bob.onIncomingTransfer(async (incoming) => {
incomings.push(incoming);
const handle = await incoming.accept({ output: { kind: 'buffer' } });
recvHandles.push(handle);
});
const mainHandle = await rig.alice.upload({
to: 'bob',
input: main,
thumbnail: { bytes: thumb, mime: 'image/jpeg' },
lanes: 1,
chunkSize: 16 * 1024,
metadata: {
fileMetadata: {
filename: 'doc.pdf',
mimeType: 'application/pdf',
},
},
});
await mainHandle.done();
// Wait for any background thumbnail finish before tearing down.
for (const h of recvHandles) await h.done();
unsub();
// Two transfers: thumb then main (thumb is shipped first).
expect(incomings.length).toBe(2);
const thumbIncoming = incomings.find(
(i) => i.metadata.userMetadata?.shadeThumbnail === '1',
);
const mainIncoming = incomings.find(
(i) => i.metadata.userMetadata?.shadeThumbnail !== '1',
);
expect(thumbIncoming).toBeDefined();
expect(mainIncoming).toBeDefined();
expect(thumbIncoming!.metadata.contentType).toBe('image/jpeg');
expect(mainIncoming!.metadata.fileMetadata?.thumbnailHash).toBe(expectedHashB64);
expect(mainIncoming!.metadata.fileMetadata?.thumbnailMime).toBe('image/jpeg');
expect(mainIncoming!.metadata.fileMetadata?.thumbnailBytes).toBe(thumb.byteLength);
expect(mainIncoming!.metadata.fileMetadata?.thumbnailStreamId).toBe(
thumbIncoming!.streamId,
);
expect(mainIncoming!.metadata.fileMetadata?.filename).toBe('doc.pdf');
expect(mainIncoming!.metadata.fileMetadata?.mimeType).toBe('application/pdf');
});
test('legacy receiver (no thumbnail handling) still receives main stream', async () => {
const main = crypto.randomBytes(8 * 1024);
const thumb = fakeJpeg(2048);
let resolveMain!: (h: TransferHandle) => void;
const mainHandlePromise = new Promise<TransferHandle>((r) => {
resolveMain = r;
});
const allHandles: TransferHandle[] = [];
const unsub = await rig.bob.onIncomingTransfer(async (incoming) => {
// "Legacy" path: ignore fileMetadata entirely. Just accept everything.
const handle = await incoming.accept({ output: { kind: 'buffer' } });
allHandles.push(handle);
// Resolve as soon as we see the main (non-thumb) stream.
if (incoming.metadata.userMetadata?.shadeThumbnail !== '1') {
resolveMain(handle);
}
});
const senderHandle = await rig.alice.upload({
to: 'bob',
input: main,
thumbnail: { bytes: thumb, mime: 'image/jpeg' },
lanes: 1,
chunkSize: 4 * 1024,
});
const mainHandle = await mainHandlePromise;
const [senderResult, mainResult] = await Promise.all([
senderHandle.done(),
mainHandle.done(),
]);
for (const h of allHandles) await h.done();
unsub();
expect(
(mainResult as TransferResult & { bytes?: Uint8Array }).bytes,
).toEqual(main);
expect(senderResult.bytesSent).toBe(main.byteLength);
});
test('throws on oversize thumbnail bytes', async () => {
const main = crypto.randomBytes(1024);
const tooLarge = fakeJpeg(THUMBNAIL_MAX_BYTES + 1);
await expect(
rig.alice.upload({
to: 'bob',
input: main,
thumbnail: { bytes: tooLarge, mime: 'image/jpeg' },
}),
).rejects.toThrow();
});
test('throws on disallowed thumbnail mime', async () => {
const main = crypto.randomBytes(1024);
await expect(
rig.alice.upload({
to: 'bob',
input: main,
// @ts-expect-error — testing runtime guard
thumbnail: { bytes: fakeJpeg(1024), mime: 'image/svg+xml' },
}),
).rejects.toThrow();
});
});
describe('V3.9 ShadeThumbnailCache', () => {
test('rejects oversize bytes', () => {
const cache = new ShadeThumbnailCache();
const oversize = new Uint8Array(THUMBNAIL_MAX_BYTES + 1);
expect(cache.put('s1', oversize, 'image/jpeg')).toBe(false);
expect(cache.size).toBe(0);
});
test('rejects disallowed mime', () => {
const cache = new ShadeThumbnailCache();
const tiny = new Uint8Array(8);
expect(cache.put('s1', tiny, 'image/svg+xml')).toBe(false);
expect(cache.size).toBe(0);
});
test('drops bytes whose hash does not match expectedHash', () => {
const cache = new ShadeThumbnailCache();
const bytes = new Uint8Array([1, 2, 3, 4]);
const wrongHash = bytesToBase64(new Uint8Array(32)); // all zero
cache.setExpectedHash('s1', wrongHash);
expect(cache.put('s1', bytes, 'image/jpeg')).toBe(false);
expect(cache.get('s1')).toBeNull();
});
test('round-trips when hash matches', () => {
const cache = new ShadeThumbnailCache();
const bytes = new Uint8Array([0xff, 0xd8, 0xff, 0x42]);
const hash = bytesToBase64(sha256Once(bytes));
cache.setExpectedHash('s1', hash);
expect(cache.put('s1', bytes, 'image/jpeg')).toBe(true);
const hit = cache.get('s1', hash);
expect(hit).not.toBeNull();
expect(hit!.bytes).toEqual(bytes);
expect(hit!.mime).toBe('image/jpeg');
});
test('get drops entry on hash mismatch', () => {
const cache = new ShadeThumbnailCache();
const bytes = new Uint8Array([1, 2, 3]);
cache.put('s1', bytes, 'image/png');
const wrongHash = bytesToBase64(new Uint8Array(32));
expect(cache.get('s1', wrongHash)).toBeNull();
expect(cache.size).toBe(0);
});
test('emits onChange when an entry is added', () => {
const cache = new ShadeThumbnailCache();
const seen: string[] = [];
cache.onChange((s) => seen.push(s));
cache.put('s1', new Uint8Array([1]), 'image/jpeg');
cache.put('s2', new Uint8Array([2]), 'image/jpeg');
expect(seen).toEqual(['s1', 's2']);
});
});
function bytesToBase64(bytes: Uint8Array): string {
let bin = '';
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]!);
return btoa(bin);
}