Ask before downloading and show transfer progress

Tracker codes now use 8 of [0-9a-zA-Z] instead of 8 digits. After both
security checks the receiver sees the offered file and starts the download
itself, the sender waits for it. Both sides show a progress bar of what the
download manager has read. Copy buttons for the tracker code, offer and
answer; the manual exchange is folded away below the tracker on both sides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SchluffeandClaude Opus 5.5 committed 2026-10-24 14:15:48 +02:00
1 parent 85ccdbfc03
commit bacc15baaf
4 files changed
+321 -72

No files matched your search

+38 -5
View File
@@ -6,6 +6,8 @@ import { createServer, type ViteDevServer } from 'vite';
// Runs against the real public trackers and STUN servers, the defaults in src/main.ts.
const BROWSERS: Record<string, BrowserType> = { chromium, firefox };
// Chromium needs a permission for the copy buttons, Firefox lets the click do it and rejects the name.
const PERMISSIONS: Record<string, string[]> = { chromium: ['clipboard-write'], firefox: [] };
const SIZE = 5_000_000;
// Opt-in with LARGE=1: beyond what fits comfortably in RAM and beyond the 2000 MiB the old OPFS
// staging could store in incognito-like contexts.
@@ -18,8 +20,12 @@ const WINDOW = 8 * 2 ** 20;
// Each mode connects a sender page that already picked a file to a receiver page.
const MODES: Record<string, (sender: Page, receiver: Page) => Promise<void>> = {
async manual(sender, receiver) {
await sender.click('#send-manual summary');
await sender.click('#create-offer');
await expect(sender.locator('#offer')).toHaveValue(/.+/, { timeout: 15_000 });
await sender.click('#copy-offer');
await expect(sender.locator('#copy-offer')).toHaveText('Copied');
await receiver.click('#receive-manual summary');
await receiver.fill('#offer-input', await sender.inputValue('#offer'));
await receiver.click('#create-answer');
await expect(receiver.locator('#answer')).toHaveValue(/.+/, { timeout: 15_000 });
@@ -48,7 +54,9 @@ const MODES: Record<string, (sender: Page, receiver: Page) => Promise<void>> = {
},
async tracker(sender, receiver) {
await sender.click('#share');
await expect(sender.locator('#send-code')).toHaveText(/^\d{8}$/);
await expect(sender.locator('#send-code')).toHaveText(/^[0-9a-zA-Z]{8}$/);
await sender.click('#copy-send-code');
await expect(sender.locator('#copy-send-code')).toHaveText('Copied');
await expect(sender.locator('#log')).toContainText('waiting on');
await receiver.fill('#receive-code', (await sender.locator('#send-code').textContent())!);
await receiver.click('#receive');
@@ -93,7 +101,7 @@ test.afterAll(async () => {
async function open(name: string, sockets: string[], init?: () => void) {
const browser = await BROWSERS[name]!.launch();
browsers.push(browser);
const page = await (await browser.newContext()).newPage();
const page = await (await browser.newContext({ permissions: PERMISSIONS[name] })).newPage();
page.on('websocket', (ws) => sockets.push(ws.url()));
if (init) await page.addInitScript(init);
await page.goto(url);
@@ -202,14 +210,14 @@ async function connect(mode: string, sender: Page, receiver: Page, source: strin
}
}
// Resolves with the download, which starts on its own as soon as the file is announced, and the
// receiver log at that moment.
// Resolves with the download, which starts once the receiver clicked Download, and the receiver log
// at that moment.
const downloadStarts = (receiver: Page) =>
receiver
.waitForEvent('download', { timeout: test.info().timeout })
.then(async (download) => ({ download, logAtStart: await log(receiver) }));
// Connects and confirms the security check on both sides.
// Connects, confirms the security check on both sides and starts the download.
async function start(mode: string, sender: Page, receiver: Page, source: string) {
await connect(mode, sender, receiver, source);
const started = downloadStarts(receiver);
@@ -217,6 +225,11 @@ async function start(mode: string, sender: Page, receiver: Page, source: string)
await expect(sender.locator('#log')).toContainText("waiting for the receiver's security check");
await expect(receiver.locator('#log')).not.toContainText('receiving');
await receiver.click('#confirm');
// Offered, but nothing is sent before the user asks for it.
await expect(receiver.locator('#incoming')).toContainText(source.split('/').pop()!);
await expect(sender.locator('#log')).toContainText('waiting for the receiver to download');
await expect(receiver.locator('#log')).not.toContainText('receiving');
await receiver.click('#download');
return started;
}
@@ -239,6 +252,8 @@ async function transfer(
expect(await hash(await download.path())).toBe(await hash(source));
await expect(receiver.locator('#log')).toContainText('download complete');
await expect(sender.locator('#log')).toContainText('delivered');
await expect(sender.locator('#send-progress')).toHaveJSProperty('value', 100);
await expect(receiver.locator('#receive-progress')).toHaveJSProperty('value', 100);
// Removed hidden pages release the service worker for updates.
await expect(receiver.locator('iframe')).toHaveCount(0);
});
@@ -352,6 +367,20 @@ for (const name of Object.keys(BROWSERS)) {
});
});
test(`progress: ${name} follows the shared file, not a later pick`, async () => {
const [sender, receiver] = await Promise.all([open(name, []), open(name, [])]);
const source = test.info().outputPath('payload.bin');
await writeFile(source, randomBytes(SIZE));
await withLogs(sender!, receiver!, async () => {
await start('offline', sender!, receiver!, source);
await expect(sender!.locator('#log')).toContainText('delivered');
const other = { name: 'other.bin', mimeType: 'application/octet-stream', buffer: Buffer.alloc(2 * SIZE) };
await sender!.setInputFiles('input[type=file]', other);
await expect(sender!.locator('#send-progress')).toHaveJSProperty('value', 100);
});
});
test(`check: ${name} accepts nothing before the receiver confirmed`, async () => {
const [sender, receiver] = await Promise.all([open(name, [], recordChannels), open(name, [])]);
const source = test.info().outputPath('payload.bin');
@@ -377,6 +406,7 @@ for (const name of Object.keys(BROWSERS)) {
const started = downloadStarts(receiver!);
await receiver!.click('#confirm');
await receiver!.click('#download');
const { download } = await started;
expect(download.suggestedFilename()).toBe('payload.bin');
expect(await hash(await download.path())).toBe(await hash(source));
@@ -406,6 +436,7 @@ for (const name of Object.keys(BROWSERS)) {
const started = downloadStarts(receiver!);
await sender!.click('#confirm');
await receiver!.click('#confirm');
await receiver!.click('#download');
const { download } = await started;
expect(await hash(await download.path())).toBe(await hash(source));
await expect(sender!.locator('#log')).toContainText('delivered');
@@ -434,6 +465,7 @@ for (const name of Object.keys(BROWSERS)) {
const started = downloadStarts(receiver!);
await sender!.click('#confirm');
await receiver!.click('#confirm');
await receiver!.click('#download');
const { download } = await started;
expect(await hash(await download.path())).toBe(await hash(source));
await expect(sender!.locator('#log')).toContainText('delivered');
@@ -491,6 +523,7 @@ for (const name of Object.keys(BROWSERS)) {
['sender', 'receiver'].map((profile) =>
BROWSERS[name]!.launchPersistentContext(test.info().outputPath(profile), {
downloadsPath: test.info().outputPath('downloads'),
permissions: PERMISSIONS[name],
}),
),
);