eventmodeler 0.5.0 → 0.6.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 (150) hide show
  1. package/dist/index.js +6776 -2132
  2. package/package.json +11 -5
  3. package/dist/api/index.d.ts +0 -285
  4. package/dist/api/index.js +0 -323
  5. package/dist/cloud/slices/index.d.ts +0 -276
  6. package/dist/cloud/slices/index.js +0 -406
  7. package/dist/eventmodeler.js +0 -5646
  8. package/dist/formatters.d.ts +0 -17
  9. package/dist/formatters.js +0 -482
  10. package/dist/index.d.ts +0 -2
  11. package/dist/lib/auth.d.ts +0 -24
  12. package/dist/lib/auth.js +0 -331
  13. package/dist/lib/backend.d.ts +0 -43
  14. package/dist/lib/backend.js +0 -73
  15. package/dist/lib/chapter-utils.d.ts +0 -13
  16. package/dist/lib/chapter-utils.js +0 -71
  17. package/dist/lib/cloud-client.d.ts +0 -69
  18. package/dist/lib/cloud-client.js +0 -364
  19. package/dist/lib/config.d.ts +0 -30
  20. package/dist/lib/config.js +0 -95
  21. package/dist/lib/diff/merge-rules.d.ts +0 -45
  22. package/dist/lib/diff/merge-rules.js +0 -210
  23. package/dist/lib/diff/model-differ.d.ts +0 -8
  24. package/dist/lib/diff/model-differ.js +0 -568
  25. package/dist/lib/diff/three-way-merge.d.ts +0 -7
  26. package/dist/lib/diff/three-way-merge.js +0 -390
  27. package/dist/lib/diff/types.d.ts +0 -75
  28. package/dist/lib/diff/types.js +0 -1
  29. package/dist/lib/element-lookup.d.ts +0 -58
  30. package/dist/lib/element-lookup.js +0 -126
  31. package/dist/lib/file-loader.d.ts +0 -8
  32. package/dist/lib/file-loader.js +0 -108
  33. package/dist/lib/flow-utils.d.ts +0 -53
  34. package/dist/lib/flow-utils.js +0 -348
  35. package/dist/lib/format.d.ts +0 -10
  36. package/dist/lib/format.js +0 -23
  37. package/dist/lib/project-config.d.ts +0 -27
  38. package/dist/lib/project-config.js +0 -83
  39. package/dist/lib/slice-utils.d.ts +0 -59
  40. package/dist/lib/slice-utils.js +0 -140
  41. package/dist/local/slices/index.d.ts +0 -11
  42. package/dist/local/slices/index.js +0 -13
  43. package/dist/projection.d.ts +0 -3
  44. package/dist/projection.js +0 -828
  45. package/dist/slices/add-field/index.d.ts +0 -8
  46. package/dist/slices/add-field/index.js +0 -211
  47. package/dist/slices/add-scenario/index.d.ts +0 -27
  48. package/dist/slices/add-scenario/index.js +0 -307
  49. package/dist/slices/codegen-chapter-events/index.d.ts +0 -2
  50. package/dist/slices/codegen-chapter-events/index.js +0 -145
  51. package/dist/slices/codegen-slice/index.d.ts +0 -2
  52. package/dist/slices/codegen-slice/index.js +0 -448
  53. package/dist/slices/create-automation-slice/index.d.ts +0 -2
  54. package/dist/slices/create-automation-slice/index.js +0 -304
  55. package/dist/slices/create-flow/index.d.ts +0 -2
  56. package/dist/slices/create-flow/index.js +0 -183
  57. package/dist/slices/create-state-change-slice/index.d.ts +0 -2
  58. package/dist/slices/create-state-change-slice/index.js +0 -263
  59. package/dist/slices/create-state-view-slice/index.d.ts +0 -2
  60. package/dist/slices/create-state-view-slice/index.js +0 -128
  61. package/dist/slices/diff/index.d.ts +0 -11
  62. package/dist/slices/diff/index.js +0 -293
  63. package/dist/slices/export-eventmodel-to-json/index.d.ts +0 -2
  64. package/dist/slices/export-eventmodel-to-json/index.js +0 -355
  65. package/dist/slices/git/index.d.ts +0 -2
  66. package/dist/slices/git/index.js +0 -125
  67. package/dist/slices/guide/guides/codegen.d.ts +0 -5
  68. package/dist/slices/guide/guides/codegen.js +0 -339
  69. package/dist/slices/guide/guides/connect-slices.d.ts +0 -5
  70. package/dist/slices/guide/guides/connect-slices.js +0 -202
  71. package/dist/slices/guide/guides/create-slices.d.ts +0 -5
  72. package/dist/slices/guide/guides/create-slices.js +0 -303
  73. package/dist/slices/guide/guides/explore.d.ts +0 -5
  74. package/dist/slices/guide/guides/explore.js +0 -251
  75. package/dist/slices/guide/guides/information-flow.d.ts +0 -5
  76. package/dist/slices/guide/guides/information-flow.js +0 -318
  77. package/dist/slices/guide/guides/scenarios.d.ts +0 -5
  78. package/dist/slices/guide/guides/scenarios.js +0 -269
  79. package/dist/slices/guide/index.d.ts +0 -1
  80. package/dist/slices/guide/index.js +0 -40
  81. package/dist/slices/import/index.d.ts +0 -8
  82. package/dist/slices/import/index.js +0 -63
  83. package/dist/slices/init/index.d.ts +0 -5
  84. package/dist/slices/init/index.js +0 -80
  85. package/dist/slices/list-chapters/index.d.ts +0 -3
  86. package/dist/slices/list-chapters/index.js +0 -21
  87. package/dist/slices/list-commands/index.d.ts +0 -3
  88. package/dist/slices/list-commands/index.js +0 -20
  89. package/dist/slices/list-events/index.d.ts +0 -3
  90. package/dist/slices/list-events/index.js +0 -98
  91. package/dist/slices/list-processors/index.d.ts +0 -3
  92. package/dist/slices/list-processors/index.js +0 -20
  93. package/dist/slices/list-readmodels/index.d.ts +0 -3
  94. package/dist/slices/list-readmodels/index.js +0 -21
  95. package/dist/slices/list-scenarios/index.d.ts +0 -3
  96. package/dist/slices/list-scenarios/index.js +0 -35
  97. package/dist/slices/list-screens/index.d.ts +0 -3
  98. package/dist/slices/list-screens/index.js +0 -47
  99. package/dist/slices/list-slices/index.d.ts +0 -3
  100. package/dist/slices/list-slices/index.js +0 -35
  101. package/dist/slices/login/index.d.ts +0 -1
  102. package/dist/slices/login/index.js +0 -20
  103. package/dist/slices/logout/index.d.ts +0 -1
  104. package/dist/slices/logout/index.js +0 -14
  105. package/dist/slices/map-fields/index.d.ts +0 -2
  106. package/dist/slices/map-fields/index.js +0 -269
  107. package/dist/slices/mark-slice-status/index.d.ts +0 -2
  108. package/dist/slices/mark-slice-status/index.js +0 -31
  109. package/dist/slices/merge/index.d.ts +0 -19
  110. package/dist/slices/merge/index.js +0 -147
  111. package/dist/slices/open-app/index.d.ts +0 -1
  112. package/dist/slices/open-app/index.js +0 -36
  113. package/dist/slices/remove-field/index.d.ts +0 -8
  114. package/dist/slices/remove-field/index.js +0 -167
  115. package/dist/slices/remove-scenario/index.d.ts +0 -2
  116. package/dist/slices/remove-scenario/index.js +0 -77
  117. package/dist/slices/search/index.d.ts +0 -3
  118. package/dist/slices/search/index.js +0 -302
  119. package/dist/slices/show-actor/index.d.ts +0 -4
  120. package/dist/slices/show-actor/index.js +0 -115
  121. package/dist/slices/show-aggregate/index.d.ts +0 -3
  122. package/dist/slices/show-aggregate/index.js +0 -108
  123. package/dist/slices/show-aggregate-completeness/index.d.ts +0 -4
  124. package/dist/slices/show-aggregate-completeness/index.js +0 -181
  125. package/dist/slices/show-chapter/index.d.ts +0 -3
  126. package/dist/slices/show-chapter/index.js +0 -195
  127. package/dist/slices/show-command/index.d.ts +0 -3
  128. package/dist/slices/show-command/index.js +0 -133
  129. package/dist/slices/show-completeness/index.d.ts +0 -4
  130. package/dist/slices/show-completeness/index.js +0 -731
  131. package/dist/slices/show-event/index.d.ts +0 -3
  132. package/dist/slices/show-event/index.js +0 -118
  133. package/dist/slices/show-model-summary/index.d.ts +0 -3
  134. package/dist/slices/show-model-summary/index.js +0 -31
  135. package/dist/slices/show-processor/index.d.ts +0 -3
  136. package/dist/slices/show-processor/index.js +0 -111
  137. package/dist/slices/show-readmodel/index.d.ts +0 -3
  138. package/dist/slices/show-readmodel/index.js +0 -158
  139. package/dist/slices/show-scenario/index.d.ts +0 -3
  140. package/dist/slices/show-scenario/index.js +0 -196
  141. package/dist/slices/show-screen/index.d.ts +0 -3
  142. package/dist/slices/show-screen/index.js +0 -139
  143. package/dist/slices/show-slice/index.d.ts +0 -3
  144. package/dist/slices/show-slice/index.js +0 -696
  145. package/dist/slices/update-field/index.d.ts +0 -15
  146. package/dist/slices/update-field/index.js +0 -208
  147. package/dist/slices/whoami/index.d.ts +0 -2
  148. package/dist/slices/whoami/index.js +0 -44
  149. package/dist/types.d.ts +0 -195
  150. package/dist/types.js +0 -1
@@ -1,8 +0,0 @@
1
- import type { EventModel } from '../../types.js';
2
- export declare function addField(model: EventModel, filePath: string, options: {
3
- command?: string;
4
- event?: string;
5
- readModel?: string;
6
- screen?: string;
7
- processor?: string;
8
- }, input: string): void;
@@ -1,211 +0,0 @@
1
- import * as crypto from 'node:crypto';
2
- import { appendEvent } from '../../lib/file-loader.js';
3
- import { findElementOrExit, excludeLinkedCopies } from '../../lib/element-lookup.js';
4
- const validFieldTypes = ['UUID', 'Boolean', 'Double', 'Decimal', 'Date', 'DateTime', 'Long', 'Int', 'String', 'Custom'];
5
- function parseJsonInput(input) {
6
- return JSON.parse(input);
7
- }
8
- function parseXmlInput(input) {
9
- const getAttr = (tag, attr) => {
10
- const match = tag.match(new RegExp(`${attr}="([^"]*)"`));
11
- return match ? match[1] : undefined;
12
- };
13
- const getBoolAttr = (tag, attr) => {
14
- const value = getAttr(tag, attr);
15
- if (value === 'true')
16
- return true;
17
- if (value === 'false')
18
- return false;
19
- return undefined;
20
- };
21
- const fieldMatch = input.match(/<field([^>]*?)(?:\/>|>([\s\S]*?)<\/field>)/);
22
- if (!fieldMatch) {
23
- throw new Error('Invalid XML: missing <field> tag');
24
- }
25
- const name = getAttr(fieldMatch[1], 'name');
26
- if (!name) {
27
- throw new Error('Invalid XML: field must have a name attribute');
28
- }
29
- const type = getAttr(fieldMatch[1], 'type');
30
- if (!type) {
31
- throw new Error('Invalid XML: field must have a type attribute');
32
- }
33
- const fieldInput = {
34
- name,
35
- type,
36
- isList: getBoolAttr(fieldMatch[1], 'isList'),
37
- isGenerated: getBoolAttr(fieldMatch[1], 'isGenerated'),
38
- isOptional: getBoolAttr(fieldMatch[1], 'isOptional'),
39
- isUserInput: getBoolAttr(fieldMatch[1], 'isUserInput'),
40
- };
41
- // Parse nested subfields for Custom type
42
- if (type === 'Custom' && fieldMatch[2]) {
43
- fieldInput.subfields = [];
44
- const subfieldMatches = fieldMatch[2].matchAll(/<field([^>]*?)(?:\/>|>([\s\S]*?)<\/field>)/g);
45
- for (const match of subfieldMatches) {
46
- const subfieldXml = match[0];
47
- fieldInput.subfields.push(parseXmlInput(subfieldXml));
48
- }
49
- }
50
- return fieldInput;
51
- }
52
- function parseInput(input) {
53
- const trimmed = input.trim();
54
- if (trimmed.startsWith('<')) {
55
- return parseXmlInput(trimmed);
56
- }
57
- return parseJsonInput(trimmed);
58
- }
59
- function createFieldFromInput(input) {
60
- return {
61
- id: crypto.randomUUID(),
62
- name: input.name,
63
- fieldType: input.type,
64
- isList: input.isList ?? false,
65
- isGenerated: input.isGenerated ?? false,
66
- isOptional: input.isOptional,
67
- isUserInput: input.isUserInput,
68
- subfields: input.subfields?.map(createFieldFromInput),
69
- };
70
- }
71
- export function addField(model, filePath, options, input) {
72
- // Determine which entity type
73
- const entityCount = [options.command, options.event, options.readModel, options.screen, options.processor].filter(Boolean).length;
74
- if (entityCount === 0) {
75
- console.error('Error: Must specify one of --command, --event, --read-model, --screen, or --processor');
76
- process.exit(1);
77
- }
78
- if (entityCount > 1) {
79
- console.error('Error: Can only specify one of --command, --event, --read-model, --screen, or --processor');
80
- process.exit(1);
81
- }
82
- // Parse input
83
- let fieldInput;
84
- try {
85
- fieldInput = parseInput(input);
86
- }
87
- catch (err) {
88
- console.error(`Error: Invalid input format: ${err.message}`);
89
- process.exit(1);
90
- }
91
- // Validate field type
92
- if (!validFieldTypes.includes(fieldInput.type)) {
93
- console.error(`Error: Invalid field type: ${fieldInput.type}`);
94
- console.error(`Valid types: ${validFieldTypes.join(', ')}`);
95
- const typeLower = fieldInput.type.toLowerCase();
96
- if (typeLower === 'list' || typeLower === 'array') {
97
- console.error(`\nHint: To make a field a list/array, use isList="true" with a valid type:`);
98
- console.error(` <field name="items" type="String" isList="true"/>`);
99
- }
100
- process.exit(1);
101
- }
102
- // Find entity and add field
103
- if (options.command) {
104
- addFieldToCommand(model, filePath, options.command, fieldInput);
105
- }
106
- else if (options.event) {
107
- addFieldToEvent(model, filePath, options.event, fieldInput);
108
- }
109
- else if (options.readModel) {
110
- addFieldToReadModel(model, filePath, options.readModel, fieldInput);
111
- }
112
- else if (options.screen) {
113
- addFieldToScreen(model, filePath, options.screen, fieldInput);
114
- }
115
- else if (options.processor) {
116
- addFieldToProcessor(model, filePath, options.processor, fieldInput);
117
- }
118
- }
119
- function addFieldToCommand(model, filePath, commandName, fieldInput) {
120
- const command = findElementOrExit(model.commands, commandName, 'command');
121
- // Check for duplicate field name
122
- if (command.fields.some(f => f.name.toLowerCase() === fieldInput.name.toLowerCase())) {
123
- console.error(`Error: Field "${fieldInput.name}" already exists on command "${command.name}"`);
124
- process.exit(1);
125
- }
126
- const field = createFieldFromInput(fieldInput);
127
- appendEvent(filePath, {
128
- type: 'CommandFieldAdded',
129
- data: {
130
- commandStickyId: command.id,
131
- field,
132
- timestamp: Date.now(),
133
- },
134
- });
135
- console.log(`Added field "${field.name}" to command "${command.name}"`);
136
- }
137
- function addFieldToEvent(model, filePath, eventName, fieldInput) {
138
- // Exclude linked copies - they share fields with their canonical original
139
- const event = findElementOrExit(excludeLinkedCopies(model.events), eventName, 'event');
140
- // Check for duplicate field name
141
- if (event.fields.some(f => f.name.toLowerCase() === fieldInput.name.toLowerCase())) {
142
- console.error(`Error: Field "${fieldInput.name}" already exists on event "${event.name}"`);
143
- process.exit(1);
144
- }
145
- const field = createFieldFromInput(fieldInput);
146
- appendEvent(filePath, {
147
- type: 'EventFieldAdded',
148
- data: {
149
- eventStickyId: event.id,
150
- field,
151
- timestamp: Date.now(),
152
- },
153
- });
154
- console.log(`Added field "${field.name}" to event "${event.name}"`);
155
- }
156
- function addFieldToReadModel(model, filePath, readModelName, fieldInput) {
157
- // Exclude linked copies - they share fields with their canonical original
158
- const readModel = findElementOrExit(excludeLinkedCopies(model.readModels), readModelName, 'read model');
159
- // Check for duplicate field name
160
- if (readModel.fields.some(f => f.name.toLowerCase() === fieldInput.name.toLowerCase())) {
161
- console.error(`Error: Field "${fieldInput.name}" already exists on read model "${readModel.name}"`);
162
- process.exit(1);
163
- }
164
- const field = createFieldFromInput(fieldInput);
165
- appendEvent(filePath, {
166
- type: 'ReadModelFieldAdded',
167
- data: {
168
- readModelStickyId: readModel.id,
169
- field,
170
- timestamp: Date.now(),
171
- },
172
- });
173
- console.log(`Added field "${field.name}" to read model "${readModel.name}"`);
174
- }
175
- function addFieldToScreen(model, filePath, screenName, fieldInput) {
176
- // Exclude linked copies - they share fields with their canonical original
177
- const screen = findElementOrExit(excludeLinkedCopies(model.screens), screenName, 'screen');
178
- // Check for duplicate field name
179
- if (screen.fields.some(f => f.name.toLowerCase() === fieldInput.name.toLowerCase())) {
180
- console.error(`Error: Field "${fieldInput.name}" already exists on screen "${screen.name}"`);
181
- process.exit(1);
182
- }
183
- const field = createFieldFromInput(fieldInput);
184
- appendEvent(filePath, {
185
- type: 'ScreenFieldAdded',
186
- data: {
187
- screenId: screen.id,
188
- field,
189
- timestamp: Date.now(),
190
- },
191
- });
192
- console.log(`Added field "${field.name}" to screen "${screen.name}"`);
193
- }
194
- function addFieldToProcessor(model, filePath, processorName, fieldInput) {
195
- const processor = findElementOrExit(model.processors, processorName, 'processor');
196
- // Check for duplicate field name
197
- if (processor.fields.some(f => f.name.toLowerCase() === fieldInput.name.toLowerCase())) {
198
- console.error(`Error: Field "${fieldInput.name}" already exists on processor "${processor.name}"`);
199
- process.exit(1);
200
- }
201
- const field = createFieldFromInput(fieldInput);
202
- appendEvent(filePath, {
203
- type: 'ProcessorFieldAdded',
204
- data: {
205
- processorId: processor.id,
206
- field,
207
- timestamp: Date.now(),
208
- },
209
- });
210
- console.log(`Added field "${field.name}" to processor "${processor.name}"`);
211
- }
@@ -1,27 +0,0 @@
1
- export interface ScenarioInput {
2
- name: string;
3
- description?: string;
4
- given?: EventInput[];
5
- when?: {
6
- command?: string;
7
- commandFieldValues?: Record<string, unknown>;
8
- events?: EventInput[];
9
- };
10
- then: {
11
- type: 'error' | 'events' | 'readModelAssertion' | 'command' | 'noCommand';
12
- errorMessage?: string;
13
- errorType?: string;
14
- events?: EventInput[];
15
- command?: string;
16
- commandFieldValues?: Record<string, unknown>;
17
- readModel?: string;
18
- givenEvents?: EventInput[];
19
- expected?: Record<string, unknown>;
20
- };
21
- }
22
- interface EventInput {
23
- event: string;
24
- fieldValues?: Record<string, unknown>;
25
- }
26
- export declare function parseScenarioInput(input: string): ScenarioInput;
27
- export {};
@@ -1,307 +0,0 @@
1
- import { XMLParser } from 'fast-xml-parser';
2
- const xmlParser = new XMLParser({
3
- ignoreAttributes: false,
4
- attributeNamePrefix: '@_',
5
- allowBooleanAttributes: false,
6
- parseAttributeValue: false,
7
- isArray: (name) => name === 'event' || name === 'field',
8
- trimValues: true,
9
- });
10
- function asArray(value) {
11
- if (value === undefined || value === null)
12
- return [];
13
- return Array.isArray(value) ? value : [value];
14
- }
15
- function asRecord(value, context) {
16
- if (!value || typeof value !== 'object' || Array.isArray(value)) {
17
- throw new Error(`${context} must be an object`);
18
- }
19
- return value;
20
- }
21
- function parseFieldValue(value) {
22
- const trimmed = value.trim();
23
- if (trimmed === '')
24
- return '';
25
- if (trimmed === 'true')
26
- return true;
27
- if (trimmed === 'false')
28
- return false;
29
- if (trimmed === 'null')
30
- return null;
31
- if (/^-?\d+$/.test(trimmed))
32
- return parseInt(trimmed, 10);
33
- if (/^-?\d+\.\d+$/.test(trimmed))
34
- return parseFloat(trimmed);
35
- try {
36
- return JSON.parse(trimmed);
37
- }
38
- catch {
39
- return trimmed;
40
- }
41
- }
42
- function parseFieldValuesFromNodes(nodes) {
43
- if (nodes.length === 0)
44
- return undefined;
45
- const result = {};
46
- for (const node of nodes) {
47
- const n = node;
48
- const name = n['@_name'];
49
- if (!name)
50
- continue;
51
- const childFields = asArray(n['field']);
52
- let value;
53
- if (childFields.length > 0) {
54
- value = parseFieldValuesFromNodes(childFields);
55
- }
56
- else {
57
- // Get text content (fast-xml-parser stores it as #text)
58
- const text = n['#text'];
59
- if (text === undefined || text === '') {
60
- value = null;
61
- }
62
- else {
63
- value = parseFieldValue(String(text));
64
- }
65
- }
66
- // Handle duplicate keys (multiple values → array)
67
- if (name in result) {
68
- const current = result[name];
69
- if (Array.isArray(current)) {
70
- current.push(value);
71
- }
72
- else {
73
- result[name] = [current, value];
74
- }
75
- }
76
- else {
77
- result[name] = value;
78
- }
79
- }
80
- return Object.keys(result).length > 0 ? result : undefined;
81
- }
82
- function parseEventInputsFromNodes(nodes) {
83
- const result = [];
84
- for (const node of nodes) {
85
- const n = node;
86
- const nameFromAttr = n['@_name'];
87
- const textContent = n['#text'];
88
- const eventName = nameFromAttr ?? textContent;
89
- if (!eventName)
90
- continue;
91
- const childFields = asArray(n['field']);
92
- result.push({
93
- event: eventName,
94
- fieldValues: parseFieldValuesFromNodes(childFields),
95
- });
96
- }
97
- return result;
98
- }
99
- function parseEventInput(value, context) {
100
- if (typeof value === 'string') {
101
- return { event: value };
102
- }
103
- const map = asRecord(value, context);
104
- const eventName = map.event ?? map.name ?? map.eventName;
105
- if (typeof eventName !== 'string' || eventName.trim() === '') {
106
- throw new Error(`${context} must include an event name`);
107
- }
108
- const fieldValues = map.fieldValues ?? map.fields;
109
- if (fieldValues === undefined) {
110
- return { event: eventName };
111
- }
112
- if (!fieldValues || typeof fieldValues !== 'object' || Array.isArray(fieldValues)) {
113
- throw new Error(`${context}.fieldValues must be an object`);
114
- }
115
- return { event: eventName, fieldValues: fieldValues };
116
- }
117
- function parseEventInputs(value, context) {
118
- if (value === undefined || value === null)
119
- return undefined;
120
- if (!Array.isArray(value)) {
121
- throw new Error(`${context} must be an array`);
122
- }
123
- return value.map((entry, index) => parseEventInput(entry, `${context}[${index}]`));
124
- }
125
- function parseCommandName(value, context) {
126
- if (value === undefined || value === null)
127
- return undefined;
128
- if (typeof value === 'string')
129
- return value;
130
- const map = asRecord(value, context);
131
- const name = map.name ?? map.command;
132
- if (typeof name !== 'string' || name.trim() === '') {
133
- throw new Error(`${context} must include a command name`);
134
- }
135
- return name;
136
- }
137
- function parseObjectValue(value, context) {
138
- if (value === undefined || value === null)
139
- return undefined;
140
- if (typeof value !== 'object' || Array.isArray(value)) {
141
- throw new Error(`${context} must be an object`);
142
- }
143
- return value;
144
- }
145
- function normalizeScenarioInput(rawValue) {
146
- const raw = asRecord(rawValue, 'scenario');
147
- const name = raw.name;
148
- if (typeof name !== 'string' || name.trim() === '') {
149
- throw new Error('scenario.name is required');
150
- }
151
- const given = Array.isArray(raw.given)
152
- ? parseEventInputs(raw.given, 'scenario.given')
153
- : raw.given && typeof raw.given === 'object'
154
- ? parseEventInputs(raw.given.events, 'scenario.given.events')
155
- : undefined;
156
- const whenRaw = raw.when && typeof raw.when === 'object'
157
- ? raw.when
158
- : undefined;
159
- const whenCommand = parseCommandName(whenRaw?.command, 'scenario.when.command');
160
- const whenCommandFieldValues = parseObjectValue(whenRaw?.commandFieldValues ?? whenRaw?.command?.fieldValues, 'scenario.when.commandFieldValues');
161
- const whenEvents = parseEventInputs(whenRaw?.events, 'scenario.when.events');
162
- const thenRaw = asRecord(raw.then, 'scenario.then');
163
- const thenType = thenRaw.type;
164
- if (thenType !== 'error' &&
165
- thenType !== 'events' &&
166
- thenType !== 'readModelAssertion' &&
167
- thenType !== 'command' &&
168
- thenType !== 'noCommand') {
169
- throw new Error('scenario.then.type must be one of: error, events, readModelAssertion, command, noCommand');
170
- }
171
- const readModelAssertionRaw = thenRaw.readModelAssertion && typeof thenRaw.readModelAssertion === 'object'
172
- ? thenRaw.readModelAssertion
173
- : undefined;
174
- const then = {
175
- type: thenType,
176
- errorMessage: typeof thenRaw.errorMessage === 'string' ? thenRaw.errorMessage : undefined,
177
- errorType: typeof thenRaw.errorType === 'string' ? thenRaw.errorType : undefined,
178
- events: parseEventInputs(thenRaw.events, 'scenario.then.events'),
179
- command: parseCommandName(thenRaw.command, 'scenario.then.command'),
180
- commandFieldValues: parseObjectValue(thenRaw.commandFieldValues ?? thenRaw.command?.fieldValues, 'scenario.then.commandFieldValues'),
181
- readModel: (thenRaw.readModel
182
- ?? readModelAssertionRaw?.readModel
183
- ?? readModelAssertionRaw?.readModelName
184
- ?? readModelAssertionRaw?.readModelStickyId),
185
- givenEvents: parseEventInputs(thenRaw.givenEvents ?? readModelAssertionRaw?.givenEvents, 'scenario.then.givenEvents'),
186
- expected: parseObjectValue(thenRaw.expected ?? thenRaw.expectedFieldValues ?? readModelAssertionRaw?.expectedFieldValues, 'scenario.then.expected'),
187
- };
188
- return {
189
- name,
190
- description: typeof raw.description === 'string' ? raw.description : undefined,
191
- given,
192
- when: whenRaw ? { command: whenCommand, commandFieldValues: whenCommandFieldValues, events: whenEvents } : undefined,
193
- then,
194
- };
195
- }
196
- function parseJsonInput(input) {
197
- return normalizeScenarioInput(JSON.parse(input));
198
- }
199
- function parseXmlInput(input) {
200
- const parsed = xmlParser.parse(input);
201
- const scenario = parsed['scenario'];
202
- if (!scenario) {
203
- throw new Error('Invalid XML: missing <scenario> tag');
204
- }
205
- const name = scenario['@_name'];
206
- if (!name) {
207
- throw new Error('Invalid XML: scenario must have a name attribute');
208
- }
209
- const description = scenario['@_description'];
210
- // Parse given events
211
- const givenNode = scenario['given'];
212
- const given = givenNode ? parseEventInputsFromNodes(asArray(givenNode['event'])) : [];
213
- // Parse when
214
- let when;
215
- const whenNode = scenario['when'];
216
- if (whenNode) {
217
- const commandNodes = asArray(whenNode['command']);
218
- const commandNode = commandNodes[0];
219
- const whenEvents = parseEventInputsFromNodes(asArray(whenNode['event']));
220
- if (commandNode || whenEvents.length > 0) {
221
- let commandName;
222
- let commandFieldValues;
223
- if (commandNode) {
224
- const nameFromAttr = commandNode['@_name'];
225
- const textContent = commandNode['#text'];
226
- commandName = nameFromAttr ?? textContent ?? undefined;
227
- commandFieldValues = parseFieldValuesFromNodes(asArray(commandNode['field']));
228
- }
229
- when = {
230
- command: commandName || undefined,
231
- commandFieldValues,
232
- events: whenEvents.length > 0 ? whenEvents : undefined,
233
- };
234
- }
235
- }
236
- // Parse then
237
- const thenNode = scenario['then'];
238
- if (!thenNode) {
239
- throw new Error('Invalid XML: missing <then> tag');
240
- }
241
- const thenType = thenNode['@_type'];
242
- if (thenType !== 'error' &&
243
- thenType !== 'events' &&
244
- thenType !== 'readModelAssertion' &&
245
- thenType !== 'command' &&
246
- thenType !== 'noCommand') {
247
- throw new Error('Invalid XML: <then> must have a type attribute');
248
- }
249
- const then = { type: thenType };
250
- if (thenType === 'error') {
251
- then.errorType = thenNode['@_errorType'];
252
- const errorTypeNode = thenNode['errorType'];
253
- if (!then.errorType && errorTypeNode) {
254
- then.errorType = typeof errorTypeNode === 'string' ? errorTypeNode : errorTypeNode['#text'];
255
- }
256
- const messageNode = thenNode['message'];
257
- if (messageNode) {
258
- then.errorMessage = typeof messageNode === 'string' ? messageNode : messageNode['#text'];
259
- }
260
- }
261
- else if (thenType === 'events') {
262
- then.events = parseEventInputsFromNodes(asArray(thenNode['event']));
263
- }
264
- else if (thenType === 'command') {
265
- const commandNodes = asArray(thenNode['command']);
266
- const commandNode = commandNodes[0];
267
- if (commandNode) {
268
- const nameFromAttr = commandNode['@_name'];
269
- const textContent = commandNode['#text'];
270
- then.command = nameFromAttr ?? textContent ?? undefined;
271
- then.commandFieldValues = parseFieldValuesFromNodes(asArray(commandNode['field']));
272
- }
273
- }
274
- else if (thenType === 'readModelAssertion') {
275
- const readModelNode = thenNode['read-model'];
276
- if (readModelNode) {
277
- const nameFromAttr = readModelNode['@_name'];
278
- const textContent = readModelNode['#text'];
279
- then.readModel = nameFromAttr ?? textContent ?? undefined;
280
- const expectedNode = readModelNode['expected'];
281
- const directFields = parseFieldValuesFromNodes(asArray(readModelNode['field']));
282
- const expectedFields = expectedNode ? parseFieldValuesFromNodes(asArray(expectedNode['field'])) : undefined;
283
- then.expected = {
284
- ...(directFields ?? {}),
285
- ...(expectedFields ?? {}),
286
- };
287
- const givenEventsNode = (readModelNode['given'] ?? readModelNode['given-events']);
288
- if (givenEventsNode) {
289
- then.givenEvents = parseEventInputsFromNodes(asArray(givenEventsNode['event']));
290
- }
291
- }
292
- }
293
- return {
294
- name,
295
- description,
296
- given: given.length > 0 ? given : undefined,
297
- when,
298
- then,
299
- };
300
- }
301
- export function parseScenarioInput(input) {
302
- const trimmed = input.trim();
303
- if (trimmed.startsWith('<')) {
304
- return parseXmlInput(trimmed);
305
- }
306
- return parseJsonInput(trimmed);
307
- }
@@ -1,2 +0,0 @@
1
- import type { EventModel } from '../../types.js';
2
- export declare function codegenEvents(model: EventModel, chapterName?: string): void;