From 9933bd5385a010270e034eefb869eca9b2488e07 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 4 Feb 2025 08:06:10 +0530 Subject: [PATCH] Reduce duplication in tests --- web/apps/photos/tests/upload.test.ts | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/web/apps/photos/tests/upload.test.ts b/web/apps/photos/tests/upload.test.ts index b9f77682ed..130d44a8cc 100644 --- a/web/apps/photos/tests/upload.test.ts +++ b/web/apps/photos/tests/upload.test.ts @@ -7,10 +7,7 @@ import { } from "@/new/photos/services/files"; import { parseDateFromDigitGroups } from "services/upload/date"; import { - getClippedMetadataJSONMapKeyForFile, - getFileNameComponents, - getMetadataJSONMapKeyForFile, - getSupplementaryMetadataJSONMapKeyForFile, + matchTakeoutMetadata, metadataJSONMapKeyForJSON, } from "services/upload/takeout"; import { getUserDetailsV2 } from "services/userService"; @@ -429,19 +426,14 @@ const fileNameToJSONMappingTests = () => { for (const { filename, jsonFilename } of fileNameToJSONMappingCases) { const jsonKey = metadataJSONMapKeyForJSON(0, jsonFilename); - // This duplicates somewhat the logic in takeout.ts:matchTakeoutMetadata() - const components = getFileNameComponents(filename); - let fileKey = getMetadataJSONMapKeyForFile(0, components); - if (fileKey != jsonKey) { - fileKey = getClippedMetadataJSONMapKeyForFile(0, components); - } - if (fileKey != jsonKey) { - fileKey = getSupplementaryMetadataJSONMapKeyForFile(0, components); - } + // 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. - if (fileKey != jsonKey) { + const map = new Map([[jsonKey, {}]]); + if (!matchTakeoutMetadata(filename, 0, map)) { throw Error( - `fileNameToJSONMappingTests failed ❌ for ${filename} (expected: ${jsonKey} got: ${fileKey})`, + `fileNameToJSONMappingTests failed ❌ for ${filename} and ${jsonFilename}`, ); } }