Skip to content

Commit f55148f

Browse files
committed
specify default reason details, add tests
1 parent 590bd7c commit f55148f

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,6 @@ verdaccio
104104
e2e/js/js-cdn/src/assets/devcycle.min.js
105105
e2e/js/js-cdn/playwright-report/
106106
e2e/js/js-esm/playwright-report/
107+
108+
#opencode ai guidelines
109+
AGENTS.md

sdk/nodejs/__tests__/client.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
22
import { DevCycleClient } from '../src/client'
33
import { DevCycleUser } from '@devcycle/js-cloud-server-sdk'
4-
import { DVCCustomDataJSON } from '@devcycle/types'
4+
import {
5+
DEFAULT_REASON_DETAILS,
6+
DVCCustomDataJSON,
7+
EVAL_REASON_DETAILS,
8+
EVAL_REASONS,
9+
} from '@devcycle/types'
510
import { EvalHook } from '../src/hooks/EvalHook'
611

712
jest.mock('../src/bucketing')
@@ -80,6 +85,11 @@ describe('variable', () => {
8085
const variable = client.variable(user, 'test-key', false)
8186
expect(variable.value).toEqual(true)
8287
expect(variable.type).toEqual('Boolean')
88+
expect(variable.eval).toEqual({
89+
reason: EVAL_REASONS.TARGETING_MATCH,
90+
details: EVAL_REASON_DETAILS.ALL_USERS,
91+
target_id: 'mockTargetId',
92+
})
8393

8494
expect(client.variableValue(user, 'test-key', false)).toEqual(true)
8595
})
@@ -98,6 +108,10 @@ describe('variable', () => {
98108
const variable = client.variable(user, 'test-key2', false)
99109
expect(variable.value).toEqual(false)
100110
expect(variable.isDefaulted).toEqual(true)
111+
expect(variable.eval).toEqual({
112+
reason: EVAL_REASONS.DEFAULT,
113+
details: DEFAULT_REASON_DETAILS.USER_NOT_TARGETED,
114+
})
101115

102116
// @ts-ignore
103117
client.bucketingLib.variableForUser_PB.mockReturnValueOnce(null)
@@ -110,6 +124,10 @@ describe('variable', () => {
110124
const variable = client.variable(user, 'test-key', 'test')
111125
expect(variable.value).toEqual('test')
112126
expect(variable.isDefaulted).toEqual(true)
127+
expect(variable.eval).toEqual({
128+
reason: EVAL_REASONS.DEFAULT,
129+
details: DEFAULT_REASON_DETAILS.TYPE_MISMATCH,
130+
})
113131

114132
expect(client.variableValue(user, 'test-key', 'test')).toEqual('test')
115133
})

sdk/nodejs/src/__mocks__/bucketing.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { Exports, ProtobufTypes } from '@devcycle/bucketing-assembly-script'
2+
import { EVAL_REASON_DETAILS, EVAL_REASONS } from '@devcycle/types'
23

34
const testVariable = {
45
_id: 'test-id',
56
value: true,
67
type: 'Boolean',
78
key: 'test-key',
89
evalReason: null,
10+
eval: {
11+
reason: EVAL_REASONS.TARGETING_MATCH,
12+
details: EVAL_REASON_DETAILS.ALL_USERS,
13+
target_id: 'mockTargetId',
14+
},
915
}
1016
const buffer = ProtobufTypes.SDKVariable_PB.encode({
1117
_id: testVariable._id,
@@ -14,6 +20,7 @@ const buffer = ProtobufTypes.SDKVariable_PB.encode({
1420
boolValue: testVariable.value,
1521
doubleValue: 0,
1622
stringValue: '',
23+
eval: testVariable.eval,
1724
}).finish()
1825

1926
enum VariableType {

sdk/nodejs/src/client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export class DevCycleClient<
281281
if (configVariable) {
282282
if (type === configVariable.type) {
283283
options.value = configVariable.value as VariableTypeAlias<T>
284-
options.eval = configVariable?.eval
284+
options.eval = { ...configVariable?.eval }
285285
} else {
286286
options.eval = {
287287
reason: EVAL_REASONS.DEFAULT,
@@ -291,6 +291,11 @@ export class DevCycleClient<
291291
`Type mismatch for variable ${key}. Expected ${type}, got ${configVariable.type}`,
292292
)
293293
}
294+
} else {
295+
options.eval = {
296+
reason: EVAL_REASONS.DEFAULT,
297+
details: DEFAULT_REASON_DETAILS.USER_NOT_TARGETED,
298+
}
294299
}
295300

296301
return new DVCVariable(options)

0 commit comments

Comments
 (0)