From 2f6ac92f9c68c25084b922ec465311a7743347d1 Mon Sep 17 00:00:00 2001 From: Alfred Grip Date: Tue, 7 Jan 2025 14:11:34 +0100 Subject: [PATCH] refactor search attribute used as links, add test --- src/lib/search/searchTypes.test.ts | 29 +++++++++++++++++++++++++ src/lib/search/searchTypes.ts | 34 +++++++++++++++++------------- 2 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 src/lib/search/searchTypes.test.ts diff --git a/src/lib/search/searchTypes.test.ts b/src/lib/search/searchTypes.test.ts new file mode 100644 index 00000000..506ab9de --- /dev/null +++ b/src/lib/search/searchTypes.test.ts @@ -0,0 +1,29 @@ +import { expect, test } from "vitest"; +import { + attributesUsedAsLink, + availableSearchIndexes, + listOfattributesUsedAsLink, +} from "./searchTypes"; + +/** + * Test that all indexes has a link attribute + * To reduce data sent to client during search + * we slice some attributes from the search result + * This test makes sure that we don't slice attributes + * that are used as links. + * All indexes should have at least one such attribute + */ +test("all indexes has attribute that doesn't get sliced", () => { + for (const index of availableSearchIndexes) { + const indexAttributes = attributesUsedAsLink[index]; + expect( + indexAttributes, + `Index "${index}" has no attributes that doesn't get sliced`, + ).not.toBeUndefined(); + expect(Array.isArray(indexAttributes)).toBe(true); + expect(indexAttributes.length > 0).toBe(true); + } + expect(listOfattributesUsedAsLink.length).toBeGreaterThanOrEqual( + availableSearchIndexes.length, + ); +}); diff --git a/src/lib/search/searchTypes.ts b/src/lib/search/searchTypes.ts index 9afae03b..b22d0779 100644 --- a/src/lib/search/searchTypes.ts +++ b/src/lib/search/searchTypes.ts @@ -291,25 +291,29 @@ export type SearchDataWithType = data: CommitteeSearchReturnAttributes; }; +/** + * The server slices strings before sending them to the client + * to reduce traffic. Some attributes cannot be sliced however, + * since they are used to link to the entry, or e.g. as a image + */ export const attributesUsedAsLink: { - members: keyof MemberSearchReturnAttributes; - events: keyof EventSearchReturnAttributes; - articles: keyof ArticleSearchReturnAttributes; - songs: keyof SongSearchReturnAttributes; - positions: keyof PositionSearchReturnAttributes; - committees: keyof CommitteeSearchReturnAttributes; + members: Array; + events: Array; + articles: Array; + songs: Array; + positions: Array; + committees: Array; } = { - members: "studentId", - events: "slug", - articles: "slug", - songs: "slug", - positions: "dsekId", - committees: "shortName", + members: ["studentId", "picturePath"], + events: ["slug"], + articles: ["slug"], + songs: ["slug"], + positions: ["dsekId"], + committees: ["shortName", "darkImageUrl", "lightImageUrl", "monoImageUrl"], }; -export const listOfattributesUsedAsLink: string[] = Object.values( - attributesUsedAsLink, -) satisfies string[]; +export const listOfattributesUsedAsLink: string[] = + Object.values(attributesUsedAsLink).flat(); type DefaultRankingRules = | "words"