@selfhelp/shared 1.22.1 → 1.24.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/CHANGELOG.md +128 -0
- package/dist/index.d.mts +308 -100
- package/dist/index.d.ts +308 -100
- package/dist/index.js +519 -3
- package/dist/index.mjs +495 -3
- package/dist/plugin-sdk/index.d.mts +1 -1
- package/dist/plugin-sdk/index.d.ts +1 -1
- package/dist/plugin-sdk/index.js +3 -1
- package/dist/plugin-sdk/index.mjs +3 -1
- package/dist/registry/index.d.mts +2 -2
- package/dist/registry/index.d.ts +2 -2
- package/dist/registry/index.js +3 -1
- package/dist/registry/index.mjs +3 -1
- package/dist/{styles.registry-DylfiWW_.d.ts → styles.registry-BKWoDg7q.d.ts} +756 -6
- package/dist/{styles.registry-Dl55-gWX.d.mts → styles.registry-aopb3vsI.d.mts} +756 -6
- package/package.json +1 -1
- package/src/api/__tests__/endpoints.test.ts +45 -0
- package/src/api/__tests__/formListErrors.test.ts +143 -0
- package/src/api/endpoints.ts +41 -0
- package/src/api/formListErrors.ts +134 -0
- package/src/api/index.ts +1 -0
- package/src/content/__tests__/formListState.test.ts +448 -0
- package/src/content/__tests__/formRecordPrefill.test.ts +120 -0
- package/src/content/formListState.ts +559 -0
- package/src/content/formRecordPrefill.ts +108 -5
- package/src/content/index.ts +1 -0
- package/src/registry/__tests__/platform.test.ts +15 -6
- package/src/registry/styles.registry.ts +2 -0
- package/src/types/__tests__/version-about.test.ts +80 -0
- package/src/types/api/__tests__/formWrites.test.ts +84 -0
- package/src/types/api/__tests__/system.test.ts +10 -0
- package/src/types/api/form-lists.ts +469 -0
- package/src/types/api/forms.ts +149 -4
- package/src/types/api/index.ts +1 -0
- package/src/types/api/system.ts +9 -0
- package/src/types/styles/composite.ts +13 -0
- package/src/types/styles/error.ts +54 -4
- package/src/types/styles/forms.ts +84 -0
- package/src/types/styles/unknown.ts +5 -1
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from 'vitest';
|
|
7
|
+
import {
|
|
8
|
+
applyFormListUrlChange,
|
|
9
|
+
emptyFormListUrlState,
|
|
10
|
+
encodeFormListQuery,
|
|
11
|
+
formListCacheKey,
|
|
12
|
+
formListRouteQuery,
|
|
13
|
+
formListQueryString,
|
|
14
|
+
nextSortOnHeaderClick,
|
|
15
|
+
parseFormListQuery,
|
|
16
|
+
pickFormListQuery,
|
|
17
|
+
type IFormListUrlRules,
|
|
18
|
+
type IFormListUrlState,
|
|
19
|
+
} from '../formListState';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The vectors of the backend `FormListUrlStateParserTest`: the web frontend and
|
|
23
|
+
* the mobile app must read a URL exactly as the server does, refuse what it
|
|
24
|
+
* refuses and write what it echoes.
|
|
25
|
+
*/
|
|
26
|
+
const RULES: IFormListUrlRules = { sortable: ['title', 'entry_date'], controls: ['state'] };
|
|
27
|
+
|
|
28
|
+
function parsed(query: string, rules: IFormListUrlRules = RULES): IFormListUrlState {
|
|
29
|
+
const result = parseFormListQuery(query, 'tasks', rules);
|
|
30
|
+
if (!result.ok) {
|
|
31
|
+
throw new Error(`Expected "${query}" to parse, got ${result.error.reason}: ${result.error.message}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return result.state;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function refusedWith(query: string, rules: IFormListUrlRules = RULES) {
|
|
38
|
+
const result = parseFormListQuery(query, 'tasks', rules);
|
|
39
|
+
if (result.ok) {
|
|
40
|
+
throw new Error(`Expected "${query.slice(0, 60)}" to be refused`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return result.error;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe('parseFormListQuery', () => {
|
|
47
|
+
it('reads an empty query as the list itself', () => {
|
|
48
|
+
expect(parsed('')).toEqual(emptyFormListUrlState());
|
|
49
|
+
expect(encodeFormListQuery('tasks', parsed(''))).toEqual({});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('reads a leading question mark and URLSearchParams like a plain query string', () => {
|
|
53
|
+
expect(parsed('?tasks.page=2').page).toBe(2);
|
|
54
|
+
expect(parsed(new URLSearchParams({ 'tasks.page': '5' }).toString()).page).toBe(5);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('round trips everything a visitor can choose through the URL', () => {
|
|
58
|
+
const query = 'tasks.page=3&tasks.q=write+report&tasks.sort=title%3Aasc%2Centry_date%3Adesc&tasks.f.state=open&tasks.record=17&tasks.mode=edit';
|
|
59
|
+
const state = parsed(query);
|
|
60
|
+
|
|
61
|
+
expect(state).toEqual({
|
|
62
|
+
page: 3,
|
|
63
|
+
q: 'write report',
|
|
64
|
+
sort: [
|
|
65
|
+
{ field: 'title', direction: 'asc' },
|
|
66
|
+
{ field: 'entry_date', direction: 'desc' },
|
|
67
|
+
],
|
|
68
|
+
filters: { state: 'open' },
|
|
69
|
+
record: 17,
|
|
70
|
+
mode: 'edit',
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const written = encodeFormListQuery('tasks', state);
|
|
74
|
+
expect(written).toEqual({
|
|
75
|
+
'tasks.page': '3',
|
|
76
|
+
'tasks.q': 'write report',
|
|
77
|
+
'tasks.sort': 'title:asc,entry_date:desc',
|
|
78
|
+
'tasks.f.state': 'open',
|
|
79
|
+
'tasks.record': '17',
|
|
80
|
+
'tasks.mode': 'edit',
|
|
81
|
+
});
|
|
82
|
+
expect(Object.keys(written)).toEqual([
|
|
83
|
+
'tasks.page', 'tasks.q', 'tasks.sort', 'tasks.f.state', 'tasks.record', 'tasks.mode',
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
const again = parseFormListQuery(formListQueryString('', 'tasks', state), 'tasks', RULES);
|
|
87
|
+
expect(again).toEqual({ ok: true, state });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('does not touch parameters of the page, the language or other lists', () => {
|
|
91
|
+
const state = parsed('page_id=5&lang=de&other.page=9&other.f.x=1&tasks.page=2&utm_source=mail');
|
|
92
|
+
|
|
93
|
+
expect(state.page).toBe(2);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('accepts an encoded dot in a parameter name', () => {
|
|
97
|
+
expect(parsed('tasks%2Epage=4').page).toBe(4);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('views a record that comes without a mode', () => {
|
|
101
|
+
expect(parsed('tasks.record=8').mode).toBe('view');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('treats an empty filter value as no filter and a blank search as no search', () => {
|
|
105
|
+
const state = parsed('tasks.f.state=&tasks.q=%20%20');
|
|
106
|
+
|
|
107
|
+
expect(state.filters).toEqual({});
|
|
108
|
+
expect(state.q).toBeNull();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('takes create as a mode of its own', () => {
|
|
112
|
+
const state = parsed('tasks.mode=create');
|
|
113
|
+
|
|
114
|
+
expect(state.mode).toBe('create');
|
|
115
|
+
expect(state.record).toBeNull();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('trims a search the way the server does and keeps its inner spaces', () => {
|
|
119
|
+
expect(parsed('tasks.q=%20%20anna%20maria%20').q).toBe('anna maria');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('trims exactly what PHP trim() trims: NUL and vertical tab go, a no-break space stays', () => {
|
|
123
|
+
expect(parsed('tasks.q=%00%0Banna%0B%00').q).toBe('anna');
|
|
124
|
+
expect(parsed('tasks.q=%C2%A0anna%C2%A0').q).toBe('\u00a0anna\u00a0');
|
|
125
|
+
expect(applyFormListUrlChange(emptyFormListUrlState(), { type: 'search', q: '\u00a0anna\u00a0' }).q).toBe('\u00a0anna\u00a0');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('counts a search in characters, not bytes', () => {
|
|
129
|
+
expect(parsed(`tasks.q=${encodeURIComponent('äö')}`).q).toBe('äö');
|
|
130
|
+
expect(refusedWith(`tasks.q=${encodeURIComponent('ä')}`).reason).toBe('invalid_list_parameter');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('accepts the longest page and search and the longest value', () => {
|
|
134
|
+
expect(parsed('tasks.page=1000000').page).toBe(1_000_000);
|
|
135
|
+
expect(parsed(`tasks.q=${'x'.repeat(100)}`).q).toHaveLength(100);
|
|
136
|
+
expect(parsed(`tasks.f.state=${'x'.repeat(1000)}`).filters.state).toHaveLength(1000);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const refused: ReadonlyArray<[string, string, string, 400 | 403]> = [
|
|
140
|
+
['page given twice', 'tasks.page=1&tasks.page=2', 'duplicate_list_parameter', 400],
|
|
141
|
+
['page zero', 'tasks.page=0', 'invalid_list_parameter', 400],
|
|
142
|
+
['page negative', 'tasks.page=-1', 'invalid_list_parameter', 400],
|
|
143
|
+
['page not a number', 'tasks.page=two', 'invalid_list_parameter', 400],
|
|
144
|
+
['page with a leading zero', 'tasks.page=02', 'invalid_list_parameter', 400],
|
|
145
|
+
['page beyond the limit', 'tasks.page=1000001', 'invalid_list_parameter', 400],
|
|
146
|
+
['page written as an array', 'tasks.page[]=1', 'unknown_list_parameter', 400],
|
|
147
|
+
['unknown parameter', 'tasks.limit=500', 'unknown_list_parameter', 400],
|
|
148
|
+
['unknown filter', 'tasks.f.secret=1', 'unknown_list_parameter', 400],
|
|
149
|
+
['search too short', 'tasks.q=a', 'invalid_list_parameter', 400],
|
|
150
|
+
['search too long', `tasks.q=${'x'.repeat(101)}`, 'invalid_list_parameter', 400],
|
|
151
|
+
['sort of a column that is not sortable', 'tasks.sort=status:asc', 'invalid_list_parameter', 400],
|
|
152
|
+
['sort without a direction', 'tasks.sort=title', 'invalid_list_parameter', 400],
|
|
153
|
+
['sort with a bad direction', 'tasks.sort=title:up', 'invalid_list_parameter', 400],
|
|
154
|
+
['sort twice on a column', 'tasks.sort=title:asc,title:desc', 'invalid_list_parameter', 400],
|
|
155
|
+
['too many sort keys', 'tasks.sort=title:asc,entry_date:desc,title:desc,x:asc', 'invalid_list_parameter', 400],
|
|
156
|
+
['record not a number', 'tasks.record=abc', 'invalid_list_parameter', 400],
|
|
157
|
+
['unknown mode', 'tasks.record=1&tasks.mode=delete', 'invalid_list_parameter', 400],
|
|
158
|
+
['create with a record', 'tasks.record=1&tasks.mode=create', 'invalid_list_parameter', 400],
|
|
159
|
+
['edit without a record', 'tasks.mode=edit', 'invalid_list_parameter', 400],
|
|
160
|
+
['view without a record', 'tasks.mode=view', 'invalid_list_parameter', 400],
|
|
161
|
+
['value over the length limit', `tasks.f.state=${'x'.repeat(1001)}`, 'invalid_list_parameter', 400],
|
|
162
|
+
];
|
|
163
|
+
|
|
164
|
+
it.each(refused)('refuses, not ignores: %s', (_label, query, reason, status) => {
|
|
165
|
+
const error = refusedWith(query);
|
|
166
|
+
|
|
167
|
+
expect(error.reason).toBe(reason);
|
|
168
|
+
expect(error.status).toBe(status);
|
|
169
|
+
expect(error.parameter.startsWith('tasks.')).toBe(true);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('reports a duplicate before any other problem, as the server does', () => {
|
|
173
|
+
expect(refusedWith('tasks.page=0&tasks.q=a&tasks.q=bb').reason).toBe('duplicate_list_parameter');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('names the parameter a client may drop and retry', () => {
|
|
177
|
+
expect(refusedWith('tasks.page=0').parameter).toBe('tasks.page');
|
|
178
|
+
expect(refusedWith('tasks.mode=edit').parameter).toBe('tasks.record');
|
|
179
|
+
expect(refusedWith('tasks.f.secret=1').parameter).toBe('tasks.f.secret');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('forbids, not malforms, a mode the list does not offer', () => {
|
|
183
|
+
const readOnly: IFormListUrlRules = { ...RULES, allowCreate: false, allowUpdate: false };
|
|
184
|
+
|
|
185
|
+
for (const query of ['tasks.mode=create', 'tasks.record=1&tasks.mode=edit']) {
|
|
186
|
+
const error = refusedWith(query, readOnly);
|
|
187
|
+
expect(error.status).toBe(403);
|
|
188
|
+
expect(error.reason).toBe('operation_disabled');
|
|
189
|
+
}
|
|
190
|
+
expect(parsed('tasks.record=1&tasks.mode=view', readOnly).mode).toBe('view');
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('accepts mode=history as a deep link to the history tab', () => {
|
|
194
|
+
expect(parsed('tasks.record=8&tasks.mode=history').mode).toBe('history');
|
|
195
|
+
expect(parsed('tasks.record=8&tasks.mode=history').record).toBe(8);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('forbids history when the list does not offer it', () => {
|
|
199
|
+
const noHistory: IFormListUrlRules = { ...RULES, showHistory: false };
|
|
200
|
+
const error = refusedWith('tasks.record=1&tasks.mode=history', noHistory);
|
|
201
|
+
expect(error.status).toBe(403);
|
|
202
|
+
expect(error.reason).toBe('history_disabled');
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('checks only the grammar when no rules are given', () => {
|
|
206
|
+
expect(parsed('tasks.sort=anything:desc&tasks.f.some_id=1', {}).filters).toEqual({ some_id: '1' });
|
|
207
|
+
expect(refusedWith('tasks.f.Bad-Id=1', {}).reason).toBe('unknown_list_parameter');
|
|
208
|
+
expect(refusedWith('tasks.sort=1abc:asc', {}).reason).toBe('invalid_list_parameter');
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('leaves a malformed percent escape as written instead of throwing', () => {
|
|
212
|
+
expect(parsed('tasks.q=100%25%zz').q).toBe('100%%zz');
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('scopes the namespace to the key: a longer key sharing the prefix is another list', () => {
|
|
216
|
+
const result = parseFormListQuery('tasks_archive.page=9&tasks.page=2', 'tasks', RULES);
|
|
217
|
+
|
|
218
|
+
expect(result).toMatchObject({ ok: true, state: { page: 2 } });
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe('formListQueryString', () => {
|
|
223
|
+
it('keeps every foreign parameter exactly as written and appends the list state last', () => {
|
|
224
|
+
const base = 'lang=de&other.page=9&tasks.page=2&utm_source=mail%20x&tasks.q=old';
|
|
225
|
+
const state = applyFormListUrlChange(emptyFormListUrlState(), { type: 'search', q: 'anna' });
|
|
226
|
+
|
|
227
|
+
expect(formListQueryString(base, 'tasks', state)).toBe('lang=de&other.page=9&utm_source=mail%20x&tasks.q=anna');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('drops the whole list namespace when the state is empty', () => {
|
|
231
|
+
expect(formListQueryString('tasks.page=4&tasks.record=3&lang=de', 'tasks', emptyFormListUrlState())).toBe('lang=de');
|
|
232
|
+
expect(formListQueryString('', 'tasks', emptyFormListUrlState())).toBe('');
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('writes readable separators and encodes the rest', () => {
|
|
236
|
+
const state: IFormListUrlState = {
|
|
237
|
+
...emptyFormListUrlState(),
|
|
238
|
+
q: 'a&b c',
|
|
239
|
+
sort: [{ field: 'title', direction: 'asc' }, { field: 'entry_date', direction: 'desc' }],
|
|
240
|
+
filters: { state: 'open' },
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
expect(formListQueryString('', 'tasks', state)).toBe('tasks.q=a%26b%20c&tasks.sort=title:asc,entry_date:desc&tasks.f.state=open');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('keeps the state of other lists of the page', () => {
|
|
247
|
+
const state = applyFormListUrlChange(emptyFormListUrlState(), { type: 'page', page: 3 });
|
|
248
|
+
|
|
249
|
+
expect(formListQueryString('news.page=2&news.q=x', 'tasks', state)).toBe('news.page=2&news.q=x&tasks.page=3');
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe('applyFormListUrlChange', () => {
|
|
254
|
+
const wide: IFormListUrlState = {
|
|
255
|
+
page: 4,
|
|
256
|
+
q: 'anna',
|
|
257
|
+
sort: [{ field: 'title', direction: 'asc' }],
|
|
258
|
+
filters: { state: 'open' },
|
|
259
|
+
record: 8,
|
|
260
|
+
mode: 'edit',
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
it('changes the page and nothing else', () => {
|
|
264
|
+
expect(applyFormListUrlChange(wide, { type: 'page', page: 2 })).toEqual({ ...wide, page: 2 });
|
|
265
|
+
expect(applyFormListUrlChange(wide, { type: 'page', page: 0 }).page).toBe(1);
|
|
266
|
+
expect(applyFormListUrlChange(wide, { type: 'page', page: Number.NaN }).page).toBe(1);
|
|
267
|
+
expect(applyFormListUrlChange(wide, { type: 'page', page: 2_000_000 }).page).toBe(1_000_000);
|
|
268
|
+
expect(applyFormListUrlChange(wide, { type: 'page', page: 2.9 }).page).toBe(2);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('goes back to the first page when the list is narrowed', () => {
|
|
272
|
+
expect(applyFormListUrlChange(wide, { type: 'search', q: ' berta ' })).toMatchObject({ q: 'berta', page: 1 });
|
|
273
|
+
expect(applyFormListUrlChange(wide, { type: 'search', q: ' ' })).toMatchObject({ q: null, page: 1 });
|
|
274
|
+
expect(applyFormListUrlChange(wide, { type: 'search', q: null })).toMatchObject({ q: null, page: 1 });
|
|
275
|
+
expect(applyFormListUrlChange(wide, { type: 'sort', sort: [{ field: 'entry_date', direction: 'desc' }] })).toMatchObject({
|
|
276
|
+
sort: [{ field: 'entry_date', direction: 'desc' }],
|
|
277
|
+
page: 1,
|
|
278
|
+
});
|
|
279
|
+
expect(applyFormListUrlChange(wide, { type: 'filter', id: 'state', value: 'done' })).toMatchObject({ filters: { state: 'done' }, page: 1 });
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('removes a filter whose value is cleared', () => {
|
|
283
|
+
expect(applyFormListUrlChange(wide, { type: 'filter', id: 'state', value: null }).filters).toEqual({});
|
|
284
|
+
expect(applyFormListUrlChange(wide, { type: 'filter', id: 'state', value: '' }).filters).toEqual({});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('keeps the open record when the list is narrowed: the server reads it outside the visitor filters', () => {
|
|
288
|
+
const narrowed = applyFormListUrlChange(wide, { type: 'filter', id: 'state', value: 'done' });
|
|
289
|
+
|
|
290
|
+
expect(narrowed.record).toBe(8);
|
|
291
|
+
expect(narrowed.mode).toBe('edit');
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('resets search, sort, filters and page but not the record', () => {
|
|
295
|
+
expect(applyFormListUrlChange(wide, { type: 'reset' })).toEqual({
|
|
296
|
+
page: 1, q: null, sort: [], filters: {}, record: 8, mode: 'edit',
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('opens, creates and closes a record keeping the list state', () => {
|
|
301
|
+
const closed = applyFormListUrlChange(wide, { type: 'close' });
|
|
302
|
+
expect(closed).toEqual({ ...wide, record: null, mode: null });
|
|
303
|
+
expect(applyFormListUrlChange(closed, { type: 'open', record: 12 })).toMatchObject({ record: 12, mode: 'view' });
|
|
304
|
+
expect(applyFormListUrlChange(closed, { type: 'open', record: 12, mode: 'edit' })).toMatchObject({ record: 12, mode: 'edit' });
|
|
305
|
+
expect(applyFormListUrlChange(wide, { type: 'create' })).toMatchObject({ record: null, mode: 'create', q: 'anna' });
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('does not mutate the state it was given and caps the sort keys', () => {
|
|
309
|
+
const before = structuredClone(wide);
|
|
310
|
+
applyFormListUrlChange(wide, { type: 'filter', id: 'state', value: 'x' });
|
|
311
|
+
applyFormListUrlChange(wide, { type: 'sort', sort: [] });
|
|
312
|
+
|
|
313
|
+
expect(wide).toEqual(before);
|
|
314
|
+
const many = applyFormListUrlChange(wide, {
|
|
315
|
+
type: 'sort',
|
|
316
|
+
sort: [
|
|
317
|
+
{ field: 'a', direction: 'asc' }, { field: 'b', direction: 'asc' },
|
|
318
|
+
{ field: 'c', direction: 'asc' }, { field: 'd', direction: 'asc' },
|
|
319
|
+
],
|
|
320
|
+
});
|
|
321
|
+
expect(many.sort).toHaveLength(3);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('produces a state the parser accepts', () => {
|
|
325
|
+
let state = emptyFormListUrlState();
|
|
326
|
+
state = applyFormListUrlChange(state, { type: 'search', q: 'write' });
|
|
327
|
+
state = applyFormListUrlChange(state, { type: 'filter', id: 'state', value: 'open' });
|
|
328
|
+
state = applyFormListUrlChange(state, { type: 'page', page: 2 });
|
|
329
|
+
state = applyFormListUrlChange(state, { type: 'open', record: 5, mode: 'edit' });
|
|
330
|
+
|
|
331
|
+
expect(parseFormListQuery(formListQueryString('', 'tasks', state), 'tasks', RULES)).toEqual({ ok: true, state });
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
describe('nextSortOnHeaderClick', () => {
|
|
336
|
+
it('cycles a column none, ascending, descending, none', () => {
|
|
337
|
+
const first = nextSortOnHeaderClick([], 'title');
|
|
338
|
+
expect(first).toEqual([{ field: 'title', direction: 'asc' }]);
|
|
339
|
+
const second = nextSortOnHeaderClick(first, 'title');
|
|
340
|
+
expect(second).toEqual([{ field: 'title', direction: 'desc' }]);
|
|
341
|
+
expect(nextSortOnHeaderClick(second, 'title')).toEqual([]);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('starts another column ascending and drops the others', () => {
|
|
345
|
+
expect(nextSortOnHeaderClick([{ field: 'title', direction: 'desc' }, { field: 'entry_date', direction: 'asc' }], 'entry_date')).toEqual([
|
|
346
|
+
{ field: 'entry_date', direction: 'asc' },
|
|
347
|
+
]);
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
describe('pickFormListQuery', () => {
|
|
352
|
+
it('keeps the pairs that can belong to a list, unchanged and in order', () => {
|
|
353
|
+
expect(pickFormListQuery('?page_id=5&tasks.page=2&lang=de&news.q=a%20b&utm_source=mail&tasks.f.state=open')).toBe(
|
|
354
|
+
'tasks.page=2&news.q=a%20b&tasks.f.state=open',
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('recognises an encoded dot and ignores names that are not a list key', () => {
|
|
359
|
+
expect(pickFormListQuery('tasks%2Epage=2&Tasks.page=1&.page=1&9a.page=1&x=1')).toBe('tasks%2Epage=2');
|
|
360
|
+
expect(pickFormListQuery('')).toBe('');
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
describe('formListCacheKey', () => {
|
|
365
|
+
const scope = {
|
|
366
|
+
apiBase: 'https://api.example.test',
|
|
367
|
+
account: 41,
|
|
368
|
+
languageId: 2,
|
|
369
|
+
platform: 'web' as const,
|
|
370
|
+
pageId: 7,
|
|
371
|
+
sectionId: 311,
|
|
372
|
+
key: 'tasks',
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
it('is the same for two spellings of one state and ignores foreign parameters', () => {
|
|
376
|
+
const a = formListCacheKey(scope, 'tasks.q=write+report&tasks.page=2&utm_source=x');
|
|
377
|
+
const b = formListCacheKey(scope, 'lang=de&tasks.page=2&tasks.q=write%20report');
|
|
378
|
+
|
|
379
|
+
expect(a).toEqual(b);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it('differs by account, guest, language, platform, server, page and section', () => {
|
|
383
|
+
const query = 'tasks.page=2';
|
|
384
|
+
const base = JSON.stringify(formListCacheKey(scope, query));
|
|
385
|
+
const variants = [
|
|
386
|
+
{ ...scope, account: 42 },
|
|
387
|
+
{ ...scope, account: null },
|
|
388
|
+
{ ...scope, languageId: 3 },
|
|
389
|
+
{ ...scope, platform: 'mobile' as const },
|
|
390
|
+
{ ...scope, apiBase: 'https://other.example.test' },
|
|
391
|
+
{ ...scope, pageId: 8 },
|
|
392
|
+
{ ...scope, sectionId: 312 },
|
|
393
|
+
].map((variant) => JSON.stringify(formListCacheKey(variant, query)));
|
|
394
|
+
|
|
395
|
+
expect(new Set([base, ...variants]).size).toBe(variants.length + 1);
|
|
396
|
+
expect(formListCacheKey({ ...scope, account: null }, query)).toContain('guest');
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it('differs by list state', () => {
|
|
400
|
+
expect(formListCacheKey(scope, 'tasks.page=2')).not.toEqual(formListCacheKey(scope, 'tasks.page=3'));
|
|
401
|
+
expect(formListCacheKey(scope, '')).toEqual(formListCacheKey(scope, 'news.page=3'));
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it('differs by the route of the page and keeps the list state as its last part', () => {
|
|
405
|
+
const three = formListCacheKey({ ...scope, route: { record_id: '3' } }, 'tasks.page=2');
|
|
406
|
+
const four = formListCacheKey({ ...scope, route: { record_id: '4' } }, 'tasks.page=2');
|
|
407
|
+
const none = formListCacheKey(scope, 'tasks.page=2');
|
|
408
|
+
|
|
409
|
+
expect(three).not.toEqual(four);
|
|
410
|
+
expect(three).not.toEqual(none);
|
|
411
|
+
expect(formListCacheKey({ ...scope, route: {} }, 'tasks.page=2')).toEqual(none);
|
|
412
|
+
expect(formListCacheKey({ ...scope, route: null }, 'tasks.page=2')).toEqual(none);
|
|
413
|
+
expect(three[three.length - 1]).toBe('tasks.page=2');
|
|
414
|
+
expect(three.slice(0, -1)).toHaveLength(8);
|
|
415
|
+
// Two spellings of one route share an entry.
|
|
416
|
+
expect(formListCacheKey({ ...scope, route: { b: '2', a: '1' } }, '')).toEqual(formListCacheKey({ ...scope, route: { a: '1', b: '2' } }, ''));
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
describe('formListRouteQuery', () => {
|
|
421
|
+
it('names the route parameters of a page the way the state request wants them', () => {
|
|
422
|
+
expect(formListRouteQuery({ record_id: '3' })).toBe('route%5Brecord_id%5D=3');
|
|
423
|
+
expect(formListRouteQuery({ token: 'a.b-c~d', user_id: '42' })).toBe('route%5Btoken%5D=a.b-c~d&route%5Buser_id%5D=42');
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it('sorts by name and percent-encodes the values', () => {
|
|
427
|
+
expect(formListRouteQuery({ b: 'x y', a: 'ü/&=' })).toBe('route%5Ba%5D=%C3%BC%2F%26%3D&route%5Bb%5D=x%20y');
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it('is empty for a page without route parameters and leaves out what cannot be one', () => {
|
|
431
|
+
expect(formListRouteQuery(undefined)).toBe('');
|
|
432
|
+
expect(formListRouteQuery(null)).toBe('');
|
|
433
|
+
expect(formListRouteQuery({})).toBe('');
|
|
434
|
+
expect(formListRouteQuery({ record_id: '' })).toBe('');
|
|
435
|
+
expect(formListRouteQuery({ record_id: undefined })).toBe('');
|
|
436
|
+
expect(formListRouteQuery({ 'bad name': '1', '9x': '1', 'a[b]': '1' })).toBe('');
|
|
437
|
+
expect(formListRouteQuery({ long: 'x'.repeat(256) })).toBe('');
|
|
438
|
+
expect(formListRouteQuery({ long: 'x'.repeat(255) })).toBe(`route%5Blong%5D=${'x'.repeat(255)}`);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it('never names more parameters than the server accepts', () => {
|
|
442
|
+
const route = Object.fromEntries(Array.from({ length: 12 }, (_, index) => [`p${String(index).padStart(2, '0')}`, '1']));
|
|
443
|
+
const query = formListRouteQuery(route);
|
|
444
|
+
|
|
445
|
+
expect(query.split('&')).toHaveLength(8);
|
|
446
|
+
expect(query.split('&')[0]).toBe('route%5Bp00%5D=1');
|
|
447
|
+
});
|
|
448
|
+
});
|
|
@@ -5,10 +5,55 @@
|
|
|
5
5
|
|
|
6
6
|
import { describe, expect, it } from 'vitest';
|
|
7
7
|
import {
|
|
8
|
+
asRecordVersion,
|
|
8
9
|
flattenFormRecordPrefillValues,
|
|
9
10
|
parseFormRecordPrefill,
|
|
10
11
|
} from '../formRecordPrefill';
|
|
11
12
|
|
|
13
|
+
describe('the record version of a prefill', () => {
|
|
14
|
+
it('is read from the rows and never becomes a form field', () => {
|
|
15
|
+
const result = parseFormRecordPrefill({
|
|
16
|
+
section_data: [
|
|
17
|
+
{ record_id: 12, id_languages: 1, _version: 4, title: 'Hello' },
|
|
18
|
+
{ record_id: 12, id_languages: 3, _version: 4, title: 'Hello' },
|
|
19
|
+
],
|
|
20
|
+
children: [{ name: { content: 'title' }, translatable: { content: '0' } }],
|
|
21
|
+
});
|
|
22
|
+
expect(result).toEqual({ recordId: 12, values: { title: 'Hello' }, version: 4 });
|
|
23
|
+
expect(Object.keys(result.values)).not.toContain('_version');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('is the version of the record the prefill is for, not of another one', () => {
|
|
27
|
+
const result = parseFormRecordPrefill({
|
|
28
|
+
section_data: [
|
|
29
|
+
{ record_id: 1, id_languages: 1, _version: 2, title: 'first' },
|
|
30
|
+
{ record_id: 2, id_languages: 1, _version: 9, title: 'second' },
|
|
31
|
+
],
|
|
32
|
+
children: [{ name: { content: 'title' }, translatable: { content: '0' } }],
|
|
33
|
+
});
|
|
34
|
+
expect(result.recordId).toBe(1);
|
|
35
|
+
expect(result.version).toBe(2);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('is absent when the page offered none, and a malformed one is not trusted', () => {
|
|
39
|
+
expect(parseFormRecordPrefill({ section_data: [{ record_id: 3, id_languages: 1, title: 'x' }] })).not.toHaveProperty('version');
|
|
40
|
+
for (const bad of [0, -1, 1.5, '0', '01', 'x', '', null, true, {}]) {
|
|
41
|
+
expect(parseFormRecordPrefill({ section_data: [{ record_id: 3, id_languages: 1, _version: bad }] })).not.toHaveProperty('version');
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('asRecordVersion accepts a whole number of at least 1 as a number or as digits', () => {
|
|
46
|
+
expect(asRecordVersion(7)).toBe(7);
|
|
47
|
+
expect(asRecordVersion('7')).toBe(7);
|
|
48
|
+
expect(asRecordVersion(' 12 ')).toBe(12);
|
|
49
|
+
expect(asRecordVersion('0')).toBeNull();
|
|
50
|
+
expect(asRecordVersion('07')).toBeNull();
|
|
51
|
+
expect(asRecordVersion(Number.NaN)).toBeNull();
|
|
52
|
+
expect(asRecordVersion(Number.MAX_SAFE_INTEGER + 2)).toBeNull();
|
|
53
|
+
expect(asRecordVersion(undefined)).toBeNull();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
12
57
|
describe('parseFormRecordPrefill', () => {
|
|
13
58
|
it('returns empty create state when section_data is missing', () => {
|
|
14
59
|
expect(parseFormRecordPrefill({})).toEqual({ recordId: null, values: {} });
|
|
@@ -45,6 +90,28 @@ describe('parseFormRecordPrefill', () => {
|
|
|
45
90
|
});
|
|
46
91
|
});
|
|
47
92
|
|
|
93
|
+
it('skips every spelling of the trigger type, so a form never writes it back', () => {
|
|
94
|
+
const result = parseFormRecordPrefill({
|
|
95
|
+
section_data: [
|
|
96
|
+
{
|
|
97
|
+
record_id: 7,
|
|
98
|
+
entry_date: '2026-11-13 10:00:00',
|
|
99
|
+
id_users: 3,
|
|
100
|
+
user_code: 'ABC',
|
|
101
|
+
id_action_trigger_types: 73,
|
|
102
|
+
id_actionTriggerTypes: 73,
|
|
103
|
+
triggerType: 'updated',
|
|
104
|
+
id_languages: 1,
|
|
105
|
+
language_locale: 'all',
|
|
106
|
+
language_name: 'Independent',
|
|
107
|
+
title: 'Hello',
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
children: [{ name: { content: 'title' }, translatable: { content: '0' } }],
|
|
111
|
+
});
|
|
112
|
+
expect(result).toEqual({ recordId: 7, values: { title: 'Hello' } });
|
|
113
|
+
});
|
|
114
|
+
|
|
48
115
|
it('accumulates per-language values for translatable fields', () => {
|
|
49
116
|
const result = parseFormRecordPrefill({
|
|
50
117
|
section_data: [
|
|
@@ -101,4 +168,57 @@ describe('flattenFormRecordPrefillValues', () => {
|
|
|
101
168
|
title: 'plain',
|
|
102
169
|
});
|
|
103
170
|
});
|
|
171
|
+
|
|
172
|
+
it('keeps file cells out of the flattened string map', () => {
|
|
173
|
+
expect(
|
|
174
|
+
flattenFormRecordPrefillValues({
|
|
175
|
+
title: 'Hello',
|
|
176
|
+
portrait: {
|
|
177
|
+
files: [{ index: 0, name: 'a.jpg', url: '/lists/1/records/2/files/portrait?page_id=3&index=0' }],
|
|
178
|
+
},
|
|
179
|
+
}),
|
|
180
|
+
).toEqual({ title: 'Hello' });
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe('file-input prefill (contract 4)', () => {
|
|
185
|
+
it('keeps the server files list and never a storage key', () => {
|
|
186
|
+
const result = parseFormRecordPrefill({
|
|
187
|
+
section_data: [
|
|
188
|
+
{
|
|
189
|
+
record_id: 9,
|
|
190
|
+
id_languages: 1,
|
|
191
|
+
_version: 2,
|
|
192
|
+
title: 'Photo',
|
|
193
|
+
portrait: {
|
|
194
|
+
files: [
|
|
195
|
+
{
|
|
196
|
+
index: 0,
|
|
197
|
+
name: 'shot.jpg',
|
|
198
|
+
mime: 'image/jpeg',
|
|
199
|
+
size: 12,
|
|
200
|
+
url: '/lists/5/records/9/files/portrait?page_id=1&index=0',
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
children: [
|
|
207
|
+
{ name: { content: 'title' }, translatable: { content: '0' } },
|
|
208
|
+
{ name: { content: 'portrait' }, translatable: { content: '0' } },
|
|
209
|
+
],
|
|
210
|
+
});
|
|
211
|
+
expect(result.values.portrait).toEqual({
|
|
212
|
+
files: [
|
|
213
|
+
{
|
|
214
|
+
index: 0,
|
|
215
|
+
name: 'shot.jpg',
|
|
216
|
+
mime: 'image/jpeg',
|
|
217
|
+
size: 12,
|
|
218
|
+
url: '/lists/5/records/9/files/portrait?page_id=1&index=0',
|
|
219
|
+
},
|
|
220
|
+
],
|
|
221
|
+
});
|
|
222
|
+
expect(JSON.stringify(result.values)).not.toContain('uploads/form-files');
|
|
223
|
+
});
|
|
104
224
|
});
|