asma-core-helpers 0.16.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ /**
2
+ * The question-type register — the one place that says what a question can be.
3
+ *
4
+ * `QnrQuestion.type` is this union rather than `string` on purpose: the value is part of the stored
5
+ * document and therefore part of `document_hash`, so a typo cannot be corrected later without a
6
+ * document migration across every stored version. A closed union makes an unknown type a build
7
+ * error on both sides instead of a row nobody can render.
8
+ *
9
+ * **The values are the legacy `QUESTION_TYPE` values verbatim, with exactly one rename**
10
+ * (`CompositeQuestion` → `QuestionGrid`, OQ-V2-17). That is deliberate backward compatibility: 18 of
11
+ * 19 types need no translation at all, so the import mapper and the reverse projection carry a
12
+ * one-row table rather than a whole vocabulary, and the legacy wire keeps receiving the names it
13
+ * always did.
14
+ *
15
+ * Transcribed from `asma-modules/editor/shared/types/questions/fields/Question.constants.ts`
16
+ * (`QUESTION_TYPE`), which stays canonical for the **legacy** contract. It cannot be imported here:
17
+ * it lives in the polyrepo root, outside both service repos' build contexts.
18
+ *
19
+ * @see asma-modules/_docs/editor/qnrs/cross/2026-07-12-21-20-analysis-qnr-template-field-inventory.md:365 — M-062 `q.type` → `question.type` (19-value enum)
20
+ * @see asma-modules/_docs/editor/qnrs/cross/2026-07-12-21-20-analysis-qnr-template-field-inventory.md:417 — M-098 `q.subtype`
21
+ * @see asma-modules/_docs/editor/qnrs/cross/2026-07-12-21-40-plan-qnr-stage2-new-model-editor-and-sync.md — OQ-V2-17 (CompositeQuestion → QuestionGrid)
22
+ */
23
+ /** The legacy discriminator values, exactly as `templates.properties` stores them. */
24
+ export const LEGACY_QUESTION_TYPES = [
25
+ 'BooleanQuestion',
26
+ 'Chart',
27
+ 'CheckBoxes',
28
+ 'CompositeQuestion',
29
+ 'DateField',
30
+ 'DocumentUpload',
31
+ 'Dropdown',
32
+ 'Emoticons',
33
+ 'ExpressionQuestion',
34
+ 'FormatNumber',
35
+ 'LinearScale',
36
+ 'Link',
37
+ 'RadioButtons',
38
+ 'Readonly',
39
+ 'Space',
40
+ 'TextLong',
41
+ 'TextShort',
42
+ 'Title',
43
+ 'Widget',
44
+ ];
45
+ /** What a v2 document stores in `question.type`. */
46
+ export const QUESTION_TYPES = [
47
+ 'BooleanQuestion',
48
+ 'Chart',
49
+ 'CheckBoxes',
50
+ 'DateField',
51
+ 'DocumentUpload',
52
+ 'Dropdown',
53
+ 'Emoticons',
54
+ 'ExpressionQuestion',
55
+ 'FormatNumber',
56
+ 'LinearScale',
57
+ 'Link',
58
+ 'QuestionGrid',
59
+ 'RadioButtons',
60
+ 'Readonly',
61
+ 'Space',
62
+ 'TextLong',
63
+ 'TextShort',
64
+ 'Title',
65
+ 'Widget',
66
+ ];
67
+ /**
68
+ * Written out in full rather than derived from the two arrays, so that `Record` makes TypeScript
69
+ * prove the table covers every legacy value — a derived table would need a cast, and the cast is
70
+ * exactly where a missing entry would hide.
71
+ */
72
+ export const LEGACY_TO_V2_QUESTION_TYPE = {
73
+ BooleanQuestion: 'BooleanQuestion',
74
+ Chart: 'Chart',
75
+ CheckBoxes: 'CheckBoxes',
76
+ // The one rename in the whole register (OQ-V2-17): a "composite question" is a grid of
77
+ // questions, and the legacy name says nothing about that.
78
+ CompositeQuestion: 'QuestionGrid',
79
+ DateField: 'DateField',
80
+ DocumentUpload: 'DocumentUpload',
81
+ Dropdown: 'Dropdown',
82
+ Emoticons: 'Emoticons',
83
+ ExpressionQuestion: 'ExpressionQuestion',
84
+ FormatNumber: 'FormatNumber',
85
+ LinearScale: 'LinearScale',
86
+ Link: 'Link',
87
+ RadioButtons: 'RadioButtons',
88
+ Readonly: 'Readonly',
89
+ Space: 'Space',
90
+ TextLong: 'TextLong',
91
+ TextShort: 'TextShort',
92
+ Title: 'Title',
93
+ Widget: 'Widget',
94
+ };
95
+ const V2_TO_LEGACY_QUESTION_TYPE = Object.fromEntries(Object.entries(LEGACY_TO_V2_QUESTION_TYPE).map(([legacy, v2]) => [v2, legacy]));
96
+ export const isQuestionType = (value) => typeof value === 'string' && QUESTION_TYPES.includes(value);
97
+ /**
98
+ * Legacy → v2. Returns `undefined` for anything not in the register instead of passing the value
99
+ * through: an unmapped type reaching a document would be a question no editor can render, and the
100
+ * import has a report to record it in.
101
+ */
102
+ export const toV2QuestionType = (legacy) => LEGACY_TO_V2_QUESTION_TYPE[legacy];
103
+ /** v2 → legacy, for the projection that writes back to the legacy wire. Total by construction. */
104
+ export const toLegacyQuestionType = (type) => V2_TO_LEGACY_QUESTION_TYPE[type];
105
+ //# sourceMappingURL=questionTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"questionTypes.js","sourceRoot":"","sources":["../../src/collaboration/questionTypes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,sFAAsF;AACtF,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACjC,iBAAiB;IACjB,OAAO;IACP,YAAY;IACZ,mBAAmB;IACnB,WAAW;IACX,gBAAgB;IAChB,UAAU;IACV,WAAW;IACX,oBAAoB;IACpB,cAAc;IACd,aAAa;IACb,MAAM;IACN,cAAc;IACd,UAAU;IACV,OAAO;IACP,UAAU;IACV,WAAW;IACX,OAAO;IACP,QAAQ;CACF,CAAA;AAIV,oDAAoD;AACpD,MAAM,CAAC,MAAM,cAAc,GAAG;IAC1B,iBAAiB;IACjB,OAAO;IACP,YAAY;IACZ,WAAW;IACX,gBAAgB;IAChB,UAAU;IACV,WAAW;IACX,oBAAoB;IACpB,cAAc;IACd,aAAa;IACb,MAAM;IACN,cAAc;IACd,cAAc;IACd,UAAU;IACV,OAAO;IACP,UAAU;IACV,WAAW;IACX,OAAO;IACP,QAAQ;CACF,CAAA;AAWV;;;;GAIG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAA6C;IAChF,eAAe,EAAE,iBAAiB;IAClC,KAAK,EAAE,OAAO;IACd,UAAU,EAAE,YAAY;IACxB,uFAAuF;IACvF,0DAA0D;IAC1D,iBAAiB,EAAE,cAAc;IACjC,SAAS,EAAE,WAAW;IACtB,cAAc,EAAE,gBAAgB;IAChC,QAAQ,EAAE,UAAU;IACpB,SAAS,EAAE,WAAW;IACtB,kBAAkB,EAAE,oBAAoB;IACxC,YAAY,EAAE,cAAc;IAC5B,WAAW,EAAE,aAAa;IAC1B,IAAI,EAAE,MAAM;IACZ,YAAY,EAAE,cAAc;IAC5B,QAAQ,EAAE,UAAU;IACpB,KAAK,EAAE,OAAO;IACd,QAAQ,EAAE,UAAU;IACpB,SAAS,EAAE,WAAW;IACtB,KAAK,EAAE,OAAO;IACd,MAAM,EAAE,QAAQ;CACnB,CAAA;AAED,MAAM,0BAA0B,GAAG,MAAM,CAAC,WAAW,CACjD,MAAM,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CACrC,CAAA;AAE7C,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAc,EAAyB,EAAE,CACpE,OAAO,KAAK,KAAK,QAAQ,IAAK,cAAoC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AAEtF;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,MAAc,EAA4B,EAAE,CACzE,0BAA0B,CAAC,MAA4B,CAAC,CAAA;AAE5D,kGAAkG;AAClG,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,IAAkB,EAAsB,EAAE,CAAC,0BAA0B,CAAC,IAAI,CAAC,CAAA"}
@@ -0,0 +1,120 @@
1
+ /**
2
+ * The template authoring document — a normalized entity graph with stable ids and
3
+ * explicit order arrays, in DOC-LAW-1 / DOC-LAW-2 minimal form.
4
+ *
5
+ * Every collection is optional because DOC-LAW-2 forbids storing an empty one: a
6
+ * document with no alternatives has no `alternativesById` key at all. Readers hydrate
7
+ * to a total in-memory model once at the document boundary rather than writing `?? {}`
8
+ * at each use site.
9
+ *
10
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:152 (document shape)
11
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:193 (DOC-LAW-1)
12
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:214 (§2.2a mapping graph)
13
+ */
14
+ import type { QuestionSubtype, QuestionType } from './questionTypes.js';
15
+ export type QuestionId = string;
16
+ export type RowId = string;
17
+ export type AltId = string;
18
+ export type TabId = string;
19
+ export type ActionId = string;
20
+ export type NodeId = string;
21
+ export type BindingId = string;
22
+ export type FilterId = string;
23
+ export type MappingId = string;
24
+ /** A scalar a document may carry. Deliberately not `any`: DOC-LAW-2 bans `null`. */
25
+ export type DocScalar = string | number | boolean;
26
+ /**
27
+ * `type` is the question's own discriminator (M-062), a closed union rather than a string — see
28
+ * `questionTypes.ts` for why the value cannot be loose. Not to be confused with the other `kind`
29
+ * slots in this file: `QnrAction.kind` and `BindingTarget.kind` discriminate *structure*, not the
30
+ * question a user authored.
31
+ */
32
+ export type QnrQuestion = {
33
+ type: QuestionType;
34
+ subtype?: QuestionSubtype;
35
+ label?: string;
36
+ required?: boolean;
37
+ defaultValue?: DocScalar;
38
+ [key: string]: unknown;
39
+ };
40
+ export type QnrAlternative = {
41
+ label?: string;
42
+ value?: DocScalar;
43
+ [key: string]: unknown;
44
+ };
45
+ export type QnrGridRow = {
46
+ label?: string;
47
+ [key: string]: unknown;
48
+ };
49
+ export type QnrTab = {
50
+ label?: string;
51
+ [key: string]: unknown;
52
+ };
53
+ export type QnrAction = {
54
+ kind?: string;
55
+ [key: string]: unknown;
56
+ };
57
+ /** One traversal step. Many questions off one entity share a single node (§2.2a). */
58
+ export type MappingNode = {
59
+ entityId: string;
60
+ parentNodeId?: NodeId;
61
+ relationshipId?: string;
62
+ filterOrder?: FilterId[];
63
+ [key: string]: unknown;
64
+ };
65
+ /**
66
+ * A binding's target. A `gridColumn` target is a pair, which is why the binding key is
67
+ * a synthetic id rather than the question id (§2.2a).
68
+ */
69
+ export type BindingTarget = {
70
+ kind: 'question';
71
+ questionId: QuestionId;
72
+ } | {
73
+ kind: 'gridColumn';
74
+ gridQuestionId: QuestionId;
75
+ columnQuestionId: QuestionId;
76
+ };
77
+ export type MappingBinding = {
78
+ nodeId: NodeId;
79
+ fieldId: string;
80
+ target: BindingTarget;
81
+ cardinality?: string;
82
+ onMissing?: string;
83
+ onMany?: string;
84
+ [key: string]: unknown;
85
+ };
86
+ /** Carries no `nodeId` back-pointer: the node already lists it (§2.2a). */
87
+ export type MappingFilter = {
88
+ fieldId: string;
89
+ operator: string;
90
+ value?: DocScalar;
91
+ };
92
+ export type QnrTemplateDocument = {
93
+ documentId: string;
94
+ revision: number;
95
+ meta?: Record<string, unknown>;
96
+ questionsById?: Record<QuestionId, QnrQuestion>;
97
+ questionOrder: QuestionId[];
98
+ gridRowsById?: Record<RowId, QnrGridRow>;
99
+ gridRowOrderByQuestionId?: Record<QuestionId, RowId[]>;
100
+ alternativesById?: Record<AltId, QnrAlternative>;
101
+ alternativeOrderByQuestionId?: Record<QuestionId, AltId[]>;
102
+ tabsById?: Record<TabId, QnrTab>;
103
+ tabOrder?: TabId[];
104
+ actionsById?: Record<ActionId, QnrAction>;
105
+ dataMappingsById?: Record<MappingId, {
106
+ sourceId: string;
107
+ rootNodeId: NodeId;
108
+ bindingOrder?: BindingId[];
109
+ }>;
110
+ mappingNodesById?: Record<NodeId, MappingNode>;
111
+ mappingBindingsById?: Record<BindingId, MappingBinding>;
112
+ mappingFiltersById?: Record<FilterId, MappingFilter>;
113
+ };
114
+ /**
115
+ * A new family's document. `questionOrder` is present-and-empty rather than absent
116
+ * because it is the document's only structural array and every reader indexes it; an
117
+ * absent order array would make "no questions yet" and "malformed" the same shape.
118
+ */
119
+ export declare const emptyTemplateDocument: (documentId: string) => QnrTemplateDocument;
120
+ //# sourceMappingURL=templateDocument.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templateDocument.d.ts","sourceRoot":"","sources":["../../src/collaboration/templateDocument.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEvE,MAAM,MAAM,UAAU,GAAG,MAAM,CAAA;AAC/B,MAAM,MAAM,KAAK,GAAG,MAAM,CAAA;AAC1B,MAAM,MAAM,KAAK,GAAG,MAAM,CAAA;AAC1B,MAAM,MAAM,KAAK,GAAG,MAAM,CAAA;AAC1B,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAA;AAC7B,MAAM,MAAM,MAAM,GAAG,MAAM,CAAA;AAC3B,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAC9B,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAA;AAC7B,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAE9B,oFAAoF;AACpF,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAA;AAEjD;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG;IACtB,IAAI,EAAE,YAAY,CAAA;IAClB,OAAO,CAAC,EAAE,eAAe,CAAA;IACzB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,YAAY,CAAC,EAAE,SAAS,CAAA;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACzB,CAAA;AAED,MAAM,MAAM,cAAc,GAAG;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,SAAS,CAAC;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAA;AAC1F,MAAM,MAAM,UAAU,GAAG;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC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alized entity graph with stable ids and
3
+ * explicit order arrays, in DOC-LAW-1 / DOC-LAW-2 minimal form.
4
+ *
5
+ * Every collection is optional because DOC-LAW-2 forbids storing an empty one: a
6
+ * document with no alternatives has no `alternativesById` key at all. Readers hydrate
7
+ * to a total in-memory model once at the document boundary rather than writing `?? {}`
8
+ * at each use site.
9
+ *
10
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:152 (document shape)
11
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:193 (DOC-LAW-1)
12
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:214 (§2.2a mapping graph)
13
+ */
14
+ /**
15
+ * A new family's document. `questionOrder` is present-and-empty rather than absent
16
+ * because it is the document's only structural array and every reader indexes it; an
17
+ * absent order array would make "no questions yet" and "malformed" the same shape.
18
+ */
19
+ export const emptyTemplateDocument = (documentId) => ({
20
+ documentId,
21
+ revision: 0,
22
+ questionOrder: [],
23
+ });
24
+ //# sourceMappingURL=templateDocument.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templateDocument.js","sourceRoot":"","sources":["../../src/collaboration/templateDocument.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AA0FH;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,UAAkB,EAAuB,EAAE,CAAC,CAAC;IAC/E,UAAU;IACV,QAAQ,EAAE,CAAC;IACX,aAAa,EAAE,EAAE;CACpB,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asma-core-helpers",
3
- "version": "0.16.0",
3
+ "version": "0.18.0",
4
4
  "description": "Core helper utilities for ASMA applications",
5
5
  "files": [
6
6
  "lib/**/*",
@@ -14,6 +14,10 @@
14
14
  "./urql": {
15
15
  "types": "./lib/helpers/urql.d.ts",
16
16
  "default": "./lib/helpers/urql.js"
17
+ },
18
+ "./collaboration": {
19
+ "types": "./lib/collaboration/index.d.ts",
20
+ "default": "./lib/collaboration/index.js"
17
21
  }
18
22
  },
19
23
  "scripts": {
@@ -0,0 +1,124 @@
1
+ import assert from 'node:assert/strict'
2
+ import { describe, it } from 'node:test'
3
+
4
+ import {
5
+ CollaborationError,
6
+ OperationConflictError,
7
+ UnknownOperationError,
8
+ applyOperation,
9
+ } from './applyOperation.js'
10
+ import type { TemplateOp } from './operations.js'
11
+ import { emptyTemplateDocument } from './templateDocument.js'
12
+
13
+ /**
14
+ * Guards the invariants a consumer would notice if a publish broke them. The exhaustive
15
+ * per-op suite lives with the service that owns the storage; this one exists because this
16
+ * package publishes on every push to master, so the reducer must not be able to ship
17
+ * broken without a red build here first.
18
+ *
19
+ * @see asma-modules/_docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:380 (op vocabulary)
20
+ * @see asma-modules/_docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:195 (DOC-LAW-2)
21
+ */
22
+
23
+ const doc0 = emptyTemplateDocument('tpl-1')
24
+
25
+ const apply = (ops: TemplateOp[], from = doc0) => ops.reduce((doc, op) => applyOperation(doc, op), from)
26
+
27
+ const threeQuestions = apply([
28
+ { type: 'question.create', questionId: 'q-1', questionType: 'TextShort' },
29
+ { type: 'question.create', questionId: 'q-2', questionType: 'TextShort' },
30
+ { type: 'question.create', questionId: 'q-3', questionType: 'TextShort', atIndex: 0 },
31
+ ])
32
+
33
+ describe('applyOperation', () => {
34
+ it('bumps the revision by one and leaves its input untouched', () => {
35
+ const next = applyOperation(doc0, { type: 'question.create', questionId: 'q-1', questionType: 'TextShort' })
36
+
37
+ assert.equal(next.revision, 1)
38
+ assert.equal(doc0.revision, 0)
39
+ assert.deepEqual(doc0.questionOrder, [])
40
+ })
41
+
42
+ it('inserts at the requested index and clamps one past the end', () => {
43
+ assert.deepEqual(threeQuestions.questionOrder, ['q-3', 'q-1', 'q-2'])
44
+
45
+ const clamped = applyOperation(threeQuestions, {
46
+ type: 'question.create',
47
+ questionId: 'q-4',
48
+ questionType: 'TextShort',
49
+ atIndex: 99,
50
+ })
51
+
52
+ assert.deepEqual(clamped.questionOrder, ['q-3', 'q-1', 'q-2', 'q-4'])
53
+ })
54
+
55
+ it('moves by removing first, so the index counts the other questions only', () => {
56
+ const moved = applyOperation(threeQuestions, { type: 'question.move', questionId: 'q-2', toIndex: 0 })
57
+
58
+ assert.deepEqual(moved.questionOrder, ['q-2', 'q-3', 'q-1'])
59
+ })
60
+
61
+ it('drops the bindings that targeted a deleted question, keeping the shared node', () => {
62
+ const bound = apply([
63
+ { type: 'question.create', questionId: 'q-1', questionType: 'TextShort' },
64
+ { type: 'mappingNode.create', nodeId: 'n-1', entityId: 'Actor' },
65
+ {
66
+ type: 'mappingBinding.create',
67
+ bindingId: 'b-1',
68
+ nodeId: 'n-1',
69
+ fieldId: 'Navn',
70
+ target: { kind: 'question', questionId: 'q-1' },
71
+ },
72
+ ])
73
+ assert.ok(bound.mappingBindingsById?.['b-1'])
74
+
75
+ const deleted = applyOperation(bound, { type: 'question.delete', questionId: 'q-1' })
76
+
77
+ assert.equal(deleted.mappingBindingsById, undefined)
78
+ assert.ok(deleted.mappingNodesById?.['n-1'])
79
+ })
80
+
81
+ it('removes a collection that just became empty rather than storing it empty', () => {
82
+ const withAlternative = apply([
83
+ { type: 'question.create', questionId: 'q-1', questionType: 'RadioButtons' },
84
+ { type: 'alternative.create', questionId: 'q-1', alternativeId: 'a-1', label: 'Yes' },
85
+ ])
86
+ assert.deepEqual(withAlternative.alternativesById, { 'a-1': { label: 'Yes' } })
87
+
88
+ const removed = applyOperation(withAlternative, {
89
+ type: 'alternative.delete',
90
+ questionId: 'q-1',
91
+ alternativeId: 'a-1',
92
+ })
93
+
94
+ assert.equal(removed.alternativesById, undefined)
95
+ })
96
+
97
+ it('refuses a duplicate create as a conflict', () => {
98
+ const once = applyOperation(doc0, { type: 'question.create', questionId: 'q-1', questionType: 'TextShort' })
99
+
100
+ assert.throws(
101
+ () => applyOperation(once, { type: 'question.create', questionId: 'q-1', questionType: 'TextShort' }),
102
+ (error: unknown) => {
103
+ assert.ok(error instanceof OperationConflictError)
104
+ assert.equal(error.statusCode, 409)
105
+ assert.equal(error.code, 'qnr_operation_conflict')
106
+ return true
107
+ },
108
+ )
109
+ })
110
+
111
+ it('reports an unknown op in the shape the service error resolver maps', () => {
112
+ assert.throws(
113
+ () => applyOperation(doc0, { type: 'question.teleport' } as unknown as TemplateOp),
114
+ (error: unknown) => {
115
+ assert.ok(error instanceof UnknownOperationError)
116
+ // bunjs keys the HTTP envelope off this base class, not off CustomError.
117
+ assert.ok(error instanceof CollaborationError)
118
+ assert.equal(error.statusCode, 422)
119
+ assert.equal(error.code, 'qnr_unknown_operation')
120
+ return true
121
+ },
122
+ )
123
+ })
124
+ })