* refactor: convert getShouldServeCache to CacheService with dependency injection - Create CacheService class following AvailableSlotsService DI pattern - Add FeaturesRepository and CacheService to DI tokens and modules - Create cache container with proper dependency injection setup - Update handleNewBooking.ts and slots/util.ts to use new service - Maintain backward compatibility with error-throwing wrapper function - Follow established service patterns for clean architecture Co-Authored-By: morgan@cal.com <morgan@cal.com> * feat: inject CacheService into AvailableSlotsService via dependency injection - Add cacheService to IAvailableSlotsService interface - Update available-slots container to load cache modules - Update available-slots module to inject CacheService dependency - Replace direct getShouldServeCache call with injected service method - Add CacheService import to util.ts for proper typing Co-Authored-By: morgan@cal.com <morgan@cal.com> * chore: DI api v2 cache service * refactor: convert FeaturesRepository to use factory pattern in DI - Change from constructor injection to factory pattern to avoid PRISMA_CLIENT binding issues in tests - FeaturesRepository now uses default prisma instance instead of DI injection - Resolves test failures while maintaining DI container compatibility - Tests reduced from 123+ failures to only 5 unrelated failures Co-Authored-By: morgan@cal.com <morgan@cal.com> * revert: use direct FeaturesRepository instantiation in most usage points - Revert getFeaturesRepository() calls back to new FeaturesRepository() - Tests require direct instantiation for mocking compatibility - Keep DI container for specific use cases that need dependency injection - Resolves test failures while maintaining both DI and direct usage patterns Co-Authored-By: morgan@cal.com <morgan@cal.com> * fix: resolve FeaturesRepository DI container issues - Update cache module to use factory pattern with proper ICacheService interface - Remove featuresModule loading from cache and available-slots containers - Use direct FeaturesRepository instantiation via getFeaturesRepository() - Resolves 'No binding found for key: Symbol(FeaturesRepository)' errors - Reduces test failures from 125 to 7 (remaining failures appear unrelated) Co-Authored-By: morgan@cal.com <morgan@cal.com> * fix: update all FeaturesRepository instantiations to include prisma parameter - Add prisma parameter to all new FeaturesRepository() calls across the codebase - Update API v2 services to match main repo interfaces - Fix PrismaFeaturesRepository to implement IFeaturesRepository directly - Update CacheService in API v2 to expose required dependencies and getShouldServeCache - Implement CheckBookingLimitsService in API v2 with proper interface - Resolves type assignment errors between API v2 and main repo implementations Co-Authored-By: morgan@cal.com <morgan@cal.com> * fix: add prisma parameter to remaining FeaturesRepository instantiations in apps/web/lib - Update getServerSideProps files to pass prisma parameter to FeaturesRepository - Ensures all FeaturesRepository instantiations follow the new constructor pattern - Completes the refactoring to use direct instantiation with prisma parameter Co-Authored-By: morgan@cal.com <morgan@cal.com> * refactor clean and fix devin issues * chore: bump platform libs * chore: bump platform libs * chore: bump platform libs * chore: bump platform libs * fix: missing di * fix workflow test * fix workflow test * fix integration test --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: morgan@cal.com <morgan@cal.com> Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
329 lines
8.8 KiB
TypeScript
329 lines
8.8 KiB
TypeScript
import { describe, expect, beforeAll, afterAll, beforeEach, it } from "vitest";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import type { AppFlags } from "./config";
|
|
import { FeaturesRepository } from "./features.repository";
|
|
|
|
const featuresRepository = new FeaturesRepository(prisma);
|
|
|
|
// Access private clearCache method through type assertion
|
|
const clearCache = () => {
|
|
(featuresRepository as any).clearCache();
|
|
};
|
|
|
|
describe("FeaturesRepository Integration Tests", () => {
|
|
let testUser: { id: number };
|
|
let testTeam: { id: number };
|
|
const testFeature = "teams" as keyof AppFlags;
|
|
|
|
beforeAll(async () => {
|
|
// Create test user
|
|
testUser = await prisma.user.create({
|
|
data: {
|
|
email: `test-${Date.now()}@example.com`,
|
|
username: `test-${Date.now()}`,
|
|
},
|
|
});
|
|
|
|
// Create test team
|
|
testTeam = await prisma.team.create({
|
|
data: {
|
|
name: `Test Team ${Date.now()}`,
|
|
slug: `test-team-${Date.now()}`,
|
|
},
|
|
});
|
|
});
|
|
|
|
afterAll(async () => {
|
|
// Clean up test data in correct order to respect foreign key constraints
|
|
await prisma.userFeatures.deleteMany({
|
|
where: { userId: testUser.id },
|
|
});
|
|
await prisma.teamFeatures.deleteMany({
|
|
where: { teamId: testTeam.id },
|
|
});
|
|
await prisma.feature.deleteMany({
|
|
where: { slug: testFeature },
|
|
});
|
|
await prisma.membership.deleteMany({
|
|
where: { userId: testUser.id },
|
|
});
|
|
await prisma.team.deleteMany({
|
|
where: { id: testTeam.id },
|
|
});
|
|
await prisma.user.delete({
|
|
where: { id: testUser.id },
|
|
});
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
// Reset feature state before each test
|
|
await prisma.feature.deleteMany({
|
|
where: { slug: testFeature },
|
|
});
|
|
await prisma.userFeatures.deleteMany({
|
|
where: { userId: testUser.id },
|
|
});
|
|
await prisma.teamFeatures.deleteMany({
|
|
where: { teamId: testTeam.id },
|
|
});
|
|
await prisma.membership.deleteMany({
|
|
where: { teamId: testTeam.id },
|
|
});
|
|
await prisma.team.deleteMany({
|
|
where: { id: testTeam.id },
|
|
});
|
|
|
|
// Recreate test team
|
|
testTeam = await prisma.team.create({
|
|
data: {
|
|
name: `Test Team ${Date.now()}`,
|
|
slug: `test-team-${Date.now()}`,
|
|
},
|
|
});
|
|
|
|
// Clear the feature flag cache
|
|
clearCache();
|
|
});
|
|
|
|
describe("checkIfFeatureIsEnabledGlobally", () => {
|
|
it("should return false when feature is not enabled globally", async () => {
|
|
// Verify database state
|
|
const dbFeature = await prisma.feature.findUnique({
|
|
where: { slug: testFeature },
|
|
});
|
|
expect(dbFeature).toBeNull();
|
|
|
|
// First call to initialize cache
|
|
await featuresRepository.checkIfFeatureIsEnabledGlobally(testFeature);
|
|
// Second call to get actual result
|
|
const result = await featuresRepository.checkIfFeatureIsEnabledGlobally(testFeature);
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("should return true when feature is enabled globally", async () => {
|
|
// Create the feature with enabled set to true
|
|
await prisma.feature.create({
|
|
data: {
|
|
slug: testFeature,
|
|
enabled: true,
|
|
type: "OPERATIONAL",
|
|
},
|
|
});
|
|
|
|
// Clear the feature flag cache
|
|
clearCache();
|
|
|
|
// Verify database state
|
|
const dbFeature = await prisma.feature.findUnique({
|
|
where: { slug: testFeature },
|
|
});
|
|
expect(dbFeature).not.toBeNull();
|
|
expect(dbFeature?.enabled).toBe(true);
|
|
|
|
// First call to initialize cache
|
|
await featuresRepository.checkIfFeatureIsEnabledGlobally(testFeature);
|
|
// Second call to get actual result
|
|
const result = await featuresRepository.checkIfFeatureIsEnabledGlobally(testFeature);
|
|
expect(result).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("checkIfUserHasFeature", () => {
|
|
it("should return false when user does not have feature", async () => {
|
|
const result = await featuresRepository.checkIfUserHasFeature(testUser.id, testFeature);
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("should return true when user has feature directly assigned", async () => {
|
|
await prisma.feature.create({
|
|
data: {
|
|
slug: testFeature,
|
|
enabled: true,
|
|
type: "OPERATIONAL",
|
|
},
|
|
});
|
|
|
|
await prisma.userFeatures.create({
|
|
data: {
|
|
userId: testUser.id,
|
|
featureId: testFeature,
|
|
assignedBy: "test",
|
|
},
|
|
});
|
|
|
|
const result = await featuresRepository.checkIfUserHasFeature(testUser.id, testFeature);
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("should return true when user belongs to team with feature", async () => {
|
|
await prisma.feature.create({
|
|
data: {
|
|
slug: testFeature,
|
|
enabled: true,
|
|
type: "OPERATIONAL",
|
|
},
|
|
});
|
|
|
|
await prisma.teamFeatures.create({
|
|
data: {
|
|
teamId: testTeam.id,
|
|
featureId: testFeature,
|
|
assignedBy: "test",
|
|
},
|
|
});
|
|
|
|
await prisma.membership.create({
|
|
data: {
|
|
teamId: testTeam.id,
|
|
userId: testUser.id,
|
|
role: "MEMBER",
|
|
accepted: true,
|
|
},
|
|
});
|
|
|
|
const result = await featuresRepository.checkIfUserHasFeature(testUser.id, testFeature);
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("should return true when user belongs to team where parent team has feature", async () => {
|
|
// Create an organization
|
|
const org = await prisma.team.create({
|
|
data: {
|
|
name: `Test Org ${Date.now()}`,
|
|
slug: `test-org-${Date.now()}`,
|
|
isOrganization: true,
|
|
},
|
|
});
|
|
|
|
// Make the test team part of the organization
|
|
await prisma.team.update({
|
|
where: { id: testTeam.id },
|
|
data: { parentId: org.id },
|
|
});
|
|
|
|
// Create the feature
|
|
await prisma.feature.create({
|
|
data: {
|
|
slug: testFeature,
|
|
enabled: true,
|
|
type: "OPERATIONAL",
|
|
},
|
|
});
|
|
|
|
// Clear the feature flag cache
|
|
clearCache();
|
|
|
|
// Assign feature to the organization
|
|
await prisma.teamFeatures.create({
|
|
data: {
|
|
teamId: org.id,
|
|
featureId: testFeature,
|
|
assignedBy: "test",
|
|
},
|
|
});
|
|
|
|
// Check if user is already a member of the team
|
|
const existingMembership = await prisma.membership.findFirst({
|
|
where: {
|
|
teamId: testTeam.id,
|
|
userId: testUser.id,
|
|
},
|
|
});
|
|
|
|
// Only create membership if it doesn't exist
|
|
if (!existingMembership) {
|
|
await prisma.membership.create({
|
|
data: {
|
|
teamId: testTeam.id,
|
|
userId: testUser.id,
|
|
role: "MEMBER",
|
|
accepted: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
const result = await featuresRepository.checkIfUserHasFeature(testUser.id, testFeature);
|
|
expect(result).toBe(true);
|
|
|
|
// Clean up organization
|
|
await prisma.teamFeatures.deleteMany({
|
|
where: { teamId: org.id },
|
|
});
|
|
await prisma.team.delete({
|
|
where: { id: org.id },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("checkIfTeamHasFeature", () => {
|
|
it("should return false when team does not have feature", async () => {
|
|
const result = await featuresRepository.checkIfTeamHasFeature(testTeam.id, testFeature);
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("should return true when team has feature directly assigned", async () => {
|
|
await prisma.feature.create({
|
|
data: {
|
|
slug: testFeature,
|
|
enabled: true,
|
|
type: "OPERATIONAL",
|
|
},
|
|
});
|
|
|
|
await prisma.teamFeatures.create({
|
|
data: {
|
|
teamId: testTeam.id,
|
|
featureId: testFeature,
|
|
assignedBy: "test",
|
|
},
|
|
});
|
|
|
|
const result = await featuresRepository.checkIfTeamHasFeature(testTeam.id, testFeature);
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("should return true when parent team has feature", async () => {
|
|
const parentTeam = await prisma.team.create({
|
|
data: {
|
|
name: `Parent Team ${Date.now()}`,
|
|
slug: `parent-team-${Date.now()}`,
|
|
},
|
|
});
|
|
|
|
await prisma.team.update({
|
|
where: { id: testTeam.id },
|
|
data: { parentId: parentTeam.id },
|
|
});
|
|
|
|
await prisma.feature.create({
|
|
data: {
|
|
slug: testFeature,
|
|
enabled: true,
|
|
type: "OPERATIONAL",
|
|
},
|
|
});
|
|
|
|
await prisma.teamFeatures.create({
|
|
data: {
|
|
teamId: parentTeam.id,
|
|
featureId: testFeature,
|
|
assignedBy: "test",
|
|
},
|
|
});
|
|
|
|
const result = await featuresRepository.checkIfTeamHasFeature(testTeam.id, testFeature);
|
|
expect(result).toBe(true);
|
|
|
|
// Clean up parent team
|
|
await prisma.teamFeatures.deleteMany({
|
|
where: { teamId: parentTeam.id },
|
|
});
|
|
await prisma.team.delete({
|
|
where: { id: parentTeam.id },
|
|
});
|
|
});
|
|
});
|
|
});
|