ente/web/apps/photos/tests/zip-file-reading.test.ts

112 lines
4 KiB
TypeScript
Raw Normal View History

2024-04-08 15:15:29 +00:00
import { getFileNameSize } from "@/next/file";
import { FILE_READER_CHUNK_SIZE, PICKED_UPLOAD_TYPE } from "constants/upload";
import { getElectronFileStream, getFileStream } from "services/readerService";
import { DataStream } from "types/upload";
import { getImportSuggestion } from "utils/upload";
2022-12-20 14:13:09 +00:00
// This was for used to verify that converting from the browser readable stream
// to the node readable stream correctly handles files that align on the 4 MB
// data boundary. This expects a zip file containing random files of various
// sizes starting from 1M to 20M.
2022-12-20 14:13:09 +00:00
export const testZipFileReading = async () => {
try {
2024-04-09 06:42:25 +00:00
const electron = globalThis.electron;
if (!electron) {
console.log("testZipFileReading Check is for desktop only");
2022-12-20 15:01:50 +00:00
return;
}
if (!process.env.NEXT_PUBLIC_FILE_READING_TEST_ZIP_PATH) {
throw Error(
"upload test failed NEXT_PUBLIC_FILE_READING_TEST_ZIP_PATH missing",
2022-12-20 15:01:50 +00:00
);
}
2024-04-09 06:42:25 +00:00
const files = await electron.getElectronFilesFromGoogleZip(
process.env.NEXT_PUBLIC_FILE_READING_TEST_ZIP_PATH,
2022-12-20 15:01:50 +00:00
);
2022-12-20 14:13:09 +00:00
if (!files?.length) {
throw Error(
`testZipFileReading Check failed ❌
No files selected`,
2022-12-20 14:13:09 +00:00
);
}
console.log("test zip file reading check started");
2022-12-20 14:13:09 +00:00
let i = 0;
for (const file of files) {
i++;
let filedata: DataStream;
if (file instanceof File) {
filedata = getFileStream(file, FILE_READER_CHUNK_SIZE);
} else {
filedata = await getElectronFileStream(
file,
FILE_READER_CHUNK_SIZE,
2022-12-20 14:13:09 +00:00
);
}
const streamReader = filedata.stream.getReader();
for (let i = 0; i < filedata.chunkCount; i++) {
const { done } = await streamReader.read();
if (done) {
throw Error(
`testZipFileReading Check failed ❌
${getFileNameSize(
file,
2022-12-20 14:13:09 +00:00
)} less than expected chunks, expected: ${
filedata.chunkCount
}, got ${i - 1}`,
2022-12-20 14:13:09 +00:00
);
}
}
const { done } = await streamReader.read();
if (!done) {
throw Error(
`testZipFileReading Check failed ❌
${getFileNameSize(
file,
2022-12-20 14:13:09 +00:00
)} more than expected chunks, expected: ${
2024-02-22 06:06:17 +00:00
filedata.chunkCount
}`,
2022-12-20 14:13:09 +00:00
);
}
console.log(`${i}/${files.length} passed ✅`);
}
console.log("test zip file reading check passed ✅");
2022-12-20 14:13:09 +00:00
} catch (e) {
console.log(e);
}
};
2022-12-20 15:01:50 +00:00
// This was used when fixing a bug around handling a zip file that has a photo
// at the root.
2022-12-20 15:01:50 +00:00
export const testZipWithRootFileReadingTest = async () => {
try {
2024-04-09 06:42:25 +00:00
const electron = globalThis.electron;
if (!electron) {
console.log("testZipFileReading Check is for desktop only");
2022-12-20 15:01:50 +00:00
return;
}
if (!process.env.NEXT_PUBLIC_ZIP_WITH_ROOT_FILE_PATH) {
throw Error(
"upload test failed NEXT_PUBLIC_ZIP_WITH_ROOT_FILE_PATH missing",
2022-12-20 15:01:50 +00:00
);
}
2024-04-09 06:42:25 +00:00
const files = await electron.getElectronFilesFromGoogleZip(
process.env.NEXT_PUBLIC_ZIP_WITH_ROOT_FILE_PATH,
2022-12-20 15:01:50 +00:00
);
const importSuggestion = getImportSuggestion(
PICKED_UPLOAD_TYPE.ZIPS,
files,
2022-12-20 15:01:50 +00:00
);
if (!importSuggestion.rootFolderName) {
throw Error(
`testZipWithRootFileReadingTest Check failed ❌
rootFolderName is missing`,
2022-12-20 15:01:50 +00:00
);
}
console.log("testZipWithRootFileReadingTest passed ✅");
2022-12-20 15:01:50 +00:00
} catch (e) {
console.log(e);
}
};