Skip to content

Commit 0b962fe

Browse files
committed
Enhance CEL expression evaluation tests for country code handling. Added tests to verify behavior of 'in_list' and 'equals' operations for country codes. Updated user dialog and country code select components to handle null values and improve validation logic. Refactored condition builder to ensure proper handling of single values and arrays for country code conditions.
1 parent 180c1a7 commit 0b962fe

6 files changed

Lines changed: 19 additions & 7 deletions

File tree

apps/backend/src/lib/cel-evaluator.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,17 @@ import.meta.vitest?.test('evaluateCelExpression with missing email', async ({ ex
332332
// Empty email should match empty string
333333
expect(evaluateCelExpression('email == ""', context)).toBe(true);
334334
});
335+
336+
import.meta.vitest?.test('countryCode in_list vs equals', ({ expect }) => {
337+
const usContext = createSignUpRuleContext({
338+
email: 'test@example.com',
339+
countryCode: 'US',
340+
authMethod: 'password',
341+
oauthProvider: null,
342+
riskScores: { bot: 0, freeTrialAbuse: 0 },
343+
});
344+
345+
expect(evaluateCelExpression('countryCode in ["US", "CA"]', usContext)).toBe(true);
346+
expect(evaluateCelExpression('countryCode in ["CA"]', usContext)).toBe(false);
347+
expect(evaluateCelExpression('countryCode == "US"', usContext)).toBe(true);
348+
});

apps/dashboard/src/components/country-code-select.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function CountryCodeField<F extends FieldValues>(props: {
125125
<FormControl>
126126
<CountryCodeSelect
127127
value={field.value || null}
128-
onChange={(val) => field.onChange(val ?? "")}
128+
onChange={(val) => field.onChange(val)}
129129
placeholder={props.placeholder ?? "Select country code..."}
130130
disabled={props.disabled}
131131
className="max-w-lg"

apps/dashboard/src/components/rule-builder/condition-builder.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,8 @@ function ConditionRow({
175175

176176
const handleOperatorChange = (operator: ConditionOperator) => {
177177
let value = condition.value;
178-
// Convert between single value and array for in_list
179178
if (operator === 'in_list' && !Array.isArray(value)) {
180-
value = value ? [String(value)] : [];
179+
value = value ? [String(value)] : [''];
181180
} else if (operator !== 'in_list' && Array.isArray(value)) {
182181
value = value[0] ?? '';
183182
}
@@ -261,7 +260,6 @@ function ConditionRow({
261260
aria-label={`Remove country code ${index + 1}`}
262261
title="Remove country code"
263262
onClick={() => handleRemoveCountryCodeListItem(index)}
264-
disabled={countryCodeListValues.length <= 1}
265263
>
266264
<MinusIcon className="h-4 w-4" />
267265
</Button>

apps/dashboard/src/components/user-dialog.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function UserDialog(props: {
4242
} else {
4343
defaultValues = {
4444
signedUpAt: new Date(),
45-
countryCode: "",
45+
countryCode: null as string | null,
4646
botRiskScore: "",
4747
freeTrialAbuseRiskScore: "",
4848
};
@@ -75,7 +75,7 @@ export function UserDialog(props: {
7575
}).optional(),
7676
passwordEnabled: yup.boolean().optional(),
7777
updatePassword: yup.boolean().optional(),
78-
countryCode: countryCodeSchema.transform((value) => value === "" ? undefined : value).optional(),
78+
countryCode: countryCodeSchema.nullable().transform((value) => value === "" || value == null ? undefined : value).optional(),
7979
botRiskScore: yup.string().test({
8080
name: "bot-risk-score-format",
8181
message: "Bot risk score must be an integer between 0 and 100",

apps/dashboard/src/lib/cel-visual-parser.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,5 @@ describe('cel-visual-parser', () => {
231231
}
232232
});
233233
});
234+
234235
});

apps/dashboard/src/lib/cel-visual-parser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ function groupToCel(group: GroupNode): string {
174174
const celOperator = group.operator === 'and' ? ' && ' : ' || ';
175175
const childExpressions = group.children.map(child => {
176176
const expr = visualTreeToCel(child);
177-
// Wrap child groups in parentheses if they have a different operator
178177
if (child.type === 'group' && child.operator !== group.operator) {
179178
return `(${expr})`;
180179
}

0 commit comments

Comments
 (0)