@rocapine/react-native-onboarding 1.74.0 → 1.75.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -3
- package/dist/evaluateCondition.d.ts.map +1 -1
- package/dist/evaluateCondition.js +103 -4
- package/dist/evaluateCondition.js.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -1
- package/dist/index.js.map +1 -1
- package/dist/infra/provider/OnboardingProvider.d.ts +4 -0
- package/dist/infra/provider/OnboardingProvider.d.ts.map +1 -1
- package/dist/infra/provider/OnboardingProvider.js +51 -15
- package/dist/infra/provider/OnboardingProvider.js.map +1 -1
- package/dist/onboarding-example.d.ts +235 -99
- package/dist/onboarding-example.d.ts.map +1 -1
- package/dist/onboarding-example.js +77 -0
- package/dist/onboarding-example.js.map +1 -1
- package/dist/screens/completingActions.d.ts +43 -0
- package/dist/screens/completingActions.d.ts.map +1 -0
- package/dist/screens/completingActions.js +108 -0
- package/dist/screens/completingActions.js.map +1 -0
- package/dist/screens/elementTypeRegistry.d.ts +31 -0
- package/dist/screens/elementTypeRegistry.d.ts.map +1 -0
- package/dist/screens/elementTypeRegistry.js +82 -0
- package/dist/screens/elementTypeRegistry.js.map +1 -0
- package/dist/screens/resolveRenderableStep.d.ts +42 -0
- package/dist/screens/resolveRenderableStep.d.ts.map +1 -0
- package/dist/screens/resolveRenderableStep.js +28 -0
- package/dist/screens/resolveRenderableStep.js.map +1 -0
- package/dist/screens/unknownElementTypes.d.ts +94 -0
- package/dist/screens/unknownElementTypes.d.ts.map +1 -0
- package/dist/screens/unknownElementTypes.js +141 -0
- package/dist/screens/unknownElementTypes.js.map +1 -0
- package/dist/screens/unknownKeys.d.ts +5 -2
- package/dist/screens/unknownKeys.d.ts.map +1 -1
- package/dist/screens/unknownKeys.js +7 -52
- package/dist/screens/unknownKeys.js.map +1 -1
- package/dist/steps/common.types.d.ts +84 -3
- package/dist/steps/common.types.d.ts.map +1 -1
- package/dist/steps/common.types.js.map +1 -1
- package/dist/userProperties/store.d.ts +3 -1
- package/dist/userProperties/store.d.ts.map +1 -1
- package/dist/userProperties/store.js +3 -1
- package/dist/userProperties/store.js.map +1 -1
- package/package.json +5 -1
- package/src/__tests__/OnboardingDataGate.test.tsx +445 -0
- package/src/__tests__/completingActions.test.ts +139 -0
- package/src/__tests__/elementTypeRegistry.test.ts +48 -0
- package/src/__tests__/evaluateCondition.test.ts +228 -3
- package/src/__tests__/onboardingExampleElements.test.ts +27 -0
- package/src/__tests__/resolveRenderableStep.test.ts +117 -0
- package/src/__tests__/unknownElementTypes.test.ts +240 -0
- package/src/evaluateCondition.ts +107 -4
- package/src/index.ts +23 -0
- package/src/infra/provider/OnboardingProvider.tsx +67 -20
- package/src/onboarding-example.ts +78 -0
- package/src/screens/completingActions.ts +108 -0
- package/src/screens/elementTypeRegistry.ts +88 -0
- package/src/screens/resolveRenderableStep.ts +59 -0
- package/src/screens/unknownElementTypes.ts +204 -0
- package/src/screens/unknownKeys.ts +7 -50
- package/src/steps/common.types.ts +84 -3
- package/src/userProperties/store.ts +3 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
2
|
import { evaluateLeaf, evaluateCondition } from "../evaluateCondition";
|
|
3
3
|
import type { Condition } from "../evaluateCondition";
|
|
4
4
|
|
|
@@ -58,7 +58,9 @@ describe("evaluateLeaf — contains (array)", () => {
|
|
|
58
58
|
describe("evaluateLeaf — in", () => {
|
|
59
59
|
it("passes when variable is in list", () => expect(evaluateLeaf({ variable: "gender", operator: "in", value: ["male", "female"] }, { gender: "male" })).toBe(true));
|
|
60
60
|
it("fails when variable not in list", () => expect(evaluateLeaf({ variable: "gender", operator: "in", value: ["male", "female"] }, { gender: "other" })).toBe(false));
|
|
61
|
-
|
|
61
|
+
// A right-hand side that is not literally an array is covered by the `#225`
|
|
62
|
+
// block at the bottom of this file: it is normalized to a member list rather
|
|
63
|
+
// than answering a constant.
|
|
62
64
|
it("empty list always false", () => expect(evaluateLeaf({ variable: "x", operator: "in", value: [] }, { x: "male" })).toBe(false));
|
|
63
65
|
it("coerces numeric raw to string before comparison", () => expect(evaluateLeaf({ variable: "age", operator: "in", value: ["18", "25"] }, { age: 18 })).toBe(true));
|
|
64
66
|
it("numeric raw not in string list returns false", () => expect(evaluateLeaf({ variable: "age", operator: "in", value: ["30", "40"] }, { age: 18 })).toBe(false));
|
|
@@ -67,7 +69,6 @@ describe("evaluateLeaf — in", () => {
|
|
|
67
69
|
describe("evaluateLeaf — not_in", () => {
|
|
68
70
|
it("passes when variable not in list", () => expect(evaluateLeaf({ variable: "gender", operator: "not_in", value: ["male", "female"] }, { gender: "other" })).toBe(true));
|
|
69
71
|
it("fails when variable is in list", () => expect(evaluateLeaf({ variable: "gender", operator: "not_in", value: ["male", "female"] }, { gender: "male" })).toBe(false));
|
|
70
|
-
it("returns true when value is not array (safe default)", () => expect(evaluateLeaf({ variable: "x", operator: "not_in", value: "male" as any }, { x: "male" })).toBe(true));
|
|
71
72
|
it("coerces numeric raw to string before comparison", () => expect(evaluateLeaf({ variable: "age", operator: "not_in", value: ["18", "25"] }, { age: 30 })).toBe(true));
|
|
72
73
|
it("numeric raw in string list returns false", () => expect(evaluateLeaf({ variable: "age", operator: "not_in", value: ["18", "25"] }, { age: 18 })).toBe(false));
|
|
73
74
|
});
|
|
@@ -200,3 +201,227 @@ describe("evaluateCondition — leaf passthrough", () => {
|
|
|
200
201
|
expect(evaluateCondition(leaf, { x: "no" })).toBe(false);
|
|
201
202
|
});
|
|
202
203
|
});
|
|
204
|
+
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
// evaluateLeaf — `{{var}}` references on the RIGHT-hand side
|
|
207
|
+
//
|
|
208
|
+
// `RepeatElement` documents "Repeat plus a per-row gate is a switch" using
|
|
209
|
+
// `{ variable: "item.sign", operator: "eq", value: "{{zodiacSign}}" }`. That
|
|
210
|
+
// only works if the condition's `value` is interpolated against the same
|
|
211
|
+
// variable map, which lets a condition compare two variables rather than a
|
|
212
|
+
// variable against an authored literal. See issue #217.
|
|
213
|
+
// ---------------------------------------------------------------------------
|
|
214
|
+
|
|
215
|
+
describe("evaluateLeaf — variable references in `value`", () => {
|
|
216
|
+
it("matches the documented Repeat-as-Match example", () =>
|
|
217
|
+
expect(
|
|
218
|
+
evaluateLeaf(
|
|
219
|
+
{ variable: "item.sign", operator: "eq", value: "{{zodiacSign}}" },
|
|
220
|
+
{ "item.sign": "aries", zodiacSign: "aries" }
|
|
221
|
+
)
|
|
222
|
+
).toBe(true));
|
|
223
|
+
|
|
224
|
+
it("rejects the rows the switch should not select", () =>
|
|
225
|
+
expect(
|
|
226
|
+
evaluateLeaf(
|
|
227
|
+
{ variable: "item.sign", operator: "eq", value: "{{zodiacSign}}" },
|
|
228
|
+
{ "item.sign": "taurus", zodiacSign: "aries" }
|
|
229
|
+
)
|
|
230
|
+
).toBe(false));
|
|
231
|
+
|
|
232
|
+
it("compares two variables numerically", () =>
|
|
233
|
+
expect(
|
|
234
|
+
evaluateLeaf(
|
|
235
|
+
{ variable: "score", operator: "gt", value: "{{threshold}}" },
|
|
236
|
+
{ score: 80, threshold: 50 }
|
|
237
|
+
)
|
|
238
|
+
).toBe(true));
|
|
239
|
+
|
|
240
|
+
it("resolves a reference embedded in surrounding text", () =>
|
|
241
|
+
expect(
|
|
242
|
+
evaluateLeaf(
|
|
243
|
+
{ variable: "sku", operator: "eq", value: "plan_{{tier}}" },
|
|
244
|
+
{ sku: "plan_yearly", tier: "yearly" }
|
|
245
|
+
)
|
|
246
|
+
).toBe(true));
|
|
247
|
+
|
|
248
|
+
it("interpolates references inside an `in` array", () =>
|
|
249
|
+
expect(
|
|
250
|
+
evaluateLeaf(
|
|
251
|
+
{ variable: "x", operator: "in", value: ["{{a}}", "{{b}}"] },
|
|
252
|
+
{ x: "two", a: "one", b: "two" }
|
|
253
|
+
)
|
|
254
|
+
).toBe(true));
|
|
255
|
+
|
|
256
|
+
it("resolves an unknown reference to the empty string", () =>
|
|
257
|
+
expect(
|
|
258
|
+
evaluateLeaf(
|
|
259
|
+
{ variable: "x", operator: "eq", value: "{{nope}}" },
|
|
260
|
+
{ x: "something" }
|
|
261
|
+
)
|
|
262
|
+
).toBe(false));
|
|
263
|
+
|
|
264
|
+
it("treats a plain literal as a literal", () =>
|
|
265
|
+
expect(
|
|
266
|
+
evaluateLeaf({ variable: "x", operator: "eq", value: "aries" }, { x: "aries" })
|
|
267
|
+
).toBe(true));
|
|
268
|
+
|
|
269
|
+
it("does not interpolate a literal containing no reference syntax", () =>
|
|
270
|
+
expect(
|
|
271
|
+
evaluateLeaf({ variable: "x", operator: "eq", value: "{not a ref}" }, { x: "{not a ref}" })
|
|
272
|
+
).toBe(true));
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// ---------------------------------------------------------------------------
|
|
276
|
+
// evaluateLeaf — `in` / `not_in` right-hand side that is not literally an array
|
|
277
|
+
//
|
|
278
|
+
// Both operators used to answer a CONSTANT whenever `Array.isArray(value)` was
|
|
279
|
+
// false — `in` false for every row, `not_in` true for every row, no warning
|
|
280
|
+
// (issue #225). Two shapes reach them that way, and both are the natural way to
|
|
281
|
+
// author a membership test now that #217 made a `{{ref}}` on the right-hand
|
|
282
|
+
// side resolve:
|
|
283
|
+
//
|
|
284
|
+
// value: "{{selected}}" a reference to a multi-select variable, whose
|
|
285
|
+
// flat value is a JSON-encoded string '["a","b"]'
|
|
286
|
+
// value: ["{{selected}}"] what Studio's condition editor emits — it splits
|
|
287
|
+
// the value field on commas, so the reference lands
|
|
288
|
+
// as the single member of a one-member array
|
|
289
|
+
//
|
|
290
|
+
// Both must flatten to the same member list, else a fix covering only the bare
|
|
291
|
+
// string leaves every Studio-authored payload broken in the same silent way.
|
|
292
|
+
// ---------------------------------------------------------------------------
|
|
293
|
+
|
|
294
|
+
describe("evaluateLeaf — in / not_in against a JSON-array-string reference", () => {
|
|
295
|
+
const vars = (row: string) => ({ selected: '["sleep","energy"]', "item.value": row });
|
|
296
|
+
|
|
297
|
+
it("`in` matches a row that is a member of the referenced selection", () =>
|
|
298
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "in", value: "{{selected}}" }, vars("sleep"))).toBe(true));
|
|
299
|
+
|
|
300
|
+
it("`in` rejects a row that is not a member", () =>
|
|
301
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "in", value: "{{selected}}" }, vars("focus"))).toBe(false));
|
|
302
|
+
|
|
303
|
+
it("`not_in` rejects a row that is a member", () =>
|
|
304
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "not_in", value: "{{selected}}" }, vars("sleep"))).toBe(false));
|
|
305
|
+
|
|
306
|
+
it("`not_in` matches a row that is not a member", () =>
|
|
307
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "not_in", value: "{{selected}}" }, vars("focus"))).toBe(true));
|
|
308
|
+
|
|
309
|
+
it("an authored JSON-array literal string is also a list", () =>
|
|
310
|
+
expect(evaluateLeaf({ variable: "x", operator: "in", value: '["a","b"]' }, { x: "b" })).toBe(true));
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
describe("evaluateLeaf — in / not_in with the array-wrapped shape Studio emits", () => {
|
|
314
|
+
const vars = (row: string) => ({ selected: '["sleep","energy"]', "item.value": row });
|
|
315
|
+
|
|
316
|
+
it("flattens a one-member array holding the JSON string", () =>
|
|
317
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "in", value: ["{{selected}}"] }, vars("sleep"))).toBe(true));
|
|
318
|
+
|
|
319
|
+
it("still rejects a non-member through the flattened shape", () =>
|
|
320
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "in", value: ["{{selected}}"] }, vars("focus"))).toBe(false));
|
|
321
|
+
|
|
322
|
+
it("`not_in` is the negation through the flattened shape", () => {
|
|
323
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "not_in", value: ["{{selected}}"] }, vars("energy"))).toBe(false);
|
|
324
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "not_in", value: ["{{selected}}"] }, vars("focus"))).toBe(true);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it("mixes a literal member with a referenced list", () => {
|
|
328
|
+
const value = ["mood", "{{selected}}"];
|
|
329
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "in", value }, vars("mood"))).toBe(true);
|
|
330
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "in", value }, vars("energy"))).toBe(true);
|
|
331
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "in", value }, vars("focus"))).toBe(false);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("an empty value field (Studio yields []) keeps its documented constants", () => {
|
|
335
|
+
expect(evaluateLeaf({ variable: "x", operator: "in", value: [] }, { x: "a" })).toBe(false);
|
|
336
|
+
expect(evaluateLeaf({ variable: "x", operator: "not_in", value: [] }, { x: "a" })).toBe(true);
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
describe("evaluateLeaf — an unresolved reference has no members in EITHER shape", () => {
|
|
341
|
+
// The array-wrapped shape is the one Studio actually emits — its condition
|
|
342
|
+
// editor always splits the value field into an array — so an unresolved
|
|
343
|
+
// reference has to mean "no members" there too. Interpolation resolves it to
|
|
344
|
+
// the empty string, and keeping that as a MEMBER makes an empty-string
|
|
345
|
+
// variable a member of a list nobody has written: `in` true, `not_in` false,
|
|
346
|
+
// the exact opposite of the bare-reference shape on the same data. An
|
|
347
|
+
// empty-string variable is reachable — `InputElement` stores "" on clear and
|
|
348
|
+
// `Input.defaultValue: ""` is overlaid into the variable map.
|
|
349
|
+
it("`in` never matches, even when the variable is the empty string", () =>
|
|
350
|
+
expect(evaluateLeaf({ variable: "name", operator: "in", value: ["{{blocked}}"] }, { name: "" })).toBe(false));
|
|
351
|
+
|
|
352
|
+
it("`not_in` matches every row, even when the variable is the empty string", () =>
|
|
353
|
+
expect(evaluateLeaf({ variable: "name", operator: "not_in", value: ["{{blocked}}"] }, { name: "" })).toBe(true));
|
|
354
|
+
|
|
355
|
+
it("answers the same as the bare-reference shape on the same data", () => {
|
|
356
|
+
const vars = { name: "" };
|
|
357
|
+
expect(evaluateLeaf({ variable: "name", operator: "in", value: ["{{blocked}}"] }, vars)).toBe(
|
|
358
|
+
evaluateLeaf({ variable: "name", operator: "in", value: "{{blocked}}" }, vars)
|
|
359
|
+
);
|
|
360
|
+
expect(evaluateLeaf({ variable: "name", operator: "not_in", value: ["{{blocked}}"] }, vars)).toBe(
|
|
361
|
+
evaluateLeaf({ variable: "name", operator: "not_in", value: "{{blocked}}" }, vars)
|
|
362
|
+
);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("drops only the unresolved member, keeping the resolved ones", () => {
|
|
366
|
+
const value = ["{{blocked}}", "{{selected}}"];
|
|
367
|
+
const vars = (row: string) => ({ selected: '["sleep"]', "item.value": row });
|
|
368
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "in", value }, vars("sleep"))).toBe(true);
|
|
369
|
+
expect(evaluateLeaf({ variable: "item.value", operator: "in", value }, vars(""))).toBe(false);
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
describe("evaluateLeaf — membership compares members stringified", () => {
|
|
374
|
+
// A payload-authored literal array may hold numbers (ConditionValueSchema
|
|
375
|
+
// permits them), and `Repeat` keeps a numeric row field numeric on purpose
|
|
376
|
+
// (`repeatScope.buildRowFlat`), so `1` and `"1"` must be the same member.
|
|
377
|
+
it("a numeric variable is a member of a numeric list", () =>
|
|
378
|
+
expect(evaluateLeaf({ variable: "id", operator: "in", value: [1, 2] }, { id: 1 })).toBe(true));
|
|
379
|
+
|
|
380
|
+
it("`not_in` agrees with `in` on a numeric list", () =>
|
|
381
|
+
expect(evaluateLeaf({ variable: "id", operator: "not_in", value: [1, 2] }, { id: 1 })).toBe(false));
|
|
382
|
+
|
|
383
|
+
it("a numeric variable is not a member of a list it is absent from", () =>
|
|
384
|
+
expect(evaluateLeaf({ variable: "id", operator: "not_in", value: [1, 2] }, { id: 3 })).toBe(true));
|
|
385
|
+
|
|
386
|
+
it("a numeric row field matches a decoded string list", () =>
|
|
387
|
+
expect(evaluateLeaf({ variable: "item.id", operator: "in", value: "{{picked}}" }, { picked: '["1","2"]', "item.id": 1 })).toBe(true));
|
|
388
|
+
|
|
389
|
+
it("`contains` uses the same member comparison on an array variable", () => {
|
|
390
|
+
expect(evaluateLeaf({ variable: "ids", operator: "contains", value: "1" }, { ids: [1, 2] })).toBe(true);
|
|
391
|
+
expect(evaluateLeaf({ variable: "ids", operator: "contains", value: "{{item.id}}" }, { ids: [1, 2], "item.id": 1 })).toBe(true);
|
|
392
|
+
expect(evaluateLeaf({ variable: "ids", operator: "contains", value: "3" }, { ids: [1, 2] })).toBe(false);
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
describe("evaluateLeaf — in / not_in with a right-hand side that is no list at all", () => {
|
|
397
|
+
it("an unresolved reference has no members, so `in` never matches", () =>
|
|
398
|
+
expect(evaluateLeaf({ variable: "x", operator: "in", value: "{{nope}}" }, { x: "sleep" })).toBe(false));
|
|
399
|
+
|
|
400
|
+
it("an unresolved reference has no members, so `not_in` matches every row", () =>
|
|
401
|
+
expect(evaluateLeaf({ variable: "x", operator: "not_in", value: "{{nope}}" }, { x: "sleep" })).toBe(true));
|
|
402
|
+
|
|
403
|
+
it("a scalar right-hand side reads as a one-member list rather than a constant", () => {
|
|
404
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
405
|
+
expect(evaluateLeaf({ variable: "x", operator: "in", value: "male" }, { x: "male" })).toBe(true);
|
|
406
|
+
expect(evaluateLeaf({ variable: "x", operator: "in", value: "male" }, { x: "female" })).toBe(false);
|
|
407
|
+
expect(evaluateLeaf({ variable: "x", operator: "not_in", value: "male" }, { x: "male" })).toBe(false);
|
|
408
|
+
warn.mockRestore();
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it("warns once per evaluation about a scalar right-hand side, naming the operator", () => {
|
|
412
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
413
|
+
evaluateLeaf({ variable: "x", operator: "in", value: "male" }, { x: "male" });
|
|
414
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
415
|
+
expect(String(warn.mock.calls[0]?.[0])).toContain('operator "in"');
|
|
416
|
+
warn.mockRestore();
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it("does not warn when the right-hand side is a real list", () => {
|
|
420
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
421
|
+
evaluateLeaf({ variable: "x", operator: "in", value: ["male"] }, { x: "male" });
|
|
422
|
+
evaluateLeaf({ variable: "x", operator: "not_in", value: "{{sel}}" }, { x: "male", sel: '["male"]' });
|
|
423
|
+
evaluateLeaf({ variable: "x", operator: "in", value: "{{nope}}" }, { x: "male" });
|
|
424
|
+
expect(warn).not.toHaveBeenCalled();
|
|
425
|
+
warn.mockRestore();
|
|
426
|
+
});
|
|
427
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { ScreenElementsSchema } from "../screens/types";
|
|
3
|
+
import { onboardingExample } from "../onboarding-example";
|
|
4
|
+
|
|
5
|
+
// The default onboarding is the payload every example app and the studio's
|
|
6
|
+
// seed data render, and CLAUDE.md's element procedure (step 1) requires keeping
|
|
7
|
+
// it in sync with the schema. Nothing enforced that: a `setVariable` action or
|
|
8
|
+
// element prop added to the example but not to the Zod union would ship a
|
|
9
|
+
// payload the runtime refuses to parse, and the only symptom would be a broken
|
|
10
|
+
// example app. This test closes that hole for the ComposableScreen steps.
|
|
11
|
+
describe("onboarding-example ComposableScreen payloads", () => {
|
|
12
|
+
const composableSteps = onboardingExample.steps.filter(
|
|
13
|
+
(s) => s.type === "ComposableScreen"
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
it("has ComposableScreen steps to check", () => {
|
|
17
|
+
expect(composableSteps.length).toBeGreaterThan(0);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
for (const step of composableSteps) {
|
|
21
|
+
it(`step "${step.name}" validates against ScreenElementsSchema`, () => {
|
|
22
|
+
const result = ScreenElementsSchema.safeParse(step.payload?.elements);
|
|
23
|
+
// Surface the actual zod issues rather than a bare `false`.
|
|
24
|
+
expect(result.success ? [] : result.error.issues).toEqual([]);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { resolveRenderableStep } from "../screens/resolveRenderableStep";
|
|
3
|
+
import { ComposableScreenStepTypeSchema } from "../steps/ComposableScreen/types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The whole render-boundary decision for #209, in one pure function, so the
|
|
7
|
+
* ComposableScreen renderer holds no logic that this package's tests cannot
|
|
8
|
+
* reach (the UI package's vitest runs in plain Node and cannot import
|
|
9
|
+
* `react-native`, so anything decided there is only assertable at source level).
|
|
10
|
+
*
|
|
11
|
+
* Two questions, answered together because the second only has meaning after
|
|
12
|
+
* the first: what should be parsed, and can the user still leave the screen?
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const text = (id: string, content: string) => ({ id, type: "Text", props: { content } });
|
|
16
|
+
const cta = (id: string) => ({
|
|
17
|
+
id,
|
|
18
|
+
type: "Button",
|
|
19
|
+
props: { label: "Continue", actions: ["continue"] },
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const step = (elements: unknown[]) => ({
|
|
23
|
+
id: "s",
|
|
24
|
+
name: "Step one",
|
|
25
|
+
type: "ComposableScreen",
|
|
26
|
+
displayProgressHeader: false,
|
|
27
|
+
payload: { elements },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("resolveRenderableStep", () => {
|
|
31
|
+
it("hands back the same step reference when every element type is known", () => {
|
|
32
|
+
const clean = step([text("t", "hi"), cta("go")]);
|
|
33
|
+
const resolved = resolveRenderableStep(clean);
|
|
34
|
+
expect(resolved.step).toBe(clean);
|
|
35
|
+
expect(resolved.omitted).toEqual([]);
|
|
36
|
+
expect(resolved.needsEscape).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("asks for an escape when the strip takes the screen's only way forward", () => {
|
|
40
|
+
// The reviewer's reproduction, and the shape the shipped example payload
|
|
41
|
+
// itself uses: ONE root container holding everything, including the CTA. If
|
|
42
|
+
// that container is a type this build does not know, the strip empties the
|
|
43
|
+
// screen — `[]` parses cleanly, so nothing throws and nothing renders.
|
|
44
|
+
const trapped = step([
|
|
45
|
+
{ id: "root", type: "HolographicCard", props: {}, children: [text("t", "hi"), cta("go")] },
|
|
46
|
+
]);
|
|
47
|
+
const resolved = resolveRenderableStep(trapped);
|
|
48
|
+
|
|
49
|
+
const parsed = ComposableScreenStepTypeSchema.parse(resolved.step);
|
|
50
|
+
expect(parsed.payload.elements).toEqual([]);
|
|
51
|
+
expect(resolved.needsEscape).toBe(true);
|
|
52
|
+
expect(resolved.omitted.map((o) => o.elementType)).toEqual(["HolographicCard"]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("needs no escape when a CTA survives the strip", () => {
|
|
56
|
+
const resolved = resolveRenderableStep(
|
|
57
|
+
step([{ id: "x", type: "HolographicCard", props: {} }, cta("go")])
|
|
58
|
+
);
|
|
59
|
+
expect(resolved.needsEscape).toBe(false);
|
|
60
|
+
expect(resolved.omitted).toHaveLength(1);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("needs an escape when the surviving tree has content but nothing pressable", () => {
|
|
64
|
+
const resolved = resolveRenderableStep(
|
|
65
|
+
step([text("t", "hi"), { id: "x", type: "TimeOfDayPicker", props: {}, children: [cta("go")] }])
|
|
66
|
+
);
|
|
67
|
+
expect(resolved.needsEscape).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("leaves an authored screen with no CTA alone — nothing was stripped, so nothing is ours to fix", () => {
|
|
71
|
+
// A screen the author gave no way forward is an authoring bug, and the host
|
|
72
|
+
// may well advance it some other way (a custom action that navigates, a
|
|
73
|
+
// timer). Injecting a CTA there would change shipped screens that have
|
|
74
|
+
// nothing to do with an unknown element type.
|
|
75
|
+
const authored = step([text("t", "hi")]);
|
|
76
|
+
const resolved = resolveRenderableStep(authored);
|
|
77
|
+
expect(resolved.step).toBe(authored);
|
|
78
|
+
expect(resolved.needsEscape).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("passes a step with no element payload straight through", () => {
|
|
82
|
+
const ratings = { id: "r", type: "Ratings", payload: { title: "x" } };
|
|
83
|
+
const resolved = resolveRenderableStep(ratings);
|
|
84
|
+
expect(resolved.step).toBe(ratings);
|
|
85
|
+
expect(resolved.needsEscape).toBe(false);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("strips nothing and asks for nothing when it cannot tell which types are known", () => {
|
|
89
|
+
// An empty capability set means the derivation failed, which must read as
|
|
90
|
+
// "learn nothing" — never as "reject everything", which would blank every
|
|
91
|
+
// screen in every app and then cover each one with a fallback CTA.
|
|
92
|
+
const trapped = step([
|
|
93
|
+
{ id: "root", type: "HolographicCard", props: {}, children: [cta("go")] },
|
|
94
|
+
]);
|
|
95
|
+
const resolved = resolveRenderableStep(trapped, new Set<string>());
|
|
96
|
+
expect(resolved.step).toBe(trapped);
|
|
97
|
+
expect(resolved.omitted).toEqual([]);
|
|
98
|
+
expect(resolved.needsEscape).toBe(false);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("keys off the capability set it is given, not the schema it was built from", () => {
|
|
102
|
+
// What renders is the onboarding-ui package's own re-declared union, whose
|
|
103
|
+
// installed version can differ from this one's. The caller supplies the set;
|
|
104
|
+
// here `Button` is deliberately absent from it and must be stripped.
|
|
105
|
+
const resolved = resolveRenderableStep(step([text("t", "hi"), cta("go")]), new Set(["Text"]));
|
|
106
|
+
const parsed = ComposableScreenStepTypeSchema.parse(resolved.step);
|
|
107
|
+
expect(parsed.payload.elements.map((e) => e.id)).toEqual(["t"]);
|
|
108
|
+
expect(resolved.needsEscape).toBe(true);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("does not mutate the step it was given", () => {
|
|
112
|
+
const trapped = step([{ id: "x", type: "HolographicCard", props: {} }, cta("go")]);
|
|
113
|
+
const before = JSON.stringify(trapped);
|
|
114
|
+
resolveRenderableStep(trapped);
|
|
115
|
+
expect(JSON.stringify(trapped)).toBe(before);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
KNOWN_ELEMENT_TYPES,
|
|
4
|
+
collectUnknownElementTypes,
|
|
5
|
+
collectUnknownElementTypesInSteps,
|
|
6
|
+
dropUnknownElementTypes,
|
|
7
|
+
dropUnknownElementTypesInStep,
|
|
8
|
+
formatUnknownElementTypes,
|
|
9
|
+
} from "../screens/unknownElementTypes";
|
|
10
|
+
import { ScreenElementsSchema } from "../screens/types";
|
|
11
|
+
import { ComposableScreenStepTypeSchema } from "../steps/ComposableScreen/types";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Forward compatibility for element TYPES (#209).
|
|
15
|
+
*
|
|
16
|
+
* `UIElementSchema` is a discriminated union of the types this SDK build knows.
|
|
17
|
+
* Before this, one element type published after an app shipped made the WHOLE
|
|
18
|
+
* screen fail to parse — and the ComposableScreen renderer's `.parse` threw
|
|
19
|
+
* inside its error boundary, whose fallback has no interactive control, so on a
|
|
20
|
+
* step with `displayProgressHeader: false` the user had no way forward OR back.
|
|
21
|
+
*
|
|
22
|
+
* The contract asserted here: an unrecognized element type is OMITTED (matching
|
|
23
|
+
* `renderElement`'s terminal `return null` and `buildAnimation`'s unknown-preset
|
|
24
|
+
* no-op), the rest of the screen renders, and a genuine data error on a KNOWN
|
|
25
|
+
* type still fails loudly.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const text = (id: string, content: string) => ({ id, type: "Text", props: { content } });
|
|
29
|
+
const stack = (id: string, children: unknown[]) => ({ id, type: "YStack", props: {}, children });
|
|
30
|
+
const unknown = (id: string, type = "HolographicCard") => ({ id, type, props: { magic: true } });
|
|
31
|
+
|
|
32
|
+
const step = (elements: unknown[]) => ({
|
|
33
|
+
id: "step-1",
|
|
34
|
+
name: "Step one",
|
|
35
|
+
type: "ComposableScreen",
|
|
36
|
+
displayProgressHeader: false,
|
|
37
|
+
payload: { elements },
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("dropUnknownElementTypes", () => {
|
|
41
|
+
it("keeps the rest of the screen when a top-level element type is unknown", () => {
|
|
42
|
+
const parsed = ScreenElementsSchema.safeParse(
|
|
43
|
+
dropUnknownElementTypes([text("a", "before"), unknown("x"), text("b", "after")])
|
|
44
|
+
);
|
|
45
|
+
expect(parsed.success).toBe(true);
|
|
46
|
+
if (!parsed.success) return;
|
|
47
|
+
expect(parsed.data.map((e) => e.id)).toEqual(["a", "b"]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("keeps a container's known children when one child type is unknown", () => {
|
|
51
|
+
const parsed = ScreenElementsSchema.safeParse(
|
|
52
|
+
dropUnknownElementTypes([stack("root", [text("a", "hi"), unknown("x")])])
|
|
53
|
+
);
|
|
54
|
+
expect(parsed.success).toBe(true);
|
|
55
|
+
if (!parsed.success) return;
|
|
56
|
+
const [root] = parsed.data;
|
|
57
|
+
expect(root.type).toBe("YStack");
|
|
58
|
+
expect("children" in root ? root.children.map((c) => c.id) : []).toEqual(["a"]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("drops the whole subtree of an unknown element, never hoisting its children", () => {
|
|
62
|
+
// Matches the render half: `renderElement` returns null for an unrecognized
|
|
63
|
+
// type, so its children never render either. Hoisting them would invent a
|
|
64
|
+
// layout the author never wrote.
|
|
65
|
+
const parsed = ScreenElementsSchema.safeParse(
|
|
66
|
+
dropUnknownElementTypes([{ ...unknown("x"), children: [text("inner", "hidden")] }])
|
|
67
|
+
);
|
|
68
|
+
expect(parsed.success).toBe(true);
|
|
69
|
+
if (!parsed.success) return;
|
|
70
|
+
expect(parsed.data).toEqual([]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("drops an unknown type nested in a RichText's Text-only children", () => {
|
|
74
|
+
const parsed = ScreenElementsSchema.safeParse(
|
|
75
|
+
dropUnknownElementTypes([
|
|
76
|
+
{ id: "rt", type: "RichText", props: {}, children: [text("w", "word"), unknown("x")] },
|
|
77
|
+
])
|
|
78
|
+
);
|
|
79
|
+
expect(parsed.success).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("still fails loudly on a bad prop of a KNOWN element type", () => {
|
|
83
|
+
// The forward-compat escape must not become a silence-everything escape:
|
|
84
|
+
// `variant: "plain"` is a CMS data bug the author has to fix.
|
|
85
|
+
const bad = { id: "b", type: "Button", props: { label: "Go", variant: "plain" } };
|
|
86
|
+
const parsed = ScreenElementsSchema.safeParse(dropUnknownElementTypes([bad]));
|
|
87
|
+
expect(parsed.success).toBe(false);
|
|
88
|
+
if (parsed.success) return;
|
|
89
|
+
expect(parsed.error.issues.some((i) => i.path.join(".").endsWith("props.variant"))).toBe(true);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("still fails loudly on a KNOWN element type missing its `id`", () => {
|
|
93
|
+
const parsed = ScreenElementsSchema.safeParse(
|
|
94
|
+
dropUnknownElementTypes([{ type: "Text", props: { content: "x" } }])
|
|
95
|
+
);
|
|
96
|
+
expect(parsed.success).toBe(false);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("leaves an element with no `type` for the parser to report", () => {
|
|
100
|
+
// A missing/non-string discriminator is a malformed payload, not a newer
|
|
101
|
+
// element — dropping it would hide the real error.
|
|
102
|
+
const parsed = ScreenElementsSchema.safeParse(dropUnknownElementTypes([{ id: "a", props: {} }]));
|
|
103
|
+
expect(parsed.success).toBe(false);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("returns the input unchanged when it is not an array", () => {
|
|
107
|
+
expect(dropUnknownElementTypes(undefined)).toBeUndefined();
|
|
108
|
+
expect(dropUnknownElementTypes("nope")).toBe("nope");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("does not mutate the payload it was given", () => {
|
|
112
|
+
const elements = [stack("root", [text("a", "hi"), unknown("x")])];
|
|
113
|
+
const before = JSON.stringify(elements);
|
|
114
|
+
dropUnknownElementTypes(elements);
|
|
115
|
+
expect(JSON.stringify(elements)).toBe(before);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("keeps every element when the type registry cannot be built", () => {
|
|
119
|
+
// The registry is derived from zod's internals, so a future zod release can
|
|
120
|
+
// break it. It must degrade to "everything is known" (parse as today), never
|
|
121
|
+
// to "nothing is known" — which would blank every screen in every app.
|
|
122
|
+
const elements = [text("a", "hi"), unknown("x")];
|
|
123
|
+
expect(dropUnknownElementTypes(elements, new Set<string>())).toBe(elements);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("returns the same array reference when nothing is unknown", () => {
|
|
127
|
+
// Structural sharing: the strip runs on every screen render, so a clean
|
|
128
|
+
// payload must not be cloned (and must stay `useMemo`-stable).
|
|
129
|
+
const elements = [stack("root", [text("a", "hi")])];
|
|
130
|
+
expect(dropUnknownElementTypes(elements)).toBe(elements);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("dropUnknownElementTypesInStep", () => {
|
|
135
|
+
it("makes the ComposableScreen step parse instead of throwing", () => {
|
|
136
|
+
// The exact call at ComposableScreen/Renderer.tsx: `.parse(step)`.
|
|
137
|
+
expect(() => ComposableScreenStepTypeSchema.parse(step([unknown("x")]))).toThrow();
|
|
138
|
+
const parsed = ComposableScreenStepTypeSchema.parse(
|
|
139
|
+
dropUnknownElementTypesInStep(step([text("a", "hi"), unknown("x")]))
|
|
140
|
+
);
|
|
141
|
+
expect(parsed.payload.elements.map((e) => e.id)).toEqual(["a"]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("passes a step through untouched when it carries no element payload", () => {
|
|
145
|
+
const ratings = { id: "r", type: "Ratings", payload: { title: "x" } };
|
|
146
|
+
expect(dropUnknownElementTypesInStep(ratings)).toBe(ratings);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("returns the same step reference when every element type is known", () => {
|
|
150
|
+
const clean = step([text("a", "hi")]);
|
|
151
|
+
expect(dropUnknownElementTypesInStep(clean)).toBe(clean);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe("collectUnknownElementTypes", () => {
|
|
156
|
+
it("reports the path, id and type of each unknown element", () => {
|
|
157
|
+
const found = collectUnknownElementTypes([
|
|
158
|
+
text("a", "hi"),
|
|
159
|
+
unknown("x", "HolographicCard"),
|
|
160
|
+
stack("root", [unknown("y", "TimeOfDayPicker")]),
|
|
161
|
+
]);
|
|
162
|
+
expect(found).toEqual([
|
|
163
|
+
{ path: "elements[1]", elementId: "x", elementType: "HolographicCard" },
|
|
164
|
+
{ path: "elements[2].children[0]", elementId: "y", elementType: "TimeOfDayPicker" },
|
|
165
|
+
]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("reports an unknown parent once, not its dropped descendants", () => {
|
|
169
|
+
const found = collectUnknownElementTypes([
|
|
170
|
+
{ ...unknown("x"), children: [unknown("y", "AlsoUnknown")] },
|
|
171
|
+
]);
|
|
172
|
+
expect(found).toHaveLength(1);
|
|
173
|
+
expect(found[0].elementId).toBe("x");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("says nothing about a tree of known types", () => {
|
|
177
|
+
expect(collectUnknownElementTypes([stack("root", [text("a", "hi")])])).toEqual([]);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("walks every step carrying an element payload", () => {
|
|
181
|
+
const found = collectUnknownElementTypesInSteps([
|
|
182
|
+
{ id: "s1", type: "Ratings", payload: {} },
|
|
183
|
+
step([unknown("x")]),
|
|
184
|
+
]);
|
|
185
|
+
expect(found).toHaveLength(1);
|
|
186
|
+
expect(found[0].path).toContain("step-1");
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("formatUnknownElementTypes", () => {
|
|
191
|
+
it("says nothing when there is nothing to say", () => {
|
|
192
|
+
expect(formatUnknownElementTypes([])).toBe("");
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("names the type, the path and the omit contract", () => {
|
|
196
|
+
const report = formatUnknownElementTypes(
|
|
197
|
+
collectUnknownElementTypes([unknown("x", "HolographicCard")])
|
|
198
|
+
);
|
|
199
|
+
expect(report).toContain("HolographicCard");
|
|
200
|
+
expect(report).toContain("elements[0]");
|
|
201
|
+
expect(report).toMatch(/omitted/i);
|
|
202
|
+
expect(report).toMatch(/upgrade|newer|version/i);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe("KNOWN_ELEMENT_TYPES", () => {
|
|
207
|
+
it("is derived from the schema, so it cannot drift as elements are added", () => {
|
|
208
|
+
// Every variant of the discriminated union, and nothing else.
|
|
209
|
+
expect(KNOWN_ELEMENT_TYPES).toContain("YStack");
|
|
210
|
+
expect(KNOWN_ELEMENT_TYPES).toContain("Slider");
|
|
211
|
+
expect(KNOWN_ELEMENT_TYPES).not.toContain("HolographicCard");
|
|
212
|
+
// No known type may be rejected on its own discriminator. Other issues are
|
|
213
|
+
// expected and fine: `props: {}` is incomplete for most variants.
|
|
214
|
+
const rejectedOnDiscriminator = KNOWN_ELEMENT_TYPES.filter((type) => {
|
|
215
|
+
const parsed = ScreenElementsSchema.safeParse([{ id: "a", type, props: {} }]);
|
|
216
|
+
return (
|
|
217
|
+
!parsed.success && parsed.error.issues.some((i) => i.path.join(".") === "0.type")
|
|
218
|
+
);
|
|
219
|
+
});
|
|
220
|
+
expect(rejectedOnDiscriminator).toEqual([]);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("is sorted and free of duplicates, so it is usable as a capability list", () => {
|
|
224
|
+
expect([...KNOWN_ELEMENT_TYPES]).toEqual([...new Set(KNOWN_ELEMENT_TYPES)].sort());
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe("no backtracking is reintroduced", () => {
|
|
229
|
+
it("strips a deep tree and still fails fast on a pathological payload", () => {
|
|
230
|
+
// Canary for the discriminated union (screens/types.ts:296-348): a plain
|
|
231
|
+
// union of these recursive variants exhausted a 512 MB heap on a real
|
|
232
|
+
// 52-node paywall. The strip must stay a linear walk in front of it.
|
|
233
|
+
const nest = (depth: number, leaf: unknown): unknown =>
|
|
234
|
+
depth === 0 ? leaf : { name: "c", type: "YStack", props: {}, children: [nest(depth - 1, leaf)] };
|
|
235
|
+
const started = Date.now();
|
|
236
|
+
const parsed = ScreenElementsSchema.safeParse(dropUnknownElementTypes([nest(10, text("l", "x"))]));
|
|
237
|
+
expect(parsed.success).toBe(false);
|
|
238
|
+
expect(Date.now() - started).toBeLessThan(2000);
|
|
239
|
+
});
|
|
240
|
+
});
|