asma-core-helpers 0.40.0 → 0.41.1
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 +212 -0
- 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/narrativePresence.d.ts +12 -0
- package/lib/collaboration/narrativePresence.d.ts.map +1 -0
- package/lib/collaboration/narrativePresence.js +34 -0
- package/lib/collaboration/narrativePresence.js.map +1 -0
- package/lib/collaboration/operations.d.ts +53 -1
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js +4 -0
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +92 -1
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +49 -2
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/collaboration/templateAuthoringMeta.d.ts +4 -4
- package/lib/collaboration/templateAuthoringMeta.d.ts.map +1 -1
- package/lib/collaboration/templateAuthoringMeta.js +103 -5
- package/lib/collaboration/templateAuthoringMeta.js.map +1 -1
- package/lib/collaboration/templateDocument.d.ts +22 -0
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/lib/collaboration/typeContracts.d.ts +30 -1
- package/lib/collaboration/typeContracts.d.ts.map +1 -1
- package/lib/g-definitions.d.ts +5 -0
- package/lib/g-definitions.d.ts.map +1 -1
- package/lib/g-definitions.js +6 -0
- package/lib/g-definitions.js.map +1 -1
- package/lib/overlayBack.d.ts +103 -0
- package/lib/overlayBack.d.ts.map +1 -0
- package/lib/overlayBack.js +540 -0
- package/lib/overlayBack.js.map +1 -0
- package/package.json +2 -1
- package/src/collaboration/applyOperation.ts +230 -0
- package/src/collaboration/index.ts +1 -0
- package/src/collaboration/narrativePresence.test.ts +328 -0
- package/src/collaboration/narrativePresence.ts +42 -0
- package/src/collaboration/operations.ts +25 -0
- package/src/collaboration/ruleAuthoring.test.ts +285 -0
- package/src/collaboration/schemas.ts +65 -2
- package/src/collaboration/templateAuthoringMeta.ts +107 -6
- package/src/collaboration/templateDocument.ts +23 -0
- package/src/collaboration/typeContracts.ts +17 -0
- package/src/g-definitions.ts +13 -0
- package/src/overlayBack.test.ts +742 -0
- package/src/overlayBack.ts +647 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "asma-core-helpers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.41.1",
|
|
4
4
|
"description": "Core helper utilities for ASMA applications",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib/**/*",
|
|
@@ -54,6 +54,7 @@
|
|
|
54
54
|
"asma-qiankun-react-loader": "^0.8.2",
|
|
55
55
|
"asma-types": "^2.14.0",
|
|
56
56
|
"history": "^5.3.0",
|
|
57
|
+
"jsdom": "^27.0.0",
|
|
57
58
|
"lefthook": "^2.1.1",
|
|
58
59
|
"tsx": "^4.23.0",
|
|
59
60
|
"typescript": "7.0.2"
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { TemplateOp } from './operations.js'
|
|
2
|
+
import { findNarrativePresenceViolations, hasSingleNarrativeQuestionOwner } from './narrativePresence.js'
|
|
2
3
|
import type {
|
|
3
4
|
ActionMetadata,
|
|
4
5
|
AlternativeChartLegend,
|
|
@@ -11,6 +12,8 @@ import type {
|
|
|
11
12
|
MappingBinding,
|
|
12
13
|
MappingFilter,
|
|
13
14
|
MappingNode,
|
|
15
|
+
HighlightRule,
|
|
16
|
+
RuleCondition,
|
|
14
17
|
QnrAlternative,
|
|
15
18
|
QnrDataMapping,
|
|
16
19
|
QnrAction,
|
|
@@ -1079,6 +1082,7 @@ const OPTIONAL_COLLECTIONS = [
|
|
|
1079
1082
|
'prefillRulesById',
|
|
1080
1083
|
'prefillRuleOrderByQuestionId',
|
|
1081
1084
|
'highlightRuleSettingsByQuestionId',
|
|
1085
|
+
'narrativeRuleSettingsByQuestionId',
|
|
1082
1086
|
'dataMappingsById',
|
|
1083
1087
|
'mappingNodesById',
|
|
1084
1088
|
'mappingBindingsById',
|
|
@@ -1432,6 +1436,8 @@ const deleteQuestion = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTe
|
|
|
1432
1436
|
// unreachable state that still changes `document_hash`.
|
|
1433
1437
|
highlightRuleSettingsByQuestionId:
|
|
1434
1438
|
doc.highlightRuleSettingsByQuestionId && omitKey(doc.highlightRuleSettingsByQuestionId, questionId),
|
|
1439
|
+
narrativeRuleSettingsByQuestionId:
|
|
1440
|
+
doc.narrativeRuleSettingsByQuestionId && omitKey(doc.narrativeRuleSettingsByQuestionId, questionId),
|
|
1435
1441
|
})
|
|
1436
1442
|
// A binding pointing at a deleted question is an unresolvable reference the
|
|
1437
1443
|
// compiler would reject at publication; drop it with its target. A grid column
|
|
@@ -1503,8 +1509,220 @@ const scrubColumnPresentationRefs = (doc: QnrTemplateDocument, questionId: Quest
|
|
|
1503
1509
|
return changed ? { ...doc, questionsById: nextQuestions } : doc
|
|
1504
1510
|
}
|
|
1505
1511
|
|
|
1512
|
+
/** New operations are checked on direct replay as well as at the wire boundary. */
|
|
1513
|
+
const assertRuleEditShape = (op: TemplateOp, keys: readonly string[]): void => {
|
|
1514
|
+
if (Object.keys(op).some((key) => !keys.includes(key))) {
|
|
1515
|
+
throw new OperationConflictError(`Operation ${op.type} carries an undeclared member`)
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
const requireRuleQuestion = (doc: QnrTemplateDocument, questionId: unknown): void => {
|
|
1520
|
+
if (typeof questionId !== 'string' || questionId === '' || !Object.hasOwn(doc.questionsById ?? {}, questionId)) {
|
|
1521
|
+
throw new OperationConflictError(`Unknown question "${questionId}"`)
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
/** Fieldwise true-only settings. Neither a rule nor the other setting is touched. */
|
|
1526
|
+
const setRuleSetting = (
|
|
1527
|
+
doc: QnrTemplateDocument,
|
|
1528
|
+
op: Extract<TemplateOp, { type: 'highlightRuleSettings.set' | 'narrativeRuleSettings.set' }>,
|
|
1529
|
+
root: 'highlightRuleSettingsByQuestionId' | 'narrativeRuleSettingsByQuestionId',
|
|
1530
|
+
): QnrTemplateDocument => {
|
|
1531
|
+
assertRuleEditShape(op, ['type', 'questionId', 'field', 'value'])
|
|
1532
|
+
requireRuleQuestion(doc, op.questionId)
|
|
1533
|
+
if ((op.field !== 'enabled' && op.field !== 'requiredAll') || typeof op.value !== 'boolean') {
|
|
1534
|
+
throw new OperationConflictError(`Operation ${op.type} requires a known field and boolean value`)
|
|
1535
|
+
}
|
|
1536
|
+
const settings = { ...(doc[root]?.[op.questionId] ?? {}) }
|
|
1537
|
+
if (op.value) settings[op.field] = true
|
|
1538
|
+
else delete settings[op.field]
|
|
1539
|
+
const byQuestion = omitKey(doc[root] ?? {}, op.questionId)
|
|
1540
|
+
return {
|
|
1541
|
+
...doc,
|
|
1542
|
+
[root]: Object.keys(settings).length === 0 ? byQuestion : { ...byQuestion, [op.questionId]: settings },
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
/** A new condition edit must resolve its source and every alternative to that source's ownership. */
|
|
1547
|
+
const requireRuleCondition = (doc: QnrTemplateDocument, value: unknown): void => {
|
|
1548
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
1549
|
+
throw new OperationConflictError('A highlight condition must be an object')
|
|
1550
|
+
}
|
|
1551
|
+
const condition = value as RuleCondition
|
|
1552
|
+
requireRuleQuestion(doc, condition.sourceQuestionId)
|
|
1553
|
+
if ((condition.operator !== undefined && typeof condition.operator !== 'string') ||
|
|
1554
|
+
(condition.value !== undefined && (!isDocScalarValue(condition.value) ||
|
|
1555
|
+
(typeof condition.value === 'number' && !Number.isFinite(condition.value)))) ||
|
|
1556
|
+
(condition.alternativeId !== undefined && typeof condition.alternativeId !== 'string') ||
|
|
1557
|
+
(condition.alternativeIds !== undefined && (!Array.isArray(condition.alternativeIds) ||
|
|
1558
|
+
condition.alternativeIds.some((id) => typeof id !== 'string')))) {
|
|
1559
|
+
throw new OperationConflictError('A highlight condition carries a malformed member')
|
|
1560
|
+
}
|
|
1561
|
+
const alternatives = [
|
|
1562
|
+
...(condition.alternativeId === undefined ? [] : [condition.alternativeId]),
|
|
1563
|
+
...(condition.alternativeIds ?? []),
|
|
1564
|
+
]
|
|
1565
|
+
for (const id of alternatives) {
|
|
1566
|
+
if (!Object.hasOwn(doc.alternativesById ?? {}, id) ||
|
|
1567
|
+
!doc.alternativeOrderByQuestionId?.[condition.sourceQuestionId]?.includes(id)) {
|
|
1568
|
+
throw new OperationConflictError(`Alternative "${id}" is not owned by question "${condition.sourceQuestionId}"`)
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
/** A field edit requires the existing owner; unlike the released setter it never moves/replaces. */
|
|
1574
|
+
const setHighlightField = (
|
|
1575
|
+
doc: QnrTemplateDocument,
|
|
1576
|
+
op: Extract<TemplateOp, { type: 'highlightRule.setField' }>,
|
|
1577
|
+
): QnrTemplateDocument => {
|
|
1578
|
+
assertRuleEditShape(op, ['type', 'ruleId', 'questionId', 'field', 'value'])
|
|
1579
|
+
requireRuleQuestion(doc, op.questionId)
|
|
1580
|
+
const owners = Object.entries(doc.highlightRuleOrderByQuestionId ?? {})
|
|
1581
|
+
.filter(([, ids]) => ids.includes(op.ruleId)).map(([id]) => id)
|
|
1582
|
+
const existing = doc.highlightRulesById?.[op.ruleId]
|
|
1583
|
+
if (typeof op.ruleId !== 'string' || op.ruleId === '' || !existing ||
|
|
1584
|
+
!Object.hasOwn(doc.highlightRulesById ?? {}, op.ruleId) || owners.length !== 1 || owners[0] !== op.questionId) {
|
|
1585
|
+
throw new OperationConflictError(`Highlight rule "${op.ruleId}" is not owned by question "${op.questionId}"`)
|
|
1586
|
+
}
|
|
1587
|
+
const rule: HighlightRule = { ...existing }
|
|
1588
|
+
switch (op.field) {
|
|
1589
|
+
case 'condition':
|
|
1590
|
+
requireRuleCondition(doc, op.value)
|
|
1591
|
+
rule.condition = op.value
|
|
1592
|
+
break
|
|
1593
|
+
case 'state':
|
|
1594
|
+
if (op.value !== null && (typeof op.value !== 'number' || !Number.isFinite(op.value))) {
|
|
1595
|
+
throw new OperationConflictError('Highlight state must be a finite number or null')
|
|
1596
|
+
}
|
|
1597
|
+
if (op.value === null) delete rule.state
|
|
1598
|
+
else rule.state = op.value
|
|
1599
|
+
break
|
|
1600
|
+
case 'highlight':
|
|
1601
|
+
case 'showLink':
|
|
1602
|
+
if (op.value !== null && typeof op.value !== 'boolean') {
|
|
1603
|
+
throw new OperationConflictError('Highlight output must be a boolean or null')
|
|
1604
|
+
}
|
|
1605
|
+
if (op.value === null) delete rule[op.field]
|
|
1606
|
+
else rule[op.field] = op.value
|
|
1607
|
+
break
|
|
1608
|
+
default:
|
|
1609
|
+
throw new OperationConflictError('Unknown highlight field')
|
|
1610
|
+
}
|
|
1611
|
+
return { ...doc, highlightRulesById: { ...doc.highlightRulesById, [op.ruleId]: rule } }
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
const requireNarrativePresenceState = (document: QnrTemplateDocument): void => {
|
|
1615
|
+
const violations = findNarrativePresenceViolations(document)
|
|
1616
|
+
if (violations.length !== 0) throw new OperationConflictError(violations.map((v) => `${v.path}: ${v.detail}`).join('; '))
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
/** Strict NEW-arm-only ownership. Released replacing/moving setters keep their old replay. */
|
|
1620
|
+
const requireNarrativeQuestion = (doc: QnrTemplateDocument, questionId: unknown): void => {
|
|
1621
|
+
requireRuleQuestion(doc, questionId)
|
|
1622
|
+
if (!hasSingleNarrativeQuestionOwner(doc, questionId as string)) {
|
|
1623
|
+
throw new OperationConflictError(`Question "${questionId}" must have exactly one structural owner`)
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
const editNarrativeCollection = (
|
|
1628
|
+
doc: QnrTemplateDocument,
|
|
1629
|
+
op: Extract<TemplateOp, { type: 'narrativeRuleCollection.edit' }>,
|
|
1630
|
+
): QnrTemplateDocument => {
|
|
1631
|
+
assertRuleEditShape(op, ['type', 'questionId', 'expectedPresence', 'intent', 'action'])
|
|
1632
|
+
requireNarrativeQuestion(doc, op.questionId)
|
|
1633
|
+
const presence = doc.narrativeRuleSettingsByQuestionId?.[op.questionId]?.conditionalPresence ?? 'array'
|
|
1634
|
+
if (presence !== op.expectedPresence) throw new OperationConflictError('Stale narrative expectedPresence')
|
|
1635
|
+
if (!['array', 'absent', 'null'].includes(op.expectedPresence) ||
|
|
1636
|
+
op.intent !== (presence === 'null' ? 'repair-null' : 'edit')) {
|
|
1637
|
+
throw new OperationConflictError('Narrative edit requires the matching presence and intent')
|
|
1638
|
+
}
|
|
1639
|
+
const action = op.action
|
|
1640
|
+
if (action === null || typeof action !== 'object' || Array.isArray(action) ||
|
|
1641
|
+
!['set', 'delete', 'materialize'].includes(action.kind)) {
|
|
1642
|
+
throw new OperationConflictError('Unknown narrative collection action')
|
|
1643
|
+
}
|
|
1644
|
+
const allowed = action.kind === 'set' ? ['kind', 'ruleId', 'condition'] :
|
|
1645
|
+
action.kind === 'delete' ? ['kind', 'ruleId'] : ['kind']
|
|
1646
|
+
if (Object.keys(action).some((key) => !allowed.includes(key)) || allowed.some((key) => !Object.hasOwn(action, key))) {
|
|
1647
|
+
throw new OperationConflictError('Narrative action carries an undeclared member')
|
|
1648
|
+
}
|
|
1649
|
+
const settingsEntry = doc.narrativeRuleSettingsByQuestionId?.[op.questionId]
|
|
1650
|
+
if (settingsEntry !== undefined && (settingsEntry === null || typeof settingsEntry !== 'object' ||
|
|
1651
|
+
Array.isArray(settingsEntry) || Object.keys(settingsEntry).some((key) =>
|
|
1652
|
+
key !== 'conditionalPresence' && !((key === 'enabled' || key === 'requiredAll') && settingsEntry[key] === true)))) {
|
|
1653
|
+
throw new OperationConflictError('Narrative target settings are malformed')
|
|
1654
|
+
}
|
|
1655
|
+
if (Object.values(doc.narrativeRuleOrderByQuestionId ?? {}).some((ids) => !Array.isArray(ids) ||
|
|
1656
|
+
ids.some((id) => typeof id !== 'string' || id === ''))) {
|
|
1657
|
+
throw new OperationConflictError('Narrative ownership orders are malformed')
|
|
1658
|
+
}
|
|
1659
|
+
const order = doc.narrativeRuleOrderByQuestionId?.[op.questionId] ?? []
|
|
1660
|
+
if (new Set(order).size !== order.length || order.some((id) =>
|
|
1661
|
+
typeof id !== 'string' || id === '' || !Object.hasOwn(doc.narrativeRulesById ?? {}, id))) {
|
|
1662
|
+
throw new OperationConflictError('Narrative target order is malformed')
|
|
1663
|
+
}
|
|
1664
|
+
for (const id of order) {
|
|
1665
|
+
const rule = doc.narrativeRulesById?.[id]
|
|
1666
|
+
const owners = Object.entries(doc.narrativeRuleOrderByQuestionId ?? {})
|
|
1667
|
+
.flatMap(([owner, ids]) => ids.filter((entry) => entry === id).map(() => owner))
|
|
1668
|
+
if (rule === null || typeof rule !== 'object' || Array.isArray(rule) ||
|
|
1669
|
+
!Object.hasOwn(rule, 'condition') || owners.length !== 1 || owners[0] !== op.questionId) {
|
|
1670
|
+
throw new OperationConflictError('Narrative target order must contain exclusively owned rule records')
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
let next = doc
|
|
1674
|
+
if (action.kind === 'materialize') {
|
|
1675
|
+
if (order.length !== 0) throw new OperationConflictError('Materialize requires an empty narrative collection')
|
|
1676
|
+
} else {
|
|
1677
|
+
if (typeof action.ruleId !== 'string' || action.ruleId === '') {
|
|
1678
|
+
throw new OperationConflictError('Narrative edit requires a nonempty ruleId')
|
|
1679
|
+
}
|
|
1680
|
+
const owners = Object.entries(doc.narrativeRuleOrderByQuestionId ?? {})
|
|
1681
|
+
.flatMap(([owner, ids]) => ids.filter((id) => id === action.ruleId).map(() => owner))
|
|
1682
|
+
const exists = Object.hasOwn(doc.narrativeRulesById ?? {}, action.ruleId)
|
|
1683
|
+
if ((exists || owners.length !== 0) && (!exists || owners.length !== 1 || owners[0] !== op.questionId)) {
|
|
1684
|
+
throw new OperationConflictError('Narrative rule must belong only to its target')
|
|
1685
|
+
}
|
|
1686
|
+
if (action.kind === 'delete') {
|
|
1687
|
+
if (presence !== 'array' || !exists) throw new OperationConflictError('Delete requires an existing ARRAY rule')
|
|
1688
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(doc.narrativeRulesById, doc.narrativeRuleOrderByQuestionId, action.ruleId)
|
|
1689
|
+
next = { ...doc, narrativeRulesById: rulesById, narrativeRuleOrderByQuestionId: orderByQuestionId }
|
|
1690
|
+
} else {
|
|
1691
|
+
requireRuleCondition(doc, action.condition)
|
|
1692
|
+
if (!Object.hasOwn(action.condition, 'sourceQuestionId') ||
|
|
1693
|
+
['operator', 'value', 'alternativeId', 'alternativeIds'].some((key) =>
|
|
1694
|
+
Object.hasOwn(action.condition, key) && action.condition[key] === undefined)) {
|
|
1695
|
+
throw new OperationConflictError('Narrative condition carries a missing or undefined member')
|
|
1696
|
+
}
|
|
1697
|
+
requireNarrativeQuestion(doc, action.condition.sourceQuestionId)
|
|
1698
|
+
const alternatives = [...(action.condition.alternativeId === undefined ? [] : [action.condition.alternativeId]),
|
|
1699
|
+
...(action.condition.alternativeIds ?? [])]
|
|
1700
|
+
if (Object.values(doc.alternativeOrderByQuestionId ?? {}).some((ids) => !Array.isArray(ids))) {
|
|
1701
|
+
throw new OperationConflictError('Alternative ownership orders are malformed')
|
|
1702
|
+
}
|
|
1703
|
+
for (const id of alternatives) {
|
|
1704
|
+
const owners = Object.entries(doc.alternativeOrderByQuestionId ?? {})
|
|
1705
|
+
.flatMap(([owner, ids]) => ids.filter((entry) => entry === id).map(() => owner))
|
|
1706
|
+
if (owners.length !== 1 || owners[0] !== action.condition.sourceQuestionId) {
|
|
1707
|
+
throw new OperationConflictError('Narrative alternative must have exactly its source owner')
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
const { rulesById, orderByQuestionId } = setScopedRule(doc.narrativeRulesById, doc.narrativeRuleOrderByQuestionId,
|
|
1711
|
+
action.ruleId, op.questionId, { ...doc.narrativeRulesById?.[action.ruleId], condition: action.condition })
|
|
1712
|
+
next = { ...doc, narrativeRulesById: rulesById, narrativeRuleOrderByQuestionId: orderByQuestionId }
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
const settings = { ...(doc.narrativeRuleSettingsByQuestionId?.[op.questionId] ?? {}) }
|
|
1716
|
+
delete settings.conditionalPresence
|
|
1717
|
+
const byQuestion = omitKey(doc.narrativeRuleSettingsByQuestionId ?? {}, op.questionId)
|
|
1718
|
+
return { ...next, narrativeRuleSettingsByQuestionId:
|
|
1719
|
+
Object.keys(settings).length === 0 ? byQuestion : { ...byQuestion, [op.questionId]: settings } }
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1506
1722
|
export const applyOperation = (document: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument => {
|
|
1723
|
+
requireNarrativePresenceState(document)
|
|
1507
1724
|
const next = reduce(document, op)
|
|
1725
|
+
requireNarrativePresenceState(next)
|
|
1508
1726
|
return prune({ ...next, revision: document.revision + 1 })
|
|
1509
1727
|
}
|
|
1510
1728
|
|
|
@@ -2749,6 +2967,18 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
2749
2967
|
})
|
|
2750
2968
|
}
|
|
2751
2969
|
|
|
2970
|
+
case 'highlightRule.setField':
|
|
2971
|
+
return setHighlightField(doc, op)
|
|
2972
|
+
|
|
2973
|
+
case 'highlightRuleSettings.set':
|
|
2974
|
+
return setRuleSetting(doc, op, 'highlightRuleSettingsByQuestionId')
|
|
2975
|
+
|
|
2976
|
+
case 'narrativeRuleSettings.set':
|
|
2977
|
+
return setRuleSetting(doc, op, 'narrativeRuleSettingsByQuestionId')
|
|
2978
|
+
|
|
2979
|
+
case 'narrativeRuleCollection.edit':
|
|
2980
|
+
return editNarrativeCollection(doc, op)
|
|
2981
|
+
|
|
2752
2982
|
case 'narrativeRule.set': {
|
|
2753
2983
|
if (!doc.questionsById?.[op.questionId]) {
|
|
2754
2984
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
@@ -27,6 +27,7 @@ export * from './templateDocument.js'
|
|
|
27
27
|
export * from './templateAuthoringMeta.js'
|
|
28
28
|
export * from './answerDocument.js'
|
|
29
29
|
export * from './operations.js'
|
|
30
|
+
export * from './narrativePresence.js'
|
|
30
31
|
export * from './answerOperations.js'
|
|
31
32
|
export * from './applyOperation.js'
|
|
32
33
|
export * from './instanceLifecycle.js'
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { describe, it } from 'node:test'
|
|
3
|
+
import type { TemplateOp } from './operations.js'
|
|
4
|
+
import { emptyTemplateDocument, type QnrTemplateDocument } from './templateDocument.js'
|
|
5
|
+
|
|
6
|
+
// Reuse the SAME behavioral assertions against emitted JS for the compiling-mutant gate.
|
|
7
|
+
const testEntry = process.env['ASMA_NARRATIVE_TEST_ENTRY'] ?? './index.js'
|
|
8
|
+
const contract: typeof import('./index.js') = await import(testEntry)
|
|
9
|
+
const {
|
|
10
|
+
applyOperation, OperationConflictError, canonicalJson, hashCanonical, reduceToMinimalForm,
|
|
11
|
+
IMPLEMENTED_OP_TYPES, findNarrativePresenceViolations, findTemplateDocumentContractViolations,
|
|
12
|
+
qnrTemplateDocumentSchema, SCHEMA_TEMPLATE_OP_TYPES, templateOpSchema, templateRuleAuthoringIsDefault,
|
|
13
|
+
validateTemplateDocument, classifyTemplateAuthoringOverlap, readTemplateAuthoringLocus, templateAuthoringIntentsOf,
|
|
14
|
+
} = contract
|
|
15
|
+
|
|
16
|
+
type Edit = Extract<TemplateOp, { type: 'narrativeRuleCollection.edit' }>
|
|
17
|
+
const base = (): QnrTemplateDocument => [
|
|
18
|
+
{ type: 'question.create', questionId: 'q', questionType: 'TextShort' },
|
|
19
|
+
{ type: 'question.create', questionId: 'other', questionType: 'TextShort' },
|
|
20
|
+
{ type: 'question.create', questionId: 's', questionType: 'Dropdown' },
|
|
21
|
+
{ type: 'alternative.create', questionId: 's', alternativeId: 'a' },
|
|
22
|
+
{ type: 'question.create', questionId: 'grid', questionType: 'QuestionGrid' },
|
|
23
|
+
{ type: 'gridColumn.create', questionId: 'grid', columnQuestionId: 'column', questionType: 'BooleanQuestion' },
|
|
24
|
+
].reduce((doc, op) => applyOperation(doc, op as TemplateOp), emptyTemplateDocument('m065-shared'))
|
|
25
|
+
const marked = (presence: 'absent' | 'null', target = 'q'): QnrTemplateDocument => ({ ...base(),
|
|
26
|
+
narrativeRuleSettingsByQuestionId: { [target]: { enabled: true, requiredAll: true, conditionalPresence: presence } },
|
|
27
|
+
})
|
|
28
|
+
const set = (expectedPresence: 'array' | 'absent' = 'absent', target = 'q', ruleId = 'r'): Edit => ({
|
|
29
|
+
type: 'narrativeRuleCollection.edit', questionId: target, expectedPresence, intent: 'edit',
|
|
30
|
+
action: { kind: 'set', ruleId, condition: { sourceQuestionId: 's', alternativeIds: ['a'], extra: ['preserved', 'preserved'] } },
|
|
31
|
+
})
|
|
32
|
+
const materialize = (expectedPresence: 'array' | 'absent' = 'absent', target = 'q'): Edit => ({
|
|
33
|
+
type: 'narrativeRuleCollection.edit', questionId: target, expectedPresence, intent: 'edit', action: { kind: 'materialize' },
|
|
34
|
+
})
|
|
35
|
+
const unsafe = (doc: QnrTemplateDocument, op: unknown) => applyOperation(doc, op as TemplateOp)
|
|
36
|
+
const refuse = (doc: QnrTemplateDocument, op: unknown): void => {
|
|
37
|
+
const before = structuredClone(doc)
|
|
38
|
+
assert.throws(() => unsafe(doc, op), (error) => {
|
|
39
|
+
assert.ok(error instanceof OperationConflictError)
|
|
40
|
+
assert.equal(error.statusCode, 409)
|
|
41
|
+
assert.equal(error.code, 'qnr_operation_conflict')
|
|
42
|
+
return true
|
|
43
|
+
})
|
|
44
|
+
assert.deepEqual(doc, before, 'no partial input content/revision writes')
|
|
45
|
+
}
|
|
46
|
+
const remove = (questionId = 'q', ruleId = 'r'): Edit => ({ type: 'narrativeRuleCollection.edit', questionId,
|
|
47
|
+
expectedPresence: 'array', intent: 'edit', action: { kind: 'delete', ruleId } })
|
|
48
|
+
|
|
49
|
+
describe('M065 shared O07/O09/O10/O11/O12: atomic source presence transitions', () => {
|
|
50
|
+
for (const target of ['q', 'column']) it(`set enters ARRAY at ${target} once and preserves flags/extras/order`, () => {
|
|
51
|
+
const input = marked('absent', target)
|
|
52
|
+
const before = canonicalJson(input)
|
|
53
|
+
const op = set('absent', target)
|
|
54
|
+
const next = applyOperation(input, op)
|
|
55
|
+
assert.equal(next.revision, input.revision + 1)
|
|
56
|
+
assert.equal(canonicalJson(input), before)
|
|
57
|
+
assert.deepEqual(next.narrativeRuleSettingsByQuestionId, { [target]: { enabled: true, requiredAll: true } })
|
|
58
|
+
assert.deepEqual(next.narrativeRulesById?.r, { condition: (op.action as { condition: unknown }).condition })
|
|
59
|
+
assert.deepEqual(next.narrativeRuleOrderByQuestionId, { [target]: ['r'] })
|
|
60
|
+
const duplicate = applyOperation(next, set('array', target, 'r2'))
|
|
61
|
+
assert.deepEqual(duplicate.narrativeRuleOrderByQuestionId, { [target]: ['r', 'r2'] })
|
|
62
|
+
assert.deepEqual(duplicate.narrativeRulesById?.r, duplicate.narrativeRulesById?.r2, 'duplicate CONDITIONS remain separate occurrences')
|
|
63
|
+
const withExtra = { ...duplicate, narrativeRulesById: { ...duplicate.narrativeRulesById, r: { ...duplicate.narrativeRulesById!.r!, imported: 'keep' } } }
|
|
64
|
+
const updated = applyOperation(withExtra, { ...set('array', target), action: { kind: 'set', ruleId: 'r', condition: { sourceQuestionId: 'column' } } })
|
|
65
|
+
assert.deepEqual(updated.narrativeRulesById?.r, { imported: 'keep', condition: { sourceQuestionId: 'column' } })
|
|
66
|
+
assert.deepEqual(updated.narrativeRulesById?.r2, withExtra.narrativeRulesById?.r2)
|
|
67
|
+
assert.deepEqual(updated.narrativeRuleOrderByQuestionId, duplicate.narrativeRuleOrderByQuestionId)
|
|
68
|
+
})
|
|
69
|
+
it('delete-last remains ARRAY and preserves both flags', () => {
|
|
70
|
+
const input = applyOperation(marked('absent'), set())
|
|
71
|
+
const next = applyOperation(input, remove())
|
|
72
|
+
assert.equal(next.narrativeRulesById, undefined)
|
|
73
|
+
assert.equal(next.narrativeRuleOrderByQuestionId, undefined)
|
|
74
|
+
assert.deepEqual(next.narrativeRuleSettingsByQuestionId, { q: { enabled: true, requiredAll: true } })
|
|
75
|
+
assert.equal(next.revision, input.revision + 1)
|
|
76
|
+
assert.deepEqual(applyOperation(next, materialize('array')), { ...next, revision: next.revision + 1 })
|
|
77
|
+
})
|
|
78
|
+
for (const presence of ['absent', 'null'] as const) {
|
|
79
|
+
for (const kind of ['set', 'materialize'] as const) it(`${presence} explicit ${kind} removes only the marker`, () => {
|
|
80
|
+
const doc = marked(presence)
|
|
81
|
+
const action = kind === 'set' ? set().action : { kind: 'materialize' as const }
|
|
82
|
+
const op = { type: 'narrativeRuleCollection.edit', questionId: 'q', expectedPresence: presence,
|
|
83
|
+
intent: presence === 'null' ? 'repair-null' : 'edit', action }
|
|
84
|
+
assert.equal(templateOpSchema.allows(op), true)
|
|
85
|
+
const next = unsafe(doc, op)
|
|
86
|
+
assert.deepEqual(next.narrativeRuleSettingsByQuestionId, { q: { enabled: true, requiredAll: true } })
|
|
87
|
+
assert.equal(next.revision, doc.revision + 1)
|
|
88
|
+
assert.equal(!!next.narrativeRulesById?.r, kind === 'set')
|
|
89
|
+
})
|
|
90
|
+
for (const field of ['enabled', 'requiredAll'] as const) it(`${presence} flag ${field} is fieldwise and unrelated edits preserve marker`, () => {
|
|
91
|
+
const doc = marked(presence)
|
|
92
|
+
const cleared = applyOperation(doc, { type: 'narrativeRuleSettings.set', questionId: 'q', field, value: false })
|
|
93
|
+
const expected = { ...doc.narrativeRuleSettingsByQuestionId!.q! }
|
|
94
|
+
delete expected[field]
|
|
95
|
+
assert.deepEqual(cleared.narrativeRuleSettingsByQuestionId, { q: expected })
|
|
96
|
+
const other = applyOperation(cleared, { type: 'template.updateMeta', patch: { title: 'changed' } })
|
|
97
|
+
assert.deepEqual(other.narrativeRuleSettingsByQuestionId, cleared.narrativeRuleSettingsByQuestionId)
|
|
98
|
+
assert.equal(other.narrativeRuleSettingsByQuestionId?.q?.conditionalPresence, presence)
|
|
99
|
+
})
|
|
100
|
+
it(`${presence} canonical fixed point differs from ARRAY-empty without inventing empty-order encoding`, async () => {
|
|
101
|
+
const doc = { ...marked(presence), narrativeRuleOrderByQuestionId: { q: [] } }
|
|
102
|
+
const canonical = reduceToMinimalForm(doc, { isDefault: templateRuleAuthoringIsDefault })
|
|
103
|
+
assert.equal(canonical.narrativeRuleSettingsByQuestionId?.q?.conditionalPresence, presence)
|
|
104
|
+
assert.equal(canonical.narrativeRuleOrderByQuestionId, undefined)
|
|
105
|
+
assert.deepEqual(reduceToMinimalForm(canonical, { isDefault: templateRuleAuthoringIsDefault }), canonical)
|
|
106
|
+
const array = applyOperation(canonical, {
|
|
107
|
+
type: 'narrativeRuleCollection.edit', questionId: 'q', expectedPresence: presence,
|
|
108
|
+
intent: presence === 'null' ? 'repair-null' : 'edit', action: { kind: 'materialize' },
|
|
109
|
+
} as Edit)
|
|
110
|
+
assert.notEqual(await hashCanonical(canonical), await hashCanonical({ ...array, revision: canonical.revision }),
|
|
111
|
+
'same revision: the source marker itself distinguishes ARRAY-empty')
|
|
112
|
+
assert.deepEqual(findTemplateDocumentContractViolations(canonical), [])
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
it('materialize marker-only settings prunes the target/root without manufacturing flags', () => {
|
|
116
|
+
const doc = { ...base(), narrativeRuleSettingsByQuestionId: { q: { conditionalPresence: 'absent' as const } } }
|
|
117
|
+
const next = applyOperation(doc, materialize())
|
|
118
|
+
assert.equal(Object.hasOwn(next, 'narrativeRuleSettingsByQuestionId'), false)
|
|
119
|
+
assert.equal(next.narrativeRuleOrderByQuestionId, undefined)
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
describe('M065 shared O06/O11/O16: closed wire and direct-replay refusal', () => {
|
|
124
|
+
for (const patch of [
|
|
125
|
+
{ questionId: '' }, { expectedPresence: 'ARRAY' }, { intent: 'repair-null' }, { extra: true },
|
|
126
|
+
{ action: null }, { action: [] }, { action: { kind: 'unknown' } },
|
|
127
|
+
{ action: { kind: 'materialize', extra: true } }, { action: { kind: 'set', ruleId: '', condition: { sourceQuestionId: 's' } } },
|
|
128
|
+
{ action: { kind: 'set', ruleId: 'r' } }, { action: { kind: 'set', ruleId: 'r', condition: null } },
|
|
129
|
+
{ action: { kind: 'set', ruleId: 'r', condition: { sourceQuestionId: 's', value: [] } } },
|
|
130
|
+
{ action: { kind: 'set', ruleId: 'r', condition: { sourceQuestionId: 's', alternativeIds: [1] } } },
|
|
131
|
+
{ action: { kind: 'set', ruleId: 'r', condition: { sourceQuestionId: 's', operator: 1 } } },
|
|
132
|
+
{ action: { kind: 'set', ruleId: 'r', condition: { sourceQuestionId: 's' }, extra: true } },
|
|
133
|
+
]) it(`wire and direct replay reject ${JSON.stringify(patch)}`, () => {
|
|
134
|
+
const op = { ...set(), ...patch }
|
|
135
|
+
assert.equal(templateOpSchema.allows(op), false)
|
|
136
|
+
refuse(marked('absent'), op)
|
|
137
|
+
})
|
|
138
|
+
for (const presence of ['array', 'absent', 'null'] as const) it(`stale ${presence} refuses with original bytes/revision`, () => {
|
|
139
|
+
const doc = presence === 'array' ? base() : marked(presence)
|
|
140
|
+
const expected = presence === 'array' ? 'absent' : 'array'
|
|
141
|
+
refuse(doc, { ...materialize(), expectedPresence: expected })
|
|
142
|
+
})
|
|
143
|
+
it('NULL requires repair intent; delete cannot repair NULL or ABSENT', () => {
|
|
144
|
+
for (const presence of ['absent', 'null'] as const) {
|
|
145
|
+
refuse(marked(presence), { ...remove(), expectedPresence: presence, intent: presence === 'null' ? 'repair-null' : 'edit' })
|
|
146
|
+
}
|
|
147
|
+
const op = { ...set(), expectedPresence: 'null', intent: 'edit' }
|
|
148
|
+
assert.equal(templateOpSchema.allows(op), false)
|
|
149
|
+
refuse(marked('null'), op)
|
|
150
|
+
assert.equal(templateOpSchema.allows({ ...remove(), expectedPresence: 'null', intent: 'repair-null' }), false)
|
|
151
|
+
})
|
|
152
|
+
for (const condition of [
|
|
153
|
+
{ sourceQuestionId: 'gone' }, { sourceQuestionId: 's', alternativeId: 'gone' },
|
|
154
|
+
{ sourceQuestionId: 's', alternativeIds: ['a', 'gone'] },
|
|
155
|
+
{ sourceQuestionId: 'column', alternativeId: 'a' },
|
|
156
|
+
{ sourceQuestionId: 's', value: Infinity }, { sourceQuestionId: 's', value: NaN },
|
|
157
|
+
]) it(`validates all condition references before marker removal: ${JSON.stringify(condition)}`, () => {
|
|
158
|
+
refuse(marked('absent'), { ...set(), action: { kind: 'set', ruleId: 'r', condition } })
|
|
159
|
+
})
|
|
160
|
+
for (const key of ['operator', 'value', 'alternativeId', 'alternativeIds']) it(`direct replay matches wire refusal for explicit undefined ${key}`, () => {
|
|
161
|
+
const op = { ...set(), action: { kind: 'set', ruleId: 'r', condition: { sourceQuestionId: 's', [key]: undefined } } }
|
|
162
|
+
assert.equal(templateOpSchema.allows(op), false)
|
|
163
|
+
refuse(marked('absent'), op)
|
|
164
|
+
})
|
|
165
|
+
it('refuses orphan/foreign/multiple/duplicate rule IDs and incomplete orders', () => {
|
|
166
|
+
const owned = applyOperation(base(), set('array', 'other'))
|
|
167
|
+
refuse(owned, set('array'))
|
|
168
|
+
refuse(owned, remove())
|
|
169
|
+
const orphan = { ...base(), narrativeRulesById: { r: { condition: { sourceQuestionId: 's' } } } }
|
|
170
|
+
refuse(orphan, set('array'))
|
|
171
|
+
const dangling = { ...base(), narrativeRuleOrderByQuestionId: { q: ['r'] } }
|
|
172
|
+
refuse(dangling, set('array'))
|
|
173
|
+
const ambiguous = { ...owned, narrativeRuleOrderByQuestionId: { q: ['r'], other: ['r'] } }
|
|
174
|
+
refuse(ambiguous, set('array'))
|
|
175
|
+
const duplicate = { ...owned, narrativeRuleOrderByQuestionId: { other: ['r', 'r'] } }
|
|
176
|
+
refuse(duplicate, set('array', 'other'))
|
|
177
|
+
refuse(base(), remove())
|
|
178
|
+
refuse(owned, materialize('array', 'other'))
|
|
179
|
+
})
|
|
180
|
+
for (const target of ['q', 's']) it(`refuses missing/orphan/duplicate structural owner ${target}`, () => {
|
|
181
|
+
const op = set('array')
|
|
182
|
+
const orphan = base()
|
|
183
|
+
orphan.questionOrder = orphan.questionOrder.filter((id) => id !== target)
|
|
184
|
+
refuse(orphan, op)
|
|
185
|
+
const duplicate = base()
|
|
186
|
+
duplicate.questionOrder.push(target)
|
|
187
|
+
refuse(duplicate, op)
|
|
188
|
+
const gridOwned = base()
|
|
189
|
+
gridOwned.questionsById!.grid!.grid!.columnIds!.push(target)
|
|
190
|
+
refuse(gridOwned, op)
|
|
191
|
+
const deleted = applyOperation(base(), { type: 'question.delete', questionId: target })
|
|
192
|
+
refuse(deleted, op)
|
|
193
|
+
})
|
|
194
|
+
it('fresh-ID set refuses a target order containing another foreign-owned rule', () => {
|
|
195
|
+
const doc = applyOperation(base(), set('array'))
|
|
196
|
+
doc.narrativeRuleOrderByQuestionId!.other = ['r']
|
|
197
|
+
refuse(doc, set('array', 'q', 'fresh'))
|
|
198
|
+
const malformed = applyOperation(base(), set('array'))
|
|
199
|
+
malformed.narrativeRulesById!.r = null as never
|
|
200
|
+
refuse(malformed, set('array', 'q', 'fresh'))
|
|
201
|
+
})
|
|
202
|
+
it('refuses duplicate/foreign alternative ownership without replacing the marker', () => {
|
|
203
|
+
const doc = marked('absent')
|
|
204
|
+
doc.alternativeOrderByQuestionId!.s!.push('a')
|
|
205
|
+
refuse(doc, set())
|
|
206
|
+
const foreign = marked('absent')
|
|
207
|
+
foreign.alternativeOrderByQuestionId!.column = ['a']
|
|
208
|
+
refuse(foreign, set())
|
|
209
|
+
})
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
describe('M065 shared O09/O10/O13/O16: document invariants, cleanup and old replay', () => {
|
|
213
|
+
for (const marker of [null, false, true, '', 'array', 'unknown', undefined]) it(`rejects malformed marker ${String(marker)} rather than canonicalizing it to ARRAY`, () => {
|
|
214
|
+
const doc = { ...base(), narrativeRuleSettingsByQuestionId: { q: { conditionalPresence: marker } } }
|
|
215
|
+
assert.equal(qnrTemplateDocumentSchema.allows(doc), false)
|
|
216
|
+
assert.equal(validateTemplateDocument(doc).ok, false)
|
|
217
|
+
refuse(doc as QnrTemplateDocument, materialize('array'))
|
|
218
|
+
})
|
|
219
|
+
it('new settings entry stays closed and validates true-only flags', () => {
|
|
220
|
+
for (const patch of [{ enabled: false }, { requiredAll: null }, { unknown: true }]) {
|
|
221
|
+
const doc = { ...marked('absent'), narrativeRuleSettingsByQuestionId: { q: { conditionalPresence: 'absent', ...patch } } }
|
|
222
|
+
assert.equal(qnrTemplateDocumentSchema.allows(doc), false)
|
|
223
|
+
refuse(doc as QnrTemplateDocument, materialize())
|
|
224
|
+
}
|
|
225
|
+
})
|
|
226
|
+
it('marker plus owned rules, missing owner and duplicate owner are contract refusals', () => {
|
|
227
|
+
const withRule = applyOperation(base(), set('array'))
|
|
228
|
+
const contradiction = { ...withRule, narrativeRuleSettingsByQuestionId: marked('absent').narrativeRuleSettingsByQuestionId }
|
|
229
|
+
assert.equal(validateTemplateDocument(contradiction).ok, false)
|
|
230
|
+
assert.ok(findTemplateDocumentContractViolations(contradiction).some((v) => v.law === 'NARRATIVE-PRESENCE'))
|
|
231
|
+
refuse(contradiction, { type: 'template.updateMeta', patch: { title: 'cannot normalize' } })
|
|
232
|
+
const missing = marked('null', 'gone')
|
|
233
|
+
assert.equal(validateTemplateDocument(missing).ok, false)
|
|
234
|
+
const duplicate = marked('absent')
|
|
235
|
+
duplicate.questionOrder.push('q')
|
|
236
|
+
assert.equal(validateTemplateDocument(duplicate).ok, false)
|
|
237
|
+
})
|
|
238
|
+
it('old setter cannot create marker+rules on new input; ARRAY old setter still moves/replaces/dangles', () => {
|
|
239
|
+
refuse(marked('absent'), { type: 'narrativeRule.set', questionId: 'q', ruleId: 'r', condition: { sourceQuestionId: 's' } })
|
|
240
|
+
const doc = applyOperation(base(), set('array'))
|
|
241
|
+
const next = applyOperation(doc, { type: 'narrativeRule.set', questionId: 'other', ruleId: 'r', condition: { sourceQuestionId: 'historically-dangling', conditionalPresence: 'null' } })
|
|
242
|
+
assert.deepEqual(next.narrativeRuleOrderByQuestionId, { other: ['r'] })
|
|
243
|
+
assert.deepEqual(next.narrativeRulesById?.r, { condition: { sourceQuestionId: 'historically-dangling', conditionalPresence: 'null' } })
|
|
244
|
+
assert.deepEqual(findNarrativePresenceViolations(next), [])
|
|
245
|
+
})
|
|
246
|
+
for (const questionId of ['q', 'column', 'grid']) it(`existing deletion cleans marker owner ${questionId}`, () => {
|
|
247
|
+
const owner = questionId === 'grid' ? 'column' : questionId
|
|
248
|
+
const doc = marked('null', owner)
|
|
249
|
+
doc.narrativeRuleSettingsByQuestionId!.other = { enabled: true, conditionalPresence: 'absent' }
|
|
250
|
+
const next = applyOperation(doc, { type: 'question.delete', questionId })
|
|
251
|
+
assert.equal(next.narrativeRuleSettingsByQuestionId?.[owner], undefined)
|
|
252
|
+
assert.deepEqual(next.narrativeRuleSettingsByQuestionId?.other, doc.narrativeRuleSettingsByQuestionId?.other)
|
|
253
|
+
assert.equal(validateTemplateDocument(next).ok, true)
|
|
254
|
+
})
|
|
255
|
+
it('source deletion preserves target marker and never retargets it', () => {
|
|
256
|
+
const doc = marked('absent')
|
|
257
|
+
const next = applyOperation(doc, { type: 'question.delete', questionId: 's' })
|
|
258
|
+
assert.deepEqual(next.narrativeRuleSettingsByQuestionId, doc.narrativeRuleSettingsByQuestionId)
|
|
259
|
+
assert.equal(next.narrativeRuleSettingsByQuestionId?.s, undefined)
|
|
260
|
+
})
|
|
261
|
+
it('new vocabulary is present once and exported schemas preserve DOC laws', () => {
|
|
262
|
+
for (const inventory of [IMPLEMENTED_OP_TYPES, SCHEMA_TEMPLATE_OP_TYPES]) {
|
|
263
|
+
assert.equal(inventory.filter((type) => type === 'narrativeRuleCollection.edit').length, 1)
|
|
264
|
+
}
|
|
265
|
+
assert.deepEqual([...IMPLEMENTED_OP_TYPES].sort(), [...SCHEMA_TEMPLATE_OP_TYPES].sort())
|
|
266
|
+
})
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
describe('M065 shared C_CONFLICT: intents, ownership dependencies and released classifier', () => {
|
|
270
|
+
const status = (a: TemplateOp, b: TemplateOp, expected: string): void => {
|
|
271
|
+
assert.equal(classifyTemplateAuthoringOverlap(a, b).status, expected)
|
|
272
|
+
assert.equal(classifyTemplateAuthoringOverlap(b, a).status, expected, 'pair is symmetric')
|
|
273
|
+
}
|
|
274
|
+
it('marker leaf is determinate, order/rule opaque; materialize only writes marker', () => {
|
|
275
|
+
assert.deepEqual(templateAuthoringIntentsOf(set()), [
|
|
276
|
+
{ locus: 'narrativeRuleSettingsByQuestionId["q"].conditionalPresence', kind: 'set', value: null },
|
|
277
|
+
{ locus: 'narrativeRuleOrderByQuestionId["q"]', kind: 'opaque' },
|
|
278
|
+
{ locus: 'narrativeRulesById["r"]', kind: 'opaque' },
|
|
279
|
+
])
|
|
280
|
+
assert.deepEqual(templateAuthoringIntentsOf(materialize()), [templateAuthoringIntentsOf(set())[0]])
|
|
281
|
+
})
|
|
282
|
+
it('same-target distinct rule edits conflict, different-target independent rules commute', () => {
|
|
283
|
+
status(set(), set('absent', 'q', 'r2'), 'conflict')
|
|
284
|
+
status(set(), set('absent', 'other', 'r2'), 'disjoint')
|
|
285
|
+
status(set(), set('absent', 'other', 'r'), 'conflict')
|
|
286
|
+
status(materialize(), materialize(), 'convergent')
|
|
287
|
+
status(materialize(), set(), 'conflict')
|
|
288
|
+
})
|
|
289
|
+
it('flag-only writes commute with collection edit and with one another in actual reducer', () => {
|
|
290
|
+
for (const field of ['enabled', 'requiredAll'] as const) {
|
|
291
|
+
const flag: TemplateOp = { type: 'narrativeRuleSettings.set', questionId: 'q', field, value: false }
|
|
292
|
+
status(set(), flag, 'disjoint')
|
|
293
|
+
const doc = marked('absent')
|
|
294
|
+
assert.deepEqual(applyOperation(applyOperation(doc, set()), flag), applyOperation(applyOperation(doc, flag), set()))
|
|
295
|
+
}
|
|
296
|
+
})
|
|
297
|
+
it('intervening target/source/grid deletion, alternative deletion/order and legacy rule move/delete conflict', () => {
|
|
298
|
+
for (const op of [
|
|
299
|
+
{ type: 'question.delete', questionId: 'q' }, { type: 'question.delete', questionId: 's' },
|
|
300
|
+
{ type: 'question.delete', questionId: 'grid' },
|
|
301
|
+
{ type: 'gridColumn.move', questionId: 'grid', columnQuestionId: 'column', toIndex: 0 },
|
|
302
|
+
{ type: 'alternative.delete', questionId: 's', alternativeId: 'a' },
|
|
303
|
+
{ type: 'alternative.move', questionId: 's', alternativeId: 'a', toIndex: 1 },
|
|
304
|
+
{ type: 'narrativeRule.set', questionId: 'other', ruleId: 'r', condition: { sourceQuestionId: 's' } },
|
|
305
|
+
{ type: 'narrativeRule.delete', ruleId: 'r' },
|
|
306
|
+
] as TemplateOp[]) status(set(), op, 'conflict')
|
|
307
|
+
status(materialize(), { type: 'narrativeRule.set', questionId: 'q', ruleId: 'r', condition: { sourceQuestionId: 's' } }, 'conflict')
|
|
308
|
+
})
|
|
309
|
+
it('pure source title/value edits and OLD-vs-OLD classifier stay released', () => {
|
|
310
|
+
for (const field of ['title', 'value']) status(set(), { type: 'question.updateField', questionId: 's', field, value: 'changed' }, 'disjoint')
|
|
311
|
+
const old: TemplateOp = { type: 'narrativeRule.set', questionId: 'q', ruleId: 'r', condition: { sourceQuestionId: 's' } }
|
|
312
|
+
assert.deepEqual(templateAuthoringIntentsOf(old), [])
|
|
313
|
+
status(old, { ...old, questionId: 'other' }, 'disjoint')
|
|
314
|
+
})
|
|
315
|
+
it('conflict value reader handles exact bracket IDs, own fields and missing/default ARRAY', () => {
|
|
316
|
+
const id = 'q].a"quoted'
|
|
317
|
+
const doc = marked('null')
|
|
318
|
+
doc.questionsById![id] = { type: 'TextShort' }
|
|
319
|
+
doc.questionOrder.push(id)
|
|
320
|
+
doc.narrativeRuleSettingsByQuestionId![id] = { conditionalPresence: 'absent' }
|
|
321
|
+
const locus = `narrativeRuleSettingsByQuestionId[${JSON.stringify(id)}].conditionalPresence`
|
|
322
|
+
assert.equal(readTemplateAuthoringLocus(doc, locus), 'absent')
|
|
323
|
+
assert.equal(readTemplateAuthoringLocus(doc, 'narrativeRuleSettingsByQuestionId["q"].conditionalPresence'), 'null')
|
|
324
|
+
assert.equal(readTemplateAuthoringLocus(doc, 'narrativeRulesById["gone"]'), null)
|
|
325
|
+
assert.equal(readTemplateAuthoringLocus(doc, 'narrativeRuleSettingsByQuestionId["__proto__"].conditionalPresence'), null)
|
|
326
|
+
assert.equal(readTemplateAuthoringLocus(applyOperation(doc, materialize('absent', id)), locus), null)
|
|
327
|
+
})
|
|
328
|
+
})
|