Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Convert upload checks to Vitest

Manav Rathi72ccdffaeb44
diff --git a/web/apps/photos/package.json b/web/apps/photos/package.json
index 05fbf89680..49c1d71956 100644
--- a/web/apps/photos/package.json
+++ b/web/apps/photos/package.json
@@ -2,6 +2,9 @@
"name": "photos",
"version": "0.0.0",
"private": true,
+ "scripts": {
+ "test": "vitest --run"
+ },
"dependencies": {
"@hugeicons/core-free-icons": "1.2.1",
"@hugeicons/react": "1.1.6",
@@ -33,6 +36,7 @@
"@types/react": "19.2.16",
"@types/react-dom": "19.2.3",
"@types/react-window": "1.8.8",
- "ente-build-config": "*"
+ "ente-build-config": "*",
+ "vitest": "4.1.7"
}
}
diff --git a/web/apps/photos/src/components/Sidebar.tsx b/web/apps/photos/src/components/Sidebar.tsx
index 90d90c0d3d..ab1d6c4e54 100644
--- a/web/apps/photos/src/components/Sidebar.tsx
+++ b/web/apps/photos/src/components/Sidebar.tsx
@@ -97,7 +97,6 @@ import { isMLSupported } from "ente-new/photos/services/ml";
import { performSidebarAction as performSidebarRegistryAction } from "ente-new/photos/services/search/sidebar-search-registry";
import type { SidebarActionID } from "ente-new/photos/services/search/types";
import {
- isDevBuildAndUser,
pullSettings,
updateCFProxyDisabledPreference,
updateCustomDomain,
@@ -136,7 +135,6 @@ import React, {
type MouseEventHandler,
} from "react";
import { Trans } from "react-i18next";
-import { testUpload } from "../../tests/upload.test";
import { SubscriptionCard } from "./SubscriptionCard";
type SidebarProps = ModalVisibilityProps & {
@@ -233,7 +231,6 @@ type HelpAction = Extract<
| "help.requestFeature"
| "help.support"
| "help.viewLogs"
- | "help.testUpload"
>;
type FreeUpSpaceAction = Extract<
@@ -1827,11 +1824,6 @@ const Help: React.FC<HelpProps> = ({
case "help.viewLogs":
confirmViewLogs();
break;
- case "help.testUpload":
- if (isDevBuildAndUser()) {
- void testUpload();
- }
- break;
}
onActionHandled?.();
}, [
@@ -1892,17 +1884,6 @@ const Help: React.FC<HelpProps> = ({
onClick={confirmViewLogs}
/>
</RowButtonGroup>
- {isDevBuildAndUser() && (
- <RowButton
- variant="secondary"
- label={
- <Typography variant="mini" color="text.muted">
- {ut("Test upload")}
- </Typography>
- }
- onClick={testUpload}
- />
- )}
</Stack>
</TitledNestedSidebarDrawer>
);
diff --git a/web/apps/photos/tests/upload.test.ts b/web/apps/photos/tests/upload.test.ts
index 8d29e91cb1..81e1f2d3bf 100644
--- a/web/apps/photos/tests/upload.test.ts
+++ b/web/apps/photos/tests/upload.test.ts
@@ -1,7 +1,3 @@
-// TODO: Audit this file
-/* eslint-disable @typescript-eslint/ban-ts-comment */
-/* eslint-disable @typescript-eslint/restrict-template-expressions */
-// @ts-nocheck
import {
parseDateFromDigitGroups,
tryParseEpochMicrosecondsFromFileName,
@@ -10,210 +6,120 @@ import {
matchJSONMetadata,
metadataJSONMapKeyForJSON,
} from "ente-gallery/services/upload/metadata-json";
+import { describe, expect, test } from "vitest";
-const DATE_TIME_PARSING_TEST_FILE_NAMES = [
- {
- fileName: "Screenshot_20220807-195908_Firefox",
- expectedDateTime: "2022-08-07 19:59:08",
- },
- {
- fileName: "Screenshot_20220507-195908",
- expectedDateTime: "2022-05-07 19:59:08",
- },
- {
- fileName: "2022-02-18 16.00.12-DCMX.png",
- expectedDateTime: "2022-02-18 16:00:12",
- },
- { fileName: "20221107_231730", expectedDateTime: "2022-11-07 23:17:30" },
- {
- fileName: "2020-11-01 02.31.02",
- expectedDateTime: "2020-11-01 02:31:02",
- },
- {
- fileName: "IMG_20210921_144423",
- expectedDateTime: "2021-09-21 14:44:23",
- },
- {
- // we don't parse the time from this format, will improve later
- fileName: "2019-10-31 155703",
- expectedDateTime: "2019-10-31 00:00:00",
- correctExpectedDateTime: "2019-10-31 15:57:03",
- },
- {
- fileName: "IMG_20210921_144423_783",
- expectedDateTime: "2021-09-21 14:44:23",
- },
- {
- fileName: "Screenshot_2022-06-21-16-51-29-164_newFormat.heic",
- expectedDateTime: "2022-06-21 16:51:29",
- },
- {
- fileName:
- "Screenshot 20221106 211633.com.google.android.apps.nbu.paisa.user.jpg",
- expectedDateTime: "2022-11-06 21:16:33",
- },
- {
- fileName: "signal-2022-12-17-15-16-04-718.jpg",
- expectedDateTime: "2022-12-17 15:16:04",
- },
-];
+const dateCases = [
+ ["Screenshot_20220807-195908_Firefox", "2022-08-07 19:59:08"],
+ ["Screenshot_20220507-195908", "2022-05-07 19:59:08"],
+ ["2022-02-18 16.00.12-DCMX.png", "2022-02-18 16:00:12"],
+ ["20221107_231730", "2022-11-07 23:17:30"],
+ ["2020-11-01 02.31.02", "2020-11-01 02:31:02"],
+ ["IMG_20210921_144423", "2021-09-21 14:44:23"],
+ ["2019-10-31 155703", "2019-10-31 00:00:00"],
+ ["IMG_20210921_144423_783", "2021-09-21 14:44:23"],
+ [
+ "Screenshot_2022-06-21-16-51-29-164_newFormat.heic",
+ "2022-06-21 16:51:29",
+ ],
+ [
+ "Screenshot 20221106 211633.com.google.android.apps.nbu.paisa.user.jpg",
+ "2022-11-06 21:16:33",
+ ],
+ ["signal-2022-12-17-15-16-04-718.jpg", "2022-12-17 15:16:04"],
+] as const;
-const dateTimeParsingTestFilenames2 = [
- {
- fileName: "20170923_220934000_iOS.jpg",
- expectedDateTime: "2017-09-23 22:09:34",
- },
-];
+const epochMicrosecondCases = [
+ ["20170923_220934000_iOS.jpg", "2017-09-23 22:09:34"],
+] as const;
-const DATE_TIME_PARSING_TEST_FILE_NAMES_MUST_FAIL = [
+const unparseableDateFileNames = [
"Snapchat-431959199.mp4.",
"Snapchat-400000000.mp4",
"Snapchat-900000000.mp4",
"Snapchat-100-10-20-19-15-12",
-];
+] as const;
-const fileNameToJSONMappingCases = [
- {
- filename: "IMG20210211125718-edited.jpg",
- jsonFilename: "IMG20210211125718.jpg.json",
- },
- {
- filename: "IMG20210211174722.jpg",
- jsonFilename: "IMG20210211174722.jpg.json",
- },
- {
- filename: "21345678901234567890123456789012345678901234567.png",
- jsonFilename: "2134567890123456789012345678901234567890123456.json",
- },
- {
- filename: "IMG20210211174722(1).jpg",
- jsonFilename: "IMG20210211174722.jpg(1).json",
- },
- {
- filename: "IMG2021021(4455)74722(1).jpg",
- jsonFilename: "IMG2021021(4455)74722.jpg(1).json",
- },
- {
- filename: "IMG2021021.json74722(1).jpg",
- jsonFilename: "IMG2021021.json74722.jpg(1).json",
- },
- {
- filename: "IMG2021021(1)74722(1).jpg",
- jsonFilename: "IMG2021021(1)74722.jpg(1).json",
- },
- {
- filename: "IMG_1159.HEIC",
- jsonFilename: "IMG_1159.HEIC.supplemental-metadata.json",
- },
- {
- filename: "PXL_20241231_151646544.MP.jpg",
- jsonFilename: "PXL_20241231_151646544.MP.jpg.supplemental-met.json",
- },
- {
- filename: "PXL_20240827_094331806.PORTRAIT(1).jpg",
- jsonFilename: "PXL_20240827_094331806.PORTRAIT.jpg.supplement(1).json",
- },
- {
- filename: "PXL_20240506_142610305.LONG_EXPOSURE-01.COVER.jpg",
- jsonFilename: "PXL_20240506_142610305.LONG_EXPOSURE-01.COVER..json",
- },
- {
- filename: "PXL_20211120_223243932.MOTION-02.ORIGINAL.jpg",
- jsonFilename: "PXL_20211120_223243932.MOTION-02.ORIGINAL.jpg..json",
- },
- {
- filename: "20220322_205147-edited(1).jpg",
- jsonFilename: "20220322_205147.jpg.supplemental-metadata(1).json",
- },
- // Some exports keep the numbered suffix in the original position
- {
- filename: "skytree (2).jpg",
- jsonFilename: "skytree (2).jpg.supplemental-metadata.json",
- },
-];
+const metadataCases = [
+ ["IMG20210211125718-edited.jpg", "IMG20210211125718.jpg.json"],
+ ["IMG20210211174722.jpg", "IMG20210211174722.jpg.json"],
+ [
+ "21345678901234567890123456789012345678901234567.png",
+ "2134567890123456789012345678901234567890123456.json",
+ ],
+ ["IMG20210211174722(1).jpg", "IMG20210211174722.jpg(1).json"],
+ ["IMG2021021(4455)74722(1).jpg", "IMG2021021(4455)74722.jpg(1).json"],
+ ["IMG2021021.json74722(1).jpg", "IMG2021021.json74722.jpg(1).json"],
+ ["IMG2021021(1)74722(1).jpg", "IMG2021021(1)74722.jpg(1).json"],
+ ["IMG_1159.HEIC", "IMG_1159.HEIC.supplemental-metadata.json"],
+ [
+ "PXL_20241231_151646544.MP.jpg",
+ "PXL_20241231_151646544.MP.jpg.supplemental-met.json",
+ ],
+ [
+ "PXL_20240827_094331806.PORTRAIT(1).jpg",
+ "PXL_20240827_094331806.PORTRAIT.jpg.supplement(1).json",
+ ],
+ [
+ "PXL_20240506_142610305.LONG_EXPOSURE-01.COVER.jpg",
+ "PXL_20240506_142610305.LONG_EXPOSURE-01.COVER..json",
+ ],
+ [
+ "PXL_20211120_223243932.MOTION-02.ORIGINAL.jpg",
+ "PXL_20211120_223243932.MOTION-02.ORIGINAL.jpg..json",
+ ],
+ [
+ "20220322_205147-edited(1).jpg",
+ "20220322_205147.jpg.supplemental-metadata(1).json",
+ ],
+ ["skytree (2).jpg", "skytree (2).jpg.supplemental-metadata.json"],
+] as const;
-export function testUpload() {
- try {
- parseDateTimeFromFileNameTest();
- fileNameToJSONMappingTests();
- } catch (e) {
- console.log(e);
- }
-}
+const formatLocalDateTime = (date: Date | undefined) => {
+ if (!date) throw new Error("Expected date to parse");
-function parseDateTimeFromFileNameTest() {
- DATE_TIME_PARSING_TEST_FILE_NAMES.forEach(
- ({ fileName, expectedDateTime }) => {
- const dateTime = parseDateFromDigitGroups(fileName);
- const formattedDateTime = getFormattedDateTime(dateTime);
- if (formattedDateTime !== expectedDateTime) {
- throw Error(
- `parseDateTimeFromFileNameTest failed ❌ ,
- for ${fileName}
- expected: ${expectedDateTime} got: ${formattedDateTime}`,
- );
- }
- },
- );
+ const year = date.getFullYear();
+ const month = formatTwoDigits(date.getMonth() + 1);
+ const day = formatTwoDigits(date.getDate());
+ const hour = formatTwoDigits(date.getHours());
+ const minute = formatTwoDigits(date.getMinutes());
+ const second = formatTwoDigits(date.getSeconds());
- dateTimeParsingTestFilenames2.forEach(({ fileName, expectedDateTime }) => {
- const epochMicroseconds =
- tryParseEpochMicrosecondsFromFileName(fileName);
- const formattedDateTime = getFormattedDateTime(
- new Date(epochMicroseconds / 1000),
- );
- if (formattedDateTime !== expectedDateTime) {
- throw Error(
- `parseDateTimeFromFileNameTest2 failed ❌ ,
- for ${fileName}
- expected: ${expectedDateTime} got: ${formattedDateTime}`,
- );
- }
- });
+ return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
+};
- DATE_TIME_PARSING_TEST_FILE_NAMES_MUST_FAIL.forEach((fileName) => {
- const dateTime = parseDateFromDigitGroups(fileName);
- if (dateTime) {
- throw Error(
- `parseDateTimeFromFileNameTest failed ❌ ,
- for ${fileName}
- expected: null got: ${dateTime}`,
- );
- }
- });
- console.log("parseDateTimeFromFileNameTest passed βœ…");
-}
+const formatTwoDigits = (value: number) => value.toString().padStart(2, "0");
-const fileNameToJSONMappingTests = () => {
- for (const { filename, jsonFilename } of fileNameToJSONMappingCases) {
- const jsonKey = metadataJSONMapKeyForJSON(undefined, 0, jsonFilename);
+describe("upload filename metadata", () => {
+ test.each(dateCases)("parses %s", (fileName, expected) => {
+ expect(formatLocalDateTime(parseDateFromDigitGroups(fileName))).toBe(
+ expected,
+ );
+ });
- // See the docs for the file name matcher as to why it doesn't return
- // the key but instead indexes into the map for us. To test it, we
- // construct a placeholder map with a dummy entry for the expected key.
+ test.each(epochMicrosecondCases)(
+ "parses epoch microseconds from %s",
+ (fileName, expected) => {
+ const epochMicroseconds =
+ tryParseEpochMicrosecondsFromFileName(fileName);
+ if (epochMicroseconds == undefined)
+ throw new Error("Expected epoch microseconds to parse");
- const map = new Map([[jsonKey, {}]]);
- if (!matchJSONMetadata(undefined, 0, filename, map)) {
- throw Error(
- `fileNameToJSONMappingTests failed ❌ for ${filename} and ${jsonFilename}`,
- );
- }
- }
- console.log("fileNameToJSONMappingTests passed βœ…");
-};
+ expect(
+ formatLocalDateTime(new Date(epochMicroseconds / 1000)),
+ ).toBe(expected);
+ },
+ );
-// format: YYYY-MM-DD HH:MM:SS
-function getFormattedDateTime(date: Date) {
- const year = date.getFullYear();
- const month = padZero(date.getMonth() + 1);
- const day = padZero(date.getDate());
- const hour = padZero(date.getHours());
- const minute = padZero(date.getMinutes());
- const second = padZero(date.getSeconds());
+ test.each(unparseableDateFileNames)("does not parse %s", (fileName) => {
+ expect(parseDateFromDigitGroups(fileName)).toBeUndefined();
+ });
- return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
-}
+ test.each(metadataCases)("matches %s to %s", (fileName, jsonFileName) => {
+ const metadata = { description: fileName };
+ const map = new Map([
+ [metadataJSONMapKeyForJSON(undefined, 0, jsonFileName), metadata],
+ ]);
-function padZero(number: number) {
- return number < 10 ? `0${number}` : number;
-}
+ expect(matchJSONMetadata(undefined, 0, fileName, map)).toBe(metadata);
+ });
+});
diff --git a/web/package-lock.json b/web/package-lock.json
index 295ef58d57..5cc90fed0e 100644
--- a/web/package-lock.json
+++ b/web/package-lock.json
@@ -295,7 +295,8 @@
"@types/react": "19.2.16",
"@types/react-dom": "19.2.3",
"@types/react-window": "1.8.8",
- "ente-build-config": "*"
+ "ente-build-config": "*",
+ "vitest": "4.1.7"
}
},
"apps/share": {
diff --git a/web/package.json b/web/package.json
index 734b803dfb..6b21a23e42 100644
--- a/web/package.json
+++ b/web/package.json
@@ -40,7 +40,7 @@
"dev:twoof3": "npm exec --workspace twoof3 -- next dev -p 3009",
"lint": "concurrently --names 'prettier,eslint,tsc' \"prettier --check --log-level warn .\" \"npm exec --workspaces -- eslint\" \"npm exec --workspaces -- tsc\"",
"lint:fix": "concurrently --names 'prettier,eslint,tsc' \"prettier --write --log-level warn .\" \"npm exec --workspaces -- eslint --fix\" \"npm exec --workspaces -- tsc\"",
- "test": "npm run test --workspace ente-wasm && npm run test --workspace ente-contacts-web"
+ "test": "npm run test --workspace ente-wasm && npm run test --workspace ente-contacts-web && npm run test --workspace photos"
},
"overrides": {
"@types/react": "19.2.16",
diff --git a/web/packages/new/photos/services/search/sidebar-search-registry.ts b/web/packages/new/photos/services/search/sidebar-search-registry.ts
index c70c9ca874..d2a9e74ce4 100644
--- a/web/packages/new/photos/services/search/sidebar-search-registry.ts
+++ b/web/packages/new/photos/services/search/sidebar-search-registry.ts
@@ -3,7 +3,6 @@ import { isHLSGenerationSupported } from "ente-gallery/services/video";
import { wait } from "ente-utils/promise";
import { t } from "i18next";
import { isMLSupported } from "../ml";
-import { isDevBuildAndUser } from "../settings";
import type { SearchOption, SidebarActionID } from "./types";
export interface SidebarAction {
@@ -286,13 +285,6 @@ const sidebarActions = (): SidebarAction[] => {
path: [helpCategory, t("view_logs")],
keywords: ["logs", "debug"],
},
- {
- id: "help.testUpload",
- label: t("test_upload"),
- path: [helpCategory, t("test_upload")],
- keywords: ["test", "upload"],
- available: () => isDevBuildAndUser(),
- },
{
id: "utility.logout",
label: t("logout"),
@@ -413,7 +405,6 @@ export const performSidebarAction = async (
case "help.requestFeature":
case "help.support":
case "help.viewLogs":
- case "help.testUpload":
ctx.setPendingHelpAction(actionID);
ctx.showHelp();
return Promise.resolve();
diff --git a/web/packages/new/photos/services/search/types.ts b/web/packages/new/photos/services/search/types.ts
index 16af62e126..9863d1ab86 100644
--- a/web/packages/new/photos/services/search/types.ts
+++ b/web/packages/new/photos/services/search/types.ts
@@ -60,8 +60,7 @@ export type SidebarActionID =
| "help.blog"
| "help.requestFeature"
| "help.support"
- | "help.viewLogs"
- | "help.testUpload";
+ | "help.viewLogs";
/**
* A search suggestion.
diff --git a/web/packages/new/photos/services/settings.ts b/web/packages/new/photos/services/settings.ts
index b69da1b75a..afe3922da6 100644
--- a/web/packages/new/photos/services/settings.ts
+++ b/web/packages/new/photos/services/settings.ts
@@ -2,8 +2,6 @@
* @file Storage (in-memory, local, remote) and update of various settings.
*/
-import { savedPartialLocalUser } from "ente-accounts/services/accounts-db";
-import { isDevBuild } from "ente-base/env";
import log from "ente-base/log";
import {
updateChecksumProtectedUploadsEnabled,
@@ -235,17 +233,6 @@ const setSettingsSnapshot = (snapshot: Settings) => {
_state.settingsListeners.forEach((l) => l());
};
-/**
- * Return `true` if this is a development build, and the current user (if any)
- * is marked as an "development" user.
- *
- * Emails that end in "@ente.com" are considered as dev users.
- */
-export const isDevBuildAndUser = () => isDevBuild && isDevUserViaEmail();
-
-const isDevUserViaEmail = () =>
- !!savedPartialLocalUser()?.email?.endsWith("@ente.com");
-
/**
* Persist the user's custom domain preference both locally and on remote.
*