From f3f31b0e3b8b46d3f4e82b3d034c4ef8b009484b Mon Sep 17 00:00:00 2001 From: monsterlag <64719697+monsterlag@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:13:26 -0500 Subject: [PATCH] fix(core): read all batches of dropped directory entries (#11831) --- tabby-core/src/api/platform.ts | 12 +- tabby-electron/test/directoryUpload.test.ts | 121 ++++++++++++++++++++ 2 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 tabby-electron/test/directoryUpload.test.ts diff --git a/tabby-core/src/api/platform.ts b/tabby-core/src/api/platform.ts index 564faba1d..926a0c47e 100644 --- a/tabby-core/src/api/platform.ts +++ b/tabby-core/src/api/platform.ts @@ -179,12 +179,18 @@ export abstract class PlatformService { } else if (item.isDirectory) { const dirReader = item.createReader() const childrenFolder = new DirectoryUpload(item.name) - dirReader.readEntries(async (entries: any[]) => { + const readEntries = async (entries: any[]): Promise => { + if (!entries.length) { + resolve() + return + } for (const entry of entries) { await traverseFileTree(entry, childrenFolder) } - resolve() - }) + // Directory entries arrive in batches; only an empty batch marks the end. + dirReader.readEntries(readEntries) + } + dirReader.readEntries(readEntries) root.pushChildren(childrenFolder) } else { resolve() diff --git a/tabby-electron/test/directoryUpload.test.ts b/tabby-electron/test/directoryUpload.test.ts new file mode 100644 index 000000000..4fc6448d9 --- /dev/null +++ b/tabby-electron/test/directoryUpload.test.ts @@ -0,0 +1,121 @@ +import assert from 'node:assert/strict' +import { describe, it } from 'node:test' +import { DirectoryUpload, FileTransfer, FileUpload, PlatformService } from '../../tabby-core/src/api/platform.ts' + +function fileEntry (name: string) { + return { + isFile: true, + file (callback: (file: File) => void) { + queueMicrotask(() => callback(new File([name], name))) + }, + } +} + +function files (count: number) { + return Array.from({ length: count }, (_, i) => fileEntry(`file-${i}.txt`)) +} + +function directoryEntry (name: string, entries: any[], batchSize = 100) { + return { + name, + isDirectory: true, + readerCount: 0, + readCount: 0, + createReader () { + this.readerCount++ + let offset = 0 + return { + readEntries: (callback: (batch: any[]) => void) => { + this.readCount++ + const batch = entries.slice(offset, offset + batchSize) + offset += batch.length + queueMicrotask(() => callback(batch)) + }, + } + }, + } +} + +async function drop (entries: any[], multiple?: boolean) { + // Only the concrete drag-and-drop method is used, not the platform-specific abstract methods. + const platform: PlatformService = new (PlatformService as any)() + const transfers: FileTransfer[] = [] + platform.fileTransferStarted$.subscribe(transfer => transfers.push(transfer)) + const event = { + dataTransfer: { items: entries.map(entry => ({ webkitGetAsEntry: () => entry })) }, + } as unknown as DragEvent + const result = await platform.startUploadFromDragEvent(event, multiple) + return { result, transfers } +} + +function children (parent: DirectoryUpload, index = 0): (FileUpload|DirectoryUpload)[] { + const directory = parent.getChildrens()[index] + assert.ok(directory instanceof DirectoryUpload) + return directory.getChildrens() +} + +describe('PlatformService.startUploadFromDragEvent()', () => { + it('reads every batch of a dropped directory using the same reader', async () => { + const directory = directoryEntry('dist', files(237)) + const { result, transfers } = await drop([directory]) + const uploads = children(result) + + assert.equal(uploads.length, 237) + assert.deepEqual(uploads.map(upload => upload.getName()), Array.from({ length: 237 }, (_, i) => `file-${i}.txt`)) + assert.deepEqual(transfers, uploads) + assert.equal(directory.readerCount, 1) + assert.equal(directory.readCount, 4) // 100, 100, 37, then the terminal empty batch + assert.equal(result.getChildrens()[0].getName(), 'dist') + assert.equal(Buffer.from(await (uploads[236] as FileUpload).readAll()).toString(), 'file-236.txt') + }) + + for (const count of [0, 3, 100]) { + it(`reads a directory containing ${count} files until an empty batch`, async () => { + const directory = directoryEntry('dist', files(count)) + const { result, transfers } = await drop([directory]) + + assert.equal(children(result).length, count) + assert.deepEqual(transfers, children(result)) + assert.equal(directory.readerCount, 1) + assert.equal(directory.readCount, count ? 2 : 1) + }) + } + + it('preserves nested directories and reads their later batches before completing', async () => { + const nested = directoryEntry('assets', files(101)) + const empty = directoryEntry('empty', []) + const directory = directoryEntry('dist', [fileEntry('index.html'), nested, empty], 1) + const { result, transfers } = await drop([directory]) + const uploads = children(result) + + assert.deepEqual(uploads.map(upload => upload.getName()), ['index.html', 'assets', 'empty']) + const subdirectories = result.getChildrens()[0] as DirectoryUpload + assert.equal(children(subdirectories, 1).length, 101) + assert.equal(children(subdirectories, 2).length, 0) + assert.deepEqual(transfers, [uploads[0], ...children(subdirectories, 1)]) + assert.equal(directory.readCount, 4) + assert.equal(nested.readCount, 3) + assert.equal(empty.readCount, 1) + }) + + it('includes all dropped roots when multiple is enabled', async () => { + const { result, transfers } = await drop([ + directoryEntry('first', files(101)), directoryEntry('second', files(2)), fileEntry('loose.txt'), + ], true) + + assert.deepEqual(result.getChildrens().map(upload => upload.getName()), ['first', 'second', 'loose.txt']) + assert.equal(children(result).length, 101) + assert.equal(children(result, 1).length, 2) + assert.equal(transfers.length, 104) + }) + + it('only includes the first valid root by default', async () => { + const { result, transfers } = await drop([ + null, directoryEntry('first', files(101)), directoryEntry('second', files(2)), + ]) + + assert.deepEqual(result.getChildrens().map(upload => upload.getName()), ['first']) + assert.equal(children(result).length, 101) + assert.deepEqual(transfers, children(result)) + }) +})