asma-core-helpers 0.30.0 → 0.31.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/lib/collaboration/applyOperation.d.ts +33 -1
- package/lib/collaboration/applyOperation.d.ts.map +1 -1
- package/lib/collaboration/applyOperation.js +633 -9
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/operations.d.ts +109 -2
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js +7 -0
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +163 -0
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +173 -3
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/collaboration/templateDocument.d.ts +176 -0
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js +9 -0
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.js +78 -1
- package/lib/collaboration/testHelpers/documentGenerator.js.map +1 -1
- package/lib/collaboration/typeContracts.d.ts +53 -0
- package/lib/collaboration/typeContracts.d.ts.map +1 -0
- package/lib/collaboration/typeContracts.js +2 -0
- package/lib/collaboration/typeContracts.js.map +1 -0
- package/package.json +1 -1
- package/src/collaboration/applyOperation.ts +736 -9
- package/src/collaboration/operations.ts +97 -0
- package/src/collaboration/schemas.ts +191 -1
- package/src/collaboration/templateDocument.ts +167 -1
- package/src/collaboration/testHelpers/documentGenerator.ts +77 -1
- package/src/collaboration/typeContracts.ts +57 -0
- package/src/collaboration/typedAuthoring.test.ts +641 -0
- package/src/collaboration/typedAuthoringHardening.test.ts +408 -0
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { describe, it } from 'node:test'
|
|
3
|
+
|
|
4
|
+
import { applyOperation, OperationConflictError, knownActionFieldRefusal, tabFieldPathRefusal } from './applyOperation.js'
|
|
5
|
+
import { canonicalJson, reduceToMinimalForm } from './canonicalize.js'
|
|
6
|
+
import { findDocLawViolations } from './docLaws.js'
|
|
7
|
+
import type { TemplateOp } from './operations.js'
|
|
8
|
+
import { templateDocumentIsDefault, templateOpSchema, validateTemplateDocument } from './schemas.js'
|
|
9
|
+
import {
|
|
10
|
+
emptyTemplateDocument,
|
|
11
|
+
isKnownActionKind,
|
|
12
|
+
isKnownMappingFilterOperator,
|
|
13
|
+
MAPPING_FILTER_OPERATORS,
|
|
14
|
+
type QnrTemplateDocument,
|
|
15
|
+
} from './templateDocument.js'
|
|
16
|
+
import { type } from 'arktype'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The ASMA-7683 additive authoring surface: typed grid presentation, tab layout, closed filters and
|
|
20
|
+
* typed actions.
|
|
21
|
+
*
|
|
22
|
+
* Every addition here is **append-only over a released contract**, which is the property most of these
|
|
23
|
+
* cases exist to defend. ADR-0008 DEC-006 forbids narrowing a shipped document or op shape, so
|
|
24
|
+
* `MappingFilter.operator` stays a loose `string`, `QnrAction` stays an open bag with an arbitrary
|
|
25
|
+
* `kind`, and `mappingFilter.set` / `action.create` / `action.updateField` keep replaying byte for byte.
|
|
26
|
+
* The closed vocabularies arrive beside them, and only the new ops can write them.
|
|
27
|
+
*
|
|
28
|
+
* @see the ASMA-7683 shared freeze — §2-§5 and the required-test list
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const doc0 = emptyTemplateDocument('tpl-typed')
|
|
32
|
+
const apply = (ops: TemplateOp[], from: QnrTemplateDocument = doc0): QnrTemplateDocument =>
|
|
33
|
+
ops.reduce((doc, op) => applyOperation(doc, op), from)
|
|
34
|
+
|
|
35
|
+
/** A grid with two columns, a tab, and a node to hang filters on. */
|
|
36
|
+
const scaffold = (): QnrTemplateDocument =>
|
|
37
|
+
apply([
|
|
38
|
+
{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' },
|
|
39
|
+
{ type: 'gridColumn.create', questionId: 'g-1', columnQuestionId: 'c-1', questionType: 'TextShort' },
|
|
40
|
+
{ type: 'gridColumn.create', questionId: 'g-1', columnQuestionId: 'c-2', questionType: 'DateField' },
|
|
41
|
+
{ type: 'question.create', questionId: 'q-top', questionType: 'TextShort' },
|
|
42
|
+
{ type: 'tab.create', tabId: 't-1', label: 'Første' },
|
|
43
|
+
{ type: 'mappingNode.create', nodeId: 'n-1', entityId: 'Actor' },
|
|
44
|
+
])
|
|
45
|
+
|
|
46
|
+
const grid = (doc: QnrTemplateDocument) => doc.questionsById?.['g-1']
|
|
47
|
+
const action = (doc: QnrTemplateDocument, id: string) => doc.actionsById?.[id]
|
|
48
|
+
const throwsConflict = (fn: () => unknown, hint: string) =>
|
|
49
|
+
assert.throws(fn, (error: unknown) => error instanceof OperationConflictError, hint)
|
|
50
|
+
|
|
51
|
+
// ─────────────────────────────── released-shape compatibility ───────────────────────────────
|
|
52
|
+
|
|
53
|
+
describe('the released shapes still replay byte for byte', () => {
|
|
54
|
+
it('keeps mappingFilter.set with an operator outside the closed set', () => {
|
|
55
|
+
// `contains` is stored in real documents. Narrowing `operator` would make them unreplayable,
|
|
56
|
+
// which is precisely what DEC-006 forbids — so the loose write stays as released.
|
|
57
|
+
const doc = apply(
|
|
58
|
+
[{ type: 'mappingFilter.set', filterId: 'f-1', nodeId: 'n-1', fieldId: 'Navn', operator: 'contains', value: 'ada' }],
|
|
59
|
+
scaffold(),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
assert.deepEqual(doc.mappingFiltersById?.['f-1'], { fieldId: 'Navn', operator: 'contains', value: 'ada' })
|
|
63
|
+
assert.equal(isKnownMappingFilterOperator('contains'), false)
|
|
64
|
+
// And it is still a valid document: readability is the compatibility guarantee.
|
|
65
|
+
assert.equal(validateTemplateDocument(doc).ok, true)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('keeps action.create and action.updateField on an unknown kind, buffers included', () => {
|
|
69
|
+
const doc = apply(
|
|
70
|
+
[
|
|
71
|
+
{ type: 'action.create', actionId: 'x-legacy', kind: 'submit' },
|
|
72
|
+
{ type: 'action.updateField', actionId: 'x-legacy', field: 'editable_label', value: true },
|
|
73
|
+
{ type: 'action.updateField', actionId: 'x-legacy', field: 'label', value: 'Send' },
|
|
74
|
+
],
|
|
75
|
+
scaffold(),
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
// The buffer lands, because on a legacy record it always did.
|
|
79
|
+
assert.deepEqual(action(doc, 'x-legacy'), { kind: 'submit', editable_label: true, label: 'Send' })
|
|
80
|
+
assert.equal(isKnownActionKind('submit'), false)
|
|
81
|
+
assert.equal(validateTemplateDocument(doc).ok, true)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('reads an old broad stored action and filter', () => {
|
|
85
|
+
// Hand-built rather than op-produced: this is what an imported document looks like.
|
|
86
|
+
const stored = {
|
|
87
|
+
...emptyTemplateDocument('tpl-old'),
|
|
88
|
+
questionOrder: ['q-1'],
|
|
89
|
+
questionsById: { 'q-1': { type: 'TextShort' as const } },
|
|
90
|
+
actionsById: { 'x-1': { kind: 'weird', editable_type: 'COPY', nested: { anything: 1 } } },
|
|
91
|
+
mappingNodesById: { 'n-1': { entityId: 'Actor', filterOrder: ['f-1'] } },
|
|
92
|
+
mappingFiltersById: { 'f-1': { fieldId: 'Navn', operator: 'startsWith', value: 'a' } },
|
|
93
|
+
} as unknown as QnrTemplateDocument
|
|
94
|
+
|
|
95
|
+
assert.equal(validateTemplateDocument(stored).ok, true)
|
|
96
|
+
assert.deepEqual(findDocLawViolations(stored), [])
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
// ─────────────────────────────── §4 closed filter authoring ───────────────────────────────
|
|
101
|
+
|
|
102
|
+
describe('mappingFilter.setTyped', () => {
|
|
103
|
+
const setTyped = (payload: Record<string, unknown>) =>
|
|
104
|
+
({ type: 'mappingFilter.setTyped', filterId: 'f-1', nodeId: 'n-1', fieldId: 'Navn', ...payload }) as TemplateOp
|
|
105
|
+
|
|
106
|
+
it('writes exactly the four operators and their canonical payloads', () => {
|
|
107
|
+
const cases: Array<[Record<string, unknown>, Record<string, unknown>]> = [
|
|
108
|
+
[{ operator: 'eq', value: 'ada' }, { fieldId: 'Navn', operator: 'eq', value: 'ada' }],
|
|
109
|
+
[{ operator: 'in', values: ['a', 'b'] }, { fieldId: 'Navn', operator: 'in', values: ['a', 'b'] }],
|
|
110
|
+
[{ operator: 'range', from: 1, to: 9 }, { fieldId: 'Navn', operator: 'range', from: 1, to: 9 }],
|
|
111
|
+
[{ operator: 'range', from: 1 }, { fieldId: 'Navn', operator: 'range', from: 1 }],
|
|
112
|
+
[{ operator: 'range', to: 9 }, { fieldId: 'Navn', operator: 'range', to: 9 }],
|
|
113
|
+
[{ operator: 'isNull', value: true }, { fieldId: 'Navn', operator: 'isNull', value: true }],
|
|
114
|
+
]
|
|
115
|
+
|
|
116
|
+
for (const [payload, expected] of cases) {
|
|
117
|
+
const doc = apply([setTyped(payload)], scaffold())
|
|
118
|
+
assert.deepEqual(doc.mappingFiltersById?.['f-1'], expected, JSON.stringify(payload))
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
assert.deepEqual([...MAPPING_FILTER_OPERATORS], ['eq', 'in', 'range', 'isNull'])
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('registers the filter in its node order, exactly once', () => {
|
|
125
|
+
const doc = apply([setTyped({ operator: 'eq', value: 'ada' }), setTyped({ operator: 'in', values: ['b'] })], scaffold())
|
|
126
|
+
|
|
127
|
+
assert.deepEqual(doc.mappingNodesById?.['n-1']?.filterOrder, ['f-1'])
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('deletes every stale member when the operator switches', () => {
|
|
131
|
+
// The reason the reducer REPLACES rather than patches: a merged record would carry `value`
|
|
132
|
+
// beside `values`, and no reader could tell which one the operator meant.
|
|
133
|
+
let doc = apply([setTyped({ operator: 'eq', value: 'ada' })], scaffold())
|
|
134
|
+
doc = apply([setTyped({ operator: 'in', values: ['a', 'b'] })], doc)
|
|
135
|
+
assert.deepEqual(doc.mappingFiltersById?.['f-1'], { fieldId: 'Navn', operator: 'in', values: ['a', 'b'] })
|
|
136
|
+
|
|
137
|
+
doc = apply([setTyped({ operator: 'range', from: 1 })], doc)
|
|
138
|
+
assert.deepEqual(doc.mappingFiltersById?.['f-1'], { fieldId: 'Navn', operator: 'range', from: 1 })
|
|
139
|
+
|
|
140
|
+
doc = apply([setTyped({ operator: 'isNull', value: false })], doc)
|
|
141
|
+
assert.deepEqual(doc.mappingFiltersById?.['f-1'], { fieldId: 'Navn', operator: 'isNull', value: false })
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it('refuses at the schema what the typed path may not express', () => {
|
|
145
|
+
const invalid: Array<[string, Record<string, unknown>]> = [
|
|
146
|
+
['an operator outside the closed set', { operator: 'contains', value: 'a' }],
|
|
147
|
+
['an empty in-list', { operator: 'in', values: [] }],
|
|
148
|
+
['a range with neither bound', { operator: 'range' }],
|
|
149
|
+
['eq with no value', { operator: 'eq' }],
|
|
150
|
+
['isNull with a non-boolean', { operator: 'isNull', value: 'yes' }],
|
|
151
|
+
['eq carrying a stale values member', { operator: 'eq', value: 'a', values: ['b'] }],
|
|
152
|
+
['in carrying a stale value member', { operator: 'in', values: ['b'], value: 'a' }],
|
|
153
|
+
['range carrying a stale value member', { operator: 'range', from: 1, value: 'a' }],
|
|
154
|
+
]
|
|
155
|
+
|
|
156
|
+
for (const [hint, payload] of invalid) {
|
|
157
|
+
const result = templateOpSchema(setTyped(payload))
|
|
158
|
+
assert.ok(result instanceof type.errors, hint)
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('refuses a node that does not exist', () => {
|
|
163
|
+
throwsConflict(
|
|
164
|
+
() => apply([{ ...(setTyped({ operator: 'eq', value: 'a' }) as object), nodeId: 'n-ghost' } as TemplateOp], scaffold()),
|
|
165
|
+
'unknown node',
|
|
166
|
+
)
|
|
167
|
+
})
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
// ─────────────────────────────── §2 typed grid presentation ───────────────────────────────
|
|
171
|
+
|
|
172
|
+
describe('gridColumn.setFilter and setAction', () => {
|
|
173
|
+
it('inserts, moves and removes one member at a time', () => {
|
|
174
|
+
let doc = apply(
|
|
175
|
+
[
|
|
176
|
+
{ type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-1', include: true },
|
|
177
|
+
{ type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-2', include: true },
|
|
178
|
+
],
|
|
179
|
+
scaffold(),
|
|
180
|
+
)
|
|
181
|
+
assert.deepEqual(grid(doc)?.presentation?.filterQuestionIds, ['c-1', 'c-2'])
|
|
182
|
+
|
|
183
|
+
// A move is an insert with an index, not a delete-then-add.
|
|
184
|
+
doc = apply([{ type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-2', include: true, atIndex: 0 }], doc)
|
|
185
|
+
assert.deepEqual(grid(doc)?.presentation?.filterQuestionIds, ['c-2', 'c-1'])
|
|
186
|
+
|
|
187
|
+
// Re-including without an index is a no-op rather than a duplicate.
|
|
188
|
+
doc = apply([{ type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-2', include: true }], doc)
|
|
189
|
+
assert.deepEqual(grid(doc)?.presentation?.filterQuestionIds, ['c-2', 'c-1'])
|
|
190
|
+
|
|
191
|
+
doc = apply(
|
|
192
|
+
[
|
|
193
|
+
{ type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-1', include: false },
|
|
194
|
+
{ type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-2', include: false },
|
|
195
|
+
],
|
|
196
|
+
doc,
|
|
197
|
+
)
|
|
198
|
+
// Emptied list is omitted, not stored as [] (DOC-LAW-2).
|
|
199
|
+
assert.equal('filterQuestionIds' in (grid(doc)?.presentation ?? {}), false)
|
|
200
|
+
assert.equal(grid(doc)?.presentation, undefined)
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
it('lets two authors add DIFFERENT filters concurrently without either being lost', () => {
|
|
204
|
+
// The whole reason these are member-wise ops. Both edits are computed against the same base and
|
|
205
|
+
// replayed in sequence, which is what a wholesale array write would break.
|
|
206
|
+
const base = scaffold()
|
|
207
|
+
const mine = { type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-1', include: true } as const
|
|
208
|
+
const theirs = { type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-2', include: true } as const
|
|
209
|
+
|
|
210
|
+
const oneOrder = apply([mine, theirs], base)
|
|
211
|
+
const otherOrder = apply([theirs, mine], base)
|
|
212
|
+
|
|
213
|
+
assert.deepEqual(grid(oneOrder)?.presentation?.filterQuestionIds, ['c-1', 'c-2'])
|
|
214
|
+
assert.deepEqual(grid(otherOrder)?.presentation?.filterQuestionIds, ['c-2', 'c-1'])
|
|
215
|
+
// Order differs by arrival, but nobody's filter disappeared.
|
|
216
|
+
for (const doc of [oneOrder, otherOrder]) {
|
|
217
|
+
assert.deepEqual([...(grid(doc)?.presentation?.filterQuestionIds ?? [])].sort(), ['c-1', 'c-2'])
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('refuses a filter on a question the grid does not own', () => {
|
|
222
|
+
throwsConflict(
|
|
223
|
+
() => apply([{ type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'q-top', include: true }], scaffold()),
|
|
224
|
+
'top-level question is not a column',
|
|
225
|
+
)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('keeps a grid action owned by at most one grid', () => {
|
|
229
|
+
const base = apply(
|
|
230
|
+
[
|
|
231
|
+
{ type: 'question.create', questionId: 'g-2', questionType: 'QuestionGrid' },
|
|
232
|
+
{ type: 'action.createTyped', actionId: 'x-1', kind: 'gridAction' },
|
|
233
|
+
{ type: 'gridColumn.setAction', questionId: 'g-1', actionId: 'x-1', include: true },
|
|
234
|
+
],
|
|
235
|
+
scaffold(),
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
// Two owners would give one action two column vocabularies for its metadata.
|
|
239
|
+
throwsConflict(
|
|
240
|
+
() => apply([{ type: 'gridColumn.setAction', questionId: 'g-2', actionId: 'x-1', include: true }], base),
|
|
241
|
+
'second owner',
|
|
242
|
+
)
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
it('reserves both lists from question.updateField, in both directions', () => {
|
|
246
|
+
for (const field of [
|
|
247
|
+
'presentation.filterQuestionIds',
|
|
248
|
+
'presentation.filterQuestionIds.0',
|
|
249
|
+
'presentation.actionIds',
|
|
250
|
+
'presentation',
|
|
251
|
+
]) {
|
|
252
|
+
throwsConflict(
|
|
253
|
+
() => apply([{ type: 'question.updateField', questionId: 'g-1', field, value: 'x' }], scaffold()),
|
|
254
|
+
field,
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// The neighbouring scalars stay ordinary field edits.
|
|
259
|
+
const doc = apply(
|
|
260
|
+
[
|
|
261
|
+
{ type: 'question.updateField', questionId: 'g-1', field: 'presentation.headerTabId', value: 't-1' },
|
|
262
|
+
{ type: 'question.updateField', questionId: 'g-1', field: 'presentation.defaultColumnWidthsByQuestionId.c-1', value: 120 },
|
|
263
|
+
],
|
|
264
|
+
scaffold(),
|
|
265
|
+
)
|
|
266
|
+
assert.equal(grid(doc)?.presentation?.headerTabId, 't-1')
|
|
267
|
+
assert.equal(grid(doc)?.presentation?.defaultColumnWidthsByQuestionId?.['c-1'], 120)
|
|
268
|
+
})
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
// ─────────────────────────────── §3 tab layout ───────────────────────────────
|
|
272
|
+
|
|
273
|
+
describe('tab.setLayout', () => {
|
|
274
|
+
const placements = (doc: QnrTemplateDocument) =>
|
|
275
|
+
(doc.tabsById?.['t-1']?.layout ?? {})['placementsByQuestionId'] as Record<string, unknown> | undefined
|
|
276
|
+
|
|
277
|
+
it('writes and clears a whole placement atomically', () => {
|
|
278
|
+
let doc = apply(
|
|
279
|
+
[{ type: 'tab.setLayout', tabId: 't-1', questionId: 'c-1', placement: { row: 2, cell: 1, keepCellSize: true } }],
|
|
280
|
+
scaffold(),
|
|
281
|
+
)
|
|
282
|
+
assert.deepEqual(placements(doc)?.['c-1'], { row: 2, cell: 1, keepCellSize: true })
|
|
283
|
+
|
|
284
|
+
doc = apply([{ type: 'tab.setLayout', tabId: 't-1', questionId: 'c-1', placement: null }], doc)
|
|
285
|
+
// Emptied containers are dropped all the way up.
|
|
286
|
+
assert.equal(doc.tabsById?.['t-1']?.layout, undefined)
|
|
287
|
+
assert.deepEqual(doc.tabsById?.['t-1'], { label: 'Første' })
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it('never tears a placement into a row/cell hybrid', () => {
|
|
291
|
+
const base = apply(
|
|
292
|
+
[{ type: 'tab.setLayout', tabId: 't-1', questionId: 'c-1', placement: { row: 1, cell: 1 } }],
|
|
293
|
+
scaffold(),
|
|
294
|
+
)
|
|
295
|
+
const moved = apply(
|
|
296
|
+
[{ type: 'tab.setLayout', tabId: 't-1', questionId: 'c-1', placement: { row: 4, cell: 3 } }],
|
|
297
|
+
base,
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
// Last-writer-wins on ONE whole tuple: never row from one edit and cell from another.
|
|
301
|
+
assert.deepEqual(placements(moved)?.['c-1'], { row: 4, cell: 3 })
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
it('lets concurrent moves of different questions both survive', () => {
|
|
305
|
+
const base = scaffold()
|
|
306
|
+
const mine = { type: 'tab.setLayout', tabId: 't-1', questionId: 'c-1', placement: { row: 0, cell: 0 } } as const
|
|
307
|
+
const theirs = { type: 'tab.setLayout', tabId: 't-1', questionId: 'c-2', placement: { row: 1, cell: 1 } } as const
|
|
308
|
+
|
|
309
|
+
for (const doc of [apply([mine, theirs], base), apply([theirs, mine], base)]) {
|
|
310
|
+
assert.deepEqual(placements(doc)?.['c-1'], { row: 0, cell: 0 })
|
|
311
|
+
assert.deepEqual(placements(doc)?.['c-2'], { row: 1, cell: 1 })
|
|
312
|
+
}
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
it('reserves the placements map from tab.updateField', () => {
|
|
316
|
+
for (const field of ['layout.placementsByQuestionId', 'layout.placementsByQuestionId.c-1', 'layout']) {
|
|
317
|
+
assert.notEqual(tabFieldPathRefusal(field), undefined, field)
|
|
318
|
+
throwsConflict(
|
|
319
|
+
() => apply([{ type: 'tab.updateField', tabId: 't-1', field, value: 'x' }], scaffold()),
|
|
320
|
+
field,
|
|
321
|
+
)
|
|
322
|
+
}
|
|
323
|
+
assert.equal(tabFieldPathRefusal('label'), undefined)
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
it('scrubs a deleted question from every tab layout', () => {
|
|
327
|
+
const doc = apply(
|
|
328
|
+
[
|
|
329
|
+
{ type: 'tab.setLayout', tabId: 't-1', questionId: 'c-1', placement: { row: 0, cell: 0 } },
|
|
330
|
+
{ type: 'tab.setLayout', tabId: 't-1', questionId: 'c-2', placement: { row: 1, cell: 0 } },
|
|
331
|
+
{ type: 'question.delete', questionId: 'c-1' },
|
|
332
|
+
],
|
|
333
|
+
scaffold(),
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
assert.deepEqual(Object.keys(placements(doc) ?? {}), ['c-2'])
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
it('scrubs a deleted tab from every grid that named it as header', () => {
|
|
340
|
+
const doc = apply(
|
|
341
|
+
[
|
|
342
|
+
{ type: 'question.updateField', questionId: 'g-1', field: 'presentation.headerTabId', value: 't-1' },
|
|
343
|
+
{ type: 'tab.delete', tabId: 't-1' },
|
|
344
|
+
],
|
|
345
|
+
scaffold(),
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
// The reference is gone, and so is the container it emptied.
|
|
349
|
+
assert.equal(grid(doc)?.presentation, undefined)
|
|
350
|
+
assert.equal(doc.tabsById, undefined)
|
|
351
|
+
})
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
// ─────────────────────────────── §5 typed actions ───────────────────────────────
|
|
355
|
+
|
|
356
|
+
describe('typed actions', () => {
|
|
357
|
+
const withActions = (): QnrTemplateDocument =>
|
|
358
|
+
apply(
|
|
359
|
+
[
|
|
360
|
+
{ type: 'action.createTyped', actionId: 'x-grid', kind: 'gridAction', label: 'Kopier', actionType: 'COPY' },
|
|
361
|
+
{ type: 'gridColumn.setAction', questionId: 'g-1', actionId: 'x-grid', include: true },
|
|
362
|
+
{ type: 'action.createTyped', actionId: 'x-top', kind: 'topLevelAction', label: 'Kjør' },
|
|
363
|
+
],
|
|
364
|
+
scaffold(),
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
it('creates a known action in minimal form', () => {
|
|
368
|
+
const doc = apply([{ type: 'action.createTyped', actionId: 'x-1', kind: 'topLevelAction' }], scaffold())
|
|
369
|
+
|
|
370
|
+
// No empty label, no empty collections.
|
|
371
|
+
assert.deepEqual(action(doc, 'x-1'), { kind: 'topLevelAction' })
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
it('refuses actionType on a top-level action', () => {
|
|
375
|
+
throwsConflict(
|
|
376
|
+
() =>
|
|
377
|
+
apply(
|
|
378
|
+
[{ type: 'action.createTyped', actionId: 'x-1', kind: 'topLevelAction', actionType: 'COPY' }],
|
|
379
|
+
scaffold(),
|
|
380
|
+
),
|
|
381
|
+
'actionType is a gridAction member',
|
|
382
|
+
)
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
it('cannot mint a UI edit buffer through the typed path', () => {
|
|
386
|
+
for (const buffer of ['editableLabel', 'editableType', 'editable_label', 'editable_type']) {
|
|
387
|
+
const result = templateOpSchema({
|
|
388
|
+
type: 'action.createTyped',
|
|
389
|
+
actionId: 'x-1',
|
|
390
|
+
kind: 'gridAction',
|
|
391
|
+
[buffer]: true,
|
|
392
|
+
})
|
|
393
|
+
assert.ok(result instanceof type.errors, buffer)
|
|
394
|
+
}
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
it('refuses UI buffers and reserved collections on a KNOWN action, and permits label', () => {
|
|
398
|
+
const base = withActions()
|
|
399
|
+
|
|
400
|
+
for (const field of [
|
|
401
|
+
'editable_label',
|
|
402
|
+
'editableType',
|
|
403
|
+
'kind',
|
|
404
|
+
'metadataByQuestionId',
|
|
405
|
+
'metadataByQuestionId.c-1',
|
|
406
|
+
'actionIdsByGridQuestionId',
|
|
407
|
+
]) {
|
|
408
|
+
assert.notEqual(knownActionFieldRefusal('gridAction', field), undefined, field)
|
|
409
|
+
throwsConflict(
|
|
410
|
+
() => apply([{ type: 'action.updateField', actionId: 'x-grid', field, value: 'x' }], base),
|
|
411
|
+
field,
|
|
412
|
+
)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// `actionType` is a gridAction member only.
|
|
416
|
+
assert.equal(knownActionFieldRefusal('gridAction', 'actionType'), undefined)
|
|
417
|
+
assert.notEqual(knownActionFieldRefusal('topLevelAction', 'actionType'), undefined)
|
|
418
|
+
|
|
419
|
+
const doc = apply([{ type: 'action.updateField', actionId: 'x-grid', field: 'label', value: 'Ny' }], base)
|
|
420
|
+
assert.equal(action(doc, 'x-grid')?.label, 'Ny')
|
|
421
|
+
})
|
|
422
|
+
|
|
423
|
+
it('refuses the typed ops on a legacy unknown-kind record', () => {
|
|
424
|
+
const base = apply([{ type: 'action.create', actionId: 'x-legacy', kind: 'submit' }], scaffold())
|
|
425
|
+
|
|
426
|
+
throwsConflict(
|
|
427
|
+
() =>
|
|
428
|
+
apply(
|
|
429
|
+
[{ type: 'action.setMetadata', actionId: 'x-legacy', questionId: 'c-1', metadata: { all: true } }],
|
|
430
|
+
base,
|
|
431
|
+
),
|
|
432
|
+
'legacy record is not a known gridAction',
|
|
433
|
+
)
|
|
434
|
+
})
|
|
435
|
+
|
|
436
|
+
it('keeps a top-level grid reference member-wise and ordered', () => {
|
|
437
|
+
let doc = apply(
|
|
438
|
+
[
|
|
439
|
+
{ type: 'action.createTyped', actionId: 'x-grid-2', kind: 'gridAction' },
|
|
440
|
+
{ type: 'gridColumn.setAction', questionId: 'g-1', actionId: 'x-grid-2', include: true },
|
|
441
|
+
{ type: 'action.setGridActionRef', actionId: 'x-top', gridQuestionId: 'g-1', gridActionId: 'x-grid', include: true },
|
|
442
|
+
{ type: 'action.setGridActionRef', actionId: 'x-top', gridQuestionId: 'g-1', gridActionId: 'x-grid-2', include: true },
|
|
443
|
+
],
|
|
444
|
+
withActions(),
|
|
445
|
+
)
|
|
446
|
+
assert.deepEqual(action(doc, 'x-top')?.['actionIdsByGridQuestionId'], { 'g-1': ['x-grid', 'x-grid-2'] })
|
|
447
|
+
|
|
448
|
+
doc = apply(
|
|
449
|
+
[{ type: 'action.setGridActionRef', actionId: 'x-top', gridQuestionId: 'g-1', gridActionId: 'x-grid-2', include: true, atIndex: 0 }],
|
|
450
|
+
doc,
|
|
451
|
+
)
|
|
452
|
+
assert.deepEqual(action(doc, 'x-top')?.['actionIdsByGridQuestionId'], { 'g-1': ['x-grid-2', 'x-grid'] })
|
|
453
|
+
|
|
454
|
+
doc = apply(
|
|
455
|
+
[
|
|
456
|
+
{ type: 'action.setGridActionRef', actionId: 'x-top', gridQuestionId: 'g-1', gridActionId: 'x-grid', include: false },
|
|
457
|
+
{ type: 'action.setGridActionRef', actionId: 'x-top', gridQuestionId: 'g-1', gridActionId: 'x-grid-2', include: false },
|
|
458
|
+
],
|
|
459
|
+
doc,
|
|
460
|
+
)
|
|
461
|
+
// Emptied sequence drops its grid key; emptied map is omitted.
|
|
462
|
+
assert.deepEqual(action(doc, 'x-top'), { kind: 'topLevelAction', label: 'Kjør' })
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
it('lets two concurrent reference inserts both survive', () => {
|
|
466
|
+
const base = apply(
|
|
467
|
+
[
|
|
468
|
+
{ type: 'action.createTyped', actionId: 'x-grid-2', kind: 'gridAction' },
|
|
469
|
+
{ type: 'gridColumn.setAction', questionId: 'g-1', actionId: 'x-grid-2', include: true },
|
|
470
|
+
],
|
|
471
|
+
withActions(),
|
|
472
|
+
)
|
|
473
|
+
const mine = { type: 'action.setGridActionRef', actionId: 'x-top', gridQuestionId: 'g-1', gridActionId: 'x-grid', include: true } as const
|
|
474
|
+
const theirs = { type: 'action.setGridActionRef', actionId: 'x-top', gridQuestionId: 'g-1', gridActionId: 'x-grid-2', include: true } as const
|
|
475
|
+
|
|
476
|
+
for (const doc of [apply([mine, theirs], base), apply([theirs, mine], base)]) {
|
|
477
|
+
const sequence = (action(doc, 'x-top')?.['actionIdsByGridQuestionId'] as Record<string, string[]>)['g-1']
|
|
478
|
+
assert.deepEqual([...(sequence ?? [])].sort(), ['x-grid', 'x-grid-2'])
|
|
479
|
+
}
|
|
480
|
+
})
|
|
481
|
+
|
|
482
|
+
it('refuses a reference to an action the grid does not own, or to a top-level one', () => {
|
|
483
|
+
const base = apply([{ type: 'action.createTyped', actionId: 'x-orphan', kind: 'gridAction' }], withActions())
|
|
484
|
+
|
|
485
|
+
throwsConflict(
|
|
486
|
+
() =>
|
|
487
|
+
apply(
|
|
488
|
+
[{ type: 'action.setGridActionRef', actionId: 'x-top', gridQuestionId: 'g-1', gridActionId: 'x-orphan', include: true }],
|
|
489
|
+
base,
|
|
490
|
+
),
|
|
491
|
+
'unowned grid action',
|
|
492
|
+
)
|
|
493
|
+
throwsConflict(
|
|
494
|
+
() =>
|
|
495
|
+
apply(
|
|
496
|
+
[{ type: 'action.setGridActionRef', actionId: 'x-grid', gridQuestionId: 'g-1', gridActionId: 'x-grid', include: true }],
|
|
497
|
+
base,
|
|
498
|
+
),
|
|
499
|
+
'setGridActionRef is topLevelAction-only',
|
|
500
|
+
)
|
|
501
|
+
})
|
|
502
|
+
|
|
503
|
+
it('writes canonical metadata and refuses the ambiguous shapes', () => {
|
|
504
|
+
const base = withActions()
|
|
505
|
+
|
|
506
|
+
const all = apply([{ type: 'action.setMetadata', actionId: 'x-grid', questionId: 'c-1', metadata: { all: true } }], base)
|
|
507
|
+
assert.deepEqual(action(all, 'x-grid')?.['metadataByQuestionId'], { 'c-1': { all: true } })
|
|
508
|
+
|
|
509
|
+
const bounded = apply(
|
|
510
|
+
[{ type: 'action.setMetadata', actionId: 'x-grid', questionId: 'c-1', metadata: { from: 'a', to: 'b' } }],
|
|
511
|
+
base,
|
|
512
|
+
)
|
|
513
|
+
assert.deepEqual(action(bounded, 'x-grid')?.['metadataByQuestionId'], { 'c-1': { from: 'a', to: 'b' } })
|
|
514
|
+
|
|
515
|
+
// A bare {} and all+bounds are both ambiguous, and both are refused at the schema.
|
|
516
|
+
for (const metadata of [{}, { all: true, from: 'a' }]) {
|
|
517
|
+
const result = templateOpSchema({
|
|
518
|
+
type: 'action.setMetadata',
|
|
519
|
+
actionId: 'x-grid',
|
|
520
|
+
questionId: 'c-1',
|
|
521
|
+
metadata,
|
|
522
|
+
})
|
|
523
|
+
assert.ok(result instanceof type.errors, JSON.stringify(metadata))
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const cleared = apply([{ type: 'action.setMetadata', actionId: 'x-grid', questionId: 'c-1', metadata: null }], all)
|
|
527
|
+
assert.deepEqual(action(cleared, 'x-grid'), { kind: 'gridAction', label: 'Kopier', actionType: 'COPY' })
|
|
528
|
+
})
|
|
529
|
+
|
|
530
|
+
it('keeps all:true through canonical reduction and the hash', () => {
|
|
531
|
+
// The marker's whole purpose: DOC-LAW-2 strips defaults and empties, so an all-to-all selection
|
|
532
|
+
// encoded as `{}` or as nulls would vanish and become "not selected".
|
|
533
|
+
const doc = apply([{ type: 'action.setMetadata', actionId: 'x-grid', questionId: 'c-1', metadata: { all: true } }], withActions())
|
|
534
|
+
|
|
535
|
+
const reduced = reduceToMinimalForm(doc, { isDefault: templateDocumentIsDefault })
|
|
536
|
+
const json = canonicalJson(reduced)
|
|
537
|
+
|
|
538
|
+
assert.ok(json.includes('"all":true'), 'all:true must survive reduction')
|
|
539
|
+
assert.deepEqual(findDocLawViolations(reduced), [])
|
|
540
|
+
assert.equal(validateTemplateDocument(doc).ok, true)
|
|
541
|
+
})
|
|
542
|
+
|
|
543
|
+
it('refuses ambiguous metadata in the REDUCER too, not only at the wire', () => {
|
|
544
|
+
// The wire schema rejects these, so this reaches past it deliberately: the reducer is replayed
|
|
545
|
+
// over a stored `collab_ops` log without re-validating each op, which is the path an op written
|
|
546
|
+
// by an older or bypassed client actually takes. Two lines, same rule — the pattern
|
|
547
|
+
// `questionFieldPathRefusal` already follows for the grid-owned paths.
|
|
548
|
+
for (const metadata of [{}, { all: true, from: 'a' }, { all: true, to: 'b' }]) {
|
|
549
|
+
throwsConflict(
|
|
550
|
+
() =>
|
|
551
|
+
apply(
|
|
552
|
+
[
|
|
553
|
+
{
|
|
554
|
+
type: 'action.setMetadata',
|
|
555
|
+
actionId: 'x-grid',
|
|
556
|
+
questionId: 'c-1',
|
|
557
|
+
metadata,
|
|
558
|
+
} as unknown as TemplateOp,
|
|
559
|
+
],
|
|
560
|
+
withActions(),
|
|
561
|
+
),
|
|
562
|
+
JSON.stringify(metadata),
|
|
563
|
+
)
|
|
564
|
+
}
|
|
565
|
+
})
|
|
566
|
+
|
|
567
|
+
it('refuses metadata for a question that is not a column of the owning grid', () => {
|
|
568
|
+
throwsConflict(
|
|
569
|
+
() =>
|
|
570
|
+
apply(
|
|
571
|
+
[{ type: 'action.setMetadata', actionId: 'x-grid', questionId: 'q-top', metadata: { all: true } }],
|
|
572
|
+
withActions(),
|
|
573
|
+
),
|
|
574
|
+
'top-level question is not a column',
|
|
575
|
+
)
|
|
576
|
+
})
|
|
577
|
+
|
|
578
|
+
it('scrubs references when a grid action is removed or deleted', () => {
|
|
579
|
+
const base = apply(
|
|
580
|
+
[{ type: 'action.setGridActionRef', actionId: 'x-top', gridQuestionId: 'g-1', gridActionId: 'x-grid', include: true }],
|
|
581
|
+
withActions(),
|
|
582
|
+
)
|
|
583
|
+
assert.deepEqual(action(base, 'x-top')?.['actionIdsByGridQuestionId'], { 'g-1': ['x-grid'] })
|
|
584
|
+
|
|
585
|
+
// Un-owning it makes the top-level reference unexpressible, so it goes at the same moment.
|
|
586
|
+
const unowned = apply([{ type: 'gridColumn.setAction', questionId: 'g-1', actionId: 'x-grid', include: false }], base)
|
|
587
|
+
assert.equal('actionIdsByGridQuestionId' in (action(unowned, 'x-top') ?? {}), false)
|
|
588
|
+
assert.equal('actionIds' in (grid(unowned)?.presentation ?? {}), false)
|
|
589
|
+
|
|
590
|
+
// Deleting it scrubs both homes too.
|
|
591
|
+
const deleted = apply([{ type: 'action.delete', actionId: 'x-grid' }], base)
|
|
592
|
+
assert.equal(action(deleted, 'x-grid'), undefined)
|
|
593
|
+
assert.equal('actionIdsByGridQuestionId' in (action(deleted, 'x-top') ?? {}), false)
|
|
594
|
+
assert.equal('actionIds' in (grid(deleted)?.presentation ?? {}), false)
|
|
595
|
+
})
|
|
596
|
+
|
|
597
|
+
it('scrubs a deleted column from the grid filter list, layout and widths', () => {
|
|
598
|
+
const doc = apply(
|
|
599
|
+
[
|
|
600
|
+
{ type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-1', include: true },
|
|
601
|
+
{ type: 'gridColumn.setFilter', questionId: 'g-1', columnQuestionId: 'c-2', include: true },
|
|
602
|
+
{ type: 'gridColumn.setLayout', questionId: 'g-1', columnQuestionId: 'c-1', placement: { row: 0, cell: 0 } },
|
|
603
|
+
{ type: 'question.updateField', questionId: 'g-1', field: 'presentation.defaultColumnWidthsByQuestionId.c-1', value: 90 },
|
|
604
|
+
{ type: 'question.delete', questionId: 'c-1' },
|
|
605
|
+
],
|
|
606
|
+
scaffold(),
|
|
607
|
+
)
|
|
608
|
+
|
|
609
|
+
assert.deepEqual(grid(doc)?.presentation?.filterQuestionIds, ['c-2'])
|
|
610
|
+
assert.equal(grid(doc)?.presentation?.rowEditor, undefined)
|
|
611
|
+
assert.equal(grid(doc)?.presentation?.defaultColumnWidthsByQuestionId, undefined)
|
|
612
|
+
})
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
// ─────────────────────────────── §1 bundle fragment ───────────────────────────────
|
|
616
|
+
|
|
617
|
+
describe('the bundle carries a mapping fragment', () => {
|
|
618
|
+
it('types the four graph collections beside the question collections', () => {
|
|
619
|
+
// The shared half of §1: the shape exists so a pick cannot lose mapping. Reference CLOSURE is
|
|
620
|
+
// the library writer's job (asma-bunjs-editor) and is deliberately not asserted here.
|
|
621
|
+
const bundle = {
|
|
622
|
+
rootQuestionId: 'g-1',
|
|
623
|
+
questionsById: {
|
|
624
|
+
'g-1': { type: 'QuestionGrid' as const, grid: { columnIds: ['c-1'] } },
|
|
625
|
+
'c-1': { type: 'TextShort' as const },
|
|
626
|
+
},
|
|
627
|
+
tabsById: { 't-1': { label: 'Første', layout: { placementsByQuestionId: { 'c-1': { row: 0, cell: 0 } } } } },
|
|
628
|
+
actionsById: { 'x-1': { kind: 'gridAction', actionType: 'COPY' as const } },
|
|
629
|
+
dataMappingsById: { 'm-1': { sourceId: 'adopus-legacy', rootNodeId: 'n-1', bindingOrder: ['b-1'] } },
|
|
630
|
+
mappingNodesById: { 'n-1': { entityId: 'Actor', filterOrder: ['f-1'] } },
|
|
631
|
+
mappingBindingsById: {
|
|
632
|
+
'b-1': { nodeId: 'n-1', fieldId: 'Navn', target: { kind: 'gridColumn' as const, gridQuestionId: 'g-1', columnQuestionId: 'c-1' } },
|
|
633
|
+
},
|
|
634
|
+
mappingFiltersById: { 'f-1': { fieldId: 'Status', operator: 'in', values: ['A', 'B'] } },
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// DOC-LAW-1/2 hold over the fragment: keyed records, primitive order arrays, nothing empty.
|
|
638
|
+
assert.deepEqual(findDocLawViolations(bundle), [])
|
|
639
|
+
assert.deepEqual(Object.keys(bundle.mappingFiltersById['f-1']), ['fieldId', 'operator', 'values'])
|
|
640
|
+
})
|
|
641
|
+
})
|