Skip to content

Commit 49fdef7

Browse files
committed
refactor: separate browser snapshot diagnostic validation
1 parent 34d9283 commit 49fdef7

2 files changed

Lines changed: 110 additions & 37 deletions

File tree

‎packages/browser-sdk/src/flag/flagCache.ts‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@ export function isObject(item: unknown): item is Record<string, any> {
1010
return item !== null && typeof item === "object" && !Array.isArray(item);
1111
}
1212

13+
function isRuleEvaluationResult(result: unknown): boolean {
14+
return (
15+
isObject(result) &&
16+
typeof result.ruleId === "string" &&
17+
typeof result.matched === "boolean" &&
18+
(result.allocationIndex === undefined ||
19+
(Number.isInteger(result.allocationIndex) &&
20+
result.allocationIndex >= 0)) &&
21+
(result.destination === undefined ||
22+
(isObject(result.destination) &&
23+
(result.destination.type === "nextRule" ||
24+
(result.destination.type === "variant" &&
25+
typeof result.destination.variantKey === "string"))))
26+
);
27+
}
28+
29+
function isEvaluationError(error: unknown): boolean {
30+
return (
31+
isObject(error) &&
32+
typeof error.code === "string" &&
33+
typeof error.message === "string" &&
34+
(error.field === undefined || typeof error.field === "string") &&
35+
(error.operator === undefined || typeof error.operator === "string") &&
36+
(error.source === undefined ||
37+
(isObject(error.source) &&
38+
(error.source.type === "flag" || error.source.type === "segment") &&
39+
typeof error.source.key === "string"))
40+
);
41+
}
42+
1343
/** Accept complete evaluated snapshots atomically, including Node v2 bootstrap. */
1444
export function parseAPIFlagsResponse(
1545
input: unknown,
@@ -25,34 +55,9 @@ export function parseAPIFlagsResponse(
2555
(bootstrap && flag.resolved === false && flag.value === undefined)
2656
) ||
2757
!Array.isArray(flag.ruleResults) ||
28-
!flag.ruleResults.every(
29-
(r: unknown) =>
30-
isObject(r) &&
31-
typeof r.ruleId === "string" &&
32-
typeof r.matched === "boolean" &&
33-
(r.allocationIndex === undefined ||
34-
(Number.isInteger(r.allocationIndex) && r.allocationIndex >= 0)) &&
35-
(r.destination === undefined ||
36-
(isObject(r.destination) &&
37-
(r.destination.type === "nextRule" ||
38-
(r.destination.type === "variant" &&
39-
typeof r.destination.variantKey === "string")))),
40-
) ||
58+
!flag.ruleResults.every(isRuleEvaluationResult) ||
4159
!Array.isArray(flag.errors) ||
42-
!flag.errors.every(
43-
(error: unknown) =>
44-
isObject(error) &&
45-
typeof error.code === "string" &&
46-
typeof error.message === "string" &&
47-
(error.field === undefined || typeof error.field === "string") &&
48-
(error.operator === undefined ||
49-
typeof error.operator === "string") &&
50-
(error.source === undefined ||
51-
(isObject(error.source) &&
52-
(error.source.type === "flag" ||
53-
error.source.type === "segment") &&
54-
typeof error.source.key === "string")),
55-
) ||
60+
!flag.errors.every(isEvaluationError) ||
5661
(flag.matchedRuleId !== undefined &&
5762
typeof flag.matchedRuleId !== "string") ||
5863
(flag.allocationIndex !== undefined &&

‎packages/browser-sdk/test/flagCache.test.ts‎

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,86 @@ function setup() {
2222
afterEach(() => vi.useRealTimers());
2323

2424
describe("v2 evaluated cache", () => {
25-
test.each([
26-
[{ code: "MISSING_CONTEXT_FIELD" }],
27-
[
28-
{
29-
code: "MISSING_CONTEXT_FIELD",
30-
message: "missing",
31-
source: { type: "unknown", key: "s" },
25+
const rule = { ruleId: "rule", matched: true };
26+
const error = { code: "MISSING_CONTEXT_FIELD", message: "missing" };
27+
28+
test("accepts diagnostic optional fields and preserves their values", () => {
29+
const flags = {
30+
flagA: {
31+
...selectedFlag(true),
32+
ruleResults: [
33+
rule,
34+
{ ...rule, allocationIndex: 0, destination: { type: "nextRule" } },
35+
{
36+
...rule,
37+
allocationIndex: 1,
38+
destination: { type: "variant", variantKey: "" },
39+
},
40+
],
41+
errors: [
42+
error,
43+
{ ...error, source: { type: "flag", key: "parent" } },
44+
{
45+
...error,
46+
field: "user.id",
47+
operator: "IS",
48+
source: { type: "segment", key: "enterprise" },
49+
},
50+
],
3251
},
33-
],
34-
])("rejects malformed v2 diagnostics: %j", (errors) => {
52+
};
53+
expect(parseAPIFlagsResponse(flags)).toEqual(flags);
54+
});
55+
56+
test.each([
57+
null,
58+
true,
59+
{},
60+
{ ...rule, ruleId: 1 },
61+
{ ...rule, matched: undefined },
62+
{ ...rule, matched: 1 },
63+
...[-1, 0.5, Infinity, NaN, "0", null].map((allocationIndex) => ({
64+
...rule,
65+
allocationIndex,
66+
})),
67+
...[
68+
null,
69+
[],
70+
{ type: "unknown" },
71+
{ type: "variant" },
72+
{ type: "variant", variantKey: 1 },
73+
].map((destination) => ({ ...rule, destination })),
74+
])("rejects malformed rule diagnostics atomically: %j", (invalidRule) => {
75+
expect(
76+
parseAPIFlagsResponse({
77+
good: selectedFlag(true),
78+
bad: { ...selectedFlag(true), ruleResults: [invalidRule] },
79+
}),
80+
).toBeUndefined();
81+
});
82+
83+
test.each([
84+
null,
85+
true,
86+
{},
87+
{ code: "MISSING_CONTEXT_FIELD" },
88+
{ ...error, code: 1 },
89+
{ ...error, message: null },
90+
{ ...error, field: null },
91+
{ ...error, operator: 1 },
92+
...[
93+
null,
94+
[],
95+
{ type: "unknown", key: "s" },
96+
{ type: "flag" },
97+
{ type: "segment", key: 1 },
98+
].map((source) => ({ ...error, source })),
99+
])("rejects malformed error diagnostics atomically: %j", (invalidError) => {
35100
expect(
36-
parseAPIFlagsResponse({ flagA: { ...selectedFlag(true), errors } }),
101+
parseAPIFlagsResponse({
102+
good: selectedFlag(true),
103+
bad: { ...selectedFlag(true), errors: [invalidError] },
104+
}),
37105
).toBeUndefined();
38106
});
39107

0 commit comments

Comments
 (0)