asma-core-helpers 0.41.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 +108 -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 +26 -1
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js +1 -0
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +44 -1
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +18 -1
- 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 +9 -2
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js.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 +114 -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 +11 -0
- package/src/collaboration/schemas.ts +27 -2
- package/src/collaboration/templateAuthoringMeta.ts +107 -6
- package/src/collaboration/templateDocument.ts +14 -4
- package/src/g-definitions.ts +13 -0
- package/src/overlayBack.test.ts +742 -0
- package/src/overlayBack.ts +647 -0
|
@@ -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,
|
|
@@ -1610,8 +1611,118 @@ const setHighlightField = (
|
|
|
1610
1611
|
return { ...doc, highlightRulesById: { ...doc.highlightRulesById, [op.ruleId]: rule } }
|
|
1611
1612
|
}
|
|
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
|
+
|
|
1613
1722
|
export const applyOperation = (document: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument => {
|
|
1723
|
+
requireNarrativePresenceState(document)
|
|
1614
1724
|
const next = reduce(document, op)
|
|
1725
|
+
requireNarrativePresenceState(next)
|
|
1615
1726
|
return prune({ ...next, revision: document.revision + 1 })
|
|
1616
1727
|
}
|
|
1617
1728
|
|
|
@@ -2865,6 +2976,9 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
2865
2976
|
case 'narrativeRuleSettings.set':
|
|
2866
2977
|
return setRuleSetting(doc, op, 'narrativeRuleSettingsByQuestionId')
|
|
2867
2978
|
|
|
2979
|
+
case 'narrativeRuleCollection.edit':
|
|
2980
|
+
return editNarrativeCollection(doc, op)
|
|
2981
|
+
|
|
2868
2982
|
case 'narrativeRule.set': {
|
|
2869
2983
|
if (!doc.questionsById?.[op.questionId]) {
|
|
2870
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
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { QnrTemplateDocument } from './templateDocument.js'
|
|
2
|
+
|
|
3
|
+
/** Additive source-state laws; old ARRAY documents have no new validation or reinterpretation. */
|
|
4
|
+
export type NarrativePresenceViolation = {
|
|
5
|
+
law: 'NARRATIVE-PRESENCE'
|
|
6
|
+
path: string
|
|
7
|
+
detail: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Count occurrences, not just distinct owners: duplicate structural references are ambiguous. */
|
|
11
|
+
export const hasSingleNarrativeQuestionOwner = (document: QnrTemplateDocument, questionId: string): boolean => {
|
|
12
|
+
if (questionId === '' || !Object.hasOwn(document.questionsById ?? {}, questionId)) return false
|
|
13
|
+
let occurrences = document.questionOrder.filter((id) => id === questionId).length
|
|
14
|
+
for (const question of Object.values(document.questionsById ?? {})) {
|
|
15
|
+
if (question.type === 'QuestionGrid') {
|
|
16
|
+
occurrences += (question.grid?.columnIds ?? []).filter((id) => id === questionId).length
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return occurrences === 1
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Marker position is exact: similarly named open rule/condition extras are ordinary historical data. */
|
|
23
|
+
export const findNarrativePresenceViolations = (document: QnrTemplateDocument): NarrativePresenceViolation[] => {
|
|
24
|
+
const violations: NarrativePresenceViolation[] = []
|
|
25
|
+
for (const [questionId, settings] of Object.entries(document.narrativeRuleSettingsByQuestionId ?? {})) {
|
|
26
|
+
if (settings === null || typeof settings !== 'object' || !Object.hasOwn(settings, 'conditionalPresence')) continue
|
|
27
|
+
const path = `narrativeRuleSettingsByQuestionId[${JSON.stringify(questionId)}].conditionalPresence`
|
|
28
|
+
const marker = settings.conditionalPresence
|
|
29
|
+
const validShape = !Array.isArray(settings) && Object.keys(settings).every((key) =>
|
|
30
|
+
key === 'conditionalPresence' || ((key === 'enabled' || key === 'requiredAll') && settings[key] === true))
|
|
31
|
+
if (!validShape || (marker !== 'absent' && marker !== 'null')) {
|
|
32
|
+
violations.push({ law: 'NARRATIVE-PRESENCE', path, detail: 'Malformed source presence settings' })
|
|
33
|
+
}
|
|
34
|
+
if (!hasSingleNarrativeQuestionOwner(document, questionId)) {
|
|
35
|
+
violations.push({ law: 'NARRATIVE-PRESENCE', path, detail: 'Presence target must have exactly one structural owner' })
|
|
36
|
+
}
|
|
37
|
+
if ((document.narrativeRuleOrderByQuestionId?.[questionId]?.length ?? 0) !== 0) {
|
|
38
|
+
violations.push({ law: 'NARRATIVE-PRESENCE', path, detail: 'Source presence cannot coexist with owned narrative rules' })
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return violations
|
|
42
|
+
}
|
|
@@ -73,6 +73,11 @@ export type HighlightRuleFieldWrite =
|
|
|
73
73
|
| { field: 'state'; value: number | null }
|
|
74
74
|
| { field: 'highlight' | 'showLink'; value: boolean | null }
|
|
75
75
|
|
|
76
|
+
export type NarrativeRuleCollectionAction =
|
|
77
|
+
| { kind: 'set'; ruleId: NarrativeRuleId; condition: RuleCondition }
|
|
78
|
+
| { kind: 'delete'; ruleId: NarrativeRuleId }
|
|
79
|
+
| { kind: 'materialize' }
|
|
80
|
+
|
|
76
81
|
export type TemplateOp =
|
|
77
82
|
| { type: 'template.updateMeta'; patch: Record<string, OpValue | Record<string, unknown>> }
|
|
78
83
|
| {
|
|
@@ -452,6 +457,11 @@ export type TemplateOp =
|
|
|
452
457
|
| ({ type: 'highlightRule.setField'; ruleId: HighlightRuleId; questionId: QuestionId } & HighlightRuleFieldWrite)
|
|
453
458
|
| { type: 'highlightRuleSettings.set'; questionId: QuestionId; field: RuleSettingField; value: boolean }
|
|
454
459
|
| { type: 'narrativeRuleSettings.set'; questionId: QuestionId; field: RuleSettingField; value: boolean }
|
|
460
|
+
/** Atomic explicit ARRAY selection/edit; ABSENT/NULL may only originate in validated source. */
|
|
461
|
+
| ({ type: 'narrativeRuleCollection.edit'; questionId: QuestionId } & (
|
|
462
|
+
| { expectedPresence: 'array' | 'absent'; intent: 'edit'; action: NarrativeRuleCollectionAction }
|
|
463
|
+
| { expectedPresence: 'null'; intent: 'repair-null'; action: Exclude<NarrativeRuleCollectionAction, { kind: 'delete' }> }
|
|
464
|
+
))
|
|
455
465
|
| { type: 'narrativeRule.set'; ruleId: NarrativeRuleId; questionId: QuestionId; condition: RuleCondition }
|
|
456
466
|
| { type: 'narrativeRule.delete'; ruleId: NarrativeRuleId }
|
|
457
467
|
| {
|
|
@@ -533,6 +543,7 @@ const OP_TYPE_COVERAGE: Record<TemplateOpType, true> = {
|
|
|
533
543
|
'highlightRule.setField': true,
|
|
534
544
|
'highlightRuleSettings.set': true,
|
|
535
545
|
'narrativeRuleSettings.set': true,
|
|
546
|
+
'narrativeRuleCollection.edit': true,
|
|
536
547
|
'narrativeRule.set': true,
|
|
537
548
|
'narrativeRule.delete': true,
|
|
538
549
|
'qnrRule.set': true,
|
|
@@ -5,6 +5,7 @@ import { findDocLawViolations, type DocLawViolation } from './docLaws.js'
|
|
|
5
5
|
import { IMPLEMENTED_ANSWER_OP_TYPES } from './answerOperations.js'
|
|
6
6
|
import { IMPLEMENTED_OP_TYPES } from './operations.js'
|
|
7
7
|
import { QUESTION_TYPES } from './questionTypes.js'
|
|
8
|
+
import { findNarrativePresenceViolations, type NarrativePresenceViolation } from './narrativePresence.js'
|
|
8
9
|
import {
|
|
9
10
|
COMPATIBILITY_COLLECTIONS,
|
|
10
11
|
isTemplateAuthoringDefault,
|
|
@@ -99,6 +100,7 @@ const highlightRuleSettingsSchema = type({
|
|
|
99
100
|
const narrativeRuleSettingsSchema = type({
|
|
100
101
|
enabled: 'true?',
|
|
101
102
|
requiredAll: 'true?',
|
|
103
|
+
conditionalPresence: '("absent" | "null")?',
|
|
102
104
|
'+': 'reject',
|
|
103
105
|
})
|
|
104
106
|
|
|
@@ -836,6 +838,23 @@ export const templateOpSchema = type.or(
|
|
|
836
838
|
type: '"narrativeRuleSettings.set"', questionId: 'string > 0',
|
|
837
839
|
field: '"enabled" | "requiredAll"', value: 'boolean', '+': 'reject',
|
|
838
840
|
}),
|
|
841
|
+
type({
|
|
842
|
+
type: '"narrativeRuleCollection.edit"', questionId: 'string > 0',
|
|
843
|
+
expectedPresence: '"array" | "absent"', intent: '"edit"', '+': 'reject',
|
|
844
|
+
action: type.or(
|
|
845
|
+
type({ kind: '"set"', ruleId: 'string > 0', condition: ruleConditionSchema, '+': 'reject' }),
|
|
846
|
+
type({ kind: '"delete"', ruleId: 'string > 0', '+': 'reject' }),
|
|
847
|
+
type({ kind: '"materialize"', '+': 'reject' }),
|
|
848
|
+
),
|
|
849
|
+
}),
|
|
850
|
+
type({
|
|
851
|
+
type: '"narrativeRuleCollection.edit"', questionId: 'string > 0',
|
|
852
|
+
expectedPresence: '"null"', intent: '"repair-null"', '+': 'reject',
|
|
853
|
+
action: type.or(
|
|
854
|
+
type({ kind: '"set"', ruleId: 'string > 0', condition: ruleConditionSchema, '+': 'reject' }),
|
|
855
|
+
type({ kind: '"materialize"', '+': 'reject' }),
|
|
856
|
+
),
|
|
857
|
+
}),
|
|
839
858
|
type({ type: '"narrativeRule.set"', ruleId: 'string', questionId: 'string', condition: ruleConditionSchema }),
|
|
840
859
|
type({ type: '"narrativeRule.delete"', ruleId: 'string' }),
|
|
841
860
|
type({
|
|
@@ -1308,13 +1327,18 @@ const validate =
|
|
|
1308
1327
|
return { ok: true, value: out as T }
|
|
1309
1328
|
}
|
|
1310
1329
|
|
|
1311
|
-
export const validateTemplateDocument =
|
|
1330
|
+
export const validateTemplateDocument = (value: unknown): Validation<QnrTemplateDocument> => {
|
|
1331
|
+
const shape = validate<QnrTemplateDocument>(qnrTemplateDocumentSchema)(value)
|
|
1332
|
+
if (!shape.ok) return shape
|
|
1333
|
+
const violations = findNarrativePresenceViolations(shape.value)
|
|
1334
|
+
return violations.length === 0 ? shape : { ok: false, summary: violations.map((v) => `${v.path}: ${v.detail}`).join('; ') }
|
|
1335
|
+
}
|
|
1312
1336
|
|
|
1313
1337
|
/**
|
|
1314
1338
|
* Full template-document admission check for the contract test: schema + DOC-LAW-1/2 lints +
|
|
1315
1339
|
* default lint + binding uniqueness. A document passes only in its canonical minimal form.
|
|
1316
1340
|
*/
|
|
1317
|
-
export type TemplateDocumentContractViolation = DocLawViolation | QuestionOwnershipViolation
|
|
1341
|
+
export type TemplateDocumentContractViolation = DocLawViolation | QuestionOwnershipViolation | NarrativePresenceViolation
|
|
1318
1342
|
|
|
1319
1343
|
export const findTemplateDocumentContractViolations = (
|
|
1320
1344
|
document: QnrTemplateDocument,
|
|
@@ -1322,6 +1346,7 @@ export const findTemplateDocumentContractViolations = (
|
|
|
1322
1346
|
...findDocLawViolations(document),
|
|
1323
1347
|
...findDocLawDefaultViolations(document),
|
|
1324
1348
|
...findQuestionOwnershipViolations(document),
|
|
1349
|
+
...findNarrativePresenceViolations(document),
|
|
1325
1350
|
]
|
|
1326
1351
|
|
|
1327
1352
|
export const findDuplicateBindingTargetsSummary = (document: QnrTemplateDocument): string[] =>
|