asma-core-helpers 0.26.0 → 0.27.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 +354 -69
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/index.d.ts +1 -0
- package/lib/collaboration/index.d.ts.map +1 -1
- package/lib/collaboration/index.js +1 -0
- package/lib/collaboration/index.js.map +1 -1
- package/lib/collaboration/operations.d.ts +35 -1
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js +7 -0
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +125 -2
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +135 -10
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/collaboration/templateDocument.d.ts +28 -1
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.d.ts +4 -0
- package/lib/collaboration/testHelpers/documentGenerator.d.ts.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.js +100 -4
- package/lib/collaboration/testHelpers/documentGenerator.js.map +1 -1
- package/package.json +1 -1
- package/src/collaboration/applyOperation.test.ts +379 -12
- package/src/collaboration/applyOperation.ts +411 -76
- package/src/collaboration/index.ts +1 -0
- package/src/collaboration/operations.ts +35 -0
- package/src/collaboration/property.test.ts +24 -1
- package/src/collaboration/schemas.test.ts +136 -1
- package/src/collaboration/schemas.ts +167 -5
- package/src/collaboration/templateDocument.ts +27 -1
- package/src/collaboration/testHelpers/documentGenerator.ts +101 -5
|
@@ -7,6 +7,7 @@ import { canonicalJson, hashCanonical, reduceToMinimalForm } from './canonicaliz
|
|
|
7
7
|
import { findDocLawViolations } from './docLaws.js'
|
|
8
8
|
import {
|
|
9
9
|
findDuplicateBindingTargets,
|
|
10
|
+
findQuestionOwnershipViolations,
|
|
10
11
|
templateDocumentIsDefault,
|
|
11
12
|
validateTemplateDocument,
|
|
12
13
|
} from './schemas.js'
|
|
@@ -47,6 +48,23 @@ const runSequence = (seed: number, count: number) => {
|
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
describe('applyOperation determinism', () => {
|
|
51
|
+
it('exercises every ASMA-7676 operation in the seeded generator', () => {
|
|
52
|
+
const { generated } = runSequence(7676, 800)
|
|
53
|
+
const generatedTypes = new Set(generated.ops.map((op) => op.type))
|
|
54
|
+
|
|
55
|
+
for (const type of [
|
|
56
|
+
'gridColumn.create',
|
|
57
|
+
'gridColumn.move',
|
|
58
|
+
'gridColumn.setLayout',
|
|
59
|
+
'narrativeRule.set',
|
|
60
|
+
'narrativeRule.delete',
|
|
61
|
+
'qnrRule.set',
|
|
62
|
+
'qnrRule.delete',
|
|
63
|
+
] as const) {
|
|
64
|
+
assert.ok(generatedTypes.has(type), `generator did not exercise ${type}`)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
|
|
50
68
|
it('reproduces the identical document from the same seed', () => {
|
|
51
69
|
const first = runSequence(42, 200)
|
|
52
70
|
const second = runSequence(42, 200)
|
|
@@ -64,7 +82,10 @@ describe('applyOperation determinism', () => {
|
|
|
64
82
|
})
|
|
65
83
|
|
|
66
84
|
describe('DOC-LAW invariants under random op sequences', () => {
|
|
67
|
-
for
|
|
85
|
+
// Seed 4 is here for one reason: it is the smallest seed whose sequence deletes a grid that
|
|
86
|
+
// still owns columns. Without it the ownership assertion below never sees the cascade, which
|
|
87
|
+
// is the one path where a delete can strand questions in no order array at all.
|
|
88
|
+
for (const seed of [4, 7, 13, 99]) {
|
|
68
89
|
it(`holds for seed ${seed}`, () => {
|
|
69
90
|
const { doc } = runSequence(seed, 150)
|
|
70
91
|
|
|
@@ -84,6 +105,8 @@ describe('DOC-LAW invariants under random op sequences', () => {
|
|
|
84
105
|
assert.ok(validated.ok, validated.ok ? '' : validated.summary)
|
|
85
106
|
// …and the one-binding-per-target invariant is never violated by construction.
|
|
86
107
|
assert.deepEqual(findDuplicateBindingTargets(doc), [])
|
|
108
|
+
// Every accepted structural op preserves exactly one owner for each question.
|
|
109
|
+
assert.deepEqual(findQuestionOwnershipViolations(doc), [])
|
|
87
110
|
})
|
|
88
111
|
}
|
|
89
112
|
})
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
findAnswerSchemaDocLawViolations,
|
|
14
14
|
findDocLawDefaultViolations,
|
|
15
15
|
findDuplicateBindingTargets,
|
|
16
|
+
findQuestionOwnershipViolations,
|
|
16
17
|
findSchemaDocLawViolations,
|
|
17
18
|
findTemplateDocumentContractViolations,
|
|
18
19
|
findTemplateSchemaDocLawViolations,
|
|
@@ -72,7 +73,7 @@ describe('document schemas', () => {
|
|
|
72
73
|
'q-1': { type: 'QuestionGrid', grid: { columnIds: ['c-1'], singleRow: true } },
|
|
73
74
|
'c-1': { type: 'TextShort', required: true },
|
|
74
75
|
},
|
|
75
|
-
questionOrder: ['q-1'
|
|
76
|
+
questionOrder: ['q-1'],
|
|
76
77
|
gridRowOrderByQuestionId: { 'q-1': ['r-1'] },
|
|
77
78
|
gridRowsById: { 'r-1': { label: 'Row' } },
|
|
78
79
|
alternativesById: { 'a-1': { label: 'Ja' } },
|
|
@@ -98,6 +99,112 @@ describe('document schemas', () => {
|
|
|
98
99
|
})
|
|
99
100
|
})
|
|
100
101
|
|
|
102
|
+
describe('question ownership invariant', () => {
|
|
103
|
+
it('admits `grid` on a question grid alone', () => {
|
|
104
|
+
const withGrid = (type: string): QnrTemplateDocument =>
|
|
105
|
+
({
|
|
106
|
+
...emptyTemplateDocument('tpl-1'),
|
|
107
|
+
questionsById: { 'q-1': { type, grid: { columnIds: ['c-1'] } }, 'c-1': { type: 'TextShort' } },
|
|
108
|
+
questionOrder: ['q-1'],
|
|
109
|
+
}) as unknown as QnrTemplateDocument
|
|
110
|
+
|
|
111
|
+
assert.equal(validateTemplateDocument(withGrid('QuestionGrid')).ok, true)
|
|
112
|
+
// The per-type bag is open, so this is the one shape validation must NOT wave through:
|
|
113
|
+
// `columnIds` is ownership, and an ordinary question owning questions is unrepresentable.
|
|
114
|
+
assert.equal(validateTemplateDocument(withGrid('TextShort')).ok, false)
|
|
115
|
+
// The rest of the bag is untouched — an unknown per-type key is still an open extension.
|
|
116
|
+
assert.equal(
|
|
117
|
+
validateTemplateDocument({
|
|
118
|
+
...emptyTemplateDocument('tpl-1'),
|
|
119
|
+
questionsById: { 'q-1': { type: 'TextShort', dropdown: { multi: true } } },
|
|
120
|
+
questionOrder: ['q-1'],
|
|
121
|
+
} as unknown as QnrTemplateDocument).ok,
|
|
122
|
+
true,
|
|
123
|
+
)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('does not read a non-grid question as a column owner', () => {
|
|
127
|
+
// The publication gate refuses on any violation, so a stray `grid` on an ordinary
|
|
128
|
+
// question must not be able to *satisfy* ownership for the question it lists: that would
|
|
129
|
+
// hide the orphan instead of reporting it.
|
|
130
|
+
const doc = {
|
|
131
|
+
documentId: 'tpl-1',
|
|
132
|
+
revision: 0,
|
|
133
|
+
questionsById: {
|
|
134
|
+
'q-1': { type: 'TextShort', grid: { columnIds: ['c-1'] } },
|
|
135
|
+
'c-1': { type: 'TextShort' },
|
|
136
|
+
},
|
|
137
|
+
questionOrder: ['q-1'],
|
|
138
|
+
} as unknown as QnrTemplateDocument
|
|
139
|
+
|
|
140
|
+
assert.deepEqual(findQuestionOwnershipViolations(doc), [
|
|
141
|
+
{
|
|
142
|
+
law: 'QUESTION-OWNERSHIP',
|
|
143
|
+
kind: 'orphan',
|
|
144
|
+
questionId: 'c-1',
|
|
145
|
+
gridQuestionIds: [],
|
|
146
|
+
path: 'questionsById.c-1',
|
|
147
|
+
detail: 'question is neither top-level nor owned by a grid',
|
|
148
|
+
},
|
|
149
|
+
])
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
it('reports orphaned, top-level-and-column, multi-grid, and dangling column questions', () => {
|
|
153
|
+
const doc: QnrTemplateDocument = {
|
|
154
|
+
documentId: 'tpl-1',
|
|
155
|
+
revision: 0,
|
|
156
|
+
questionsById: {
|
|
157
|
+
'g-1': { type: 'QuestionGrid', grid: { columnIds: ['c-ordered', 'c-shared', 'c-missing'] } },
|
|
158
|
+
'g-2': { type: 'QuestionGrid', grid: { columnIds: ['c-shared'] } },
|
|
159
|
+
'c-ordered': { type: 'TextShort' },
|
|
160
|
+
'c-shared': { type: 'TextShort' },
|
|
161
|
+
'q-orphan': { type: 'TextShort' },
|
|
162
|
+
},
|
|
163
|
+
questionOrder: ['g-1', 'g-2', 'c-ordered'],
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
assert.deepEqual(findQuestionOwnershipViolations(doc), [
|
|
167
|
+
{
|
|
168
|
+
law: 'QUESTION-OWNERSHIP',
|
|
169
|
+
kind: 'dangling-column',
|
|
170
|
+
questionId: 'c-missing',
|
|
171
|
+
gridQuestionIds: ['g-1'],
|
|
172
|
+
path: 'questionsById.g-1.grid.columnIds',
|
|
173
|
+
detail: 'column question is absent from questionsById',
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
law: 'QUESTION-OWNERSHIP',
|
|
177
|
+
kind: 'ordered-and-column',
|
|
178
|
+
questionId: 'c-ordered',
|
|
179
|
+
gridQuestionIds: ['g-1'],
|
|
180
|
+
path: 'questionsById.c-ordered',
|
|
181
|
+
detail: 'question is both top-level and owned by a grid',
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
law: 'QUESTION-OWNERSHIP',
|
|
185
|
+
kind: 'two-grids',
|
|
186
|
+
questionId: 'c-shared',
|
|
187
|
+
gridQuestionIds: ['g-1', 'g-2'],
|
|
188
|
+
path: 'questionsById.c-shared',
|
|
189
|
+
detail: 'question is owned by more than one grid',
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
law: 'QUESTION-OWNERSHIP',
|
|
193
|
+
kind: 'orphan',
|
|
194
|
+
questionId: 'q-orphan',
|
|
195
|
+
gridQuestionIds: [],
|
|
196
|
+
path: 'questionsById.q-orphan',
|
|
197
|
+
detail: 'question is neither top-level nor owned by a grid',
|
|
198
|
+
},
|
|
199
|
+
])
|
|
200
|
+
assert.equal(
|
|
201
|
+
findTemplateDocumentContractViolations(doc).filter((violation) => violation.law === 'QUESTION-OWNERSHIP')
|
|
202
|
+
.length,
|
|
203
|
+
4,
|
|
204
|
+
)
|
|
205
|
+
})
|
|
206
|
+
})
|
|
207
|
+
|
|
101
208
|
describe('schema lint (DOC-LAW-1 / DOC-LAW-2, by construction)', () => {
|
|
102
209
|
it('finds no violations in the template document schema', () => {
|
|
103
210
|
assert.deepEqual(findTemplateSchemaDocLawViolations(), [])
|
|
@@ -213,6 +320,34 @@ describe('op schemas', () => {
|
|
|
213
320
|
assert.equal(moved instanceof type.errors, false)
|
|
214
321
|
})
|
|
215
322
|
|
|
323
|
+
it('accepts narrative rules and makes a qnr rule version reference unrepresentable', () => {
|
|
324
|
+
const narrative = templateOpSchema({
|
|
325
|
+
type: 'narrativeRule.set',
|
|
326
|
+
ruleId: 'nr-1',
|
|
327
|
+
questionId: 'q-1',
|
|
328
|
+
condition: { sourceQuestionId: 'q-2' },
|
|
329
|
+
})
|
|
330
|
+
const family = templateOpSchema({
|
|
331
|
+
type: 'qnrRule.set',
|
|
332
|
+
ruleId: 'qr-1',
|
|
333
|
+
questionId: 'q-1',
|
|
334
|
+
condition: { sourceQuestionId: 'q-1' },
|
|
335
|
+
templateFamilyId: 'family-1',
|
|
336
|
+
})
|
|
337
|
+
const pinnedVersion = templateOpSchema({
|
|
338
|
+
type: 'qnrRule.set',
|
|
339
|
+
ruleId: 'qr-1',
|
|
340
|
+
questionId: 'q-1',
|
|
341
|
+
condition: { sourceQuestionId: 'q-1' },
|
|
342
|
+
templateFamilyId: 'family-1',
|
|
343
|
+
templateVersion: 3,
|
|
344
|
+
})
|
|
345
|
+
|
|
346
|
+
assert.equal(narrative instanceof type.errors, false)
|
|
347
|
+
assert.equal(family instanceof type.errors, false)
|
|
348
|
+
assert.equal(pinnedVersion instanceof type.errors, true)
|
|
349
|
+
})
|
|
350
|
+
|
|
216
351
|
it('validates answer ops including the answer-side move', () => {
|
|
217
352
|
assert.equal(answerOpSchema({ type: 'gridRow.move', questionId: 'g-1', rowId: 'r-1', afterRowId: 'r-2' }) instanceof type.errors, false)
|
|
218
353
|
assert.equal(answerOpSchema({ type: 'gridRow.move', questionId: 'g-1', rowId: 'r-1' }) instanceof type.errors, true)
|
|
@@ -70,6 +70,17 @@ const highlightRuleSchema = type({
|
|
|
70
70
|
...({ '[string]': 'unknown' } as const),
|
|
71
71
|
})
|
|
72
72
|
|
|
73
|
+
const narrativeRuleSchema = type({
|
|
74
|
+
condition: ruleConditionSchema,
|
|
75
|
+
...({ '[string]': 'unknown' } as const),
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const qnrRuleSchema = type({
|
|
79
|
+
condition: ruleConditionSchema,
|
|
80
|
+
templateFamilyId: 'string',
|
|
81
|
+
'+': 'reject',
|
|
82
|
+
})
|
|
83
|
+
|
|
73
84
|
const questionGridConfigSchema = type({
|
|
74
85
|
columnIds: 'string[]?',
|
|
75
86
|
minRows: 'number?',
|
|
@@ -82,22 +93,55 @@ const questionGridConfigSchema = type({
|
|
|
82
93
|
rowTitle: 'string?',
|
|
83
94
|
})
|
|
84
95
|
|
|
96
|
+
const layoutPlacementSchema = type({
|
|
97
|
+
row: 'number',
|
|
98
|
+
cell: 'number',
|
|
99
|
+
keepCellSize: 'boolean?',
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
const questionGridRowEditorSchema = type({
|
|
103
|
+
"layoutByQuestionId?": recordOf(layoutPlacementSchema),
|
|
104
|
+
...({ '[string]': 'unknown' } as const),
|
|
105
|
+
})
|
|
106
|
+
|
|
85
107
|
const questionGridPresentationSchema = type({
|
|
86
108
|
headerTabId: 'string?',
|
|
109
|
+
"rowEditor?": questionGridRowEditorSchema,
|
|
87
110
|
defaultColumnWidthsByQuestionId: 'Record<string, number>?',
|
|
88
111
|
...({ '[string]': 'unknown' } as const),
|
|
89
112
|
})
|
|
90
113
|
|
|
91
|
-
|
|
92
|
-
|
|
114
|
+
/** Every question field except the discriminator and the grid-only `grid` bag. */
|
|
115
|
+
const qnrQuestionCommonFields = {
|
|
93
116
|
"subtype?": type.enumerated('ORDINARY', 'EMAIL'),
|
|
94
117
|
"label?": 'string',
|
|
95
118
|
"required?": 'boolean',
|
|
96
119
|
"defaultValue?": docScalar,
|
|
97
|
-
"grid?": questionGridConfigSchema,
|
|
98
120
|
"presentation?": questionGridPresentationSchema,
|
|
99
121
|
...({ '[string]': 'unknown' } as const),
|
|
100
|
-
}
|
|
122
|
+
} as const
|
|
123
|
+
|
|
124
|
+
const NON_GRID_QUESTION_TYPES = QUESTION_TYPES.filter((questionType) => questionType !== 'QuestionGrid')
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A question, discriminated on `type` for one reason: `grid` carries `columnIds`, the array a
|
|
128
|
+
* question's structural owner is read from, and only a `QuestionGrid` may own columns. The
|
|
129
|
+
* per-type bag being open (`[string]: unknown`) means omitting the key from the non-grid branch
|
|
130
|
+
* would admit it anyway, so that branch types it `never` — the schema itself then refuses a
|
|
131
|
+
* document where an ordinary question claims to own questions.
|
|
132
|
+
*/
|
|
133
|
+
const qnrQuestionSchema = type.or(
|
|
134
|
+
type({
|
|
135
|
+
type: '"QuestionGrid"',
|
|
136
|
+
"grid?": questionGridConfigSchema,
|
|
137
|
+
...qnrQuestionCommonFields,
|
|
138
|
+
}),
|
|
139
|
+
type({
|
|
140
|
+
type: type.enumerated(...NON_GRID_QUESTION_TYPES),
|
|
141
|
+
"grid?": 'never',
|
|
142
|
+
...qnrQuestionCommonFields,
|
|
143
|
+
}),
|
|
144
|
+
)
|
|
101
145
|
|
|
102
146
|
const qnrAlternativeSchema = type({
|
|
103
147
|
"label?": 'string',
|
|
@@ -199,6 +243,10 @@ export const qnrTemplateDocumentSchema = type({
|
|
|
199
243
|
visibilityRuleOrderByQuestionId: 'Record<string, string[]>?',
|
|
200
244
|
"highlightRulesById?": recordOf(highlightRuleSchema),
|
|
201
245
|
highlightRuleOrderByQuestionId: 'Record<string, string[]>?',
|
|
246
|
+
"narrativeRulesById?": recordOf(narrativeRuleSchema),
|
|
247
|
+
narrativeRuleOrderByQuestionId: 'Record<string, string[]>?',
|
|
248
|
+
"qnrRulesById?": recordOf(qnrRuleSchema),
|
|
249
|
+
qnrRuleOrderByQuestionId: 'Record<string, string[]>?',
|
|
202
250
|
"dataMappingsById?": recordOf(dataMappingSchema),
|
|
203
251
|
"mappingNodesById?": recordOf(mappingNodeSchema),
|
|
204
252
|
"mappingBindingsById?": recordOf(mappingBindingSchema),
|
|
@@ -236,6 +284,20 @@ export const templateOpSchema = type.or(
|
|
|
236
284
|
type({ type: '"question.updateField"', questionId: 'string', field: 'string', value: opValue }),
|
|
237
285
|
type({ type: '"question.move"', questionId: 'string', toIndex: 'number' }),
|
|
238
286
|
type({ type: '"question.delete"', questionId: 'string' }),
|
|
287
|
+
type({
|
|
288
|
+
type: '"gridColumn.create"',
|
|
289
|
+
questionId: 'string',
|
|
290
|
+
columnQuestionId: 'string',
|
|
291
|
+
questionType: type.enumerated(...QUESTION_TYPES),
|
|
292
|
+
atIndex: 'number?',
|
|
293
|
+
}),
|
|
294
|
+
type({ type: '"gridColumn.move"', questionId: 'string', columnQuestionId: 'string', toIndex: 'number' }),
|
|
295
|
+
type({
|
|
296
|
+
type: '"gridColumn.setLayout"',
|
|
297
|
+
questionId: 'string',
|
|
298
|
+
columnQuestionId: 'string',
|
|
299
|
+
placement: type.or(layoutPlacementSchema, type('null')),
|
|
300
|
+
}),
|
|
239
301
|
type({ type: '"gridRow.create"', questionId: 'string', rowId: 'string', label: 'string?', atIndex: 'number?' }),
|
|
240
302
|
type({ type: '"gridRow.move"', questionId: 'string', rowId: 'string', afterRowId: 'string | null' }),
|
|
241
303
|
type({ type: '"gridRow.delete"', questionId: 'string', rowId: 'string' }),
|
|
@@ -313,6 +375,17 @@ export const templateOpSchema = type.or(
|
|
|
313
375
|
type({ type: '"visibilityRule.delete"', ruleId: 'string' }),
|
|
314
376
|
type({ type: '"highlightRule.set"', ruleId: 'string', questionId: 'string', condition: ruleConditionSchema }),
|
|
315
377
|
type({ type: '"highlightRule.delete"', ruleId: 'string' }),
|
|
378
|
+
type({ type: '"narrativeRule.set"', ruleId: 'string', questionId: 'string', condition: ruleConditionSchema }),
|
|
379
|
+
type({ type: '"narrativeRule.delete"', ruleId: 'string' }),
|
|
380
|
+
type({
|
|
381
|
+
type: '"qnrRule.set"',
|
|
382
|
+
ruleId: 'string',
|
|
383
|
+
questionId: 'string',
|
|
384
|
+
condition: ruleConditionSchema,
|
|
385
|
+
templateFamilyId: 'string',
|
|
386
|
+
'+': 'reject',
|
|
387
|
+
}),
|
|
388
|
+
type({ type: '"qnrRule.delete"', ruleId: 'string' }),
|
|
316
389
|
)
|
|
317
390
|
|
|
318
391
|
export const answerOpSchema = type.or(
|
|
@@ -555,6 +628,90 @@ export const findDuplicateBindingTargets = (
|
|
|
555
628
|
return [...byTarget.values()].filter((entry) => entry.bindingIds.length > 1)
|
|
556
629
|
}
|
|
557
630
|
|
|
631
|
+
// ─────────────────────────────── question ownership ───────────────────────────────
|
|
632
|
+
|
|
633
|
+
export type QuestionOwnershipViolation = {
|
|
634
|
+
law: 'QUESTION-OWNERSHIP'
|
|
635
|
+
kind: 'orphan' | 'ordered-and-column' | 'two-grids' | 'dangling-column'
|
|
636
|
+
questionId: string
|
|
637
|
+
gridQuestionIds: string[]
|
|
638
|
+
path: string
|
|
639
|
+
detail: string
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* A question has exactly one structural owner: either `questionOrder` or one grid's
|
|
644
|
+
* `columnIds`. This validator covers imported/directly-written documents in addition to
|
|
645
|
+
* the reducer, and reports absent column records as dangling ownership references.
|
|
646
|
+
*/
|
|
647
|
+
export const findQuestionOwnershipViolations = (
|
|
648
|
+
document: QnrTemplateDocument,
|
|
649
|
+
): QuestionOwnershipViolation[] => {
|
|
650
|
+
const ownersByQuestionId = new Map<string, string[]>()
|
|
651
|
+
for (const [gridQuestionId, question] of Object.entries(document.questionsById ?? {})) {
|
|
652
|
+
// Only a grid owns columns. `grid` on any other type is a shape the document schema
|
|
653
|
+
// refuses outright, and reading it as ownership here would be worse than ignoring it:
|
|
654
|
+
// a stray key would adopt a top-level question, or cover for a real orphan by naming
|
|
655
|
+
// an owner that owns nothing.
|
|
656
|
+
if (question.type !== 'QuestionGrid') continue
|
|
657
|
+
for (const columnQuestionId of question.grid?.columnIds ?? []) {
|
|
658
|
+
const owners = ownersByQuestionId.get(columnQuestionId) ?? []
|
|
659
|
+
if (!owners.includes(gridQuestionId)) owners.push(gridQuestionId)
|
|
660
|
+
ownersByQuestionId.set(columnQuestionId, owners)
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const violations: QuestionOwnershipViolation[] = []
|
|
665
|
+
for (const [questionId, gridQuestionIds] of ownersByQuestionId) {
|
|
666
|
+
if (document.questionsById?.[questionId] !== undefined) continue
|
|
667
|
+
violations.push({
|
|
668
|
+
law: 'QUESTION-OWNERSHIP',
|
|
669
|
+
kind: 'dangling-column',
|
|
670
|
+
questionId,
|
|
671
|
+
gridQuestionIds,
|
|
672
|
+
path: `questionsById.${gridQuestionIds[0]}.grid.columnIds`,
|
|
673
|
+
detail: 'column question is absent from questionsById',
|
|
674
|
+
})
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
const topLevel = new Set(document.questionOrder)
|
|
678
|
+
for (const questionId of Object.keys(document.questionsById ?? {})) {
|
|
679
|
+
const gridQuestionIds = ownersByQuestionId.get(questionId) ?? []
|
|
680
|
+
const path = `questionsById.${questionId}`
|
|
681
|
+
if (!topLevel.has(questionId) && gridQuestionIds.length === 0) {
|
|
682
|
+
violations.push({
|
|
683
|
+
law: 'QUESTION-OWNERSHIP',
|
|
684
|
+
kind: 'orphan',
|
|
685
|
+
questionId,
|
|
686
|
+
gridQuestionIds,
|
|
687
|
+
path,
|
|
688
|
+
detail: 'question is neither top-level nor owned by a grid',
|
|
689
|
+
})
|
|
690
|
+
}
|
|
691
|
+
if (topLevel.has(questionId) && gridQuestionIds.length > 0) {
|
|
692
|
+
violations.push({
|
|
693
|
+
law: 'QUESTION-OWNERSHIP',
|
|
694
|
+
kind: 'ordered-and-column',
|
|
695
|
+
questionId,
|
|
696
|
+
gridQuestionIds,
|
|
697
|
+
path,
|
|
698
|
+
detail: 'question is both top-level and owned by a grid',
|
|
699
|
+
})
|
|
700
|
+
}
|
|
701
|
+
if (gridQuestionIds.length > 1) {
|
|
702
|
+
violations.push({
|
|
703
|
+
law: 'QUESTION-OWNERSHIP',
|
|
704
|
+
kind: 'two-grids',
|
|
705
|
+
questionId,
|
|
706
|
+
gridQuestionIds,
|
|
707
|
+
path,
|
|
708
|
+
detail: 'question is owned by more than one grid',
|
|
709
|
+
})
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
return violations
|
|
713
|
+
}
|
|
714
|
+
|
|
558
715
|
// ─────────────────────────────── typed validators ───────────────────────────────
|
|
559
716
|
|
|
560
717
|
export type Validation<T> = { ok: true; value: T } | { ok: false; summary: string }
|
|
@@ -573,9 +730,14 @@ export const validateTemplateDocument = validate<QnrTemplateDocument>(qnrTemplat
|
|
|
573
730
|
* Full template-document admission check for the contract test: schema + DOC-LAW-1/2 lints +
|
|
574
731
|
* default lint + binding uniqueness. A document passes only in its canonical minimal form.
|
|
575
732
|
*/
|
|
576
|
-
export
|
|
733
|
+
export type TemplateDocumentContractViolation = DocLawViolation | QuestionOwnershipViolation
|
|
734
|
+
|
|
735
|
+
export const findTemplateDocumentContractViolations = (
|
|
736
|
+
document: QnrTemplateDocument,
|
|
737
|
+
): TemplateDocumentContractViolation[] => [
|
|
577
738
|
...findDocLawViolations(document),
|
|
578
739
|
...findDocLawDefaultViolations(document),
|
|
740
|
+
...findQuestionOwnershipViolations(document),
|
|
579
741
|
]
|
|
580
742
|
|
|
581
743
|
export const findDuplicateBindingTargetsSummary = (document: QnrTemplateDocument): string[] =>
|
|
@@ -25,6 +25,8 @@ export type FilterId = string
|
|
|
25
25
|
export type MappingId = string
|
|
26
26
|
export type VisibilityRuleId = string
|
|
27
27
|
export type HighlightRuleId = string
|
|
28
|
+
export type NarrativeRuleId = string
|
|
29
|
+
export type QnrRuleId = string
|
|
28
30
|
|
|
29
31
|
/** A scalar a document may carry. Deliberately not `any`: DOC-LAW-2 bans `null`. */
|
|
30
32
|
export type DocScalar = string | number | boolean
|
|
@@ -110,10 +112,24 @@ export type QuestionGridConfig = {
|
|
|
110
112
|
/**
|
|
111
113
|
* Grid-owned presentation (OQ-V2-17/25): row-editor layout, filters, grid actions,
|
|
112
114
|
* highlight rules, the header-tab reference and authored default column widths live
|
|
113
|
-
* here — there is no parallel
|
|
115
|
+
* here — there is no parallel top-level layout map; `rowEditor` owns it.
|
|
114
116
|
*/
|
|
117
|
+
/** One atomic row-editor placement for a grid-owned column. */
|
|
118
|
+
export type LayoutPlacement = {
|
|
119
|
+
row: number
|
|
120
|
+
cell: number
|
|
121
|
+
keepCellSize?: boolean
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Row-editor state belongs to the owning grid, keyed by its column question ids. */
|
|
125
|
+
export type QuestionGridRowEditor = {
|
|
126
|
+
layoutByQuestionId?: Record<QuestionId, LayoutPlacement>
|
|
127
|
+
[key: string]: unknown
|
|
128
|
+
}
|
|
129
|
+
|
|
115
130
|
export type QuestionGridPresentation = {
|
|
116
131
|
headerTabId?: TabId
|
|
132
|
+
rowEditor?: QuestionGridRowEditor
|
|
117
133
|
/** OQ-V2-25: authored defaults; per-user widths are preference state outside both documents. */
|
|
118
134
|
defaultColumnWidthsByQuestionId?: Record<QuestionId, number>
|
|
119
135
|
[key: string]: unknown
|
|
@@ -166,6 +182,12 @@ export type RuleCondition = {
|
|
|
166
182
|
|
|
167
183
|
export type VisibilityRule = { condition: RuleCondition; [key: string]: unknown }
|
|
168
184
|
export type HighlightRule = { condition: RuleCondition; [key: string]: unknown }
|
|
185
|
+
export type NarrativeRule = { condition: RuleCondition; [key: string]: unknown }
|
|
186
|
+
export type QnrRule = {
|
|
187
|
+
condition: RuleCondition
|
|
188
|
+
/** A `qnr_templates.id` family reference; pinned versions are intentionally unrepresentable. */
|
|
189
|
+
templateFamilyId: string
|
|
190
|
+
}
|
|
169
191
|
|
|
170
192
|
/** One traversal step. Many questions off one entity share a single node (§2.2a). */
|
|
171
193
|
export type MappingNode = {
|
|
@@ -235,6 +257,10 @@ export type QnrTemplateDocument = {
|
|
|
235
257
|
visibilityRuleOrderByQuestionId?: Record<QuestionId, VisibilityRuleId[]>
|
|
236
258
|
highlightRulesById?: Record<HighlightRuleId, HighlightRule>
|
|
237
259
|
highlightRuleOrderByQuestionId?: Record<QuestionId, HighlightRuleId[]>
|
|
260
|
+
narrativeRulesById?: Record<NarrativeRuleId, NarrativeRule>
|
|
261
|
+
narrativeRuleOrderByQuestionId?: Record<QuestionId, NarrativeRuleId[]>
|
|
262
|
+
qnrRulesById?: Record<QnrRuleId, QnrRule>
|
|
263
|
+
qnrRuleOrderByQuestionId?: Record<QuestionId, QnrRuleId[]>
|
|
238
264
|
dataMappingsById?: Record<MappingId, QnrDataMapping>
|
|
239
265
|
mappingNodesById?: Record<NodeId, MappingNode>
|
|
240
266
|
mappingBindingsById?: Record<BindingId, MappingBinding>
|