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.
Files changed (50) hide show
  1. package/lib/collaboration/applyOperation.d.ts.map +1 -1
  2. package/lib/collaboration/applyOperation.js +212 -0
  3. package/lib/collaboration/applyOperation.js.map +1 -1
  4. package/lib/collaboration/index.d.ts +1 -0
  5. package/lib/collaboration/index.d.ts.map +1 -1
  6. package/lib/collaboration/index.js +1 -0
  7. package/lib/collaboration/index.js.map +1 -1
  8. package/lib/collaboration/narrativePresence.d.ts +12 -0
  9. package/lib/collaboration/narrativePresence.d.ts.map +1 -0
  10. package/lib/collaboration/narrativePresence.js +34 -0
  11. package/lib/collaboration/narrativePresence.js.map +1 -0
  12. package/lib/collaboration/operations.d.ts +53 -1
  13. package/lib/collaboration/operations.d.ts.map +1 -1
  14. package/lib/collaboration/operations.js +4 -0
  15. package/lib/collaboration/operations.js.map +1 -1
  16. package/lib/collaboration/schemas.d.ts +92 -1
  17. package/lib/collaboration/schemas.d.ts.map +1 -1
  18. package/lib/collaboration/schemas.js +49 -2
  19. package/lib/collaboration/schemas.js.map +1 -1
  20. package/lib/collaboration/templateAuthoringMeta.d.ts +4 -4
  21. package/lib/collaboration/templateAuthoringMeta.d.ts.map +1 -1
  22. package/lib/collaboration/templateAuthoringMeta.js +103 -5
  23. package/lib/collaboration/templateAuthoringMeta.js.map +1 -1
  24. package/lib/collaboration/templateDocument.d.ts +22 -0
  25. package/lib/collaboration/templateDocument.d.ts.map +1 -1
  26. package/lib/collaboration/templateDocument.js.map +1 -1
  27. package/lib/collaboration/typeContracts.d.ts +30 -1
  28. package/lib/collaboration/typeContracts.d.ts.map +1 -1
  29. package/lib/g-definitions.d.ts +5 -0
  30. package/lib/g-definitions.d.ts.map +1 -1
  31. package/lib/g-definitions.js +6 -0
  32. package/lib/g-definitions.js.map +1 -1
  33. package/lib/overlayBack.d.ts +103 -0
  34. package/lib/overlayBack.d.ts.map +1 -0
  35. package/lib/overlayBack.js +540 -0
  36. package/lib/overlayBack.js.map +1 -0
  37. package/package.json +2 -1
  38. package/src/collaboration/applyOperation.ts +230 -0
  39. package/src/collaboration/index.ts +1 -0
  40. package/src/collaboration/narrativePresence.test.ts +328 -0
  41. package/src/collaboration/narrativePresence.ts +42 -0
  42. package/src/collaboration/operations.ts +25 -0
  43. package/src/collaboration/ruleAuthoring.test.ts +285 -0
  44. package/src/collaboration/schemas.ts +65 -2
  45. package/src/collaboration/templateAuthoringMeta.ts +107 -6
  46. package/src/collaboration/templateDocument.ts +23 -0
  47. package/src/collaboration/typeContracts.ts +17 -0
  48. package/src/g-definitions.ts +13 -0
  49. package/src/overlayBack.test.ts +742 -0
  50. package/src/overlayBack.ts +647 -0
@@ -1 +1 @@
1
- {"version":3,"file":"applyOperation.d.ts","sourceRoot":"","sources":["../../src/collaboration/applyOperation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,KAAK,EAKR,eAAe,EAYf,mBAAmB,EAGtB,MAAM,uBAAuB,CAAA;AAsC9B;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;;;GAMG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IACzC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IAErB,YAAY,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAK5D;CACJ;AAED,qBAAa,qBAAsB,SAAQ,kBAAkB;IACzD,YAAY,IAAI,EAAE,MAAM,EAEvB;CACJ;AAED,qBAAa,sBAAuB,SAAQ,kBAAkB;IAC1D,YAAY,OAAO,EAAE,MAAM,EAE1B;CACJ;AAmUD;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,UAAW,MAAM,KAAG,MAAM,GAAG,SAQ5D,CAAA;AA6ID;;;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M,UAAU,KAAG,mBAK9E,CAAA"}
@@ -1,3 +1,4 @@
1
+ import { findNarrativePresenceViolations, hasSingleNarrativeQuestionOwner } from './narrativePresence.js';
1
2
  import { isCompatibilityCollection, isMetaField, isSettingField, isTemplateAuthoringDefault, metaFieldPath, normalizeCompatibilityId, normalizeCompatibilityIds, parseCompatibilityOperationId, parseMetaFieldValue, settingFieldPath, templateAliasPathsOf, } from './templateAuthoringMeta.js';
2
3
  import { ACTION_TYPES, BINDING_OPTION_DEFAULTS, MAPPING_FILTER_OPERATORS, bindingTargetKey, isKnownActionKind, makeExpressionAlternativeToken, makeExpressionTargetToken, parseLegacyBindingOverride, } from './templateDocument.js';
3
4
  /** Runtime narrowing for the two closed vocabularies the reducer must re-check on replay. */
@@ -901,6 +902,7 @@ const OPTIONAL_COLLECTIONS = [
901
902
  'prefillRulesById',
902
903
  'prefillRuleOrderByQuestionId',
903
904
  'highlightRuleSettingsByQuestionId',
905
+ 'narrativeRuleSettingsByQuestionId',
904
906
  'dataMappingsById',
905
907
  'mappingNodesById',
906
908
  'mappingBindingsById',
@@ -1236,6 +1238,7 @@ const deleteQuestion = (doc, questionId) => {
1236
1238
  // The settings ride with the question they configure; an entry no question reaches is
1237
1239
  // unreachable state that still changes `document_hash`.
1238
1240
  highlightRuleSettingsByQuestionId: doc.highlightRuleSettingsByQuestionId && omitKey(doc.highlightRuleSettingsByQuestionId, questionId),
1241
+ narrativeRuleSettingsByQuestionId: doc.narrativeRuleSettingsByQuestionId && omitKey(doc.narrativeRuleSettingsByQuestionId, questionId),
1239
1242
  });
1240
1243
  // A binding pointing at a deleted question is an unresolvable reference the
1241
1244
  // compiler would reject at publication; drop it with its target. A grid column
@@ -1302,8 +1305,209 @@ const scrubColumnPresentationRefs = (doc, questionId) => {
1302
1305
  }));
1303
1306
  return changed ? { ...doc, questionsById: nextQuestions } : doc;
1304
1307
  };
1308
+ /** New operations are checked on direct replay as well as at the wire boundary. */
1309
+ const assertRuleEditShape = (op, keys) => {
1310
+ if (Object.keys(op).some((key) => !keys.includes(key))) {
1311
+ throw new OperationConflictError(`Operation ${op.type} carries an undeclared member`);
1312
+ }
1313
+ };
1314
+ const requireRuleQuestion = (doc, questionId) => {
1315
+ if (typeof questionId !== 'string' || questionId === '' || !Object.hasOwn(doc.questionsById ?? {}, questionId)) {
1316
+ throw new OperationConflictError(`Unknown question "${questionId}"`);
1317
+ }
1318
+ };
1319
+ /** Fieldwise true-only settings. Neither a rule nor the other setting is touched. */
1320
+ const setRuleSetting = (doc, op, root) => {
1321
+ assertRuleEditShape(op, ['type', 'questionId', 'field', 'value']);
1322
+ requireRuleQuestion(doc, op.questionId);
1323
+ if ((op.field !== 'enabled' && op.field !== 'requiredAll') || typeof op.value !== 'boolean') {
1324
+ throw new OperationConflictError(`Operation ${op.type} requires a known field and boolean value`);
1325
+ }
1326
+ const settings = { ...(doc[root]?.[op.questionId] ?? {}) };
1327
+ if (op.value)
1328
+ settings[op.field] = true;
1329
+ else
1330
+ delete settings[op.field];
1331
+ const byQuestion = omitKey(doc[root] ?? {}, op.questionId);
1332
+ return {
1333
+ ...doc,
1334
+ [root]: Object.keys(settings).length === 0 ? byQuestion : { ...byQuestion, [op.questionId]: settings },
1335
+ };
1336
+ };
1337
+ /** A new condition edit must resolve its source and every alternative to that source's ownership. */
1338
+ const requireRuleCondition = (doc, value) => {
1339
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
1340
+ throw new OperationConflictError('A highlight condition must be an object');
1341
+ }
1342
+ const condition = value;
1343
+ requireRuleQuestion(doc, condition.sourceQuestionId);
1344
+ if ((condition.operator !== undefined && typeof condition.operator !== 'string') ||
1345
+ (condition.value !== undefined && (!isDocScalarValue(condition.value) ||
1346
+ (typeof condition.value === 'number' && !Number.isFinite(condition.value)))) ||
1347
+ (condition.alternativeId !== undefined && typeof condition.alternativeId !== 'string') ||
1348
+ (condition.alternativeIds !== undefined && (!Array.isArray(condition.alternativeIds) ||
1349
+ condition.alternativeIds.some((id) => typeof id !== 'string')))) {
1350
+ throw new OperationConflictError('A highlight condition carries a malformed member');
1351
+ }
1352
+ const alternatives = [
1353
+ ...(condition.alternativeId === undefined ? [] : [condition.alternativeId]),
1354
+ ...(condition.alternativeIds ?? []),
1355
+ ];
1356
+ for (const id of alternatives) {
1357
+ if (!Object.hasOwn(doc.alternativesById ?? {}, id) ||
1358
+ !doc.alternativeOrderByQuestionId?.[condition.sourceQuestionId]?.includes(id)) {
1359
+ throw new OperationConflictError(`Alternative "${id}" is not owned by question "${condition.sourceQuestionId}"`);
1360
+ }
1361
+ }
1362
+ };
1363
+ /** A field edit requires the existing owner; unlike the released setter it never moves/replaces. */
1364
+ const setHighlightField = (doc, op) => {
1365
+ assertRuleEditShape(op, ['type', 'ruleId', 'questionId', 'field', 'value']);
1366
+ requireRuleQuestion(doc, op.questionId);
1367
+ const owners = Object.entries(doc.highlightRuleOrderByQuestionId ?? {})
1368
+ .filter(([, ids]) => ids.includes(op.ruleId)).map(([id]) => id);
1369
+ const existing = doc.highlightRulesById?.[op.ruleId];
1370
+ if (typeof op.ruleId !== 'string' || op.ruleId === '' || !existing ||
1371
+ !Object.hasOwn(doc.highlightRulesById ?? {}, op.ruleId) || owners.length !== 1 || owners[0] !== op.questionId) {
1372
+ throw new OperationConflictError(`Highlight rule "${op.ruleId}" is not owned by question "${op.questionId}"`);
1373
+ }
1374
+ const rule = { ...existing };
1375
+ switch (op.field) {
1376
+ case 'condition':
1377
+ requireRuleCondition(doc, op.value);
1378
+ rule.condition = op.value;
1379
+ break;
1380
+ case 'state':
1381
+ if (op.value !== null && (typeof op.value !== 'number' || !Number.isFinite(op.value))) {
1382
+ throw new OperationConflictError('Highlight state must be a finite number or null');
1383
+ }
1384
+ if (op.value === null)
1385
+ delete rule.state;
1386
+ else
1387
+ rule.state = op.value;
1388
+ break;
1389
+ case 'highlight':
1390
+ case 'showLink':
1391
+ if (op.value !== null && typeof op.value !== 'boolean') {
1392
+ throw new OperationConflictError('Highlight output must be a boolean or null');
1393
+ }
1394
+ if (op.value === null)
1395
+ delete rule[op.field];
1396
+ else
1397
+ rule[op.field] = op.value;
1398
+ break;
1399
+ default:
1400
+ throw new OperationConflictError('Unknown highlight field');
1401
+ }
1402
+ return { ...doc, highlightRulesById: { ...doc.highlightRulesById, [op.ruleId]: rule } };
1403
+ };
1404
+ const requireNarrativePresenceState = (document) => {
1405
+ const violations = findNarrativePresenceViolations(document);
1406
+ if (violations.length !== 0)
1407
+ throw new OperationConflictError(violations.map((v) => `${v.path}: ${v.detail}`).join('; '));
1408
+ };
1409
+ /** Strict NEW-arm-only ownership. Released replacing/moving setters keep their old replay. */
1410
+ const requireNarrativeQuestion = (doc, questionId) => {
1411
+ requireRuleQuestion(doc, questionId);
1412
+ if (!hasSingleNarrativeQuestionOwner(doc, questionId)) {
1413
+ throw new OperationConflictError(`Question "${questionId}" must have exactly one structural owner`);
1414
+ }
1415
+ };
1416
+ const editNarrativeCollection = (doc, op) => {
1417
+ assertRuleEditShape(op, ['type', 'questionId', 'expectedPresence', 'intent', 'action']);
1418
+ requireNarrativeQuestion(doc, op.questionId);
1419
+ const presence = doc.narrativeRuleSettingsByQuestionId?.[op.questionId]?.conditionalPresence ?? 'array';
1420
+ if (presence !== op.expectedPresence)
1421
+ throw new OperationConflictError('Stale narrative expectedPresence');
1422
+ if (!['array', 'absent', 'null'].includes(op.expectedPresence) ||
1423
+ op.intent !== (presence === 'null' ? 'repair-null' : 'edit')) {
1424
+ throw new OperationConflictError('Narrative edit requires the matching presence and intent');
1425
+ }
1426
+ const action = op.action;
1427
+ if (action === null || typeof action !== 'object' || Array.isArray(action) ||
1428
+ !['set', 'delete', 'materialize'].includes(action.kind)) {
1429
+ throw new OperationConflictError('Unknown narrative collection action');
1430
+ }
1431
+ const allowed = action.kind === 'set' ? ['kind', 'ruleId', 'condition'] :
1432
+ action.kind === 'delete' ? ['kind', 'ruleId'] : ['kind'];
1433
+ if (Object.keys(action).some((key) => !allowed.includes(key)) || allowed.some((key) => !Object.hasOwn(action, key))) {
1434
+ throw new OperationConflictError('Narrative action carries an undeclared member');
1435
+ }
1436
+ const settingsEntry = doc.narrativeRuleSettingsByQuestionId?.[op.questionId];
1437
+ if (settingsEntry !== undefined && (settingsEntry === null || typeof settingsEntry !== 'object' ||
1438
+ Array.isArray(settingsEntry) || Object.keys(settingsEntry).some((key) => key !== 'conditionalPresence' && !((key === 'enabled' || key === 'requiredAll') && settingsEntry[key] === true)))) {
1439
+ throw new OperationConflictError('Narrative target settings are malformed');
1440
+ }
1441
+ if (Object.values(doc.narrativeRuleOrderByQuestionId ?? {}).some((ids) => !Array.isArray(ids) ||
1442
+ ids.some((id) => typeof id !== 'string' || id === ''))) {
1443
+ throw new OperationConflictError('Narrative ownership orders are malformed');
1444
+ }
1445
+ const order = doc.narrativeRuleOrderByQuestionId?.[op.questionId] ?? [];
1446
+ if (new Set(order).size !== order.length || order.some((id) => typeof id !== 'string' || id === '' || !Object.hasOwn(doc.narrativeRulesById ?? {}, id))) {
1447
+ throw new OperationConflictError('Narrative target order is malformed');
1448
+ }
1449
+ for (const id of order) {
1450
+ const rule = doc.narrativeRulesById?.[id];
1451
+ const owners = Object.entries(doc.narrativeRuleOrderByQuestionId ?? {})
1452
+ .flatMap(([owner, ids]) => ids.filter((entry) => entry === id).map(() => owner));
1453
+ if (rule === null || typeof rule !== 'object' || Array.isArray(rule) ||
1454
+ !Object.hasOwn(rule, 'condition') || owners.length !== 1 || owners[0] !== op.questionId) {
1455
+ throw new OperationConflictError('Narrative target order must contain exclusively owned rule records');
1456
+ }
1457
+ }
1458
+ let next = doc;
1459
+ if (action.kind === 'materialize') {
1460
+ if (order.length !== 0)
1461
+ throw new OperationConflictError('Materialize requires an empty narrative collection');
1462
+ }
1463
+ else {
1464
+ if (typeof action.ruleId !== 'string' || action.ruleId === '') {
1465
+ throw new OperationConflictError('Narrative edit requires a nonempty ruleId');
1466
+ }
1467
+ const owners = Object.entries(doc.narrativeRuleOrderByQuestionId ?? {})
1468
+ .flatMap(([owner, ids]) => ids.filter((id) => id === action.ruleId).map(() => owner));
1469
+ const exists = Object.hasOwn(doc.narrativeRulesById ?? {}, action.ruleId);
1470
+ if ((exists || owners.length !== 0) && (!exists || owners.length !== 1 || owners[0] !== op.questionId)) {
1471
+ throw new OperationConflictError('Narrative rule must belong only to its target');
1472
+ }
1473
+ if (action.kind === 'delete') {
1474
+ if (presence !== 'array' || !exists)
1475
+ throw new OperationConflictError('Delete requires an existing ARRAY rule');
1476
+ const { rulesById, orderByQuestionId } = deleteScopedRule(doc.narrativeRulesById, doc.narrativeRuleOrderByQuestionId, action.ruleId);
1477
+ next = { ...doc, narrativeRulesById: rulesById, narrativeRuleOrderByQuestionId: orderByQuestionId };
1478
+ }
1479
+ else {
1480
+ requireRuleCondition(doc, action.condition);
1481
+ if (!Object.hasOwn(action.condition, 'sourceQuestionId') ||
1482
+ ['operator', 'value', 'alternativeId', 'alternativeIds'].some((key) => Object.hasOwn(action.condition, key) && action.condition[key] === undefined)) {
1483
+ throw new OperationConflictError('Narrative condition carries a missing or undefined member');
1484
+ }
1485
+ requireNarrativeQuestion(doc, action.condition.sourceQuestionId);
1486
+ const alternatives = [...(action.condition.alternativeId === undefined ? [] : [action.condition.alternativeId]),
1487
+ ...(action.condition.alternativeIds ?? [])];
1488
+ if (Object.values(doc.alternativeOrderByQuestionId ?? {}).some((ids) => !Array.isArray(ids))) {
1489
+ throw new OperationConflictError('Alternative ownership orders are malformed');
1490
+ }
1491
+ for (const id of alternatives) {
1492
+ const owners = Object.entries(doc.alternativeOrderByQuestionId ?? {})
1493
+ .flatMap(([owner, ids]) => ids.filter((entry) => entry === id).map(() => owner));
1494
+ if (owners.length !== 1 || owners[0] !== action.condition.sourceQuestionId) {
1495
+ throw new OperationConflictError('Narrative alternative must have exactly its source owner');
1496
+ }
1497
+ }
1498
+ const { rulesById, orderByQuestionId } = setScopedRule(doc.narrativeRulesById, doc.narrativeRuleOrderByQuestionId, action.ruleId, op.questionId, { ...doc.narrativeRulesById?.[action.ruleId], condition: action.condition });
1499
+ next = { ...doc, narrativeRulesById: rulesById, narrativeRuleOrderByQuestionId: orderByQuestionId };
1500
+ }
1501
+ }
1502
+ const settings = { ...(doc.narrativeRuleSettingsByQuestionId?.[op.questionId] ?? {}) };
1503
+ delete settings.conditionalPresence;
1504
+ const byQuestion = omitKey(doc.narrativeRuleSettingsByQuestionId ?? {}, op.questionId);
1505
+ return { ...next, narrativeRuleSettingsByQuestionId: Object.keys(settings).length === 0 ? byQuestion : { ...byQuestion, [op.questionId]: settings } };
1506
+ };
1305
1507
  export const applyOperation = (document, op) => {
1508
+ requireNarrativePresenceState(document);
1306
1509
  const next = reduce(document, op);
1510
+ requireNarrativePresenceState(next);
1307
1511
  return prune({ ...next, revision: document.revision + 1 });
1308
1512
  };
1309
1513
  /**
@@ -2362,6 +2566,14 @@ const reduce = (doc, op) => {
2362
2566
  highlightRuleOrderByQuestionId: orderByQuestionId,
2363
2567
  });
2364
2568
  }
2569
+ case 'highlightRule.setField':
2570
+ return setHighlightField(doc, op);
2571
+ case 'highlightRuleSettings.set':
2572
+ return setRuleSetting(doc, op, 'highlightRuleSettingsByQuestionId');
2573
+ case 'narrativeRuleSettings.set':
2574
+ return setRuleSetting(doc, op, 'narrativeRuleSettingsByQuestionId');
2575
+ case 'narrativeRuleCollection.edit':
2576
+ return editNarrativeCollection(doc, op);
2365
2577
  case 'narrativeRule.set': {
2366
2578
  if (!doc.questionsById?.[op.questionId]) {
2367
2579
  throw new OperationConflictError(`Unknown question "${op.questionId}"`);