From 3f4109d8a9d98b42fd78ba88021fbb9516e10937 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Wed, 4 Feb 2026 15:43:41 +0100 Subject: [PATCH] test: add integration test for FeatureRepository with @Memoize decorator (#27613) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../FeatureRepository.integration-test.ts | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 packages/features/di/containers/FeatureRepository.integration-test.ts diff --git a/packages/features/di/containers/FeatureRepository.integration-test.ts b/packages/features/di/containers/FeatureRepository.integration-test.ts new file mode 100644 index 0000000000..a337502a15 --- /dev/null +++ b/packages/features/di/containers/FeatureRepository.integration-test.ts @@ -0,0 +1,161 @@ +import { afterEach, describe, expect, it } from "vitest"; + +import { prisma } from "@calcom/prisma"; + +import { getFeatureRepository } from "./FeatureRepository"; + +const uniqueId = () => `${Date.now()}-${Math.random().toString(36).slice(2, 9)}`; + +const cleanupFunctions: Array<() => Promise> = []; + +afterEach(async () => { + for (const cleanup of cleanupFunctions) { + await cleanup(); + } + cleanupFunctions.length = 0; +}); + +interface TestEntities { + testFeatureSlug: string; +} + +async function setup(): Promise { + const id = uniqueId(); + const testFeatureSlug = `test-memoize-feature-${id}`; + + const cleanup = async () => { + await prisma.feature.deleteMany({ + where: { slug: testFeatureSlug }, + }); + }; + + cleanupFunctions.push(cleanup); + + return { + testFeatureSlug, + }; +} + +describe("FeatureRepository DI Container Integration Tests", () => { + describe("getFeatureRepository with @Memoize decorator", () => { + it("should return the cached feature repository instance", () => { + const repo1 = getFeatureRepository(); + const repo2 = getFeatureRepository(); + + expect(repo1).toBeDefined(); + expect(repo2).toBeDefined(); + expect(repo1).toBe(repo2); + }); + + it("should find a feature by slug using the cached repository", async () => { + const { testFeatureSlug } = await setup(); + + await prisma.feature.create({ + data: { + slug: testFeatureSlug, + enabled: true, + type: "OPERATIONAL", + description: "Test feature for memoize integration test", + }, + }); + + const repo = getFeatureRepository(); + const feature = await repo.findBySlug(testFeatureSlug); + + expect(feature).not.toBeNull(); + expect(feature?.slug).toBe(testFeatureSlug); + expect(feature?.enabled).toBe(true); + }); + + it("should return null for non-existent feature", async () => { + const { testFeatureSlug } = await setup(); + + const repo = getFeatureRepository(); + const feature = await repo.findBySlug(testFeatureSlug); + + expect(feature).toBeNull(); + }); + + it("should check if feature is enabled globally", async () => { + const { testFeatureSlug } = await setup(); + + await prisma.feature.create({ + data: { + slug: testFeatureSlug, + enabled: true, + type: "OPERATIONAL", + description: "Test feature for memoize integration test", + }, + }); + + const repo = getFeatureRepository(); + const isEnabled = await repo.checkIfFeatureIsEnabledGlobally(testFeatureSlug); + + expect(isEnabled).toBe(true); + }); + + it("should return false for disabled feature", async () => { + const { testFeatureSlug } = await setup(); + + await prisma.feature.create({ + data: { + slug: testFeatureSlug, + enabled: false, + type: "OPERATIONAL", + description: "Test feature for memoize integration test", + }, + }); + + const repo = getFeatureRepository(); + const isEnabled = await repo.checkIfFeatureIsEnabledGlobally(testFeatureSlug); + + expect(isEnabled).toBe(false); + }); + + it("should find all features", async () => { + const { testFeatureSlug } = await setup(); + + await prisma.feature.create({ + data: { + slug: testFeatureSlug, + enabled: true, + type: "OPERATIONAL", + description: "Test feature for memoize integration test", + }, + }); + + const repo = getFeatureRepository(); + const features = await repo.findAll(); + + expect(Array.isArray(features)).toBe(true); + const testFeature = features.find((f) => f.slug === testFeatureSlug); + expect(testFeature).toBeDefined(); + }); + + it("should update a feature and invalidate cache via @Unmemoize", async () => { + const { testFeatureSlug } = await setup(); + + await prisma.feature.create({ + data: { + slug: testFeatureSlug, + enabled: false, + type: "OPERATIONAL", + description: "Test feature for memoize integration test", + }, + }); + + const repo = getFeatureRepository(); + + const beforeUpdate = await repo.findBySlug(testFeatureSlug); + expect(beforeUpdate?.enabled).toBe(false); + + await repo.update({ + featureId: testFeatureSlug as Parameters[0]["featureId"], + enabled: true, + }); + + const afterUpdate = await repo.findBySlug(testFeatureSlug); + expect(afterUpdate?.enabled).toBe(true); + }); + }); +});