asma-core-helpers 0.28.0 → 0.29.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 +51 -3
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/operations.d.ts +23 -3
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +34 -3
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +64 -6
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/collaboration/templateDocument.d.ts +116 -3
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js +40 -0
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/package.json +1 -1
- package/src/collaboration/applyOperation.test.ts +212 -1
- package/src/collaboration/applyOperation.ts +76 -3
- package/src/collaboration/operations.ts +26 -1
- package/src/collaboration/schemas.test.ts +164 -0
- package/src/collaboration/schemas.ts +76 -7
- package/src/collaboration/templateDocument.ts +131 -4
|
@@ -7,8 +7,18 @@ import {
|
|
|
7
7
|
UnknownOperationError,
|
|
8
8
|
applyOperation,
|
|
9
9
|
} from './applyOperation.js'
|
|
10
|
+
import { canonicalJson, reduceToMinimalForm } from './canonicalize.js'
|
|
10
11
|
import type { TemplateOp } from './operations.js'
|
|
11
|
-
import {
|
|
12
|
+
import { templateDocumentIsDefault } from './schemas.js'
|
|
13
|
+
import {
|
|
14
|
+
BINDING_CARDINALITIES,
|
|
15
|
+
BINDING_OPTION_DEFAULTS,
|
|
16
|
+
bindingIsMultiValued,
|
|
17
|
+
emptyTemplateDocument,
|
|
18
|
+
resolveBindingOptions,
|
|
19
|
+
type MappingBinding,
|
|
20
|
+
type QnrTemplateDocument,
|
|
21
|
+
} from './templateDocument.js'
|
|
12
22
|
|
|
13
23
|
/**
|
|
14
24
|
* Guards the invariants a consumer would notice if a publish broke them. The exhaustive
|
|
@@ -531,6 +541,64 @@ describe('applyOperation', () => {
|
|
|
531
541
|
assert.equal(withoutEither.qnrRuleOrderByQuestionId, undefined)
|
|
532
542
|
})
|
|
533
543
|
|
|
544
|
+
it('drops the prefill rules that fill a deleted question, and its highlight settings', () => {
|
|
545
|
+
// Prefill rules are keyed by the TARGET question, so the parallel to the four rule families
|
|
546
|
+
// is "the rules that fill the deleted question go with it" (M-068).
|
|
547
|
+
const doc: QnrTemplateDocument = {
|
|
548
|
+
documentId: 'tpl-1',
|
|
549
|
+
revision: 0,
|
|
550
|
+
questionOrder: ['q-1', 'q-2'],
|
|
551
|
+
questionsById: { 'q-1': { type: 'TextShort' }, 'q-2': { type: 'TextShort' } },
|
|
552
|
+
prefillRulesById: { 'pr-1': { sourceQuestionId: 'q-2' } },
|
|
553
|
+
prefillRuleOrderByQuestionId: { 'q-1': ['pr-1'] },
|
|
554
|
+
highlightRuleSettingsByQuestionId: { 'q-1': { enabled: true, requiredAll: true } },
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const deleted = applyOperation(doc, { type: 'question.delete', questionId: 'q-1' })
|
|
558
|
+
|
|
559
|
+
assert.equal(deleted.prefillRulesById, undefined)
|
|
560
|
+
assert.equal(deleted.prefillRuleOrderByQuestionId, undefined)
|
|
561
|
+
assert.equal(deleted.highlightRuleSettingsByQuestionId, undefined)
|
|
562
|
+
})
|
|
563
|
+
|
|
564
|
+
it('keeps a prefill rule that merely SOURCES from the deleted question, dangling', () => {
|
|
565
|
+
// Same rule as the visibility case below: the surviving question q-1 authored this prefill,
|
|
566
|
+
// and dropping it because its source vanished would be silent data loss. The dangling
|
|
567
|
+
// `sourceQuestionId` is for validation to report.
|
|
568
|
+
const doc: QnrTemplateDocument = {
|
|
569
|
+
documentId: 'tpl-1',
|
|
570
|
+
revision: 0,
|
|
571
|
+
questionOrder: ['q-1', 'q-2'],
|
|
572
|
+
questionsById: { 'q-1': { type: 'TextShort' }, 'q-2': { type: 'TextShort' } },
|
|
573
|
+
prefillRulesById: { 'pr-1': { sourceQuestionId: 'q-2' } },
|
|
574
|
+
prefillRuleOrderByQuestionId: { 'q-1': ['pr-1'] },
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const deleted = applyOperation(doc, { type: 'question.delete', questionId: 'q-2' })
|
|
578
|
+
|
|
579
|
+
assert.deepEqual(deleted.prefillRuleOrderByQuestionId, { 'q-1': ['pr-1'] })
|
|
580
|
+
assert.equal(deleted.prefillRulesById?.['pr-1']?.sourceQuestionId, 'q-2')
|
|
581
|
+
})
|
|
582
|
+
|
|
583
|
+
it('keeps the highlight settings of a question whose rules are stored but disabled', () => {
|
|
584
|
+
// `toggleNarrative` flips have_narrative without clearing conditional[], so "stored but off"
|
|
585
|
+
// is a real state: the rules stay and the settings entry simply carries no `enabled`.
|
|
586
|
+
const doc: QnrTemplateDocument = {
|
|
587
|
+
documentId: 'tpl-1',
|
|
588
|
+
revision: 0,
|
|
589
|
+
questionOrder: ['q-1', 'q-2'],
|
|
590
|
+
questionsById: { 'q-1': { type: 'TextShort' }, 'q-2': { type: 'TextShort' } },
|
|
591
|
+
highlightRulesById: { 'hr-1': { condition: { sourceQuestionId: 'q-2' }, state: 2 } },
|
|
592
|
+
highlightRuleOrderByQuestionId: { 'q-1': ['hr-1'] },
|
|
593
|
+
highlightRuleSettingsByQuestionId: { 'q-1': { requiredAll: true } },
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const deleted = applyOperation(doc, { type: 'question.delete', questionId: 'q-2' })
|
|
597
|
+
|
|
598
|
+
assert.deepEqual(deleted.highlightRuleSettingsByQuestionId, { 'q-1': { requiredAll: true } })
|
|
599
|
+
assert.equal(deleted.highlightRulesById?.['hr-1']?.state, 2)
|
|
600
|
+
})
|
|
601
|
+
|
|
534
602
|
it('keeps another question\'s rule that references the deleted question, for validation to surface', () => {
|
|
535
603
|
// Dropping the surviving question's authored rule silently would be data loss; the
|
|
536
604
|
// dangling sourceQuestionId is a validation finding, not a reducer decision.
|
|
@@ -933,3 +1001,146 @@ describe('applyOperation', () => {
|
|
|
933
1001
|
assert.equal(replayed.revision, deleted.revision + 1)
|
|
934
1002
|
})
|
|
935
1003
|
})
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
/**
|
|
1007
|
+
* The binding behaviours — `cardinality` / `onMissing` / `onMany` (§2.2a, TASK-304 AC-6).
|
|
1008
|
+
*
|
|
1009
|
+
* Two rules meet on these three members and pull in opposite directions, which is what the suite is
|
|
1010
|
+
* really about. The author must be able to state them **explicitly**, because the compiler branches on
|
|
1011
|
+
* them and an artifact is immutable once minted; and DOC-LAW-2 says the *document* may never carry a
|
|
1012
|
+
* key at its own default, because the hash is computed on the minimal form. So "explicitly chosen" and
|
|
1013
|
+
* "stored" are deliberately not the same thing: choosing the default writes nothing.
|
|
1014
|
+
*
|
|
1015
|
+
* @see asma-modules/_docs/editor/qnrs/cross/2026-07-15-20-38-analysis-qnr-external-data-mapping-options.md:832
|
|
1016
|
+
*/
|
|
1017
|
+
describe('mappingBinding behaviours', () => {
|
|
1018
|
+
const bound = apply([
|
|
1019
|
+
{ type: 'question.create', questionId: 'q-1', questionType: 'TextShort' },
|
|
1020
|
+
{ type: 'mappingNode.create', nodeId: 'n-1', entityId: 'Actor' },
|
|
1021
|
+
])
|
|
1022
|
+
|
|
1023
|
+
const create = (options: Partial<Pick<MappingBinding, 'cardinality' | 'onMissing' | 'onMany'>> = {}) =>
|
|
1024
|
+
applyOperation(bound, {
|
|
1025
|
+
type: 'mappingBinding.create',
|
|
1026
|
+
bindingId: 'b-1',
|
|
1027
|
+
nodeId: 'n-1',
|
|
1028
|
+
fieldId: 'Navn',
|
|
1029
|
+
target: { kind: 'question', questionId: 'q-1' },
|
|
1030
|
+
...options,
|
|
1031
|
+
})
|
|
1032
|
+
|
|
1033
|
+
const binding = (doc: QnrTemplateDocument) => doc.mappingBindingsById?.['b-1']
|
|
1034
|
+
|
|
1035
|
+
it('stores a behaviour the author narrowed away from its default', () => {
|
|
1036
|
+
const doc = create({ cardinality: '0..*', onMissing: 'error', onMany: 'first' })
|
|
1037
|
+
|
|
1038
|
+
assert.deepEqual(binding(doc), {
|
|
1039
|
+
nodeId: 'n-1',
|
|
1040
|
+
fieldId: 'Navn',
|
|
1041
|
+
target: { kind: 'question', questionId: 'q-1' },
|
|
1042
|
+
cardinality: '0..*',
|
|
1043
|
+
onMissing: 'error',
|
|
1044
|
+
onMany: 'first',
|
|
1045
|
+
})
|
|
1046
|
+
})
|
|
1047
|
+
|
|
1048
|
+
it('omits each behaviour the author explicitly set to its own default (DOC-LAW-2)', () => {
|
|
1049
|
+
// One at a time, so a reducer that special-cased only `cardinality` cannot pass.
|
|
1050
|
+
for (const [key, value] of Object.entries(BINDING_OPTION_DEFAULTS)) {
|
|
1051
|
+
const doc = create({ [key]: value })
|
|
1052
|
+
|
|
1053
|
+
assert.deepEqual(
|
|
1054
|
+
binding(doc),
|
|
1055
|
+
{ nodeId: 'n-1', fieldId: 'Navn', target: { kind: 'question', questionId: 'q-1' } },
|
|
1056
|
+
`${key}: '${value}' is the default and must not be stored`,
|
|
1057
|
+
)
|
|
1058
|
+
}
|
|
1059
|
+
})
|
|
1060
|
+
|
|
1061
|
+
it('makes an explicitly-defaulted binding byte-identical to an untouched one', () => {
|
|
1062
|
+
// The consequence that matters: `document_hash` decides whether a version is minted and
|
|
1063
|
+
// whether a re-import diverged. Two authors reaching the same binding through different
|
|
1064
|
+
// clicks must not produce two hashes.
|
|
1065
|
+
const untouched = canonicalJson(reduceToMinimalForm(create(), { isDefault: templateDocumentIsDefault }))
|
|
1066
|
+
const spelledOut = canonicalJson(
|
|
1067
|
+
reduceToMinimalForm(create({ cardinality: '0..1', onMissing: 'omit', onMany: 'error' }), {
|
|
1068
|
+
isDefault: templateDocumentIsDefault,
|
|
1069
|
+
}),
|
|
1070
|
+
)
|
|
1071
|
+
|
|
1072
|
+
assert.equal(spelledOut, untouched)
|
|
1073
|
+
})
|
|
1074
|
+
|
|
1075
|
+
it('sets, replaces and unsets a behaviour through update', () => {
|
|
1076
|
+
const set = applyOperation(create(), {
|
|
1077
|
+
type: 'mappingBinding.update',
|
|
1078
|
+
bindingId: 'b-1',
|
|
1079
|
+
patch: { onMany: 'first' },
|
|
1080
|
+
})
|
|
1081
|
+
assert.equal(binding(set)?.onMany, 'first')
|
|
1082
|
+
|
|
1083
|
+
const replaced = applyOperation(set, {
|
|
1084
|
+
type: 'mappingBinding.update',
|
|
1085
|
+
bindingId: 'b-1',
|
|
1086
|
+
patch: { onMany: 'error' },
|
|
1087
|
+
})
|
|
1088
|
+
// 'error' IS the default, so replacing back to it removes the key rather than storing it.
|
|
1089
|
+
assert.equal('onMany' in (binding(replaced) ?? {}), false)
|
|
1090
|
+
|
|
1091
|
+
const unset = applyOperation(set, {
|
|
1092
|
+
type: 'mappingBinding.update',
|
|
1093
|
+
bindingId: 'b-1',
|
|
1094
|
+
patch: { onMany: null },
|
|
1095
|
+
})
|
|
1096
|
+
assert.equal('onMany' in (binding(unset) ?? {}), false)
|
|
1097
|
+
})
|
|
1098
|
+
|
|
1099
|
+
it('leaves a behaviour alone when the patch does not name it', () => {
|
|
1100
|
+
const doc = create({ cardinality: '1..*', onMissing: 'error' })
|
|
1101
|
+
|
|
1102
|
+
const patched = applyOperation(doc, {
|
|
1103
|
+
type: 'mappingBinding.update',
|
|
1104
|
+
bindingId: 'b-1',
|
|
1105
|
+
patch: { fieldId: 'Adresse' },
|
|
1106
|
+
})
|
|
1107
|
+
|
|
1108
|
+
assert.equal(binding(patched)?.fieldId, 'Adresse')
|
|
1109
|
+
assert.equal(binding(patched)?.cardinality, '1..*')
|
|
1110
|
+
assert.equal(binding(patched)?.onMissing, 'error')
|
|
1111
|
+
})
|
|
1112
|
+
|
|
1113
|
+
it('never writes an explicit null into the document', () => {
|
|
1114
|
+
const doc = applyOperation(create({ onMissing: 'error' }), {
|
|
1115
|
+
type: 'mappingBinding.update',
|
|
1116
|
+
bindingId: 'b-1',
|
|
1117
|
+
patch: { onMissing: null, cardinality: null, onMany: null },
|
|
1118
|
+
})
|
|
1119
|
+
|
|
1120
|
+
assert.equal(canonicalJson(binding(doc)).includes('null'), false)
|
|
1121
|
+
})
|
|
1122
|
+
|
|
1123
|
+
it('hydrates absent behaviours to the defaults every reader must agree on', () => {
|
|
1124
|
+
assert.deepEqual(resolveBindingOptions(binding(create()) as MappingBinding), BINDING_OPTION_DEFAULTS)
|
|
1125
|
+
|
|
1126
|
+
assert.deepEqual(resolveBindingOptions(binding(create({ onMany: 'first' })) as MappingBinding), {
|
|
1127
|
+
...BINDING_OPTION_DEFAULTS,
|
|
1128
|
+
onMany: 'first',
|
|
1129
|
+
})
|
|
1130
|
+
})
|
|
1131
|
+
|
|
1132
|
+
it('reads exactly the starred cardinalities as multi-valued', () => {
|
|
1133
|
+
const multi = BINDING_CARDINALITIES.filter((cardinality) =>
|
|
1134
|
+
bindingIsMultiValued({
|
|
1135
|
+
nodeId: 'n-1',
|
|
1136
|
+
fieldId: 'Navn',
|
|
1137
|
+
target: { kind: 'question', questionId: 'q-1' },
|
|
1138
|
+
cardinality,
|
|
1139
|
+
}),
|
|
1140
|
+
)
|
|
1141
|
+
|
|
1142
|
+
assert.deepEqual(multi, ['0..*', '1..*'])
|
|
1143
|
+
// And the absent case resolves through the default, which is single-valued.
|
|
1144
|
+
assert.equal(bindingIsMultiValued(binding(create()) as MappingBinding), false)
|
|
1145
|
+
})
|
|
1146
|
+
})
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import type { TemplateOp } from './operations.js'
|
|
2
2
|
import type {
|
|
3
|
+
BindingCardinality,
|
|
4
|
+
BindingOnMany,
|
|
5
|
+
BindingOnMissing,
|
|
6
|
+
MappingBinding,
|
|
3
7
|
MappingNode,
|
|
4
8
|
QnrDataMapping,
|
|
5
9
|
QnrQuestion,
|
|
6
10
|
QnrTemplateDocument,
|
|
7
11
|
QuestionId,
|
|
8
12
|
} from './templateDocument.js'
|
|
9
|
-
import { bindingTargetKey } from './templateDocument.js'
|
|
13
|
+
import { BINDING_OPTION_DEFAULTS, bindingTargetKey } from './templateDocument.js'
|
|
14
|
+
|
|
15
|
+
/** The behaviour members, so the write loop cannot silently miss one that `BINDING_OPTION_DEFAULTS` gains. */
|
|
16
|
+
const BINDING_OPTION_KEYS = Object.keys(BINDING_OPTION_DEFAULTS) as ReadonlyArray<keyof typeof BINDING_OPTION_DEFAULTS>
|
|
10
17
|
|
|
11
18
|
/**
|
|
12
19
|
* The authoring reducer. Pure: takes a document and an op, returns a new document with
|
|
@@ -109,6 +116,38 @@ const moveInOrder = (order: readonly string[], id: string, toIndex: number): str
|
|
|
109
116
|
return [...without.slice(0, at), id, ...without.slice(at)]
|
|
110
117
|
}
|
|
111
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Writes a binding's three behaviours in canonical minimal form.
|
|
121
|
+
*
|
|
122
|
+
* Three cases, and the middle one is the reason this is a function rather than a spread:
|
|
123
|
+
* `undefined` leaves the stored value alone (the member was not part of this edit), an explicit
|
|
124
|
+
* `null` unsets it, and **a value equal to the default unsets it too** — DOC-LAW-2 forbids a key
|
|
125
|
+
* present with its own default, so an author choosing `onMany: 'error'` and an author never touching
|
|
126
|
+
* it must produce byte-identical documents. If they did not, the same binding would carry two
|
|
127
|
+
* `document_hash` values depending on which control the author clicked, minting a spurious version
|
|
128
|
+
* and reading as divergence against the imported original.
|
|
129
|
+
*
|
|
130
|
+
* @see asma-modules/_docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:195 — DOC-LAW-2
|
|
131
|
+
*/
|
|
132
|
+
const writeBindingOptions = (
|
|
133
|
+
binding: MappingBinding,
|
|
134
|
+
options: {
|
|
135
|
+
cardinality?: BindingCardinality | null
|
|
136
|
+
onMissing?: BindingOnMissing | null
|
|
137
|
+
onMany?: BindingOnMany | null
|
|
138
|
+
},
|
|
139
|
+
): MappingBinding => {
|
|
140
|
+
let next = binding
|
|
141
|
+
|
|
142
|
+
for (const key of BINDING_OPTION_KEYS) {
|
|
143
|
+
const chosen = options[key]
|
|
144
|
+
if (chosen === undefined) continue
|
|
145
|
+
next = writeField(next, key, chosen === BINDING_OPTION_DEFAULTS[key] ? null : chosen)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return next
|
|
149
|
+
}
|
|
150
|
+
|
|
112
151
|
const omitKey = <V>(record: Record<string, V>, key: string): Record<string, V> => {
|
|
113
152
|
const { [key]: _removed, ...rest } = record
|
|
114
153
|
return rest
|
|
@@ -186,6 +225,9 @@ const OPTIONAL_COLLECTIONS = [
|
|
|
186
225
|
'narrativeRuleOrderByQuestionId',
|
|
187
226
|
'qnrRulesById',
|
|
188
227
|
'qnrRuleOrderByQuestionId',
|
|
228
|
+
'prefillRulesById',
|
|
229
|
+
'prefillRuleOrderByQuestionId',
|
|
230
|
+
'highlightRuleSettingsByQuestionId',
|
|
189
231
|
'dataMappingsById',
|
|
190
232
|
'mappingNodesById',
|
|
191
233
|
'mappingBindingsById',
|
|
@@ -501,6 +543,17 @@ const deleteQuestion = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTe
|
|
|
501
543
|
if (qnrRulesById) {
|
|
502
544
|
qnrRulesById = qnrRuleIds.reduce((acc, id) => omitKey(acc, id), qnrRulesById)
|
|
503
545
|
}
|
|
546
|
+
// Prefill rules are keyed by their TARGET question — the one that gets filled — so deleting a
|
|
547
|
+
// question takes the rules that fill IT, exactly as the four families above take their owner's.
|
|
548
|
+
// A rule on a SURVIVING question that names this one as its `sourceQuestionId` is deliberately
|
|
549
|
+
// KEPT and left dangling, for the reason stated above: dropping a surviving question's authored
|
|
550
|
+
// rule is data loss, and a dangling reference is for validation to surface, not for the reducer
|
|
551
|
+
// to silently resolve.
|
|
552
|
+
const prefillRuleIds = doc.prefillRuleOrderByQuestionId?.[questionId] ?? []
|
|
553
|
+
let prefillRulesById = doc.prefillRulesById
|
|
554
|
+
if (prefillRulesById) {
|
|
555
|
+
prefillRulesById = prefillRuleIds.reduce((acc, id) => omitKey(acc, id), prefillRulesById)
|
|
556
|
+
}
|
|
504
557
|
const withoutQuestion = patchDocument(doc, {
|
|
505
558
|
questionsById: doc.questionsById && omitKey(doc.questionsById, questionId),
|
|
506
559
|
questionOrder: doc.questionOrder.filter((id) => id !== questionId),
|
|
@@ -521,6 +574,13 @@ const deleteQuestion = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTe
|
|
|
521
574
|
qnrRulesById,
|
|
522
575
|
qnrRuleOrderByQuestionId:
|
|
523
576
|
doc.qnrRuleOrderByQuestionId && omitKey(doc.qnrRuleOrderByQuestionId, questionId),
|
|
577
|
+
prefillRulesById,
|
|
578
|
+
prefillRuleOrderByQuestionId:
|
|
579
|
+
doc.prefillRuleOrderByQuestionId && omitKey(doc.prefillRuleOrderByQuestionId, questionId),
|
|
580
|
+
// The settings ride with the question they configure; an entry no question reaches is
|
|
581
|
+
// unreachable state that still changes `document_hash`.
|
|
582
|
+
highlightRuleSettingsByQuestionId:
|
|
583
|
+
doc.highlightRuleSettingsByQuestionId && omitKey(doc.highlightRuleSettingsByQuestionId, questionId),
|
|
524
584
|
})
|
|
525
585
|
// A binding pointing at a deleted question is an unresolvable reference the
|
|
526
586
|
// compiler would reject at publication; drop it with its target. A grid column
|
|
@@ -1051,7 +1111,10 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
1051
1111
|
...doc,
|
|
1052
1112
|
mappingBindingsById: {
|
|
1053
1113
|
...(doc.mappingBindingsById ?? {}),
|
|
1054
|
-
[op.bindingId]:
|
|
1114
|
+
[op.bindingId]: writeBindingOptions(
|
|
1115
|
+
{ nodeId: op.nodeId, fieldId: op.fieldId, target: op.target },
|
|
1116
|
+
op,
|
|
1117
|
+
),
|
|
1055
1118
|
},
|
|
1056
1119
|
}
|
|
1057
1120
|
// The owning mapping root's `bindingOrder` lists the new binding — when the tree is
|
|
@@ -1093,11 +1156,21 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
1093
1156
|
)
|
|
1094
1157
|
}
|
|
1095
1158
|
}
|
|
1159
|
+
// The behaviours go through `writeBindingOptions` rather than the spread: a spread would
|
|
1160
|
+
// store `cardinality: '0..1'` verbatim (a key present with its own default — DOC-LAW-2) and
|
|
1161
|
+
// would write `null` for an unset, which the document may never carry.
|
|
1162
|
+
const {
|
|
1163
|
+
cardinality: _cardinality,
|
|
1164
|
+
onMissing: _onMissing,
|
|
1165
|
+
onMany: _onMany,
|
|
1166
|
+
...structural
|
|
1167
|
+
} = op.patch
|
|
1168
|
+
|
|
1096
1169
|
return {
|
|
1097
1170
|
...doc,
|
|
1098
1171
|
mappingBindingsById: {
|
|
1099
1172
|
...doc.mappingBindingsById,
|
|
1100
|
-
[op.bindingId]: { ...binding, ...op.patch
|
|
1173
|
+
[op.bindingId]: writeBindingOptions({ ...binding, ...structural }, op.patch),
|
|
1101
1174
|
},
|
|
1102
1175
|
}
|
|
1103
1176
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
ActionId,
|
|
3
3
|
AltId,
|
|
4
|
+
BindingCardinality,
|
|
4
5
|
BindingId,
|
|
6
|
+
BindingOnMany,
|
|
7
|
+
BindingOnMissing,
|
|
5
8
|
BindingTarget,
|
|
6
9
|
DocScalar,
|
|
7
10
|
FilterId,
|
|
@@ -116,17 +119,39 @@ export type TemplateOp =
|
|
|
116
119
|
}
|
|
117
120
|
}
|
|
118
121
|
| { type: 'mappingNode.delete'; nodeId: NodeId }
|
|
122
|
+
/**
|
|
123
|
+
* The three behaviours are optional here and **omitted from the document when they equal their
|
|
124
|
+
* default** (DOC-LAW-2, `BINDING_OPTION_DEFAULTS`): the op carries what the author chose, the
|
|
125
|
+
* document stores only what differs. Authoring them is the point — a binding whose behaviour
|
|
126
|
+
* could not be stated would leave the compiler to guess what happens when the source yields
|
|
127
|
+
* nothing or yields many.
|
|
128
|
+
*/
|
|
119
129
|
| {
|
|
120
130
|
type: 'mappingBinding.create'
|
|
121
131
|
bindingId: BindingId
|
|
122
132
|
nodeId: NodeId
|
|
123
133
|
fieldId: string
|
|
124
134
|
target: BindingTarget
|
|
135
|
+
cardinality?: BindingCardinality
|
|
136
|
+
onMissing?: BindingOnMissing
|
|
137
|
+
onMany?: BindingOnMany
|
|
125
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* `null` on a behaviour is the explicit unset — the author returned it to the default, which the
|
|
141
|
+
* document encodes as absence. Without the tri-state "back to default" would be unexpressible:
|
|
142
|
+
* omitting the member from the patch means "leave it alone", which is a different edit.
|
|
143
|
+
*/
|
|
126
144
|
| {
|
|
127
145
|
type: 'mappingBinding.update'
|
|
128
146
|
bindingId: BindingId
|
|
129
|
-
patch: {
|
|
147
|
+
patch: {
|
|
148
|
+
nodeId?: NodeId
|
|
149
|
+
fieldId?: string
|
|
150
|
+
target?: BindingTarget
|
|
151
|
+
cardinality?: BindingCardinality | null
|
|
152
|
+
onMissing?: BindingOnMissing | null
|
|
153
|
+
onMany?: BindingOnMany | null
|
|
154
|
+
}
|
|
130
155
|
}
|
|
131
156
|
| { type: 'mappingBinding.delete'; bindingId: BindingId }
|
|
132
157
|
| {
|
|
@@ -3,6 +3,7 @@ import { describe, it } from 'node:test'
|
|
|
3
3
|
import { type } from 'arktype'
|
|
4
4
|
|
|
5
5
|
import { canonicalJson, reduceToMinimalForm } from './canonicalize.js'
|
|
6
|
+
import { findDocLawViolations } from './docLaws.js'
|
|
6
7
|
import { emptyTemplateDocument, type QnrTemplateDocument } from './templateDocument.js'
|
|
7
8
|
import { IMPLEMENTED_ANSWER_OP_TYPES } from './answerOperations.js'
|
|
8
9
|
import { IMPLEMENTED_OP_TYPES } from './operations.js'
|
|
@@ -268,6 +269,58 @@ describe('binding-target uniqueness', () => {
|
|
|
268
269
|
})
|
|
269
270
|
})
|
|
270
271
|
|
|
272
|
+
describe('prefill and highlight contract (M-067 / M-068)', () => {
|
|
273
|
+
const withRules = (): QnrTemplateDocument => ({
|
|
274
|
+
documentId: 'tpl-1',
|
|
275
|
+
revision: 0,
|
|
276
|
+
questionOrder: ['q-1', 'q-2'],
|
|
277
|
+
questionsById: { 'q-1': { type: 'TextShort' }, 'q-2': { type: 'RadioButtons' } },
|
|
278
|
+
prefillRulesById: { 'pr-1': { sourceQuestionId: 'q-2', sourceParentQuestionId: 'g-1' } },
|
|
279
|
+
prefillRuleOrderByQuestionId: { 'q-1': ['pr-1'] },
|
|
280
|
+
highlightRulesById: {
|
|
281
|
+
'hr-1': {
|
|
282
|
+
condition: { sourceQuestionId: 'q-2', operator: 'eq', alternativeIds: ['a-1', 'a-2'] },
|
|
283
|
+
state: 3,
|
|
284
|
+
highlight: true,
|
|
285
|
+
showLink: true,
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
highlightRuleOrderByQuestionId: { 'q-1': ['hr-1'] },
|
|
289
|
+
highlightRuleSettingsByQuestionId: { 'q-1': { enabled: true, requiredAll: true } },
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
it('accepts the widened rule shapes and the two new collections', () => {
|
|
293
|
+
const result = validateTemplateDocument(withRules())
|
|
294
|
+
assert.ok(result.ok)
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
it('refuses a prefill rule with no source address', () => {
|
|
298
|
+
const doc = withRules()
|
|
299
|
+
doc.prefillRulesById = { 'pr-1': {} as never }
|
|
300
|
+
assert.equal(validateTemplateDocument(doc).ok, false)
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
it('refuses a false in the true-only settings, so absent stays the only "not set"', () => {
|
|
304
|
+
const doc = withRules()
|
|
305
|
+
doc.highlightRuleSettingsByQuestionId = { 'q-1': { enabled: false as never } }
|
|
306
|
+
assert.equal(validateTemplateDocument(doc).ok, false)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
it('keeps the new shapes inside the document laws', () => {
|
|
310
|
+
// The schema lint is total over the shape, so this proves alternativeIds is a PRIMITIVE
|
|
311
|
+
// array — an array of objects here would be a DOC-LAW-1 violation the lint reports.
|
|
312
|
+
assert.deepEqual(findTemplateSchemaDocLawViolations(), [])
|
|
313
|
+
assert.deepEqual(findDocLawViolations(withRules()), [])
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
it('survives a reduction round trip unchanged', () => {
|
|
317
|
+
const doc = withRules()
|
|
318
|
+
const once = reduceToMinimalForm(doc, { isDefault: templateDocumentIsDefault })
|
|
319
|
+
assert.equal(canonicalJson(once), canonicalJson(reduceToMinimalForm(once, { isDefault: templateDocumentIsDefault })))
|
|
320
|
+
assert.equal(canonicalJson(once), canonicalJson(doc))
|
|
321
|
+
})
|
|
322
|
+
})
|
|
323
|
+
|
|
271
324
|
describe('DOC-LAW-2 default registry', () => {
|
|
272
325
|
it('treats sentinel-boolean defaults as omittable', () => {
|
|
273
326
|
assert.equal(templateDocumentIsDefault('questionsById.q-1.required', false), true)
|
|
@@ -369,3 +422,114 @@ describe('schema ↔ vocabulary parity', () => {
|
|
|
369
422
|
assert.equal(out instanceof type.errors, false)
|
|
370
423
|
})
|
|
371
424
|
})
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* The binding behaviours at the two validation layers (TASK-304 AC-6).
|
|
429
|
+
*
|
|
430
|
+
* The document schema and the op schema disagree on `null` **on purpose** — the op layer is the only
|
|
431
|
+
* place the absent-vs-cleared tri-state exists — so both halves are asserted rather than assumed.
|
|
432
|
+
*/
|
|
433
|
+
describe('mappingBinding behaviour schemas', () => {
|
|
434
|
+
const createOp = (options: Record<string, unknown>) => ({
|
|
435
|
+
type: 'mappingBinding.create',
|
|
436
|
+
bindingId: 'b-1',
|
|
437
|
+
nodeId: 'n-1',
|
|
438
|
+
fieldId: 'Navn',
|
|
439
|
+
target: { kind: 'question', questionId: 'q-1' },
|
|
440
|
+
...options,
|
|
441
|
+
})
|
|
442
|
+
|
|
443
|
+
const documentWith = (binding: Record<string, unknown>): QnrTemplateDocument =>
|
|
444
|
+
({
|
|
445
|
+
...emptyTemplateDocument('tpl-1'),
|
|
446
|
+
questionsById: { 'q-1': { type: 'TextShort' } },
|
|
447
|
+
questionOrder: ['q-1'],
|
|
448
|
+
mappingNodesById: { 'n-1': { entityId: 'Actor' } },
|
|
449
|
+
mappingBindingsById: { 'b-1': binding },
|
|
450
|
+
}) as unknown as QnrTemplateDocument
|
|
451
|
+
|
|
452
|
+
it('accepts every declared behaviour value on the create op', () => {
|
|
453
|
+
for (const cardinality of ['0..1', '1', '0..*', '1..*']) {
|
|
454
|
+
assert.equal(templateOpSchema(createOp({ cardinality })) instanceof type.errors, false, cardinality)
|
|
455
|
+
}
|
|
456
|
+
for (const onMissing of ['omit', 'error']) {
|
|
457
|
+
assert.equal(templateOpSchema(createOp({ onMissing })) instanceof type.errors, false, onMissing)
|
|
458
|
+
}
|
|
459
|
+
for (const onMany of ['error', 'first']) {
|
|
460
|
+
assert.equal(templateOpSchema(createOp({ onMany })) instanceof type.errors, false, onMany)
|
|
461
|
+
}
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
it('refuses a behaviour value outside the closed set', () => {
|
|
465
|
+
// The whole reason these are not `string?`: this op compiles into an immutable artifact.
|
|
466
|
+
assert.ok(templateOpSchema(createOp({ cardinality: '0..2' })) instanceof type.errors)
|
|
467
|
+
assert.ok(templateOpSchema(createOp({ onMissing: 'ignore' })) instanceof type.errors)
|
|
468
|
+
assert.ok(templateOpSchema(createOp({ onMany: 'last' })) instanceof type.errors)
|
|
469
|
+
})
|
|
470
|
+
|
|
471
|
+
it('refuses a behaviour value outside the closed set on a STORED binding too', () => {
|
|
472
|
+
// The op schema guards the wire; this one guards the document. Both are needed: an import or a
|
|
473
|
+
// replay writes a document without passing an op through, and a loose document position would
|
|
474
|
+
// let a value the compiler cannot branch on reach an immutable artifact.
|
|
475
|
+
for (const option of [{ cardinality: '0..2' }, { onMissing: 'ignore' }, { onMany: 'last' }]) {
|
|
476
|
+
const result = validateTemplateDocument(
|
|
477
|
+
documentWith({
|
|
478
|
+
nodeId: 'n-1',
|
|
479
|
+
fieldId: 'Navn',
|
|
480
|
+
target: { kind: 'question', questionId: 'q-1' },
|
|
481
|
+
...option,
|
|
482
|
+
}),
|
|
483
|
+
)
|
|
484
|
+
|
|
485
|
+
assert.equal(result.ok, false, `${JSON.stringify(option)} must not validate`)
|
|
486
|
+
}
|
|
487
|
+
})
|
|
488
|
+
|
|
489
|
+
it('admits null in an update patch and refuses it in the document', () => {
|
|
490
|
+
const patched = templateOpSchema({
|
|
491
|
+
type: 'mappingBinding.update',
|
|
492
|
+
bindingId: 'b-1',
|
|
493
|
+
patch: { cardinality: null, onMissing: null, onMany: null },
|
|
494
|
+
})
|
|
495
|
+
assert.equal(patched instanceof type.errors, false)
|
|
496
|
+
|
|
497
|
+
assert.ok(
|
|
498
|
+
validateTemplateDocument(documentWith({
|
|
499
|
+
nodeId: 'n-1',
|
|
500
|
+
fieldId: 'Navn',
|
|
501
|
+
target: { kind: 'question', questionId: 'q-1' },
|
|
502
|
+
onMany: null,
|
|
503
|
+
})).ok === false,
|
|
504
|
+
)
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
it('counts a stored behaviour at its default as a DOC-LAW-2 violation', () => {
|
|
508
|
+
const violations = findDocLawDefaultViolations(
|
|
509
|
+
documentWith({
|
|
510
|
+
nodeId: 'n-1',
|
|
511
|
+
fieldId: 'Navn',
|
|
512
|
+
target: { kind: 'question', questionId: 'q-1' },
|
|
513
|
+
cardinality: '0..1',
|
|
514
|
+
}),
|
|
515
|
+
)
|
|
516
|
+
|
|
517
|
+
assert.deepEqual(
|
|
518
|
+
violations.map((violation) => violation.path),
|
|
519
|
+
['mappingBindingsById.b-1.cardinality'],
|
|
520
|
+
)
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
it('never reads a mapping NODE cardinality as an omittable default', () => {
|
|
524
|
+
// A node's default cardinality is the catalog relation's, not a literal — so a node that
|
|
525
|
+
// narrows a `0..*` relation to a single row stores `0..1` meaningfully. A suffix-matched
|
|
526
|
+
// default rule would drop exactly that member and silently widen the traversal.
|
|
527
|
+
assert.equal(templateDocumentIsDefault('mappingNodesById.n-1.cardinality', '0..1'), false)
|
|
528
|
+
assert.equal(templateDocumentIsDefault('mappingBindingsById.b-1.cardinality', '0..1'), true)
|
|
529
|
+
|
|
530
|
+
// And a non-default value on a binding is never omittable either.
|
|
531
|
+
assert.equal(templateDocumentIsDefault('mappingBindingsById.b-1.cardinality', '0..*'), false)
|
|
532
|
+
assert.equal(templateDocumentIsDefault('mappingBindingsById.b-1.onMissing', 'omit'), true)
|
|
533
|
+
assert.equal(templateDocumentIsDefault('mappingBindingsById.b-1.onMany', 'error'), true)
|
|
534
|
+
})
|
|
535
|
+
})
|