@cratis/chronicle 6.67.4 → 6.67.6
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 +80 -0
- package/dist/testing/ConstraintKey.js.map +1 -0
- package/dist/testing/EventScenario.d.ts.map +1 -1
- package/dist/testing/EventScenario.js +2 -3
- package/dist/testing/EventScenario.js.map +1 -1
- package/dist/testing/InProcessConstraints.d.ts.map +1 -1
- package/dist/testing/InProcessConstraints.js +3 -47
- package/dist/testing/InProcessConstraints.js.map +1 -1
- package/dist/testing/InProcessEventPayload.d.ts.map +1 -1
- package/dist/testing/InProcessEventPayload.js +2 -1
- package/dist/testing/InProcessEventPayload.js.map +1 -1
- package/dist/testing/MeasuredConstraintKeys.d.ts +19 -0
- package/dist/testing/MeasuredConstraintKeys.d.ts.map +1 -0
- package/dist/testing/MeasuredConstraintKeys.js +35 -0
- package/dist/testing/MeasuredConstraintKeys.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/testing/ConstraintKey.ts +89 -0
- package/testing/EventScenario.batches.spec.ts +1 -1
- package/testing/EventScenario.constraint-values.spec.ts +3 -2
- package/testing/EventScenario.fixtures.spec.ts +10 -11
- package/testing/EventScenario.rejections.spec.ts +2 -2
- package/testing/EventScenario.ts +2 -3
- package/testing/InProcessConstraints.ts +3 -51
- package/testing/InProcessEventPayload.ts +2 -1
- package/testing/MeasuredConstraintKeys.ts +42 -0
- package/testing/for_EventScenario/when_appending_field_types/field_key_measurements.fixture.ts +163 -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_field_key_measurements.legacy.spec.ts +16 -0
- package/testing/for_EventScenario/when_appending_field_types/with_field_key_measurements.standard.spec.ts +15 -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_field_types/with_unproven_schemas.legacy.spec.ts +1 -1
|
@@ -11,6 +11,7 @@ import { Tag } from '../events/Tag.js';
|
|
|
11
11
|
import { Identity } from '../identity/Identity.js';
|
|
12
12
|
import type { JsonSchema } from '../schemas/JsonSchema.js';
|
|
13
13
|
import { matchesEventField } from './EventFieldSchema.js';
|
|
14
|
+
import { isMeasuredText } from './MeasuredConstraintKeys.js';
|
|
14
15
|
import type { InProcessConstraints } from './InProcessConstraints.js';
|
|
15
16
|
import type { InProcessEventSequenceEnvironment } from './InProcessEventSequenceEnvironment.js';
|
|
16
17
|
|
|
@@ -77,7 +78,7 @@ export class InProcessEventPayload {
|
|
|
77
78
|
|
|
78
79
|
private matchesText(value: unknown, property: JsonSchema): boolean {
|
|
79
80
|
if (property.type !== 'string' || property.format !== undefined) return true;
|
|
80
|
-
return
|
|
81
|
+
return isMeasuredText(value as string);
|
|
81
82
|
}
|
|
82
83
|
|
|
83
84
|
private matchesConstraint(event: object, content: Record<string, unknown>, key: string): boolean {
|
|
@@ -0,0 +1,42 @@
|
|
|
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 { UnsupportedEventSequenceOperation } from './UnsupportedEventSequenceOperation.js';
|
|
5
|
+
|
|
6
|
+
// constraints-field-key-measurements.json: exact characters, not a general Unicode/control range.
|
|
7
|
+
const measuredText = /^[\x00\x01\x08-\x0a\x0c\x0d\x20-\x7e\u00c9\u00df\u00e9\u00f8\u0301\u4eac\u6771]*$/;
|
|
8
|
+
const numericValues: Readonly<Record<string, readonly number[]>> = {
|
|
9
|
+
decimal: [0.1, -1.2345, 1.2345678901234567, 0.000001],
|
|
10
|
+
float: [0.1, -1.2345, 1.2345679]
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** Strings with measured JSON round-trip and kernel content-hash parity; never normalizes Unicode. */
|
|
14
|
+
export function isMeasuredText(value: string): boolean { return measuredText.test(value); }
|
|
15
|
+
|
|
16
|
+
const basicKeySchemas = new Set(['boolean/undefined', 'string/undefined', 'string/guid', 'number/undefined', 'number/double']);
|
|
17
|
+
type DefinitionShape = { scoped: boolean; isolated: boolean; definitions: number; ignoreCasing: boolean };
|
|
18
|
+
|
|
19
|
+
function isolatedMeasurement(options: DefinitionShape, properties: number): boolean {
|
|
20
|
+
return options.isolated && !options.scoped && !options.ignoreCasing && options.definitions === 1 && properties === 1;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Existing unique-key schemas plus the isolated decimal/float measurements. */
|
|
24
|
+
export function supportsMeasuredKeySchema(schema: { type?: string; format?: string } | undefined,
|
|
25
|
+
options: DefinitionShape, properties: number): boolean {
|
|
26
|
+
if (basicKeySchemas.has(`${schema?.type}/${schema?.format}`)) return true;
|
|
27
|
+
if (schema?.type !== 'number' || !isMeasuredNumericFormat(schema.format)) return false;
|
|
28
|
+
return isolatedMeasurement(options, properties);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The numeric schemas measured by the raw-JSON oracle, not a promise about other values. */
|
|
32
|
+
export function isMeasuredNumericFormat(format: string | undefined): boolean {
|
|
33
|
+
return format === 'decimal' || format === 'float';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Exact input values with matching key spelling, content and hash, in an isolated definition only. */
|
|
37
|
+
export function measuredNumericKey(value: unknown, format: string, name: string, enabled: boolean): string {
|
|
38
|
+
const candidates = enabled ? numericValues[format] ?? [] : [];
|
|
39
|
+
if (typeof value === 'number' && candidates.includes(value)) return String(value);
|
|
40
|
+
throw new UnsupportedEventSequenceOperation('artifacts.constraints', name,
|
|
41
|
+
`Only explicitly measured ${format} numeric key values in isolated, unscoped single-property constraints are fixture-backed.`);
|
|
42
|
+
}
|
package/testing/for_EventScenario/when_appending_field_types/field_key_measurements.fixture.ts
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
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 { readFileSync } from 'node:fs';
|
|
5
|
+
import type { Constructor } from '@cratis/fundamentals';
|
|
6
|
+
import { beforeEach, chai, describe, it, vi, type Assertion } from 'vitest';
|
|
7
|
+
import type { AppendResult } from '../../../eventSequences/AppendResult.js';
|
|
8
|
+
import type { JsonSchema } from '../../../schemas/JsonSchema.js';
|
|
9
|
+
import { constraint } from '../../../events/constraints/constraint.js';
|
|
10
|
+
import type { IConstraintBuilder } from '../../../events/constraints/IConstraintBuilder.js';
|
|
11
|
+
import { getEventTypeMetadata } from '../../../events/eventTypeDecorator.js';
|
|
12
|
+
import { EventScenario, UnsupportedEventSequenceOperation } from '../../index.js';
|
|
13
|
+
import { InProcessConstraints } from '../../InProcessConstraints.js';
|
|
14
|
+
|
|
15
|
+
chai.should();
|
|
16
|
+
const should = (value: unknown): Assertion => (value as { should: Assertion }).should;
|
|
17
|
+
type KeyEvent = { key: unknown };
|
|
18
|
+
type Types = { text: Constructor<KeyEvent>; decimal: Constructor<KeyEvent>; float: Constructor<KeyEvent> };
|
|
19
|
+
type Operation = { mode: string; events: Array<{ source: string; content: string }> };
|
|
20
|
+
type History = { source: string; sequence: string; content: Record<string, unknown>; hash: string | undefined };
|
|
21
|
+
type WireViolation = { ConstraintName: string; Message: string; Details: Record<string, string> };
|
|
22
|
+
type Outcome = { success: boolean; sequences: string[]; wireViolations: WireViolation[]; history: History[]; next: string };
|
|
23
|
+
const fixture = JSON.parse(readFileSync(new URL('../../fixtures/constraints-field-key-measurements.json', import.meta.url), 'utf8')) as {
|
|
24
|
+
fieldSchemas: Record<string, JsonSchema>;
|
|
25
|
+
fieldConstraintCases: Array<{ eventType: string; localSteps: number; operations: Operation[] }>;
|
|
26
|
+
expected: Array<{ steps: Outcome[] }>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function definition(type: Constructor<KeyEvent>, mode = 'plain', name = 'OracleFieldKey'): Constructor {
|
|
30
|
+
class Definition {
|
|
31
|
+
define(builder: IConstraintBuilder) {
|
|
32
|
+
if (mode === 'scoped') builder.perEventSourceType();
|
|
33
|
+
builder.unique(key => {
|
|
34
|
+
key.on(type, event => event.key);
|
|
35
|
+
if (mode === 'ignoreCasing') key.ignoreCasing();
|
|
36
|
+
if (mode === 'removable') key.removedWith(type);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
constraint(name)(Definition);
|
|
41
|
+
return Definition;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function scenarioFor(type: Constructor<KeyEvent>, constraints = [definition(type)]): EventScenario {
|
|
45
|
+
return new EventScenario({ artifacts: { eventTypes: [type], constraints } });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function eventFor(type: Constructor<KeyEvent>, content: string): KeyEvent {
|
|
49
|
+
return Object.assign(new type(), JSON.parse(content));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function historyOf(scenario: EventScenario): History[] {
|
|
53
|
+
return scenario.appendedEvents.map(event => ({ source: event.context.eventSourceId,
|
|
54
|
+
sequence: event.context.sequenceNumber.toString(), content: event.content, hash: event.context.hash }));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function append(scenario: EventScenario, type: Constructor<KeyEvent>, operation: Operation): Promise<AppendResult[]> {
|
|
58
|
+
const events = operation.events.map(entry => ({ eventSourceId: entry.source, event: eventFor(type, entry.content) }));
|
|
59
|
+
return operation.mode === 'single'
|
|
60
|
+
? [await scenario.append(events[0].eventSourceId, events[0].event)]
|
|
61
|
+
: scenario.appendMany(events);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function replay(type: Constructor<KeyEvent>, index: number): void {
|
|
65
|
+
describe(`when replaying the measured ${getEventTypeMetadata(type)!.eventType.id.value} keys`, () => {
|
|
66
|
+
it('should match collision details, sequences, hashes and rollback through the production serializer', async () => {
|
|
67
|
+
const validate = vi.spyOn(InProcessConstraints.prototype, 'validate');
|
|
68
|
+
const scenario = scenarioFor(type);
|
|
69
|
+
const test = fixture.fieldConstraintCases[index];
|
|
70
|
+
should(test.localSteps).greaterThan(0);
|
|
71
|
+
for (const [step, operation] of test.operations.slice(0, test.localSteps).entries()) {
|
|
72
|
+
const results = await append(scenario, type, operation);
|
|
73
|
+
const expected = fixture.expected[index].steps[step];
|
|
74
|
+
should(results.every(result => result.isSuccess === expected.success)).equal(true);
|
|
75
|
+
should(results.map(result => result.sequenceNumber.value.toString())).deep.equal(
|
|
76
|
+
expected.success ? expected.sequences : results.map(() => '0'));
|
|
77
|
+
const raw = validate.mock.results.at(-1)!.value as ReturnType<InProcessConstraints['validate']>;
|
|
78
|
+
should(raw.map(violation => ({ ...violation, SequenceNumber: violation.SequenceNumber.toString() }))).deep.equal(expected.wireViolations);
|
|
79
|
+
for (const result of results) {
|
|
80
|
+
should(result.constraintViolations.map(item => ({ id: item.constraintId, message: item.message, details: item.details })))
|
|
81
|
+
.deep.equal(expected.wireViolations.map(item => ({ id: item.ConstraintName, message: item.Message, details: item.Details })));
|
|
82
|
+
}
|
|
83
|
+
should(historyOf(scenario)).deep.equal(expected.history);
|
|
84
|
+
should((await scenario.eventSequence.getNextSequenceNumber()).value.toString()).equal(expected.next);
|
|
85
|
+
}
|
|
86
|
+
validate.mockRestore();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function fieldKeyMeasurements(types: Types): void {
|
|
92
|
+
for (const type of Object.values(types)) {
|
|
93
|
+
const metadata = getEventTypeMetadata(type)!;
|
|
94
|
+
metadata.schema.properties!.key = fixture.fieldSchemas[metadata.eventType.id.value];
|
|
95
|
+
}
|
|
96
|
+
// Replay complete supported prefixes; later numeric and emoji vectors remain kernel-only measurements.
|
|
97
|
+
replay(types.text, 0);
|
|
98
|
+
replay(types.decimal, 1);
|
|
99
|
+
replay(types.float, 2);
|
|
100
|
+
|
|
101
|
+
rejectUnmeasured(types);
|
|
102
|
+
rejectShapes(types);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function rejectUnmeasured(types: Types): void {
|
|
106
|
+
for (const [type, values] of [
|
|
107
|
+
[types.text, ['😀', 'λ', '\u0002']],
|
|
108
|
+
[types.decimal, [1e-7, 1e21, 1.5]],
|
|
109
|
+
[types.float, [1.23456789, 1e-7, 1e21, 0.000001, 1.5]]
|
|
110
|
+
] as const) {
|
|
111
|
+
for (const value of values) {
|
|
112
|
+
describe(`when the ${type.name} key is unproven ${JSON.stringify(value)}`, () => {
|
|
113
|
+
let scenario: EventScenario;
|
|
114
|
+
beforeEach(() => { scenario = scenarioFor(type); });
|
|
115
|
+
for (const mode of ['single', 'batch']) {
|
|
116
|
+
it(`should keep prior history, results and the next sequence after a rejected ${mode} append`, async () => {
|
|
117
|
+
const good = Object.assign(new type(), { key: type === types.text ? 'safe' : 0.1 });
|
|
118
|
+
await scenario.append('A', good);
|
|
119
|
+
const history = historyOf(scenario);
|
|
120
|
+
const results = [...scenario.results];
|
|
121
|
+
const invalid = Object.assign(new type(), { key: value });
|
|
122
|
+
const pending = mode === 'single' ? scenario.append('B', invalid)
|
|
123
|
+
: scenario.appendMany([{ eventSourceId: 'B', event: good }, { eventSourceId: 'C', event: invalid }]);
|
|
124
|
+
const error = await pending.then(() => undefined, error => error);
|
|
125
|
+
should(error).instanceOf(UnsupportedEventSequenceOperation);
|
|
126
|
+
should(historyOf(scenario)).deep.equal(history);
|
|
127
|
+
should(scenario.results).deep.equal(results);
|
|
128
|
+
should((await scenario.eventSequence.getNextSequenceNumber()).value).equal(1n);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
it('should reject atomically and leave setup results, history and the next sequence unchanged', async () => {
|
|
132
|
+
const good = Object.assign(new type(), { key: type === types.text ? 'safe' : 0.1 });
|
|
133
|
+
const invalid = Object.assign(new type(), { key: value });
|
|
134
|
+
const error = await scenario.given.forEventSource('A').events(good, invalid).then(() => undefined, error => error);
|
|
135
|
+
should(error).instanceOf(UnsupportedEventSequenceOperation);
|
|
136
|
+
should(scenario.appendedEvents).have.lengthOf(0);
|
|
137
|
+
should(scenario.results).have.lengthOf(0);
|
|
138
|
+
should((await scenario.eventSequence.getNextSequenceNumber()).value).equal(0n);
|
|
139
|
+
should((await scenario.append('A', good)).isSuccess).equal(true);
|
|
140
|
+
should((await scenario.eventSequence.getNextSequenceNumber()).value).equal(1n);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function rejectShapes(types: Types): void {
|
|
149
|
+
for (const mode of ['scoped', 'removable', 'ignoreCasing', 'overlapping']) {
|
|
150
|
+
for (const type of Object.values(types)) {
|
|
151
|
+
it(`should keep new ${type.name} keys rejected for ${mode} definitions`, async () => {
|
|
152
|
+
const constraints = mode === 'overlapping'
|
|
153
|
+
? [definition(type), definition(type, 'plain', 'OtherKey')]
|
|
154
|
+
: [definition(type, mode)];
|
|
155
|
+
const value = type === types.text ? '東京' : 0.1;
|
|
156
|
+
const error = await Promise.resolve().then(() => scenarioFor(type, constraints))
|
|
157
|
+
.then(scenario => scenario.append('A', Object.assign(new type(), { key: value }))).then(() => undefined, error => error);
|
|
158
|
+
should(error).instanceOf(UnsupportedEventSequenceOperation);
|
|
159
|
+
should(String(error)).include('artifacts.constraints');
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
package/testing/for_EventScenario/when_appending_field_types/field_scenario_behaviors.fixture.ts
CHANGED
|
@@ -156,17 +156,21 @@ export function fieldScenarioBehaviors(fixtures: {
|
|
|
156
156
|
});
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
for (const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
159
|
+
for (const [type, constraint] of [[fixtures.number, fixtures.numberConstraint], [fixtures.double, fixtures.doubleConstraint]] as const) {
|
|
160
|
+
for (const value of [0.00001, -0.00001, 1e-7, 1000000000000000.2, 4503599627370495.5,
|
|
161
|
+
-1000000000000000.2, -4503599627370495.5, 1e21, Number.MAX_SAFE_INTEGER + 1, -Number.MAX_SAFE_INTEGER - 1, NaN, Infinity]) {
|
|
162
|
+
describe(`when a constrained ${type.name} contains ${value}`, () => {
|
|
163
|
+
it('should reject the entire batch before recording results or history', async () => {
|
|
164
|
+
const scenario = new EventScenario({ artifacts: { eventTypes: [type], constraints: [constraint] } });
|
|
165
|
+
const event = new type(); event.key = value;
|
|
166
|
+
const error = await scenario.appendMany('A', [new type(), event]).then(() => undefined, error => error);
|
|
167
|
+
should(error).instanceOf(UnsupportedEventSequenceOperation);
|
|
168
|
+
should(scenario.results).have.lengthOf(0);
|
|
169
|
+
should(scenario.appendedEvents).have.lengthOf(0);
|
|
170
|
+
should((await scenario.eventSequence.getNextSequenceNumber()).value).equal(0n);
|
|
171
|
+
});
|
|
168
172
|
});
|
|
169
|
-
}
|
|
173
|
+
}
|
|
170
174
|
}
|
|
171
175
|
|
|
172
176
|
it('should normalize negative zero using production serialization before comparing numeric keys', async () => {
|
|
@@ -177,11 +181,13 @@ export function fieldScenarioBehaviors(fixtures: {
|
|
|
177
181
|
should((await scenario.append('B', negative)).isSuccess).equal(false);
|
|
178
182
|
});
|
|
179
183
|
|
|
180
|
-
const
|
|
184
|
+
const kernelFixtures = ['constraints-field-types', 'constraints-fractional-field-types'].map(name =>
|
|
185
|
+
JSON.parse(readFileSync(new URL(`../../fixtures/${name}.json`, import.meta.url), 'utf8')) as {
|
|
181
186
|
fieldConstraintCases: Array<{ kind: string; eventType: string; operations: Array<{ mode: string; events: Array<{ source: string; content: string }> }> }>;
|
|
182
187
|
expected: Array<{ fieldSchema: unknown; steps: Array<{ success: boolean; sequences: string[]; wireViolations: unknown[]; history: unknown[]; next: string }> }>;
|
|
183
|
-
};
|
|
184
|
-
for (const
|
|
188
|
+
});
|
|
189
|
+
for (const { fixture, caseIndex, test } of kernelFixtures.flatMap(fixture =>
|
|
190
|
+
fixture.fieldConstraintCases.map((test, caseIndex) => ({ fixture, test, caseIndex })))) {
|
|
185
191
|
if (test.kind !== 'kernelSemantics') continue;
|
|
186
192
|
describe(`when replaying the packaged ${test.eventType} constraint fixture`, () => {
|
|
187
193
|
it('should match every raw violation, hash, history, sequence and atomic rejection', async () => {
|
|
@@ -0,0 +1,91 @@
|
|
|
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 { readFileSync } from 'node:fs';
|
|
5
|
+
import type { Constructor } from '@cratis/fundamentals';
|
|
6
|
+
import { beforeEach, chai, describe, it, vi, type Assertion } from 'vitest';
|
|
7
|
+
import { getEventTypeMetadata } from '../../../events/eventTypeDecorator.js';
|
|
8
|
+
import { EventScenario, UnsupportedEventSequenceOperation } from '../../index.js';
|
|
9
|
+
import { InProcessConstraints } from '../../InProcessConstraints.js';
|
|
10
|
+
|
|
11
|
+
chai.should();
|
|
12
|
+
const should = (value: unknown): Assertion => (value as { should: Assertion }).should;
|
|
13
|
+
|
|
14
|
+
type Input = { source: string; type: 'number' | 'string'; value: number | string };
|
|
15
|
+
type WireViolation = { SequenceNumber: string; ConstraintName: string; Message: string; Details: Record<string, string> };
|
|
16
|
+
type RejectedDefinition = { name: string; eventTypes: Constructor[]; constraints: Constructor[]; eventType: Constructor<{ key: number }> };
|
|
17
|
+
type Fixture = {
|
|
18
|
+
fieldCombinationCases: Array<{ isolatedConstraintOperations: Array<{ mode: string; events: Input[] }> }>;
|
|
19
|
+
expected: { cases: Array<{ name: string; outcomes: Array<{
|
|
20
|
+
success: boolean; sequences: string[]; wireViolations: WireViolation[]; next: string;
|
|
21
|
+
}> }> };
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function fractionalCollisions(fixtures: { number: Constructor<{ key: number }>; text: Constructor<{ key: string }>;
|
|
25
|
+
constraint: Constructor; scoped: Constructor; rejectedDefinitions: RejectedDefinition[] }): void {
|
|
26
|
+
const fixture = JSON.parse(readFileSync(new URL('../../fixtures/constraints-fractional-collisions.json', import.meta.url), 'utf8')) as Fixture;
|
|
27
|
+
const operations = fixture.fieldCombinationCases[0].isolatedConstraintOperations;
|
|
28
|
+
const outcomes = fixture.expected.cases[0].outcomes;
|
|
29
|
+
const eventTypes = [fixtures.number, fixtures.text];
|
|
30
|
+
|
|
31
|
+
describe('when replaying unscoped string and fractional key collisions', () => {
|
|
32
|
+
it('should match the kernel violations and atomic ownership transitions after every request', async () => {
|
|
33
|
+
should(fixture.expected.cases[0].name).equal('fractional-0');
|
|
34
|
+
should(operations.length).equal(outcomes.length);
|
|
35
|
+
should(getEventTypeMetadata(fixtures.number)!.eventType.id.value).equal('OracleCombinedNumber');
|
|
36
|
+
should(getEventTypeMetadata(fixtures.text)!.eventType.id.value).equal('OracleCombinedText');
|
|
37
|
+
const scenario = new EventScenario({ artifacts: { eventTypes, constraints: [fixtures.constraint] } });
|
|
38
|
+
const validate = vi.spyOn(InProcessConstraints.prototype, 'validate');
|
|
39
|
+
try {
|
|
40
|
+
for (const [index, operation] of operations.entries()) {
|
|
41
|
+
const expected = outcomes[index];
|
|
42
|
+
const entries = operation.events.map(input => ({ eventSourceId: input.source,
|
|
43
|
+
event: Object.assign(new (input.type === 'number' ? fixtures.number : fixtures.text)(), { key: input.value }) }));
|
|
44
|
+
const before = [...scenario.appendedEvents];
|
|
45
|
+
const results = operation.mode === 'single'
|
|
46
|
+
? [await scenario.append(entries[0].eventSourceId, entries[0].event)] : await scenario.appendMany(entries);
|
|
47
|
+
should(results.every(result => result.isSuccess === expected.success)).equal(true);
|
|
48
|
+
should(results.map(result => result.sequenceNumber.value.toString())).deep.equal(
|
|
49
|
+
expected.success ? expected.sequences : results.map(() => '0'));
|
|
50
|
+
const raw = validate.mock.results.at(-1)!.value as ReturnType<InProcessConstraints['validate']>;
|
|
51
|
+
should(raw.map(violation => ({ ...violation, SequenceNumber: violation.SequenceNumber.toString() }))).deep.equal(expected.wireViolations);
|
|
52
|
+
for (const result of results) {
|
|
53
|
+
should(result.constraintViolations.map(item => ({ id: item.constraintId, message: item.message, details: item.details })))
|
|
54
|
+
.deep.equal(expected.wireViolations.map(item => ({ id: item.ConstraintName,
|
|
55
|
+
message: `Taken: ${item.Details.PropertyName}=${item.Details.PropertyValue}`, details: item.Details })));
|
|
56
|
+
}
|
|
57
|
+
if (!expected.success) should(scenario.appendedEvents).deep.equal(before);
|
|
58
|
+
else should(scenario.appendedEvents.slice(before.length).map(entry => entry.context.eventSourceId))
|
|
59
|
+
.deep.equal(operation.events.map(entry => entry.source));
|
|
60
|
+
should((await scenario.eventSequence.getNextSequenceNumber()).value.toString()).equal(expected.next);
|
|
61
|
+
}
|
|
62
|
+
} finally { validate.mockRestore(); }
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
for (const shape of [{ name: 'scoped', eventTypes, constraints: [fixtures.scoped], eventType: fixtures.number },
|
|
67
|
+
...fixtures.rejectedDefinitions]) {
|
|
68
|
+
describe(`when a ${shape.name} number attempts to use the new unscoped fractional domain`, () => {
|
|
69
|
+
let scenario: EventScenario;
|
|
70
|
+
let error: unknown;
|
|
71
|
+
let history: EventScenario['appendedEvents'];
|
|
72
|
+
let results: EventScenario['results'];
|
|
73
|
+
let next: bigint;
|
|
74
|
+
beforeEach(async () => {
|
|
75
|
+
scenario = new EventScenario({ artifacts: { eventTypes: shape.eventTypes, constraints: shape.constraints } });
|
|
76
|
+
should((await scenario.append('Existing', new shape.eventType())).isSuccess).equal(true);
|
|
77
|
+
history = [...scenario.appendedEvents];
|
|
78
|
+
results = [...scenario.results];
|
|
79
|
+
next = (await scenario.eventSequence.getNextSequenceNumber()).value;
|
|
80
|
+
const invalid = Object.assign(new shape.eventType(), { key: 1.5 });
|
|
81
|
+
error = await scenario.appendMany('A', [new shape.eventType(), invalid]).then(() => undefined, error => error);
|
|
82
|
+
});
|
|
83
|
+
it('should reject without changing existing history, results or sequence', async () => {
|
|
84
|
+
should(error instanceof UnsupportedEventSequenceOperation).equal(true);
|
|
85
|
+
should(scenario.appendedEvents).deep.equal(history);
|
|
86
|
+
should(scenario.results).deep.equal(results);
|
|
87
|
+
should((await scenario.eventSequence.getNextSequenceNumber()).value).equal(next);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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 } from '@cratis/fundamentals';
|
|
6
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
7
|
+
import { fieldKeyMeasurements } from './field_key_measurements.fixture.js';
|
|
8
|
+
|
|
9
|
+
class TextClaimed { @field(String) key = ''; }
|
|
10
|
+
eventType('OracleFieldText')(TextClaimed);
|
|
11
|
+
class DecimalClaimed { @field(Number) key = 0.1; }
|
|
12
|
+
eventType('OracleFieldDecimal')(DecimalClaimed);
|
|
13
|
+
class FloatClaimed { @field(Number) key = 0.1; }
|
|
14
|
+
eventType('OracleFieldFloat')(FloatClaimed);
|
|
15
|
+
|
|
16
|
+
fieldKeyMeasurements({ text: TextClaimed, decimal: DecimalClaimed, float: FloatClaimed });
|
|
@@ -0,0 +1,15 @@
|
|
|
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 } from '@cratis/fundamentals';
|
|
5
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
6
|
+
import { fieldKeyMeasurements } from './field_key_measurements.fixture.js';
|
|
7
|
+
|
|
8
|
+
@eventType('OracleFieldText')
|
|
9
|
+
class TextClaimed { @field(String) key = ''; }
|
|
10
|
+
@eventType('OracleFieldDecimal')
|
|
11
|
+
class DecimalClaimed { @field(Number) key = 0.1; }
|
|
12
|
+
@eventType('OracleFieldFloat')
|
|
13
|
+
class FloatClaimed { @field(Number) key = 0.1; }
|
|
14
|
+
|
|
15
|
+
fieldKeyMeasurements({ text: TextClaimed, decimal: DecimalClaimed, float: FloatClaimed });
|
|
@@ -0,0 +1,51 @@
|
|
|
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 } from '@cratis/fundamentals';
|
|
6
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
7
|
+
import { constraint } from '../../../events/constraints/constraint.js';
|
|
8
|
+
import type { IConstraintBuilder } from '../../../events/constraints/IConstraintBuilder.js';
|
|
9
|
+
import { fractionalCollisions } from './fractional_collisions.fixture.js';
|
|
10
|
+
|
|
11
|
+
class NumberClaimed { @field(Number) key = 1; }
|
|
12
|
+
eventType('OracleCombinedNumber')(NumberClaimed);
|
|
13
|
+
class TextClaimed { @field(String) key = ''; }
|
|
14
|
+
eventType('OracleCombinedText')(TextClaimed);
|
|
15
|
+
class PairClaimed { @field(Number) key = 1; @field(Number) suffix = 2; }
|
|
16
|
+
eventType('OracleFractionalPair')(PairClaimed);
|
|
17
|
+
class Removed {}
|
|
18
|
+
eventType('OracleFractionalRemoved')(Removed);
|
|
19
|
+
class SharedKey {
|
|
20
|
+
define(builder: IConstraintBuilder) {
|
|
21
|
+
builder.unique(key => key.on(NumberClaimed, event => event.key).on(TextClaimed, event => event.key)
|
|
22
|
+
.withMessage('Taken: {PropertyName}={PropertyValue}'));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
constraint('OracleFractionalKey')(SharedKey);
|
|
26
|
+
class ScopedKey extends SharedKey {
|
|
27
|
+
define(builder: IConstraintBuilder) { super.define(builder.perEventSourceType()); }
|
|
28
|
+
}
|
|
29
|
+
constraint('OracleFractionalKey')(ScopedKey);
|
|
30
|
+
class CompositeKey {
|
|
31
|
+
define(builder: IConstraintBuilder) { builder.unique(key => key.on(PairClaimed, event => event.key, event => event.suffix)); }
|
|
32
|
+
}
|
|
33
|
+
constraint('OracleFractionalComposite')(CompositeKey);
|
|
34
|
+
class RemovableKey {
|
|
35
|
+
define(builder: IConstraintBuilder) { builder.unique(key => key.on(NumberClaimed, event => event.key).removedWith(Removed)); }
|
|
36
|
+
}
|
|
37
|
+
constraint('OracleFractionalRemovable')(RemovableKey);
|
|
38
|
+
class OverlapLeft {
|
|
39
|
+
define(builder: IConstraintBuilder) { builder.unique(key => key.on(PairClaimed, event => event.key)); }
|
|
40
|
+
}
|
|
41
|
+
constraint('OracleFractionalOverlapLeft')(OverlapLeft);
|
|
42
|
+
class OverlapRight {
|
|
43
|
+
define(builder: IConstraintBuilder) { builder.unique(key => key.on(PairClaimed, event => event.suffix)); }
|
|
44
|
+
}
|
|
45
|
+
constraint('OracleFractionalOverlapRight')(OverlapRight);
|
|
46
|
+
|
|
47
|
+
fractionalCollisions({ number: NumberClaimed, text: TextClaimed, constraint: SharedKey, scoped: ScopedKey, rejectedDefinitions: [
|
|
48
|
+
{ name: 'composite', eventTypes: [PairClaimed], constraints: [CompositeKey], eventType: PairClaimed },
|
|
49
|
+
{ name: 'removable', eventTypes: [NumberClaimed, Removed], constraints: [RemovableKey], eventType: NumberClaimed },
|
|
50
|
+
{ name: 'overlapping', eventTypes: [PairClaimed], constraints: [OverlapLeft, OverlapRight], eventType: PairClaimed }
|
|
51
|
+
] });
|
|
@@ -0,0 +1,50 @@
|
|
|
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 } from '@cratis/fundamentals';
|
|
5
|
+
import { eventType } from '../../../events/eventTypeDecorator.js';
|
|
6
|
+
import { constraint } from '../../../events/constraints/constraint.js';
|
|
7
|
+
import type { IConstraintBuilder } from '../../../events/constraints/IConstraintBuilder.js';
|
|
8
|
+
import { fractionalCollisions } from './fractional_collisions.fixture.js';
|
|
9
|
+
|
|
10
|
+
@eventType('OracleCombinedNumber')
|
|
11
|
+
class NumberClaimed { @field(Number) key = 1; }
|
|
12
|
+
@eventType('OracleCombinedText')
|
|
13
|
+
class TextClaimed { @field(String) key = ''; }
|
|
14
|
+
@eventType('OracleFractionalPair')
|
|
15
|
+
class PairClaimed { @field(Number) key = 1; @field(Number) suffix = 2; }
|
|
16
|
+
@eventType('OracleFractionalRemoved')
|
|
17
|
+
class Removed {}
|
|
18
|
+
@constraint('OracleFractionalKey')
|
|
19
|
+
class SharedKey {
|
|
20
|
+
define(builder: IConstraintBuilder) {
|
|
21
|
+
builder.unique(key => key.on(NumberClaimed, event => event.key).on(TextClaimed, event => event.key)
|
|
22
|
+
.withMessage('Taken: {PropertyName}={PropertyValue}'));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
@constraint('OracleFractionalKey')
|
|
26
|
+
class ScopedKey extends SharedKey {
|
|
27
|
+
define(builder: IConstraintBuilder) { super.define(builder.perEventSourceType()); }
|
|
28
|
+
}
|
|
29
|
+
@constraint('OracleFractionalComposite')
|
|
30
|
+
class CompositeKey {
|
|
31
|
+
define(builder: IConstraintBuilder) { builder.unique(key => key.on(PairClaimed, event => event.key, event => event.suffix)); }
|
|
32
|
+
}
|
|
33
|
+
@constraint('OracleFractionalRemovable')
|
|
34
|
+
class RemovableKey {
|
|
35
|
+
define(builder: IConstraintBuilder) { builder.unique(key => key.on(NumberClaimed, event => event.key).removedWith(Removed)); }
|
|
36
|
+
}
|
|
37
|
+
@constraint('OracleFractionalOverlapLeft')
|
|
38
|
+
class OverlapLeft {
|
|
39
|
+
define(builder: IConstraintBuilder) { builder.unique(key => key.on(PairClaimed, event => event.key)); }
|
|
40
|
+
}
|
|
41
|
+
@constraint('OracleFractionalOverlapRight')
|
|
42
|
+
class OverlapRight {
|
|
43
|
+
define(builder: IConstraintBuilder) { builder.unique(key => key.on(PairClaimed, event => event.suffix)); }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fractionalCollisions({ number: NumberClaimed, text: TextClaimed, constraint: SharedKey, scoped: ScopedKey, rejectedDefinitions: [
|
|
47
|
+
{ name: 'composite', eventTypes: [PairClaimed], constraints: [CompositeKey], eventType: PairClaimed },
|
|
48
|
+
{ name: 'removable', eventTypes: [NumberClaimed, Removed], constraints: [RemovableKey], eventType: NumberClaimed },
|
|
49
|
+
{ name: 'overlapping', eventTypes: [PairClaimed], constraints: [OverlapLeft, OverlapRight], eventType: PairClaimed }
|
|
50
|
+
] });
|
package/testing/for_EventScenario/when_appending_field_types/with_unproven_schemas.legacy.spec.ts
CHANGED
|
@@ -15,7 +15,7 @@ chai.should();
|
|
|
15
15
|
|
|
16
16
|
for (const schema of [
|
|
17
17
|
{ type: 'string', format: 'date' }, { type: 'string', format: 'date-time' },
|
|
18
|
-
{ type: 'object' }, { type: 'integer', format: 'int64' }, { type: '
|
|
18
|
+
{ type: 'object' }, { type: 'integer', format: 'int64' }, { type: 'integer', format: 'uint64' }
|
|
19
19
|
] satisfies JsonSchema[]) {
|
|
20
20
|
describe(`when registering a ${schema.type}/${schema.format ?? 'unformatted'} constrained schema`, () => {
|
|
21
21
|
it('should reject the constraint without rejecting an unconstrained field', async () => {
|