in apps/chat-e2e/src/ui/pages/basePage.ts [171:252]
async downloadMultipleData<T>(
method: () => Promise<T>,
expectedDownloadsCount: number,
filename?: string[] | string,
timeoutMs = 30000,
): Promise<UploadDownloadData[]> {
const downloadedData: UploadDownloadData[] = [];
const pendingDownloads = new Map<
string,
{ download: Download; completed: boolean }
>();
let downloadCount = 0;
const receivedDownloads = new Promise<void>((fulfill, reject) => {
const timeoutId = setTimeout(() => {
cleanup();
reject(
new Error(
`Timeout waiting for ${expectedDownloadsCount} downloads. Received ${downloadCount}`,
),
);
}, timeoutMs);
const handleDownload = async (download: Download) => {
try {
const filenamePath = filename
? typeof filename === 'string'
? filename
: filename[downloadCount]
: download.suggestedFilename();
const filePath = path.join(Import.exportPath, filenamePath);
pendingDownloads.set(filenamePath, { download, completed: false });
await download.saveAs(filePath);
const fileExists = await fs.promises
.access(filePath)
.then(() => true)
.catch(() => false);
if (!fileExists) {
throw new Error(`File ${filenamePath} failed to download`);
}
downloadCount++;
pendingDownloads.get(filenamePath)!.completed = true;
downloadedData.push({ path: filePath, dataType: 'download' });
if (downloadCount === expectedDownloadsCount) {
clearTimeout(timeoutId);
cleanup();
fulfill();
}
} catch (error) {
clearTimeout(timeoutId);
cleanup();
reject(error);
}
};
const cleanup = () => {
this.page.removeListener('download', handleDownload);
};
this.page.on('download', handleDownload);
});
try {
await new Promise((resolve) => setTimeout(resolve, 100));
await method();
await receivedDownloads;
return downloadedData;
} catch (error) {
await Promise.all(
downloadedData.map((data) =>
// eslint-disable-next-line @typescript-eslint/no-empty-function
fs.promises.unlink(data.path).catch(() => {}),
),
);
throw new Error(`Download failed:`);
}
}