@cratis/chronicle 6.67.3 → 6.67.5
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/dist/testing/ConstraintKey.d.ts +17 -0
- package/dist/testing/ConstraintKey.d.ts.map +1 -0
- package/dist/testing/ConstraintKey.js +72 -0
- package/dist/testing/ConstraintKey.js.map +1 -0
- package/dist/testing/EventScenario.d.ts.map +1 -1
- package/dist/testing/EventScenario.js +56 -33
- package/dist/testing/EventScenario.js.map +1 -1
- package/dist/testing/InProcessConstraints.d.ts +2 -1
- package/dist/testing/InProcessConstraints.d.ts.map +1 -1
- package/dist/testing/InProcessConstraints.js +11 -51
- package/dist/testing/InProcessConstraints.js.map +1 -1
- package/dist/testing/InProcessEventSequence.d.ts.map +1 -1
- package/dist/testing/InProcessEventSequence.js +4 -2
- package/dist/testing/InProcessEventSequence.js.map +1 -1
- package/dist/testing/PropertyDefinitionSupport.d.ts +5 -0
- package/dist/testing/PropertyDefinitionSupport.d.ts.map +1 -0
- package/dist/testing/PropertyDefinitionSupport.js +37 -0
- package/dist/testing/PropertyDefinitionSupport.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/testing/ConstraintKey.ts +81 -0
- package/testing/EventScenario.constraint-cycles.spec.ts +1 -1
- package/testing/EventScenario.fixtures.spec.ts +2 -1
- package/testing/EventScenario.ts +66 -37
- package/testing/InProcessConstraints.ts +10 -55
- package/testing/InProcessEventSequence.ts +5 -2
- package/testing/PropertyDefinitionSupport.ts +36 -0
- package/testing/for_EventScenario/when_appending_event_sequence_constraints/event_sequence_scenario_behaviors.fixture.ts +1 -1
- package/testing/for_EventScenario/when_appending_event_sequence_constraints/with_legacy_decorators.legacy.spec.ts +7 -3
- package/testing/for_EventScenario/when_appending_event_sequence_constraints/with_standard_decorators.standard.spec.ts +7 -3
- package/testing/for_EventScenario/when_appending_field_combinations/definition_rejection_behaviors.fixture.ts +7 -3
- package/testing/for_EventScenario/when_appending_field_combinations/removal_and_overlap.legacy.spec.ts +36 -0
- package/testing/for_EventScenario/when_appending_field_combinations/removal_and_overlap.standard.spec.ts +35 -0
- package/testing/for_EventScenario/when_appending_field_combinations/removal_and_overlap_behaviors.fixture.ts +193 -0
- package/testing/for_EventScenario/when_appending_field_types/field_scenario_behaviors.fixture.ts +19 -13
- package/testing/for_EventScenario/when_appending_field_types/fractional_collisions.fixture.ts +91 -0
- package/testing/for_EventScenario/when_appending_field_types/with_fractional_collisions.legacy.spec.ts +51 -0
- package/testing/for_EventScenario/when_appending_field_types/with_fractional_collisions.standard.spec.ts +50 -0
- package/testing/for_EventScenario/when_appending_scoped_constraints/with_unproven_operations.spec.ts +1 -1
package/testing/EventScenario.ts
CHANGED
|
@@ -14,10 +14,12 @@ import { EventSequenceId } from '../eventSequences/EventSequenceId.js';
|
|
|
14
14
|
import { getEventTypeFor, getEventTypeMetadata } from '../events/eventTypeDecorator.js';
|
|
15
15
|
import { compileConstraints } from '../events/constraints/Constraints.js';
|
|
16
16
|
import type { UniqueConstraintCapture } from '../events/constraints/UniqueConstraintBuilder.js';
|
|
17
|
+
import type { ConstraintCapture } from '../events/constraints/ConstraintBuilder.js';
|
|
17
18
|
import { getUniqueEventMetadata, getUniquePropertyMetadata } from '../events/constraints/unique.js';
|
|
18
19
|
import { getRemovedConstraintNames } from '../events/constraints/removeConstraint.js';
|
|
19
20
|
import { getConstraintMetadata } from '../events/constraints/constraint.js';
|
|
20
21
|
import { InProcessConstraints } from './InProcessConstraints.js';
|
|
22
|
+
import { supportsPropertyOverlap } from './PropertyDefinitionSupport.js';
|
|
21
23
|
import type { EventScenarioOptions } from './EventScenarioOptions.js';
|
|
22
24
|
import { EventScenarioGivenBuilder } from './EventScenarioGivenBuilder.js';
|
|
23
25
|
import { EventScenarioWhenBuilder } from './EventScenarioWhenBuilder.js';
|
|
@@ -53,7 +55,8 @@ function validateExtendedDefinition(name: string, unique: UniqueConstraintCaptur
|
|
|
53
55
|
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
54
56
|
'Guid and numeric keys with covered-and-removal events are not fixture-backed.');
|
|
55
57
|
}
|
|
56
|
-
if (
|
|
58
|
+
if (!supportsFieldlessPropertyRemoval(unique, constructors) &&
|
|
59
|
+
removers.some(id => isFieldless(id, constructors))) {
|
|
57
60
|
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
58
61
|
'Guid and numeric keys with fieldless removal events are not fixture-backed.');
|
|
59
62
|
}
|
|
@@ -82,6 +85,67 @@ function validateKeySchemas(name: string, schemas: ReadonlyArray<{ type?: string
|
|
|
82
85
|
}
|
|
83
86
|
}
|
|
84
87
|
|
|
88
|
+
function supportsFieldlessPropertyRemoval(unique: UniqueConstraintCapture | undefined, constructors: ReadonlyMap<string, Function>): boolean {
|
|
89
|
+
return !!unique && !unique.ignoreCasing && unique.eventDefinitions.every(entry => {
|
|
90
|
+
if (entry.properties.length !== 1) return false;
|
|
91
|
+
const field = getEventTypeMetadata(constructors.get(entry.eventTypeId)!)?.schema.properties?.[entry.properties[0]];
|
|
92
|
+
return field?.type === 'string' || field?.type === 'number';
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isFieldless(id: string | undefined, constructors: ReadonlyMap<string, Function>): boolean {
|
|
97
|
+
const type = id === undefined ? undefined : constructors.get(id);
|
|
98
|
+
return type !== undefined && Object.keys(getEventTypeMetadata(type)?.schema.properties ?? {}).length === 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function constraintReferences(capture: ConstraintCapture): { ids: (string | undefined)[]; removedWith: (string | undefined)[] } {
|
|
102
|
+
return {
|
|
103
|
+
ids: capture.uniqueConstraint?.eventDefinitions.map(entry => entry.eventTypeId) ??
|
|
104
|
+
capture.uniqueEventType?.eventTypeIds ?? [capture.uniqueEventType?.eventTypeId],
|
|
105
|
+
removedWith: [...(capture.uniqueConstraint?.removedWithEventTypeIds ?? []),
|
|
106
|
+
capture.uniqueConstraint?.removedWithEventTypeId, ...(capture.uniqueEventType?.removedWithEventTypeIds ?? [])]
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function validateScenarioDefinition(name: string, capture: ConstraintCapture,
|
|
111
|
+
options: { selectedIds: ReadonlySet<string>; constructors: ReadonlyMap<string, Function>; definitions: number }): void {
|
|
112
|
+
const { selectedIds, constructors, definitions } = options;
|
|
113
|
+
const { ids, removedWith } = constraintReferences(capture);
|
|
114
|
+
if ([...ids, ...removedWith].some(id => id !== undefined && !selectedIds.has(id))) {
|
|
115
|
+
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
116
|
+
'Every constrained event type must be in the selected catalog. Claiming and removal references must be complete.');
|
|
117
|
+
}
|
|
118
|
+
const scoped = Object.values(capture.scope).some(Boolean);
|
|
119
|
+
if (scoped && !supportsFieldlessPropertyRemoval(capture.uniqueConstraint, constructors) && removedWith.some(id => isFieldless(id, constructors))) {
|
|
120
|
+
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
121
|
+
'Scoped fieldless removers require a case-sensitive single-property unique constraint with string, Guid or numeric keys; other combinations are not fixture-backed.');
|
|
122
|
+
}
|
|
123
|
+
validateExtendedDefinition(name, capture.uniqueConstraint, constructors);
|
|
124
|
+
for (const entry of capture.uniqueConstraint?.eventDefinitions ?? []) {
|
|
125
|
+
const schemas = entry.properties.map(property => getEventTypeMetadata(constructors.get(entry.eventTypeId)!)?.schema.properties?.[property]);
|
|
126
|
+
validateKeySchemas(name, schemas, { scoped, definitions,
|
|
127
|
+
isolated: !scoped && capture.uniqueConstraint!.eventDefinitions.length === 1 && !removedWith.some(id => id !== undefined),
|
|
128
|
+
ignoreCasing: capture.uniqueConstraint!.ignoreCasing });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function scenarioConstraints(definitions: ReadonlyMap<string, ConstraintCapture>, selectedIds: ReadonlySet<string>,
|
|
133
|
+
constructors: ReadonlyMap<string, Function>): InProcessConstraints {
|
|
134
|
+
// Other-sequence definitions neither validate nor index this scenario's event log.
|
|
135
|
+
const eventLog = EventSequenceId.eventLog.value;
|
|
136
|
+
const applicable = new Map([...definitions].filter(([, capture]) =>
|
|
137
|
+
!capture.eventSequences?.length || capture.eventSequences.includes(eventLog)));
|
|
138
|
+
const inapplicable = new Set([...definitions.keys()].filter(name => !applicable.has(name)));
|
|
139
|
+
const ignoredRemovals = new Set([...definitions].filter(([name]) => inapplicable.has(name)).flatMap(([, capture]) =>
|
|
140
|
+
[capture.uniqueConstraint?.removedWithEventTypeId, ...(capture.uniqueConstraint?.removedWithEventTypeIds ?? [])]
|
|
141
|
+
.filter((id): id is string => id !== undefined)));
|
|
142
|
+
const schemas = new Map([...constructors].filter(([id]) => selectedIds.has(id)).map(([id, type]) => [id, getEventTypeMetadata(type)!.schema]));
|
|
143
|
+
const constraints = new InProcessConstraints(applicable, schemas, inapplicable, ignoredRemovals);
|
|
144
|
+
const count = supportsPropertyOverlap(applicable, schemas) ? 1 : applicable.size;
|
|
145
|
+
for (const [name, capture] of applicable) validateScenarioDefinition(name, capture, { selectedIds, constructors, definitions: count });
|
|
146
|
+
return constraints;
|
|
147
|
+
}
|
|
148
|
+
|
|
85
149
|
function hasConstraintDecorators(type: Function): boolean {
|
|
86
150
|
return getUniqueEventMetadata(type) !== undefined || getRemovedConstraintNames(type).length > 0 ||
|
|
87
151
|
TypeIntrospector.getTrackedProperties(type).some(property => getUniquePropertyMetadata(type, property) !== undefined);
|
|
@@ -143,42 +207,7 @@ export class EventScenario {
|
|
|
143
207
|
}
|
|
144
208
|
}
|
|
145
209
|
}
|
|
146
|
-
|
|
147
|
-
// validated nor indexed there by the kernel (constraints-event-sequences.json), so it takes no part.
|
|
148
|
-
const eventLog = EventSequenceId.eventLog.value;
|
|
149
|
-
const applicable = new Map([...definitions].filter(([, capture]) =>
|
|
150
|
-
!capture.eventSequences?.length || capture.eventSequences.includes(eventLog)));
|
|
151
|
-
const inapplicable = new Set([...definitions.keys()].filter(name => !applicable.has(name)));
|
|
152
|
-
constraints = new InProcessConstraints(applicable, new Map(eventTypes.map(type =>
|
|
153
|
-
[getEventTypeFor(type).id.value, getEventTypeMetadata(type)!.schema])), inapplicable);
|
|
154
|
-
for (const [name, capture] of applicable) {
|
|
155
|
-
const ids = capture.uniqueConstraint?.eventDefinitions.map(entry => entry.eventTypeId) ??
|
|
156
|
-
capture.uniqueEventType?.eventTypeIds ?? [capture.uniqueEventType?.eventTypeId];
|
|
157
|
-
const removedWith = [...(capture.uniqueConstraint?.removedWithEventTypeIds ?? []),
|
|
158
|
-
capture.uniqueConstraint?.removedWithEventTypeId,
|
|
159
|
-
...(capture.uniqueEventType?.removedWithEventTypeIds ?? [])];
|
|
160
|
-
if ([...ids, ...removedWith].some(id => id !== undefined && !selectedIds.has(id))) {
|
|
161
|
-
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
162
|
-
'Every constrained event type must be in the selected catalog. Claiming and removal references must be complete.');
|
|
163
|
-
}
|
|
164
|
-
const scoped = Object.values(capture.scope).some(Boolean);
|
|
165
|
-
if (scoped && removedWith.some(id => {
|
|
166
|
-
const type = eventTypes.find(type => getEventTypeFor(type).id.value === id);
|
|
167
|
-
return type && Object.keys(getEventTypeMetadata(type)?.schema.properties ?? {}).length === 0;
|
|
168
|
-
})) {
|
|
169
|
-
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
170
|
-
'Scoped fieldless removal events are not fixture-backed.');
|
|
171
|
-
}
|
|
172
|
-
validateExtendedDefinition(name, capture.uniqueConstraint, constructorsById);
|
|
173
|
-
for (const entry of capture.uniqueConstraint?.eventDefinitions ?? []) {
|
|
174
|
-
const type = eventTypes.find(type => getEventTypeFor(type).id.value === entry.eventTypeId)!;
|
|
175
|
-
const schemas = entry.properties.map(property => getEventTypeMetadata(type)?.schema.properties?.[property]);
|
|
176
|
-
validateKeySchemas(name, schemas, { scoped, definitions: applicable.size,
|
|
177
|
-
isolated: !scoped && capture.uniqueConstraint!.eventDefinitions.length === 1 &&
|
|
178
|
-
!removedWith.some(id => id !== undefined),
|
|
179
|
-
ignoreCasing: capture.uniqueConstraint!.ignoreCasing });
|
|
180
|
-
}
|
|
181
|
-
}
|
|
210
|
+
constraints = scenarioConstraints(definitions, selectedIds, constructorsById);
|
|
182
211
|
} catch (error) {
|
|
183
212
|
if (error instanceof UnsupportedEventSequenceOperation) throw error;
|
|
184
213
|
throw new UnsupportedEventSequenceOperation('artifacts.constraints', 'compiler', `Invalid constraint definition: ${String(error)}.`);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// Copyright (c) Cratis. All rights reserved.
|
|
2
2
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
3
|
|
|
4
|
-
import { createHash } from 'node:crypto';
|
|
5
4
|
import type { ConstraintViolation as ContractsConstraintViolation } from '@cratis/chronicle.contracts';
|
|
6
5
|
import type { AppendedEvent } from '../events/AppendedEvent.js';
|
|
7
6
|
import type { ConstraintCapture } from '../events/constraints/ConstraintBuilder.js';
|
|
@@ -10,58 +9,12 @@ import type { ConstraintViolation } from '../eventSequences/ConstraintViolation.
|
|
|
10
9
|
import type { JsonSchema } from '../schemas/JsonSchema.js';
|
|
11
10
|
import { UnsupportedEventSequenceOperation } from './UnsupportedEventSequenceOperation.js';
|
|
12
11
|
import { eventTypeScopeKey, propertyScopeKey, scopeIndex } from './ConstraintScope.js';
|
|
12
|
+
import { supportsPropertyOverlap } from './PropertyDefinitionSupport.js';
|
|
13
|
+
import { claimOf } from './ConstraintKey.js';
|
|
13
14
|
|
|
14
15
|
export type WireConstraintViolation = ContractsConstraintViolation;
|
|
15
16
|
|
|
16
17
|
const unavailable = '18446744073709551615';
|
|
17
|
-
// Only the key characters captured by the packaged oracle, not every valid event-content character.
|
|
18
|
-
const provenString = /^[A-Za-z0-9 .@_:\-|{}$\u00e9]*$/;
|
|
19
|
-
|
|
20
|
-
function kernelKeyString(value: unknown, name: string, schema?: JsonSchema): string {
|
|
21
|
-
// constraints-field-types.json: the kernel converts Guid keys before comparing or reporting them.
|
|
22
|
-
if (schema?.format === 'guid') {
|
|
23
|
-
if (typeof value === 'string' && /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i.test(value)) return value.toLowerCase();
|
|
24
|
-
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name, 'Only dashed Guid keys are fixture-backed.');
|
|
25
|
-
}
|
|
26
|
-
// Avoid .NET floating-point formatting, exponent spelling and JavaScript precision loss.
|
|
27
|
-
if (schema?.type === 'number') {
|
|
28
|
-
if (typeof value === 'number' && Number.isSafeInteger(value)) return String(value);
|
|
29
|
-
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
30
|
-
'Only safe-integer numeric keys are fixture-backed; fractional, exponential and unsafe-integer comparisons require a kernel-backed test.');
|
|
31
|
-
}
|
|
32
|
-
if (typeof value === 'string' && provenString.test(value)) return value;
|
|
33
|
-
if (typeof value === 'boolean') return value ? 'True' : 'False';
|
|
34
|
-
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
35
|
-
'Only fixture-backed scalar string and boolean keys are supported.');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function keyHash(value: string): string {
|
|
39
|
-
return createHash('sha256').update(value).digest('hex');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
type UniqueCapture = NonNullable<ConstraintCapture['uniqueConstraint']>;
|
|
43
|
-
type Claim = { key: string; parts: Array<{ property: string; raw: string }> };
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* The kernel key: each declared property's string in declared order, joined with a literal '-'
|
|
47
|
-
* (UniqueConstraintDefinitionExtensions.GetPropertiesAndValues/GetValue at Chronicle 8fe5d30). Not a tuple:
|
|
48
|
-
* ['a-b', 'c'] and ['a', 'b-c'] claim the same key, as constraints-composite.json captures.
|
|
49
|
-
*/
|
|
50
|
-
function claimOf(name: string, unique: UniqueCapture, eventTypeId: string, content: Record<string, unknown>, schema?: JsonSchema): Claim | undefined {
|
|
51
|
-
const properties = unique.eventDefinitions.find(entry => entry.eventTypeId === eventTypeId)?.properties;
|
|
52
|
-
if (properties === undefined) return undefined;
|
|
53
|
-
const parts = properties.map(property => ({ property, raw: kernelKeyString(content[property], name, schema?.properties?.[property]) }));
|
|
54
|
-
const joined = parts.map(part => part.raw).join('-');
|
|
55
|
-
if (!unique.ignoreCasing) return { key: keyHash(joined), parts };
|
|
56
|
-
// The kernel applies .NET ToLowerInvariant to the joined value. constraints-ignore-casing.json proves
|
|
57
|
-
// only the ASCII key domain, where that is exactly A-Z to a-z; host Unicode case tables are not used.
|
|
58
|
-
if (!/^[\x20-\x7e]*$/.test(joined)) {
|
|
59
|
-
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
60
|
-
'Case-insensitive keys outside the ASCII key domain are not fixture-backed.');
|
|
61
|
-
}
|
|
62
|
-
return { key: keyHash(joined.replace(/[A-Z]/g, letter => String.fromCharCode(letter.charCodeAt(0) + 32))), parts };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
18
|
/** Narrow, fixture-backed constraint validation over serialized event snapshots. */
|
|
66
19
|
export class InProcessConstraints {
|
|
67
20
|
private readonly _constrainedProperties = new Map<string, Set<string>>();
|
|
@@ -70,7 +23,9 @@ export class InProcessConstraints {
|
|
|
70
23
|
|
|
71
24
|
constructor(private readonly _definitions: ReadonlyMap<string, ConstraintCapture>,
|
|
72
25
|
private readonly _schemas: ReadonlyMap<string, JsonSchema> = new Map(),
|
|
73
|
-
private readonly _inapplicable: ReadonlySet<string> = new Set()
|
|
26
|
+
private readonly _inapplicable: ReadonlySet<string> = new Set(),
|
|
27
|
+
private readonly _ignoredRemovals: ReadonlySet<string> = new Set()) {
|
|
28
|
+
const provenOverlap = supportsPropertyOverlap(_definitions, _schemas);
|
|
74
29
|
const coveredTypes = new Map<string, string>();
|
|
75
30
|
const removalOwners = new Map<string, string>();
|
|
76
31
|
for (const [name, capture] of _definitions) {
|
|
@@ -78,7 +33,7 @@ export class InProcessConstraints {
|
|
|
78
33
|
throw this.unsupported(name, 'A definition with both constraint kinds is not fixture-backed.');
|
|
79
34
|
}
|
|
80
35
|
const scoped = Object.values(capture.scope).some(Boolean);
|
|
81
|
-
if (scoped && _definitions.size !== 1) {
|
|
36
|
+
if (scoped && _definitions.size !== 1 && !provenOverlap) {
|
|
82
37
|
throw this.unsupported(name, 'Scoped constraints alongside other definitions are not fixture-backed.');
|
|
83
38
|
}
|
|
84
39
|
if (capture.uniqueConstraint) {
|
|
@@ -138,7 +93,7 @@ export class InProcessConstraints {
|
|
|
138
93
|
const ids = capture.uniqueConstraint?.eventDefinitions.map(entry => entry.eventTypeId) ??
|
|
139
94
|
capture.uniqueEventType?.eventTypeIds ?? [capture.uniqueEventType!.eventTypeId];
|
|
140
95
|
for (const id of ids) {
|
|
141
|
-
if (coveredTypes.has(id)) throw this.unsupported(name, 'Overlapping constraints are not fixture-backed.');
|
|
96
|
+
if (coveredTypes.has(id) && !provenOverlap) throw this.unsupported(name, 'Overlapping constraints are not fixture-backed.');
|
|
142
97
|
coveredTypes.set(id, name);
|
|
143
98
|
}
|
|
144
99
|
}
|
|
@@ -154,7 +109,7 @@ export class InProcessConstraints {
|
|
|
154
109
|
}
|
|
155
110
|
|
|
156
111
|
isRemovalOnlyType(eventTypeId: string): boolean {
|
|
157
|
-
return this._propertyRemovalTypes.has(eventTypeId) && !this._constrainedProperties.has(eventTypeId);
|
|
112
|
+
return (this._propertyRemovalTypes.has(eventTypeId) || this._ignoredRemovals.has(eventTypeId)) && !this._constrainedProperties.has(eventTypeId);
|
|
158
113
|
}
|
|
159
114
|
|
|
160
115
|
hasRemovalType(eventTypeId: string): boolean { return this._removalTypes.has(eventTypeId); }
|
|
@@ -187,7 +142,7 @@ export class InProcessConstraints {
|
|
|
187
142
|
}
|
|
188
143
|
const properties = unique.eventDefinitions.find(entry => entry.eventTypeId === type)?.properties;
|
|
189
144
|
if (properties === undefined) continue;
|
|
190
|
-
claims.set(source, { key: claimOf(name,
|
|
145
|
+
claims.set(source, { key: claimOf(name, definition, prior, { schema: this._schemas.get(type), isolated: this._definitions.size === 1 })!.key,
|
|
191
146
|
sequence: prior.context.sequenceNumber.toString() });
|
|
192
147
|
}
|
|
193
148
|
}
|
|
@@ -198,7 +153,7 @@ export class InProcessConstraints {
|
|
|
198
153
|
const source = event.context.eventSourceId;
|
|
199
154
|
const violationCount = violations.length;
|
|
200
155
|
for (const [name, definition] of this._definitions) {
|
|
201
|
-
const claim = definition.uniqueConstraint && claimOf(name, definition
|
|
156
|
+
const claim = definition.uniqueConstraint && claimOf(name, definition, event, { schema: this._schemas.get(type), isolated: this._definitions.size === 1 });
|
|
202
157
|
if (claim) {
|
|
203
158
|
const key = claim.key;
|
|
204
159
|
const scope = propertyScopeKey(definition.scope, event.context);
|
|
@@ -104,12 +104,15 @@ export class InProcessEventSequence implements IEventSequence {
|
|
|
104
104
|
if (getTagsFor(type).length) throw this.unsupported('artifacts.eventTypes.tags', type.name, 'Tagged events are not fixture-backed.');
|
|
105
105
|
// Validate the complete schema, not only properties present on a particular event.
|
|
106
106
|
const schema = metadata.schema;
|
|
107
|
+
if (schema.properties && Object.keys(schema.properties).length === 0 && !this._constraints?.isRemovalOnlyType(eventType.id.value)) {
|
|
108
|
+
throw this.unsupported('artifacts.eventTypes.schema', type.name,
|
|
109
|
+
'Fieldless events must be removal-only types of a fixture-backed property constraint, including one scoped to another event sequence.');
|
|
110
|
+
}
|
|
107
111
|
if (schema.type !== 'object' || schema.compliance?.length || schema.security?.length ||
|
|
108
112
|
schema.enum !== undefined || schema.items !== undefined || schema.format !== undefined ||
|
|
109
113
|
schema.additionalProperties !== false ||
|
|
110
114
|
schema.required?.length !== Object.keys(schema.properties ?? {}).length ||
|
|
111
|
-
!schema.properties ||
|
|
112
|
-
!this._constraints?.isRemovalOnlyType(eventType.id.value)) ||
|
|
115
|
+
!schema.properties ||
|
|
113
116
|
Object.entries(schema.properties).some(([key, property]) =>
|
|
114
117
|
!/^[a-z][a-zA-Z0-9]*$/.test(key) || !supportsEventField(property))) {
|
|
115
118
|
throw this.unsupported('artifacts.eventTypes.schema', type.name, 'Only unclassified string, boolean, numeric, Guid, date and object fields are supported.');
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import type { ConstraintCapture } from '../events/constraints/ConstraintBuilder.js';
|
|
5
|
+
import type { JsonSchema } from '../schemas/JsonSchema.js';
|
|
6
|
+
|
|
7
|
+
/** The two independent, single-property definitions measured in constraints-overlapping-definitions.json. */
|
|
8
|
+
export function supportsPropertyOverlap(definitions: ReadonlyMap<string, ConstraintCapture>, schemas: ReadonlyMap<string, JsonSchema>): boolean {
|
|
9
|
+
if (definitions.size !== 2) return false;
|
|
10
|
+
const [first, second] = [...definitions.values()];
|
|
11
|
+
if (!isIndependentProperty(first) || !isIndependentProperty(second)) return false;
|
|
12
|
+
if (JSON.stringify(first.scope) !== JSON.stringify(second.scope)) return false;
|
|
13
|
+
const left = first.uniqueConstraint!.eventDefinitions;
|
|
14
|
+
const right = second.uniqueConstraint!.eventDefinitions;
|
|
15
|
+
return left.length === right.length && left.every(entry => {
|
|
16
|
+
const other = right.find(candidate => candidate.eventTypeId === entry.eventTypeId);
|
|
17
|
+
if (!other || other.properties[0] === entry.properties[0]) return false;
|
|
18
|
+
return [entry.properties[0], other.properties[0]].every(property => {
|
|
19
|
+
const field = schemas.get(entry.eventTypeId)?.properties?.[property];
|
|
20
|
+
return isProvenOverlapKey(field);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isProvenOverlapKey(schema: JsonSchema | undefined): boolean {
|
|
26
|
+
if (schema?.type === 'number') return schema.format === undefined;
|
|
27
|
+
if (schema?.type === 'string') return schema.format === undefined || schema.format === 'guid';
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isIndependentProperty(capture: ConstraintCapture): boolean {
|
|
32
|
+
const unique = capture.uniqueConstraint;
|
|
33
|
+
return !!unique && !capture.uniqueEventType && !unique.ignoreCasing &&
|
|
34
|
+
unique.removedWithEventTypeId === undefined && !unique.removedWithEventTypeIds?.length &&
|
|
35
|
+
unique.eventDefinitions.length > 0 && unique.eventDefinitions.every(entry => entry.properties.length === 1);
|
|
36
|
+
}
|
|
@@ -45,7 +45,7 @@ export function eventSequenceScenarioBehaviors(
|
|
|
45
45
|
it('should retain every event sequence case', () => {
|
|
46
46
|
should(fixture.eventSequenceCases.map(test => test.name)).deep.equal([
|
|
47
47
|
'property-event-log', 'property-event-log-and-outbox', 'property-outbox',
|
|
48
|
-
'cycle-event-log', 'cycle-outbox', 'once-event-log', 'once-outbox'
|
|
48
|
+
'cycle-event-log', 'cycle-outbox', 'once-event-log', 'once-outbox', 'fieldless-property-outbox'
|
|
49
49
|
]);
|
|
50
50
|
should(fixture.expected.cases.map(test => test.name)).deep.equal(fixture.eventSequenceCases.map(test => test.name));
|
|
51
51
|
});
|
|
@@ -26,6 +26,10 @@ class CycleRemoved { @field(String) label: string; constructor(label: string) {
|
|
|
26
26
|
@removeConstraint('OracleSequencedCycle')
|
|
27
27
|
class CycleExpired { @field(String) label: string; constructor(label: string) { this.label = label; } }
|
|
28
28
|
|
|
29
|
+
@eventType('OracleDomainCleared')
|
|
30
|
+
@removeConstraint('OracleSequencedKey')
|
|
31
|
+
class Cleared { }
|
|
32
|
+
|
|
29
33
|
const outbox = new EventSequenceId('outbox');
|
|
30
34
|
|
|
31
35
|
function constraints(caseName: string): Constructor[] {
|
|
@@ -36,7 +40,7 @@ function constraints(caseName: string): Constructor[] {
|
|
|
36
40
|
@constraint('OracleSequencedKey')
|
|
37
41
|
class Key {
|
|
38
42
|
define(builder: IConstraintBuilder) {
|
|
39
|
-
select(builder).unique(key => key.on(Text, event => event.key).removedWith(Removed).withMessage('Taken: {PropertyValue}'));
|
|
43
|
+
select(builder).unique(key => key.on(Text, event => event.key).removedWith(caseName.startsWith('fieldless-') ? Cleared : Removed).withMessage('Taken: {PropertyValue}'));
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
46
|
@constraint('OracleSequencedFirst')
|
|
@@ -46,10 +50,10 @@ function constraints(caseName: string): Constructor[] {
|
|
|
46
50
|
@constraint('OracleSequencedOnceConstraint')
|
|
47
51
|
class Once { define(builder: IConstraintBuilder) { select(builder).uniqueFor(First, 'Already recorded', 'OracleSequencedOnce'); } }
|
|
48
52
|
const kind = caseName.split('-')[0];
|
|
49
|
-
return kind === 'property' ? [Key] : kind === 'once' ? [Once] : [FirstCycle, SiblingCycle];
|
|
53
|
+
return kind === 'property' || kind === 'fieldless' ? [Key] : kind === 'once' ? [Once] : [FirstCycle, SiblingCycle];
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
describe('when appending event sequence constraints with legacy decorators', () => {
|
|
53
|
-
eventSequenceScenarioBehaviors({ string: Text, removed: Removed, first: First, sibling: Sibling,
|
|
57
|
+
eventSequenceScenarioBehaviors({ string: Text, removed: Removed, cleared: Cleared, first: First, sibling: Sibling,
|
|
54
58
|
cycleRemoved: CycleRemoved, cycleExpired: CycleExpired }, constraints);
|
|
55
59
|
});
|
|
@@ -25,6 +25,10 @@ class CycleRemoved { @field(String) label: string; constructor(label: string) {
|
|
|
25
25
|
@removeConstraint('OracleSequencedCycle')
|
|
26
26
|
class CycleExpired { @field(String) label: string; constructor(label: string) { this.label = label; } }
|
|
27
27
|
|
|
28
|
+
@eventType('OracleDomainCleared')
|
|
29
|
+
@removeConstraint('OracleSequencedKey')
|
|
30
|
+
class Cleared { }
|
|
31
|
+
|
|
28
32
|
const outbox = new EventSequenceId('outbox');
|
|
29
33
|
|
|
30
34
|
function constraints(caseName: string): Constructor[] {
|
|
@@ -35,7 +39,7 @@ function constraints(caseName: string): Constructor[] {
|
|
|
35
39
|
@constraint('OracleSequencedKey')
|
|
36
40
|
class Key {
|
|
37
41
|
define(builder: IConstraintBuilder) {
|
|
38
|
-
select(builder).unique(key => key.on(Text, event => event.key).removedWith(Removed).withMessage('Taken: {PropertyValue}'));
|
|
42
|
+
select(builder).unique(key => key.on(Text, event => event.key).removedWith(caseName.startsWith('fieldless-') ? Cleared : Removed).withMessage('Taken: {PropertyValue}'));
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
@constraint('OracleSequencedFirst')
|
|
@@ -45,10 +49,10 @@ function constraints(caseName: string): Constructor[] {
|
|
|
45
49
|
@constraint('OracleSequencedOnceConstraint')
|
|
46
50
|
class Once { define(builder: IConstraintBuilder) { select(builder).uniqueFor(First, 'Already recorded', 'OracleSequencedOnce'); } }
|
|
47
51
|
const kind = caseName.split('-')[0];
|
|
48
|
-
return kind === 'property' ? [Key] : kind === 'once' ? [Once] : [FirstCycle, SiblingCycle];
|
|
52
|
+
return kind === 'property' || kind === 'fieldless' ? [Key] : kind === 'once' ? [Once] : [FirstCycle, SiblingCycle];
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
describe('when appending event sequence constraints with standard decorators', () => {
|
|
52
|
-
eventSequenceScenarioBehaviors({ string: Text, removed: Removed, first: First, sibling: Sibling,
|
|
56
|
+
eventSequenceScenarioBehaviors({ string: Text, removed: Removed, cleared: Cleared, first: First, sibling: Sibling,
|
|
53
57
|
cycleRemoved: CycleRemoved, cycleExpired: CycleExpired }, constraints);
|
|
54
58
|
});
|
|
@@ -41,9 +41,13 @@ export function definitionRejectionBehaviors(types: {
|
|
|
41
41
|
}): void {
|
|
42
42
|
for (const [kind, Claim] of [['Guid', types.guid], ['numeric', types.number]] as const) {
|
|
43
43
|
describe(`when configuring unmeasured ${kind} definition combinations`, () => {
|
|
44
|
-
it('should
|
|
45
|
-
const
|
|
46
|
-
|
|
44
|
+
it('should release and reclaim an extended key with a fieldless remover', async () => {
|
|
45
|
+
const supported = definition(builder => builder.unique(key => key.on(Claim, event => event.key).removedWith(types.fieldless)));
|
|
46
|
+
const scenario = new EventScenario({ artifacts: { eventTypes: [Claim, types.fieldless], constraints: [supported] } });
|
|
47
|
+
(await scenario.append('A', new Claim())).isSuccess.should.be.true;
|
|
48
|
+
(await scenario.append('B', new Claim())).isSuccess.should.be.false;
|
|
49
|
+
(await scenario.append('A', new types.fieldless())).isSuccess.should.be.true;
|
|
50
|
+
(await scenario.append('B', new Claim())).sequenceNumber.value.should.equal(2n);
|
|
47
51
|
});
|
|
48
52
|
it('should reject an unscoped covered-and-removal event without mutating claims or advancing a sequence', async () => {
|
|
49
53
|
const invalid = definition(builder => builder.unique(key => key.on(Claim, event => event.key).removedWith(Claim)));
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import 'reflect-metadata';
|
|
5
|
+
import { field, Guid } from '@cratis/fundamentals';
|
|
6
|
+
import { describe } from 'vitest';
|
|
7
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
8
|
+
import { removalAndOverlapBehaviors } from './removal_and_overlap_behaviors.fixture.js';
|
|
9
|
+
|
|
10
|
+
@eventType('OracleCombinedNumber')
|
|
11
|
+
class NumberKey {
|
|
12
|
+
@field(Number) key = -7;
|
|
13
|
+
@field(String) suffix = 'Alpha';
|
|
14
|
+
@field(Guid) identifier = Guid.parse('abcdef01-abcd-abcd-abcd-abcdef012345');
|
|
15
|
+
}
|
|
16
|
+
@eventType('OracleCombinedGuid')
|
|
17
|
+
class GuidKey {
|
|
18
|
+
@field(Guid) key = Guid.parse('abcdef01-abcd-abcd-abcd-abcdef012345');
|
|
19
|
+
@field(String) suffix = 'Alpha';
|
|
20
|
+
@field(Number) amount = -7;
|
|
21
|
+
}
|
|
22
|
+
@eventType('OracleCombinedText')
|
|
23
|
+
class TextKey { @field(String) key = ''; }
|
|
24
|
+
@eventType('OracleCombinedBoolean')
|
|
25
|
+
class BooleanKey { @field(Boolean) key = true; }
|
|
26
|
+
@eventType('OracleOverlapFormatted')
|
|
27
|
+
class FormattedKey {
|
|
28
|
+
@field(Number) key = 7;
|
|
29
|
+
@field(String) suffix = 'Alpha';
|
|
30
|
+
}
|
|
31
|
+
@eventType('OracleCombinedRemoved')
|
|
32
|
+
class Removed { }
|
|
33
|
+
|
|
34
|
+
describe('when appending fieldless removals and overlapping definitions with legacy decorators', () => {
|
|
35
|
+
removalAndOverlapBehaviors({ number: NumberKey, guid: GuidKey, string: TextKey, boolean: BooleanKey, formatted: FormattedKey, removed: Removed });
|
|
36
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { field, Guid } from '@cratis/fundamentals';
|
|
5
|
+
import { describe } from 'vitest';
|
|
6
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
7
|
+
import { removalAndOverlapBehaviors } from './removal_and_overlap_behaviors.fixture.js';
|
|
8
|
+
|
|
9
|
+
@eventType('OracleCombinedNumber')
|
|
10
|
+
class NumberKey {
|
|
11
|
+
@field(Number) key = -7;
|
|
12
|
+
@field(String) suffix = 'Alpha';
|
|
13
|
+
@field(Guid) identifier = Guid.parse('abcdef01-abcd-abcd-abcd-abcdef012345');
|
|
14
|
+
}
|
|
15
|
+
@eventType('OracleCombinedGuid')
|
|
16
|
+
class GuidKey {
|
|
17
|
+
@field(Guid) key = Guid.parse('abcdef01-abcd-abcd-abcd-abcdef012345');
|
|
18
|
+
@field(String) suffix = 'Alpha';
|
|
19
|
+
@field(Number) amount = -7;
|
|
20
|
+
}
|
|
21
|
+
@eventType('OracleCombinedText')
|
|
22
|
+
class TextKey { @field(String) key = ''; }
|
|
23
|
+
@eventType('OracleCombinedBoolean')
|
|
24
|
+
class BooleanKey { @field(Boolean) key = true; }
|
|
25
|
+
@eventType('OracleOverlapFormatted')
|
|
26
|
+
class FormattedKey {
|
|
27
|
+
@field(Number) key = 7;
|
|
28
|
+
@field(String) suffix = 'Alpha';
|
|
29
|
+
}
|
|
30
|
+
@eventType('OracleCombinedRemoved')
|
|
31
|
+
class Removed { }
|
|
32
|
+
|
|
33
|
+
describe('when appending fieldless removals and overlapping definitions with standard decorators', () => {
|
|
34
|
+
removalAndOverlapBehaviors({ number: NumberKey, guid: GuidKey, string: TextKey, boolean: BooleanKey, formatted: FormattedKey, removed: Removed });
|
|
35
|
+
});
|