-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathresolveActionParams.test.ts
More file actions
195 lines (176 loc) · 7.29 KB
/
Copy pathresolveActionParams.test.ts
File metadata and controls
195 lines (176 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* resolveActionParams — regression coverage for `visible`-predicate propagation.
*
* The create-user phone bug (framework #2871 + objectui #2406) hid the
* `phoneNumber` param via `visible: 'features.phoneNumber == true'`, but the
* resolver dropped `visible` on the way to `ActionParamDialog`, so the field
* kept rendering. These tests lock in that the predicate survives resolution —
* in both the raw-string and the spec-normalised `{ dialect, source }` envelope
* forms, and across inline / field-backed / missing-field branches.
*/
import { describe, it, expect } from 'vitest';
import { resolveActionParams, type ResolveActionParamsContext, type RawActionParam } from './resolveActionParams';
const ctx = (over: Partial<ResolveActionParamsContext> = {}): ResolveActionParamsContext => ({
objectName: 'sys_user',
objects: [
{ name: 'sys_user', fields: { phone_number: { type: 'text', label: 'Phone' } } },
],
fieldLabel: (_o, _f, fallback) => fallback,
...over,
});
describe('resolveActionParams — visible propagation', () => {
it('propagates a raw-string visible on an inline param', () => {
const params: RawActionParam[] = [{ name: 'phoneNumber', visible: 'features.phoneNumber == true' }];
expect(resolveActionParams(params, ctx())[0].visible).toBe('features.phoneNumber == true');
});
it('unwraps the { dialect, source } envelope the spec serialises to', () => {
const params: RawActionParam[] = [
{ name: 'phoneNumber', visible: { dialect: 'cel', source: 'features.phoneNumber == true' } },
];
expect(resolveActionParams(params, ctx())[0].visible).toBe('features.phoneNumber == true');
});
it('propagates visible on a field-backed param', () => {
const params: RawActionParam[] = [{ field: 'phone_number', visible: 'features.phoneNumber == true' }];
expect(resolveActionParams(params, ctx())[0].visible).toBe('features.phoneNumber == true');
});
it('propagates visible on the missing-field fallback branch', () => {
const params: RawActionParam[] = [{ field: 'does_not_exist', visible: 'features.phoneNumber == true' }];
// No such field on the object → text-input fallback, but visible still survives.
expect(resolveActionParams(params, ctx())[0].visible).toBe('features.phoneNumber == true');
});
it('leaves visible undefined when the param has no predicate', () => {
const params: RawActionParam[] = [{ name: 'email' }];
expect(resolveActionParams(params, ctx())[0].visible).toBeUndefined();
});
it('treats an empty predicate as absent (always visible)', () => {
const params: RawActionParam[] = [
{ name: 'a', visible: '' },
{ name: 'b', visible: { dialect: 'cel', source: '' } },
];
const out = resolveActionParams(params, ctx());
expect(out[0].visible).toBeUndefined();
expect(out[1].visible).toBeUndefined();
});
});
describe('resolveActionParams — widget config (ADR-0059)', () => {
const fileCtx = () =>
ctx({
objects: [
{
name: 'sys_user',
fields: {
avatar_file: {
type: 'file',
label: 'Avatar',
multiple: true,
accept: ['image/*'],
maxSize: 1024,
},
phone_number: { type: 'text', label: 'Phone' },
},
},
],
});
it('carries multiple/accept/maxSize on an inline param', () => {
const params: RawActionParam[] = [
{ name: 'attachments', type: 'file', multiple: true, accept: ['application/pdf'], maxSize: 2048 },
];
expect(resolveActionParams(params, ctx())[0]).toMatchObject({
type: 'file',
multiple: true,
accept: ['application/pdf'],
maxSize: 2048,
});
});
it('inherits multiple/accept/maxSize from the referenced field (any type, not just lookup)', () => {
const params: RawActionParam[] = [{ field: 'avatar_file' }];
expect(resolveActionParams(params, fileCtx())[0]).toMatchObject({
type: 'file',
multiple: true,
accept: ['image/*'],
maxSize: 1024,
});
});
it('inline overrides win over the field metadata', () => {
const params: RawActionParam[] = [{ field: 'avatar_file', multiple: false, maxSize: 4096 }];
expect(resolveActionParams(params, fileCtx())[0]).toMatchObject({
multiple: false,
accept: ['image/*'],
maxSize: 4096,
});
});
it('carries multiple/accept/maxSize on the missing-field fallback branch', () => {
const params: RawActionParam[] = [
{ field: 'does_not_exist', type: 'file', multiple: true, accept: ['.csv'], maxSize: 99 },
];
expect(resolveActionParams(params, ctx())[0]).toMatchObject({
multiple: true,
accept: ['.csv'],
maxSize: 99,
});
});
});
/**
* #3405 — an inline `lookup` param carries its own picker target.
*
* Real-world break (PLAT-DEF-005): a QC dispatch action declared
* `{ name: 'inspector', type: 'lookup', reference: 'sys_user' }`. The key was
* unknown to both the spec schema and this resolver, so it was dropped twice
* over and `paramToField()` degraded the param to a "paste the record id
* (UUID)" text box — a supervisor had to go find a user's UUID by hand.
*/
describe('resolveActionParams — inline lookup reference target (#3405)', () => {
const lookupCtx = () =>
ctx({
objects: [
{
name: 'quality_dispatch',
fields: {
inspector: { type: 'lookup', label: '质检人', reference: 'sys_user' },
reviewer: { type: 'lookup', label: 'Reviewer', reference_to: 'sys_user', display_field: 'name' },
},
},
],
objectName: 'quality_dispatch',
});
it('maps an inline `reference` onto referenceTo so the picker can query', () => {
const params: RawActionParam[] = [
{ name: 'inspector', label: '质检员', type: 'lookup', reference: 'sys_user', required: true },
];
expect(resolveActionParams(params, lookupCtx())[0]).toMatchObject({
name: 'inspector',
type: 'lookup',
referenceTo: 'sys_user',
});
});
it('still inherits the target from the referenced field when field-backed', () => {
expect(resolveActionParams([{ field: 'inspector' }], lookupCtx())[0]).toMatchObject({
type: 'lookup',
referenceTo: 'sys_user',
});
expect(resolveActionParams([{ field: 'reviewer' }], lookupCtx())[0]).toMatchObject({
referenceTo: 'sys_user',
displayField: 'name',
});
});
it('lets an inline reference override the field metadata', () => {
const params: RawActionParam[] = [{ field: 'inspector', reference: 'sys_member' }];
expect(resolveActionParams(params, lookupCtx())[0].referenceTo).toBe('sys_member');
});
it('keeps the inline reference on the missing-field fallback branch', () => {
const params: RawActionParam[] = [
{ field: 'does_not_exist', type: 'lookup', reference: 'sys_user' },
];
expect(resolveActionParams(params, lookupCtx())[0].referenceTo).toBe('sys_user');
});
it('leaves referenceTo undefined for a non-picker inline param', () => {
expect(resolveActionParams([{ name: 'note', type: 'textarea' }], lookupCtx())[0].referenceTo).toBeUndefined();
});
});