asma-core-helpers 0.26.0 → 0.27.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.map +1 -1
- package/lib/collaboration/applyOperation.js +237 -33
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/index.d.ts +1 -0
- package/lib/collaboration/index.d.ts.map +1 -1
- package/lib/collaboration/index.js +1 -0
- package/lib/collaboration/index.js.map +1 -1
- package/lib/collaboration/operations.d.ts +35 -1
- 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 +101 -1
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +109 -6
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/collaboration/templateDocument.d.ts +28 -1
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.d.ts +4 -0
- package/lib/collaboration/testHelpers/documentGenerator.d.ts.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.js +100 -4
- package/lib/collaboration/testHelpers/documentGenerator.js.map +1 -1
- package/package.json +1 -1
- package/src/collaboration/applyOperation.test.ts +240 -1
- package/src/collaboration/applyOperation.ts +280 -36
- package/src/collaboration/index.ts +1 -0
- package/src/collaboration/operations.ts +35 -0
- package/src/collaboration/property.test.ts +20 -0
- package/src/collaboration/schemas.test.ts +87 -1
- package/src/collaboration/schemas.ts +137 -1
- package/src/collaboration/templateDocument.ts +27 -1
- package/src/collaboration/testHelpers/documentGenerator.ts +101 -5
|
@@ -5,7 +5,6 @@ import type {
|
|
|
5
5
|
QnrQuestion,
|
|
6
6
|
QnrTemplateDocument,
|
|
7
7
|
QuestionId,
|
|
8
|
-
RuleCondition,
|
|
9
8
|
} from './templateDocument.js'
|
|
10
9
|
import { bindingTargetKey } from './templateDocument.js'
|
|
11
10
|
|
|
@@ -115,6 +114,15 @@ const omitKey = <V>(record: Record<string, V>, key: string): Record<string, V> =
|
|
|
115
114
|
return rest
|
|
116
115
|
}
|
|
117
116
|
|
|
117
|
+
const requireGridQuestion = (doc: QnrTemplateDocument, questionId: QuestionId): QnrQuestion => {
|
|
118
|
+
const question = doc.questionsById?.[questionId]
|
|
119
|
+
if (!question) throw new OperationConflictError(`Unknown question "${questionId}"`)
|
|
120
|
+
if (question.type !== 'QuestionGrid') {
|
|
121
|
+
throw new OperationConflictError(`Question "${questionId}" is not a question grid`)
|
|
122
|
+
}
|
|
123
|
+
return question
|
|
124
|
+
}
|
|
125
|
+
|
|
118
126
|
/**
|
|
119
127
|
* Spreads `patch` onto `base`, **omitting** keys whose value is `undefined` rather than
|
|
120
128
|
* assigning it. Required by `exactOptionalPropertyTypes`: `{ ...doc, x: undefined }` is
|
|
@@ -151,6 +159,10 @@ const OPTIONAL_COLLECTIONS = [
|
|
|
151
159
|
'visibilityRuleOrderByQuestionId',
|
|
152
160
|
'highlightRulesById',
|
|
153
161
|
'highlightRuleOrderByQuestionId',
|
|
162
|
+
'narrativeRulesById',
|
|
163
|
+
'narrativeRuleOrderByQuestionId',
|
|
164
|
+
'qnrRulesById',
|
|
165
|
+
'qnrRuleOrderByQuestionId',
|
|
154
166
|
'dataMappingsById',
|
|
155
167
|
'mappingNodesById',
|
|
156
168
|
'mappingBindingsById',
|
|
@@ -202,13 +214,13 @@ const setOrRemoveOrder = (
|
|
|
202
214
|
* one owner: setting it under a different question MOVES it there, so it can never sit in two
|
|
203
215
|
* order arrays at once — and the previous owner's key goes when its last rule leaves (DOC-LAW-2).
|
|
204
216
|
*/
|
|
205
|
-
const setScopedRule = (
|
|
206
|
-
rulesById: Record<string,
|
|
217
|
+
const setScopedRule = <T extends object>(
|
|
218
|
+
rulesById: Record<string, T> | undefined,
|
|
207
219
|
orderByQuestionId: Record<QuestionId, string[]> | undefined,
|
|
208
220
|
ruleId: string,
|
|
209
221
|
questionId: QuestionId,
|
|
210
|
-
|
|
211
|
-
): { rulesById: Record<string,
|
|
222
|
+
rule: T,
|
|
223
|
+
): { rulesById: Record<string, T>; orderByQuestionId: Record<QuestionId, string[]> } => {
|
|
212
224
|
const orders: Record<QuestionId, string[]> = {}
|
|
213
225
|
for (const [ownerId, ruleIds] of Object.entries(orderByQuestionId ?? {})) {
|
|
214
226
|
const kept = ownerId === questionId ? ruleIds : ruleIds.filter((id) => id !== ruleId)
|
|
@@ -217,7 +229,24 @@ const setScopedRule = (
|
|
|
217
229
|
const order = orders[questionId] ?? []
|
|
218
230
|
orders[questionId] = order.includes(ruleId) ? order : [...order, ruleId]
|
|
219
231
|
return {
|
|
220
|
-
rulesById: { ...(rulesById ?? {}), [ruleId]:
|
|
232
|
+
rulesById: { ...(rulesById ?? {}), [ruleId]: rule },
|
|
233
|
+
orderByQuestionId: orders,
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Removes one scoped rule and any order key emptied by that removal. */
|
|
238
|
+
const deleteScopedRule = <T>(
|
|
239
|
+
rulesById: Record<string, T> | undefined,
|
|
240
|
+
orderByQuestionId: Record<QuestionId, string[]> | undefined,
|
|
241
|
+
ruleId: string,
|
|
242
|
+
): { rulesById: Record<string, T>; orderByQuestionId: Record<QuestionId, string[]> } => {
|
|
243
|
+
const orders: Record<QuestionId, string[]> = {}
|
|
244
|
+
for (const [questionId, ruleIds] of Object.entries(orderByQuestionId ?? {})) {
|
|
245
|
+
const kept = ruleIds.filter((id) => id !== ruleId)
|
|
246
|
+
if (kept.length > 0) orders[questionId] = kept
|
|
247
|
+
}
|
|
248
|
+
return {
|
|
249
|
+
rulesById: rulesById ? omitKey(rulesById, ruleId) : {},
|
|
221
250
|
orderByQuestionId: orders,
|
|
222
251
|
}
|
|
223
252
|
}
|
|
@@ -287,29 +316,97 @@ const scrubBindingOrders = (doc: QnrTemplateDocument, bindingIds: ReadonlySet<st
|
|
|
287
316
|
return changed ? { ...doc, dataMappingsById: next } : doc
|
|
288
317
|
}
|
|
289
318
|
|
|
290
|
-
|
|
319
|
+
type ScrubbedPresentation = { value: Record<string, unknown>; changed: boolean }
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Removes a question key from convention-owned presentation maps at any depth. The
|
|
323
|
+
* presentation bag deliberately has an open index, so enumerating today's layout and
|
|
324
|
+
* width maps would make tomorrow's map retain dangling column state.
|
|
325
|
+
*/
|
|
326
|
+
const scrubPresentationQuestionMaps = (
|
|
327
|
+
value: Record<string, unknown>,
|
|
328
|
+
questionId: QuestionId,
|
|
329
|
+
): ScrubbedPresentation => {
|
|
330
|
+
let changed = false
|
|
331
|
+
const next: Record<string, unknown> = {}
|
|
332
|
+
for (const [key, child] of Object.entries(value)) {
|
|
333
|
+
if (typeof child !== 'object' || child === null || Array.isArray(child)) {
|
|
334
|
+
next[key] = child
|
|
335
|
+
continue
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (key.endsWith('ByQuestionId')) {
|
|
339
|
+
const map = child as Record<string, unknown>
|
|
340
|
+
if (!(questionId in map)) {
|
|
341
|
+
next[key] = child
|
|
342
|
+
continue
|
|
343
|
+
}
|
|
344
|
+
changed = true
|
|
345
|
+
const kept = omitKey(map, questionId)
|
|
346
|
+
if (Object.keys(kept).length > 0) next[key] = kept
|
|
347
|
+
continue
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const nested = scrubPresentationQuestionMaps(child as Record<string, unknown>, questionId)
|
|
351
|
+
changed ||= nested.changed
|
|
352
|
+
if (!nested.changed || Object.keys(nested.value).length > 0) next[key] = nested.value
|
|
353
|
+
}
|
|
354
|
+
return { value: changed ? next : value, changed }
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Removes a deleted question's id from every record owned by a grid: its ordered
|
|
359
|
+
* columns, authored row cells, and convention-named presentation maps.
|
|
360
|
+
*/
|
|
291
361
|
const dropFromGridColumns = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTemplateDocument => {
|
|
292
|
-
if (!doc.questionsById) return doc
|
|
293
362
|
let changed = false
|
|
294
|
-
const
|
|
295
|
-
for (const [qid, question] of Object.entries(doc.questionsById)) {
|
|
363
|
+
const nextQuestions: typeof doc.questionsById = {}
|
|
364
|
+
for (const [qid, question] of Object.entries(doc.questionsById ?? {})) {
|
|
296
365
|
// Defensive: the per-type bags are open, so a malformed `columnIds` must not crash
|
|
297
366
|
// the reducer — it is a schema violation for validation to catch, not a crash here.
|
|
298
367
|
const columnIds = question.grid?.columnIds
|
|
299
|
-
|
|
300
|
-
|
|
368
|
+
let nextQuestion: QnrQuestion = question
|
|
369
|
+
if (Array.isArray(columnIds) && columnIds.includes(questionId)) {
|
|
370
|
+
changed = true
|
|
371
|
+
const kept = columnIds.filter((id) => id !== questionId)
|
|
372
|
+
const grid = { ...question.grid }
|
|
373
|
+
if (kept.length === 0) delete grid.columnIds
|
|
374
|
+
else grid.columnIds = kept
|
|
375
|
+
nextQuestion = { ...nextQuestion, grid }
|
|
376
|
+
if (Object.keys(grid).length === 0) delete nextQuestion.grid
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (question.presentation) {
|
|
380
|
+
const scrubbed = scrubPresentationQuestionMaps(question.presentation, questionId)
|
|
381
|
+
if (scrubbed.changed) {
|
|
382
|
+
changed = true
|
|
383
|
+
nextQuestion = { ...nextQuestion }
|
|
384
|
+
if (Object.keys(scrubbed.value).length === 0) delete nextQuestion.presentation
|
|
385
|
+
else nextQuestion.presentation = scrubbed.value
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
nextQuestions[qid] = nextQuestion
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const nextRows: typeof doc.gridRowsById = {}
|
|
392
|
+
for (const [rowId, row] of Object.entries(doc.gridRowsById ?? {})) {
|
|
393
|
+
if (!row.cells || !(questionId in row.cells)) {
|
|
394
|
+
nextRows[rowId] = row
|
|
301
395
|
continue
|
|
302
396
|
}
|
|
303
397
|
changed = true
|
|
304
|
-
const
|
|
305
|
-
const
|
|
306
|
-
if (
|
|
307
|
-
else
|
|
308
|
-
|
|
309
|
-
if (Object.keys(grid).length === 0) delete nextQuestion.grid
|
|
310
|
-
next[qid] = nextQuestion
|
|
398
|
+
const cells = omitKey(row.cells, questionId)
|
|
399
|
+
const nextRow = { ...row }
|
|
400
|
+
if (Object.keys(cells).length === 0) delete nextRow.cells
|
|
401
|
+
else nextRow.cells = cells
|
|
402
|
+
nextRows[rowId] = nextRow
|
|
311
403
|
}
|
|
312
|
-
|
|
404
|
+
|
|
405
|
+
if (!changed) return doc
|
|
406
|
+
return patchDocument(doc, {
|
|
407
|
+
questionsById: doc.questionsById ? nextQuestions : undefined,
|
|
408
|
+
gridRowsById: doc.gridRowsById ? nextRows : undefined,
|
|
409
|
+
})
|
|
313
410
|
}
|
|
314
411
|
|
|
315
412
|
export const applyOperation = (document: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument => {
|
|
@@ -371,6 +468,9 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
371
468
|
if (!doc.questionsById?.[op.questionId]) {
|
|
372
469
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
373
470
|
}
|
|
471
|
+
if (!doc.questionOrder.includes(op.questionId)) {
|
|
472
|
+
throw new OperationConflictError(`Question "${op.questionId}" is not top-level`)
|
|
473
|
+
}
|
|
374
474
|
return { ...doc, questionOrder: moveInOrder(doc.questionOrder, op.questionId, op.toIndex) }
|
|
375
475
|
}
|
|
376
476
|
|
|
@@ -395,6 +495,16 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
395
495
|
if (highlightRulesById) {
|
|
396
496
|
highlightRulesById = highlightRuleIds.reduce((acc, id) => omitKey(acc, id), highlightRulesById)
|
|
397
497
|
}
|
|
498
|
+
const narrativeRuleIds = doc.narrativeRuleOrderByQuestionId?.[op.questionId] ?? []
|
|
499
|
+
let narrativeRulesById = doc.narrativeRulesById
|
|
500
|
+
if (narrativeRulesById) {
|
|
501
|
+
narrativeRulesById = narrativeRuleIds.reduce((acc, id) => omitKey(acc, id), narrativeRulesById)
|
|
502
|
+
}
|
|
503
|
+
const qnrRuleIds = doc.qnrRuleOrderByQuestionId?.[op.questionId] ?? []
|
|
504
|
+
let qnrRulesById = doc.qnrRulesById
|
|
505
|
+
if (qnrRulesById) {
|
|
506
|
+
qnrRulesById = qnrRuleIds.reduce((acc, id) => omitKey(acc, id), qnrRulesById)
|
|
507
|
+
}
|
|
398
508
|
const withoutQuestion = patchDocument(doc, {
|
|
399
509
|
questionsById: doc.questionsById && omitKey(doc.questionsById, op.questionId),
|
|
400
510
|
questionOrder: doc.questionOrder.filter((id) => id !== op.questionId),
|
|
@@ -410,6 +520,13 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
410
520
|
highlightRuleOrderByQuestionId:
|
|
411
521
|
doc.highlightRuleOrderByQuestionId &&
|
|
412
522
|
omitKey(doc.highlightRuleOrderByQuestionId, op.questionId),
|
|
523
|
+
narrativeRulesById,
|
|
524
|
+
narrativeRuleOrderByQuestionId:
|
|
525
|
+
doc.narrativeRuleOrderByQuestionId &&
|
|
526
|
+
omitKey(doc.narrativeRuleOrderByQuestionId, op.questionId),
|
|
527
|
+
qnrRulesById,
|
|
528
|
+
qnrRuleOrderByQuestionId:
|
|
529
|
+
doc.qnrRuleOrderByQuestionId && omitKey(doc.qnrRuleOrderByQuestionId, op.questionId),
|
|
413
530
|
})
|
|
414
531
|
// A binding pointing at a deleted question is an unresolvable reference the
|
|
415
532
|
// compiler would reject at publication; drop it with its target. A grid column
|
|
@@ -417,6 +534,83 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
417
534
|
return dropBindingsForQuestion(dropFromGridColumns(withoutQuestion, op.questionId), op.questionId)
|
|
418
535
|
}
|
|
419
536
|
|
|
537
|
+
case 'gridColumn.create': {
|
|
538
|
+
const gridQuestion = requireGridQuestion(doc, op.questionId)
|
|
539
|
+
if (doc.questionsById?.[op.columnQuestionId]) {
|
|
540
|
+
throw new OperationConflictError(`Question "${op.columnQuestionId}" already exists`)
|
|
541
|
+
}
|
|
542
|
+
const columnIds = gridQuestion.grid?.columnIds ?? []
|
|
543
|
+
return {
|
|
544
|
+
...doc,
|
|
545
|
+
questionsById: {
|
|
546
|
+
...doc.questionsById,
|
|
547
|
+
[op.questionId]: {
|
|
548
|
+
...gridQuestion,
|
|
549
|
+
grid: {
|
|
550
|
+
...(gridQuestion.grid ?? {}),
|
|
551
|
+
columnIds: insertAt(columnIds, op.columnQuestionId, op.atIndex),
|
|
552
|
+
},
|
|
553
|
+
},
|
|
554
|
+
[op.columnQuestionId]: { type: op.questionType },
|
|
555
|
+
},
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
case 'gridColumn.move': {
|
|
560
|
+
const gridQuestion = requireGridQuestion(doc, op.questionId)
|
|
561
|
+
const columnIds = gridQuestion.grid?.columnIds
|
|
562
|
+
if (!doc.questionsById?.[op.columnQuestionId] || !columnIds?.includes(op.columnQuestionId)) {
|
|
563
|
+
throw new OperationConflictError(
|
|
564
|
+
`Question "${op.columnQuestionId}" does not belong to grid "${op.questionId}"`,
|
|
565
|
+
)
|
|
566
|
+
}
|
|
567
|
+
return {
|
|
568
|
+
...doc,
|
|
569
|
+
questionsById: {
|
|
570
|
+
...doc.questionsById,
|
|
571
|
+
[op.questionId]: {
|
|
572
|
+
...gridQuestion,
|
|
573
|
+
grid: {
|
|
574
|
+
...gridQuestion.grid,
|
|
575
|
+
columnIds: moveInOrder(columnIds, op.columnQuestionId, op.toIndex),
|
|
576
|
+
},
|
|
577
|
+
},
|
|
578
|
+
},
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
case 'gridColumn.setLayout': {
|
|
583
|
+
const gridQuestion = requireGridQuestion(doc, op.questionId)
|
|
584
|
+
const columnIds = gridQuestion.grid?.columnIds
|
|
585
|
+
if (!doc.questionsById?.[op.columnQuestionId] || !columnIds?.includes(op.columnQuestionId)) {
|
|
586
|
+
throw new OperationConflictError(
|
|
587
|
+
`Question "${op.columnQuestionId}" does not belong to grid "${op.questionId}"`,
|
|
588
|
+
)
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
const presentation = { ...(gridQuestion.presentation ?? {}) }
|
|
592
|
+
const rowEditor = { ...(presentation.rowEditor ?? {}) }
|
|
593
|
+
const layoutByQuestionId = { ...(rowEditor.layoutByQuestionId ?? {}) }
|
|
594
|
+
if (op.placement === null) delete layoutByQuestionId[op.columnQuestionId]
|
|
595
|
+
else layoutByQuestionId[op.columnQuestionId] = op.placement
|
|
596
|
+
|
|
597
|
+
if (Object.keys(layoutByQuestionId).length === 0) delete rowEditor.layoutByQuestionId
|
|
598
|
+
else rowEditor.layoutByQuestionId = layoutByQuestionId
|
|
599
|
+
if (Object.keys(rowEditor).length === 0) delete presentation.rowEditor
|
|
600
|
+
else presentation.rowEditor = rowEditor
|
|
601
|
+
|
|
602
|
+
const nextQuestion = { ...gridQuestion }
|
|
603
|
+
if (Object.keys(presentation).length === 0) delete nextQuestion.presentation
|
|
604
|
+
else nextQuestion.presentation = presentation
|
|
605
|
+
return {
|
|
606
|
+
...doc,
|
|
607
|
+
questionsById: {
|
|
608
|
+
...doc.questionsById,
|
|
609
|
+
[op.questionId]: nextQuestion,
|
|
610
|
+
},
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
420
614
|
case 'gridRow.create': {
|
|
421
615
|
if (!doc.questionsById?.[op.questionId]) {
|
|
422
616
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
@@ -870,21 +1064,20 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
870
1064
|
doc.visibilityRuleOrderByQuestionId,
|
|
871
1065
|
op.ruleId,
|
|
872
1066
|
op.questionId,
|
|
873
|
-
op.condition,
|
|
1067
|
+
{ condition: op.condition },
|
|
874
1068
|
)
|
|
875
1069
|
return { ...doc, visibilityRulesById: rulesById, visibilityRuleOrderByQuestionId: orderByQuestionId }
|
|
876
1070
|
}
|
|
877
1071
|
|
|
878
1072
|
case 'visibilityRule.delete': {
|
|
879
|
-
const
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
]),
|
|
1073
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(
|
|
1074
|
+
doc.visibilityRulesById,
|
|
1075
|
+
doc.visibilityRuleOrderByQuestionId,
|
|
1076
|
+
op.ruleId,
|
|
884
1077
|
)
|
|
885
1078
|
return patchDocument(doc, {
|
|
886
|
-
visibilityRulesById:
|
|
887
|
-
visibilityRuleOrderByQuestionId:
|
|
1079
|
+
visibilityRulesById: rulesById,
|
|
1080
|
+
visibilityRuleOrderByQuestionId: orderByQuestionId,
|
|
888
1081
|
})
|
|
889
1082
|
}
|
|
890
1083
|
|
|
@@ -897,21 +1090,72 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
897
1090
|
doc.highlightRuleOrderByQuestionId,
|
|
898
1091
|
op.ruleId,
|
|
899
1092
|
op.questionId,
|
|
900
|
-
op.condition,
|
|
1093
|
+
{ condition: op.condition },
|
|
901
1094
|
)
|
|
902
1095
|
return { ...doc, highlightRulesById: rulesById, highlightRuleOrderByQuestionId: orderByQuestionId }
|
|
903
1096
|
}
|
|
904
1097
|
|
|
905
1098
|
case 'highlightRule.delete': {
|
|
906
|
-
const
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1099
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(
|
|
1100
|
+
doc.highlightRulesById,
|
|
1101
|
+
doc.highlightRuleOrderByQuestionId,
|
|
1102
|
+
op.ruleId,
|
|
1103
|
+
)
|
|
1104
|
+
return patchDocument(doc, {
|
|
1105
|
+
highlightRulesById: rulesById,
|
|
1106
|
+
highlightRuleOrderByQuestionId: orderByQuestionId,
|
|
1107
|
+
})
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
case 'narrativeRule.set': {
|
|
1111
|
+
if (!doc.questionsById?.[op.questionId]) {
|
|
1112
|
+
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
1113
|
+
}
|
|
1114
|
+
const { rulesById, orderByQuestionId } = setScopedRule(
|
|
1115
|
+
doc.narrativeRulesById,
|
|
1116
|
+
doc.narrativeRuleOrderByQuestionId,
|
|
1117
|
+
op.ruleId,
|
|
1118
|
+
op.questionId,
|
|
1119
|
+
{ condition: op.condition },
|
|
1120
|
+
)
|
|
1121
|
+
return { ...doc, narrativeRulesById: rulesById, narrativeRuleOrderByQuestionId: orderByQuestionId }
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
case 'narrativeRule.delete': {
|
|
1125
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(
|
|
1126
|
+
doc.narrativeRulesById,
|
|
1127
|
+
doc.narrativeRuleOrderByQuestionId,
|
|
1128
|
+
op.ruleId,
|
|
1129
|
+
)
|
|
1130
|
+
return patchDocument(doc, {
|
|
1131
|
+
narrativeRulesById: rulesById,
|
|
1132
|
+
narrativeRuleOrderByQuestionId: orderByQuestionId,
|
|
1133
|
+
})
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
case 'qnrRule.set': {
|
|
1137
|
+
if (!doc.questionsById?.[op.questionId]) {
|
|
1138
|
+
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
1139
|
+
}
|
|
1140
|
+
const { rulesById, orderByQuestionId } = setScopedRule(
|
|
1141
|
+
doc.qnrRulesById,
|
|
1142
|
+
doc.qnrRuleOrderByQuestionId,
|
|
1143
|
+
op.ruleId,
|
|
1144
|
+
op.questionId,
|
|
1145
|
+
{ condition: op.condition, templateFamilyId: op.templateFamilyId },
|
|
1146
|
+
)
|
|
1147
|
+
return { ...doc, qnrRulesById: rulesById, qnrRuleOrderByQuestionId: orderByQuestionId }
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
case 'qnrRule.delete': {
|
|
1151
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(
|
|
1152
|
+
doc.qnrRulesById,
|
|
1153
|
+
doc.qnrRuleOrderByQuestionId,
|
|
1154
|
+
op.ruleId,
|
|
911
1155
|
)
|
|
912
1156
|
return patchDocument(doc, {
|
|
913
|
-
|
|
914
|
-
|
|
1157
|
+
qnrRulesById: rulesById,
|
|
1158
|
+
qnrRuleOrderByQuestionId: orderByQuestionId,
|
|
915
1159
|
})
|
|
916
1160
|
}
|
|
917
1161
|
|
|
@@ -6,9 +6,12 @@ import type {
|
|
|
6
6
|
DocScalar,
|
|
7
7
|
FilterId,
|
|
8
8
|
HighlightRuleId,
|
|
9
|
+
LayoutPlacement,
|
|
9
10
|
MappingId,
|
|
11
|
+
NarrativeRuleId,
|
|
10
12
|
NodeId,
|
|
11
13
|
QuestionId,
|
|
14
|
+
QnrRuleId,
|
|
12
15
|
RowId,
|
|
13
16
|
RuleCondition,
|
|
14
17
|
TabId,
|
|
@@ -53,6 +56,20 @@ export type TemplateOp =
|
|
|
53
56
|
| { type: 'question.updateField'; questionId: QuestionId; field: string; value: OpValue }
|
|
54
57
|
| { type: 'question.move'; questionId: QuestionId; toIndex: number }
|
|
55
58
|
| { type: 'question.delete'; questionId: QuestionId }
|
|
59
|
+
| {
|
|
60
|
+
type: 'gridColumn.create'
|
|
61
|
+
questionId: QuestionId
|
|
62
|
+
columnQuestionId: QuestionId
|
|
63
|
+
questionType: QuestionType
|
|
64
|
+
atIndex?: number
|
|
65
|
+
}
|
|
66
|
+
| { type: 'gridColumn.move'; questionId: QuestionId; columnQuestionId: QuestionId; toIndex: number }
|
|
67
|
+
| {
|
|
68
|
+
type: 'gridColumn.setLayout'
|
|
69
|
+
questionId: QuestionId
|
|
70
|
+
columnQuestionId: QuestionId
|
|
71
|
+
placement: LayoutPlacement | null
|
|
72
|
+
}
|
|
56
73
|
// Authoring-side grid rows are the template's *predefined* rows; instance-added rows
|
|
57
74
|
// are answer ops (answerOperations.ts). `gridRow.move` is anchor-relative (OQ-V2-24).
|
|
58
75
|
| { type: 'gridRow.create'; questionId: QuestionId; rowId: RowId; label?: string; atIndex?: number }
|
|
@@ -125,6 +142,17 @@ export type TemplateOp =
|
|
|
125
142
|
| { type: 'visibilityRule.delete'; ruleId: VisibilityRuleId }
|
|
126
143
|
| { type: 'highlightRule.set'; ruleId: HighlightRuleId; questionId: QuestionId; condition: RuleCondition }
|
|
127
144
|
| { type: 'highlightRule.delete'; ruleId: HighlightRuleId }
|
|
145
|
+
| { type: 'narrativeRule.set'; ruleId: NarrativeRuleId; questionId: QuestionId; condition: RuleCondition }
|
|
146
|
+
| { type: 'narrativeRule.delete'; ruleId: NarrativeRuleId }
|
|
147
|
+
| {
|
|
148
|
+
type: 'qnrRule.set'
|
|
149
|
+
ruleId: QnrRuleId
|
|
150
|
+
questionId: QuestionId
|
|
151
|
+
condition: RuleCondition
|
|
152
|
+
/** Family id only (OQ-F18/M-066); there is deliberately no version member. */
|
|
153
|
+
templateFamilyId: string
|
|
154
|
+
}
|
|
155
|
+
| { type: 'qnrRule.delete'; ruleId: QnrRuleId }
|
|
128
156
|
|
|
129
157
|
export type TemplateOpType = TemplateOp['type']
|
|
130
158
|
|
|
@@ -142,6 +170,9 @@ const OP_TYPE_COVERAGE: Record<TemplateOpType, true> = {
|
|
|
142
170
|
'question.updateField': true,
|
|
143
171
|
'question.move': true,
|
|
144
172
|
'question.delete': true,
|
|
173
|
+
'gridColumn.create': true,
|
|
174
|
+
'gridColumn.move': true,
|
|
175
|
+
'gridColumn.setLayout': true,
|
|
145
176
|
'gridRow.create': true,
|
|
146
177
|
'gridRow.move': true,
|
|
147
178
|
'gridRow.delete': true,
|
|
@@ -171,6 +202,10 @@ const OP_TYPE_COVERAGE: Record<TemplateOpType, true> = {
|
|
|
171
202
|
'visibilityRule.delete': true,
|
|
172
203
|
'highlightRule.set': true,
|
|
173
204
|
'highlightRule.delete': true,
|
|
205
|
+
'narrativeRule.set': true,
|
|
206
|
+
'narrativeRule.delete': true,
|
|
207
|
+
'qnrRule.set': true,
|
|
208
|
+
'qnrRule.delete': true,
|
|
174
209
|
}
|
|
175
210
|
|
|
176
211
|
export const IMPLEMENTED_OP_TYPES = Object.keys(OP_TYPE_COVERAGE) as readonly TemplateOpType[]
|
|
@@ -7,6 +7,7 @@ import { canonicalJson, hashCanonical, reduceToMinimalForm } from './canonicaliz
|
|
|
7
7
|
import { findDocLawViolations } from './docLaws.js'
|
|
8
8
|
import {
|
|
9
9
|
findDuplicateBindingTargets,
|
|
10
|
+
findQuestionOwnershipViolations,
|
|
10
11
|
templateDocumentIsDefault,
|
|
11
12
|
validateTemplateDocument,
|
|
12
13
|
} from './schemas.js'
|
|
@@ -47,6 +48,23 @@ const runSequence = (seed: number, count: number) => {
|
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
describe('applyOperation determinism', () => {
|
|
51
|
+
it('exercises every ASMA-7676 operation in the seeded generator', () => {
|
|
52
|
+
const { generated } = runSequence(7676, 800)
|
|
53
|
+
const generatedTypes = new Set(generated.ops.map((op) => op.type))
|
|
54
|
+
|
|
55
|
+
for (const type of [
|
|
56
|
+
'gridColumn.create',
|
|
57
|
+
'gridColumn.move',
|
|
58
|
+
'gridColumn.setLayout',
|
|
59
|
+
'narrativeRule.set',
|
|
60
|
+
'narrativeRule.delete',
|
|
61
|
+
'qnrRule.set',
|
|
62
|
+
'qnrRule.delete',
|
|
63
|
+
] as const) {
|
|
64
|
+
assert.ok(generatedTypes.has(type), `generator did not exercise ${type}`)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
|
|
50
68
|
it('reproduces the identical document from the same seed', () => {
|
|
51
69
|
const first = runSequence(42, 200)
|
|
52
70
|
const second = runSequence(42, 200)
|
|
@@ -84,6 +102,8 @@ describe('DOC-LAW invariants under random op sequences', () => {
|
|
|
84
102
|
assert.ok(validated.ok, validated.ok ? '' : validated.summary)
|
|
85
103
|
// …and the one-binding-per-target invariant is never violated by construction.
|
|
86
104
|
assert.deepEqual(findDuplicateBindingTargets(doc), [])
|
|
105
|
+
// Every accepted structural op preserves exactly one owner for each question.
|
|
106
|
+
assert.deepEqual(findQuestionOwnershipViolations(doc), [])
|
|
87
107
|
})
|
|
88
108
|
}
|
|
89
109
|
})
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
findAnswerSchemaDocLawViolations,
|
|
14
14
|
findDocLawDefaultViolations,
|
|
15
15
|
findDuplicateBindingTargets,
|
|
16
|
+
findQuestionOwnershipViolations,
|
|
16
17
|
findSchemaDocLawViolations,
|
|
17
18
|
findTemplateDocumentContractViolations,
|
|
18
19
|
findTemplateSchemaDocLawViolations,
|
|
@@ -72,7 +73,7 @@ describe('document schemas', () => {
|
|
|
72
73
|
'q-1': { type: 'QuestionGrid', grid: { columnIds: ['c-1'], singleRow: true } },
|
|
73
74
|
'c-1': { type: 'TextShort', required: true },
|
|
74
75
|
},
|
|
75
|
-
questionOrder: ['q-1'
|
|
76
|
+
questionOrder: ['q-1'],
|
|
76
77
|
gridRowOrderByQuestionId: { 'q-1': ['r-1'] },
|
|
77
78
|
gridRowsById: { 'r-1': { label: 'Row' } },
|
|
78
79
|
alternativesById: { 'a-1': { label: 'Ja' } },
|
|
@@ -98,6 +99,63 @@ describe('document schemas', () => {
|
|
|
98
99
|
})
|
|
99
100
|
})
|
|
100
101
|
|
|
102
|
+
describe('question ownership invariant', () => {
|
|
103
|
+
it('reports orphaned, top-level-and-column, multi-grid, and dangling column questions', () => {
|
|
104
|
+
const doc: QnrTemplateDocument = {
|
|
105
|
+
documentId: 'tpl-1',
|
|
106
|
+
revision: 0,
|
|
107
|
+
questionsById: {
|
|
108
|
+
'g-1': { type: 'QuestionGrid', grid: { columnIds: ['c-ordered', 'c-shared', 'c-missing'] } },
|
|
109
|
+
'g-2': { type: 'QuestionGrid', grid: { columnIds: ['c-shared'] } },
|
|
110
|
+
'c-ordered': { type: 'TextShort' },
|
|
111
|
+
'c-shared': { type: 'TextShort' },
|
|
112
|
+
'q-orphan': { type: 'TextShort' },
|
|
113
|
+
},
|
|
114
|
+
questionOrder: ['g-1', 'g-2', 'c-ordered'],
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
assert.deepEqual(findQuestionOwnershipViolations(doc), [
|
|
118
|
+
{
|
|
119
|
+
law: 'QUESTION-OWNERSHIP',
|
|
120
|
+
kind: 'dangling-column',
|
|
121
|
+
questionId: 'c-missing',
|
|
122
|
+
gridQuestionIds: ['g-1'],
|
|
123
|
+
path: 'questionsById.g-1.grid.columnIds',
|
|
124
|
+
detail: 'column question is absent from questionsById',
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
law: 'QUESTION-OWNERSHIP',
|
|
128
|
+
kind: 'ordered-and-column',
|
|
129
|
+
questionId: 'c-ordered',
|
|
130
|
+
gridQuestionIds: ['g-1'],
|
|
131
|
+
path: 'questionsById.c-ordered',
|
|
132
|
+
detail: 'question is both top-level and owned by a grid',
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
law: 'QUESTION-OWNERSHIP',
|
|
136
|
+
kind: 'two-grids',
|
|
137
|
+
questionId: 'c-shared',
|
|
138
|
+
gridQuestionIds: ['g-1', 'g-2'],
|
|
139
|
+
path: 'questionsById.c-shared',
|
|
140
|
+
detail: 'question is owned by more than one grid',
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
law: 'QUESTION-OWNERSHIP',
|
|
144
|
+
kind: 'orphan',
|
|
145
|
+
questionId: 'q-orphan',
|
|
146
|
+
gridQuestionIds: [],
|
|
147
|
+
path: 'questionsById.q-orphan',
|
|
148
|
+
detail: 'question is neither top-level nor owned by a grid',
|
|
149
|
+
},
|
|
150
|
+
])
|
|
151
|
+
assert.equal(
|
|
152
|
+
findTemplateDocumentContractViolations(doc).filter((violation) => violation.law === 'QUESTION-OWNERSHIP')
|
|
153
|
+
.length,
|
|
154
|
+
4,
|
|
155
|
+
)
|
|
156
|
+
})
|
|
157
|
+
})
|
|
158
|
+
|
|
101
159
|
describe('schema lint (DOC-LAW-1 / DOC-LAW-2, by construction)', () => {
|
|
102
160
|
it('finds no violations in the template document schema', () => {
|
|
103
161
|
assert.deepEqual(findTemplateSchemaDocLawViolations(), [])
|
|
@@ -213,6 +271,34 @@ describe('op schemas', () => {
|
|
|
213
271
|
assert.equal(moved instanceof type.errors, false)
|
|
214
272
|
})
|
|
215
273
|
|
|
274
|
+
it('accepts narrative rules and makes a qnr rule version reference unrepresentable', () => {
|
|
275
|
+
const narrative = templateOpSchema({
|
|
276
|
+
type: 'narrativeRule.set',
|
|
277
|
+
ruleId: 'nr-1',
|
|
278
|
+
questionId: 'q-1',
|
|
279
|
+
condition: { sourceQuestionId: 'q-2' },
|
|
280
|
+
})
|
|
281
|
+
const family = templateOpSchema({
|
|
282
|
+
type: 'qnrRule.set',
|
|
283
|
+
ruleId: 'qr-1',
|
|
284
|
+
questionId: 'q-1',
|
|
285
|
+
condition: { sourceQuestionId: 'q-1' },
|
|
286
|
+
templateFamilyId: 'family-1',
|
|
287
|
+
})
|
|
288
|
+
const pinnedVersion = templateOpSchema({
|
|
289
|
+
type: 'qnrRule.set',
|
|
290
|
+
ruleId: 'qr-1',
|
|
291
|
+
questionId: 'q-1',
|
|
292
|
+
condition: { sourceQuestionId: 'q-1' },
|
|
293
|
+
templateFamilyId: 'family-1',
|
|
294
|
+
templateVersion: 3,
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
assert.equal(narrative instanceof type.errors, false)
|
|
298
|
+
assert.equal(family instanceof type.errors, false)
|
|
299
|
+
assert.equal(pinnedVersion instanceof type.errors, true)
|
|
300
|
+
})
|
|
301
|
+
|
|
216
302
|
it('validates answer ops including the answer-side move', () => {
|
|
217
303
|
assert.equal(answerOpSchema({ type: 'gridRow.move', questionId: 'g-1', rowId: 'r-1', afterRowId: 'r-2' }) instanceof type.errors, false)
|
|
218
304
|
assert.equal(answerOpSchema({ type: 'gridRow.move', questionId: 'g-1', rowId: 'r-1' }) instanceof type.errors, true)
|