1- import { LDContext , LDFlagsStateOptions } from '@launchdarkly/js-server-sdk-common' ;
1+ import { LDContext } from '@launchdarkly/js-server-sdk-common' ;
22
33import { createLDServerSession } from '../../src/server/index' ;
4+ import { makeMockServerClient } from './mockServerClient' ;
45
56const context : LDContext = { kind : 'user' , key : 'test-user' } ;
67
7- function makeMockBaseClient ( ) {
8- return {
9- initialized : jest . fn ( ( ) => true ) ,
10- boolVariation : jest . fn ( ( _key : string , _ctx : LDContext , def : boolean ) => Promise . resolve ( def ) ) ,
11- numberVariation : jest . fn ( ( _key : string , _ctx : LDContext , def : number ) => Promise . resolve ( def ) ) ,
12- stringVariation : jest . fn ( ( _key : string , _ctx : LDContext , def : string ) => Promise . resolve ( def ) ) ,
13- jsonVariation : jest . fn ( ( _key : string , _ctx : LDContext , def : unknown ) => Promise . resolve ( def ) ) ,
14- boolVariationDetail : jest . fn ( ( _key : string , _ctx : LDContext , def : boolean ) =>
15- Promise . resolve ( { value : def , variationIndex : null , reason : { kind : 'OFF' as const } } ) ,
16- ) ,
17- numberVariationDetail : jest . fn ( ( _key : string , _ctx : LDContext , def : number ) =>
18- Promise . resolve ( { value : def , variationIndex : null , reason : { kind : 'OFF' as const } } ) ,
19- ) ,
20- stringVariationDetail : jest . fn ( ( _key : string , _ctx : LDContext , def : string ) =>
21- Promise . resolve ( { value : def , variationIndex : null , reason : { kind : 'OFF' as const } } ) ,
22- ) ,
23- jsonVariationDetail : jest . fn ( ( _key : string , _ctx : LDContext , def : unknown ) =>
24- Promise . resolve ( { value : def , variationIndex : null , reason : { kind : 'OFF' as const } } ) ,
25- ) ,
26- // @ts -ignore — mock return shape matches LDFlagsState structurally
27- allFlagsState : jest . fn ( ( _context : LDContext , _options ?: LDFlagsStateOptions ) =>
28- Promise . resolve ( {
29- valid : true ,
30- getFlagValue : jest . fn ( ) ,
31- getFlagReason : jest . fn ( ) ,
32- allValues : jest . fn ( ( ) => ( { } ) ) ,
33- toJSON : jest . fn ( ( ) => ( { $flagsState : { } , $valid : true } ) ) ,
34- } ) ,
35- ) ,
36- } ;
37- }
38-
398it ( 'getContext() returns the context passed at creation' , ( ) => {
40- const client = makeMockBaseClient ( ) ;
9+ const client = makeMockServerClient ( ) ;
4110 const session = createLDServerSession ( client , context ) ;
4211 expect ( session . getContext ( ) ) . toEqual ( context ) ;
4312} ) ;
4413
4514it ( 'initialized() delegates to the base client' , ( ) => {
46- const client = makeMockBaseClient ( ) ;
15+ const client = makeMockServerClient ( ) ;
4716 client . initialized . mockReturnValue ( false ) ;
4817 const session = createLDServerSession ( client , context ) ;
4918 expect ( session . initialized ( ) ) . toBe ( false ) ;
5019 expect ( client . initialized ) . toHaveBeenCalledTimes ( 1 ) ;
5120} ) ;
5221
5322it ( 'boolVariation() calls base client with bound context' , async ( ) => {
54- const client = makeMockBaseClient ( ) ;
23+ const client = makeMockServerClient ( ) ;
5524 client . boolVariation . mockResolvedValue ( true ) ;
5625 const session = createLDServerSession ( client , context ) ;
5726 const result = await session . boolVariation ( 'my-flag' , false ) ;
@@ -60,7 +29,7 @@ it('boolVariation() calls base client with bound context', async () => {
6029} ) ;
6130
6231it ( 'numberVariation() calls base client with bound context' , async ( ) => {
63- const client = makeMockBaseClient ( ) ;
32+ const client = makeMockServerClient ( ) ;
6433 client . numberVariation . mockResolvedValue ( 42 ) ;
6534 const session = createLDServerSession ( client , context ) ;
6635 const result = await session . numberVariation ( 'my-flag' , 0 ) ;
@@ -69,7 +38,7 @@ it('numberVariation() calls base client with bound context', async () => {
6938} ) ;
7039
7140it ( 'stringVariation() calls base client with bound context' , async ( ) => {
72- const client = makeMockBaseClient ( ) ;
41+ const client = makeMockServerClient ( ) ;
7342 client . stringVariation . mockResolvedValue ( 'hello' ) ;
7443 const session = createLDServerSession ( client , context ) ;
7544 const result = await session . stringVariation ( 'my-flag' , 'default' ) ;
@@ -78,7 +47,7 @@ it('stringVariation() calls base client with bound context', async () => {
7847} ) ;
7948
8049it ( 'jsonVariation() calls base client with bound context' , async ( ) => {
81- const client = makeMockBaseClient ( ) ;
50+ const client = makeMockServerClient ( ) ;
8251 const json = { key : 'value' } ;
8352 client . jsonVariation . mockResolvedValue ( json ) ;
8453 const session = createLDServerSession ( client , context ) ;
@@ -88,7 +57,7 @@ it('jsonVariation() calls base client with bound context', async () => {
8857} ) ;
8958
9059it ( 'boolVariationDetail() calls base client with bound context' , async ( ) => {
91- const client = makeMockBaseClient ( ) ;
60+ const client = makeMockServerClient ( ) ;
9261 const detail = { value : true , variationIndex : 1 , reason : { kind : 'RULE_MATCH' as const } } ;
9362 // @ts -ignore — valid LDEvaluationDetailTyped<boolean> shape; mock type is too narrow
9463 client . boolVariationDetail . mockResolvedValue ( detail ) ;
@@ -99,7 +68,7 @@ it('boolVariationDetail() calls base client with bound context', async () => {
9968} ) ;
10069
10170it ( 'numberVariationDetail() calls base client with bound context' , async ( ) => {
102- const client = makeMockBaseClient ( ) ;
71+ const client = makeMockServerClient ( ) ;
10372 const detail = { value : 42 , variationIndex : 1 , reason : { kind : 'RULE_MATCH' as const } } ;
10473 // @ts -ignore — valid LDEvaluationDetailTyped<number> shape; mock type is too narrow
10574 client . numberVariationDetail . mockResolvedValue ( detail ) ;
@@ -110,7 +79,7 @@ it('numberVariationDetail() calls base client with bound context', async () => {
11079} ) ;
11180
11281it ( 'stringVariationDetail() calls base client with bound context' , async ( ) => {
113- const client = makeMockBaseClient ( ) ;
82+ const client = makeMockServerClient ( ) ;
11483 const detail = { value : 'hello' , variationIndex : 1 , reason : { kind : 'RULE_MATCH' as const } } ;
11584 // @ts -ignore — valid LDEvaluationDetailTyped<string> shape; mock type is too narrow
11685 client . stringVariationDetail . mockResolvedValue ( detail ) ;
@@ -121,7 +90,7 @@ it('stringVariationDetail() calls base client with bound context', async () => {
12190} ) ;
12291
12392it ( 'jsonVariationDetail() calls base client with bound context' , async ( ) => {
124- const client = makeMockBaseClient ( ) ;
93+ const client = makeMockServerClient ( ) ;
12594 const detail = {
12695 value : { key : 'value' } ,
12796 variationIndex : 1 ,
@@ -136,14 +105,14 @@ it('jsonVariationDetail() calls base client with bound context', async () => {
136105} ) ;
137106
138107it ( 'allFlagsState() calls base client with bound context' , async ( ) => {
139- const client = makeMockBaseClient ( ) ;
108+ const client = makeMockServerClient ( ) ;
140109 const session = createLDServerSession ( client , context ) ;
141110 await session . allFlagsState ( ) ;
142111 expect ( client . allFlagsState ) . toHaveBeenCalledWith ( context , undefined ) ;
143112} ) ;
144113
145114it ( 'allFlagsState() forwards options to base client' , async ( ) => {
146- const client = makeMockBaseClient ( ) ;
115+ const client = makeMockServerClient ( ) ;
147116 const session = createLDServerSession ( client , context ) ;
148117 const options = { clientSideOnly : true } ;
149118 await session . allFlagsState ( options ) ;
@@ -165,7 +134,7 @@ describe('given a browser environment (window defined)', () => {
165134 } ) ;
166135
167136 it ( 'throws an error instead of returning a no-op session' , ( ) => {
168- const client = makeMockBaseClient ( ) ;
137+ const client = makeMockServerClient ( ) ;
169138 expect ( ( ) => createLDServerSession ( client , context ) ) . toThrow (
170139 'createLDServerWrapper must only be called on the server.' ,
171140 ) ;
0 commit comments