22 * @vitest -environment node
33 */
44import { beforeEach , describe , expect , it , vi } from 'vitest'
5- import type {
6- FeatureFlagContext ,
7- FeatureFlagName ,
8- FeatureFlagsConfig ,
9- } from '@/lib/core/config/feature-flags'
5+ import type { FeatureFlagContext , FeatureFlagName } from '@/lib/core/config/feature-flags'
106
117const { mockFetch, mockIsPlatformAdmin, envRef, flagRef } = vi . hoisted ( ( ) => ( {
128 mockFetch : vi . fn ( ) ,
@@ -23,6 +19,7 @@ vi.mock('@/lib/core/config/appconfig', () => ({
2319} ) )
2420
2521vi . mock ( '@/lib/core/config/env' , ( ) => ( {
22+ isTruthy : ( v : unknown ) => Boolean ( v ) ,
2623 get env ( ) {
2724 return envRef
2825 } ,
@@ -60,21 +57,22 @@ describe('getFeatureFlags', () => {
6057 flagRef . isAppConfigEnabled = false
6158 } )
6259
63- it ( 'derives flags from fallback secrets (empty registry → empty) when AppConfig is disabled, without fetching' , async ( ) => {
64- expect ( await getFeatureFlags ( ) ) . toEqual < FeatureFlagsConfig > ( { flags : { } } )
60+ it ( 'derives flags from fallback secrets when AppConfig is disabled, without fetching' , async ( ) => {
61+ const flags = await getFeatureFlags ( )
62+ // All registered flags should be present, disabled (env vars unset in test env)
63+ expect ( flags [ 'tables-fractional-ordering' ] ) . toEqual ( { enabled : false } )
64+ expect ( flags [ 'mothership-beta' ] ) . toEqual ( { enabled : false } )
6565 expect ( mockFetch ) . not . toHaveBeenCalled ( )
6666 } )
6767
6868 it ( 'reads the feature-flags profile and normalizes the payload when enabled' , async ( ) => {
6969 withAppConfig ( {
70- flags : {
71- a : { enabled : true } ,
72- b : { orgIds : [ 'Org_1' , ' org_1 ' , '' , 'org_2' ] , userIds : 'nope' } ,
73- c : 'not-an-object' ,
74- } ,
70+ a : { enabled : true } ,
71+ b : { orgIds : [ 'Org_1' , ' org_1 ' , '' , 'org_2' ] , userIds : 'nope' } ,
72+ c : 'not-an-object' ,
7573 } )
7674
77- const { flags } = await getFeatureFlags ( )
75+ const flags = await getFeatureFlags ( )
7876 expect ( flags . a ) . toEqual ( { enabled : true } )
7977 expect ( flags . b ) . toEqual ( { orgIds : [ 'Org_1' , 'org_1' , 'org_2' ] } )
8078 expect ( flags . c ) . toBeUndefined ( )
@@ -87,14 +85,16 @@ describe('getFeatureFlags', () => {
8785 it ( 'falls back to the secret-derived document when the fetch yields null' , async ( ) => {
8886 flagRef . isAppConfigEnabled = true
8987 mockFetch . mockResolvedValue ( null )
90- expect ( await getFeatureFlags ( ) ) . toEqual < FeatureFlagsConfig > ( { flags : { } } )
88+ const flags = await getFeatureFlags ( )
89+ expect ( flags [ 'tables-fractional-ordering' ] ) . toEqual ( { enabled : false } )
90+ expect ( flags [ 'mothership-beta' ] ) . toEqual ( { enabled : false } )
9191 } )
9292
9393 it ( 'degrades gracefully on a malformed document' , async ( ) => {
94- withAppConfig ( { flags : 'not-an-object' } )
95- expect ( await getFeatureFlags ( ) ) . toEqual < FeatureFlagsConfig > ( { flags : { } } )
94+ withAppConfig ( 'not-an-object' )
95+ expect ( await getFeatureFlags ( ) ) . toMatchObject ( { } )
9696 withAppConfig ( null )
97- expect ( await getFeatureFlags ( ) ) . toEqual < FeatureFlagsConfig > ( { flags : { } } )
97+ expect ( await getFeatureFlags ( ) ) . toMatchObject ( { } )
9898 } )
9999} )
100100
@@ -105,31 +105,31 @@ describe('isFeatureEnabled', () => {
105105 } )
106106
107107 it ( 'returns false for an unknown flag' , async ( ) => {
108- withAppConfig ( { flags : { } } )
108+ withAppConfig ( { } )
109109 expect ( await enabled ( 'missing' , { userId : 'u1' } ) ) . toBe ( false )
110110 } )
111111
112112 it ( 'matches the global enabled clause' , async ( ) => {
113- withAppConfig ( { flags : { f : { enabled : true } } } )
113+ withAppConfig ( { f : { enabled : true } } )
114114 expect ( await enabled ( 'f' ) ) . toBe ( true )
115115 } )
116116
117117 it ( 'matches the userId allowlist' , async ( ) => {
118- withAppConfig ( { flags : { f : { userIds : [ 'u1' ] } } } )
118+ withAppConfig ( { f : { userIds : [ 'u1' ] } } )
119119 expect ( await enabled ( 'f' , { userId : 'u1' } ) ) . toBe ( true )
120120 expect ( await enabled ( 'f' , { userId : 'u2' } ) ) . toBe ( false )
121121 expect ( await enabled ( 'f' , { } ) ) . toBe ( false )
122122 } )
123123
124124 it ( 'matches the orgId allowlist' , async ( ) => {
125- withAppConfig ( { flags : { f : { orgIds : [ 'o1' ] } } } )
125+ withAppConfig ( { f : { orgIds : [ 'o1' ] } } )
126126 expect ( await enabled ( 'f' , { orgId : 'o1' } ) ) . toBe ( true )
127127 expect ( await enabled ( 'f' , { orgId : 'o2' } ) ) . toBe ( false )
128128 } )
129129
130130 describe ( 'admin clause (lazy resolution)' , ( ) => {
131- it ( 'resolves admin from userId when admins is the deciding clause' , async ( ) => {
132- withAppConfig ( { flags : { f : { admins : true } } } )
131+ it ( 'resolves admin from userId when adminEnabled is the deciding clause' , async ( ) => {
132+ withAppConfig ( { f : { adminEnabled : true } } )
133133 mockIsPlatformAdmin . mockResolvedValue ( true )
134134 expect ( await enabled ( 'f' , { userId : 'u1' } ) ) . toBe ( true )
135135 expect ( mockIsPlatformAdmin ) . toHaveBeenCalledWith ( 'u1' )
@@ -139,28 +139,28 @@ describe('isFeatureEnabled', () => {
139139 } )
140140
141141 it ( 'uses the isAdmin override without querying' , async ( ) => {
142- withAppConfig ( { flags : { f : { admins : true } } } )
142+ withAppConfig ( { f : { adminEnabled : true } } )
143143 expect ( await enabled ( 'f' , { userId : 'u1' , isAdmin : true } ) ) . toBe ( true )
144144 expect ( mockIsPlatformAdmin ) . not . toHaveBeenCalled ( )
145145 } )
146146
147147 it ( 'resolves to false without querying when userId is absent' , async ( ) => {
148- withAppConfig ( { flags : { f : { admins : true } } } )
148+ withAppConfig ( { f : { adminEnabled : true } } )
149149 expect ( await enabled ( 'f' , { orgId : 'o1' } ) ) . toBe ( false )
150150 expect ( mockIsPlatformAdmin ) . not . toHaveBeenCalled ( )
151151 } )
152152
153153 it ( 'does not query when an earlier clause already matched' , async ( ) => {
154- withAppConfig ( { flags : { f : { enabled : true , admins : true } } } )
154+ withAppConfig ( { f : { enabled : true , adminEnabled : true } } )
155155 expect ( await enabled ( 'f' , { userId : 'u1' } ) ) . toBe ( true )
156156
157- withAppConfig ( { flags : { g : { userIds : [ 'u1' ] , admins : true } } } )
157+ withAppConfig ( { g : { userIds : [ 'u1' ] , adminEnabled : true } } )
158158 expect ( await enabled ( 'g' , { userId : 'u1' } ) ) . toBe ( true )
159159 expect ( mockIsPlatformAdmin ) . not . toHaveBeenCalled ( )
160160 } )
161161
162- it ( 'does not query when the rule has no admins clause' , async ( ) => {
163- withAppConfig ( { flags : { f : { userIds : [ 'u2' ] } } } )
162+ it ( 'does not query when the rule has no adminEnabled clause' , async ( ) => {
163+ withAppConfig ( { f : { userIds : [ 'u2' ] } } )
164164 expect ( await enabled ( 'f' , { userId : 'u1' } ) ) . toBe ( false )
165165 expect ( mockIsPlatformAdmin ) . not . toHaveBeenCalled ( )
166166 } )
0 commit comments