Release 1.0.2: take a file picked again, refuse a dropped folder

Chromium fires no change event when the picker returns the file its input
already holds. The page kept the old one then, though the file may have
changed on disk, which makes the old one unreadable, or another file was
dropped in between, which the input never saw. The input is now emptied as
soon as its file is taken, so every pick fires. Its title names the file
taken, the browser's own tooltip would say no file is chosen.

A dropped folder came as a file of 0 B that can't be read and failed only
once the receiver downloaded it. It is refused with a note now, the file
picked before stays.

Both are covered for Chromium, whose protocol is the only one that reaches
the real picker and a drag from the file manager.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
SchluffeandClaude Fable 5.2 committed 2026-11-13 13:00:35 +01:00
1 parent 1b2696b6b4
commit 3c47047ddd
4 files changed
+105 -5

No files matched your search

+74 -1
View File
@@ -2,7 +2,7 @@ import { createCipheriv, createDecipheriv, createHash, hkdfSync, pbkdf2Sync, ran
import { createSocket, type Socket } from 'node:dgram';
import { once } from 'node:events';
import { createReadStream } from 'node:fs';
import { open as openFile, readFile, writeFile } from 'node:fs/promises';
import { mkdir, open as openFile, readFile, writeFile } from 'node:fs/promises';
import type { AddressInfo } from 'node:net';
import { networkInterfaces } from 'node:os';
import {
@@ -576,6 +576,79 @@ test('build: chromium sends to firefox from the production build under a path',
}
});
// The file picker and a drag from the file manager, the way the browser itself delivers them.
// setInputFiles fires a change event of its own whatever was picked, and no page can make up a
// dropped folder. Only Chromium's protocol reaches both, and it is the one that fires no change
// event for a file its input already holds.
async function fileManager(page: Page) {
const client = await page.context().newCDPSession(page);
const zone = page.locator('label:has(> [type=file])');
return {
zone,
async pick(path: string) {
const { result } = await client.send('Runtime.evaluate', {
expression: "document.querySelector('input[type=file]')",
});
await client.send('DOM.setFileInputFiles', { files: [path], objectId: result.objectId });
},
async drop(path: string) {
const box = (await zone.boundingBox())!;
const at = { x: box.x + box.width / 2, y: box.y + box.height / 2 };
const data = { items: [], files: [path], dragOperationsMask: 1 };
for (const type of ['dragEnter', 'dragOver', 'drop'] as const) {
await client.send('Input.dispatchDragEvent', { type, ...at, data });
}
},
};
}
// Chromium fires no change event when the picker returns the file its input holds. The page has to
// take it anyway: the file may have changed on disk, what the page held of it can't be read anymore
// then, or the user dropped another one in between, which the input never saw.
test('pick: chromium takes a file picked again, changed on disk or after a drop', async () => {
const sender = await open('chromium');
const { zone, pick, drop } = await fileManager(sender);
const source = await payload(randomBytes(1000), 'picked.bin');
await pick(source);
await expect(zone).toHaveText('picked.bin 1000 B');
await writeFile(source, randomBytes(1536));
await pick(source);
await expect(zone).toHaveText('picked.bin 1.5 KB');
await drop(await payload(randomBytes(2048), 'dropped.bin'));
await expect(zone).toHaveText('dropped.bin 2.0 KB');
// The input stays empty, its tooltip would say no file is chosen.
await expect(zone.locator('[type=file]')).toHaveAttribute('title', 'dropped.bin');
await pick(source);
await expect(zone).toHaveText('picked.bin 1.5 KB');
});
// A folder comes as a file that can't be read. Unnoticed, it would fail only once the receiver
// downloads it.
test('pick: chromium refuses a dropped folder and keeps the file it had', async () => {
const sender = await open('chromium');
const { zone, pick, drop } = await fileManager(sender);
const folder = test.info().outputPath('holiday');
await mkdir(folder);
const refused = sender.locator('#not-a-file');
await drop(folder);
await expect(refused).toHaveText('holiday is a folder, only a single file can be sent');
await expect(zone).toContainText('Drop a file or click to choose');
await expect(sender.locator('#share')).toBeDisabled();
// A file ends the note, whether picked or dropped.
await pick(await payload(randomBytes(1000), 'picked.bin'));
await expect(zone).toHaveText('picked.bin 1000 B');
await drop(folder);
await expect(refused).toBeVisible();
await expect(zone).toContainText('picked.bin 1000 B');
await expect(sender.locator('#share')).toBeEnabled();
await drop(await payload(randomBytes(2048), 'dropped.bin'));
await expect(zone).toHaveText('dropped.bin 2.0 KB');
});
for (const name of Object.keys(BROWSERS)) {
test(`servers: ${name} skips invalid lines, a reload brings back the defaults`, async () => {
const page = await open(name);