|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The read-only record-link screen field (`type: 'record_link'`), ruling B on |
| 5 | + * the screen-flow result screen. |
| 6 | + * |
| 7 | + * A screen field that SHOWS a record the run holds the id of and submits |
| 8 | + * nothing. The contract is a conditional refinement over the open `type` hint: |
| 9 | + * on this member `reference` and `defaultValue` are required, and the keys that |
| 10 | + * describe an input (`required`, `options`, `placeholder`, `min`, `max`) are |
| 11 | + * refused, each at its own key. Every other member is untouched. |
| 12 | + */ |
| 13 | + |
| 14 | +import { describe, expect, it } from 'vitest'; |
| 15 | + |
| 16 | +import { |
| 17 | + SCREEN_FIELD_RECORD_LINK_DEFAULT_VALUE_REQUIRED, |
| 18 | + SCREEN_FIELD_RECORD_LINK_REFERENCE_REQUIRED, |
| 19 | + SCREEN_FIELD_RECORD_LINK_REFUSED_KEYS, |
| 20 | + SCREEN_FIELD_RECORD_LINK_TYPE, |
| 21 | + ScreenConfigSchema, |
| 22 | + ScreenFieldConfigSchema, |
| 23 | + screenFieldRecordLinkRefusedKey, |
| 24 | +} from './builtin-node-config.zod.js'; |
| 25 | + |
| 26 | +const LINK = { |
| 27 | + name: 'created_account', |
| 28 | + label: 'Account', |
| 29 | + type: 'record_link', |
| 30 | + reference: 'crm_account', |
| 31 | + defaultValue: { dialect: 'cel', source: 'account_id' }, |
| 32 | +} as const; |
| 33 | + |
| 34 | +/** The issues a parse addressed to `key`, or `[]` when it parsed. */ |
| 35 | +function issuesAt(value: unknown, key: string) { |
| 36 | + const r = ScreenFieldConfigSchema.safeParse(value); |
| 37 | + return r.success ? [] : r.error.issues.filter((i) => i.path[0] === key); |
| 38 | +} |
| 39 | + |
| 40 | +describe("screen field `type: 'record_link'` — a read-only link to a record", () => { |
| 41 | + it('is spelled `record_link`, not the `record` the object field vocabulary already uses', () => { |
| 42 | + expect(SCREEN_FIELD_RECORD_LINK_TYPE).toBe('record_link'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('accepts the declared shape — a CEL envelope id, a literal id, and the presentation keys', () => { |
| 46 | + const accepted = [ |
| 47 | + LINK, |
| 48 | + { ...LINK, defaultValue: 'acc_0001' }, |
| 49 | + { ...LINK, inlineHelpText: 'Opens the account the conversion created.' }, |
| 50 | + { ...LINK, visibleWhen: 'create_opportunity == true' }, |
| 51 | + ]; |
| 52 | + for (const value of accepted) { |
| 53 | + const r = ScreenFieldConfigSchema.safeParse(value); |
| 54 | + expect(r.success, JSON.stringify(r.success ? null : r.error.issues)).toBe(true); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + it('refuses a missing or blank `reference`, addressed to the key', () => { |
| 59 | + for (const reference of [undefined, '', ' ']) { |
| 60 | + const value = { ...LINK, reference }; |
| 61 | + const issues = issuesAt(value, 'reference'); |
| 62 | + expect(issues.map((i) => i.message), `reference: ${JSON.stringify(reference)}`) |
| 63 | + .toEqual([SCREEN_FIELD_RECORD_LINK_REFERENCE_REQUIRED]); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + it('refuses a missing or null `defaultValue`, addressed to the key', () => { |
| 68 | + const { defaultValue: _omit, ...withoutDefault } = LINK; |
| 69 | + for (const value of [withoutDefault, { ...LINK, defaultValue: null }]) { |
| 70 | + expect(issuesAt(value, 'defaultValue').map((i) => i.message)) |
| 71 | + .toEqual([SCREEN_FIELD_RECORD_LINK_DEFAULT_VALUE_REQUIRED]); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it('refuses every input key at that key — `required: false` included, since the key itself is the error', () => { |
| 76 | + expect([...SCREEN_FIELD_RECORD_LINK_REFUSED_KEYS]).toEqual(['required', 'options', 'placeholder', 'min', 'max']); |
| 77 | + const values: Record<(typeof SCREEN_FIELD_RECORD_LINK_REFUSED_KEYS)[number], unknown[]> = { |
| 78 | + required: [true, false], |
| 79 | + options: [[{ value: 'a', label: 'A' }]], |
| 80 | + placeholder: ['Pick one'], |
| 81 | + min: [0], |
| 82 | + max: [10], |
| 83 | + }; |
| 84 | + for (const key of SCREEN_FIELD_RECORD_LINK_REFUSED_KEYS) { |
| 85 | + for (const v of values[key]) { |
| 86 | + const issues = issuesAt({ ...LINK, [key]: v }, key); |
| 87 | + expect(issues.map((i) => i.message), `${key}: ${JSON.stringify(v)}`) |
| 88 | + .toEqual([screenFieldRecordLinkRefusedKey(key)]); |
| 89 | + expect(issues[0]!.code).toBe('custom'); |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('reports every broken rule of one field at once', () => { |
| 95 | + const r = ScreenFieldConfigSchema.safeParse({ name: 'x', type: 'record_link', required: true, placeholder: 'p' }); |
| 96 | + expect(r.success).toBe(false); |
| 97 | + expect(r.error!.issues.map((i) => i.path[0]).sort()).toEqual(['defaultValue', 'placeholder', 'reference', 'required']); |
| 98 | + }); |
| 99 | + |
| 100 | + it('is refused through the screen node config too, at the field', () => { |
| 101 | + const r = ScreenConfigSchema.safeParse({ fields: [{ ...LINK, required: true }] }); |
| 102 | + expect(r.success).toBe(false); |
| 103 | + expect(r.error!.issues.some((i) => i.path.join('.') === 'fields.0.required')).toBe(true); |
| 104 | + expect(ScreenConfigSchema.safeParse({ fields: [LINK] }).success).toBe(true); |
| 105 | + }); |
| 106 | + |
| 107 | + it('leaves every other widget hint alone — the input keys still parse on them', () => { |
| 108 | + for (const type of [undefined, 'text', 'number', 'select', 'record']) { |
| 109 | + const r = ScreenFieldConfigSchema.safeParse({ |
| 110 | + name: 'x', ...(type ? { type } : {}), required: true, placeholder: 'p', min: 0, max: 1, |
| 111 | + options: [{ value: 'a', label: 'A' }], |
| 112 | + }); |
| 113 | + expect(r.success, `type: ${String(type)}`).toBe(true); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + it('prints no tracker id at the author, and names the key it refuses', () => { |
| 118 | + const texts = [ |
| 119 | + SCREEN_FIELD_RECORD_LINK_REFERENCE_REQUIRED, |
| 120 | + SCREEN_FIELD_RECORD_LINK_DEFAULT_VALUE_REQUIRED, |
| 121 | + ...SCREEN_FIELD_RECORD_LINK_REFUSED_KEYS.map(screenFieldRecordLinkRefusedKey), |
| 122 | + ]; |
| 123 | + for (const text of texts) expect(text).not.toMatch(/#\d{3,5}\b/); |
| 124 | + expect(SCREEN_FIELD_RECORD_LINK_REFERENCE_REQUIRED).toContain('`reference`'); |
| 125 | + expect(SCREEN_FIELD_RECORD_LINK_DEFAULT_VALUE_REQUIRED).toContain('`defaultValue`'); |
| 126 | + expect(screenFieldRecordLinkRefusedKey('min')).toContain('`min`'); |
| 127 | + }); |
| 128 | +}); |
0 commit comments