|
| 1 | +// #30259 — `ActionParamSchema.widget`: an action param can name the widget that |
| 2 | +// draws it, with the same key, vocabulary and meaning as `FieldSchema.widget`. |
| 3 | +// The case that asked for it is the permission-set picker ADR-0131 D4 gives the |
| 4 | +// grant column `sys_user_permission_set.permission_set` (`widget: |
| 5 | +// 'permission-ref'`): before this key no param could ask for that picker, so |
| 6 | +// Create User's `permissionSet` param was a box for a typed machine name |
| 7 | +// (#30117). |
| 8 | +// |
| 9 | +// These pins hold the widening: a param that declares the key parses and keeps |
| 10 | +// it; an unregistered name is KEPT, exactly as the field key keeps it (both |
| 11 | +// degrade to the `type` renderer at draw time); a non-string is refused; and a |
| 12 | +// param that does not declare the key parses exactly as it did before. |
| 13 | +import { describe, it, expect } from 'vitest'; |
| 14 | +import { ActionParamSchema } from './action.zod'; |
| 15 | +import { FieldSchema } from '../data/field.zod'; |
| 16 | + |
| 17 | +describe('ActionParamSchema.widget — a param names the widget that draws it (#30259)', () => { |
| 18 | + it('accepts permission-ref on an inline text param and keeps it, with `type` unchanged', () => { |
| 19 | + const r = ActionParamSchema.safeParse({ |
| 20 | + name: 'permissionSet', |
| 21 | + label: 'Permission Set', |
| 22 | + type: 'text', |
| 23 | + widget: 'permission-ref', |
| 24 | + }); |
| 25 | + expect(r.success, JSON.stringify((r as { error?: unknown }).error)).toBe(true); |
| 26 | + if (!r.success) return; |
| 27 | + expect(r.data.widget).toBe('permission-ref'); |
| 28 | + // `type` stays the VALUE's type: the widget changes how it is picked only. |
| 29 | + expect(r.data.type).toBe('text'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('accepts the key on a field-backed param (an authored override, as every inline value is)', () => { |
| 33 | + const r = ActionParamSchema.safeParse({ field: 'permission_set', widget: 'permission-ref' }); |
| 34 | + expect(r.success, JSON.stringify((r as { error?: unknown }).error)).toBe(true); |
| 35 | + if (!r.success) return; |
| 36 | + expect(r.data.widget).toBe('permission-ref'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('is a declared key, not an unrecognized one', () => { |
| 40 | + const r = ActionParamSchema.safeParse({ name: 'p', type: 'text', widget: 'permission-ref' }); |
| 41 | + const issues = r.success ? [] : r.error.issues; |
| 42 | + expect(issues.filter((i) => i.code === 'unrecognized_keys')).toEqual([]); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('value — an open string, matching FieldSchema.widget', () => { |
| 46 | + // The field key is `z.string().optional()`: an unregistered name is kept and |
| 47 | + // the renderer falls back to the `type` default. The param key makes the same |
| 48 | + // promise, so each value below must get the same verdict on both surfaces. |
| 49 | + const values: unknown[] = ['permission-ref', 'position-ref', 'object-ref', 'no-such-widget', 42, true, null]; |
| 50 | + |
| 51 | + for (const value of values) { |
| 52 | + it(`gives ${JSON.stringify(value)} the verdict the field key gives it`, () => { |
| 53 | + const onParam = ActionParamSchema.safeParse({ name: 'p', type: 'text', widget: value }); |
| 54 | + const onField = FieldSchema.safeParse({ name: 'p', type: 'text', widget: value }); |
| 55 | + expect(onParam.success).toBe(onField.success); |
| 56 | + if (onParam.success && onField.success) { |
| 57 | + expect(onParam.data.widget).toBe(value); |
| 58 | + expect(onField.data.widget).toBe(value); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + it('keeps an unregistered name rather than refusing it (it degrades at draw time)', () => { |
| 64 | + const r = ActionParamSchema.safeParse({ name: 'p', type: 'text', widget: 'no-such-widget' }); |
| 65 | + expect(r.success).toBe(true); |
| 66 | + if (!r.success) return; |
| 67 | + expect(r.data.widget).toBe('no-such-widget'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('refuses a non-string on the widget path', () => { |
| 71 | + const r = ActionParamSchema.safeParse({ name: 'p', type: 'text', widget: 42 }); |
| 72 | + expect(r.success).toBe(false); |
| 73 | + if (r.success) return; |
| 74 | + expect(r.error.issues.some((i) => i.path.join('.') === 'widget' && i.code === 'invalid_type')).toBe(true); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('existing params parse unchanged', () => { |
| 79 | + const existing: Array<Record<string, unknown>> = [ |
| 80 | + // Create User's param as it stands on main (sys-user.object.ts): inline text. |
| 81 | + { name: 'permissionSet', label: 'Permission Set', type: 'text', required: false }, |
| 82 | + { field: 'email' }, |
| 83 | + { field: 'role', objectOverride: 'sys_member' }, |
| 84 | + { name: 'inspector', label: 'Inspector', type: 'lookup', reference: 'sys_user' }, |
| 85 | + { name: 'tier', type: 'select', options: [{ label: 'Gold', value: 'gold' }] }, |
| 86 | + ]; |
| 87 | + |
| 88 | + for (const param of existing) { |
| 89 | + it(`parses ${JSON.stringify(param)} with no widget member added`, () => { |
| 90 | + const r = ActionParamSchema.safeParse(param); |
| 91 | + expect(r.success, JSON.stringify((r as { error?: unknown }).error)).toBe(true); |
| 92 | + if (!r.success) return; |
| 93 | + expect('widget' in r.data).toBe(false); |
| 94 | + }); |
| 95 | + } |
| 96 | + }); |
| 97 | +}); |
0 commit comments