f4248bf20d
* feat: implement FeatureOptInService WIP * clean up * feat: consolidate feature repositories and add updateFeatureForUser - Implement updateFeatureForUser in FeaturesRepository (similar to updateFeatureForTeam) - Move getUserFeatureState and getTeamFeatureState from PrismaFeatureOptInRepository to FeaturesRepository - Update FeatureOptInService to use only FeaturesRepository - Add setUserFeatureState and setTeamFeatureState methods to FeatureOptInService - Update _router.ts to remove PrismaFeatureOptInRepository usage - Remove PrismaFeatureOptInRepository.ts and FeatureOptInRepositoryInterface.ts - Update features.repository.interface.ts and features.repository.mock.ts - Add integration tests for updateFeatureForUser, getUserFeatureState, getTeamFeatureState - Update service.integration-test.ts to use FeaturesRepository Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * refactor: rename updateFeatureForUser to setUserFeatureState Rename to match the convention used for setTeamFeatureState Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * refactor: return FeatureState type from getUserFeatureState and getTeamFeatureState * fix integration tests * clean up logics * update services and router * refactor: change getUserFeatureState and getTeamFeatureState to accept featureIds array - Renamed getUserFeatureState to getUserFeatureStates - Renamed getTeamFeatureState to getTeamFeatureStates - Changed parameter from featureId: string to featureIds: string[] - Changed return type from FeatureState to Record<string, FeatureState> - Updated FeatureOptInService to use the new batch methods - Added tests for querying multiple features in a single call - Optimized listFeaturesForTeam to fetch all feature states in one query Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * feat: add getFeatureStateForTeams for batch querying multiple teams - Added getFeatureStateForTeams method to query a single feature across multiple teams in one call - Updated FeatureOptInService.resolveFeatureStateAcrossTeams to use the new batch method - Replaces N+1 queries with a single database query for team states - Added comprehensive integration tests for the new method Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * refactor: combine org and team state queries into single call - Include orgId in the teamIds array passed to getFeatureStateForTeams - Extract org state and team states from the combined result - Reduces database queries from 3 to 2 in resolveFeatureStateAcrossTeams Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * refactor: use team.isOrganization and clarify computeEffectiveState comment Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * refactor: use MembershipRepository.findAllByUserId with isOrganization Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * feat: add featureId validation using isOptInFeature type guard Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * less queries * add fallback value * fix type error * move files * add autoOptInFeatures column * use autoOptInFeatures flag within FeatureOptInService * add setUserAutoOptIn and setTeamAutoOptIn * fix computeEffectiveState logic * rewrite computeEffectiveState * clean up integration tests * clean up in afterEach * fix type error * refactor: use FeaturesRepository methods instead of direct Prisma calls Replace all manual userFeatures and teamFeatures Prisma operations with the new setUserFeatureState and setTeamFeatureState repository methods. Changes include: - Admin handlers (assignFeatureToTeam, unassignFeatureFromTeam) - Test fixtures and integration tests - Playwright fixtures - Development scripts This ensures consistent feature flag management through the repository pattern and supports the new tri-state semantics (enabled/disabled/inherit). Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * clean up * fix the logic * extract some logic into applyAutoOptIn() * remove wrong code * refactor: convert setUserFeatureState and setTeamFeatureState to object params with discriminated union - Convert multiple positional parameters to single object parameter - Use discriminated union types: assignedBy required for enabled/disabled, omitted for inherit - Update all callers across repository, service, handlers, fixtures, and tests * fix type error * use Promise.all * fix --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
189 lines
5.6 KiB
TypeScript
189 lines
5.6 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
|
|
import { applyAutoOptIn } from "./applyAutoOptIn";
|
|
|
|
describe("applyAutoOptIn", () => {
|
|
describe("org state transformation", () => {
|
|
it("transforms org state from inherit to enabled when orgAutoOptIn is true", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: [],
|
|
userState: "inherit",
|
|
orgAutoOptIn: true,
|
|
teamAutoOptIns: [],
|
|
userAutoOptIn: false,
|
|
});
|
|
|
|
expect(result.effectiveOrgState).toBe("enabled");
|
|
});
|
|
|
|
it("keeps org state as inherit when orgAutoOptIn is false", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: [],
|
|
userState: "inherit",
|
|
orgAutoOptIn: false,
|
|
teamAutoOptIns: [],
|
|
userAutoOptIn: false,
|
|
});
|
|
|
|
expect(result.effectiveOrgState).toBe("inherit");
|
|
});
|
|
|
|
it("does not transform org state when it is explicitly enabled", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "enabled",
|
|
teamStates: [],
|
|
userState: "inherit",
|
|
orgAutoOptIn: true,
|
|
teamAutoOptIns: [],
|
|
userAutoOptIn: false,
|
|
});
|
|
|
|
expect(result.effectiveOrgState).toBe("enabled");
|
|
});
|
|
|
|
it("does not transform org state when it is explicitly disabled", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "disabled",
|
|
teamStates: [],
|
|
userState: "inherit",
|
|
orgAutoOptIn: true,
|
|
teamAutoOptIns: [],
|
|
userAutoOptIn: false,
|
|
});
|
|
|
|
expect(result.effectiveOrgState).toBe("disabled");
|
|
});
|
|
});
|
|
|
|
describe("team states transformation", () => {
|
|
it("transforms team states from inherit to enabled when corresponding autoOptIn is true", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: ["inherit", "inherit", "inherit"],
|
|
userState: "inherit",
|
|
orgAutoOptIn: false,
|
|
teamAutoOptIns: [true, false, true],
|
|
userAutoOptIn: false,
|
|
});
|
|
|
|
expect(result.effectiveTeamStates).toEqual(["enabled", "inherit", "enabled"]);
|
|
});
|
|
|
|
it("does not transform team states when they are explicitly set", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: ["enabled", "disabled", "inherit"],
|
|
userState: "inherit",
|
|
orgAutoOptIn: false,
|
|
teamAutoOptIns: [true, true, false],
|
|
userAutoOptIn: false,
|
|
});
|
|
|
|
expect(result.effectiveTeamStates).toEqual(["enabled", "disabled", "inherit"]);
|
|
});
|
|
});
|
|
|
|
describe("user state transformation", () => {
|
|
it("transforms user state from inherit to enabled when userAutoOptIn is true", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: [],
|
|
userState: "inherit",
|
|
orgAutoOptIn: false,
|
|
teamAutoOptIns: [],
|
|
userAutoOptIn: true,
|
|
});
|
|
|
|
expect(result.effectiveUserState).toBe("enabled");
|
|
});
|
|
|
|
it("keeps user state as inherit when userAutoOptIn is false", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: [],
|
|
userState: "inherit",
|
|
orgAutoOptIn: false,
|
|
teamAutoOptIns: [],
|
|
userAutoOptIn: false,
|
|
});
|
|
|
|
expect(result.effectiveUserState).toBe("inherit");
|
|
});
|
|
|
|
it("does not transform user state when it is explicitly enabled", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: [],
|
|
userState: "enabled",
|
|
orgAutoOptIn: false,
|
|
teamAutoOptIns: [],
|
|
userAutoOptIn: true,
|
|
});
|
|
|
|
expect(result.effectiveUserState).toBe("enabled");
|
|
});
|
|
|
|
it("does not transform user state when it is explicitly disabled", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: [],
|
|
userState: "disabled",
|
|
orgAutoOptIn: false,
|
|
teamAutoOptIns: [],
|
|
userAutoOptIn: true,
|
|
});
|
|
|
|
expect(result.effectiveUserState).toBe("disabled");
|
|
});
|
|
});
|
|
|
|
describe("combined transformations", () => {
|
|
it("transforms all levels independently when autoOptIn is true", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: ["inherit", "inherit"],
|
|
userState: "inherit",
|
|
orgAutoOptIn: true,
|
|
teamAutoOptIns: [true, true],
|
|
userAutoOptIn: true,
|
|
});
|
|
|
|
expect(result.effectiveOrgState).toBe("enabled");
|
|
expect(result.effectiveTeamStates).toEqual(["enabled", "enabled"]);
|
|
expect(result.effectiveUserState).toBe("enabled");
|
|
});
|
|
|
|
it("only transforms levels with autoOptIn enabled", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "inherit",
|
|
teamStates: ["inherit", "inherit"],
|
|
userState: "inherit",
|
|
orgAutoOptIn: false,
|
|
teamAutoOptIns: [true, false],
|
|
userAutoOptIn: true,
|
|
});
|
|
|
|
expect(result.effectiveOrgState).toBe("inherit");
|
|
expect(result.effectiveTeamStates).toEqual(["enabled", "inherit"]);
|
|
expect(result.effectiveUserState).toBe("enabled");
|
|
});
|
|
|
|
it("respects explicit states even when autoOptIn is true", () => {
|
|
const result = applyAutoOptIn({
|
|
orgState: "disabled",
|
|
teamStates: ["enabled", "disabled"],
|
|
userState: "enabled",
|
|
orgAutoOptIn: true,
|
|
teamAutoOptIns: [true, true],
|
|
userAutoOptIn: true,
|
|
});
|
|
|
|
expect(result.effectiveOrgState).toBe("disabled");
|
|
expect(result.effectiveTeamStates).toEqual(["enabled", "disabled"]);
|
|
expect(result.effectiveUserState).toBe("enabled");
|
|
});
|
|
});
|
|
});
|