@reventlessdev/reventless-core 3.0.0-alpha.327 → 3.0.0-alpha.329
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/CHANGELOG.md +18 -0
- package/package.json +8 -8
- package/src/CommandSent.res +22 -0
- package/src/CommandSent.res.mjs +22 -0
- package/src/Projection.res +26 -1
- package/src/Projection.res.mjs +19 -1
- package/src/RowChange.res +37 -0
- package/src/RowChange.res.mjs +62 -0
- package/src/components/Api/GraphQL_FragmentGenerator.res +22 -0
- package/src/components/Api/GraphQL_FragmentGenerator.res.mjs +20 -4
- package/src/components/Api/GraphQL_SchemaInspector.res.mjs +2 -2
- package/src/components/AutomationSlice/AutomationSlice_Callback.res +1 -0
- package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +4 -2
- package/src/components/CommandGenerator/CommandGenerator_Callback.res +2 -1
- package/src/components/CommandGenerator/CommandGenerator_Callback.res.mjs +1 -1
- package/src/components/CommandTopic/CommandTopic.res.mjs +6 -0
- package/src/components/CommandTopic/CommandTopic_Helpers.res +44 -2
- package/src/components/CommandTopic/CommandTopic_Helpers.res.mjs +69 -11
- package/src/components/EventMapper/EventMapper_Callback.res +8 -2
- package/src/components/EventMapper/EventMapper_Callback.res.mjs +3 -2
- package/src/components/ExtensionPoint/ExtensionPoint_Callback.res +5 -1
- package/src/components/ExtensionPoint/ExtensionPoint_Callback.res.mjs +2 -1
- package/src/components/InboundTranslationSlice/InboundTranslationSlice_Callback.res +4 -0
- package/src/components/InboundTranslationSlice/InboundTranslationSlice_Callback.res.mjs +3 -1
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +4 -0
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +4 -2
- package/src/components/QueryDb/QueryDb_Operations.res +2 -0
- package/src/components/QueryDb/QueryDb_Operations.res.mjs +3 -0
- package/src/components/Task/Task_Builder.res +5 -3
- package/src/components/Task/Task_Builder.res.mjs +2 -1
- package/src/plugin/component/Plugin_Callback.res +6 -2
- package/src/plugin/component/Plugin_Callback.res.mjs +3 -2
- package/src/util/Metrics.res +151 -16
- package/src/util/Metrics.res.mjs +123 -3
- package/tests/api/GraphQL_FragmentGeneratorTest.res.mjs +2 -2
- package/tests/api/IdentitySemanticTest.res.mjs +1 -1
- package/tests/commandgenerator/CommandGeneratorCallbackTest.res +33 -0
- package/tests/commandgenerator/CommandGeneratorCallbackTest.res.mjs +51 -0
- package/tests/commandtopic/CommandOutcomeLineTest.res +121 -0
- package/tests/commandtopic/CommandOutcomeLineTest.res.mjs +149 -0
- package/tests/querydb/RowChangeTest.res +87 -0
- package/tests/querydb/RowChangeTest.res.mjs +157 -0
- package/tests/util/MetricsTest.res +72 -0
- package/tests/util/MetricsTest.res.mjs +108 -0
package/src/util/Metrics.res.mjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
|
+
import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
|
|
4
|
+
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
|
|
5
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
3
6
|
import * as AnsiStyle$Reventless from "@reventlessdev/reventless-spec/src/AnsiStyle.res.mjs";
|
|
7
|
+
import * as LogPrefix$Reventless from "@reventlessdev/reventless-spec/src/LogPrefix.res.mjs";
|
|
4
8
|
|
|
5
9
|
let discriminator = "reventlessMetric";
|
|
6
10
|
|
|
@@ -26,17 +30,133 @@ function metricLine(metric, slice, valueOpt) {
|
|
|
26
30
|
]);
|
|
27
31
|
}
|
|
28
32
|
|
|
29
|
-
function
|
|
30
|
-
let value = valueOpt !== undefined ? valueOpt : 1;
|
|
33
|
+
function line(s) {
|
|
31
34
|
if (AnsiStyle$Reventless.isJsonSink()) {
|
|
32
|
-
console.log(JSON.stringify(metricLine(metric, slice, value)));
|
|
35
|
+
console.log(JSON.stringify(metricLine(s.metric, s.slice, s.value)));
|
|
33
36
|
return;
|
|
34
37
|
}
|
|
35
38
|
}
|
|
36
39
|
|
|
40
|
+
let current = {
|
|
41
|
+
contents: line
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function install(sink) {
|
|
45
|
+
current.contents = sink;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function reset() {
|
|
49
|
+
current.contents = line;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let Sink = {
|
|
53
|
+
line: line,
|
|
54
|
+
current: current,
|
|
55
|
+
install: install,
|
|
56
|
+
reset: reset
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
function emitCount(metric, slice, valueOpt) {
|
|
60
|
+
let value = valueOpt !== undefined ? valueOpt : 1;
|
|
61
|
+
current.contents({
|
|
62
|
+
metric: metric,
|
|
63
|
+
slice: slice,
|
|
64
|
+
plugin: LogPrefix$Reventless.lookup(slice),
|
|
65
|
+
value: value,
|
|
66
|
+
unit: "Count",
|
|
67
|
+
at: Date.now()
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function make() {
|
|
72
|
+
return {
|
|
73
|
+
entries: {}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function clear(t) {
|
|
78
|
+
Object.keys(t.entries).forEach(k => Stdlib_Dict.$$delete(t.entries, k));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function minuteOf(at) {
|
|
82
|
+
return Math.floor(at / 60000);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function rolled(perMinute, from, to_) {
|
|
86
|
+
let gap = Math.min(60, to_ - from | 0);
|
|
87
|
+
if (gap <= 0) {
|
|
88
|
+
return perMinute;
|
|
89
|
+
} else {
|
|
90
|
+
return perMinute.slice(gap, 60).concat(Stdlib_Array.make(gap, 0));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function add(t, s) {
|
|
95
|
+
let plugin = Stdlib_Option.getOr(s.plugin, "");
|
|
96
|
+
let key = s.metric + `|` + plugin + `|` + s.slice;
|
|
97
|
+
let minute = Math.floor(s.at / 60000);
|
|
98
|
+
let e = t.entries[key];
|
|
99
|
+
let e$1;
|
|
100
|
+
if (e !== undefined) {
|
|
101
|
+
e$1 = e;
|
|
102
|
+
} else {
|
|
103
|
+
let e$2 = {
|
|
104
|
+
metric: s.metric,
|
|
105
|
+
plugin: plugin,
|
|
106
|
+
slice: s.slice,
|
|
107
|
+
unit: s.unit,
|
|
108
|
+
count: 0,
|
|
109
|
+
sum: 0,
|
|
110
|
+
lastAt: s.at,
|
|
111
|
+
perMinute: Stdlib_Array.make(60, 0),
|
|
112
|
+
minute: minute
|
|
113
|
+
};
|
|
114
|
+
t.entries[key] = e$2;
|
|
115
|
+
e$1 = e$2;
|
|
116
|
+
}
|
|
117
|
+
e$1.perMinute = rolled(e$1.perMinute, e$1.minute, minute);
|
|
118
|
+
e$1.minute = Math.max(e$1.minute, minute);
|
|
119
|
+
let last = 59;
|
|
120
|
+
e$1.perMinute[last] = e$1.perMinute[last] + 1 | 0;
|
|
121
|
+
e$1.count = e$1.count + 1 | 0;
|
|
122
|
+
e$1.sum = e$1.sum + s.value;
|
|
123
|
+
e$1.lastAt = Math.max(e$1.lastAt, s.at);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function sink(t) {
|
|
127
|
+
return s => add(t, s);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function read(t, now, plugins) {
|
|
131
|
+
return Object.values(t.entries).filter(e => Stdlib_Option.mapOr(plugins, true, ps => ps.includes(e.plugin))).map(e => ({
|
|
132
|
+
metric: e.metric,
|
|
133
|
+
plugin: e.plugin,
|
|
134
|
+
slice: e.slice,
|
|
135
|
+
unit: e.unit,
|
|
136
|
+
count: e.count,
|
|
137
|
+
sum: e.sum,
|
|
138
|
+
lastAt: e.lastAt,
|
|
139
|
+
perMinute: rolled(e.perMinute, e.minute, Math.floor(now / 60000))
|
|
140
|
+
})).toSorted((a, b) => (a.plugin + `|` + a.slice + `|` + a.metric).localeCompare(b.plugin + `|` + b.slice + `|` + b.metric));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
let Counts = {
|
|
144
|
+
minutes: 60,
|
|
145
|
+
minuteMs: 60000,
|
|
146
|
+
make: make,
|
|
147
|
+
clear: clear,
|
|
148
|
+
minuteOf: minuteOf,
|
|
149
|
+
rolled: rolled,
|
|
150
|
+
add: add,
|
|
151
|
+
sink: sink,
|
|
152
|
+
read: read
|
|
153
|
+
};
|
|
154
|
+
|
|
37
155
|
export {
|
|
38
156
|
discriminator,
|
|
39
157
|
metricLine,
|
|
158
|
+
Sink,
|
|
40
159
|
emitCount,
|
|
160
|
+
Counts,
|
|
41
161
|
}
|
|
42
162
|
/* AnsiStyle-Reventless Not a pure module */
|
|
@@ -361,7 +361,7 @@ globalThis.describe("semantic composites are named once, not once per field", ()
|
|
|
361
361
|
globalThis.expect(fragmentTypes.some(t => t.includes("AddProductPrice"))).toBe(false);
|
|
362
362
|
});
|
|
363
363
|
globalThis.test("the output position keeps the bare name", () => {
|
|
364
|
-
let types = GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Catalog_Product", undefined, false, undefined, Sury.$schema(s => ({
|
|
364
|
+
let types = GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Catalog_Product", undefined, false, undefined, undefined, Sury.$schema(s => ({
|
|
365
365
|
productId: s.m(Sury.string),
|
|
366
366
|
price: s.m(Money$Reventless.schema)
|
|
367
367
|
})));
|
|
@@ -579,7 +579,7 @@ globalThis.describe("the by-index door", () => {
|
|
|
579
579
|
});
|
|
580
580
|
|
|
581
581
|
globalThis.describe("@internal keeps a field off the generated type", () => {
|
|
582
|
-
let types = excludeFields => GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Platform_Widget", excludeFields, undefined, undefined, PsInternalFieldsReadModel$ReventlessCore.stateSchema).join("\n");
|
|
582
|
+
let types = excludeFields => GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Platform_Widget", excludeFields, undefined, undefined, undefined, PsInternalFieldsReadModel$ReventlessCore.stateSchema).join("\n");
|
|
583
583
|
globalThis.test("the declared fields are absent", () => {
|
|
584
584
|
globalThis.expect([
|
|
585
585
|
types(undefined).includes("eventCollector"),
|
|
@@ -49,7 +49,7 @@ globalThis.describe("the identity semantic on the JSON Schema", () => {
|
|
|
49
49
|
});
|
|
50
50
|
|
|
51
51
|
globalThis.describe("the identity semantic in the SDL", () => {
|
|
52
|
-
let sdl = GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Ordering_Order", undefined, false, undefined, orderSchema).join("\n");
|
|
52
|
+
let sdl = GraphQL_FragmentGenerator$ReventlessCore.deriveObjectTypeWithNested("Ordering_Order", undefined, false, undefined, undefined, orderSchema).join("\n");
|
|
53
53
|
globalThis.test("an identity field not named `*Id` is an ID", () => {
|
|
54
54
|
globalThis.expect(sdl.includes("buyer: ID!")).toBe(true);
|
|
55
55
|
});
|
|
@@ -286,3 +286,36 @@ describe("CommandGenerator_Callback.generateCommand:", () => {
|
|
|
286
286
|
)
|
|
287
287
|
})
|
|
288
288
|
})
|
|
289
|
+
|
|
290
|
+
type consoleObj = {mutable log: string => unit}
|
|
291
|
+
@val external _console: consoleObj = "console"
|
|
292
|
+
|
|
293
|
+
testPromise("the line of a generated command names the door it came through", async () => {
|
|
294
|
+
let captured = []
|
|
295
|
+
let original = _console.log
|
|
296
|
+
_console.log = s => captured->Array.push(s)
|
|
297
|
+
let payload: CommandGenerator.payload = {
|
|
298
|
+
...makeZeroParamPayload(~id="agg-1", ~command="Create"),
|
|
299
|
+
meta: {user: "test-user", info: "Mutation.Shop_Create"},
|
|
300
|
+
}
|
|
301
|
+
let result = try {
|
|
302
|
+
Ok(await TestGenerator.generateCommand(payload)->Effect.runPromise)
|
|
303
|
+
} catch {
|
|
304
|
+
| e => Error(e)
|
|
305
|
+
}
|
|
306
|
+
_console.log = original
|
|
307
|
+
result->Result.getOrThrow->ignore
|
|
308
|
+
let via = captured->Array.findMap(s =>
|
|
309
|
+
switch JSON.parseOrThrow(s) {
|
|
310
|
+
| JSON.Object(d)
|
|
311
|
+
if d
|
|
312
|
+
->Dict.get("message")
|
|
313
|
+
->Option.flatMap(JSON.Decode.string)
|
|
314
|
+
->Option.mapOr(false, m => m->String.startsWith("generated command")) =>
|
|
315
|
+
d->Dict.get("via")->Option.flatMap(JSON.Decode.string)
|
|
316
|
+
| _ => None
|
|
317
|
+
| exception _ => None
|
|
318
|
+
}
|
|
319
|
+
)
|
|
320
|
+
expect(via)->toEqual(Some("Mutation.Shop_Create"))
|
|
321
|
+
})
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
3
|
import * as Stdlib_JSON from "@rescript/runtime/lib/es6/Stdlib_JSON.js";
|
|
4
|
+
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
|
|
4
5
|
import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
|
|
5
6
|
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
7
|
+
import * as Stdlib_Result from "@rescript/runtime/lib/es6/Stdlib_Result.js";
|
|
6
8
|
import * as Effect from "effect/Effect";
|
|
7
9
|
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
8
10
|
import * as Plugin_ResolverError$ReventlessCore from "../../src/plugin/component/Plugin_ResolverError.res.mjs";
|
|
@@ -298,4 +300,53 @@ globalThis.describe("CommandGenerator_Callback.generateCommand:", () => {
|
|
|
298
300
|
});
|
|
299
301
|
});
|
|
300
302
|
|
|
303
|
+
globalThis.test("the line of a generated command names the door it came through", async () => {
|
|
304
|
+
let captured = [];
|
|
305
|
+
let original = console.log;
|
|
306
|
+
console.log = s => {
|
|
307
|
+
captured.push(s);
|
|
308
|
+
};
|
|
309
|
+
let init = CommandGeneratorFixtures$ReventlessCore.makeZeroParamPayload("agg-1", "Create");
|
|
310
|
+
let payload_command = init.command;
|
|
311
|
+
let payload_arguments = init.arguments;
|
|
312
|
+
let payload_meta = {
|
|
313
|
+
user: "test-user",
|
|
314
|
+
info: "Mutation.Shop_Create"
|
|
315
|
+
};
|
|
316
|
+
let payload_identity = init.identity;
|
|
317
|
+
let payload = {
|
|
318
|
+
command: payload_command,
|
|
319
|
+
arguments: payload_arguments,
|
|
320
|
+
meta: payload_meta,
|
|
321
|
+
identity: payload_identity
|
|
322
|
+
};
|
|
323
|
+
let result;
|
|
324
|
+
try {
|
|
325
|
+
result = {
|
|
326
|
+
TAG: "Ok",
|
|
327
|
+
_0: await Effect.runPromise(CommandGeneratorFixtures$ReventlessCore.TestGenerator.generateCommand(payload))
|
|
328
|
+
};
|
|
329
|
+
} catch (raw_e) {
|
|
330
|
+
let e = Primitive_exceptions.internalToException(raw_e);
|
|
331
|
+
result = {
|
|
332
|
+
TAG: "Error",
|
|
333
|
+
_0: e
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
console.log = original;
|
|
337
|
+
Stdlib_Result.getOrThrow(result, undefined);
|
|
338
|
+
let via = Stdlib_Array.findMap(captured, s => {
|
|
339
|
+
let d;
|
|
340
|
+
try {
|
|
341
|
+
d = JSON.parse(s);
|
|
342
|
+
} catch (exn) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (typeof d === "object" && d !== null && !Array.isArray(d) && Stdlib_Option.mapOr(Stdlib_Option.flatMap(d["message"], Stdlib_JSON.Decode.string), false, m => m.startsWith("generated command"))) {
|
|
346
|
+
return Stdlib_Option.flatMap(d["via"], Stdlib_JSON.Decode.string);
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
globalThis.expect(via).toEqual("Mutation.Shop_Create");
|
|
350
|
+
});
|
|
351
|
+
|
|
301
352
|
/* Not a pure module */
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Each command outcome is written as one info line with the command's ids, which is where a
|
|
2
|
+
// deployed platform's tools read what became of a command; a platform that carries outcomes
|
|
3
|
+
// another way turns the lines off.
|
|
4
|
+
|
|
5
|
+
open JestGlobals
|
|
6
|
+
|
|
7
|
+
type consoleObj = {mutable log: string => unit}
|
|
8
|
+
@val external _console: consoleObj = "console"
|
|
9
|
+
|
|
10
|
+
let captureLogs = (fn: unit => unit): array<JSON.t> => {
|
|
11
|
+
let captured = []
|
|
12
|
+
let original = _console.log
|
|
13
|
+
_console.log = s => captured->Array.push(s)
|
|
14
|
+
let result = try {
|
|
15
|
+
fn()
|
|
16
|
+
Ok()
|
|
17
|
+
} catch {
|
|
18
|
+
| e => Error(e)
|
|
19
|
+
}
|
|
20
|
+
_console.log = original
|
|
21
|
+
switch result {
|
|
22
|
+
| Ok() =>
|
|
23
|
+
captured->Array.filterMap(s =>
|
|
24
|
+
try Some(JSON.parseOrThrow(s)) catch {
|
|
25
|
+
| _ => None
|
|
26
|
+
}
|
|
27
|
+
)
|
|
28
|
+
| Error(e) => throw(e)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let field = (j: JSON.t, path: array<string>): option<string> =>
|
|
33
|
+
path
|
|
34
|
+
->Array.reduce(Some(j), (at, key) =>
|
|
35
|
+
at->Option.flatMap(JSON.Decode.object)->Option.flatMap(d => d->Dict.get(key))
|
|
36
|
+
)
|
|
37
|
+
->Option.flatMap(v =>
|
|
38
|
+
switch v {
|
|
39
|
+
| JSON.String(s) => Some(s)
|
|
40
|
+
| JSON.Number(n) => Some(n->Float.toString)
|
|
41
|
+
| _ => None
|
|
42
|
+
}
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
let outcomeLines = lines =>
|
|
46
|
+
lines->Array.filter(l =>
|
|
47
|
+
switch field(l, ["message"]) {
|
|
48
|
+
| Some("command accepted" | "command rejected") => true
|
|
49
|
+
| _ => false
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
testSync("an accepted command is written with its ids, entity and event count", () => {
|
|
54
|
+
let lines =
|
|
55
|
+
captureLogs(() =>
|
|
56
|
+
CommandTopic_Helpers.fireCommandOutcome(
|
|
57
|
+
~component="Order",
|
|
58
|
+
~reference="c-1",
|
|
59
|
+
~correlationId="c-1",
|
|
60
|
+
OutcomeAccepted({entityId: Some("o-1"), eventCount: 2}),
|
|
61
|
+
)
|
|
62
|
+
)->outcomeLines
|
|
63
|
+
expect(
|
|
64
|
+
lines->Array.map(l => (
|
|
65
|
+
field(l, ["message"]),
|
|
66
|
+
field(l, ["comp"]),
|
|
67
|
+
field(l, ["msgId"]),
|
|
68
|
+
field(l, ["correlationId"]),
|
|
69
|
+
field(l, ["detail", "entityId"]),
|
|
70
|
+
field(l, ["detail", "eventCount"]),
|
|
71
|
+
)),
|
|
72
|
+
)->toEqual([
|
|
73
|
+
(
|
|
74
|
+
Some("command accepted"),
|
|
75
|
+
Some("CommandTopic(Order)"),
|
|
76
|
+
Some("c-1"),
|
|
77
|
+
Some("c-1"),
|
|
78
|
+
Some("o-1"),
|
|
79
|
+
Some("2"),
|
|
80
|
+
),
|
|
81
|
+
])
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
testSync("a rejected command is written with its code and cause", () => {
|
|
85
|
+
let lines =
|
|
86
|
+
captureLogs(() =>
|
|
87
|
+
CommandTopic_Helpers.fireCommandOutcome(
|
|
88
|
+
~component="Order",
|
|
89
|
+
~reference="c-2",
|
|
90
|
+
~correlationId="k-1",
|
|
91
|
+
~causationId="e-1",
|
|
92
|
+
OutcomeRejected({errorCode: "Forbidden", errorDetail: "secret", cause: AccessRefusal}),
|
|
93
|
+
)
|
|
94
|
+
)->outcomeLines
|
|
95
|
+
expect(
|
|
96
|
+
lines->Array.map(l => (
|
|
97
|
+
field(l, ["message"]),
|
|
98
|
+
field(l, ["level"]),
|
|
99
|
+
field(l, ["causationId"]),
|
|
100
|
+
field(l, ["detail", "errorCode"]),
|
|
101
|
+
field(l, ["detail", "cause"]),
|
|
102
|
+
field(l, ["detail", "errorDetail"]),
|
|
103
|
+
)),
|
|
104
|
+
)->toEqual([
|
|
105
|
+
(Some("command rejected"), Some("INFO"), Some("e-1"), Some("Forbidden"), Some("ACCESS"), None),
|
|
106
|
+
])
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
testSync("a platform that carries outcomes another way writes no line", () => {
|
|
110
|
+
CommandTopic_Helpers.outcomeLines := false
|
|
111
|
+
let lines =
|
|
112
|
+
captureLogs(() =>
|
|
113
|
+
CommandTopic_Helpers.fireCommandOutcome(
|
|
114
|
+
~component="Order",
|
|
115
|
+
~reference="c-3",
|
|
116
|
+
OutcomeAccepted({entityId: None, eventCount: 1}),
|
|
117
|
+
)
|
|
118
|
+
)->outcomeLines
|
|
119
|
+
CommandTopic_Helpers.outcomeLines := true
|
|
120
|
+
expect(lines)->toEqual([])
|
|
121
|
+
})
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Stdlib_JSON from "@rescript/runtime/lib/es6/Stdlib_JSON.js";
|
|
4
|
+
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
|
|
5
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
6
|
+
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
7
|
+
import * as CommandTopic_Helpers$ReventlessCore from "../../src/components/CommandTopic/CommandTopic_Helpers.res.mjs";
|
|
8
|
+
|
|
9
|
+
function captureLogs(fn) {
|
|
10
|
+
let captured = [];
|
|
11
|
+
let original = console.log;
|
|
12
|
+
console.log = s => {
|
|
13
|
+
captured.push(s);
|
|
14
|
+
};
|
|
15
|
+
let result;
|
|
16
|
+
try {
|
|
17
|
+
fn();
|
|
18
|
+
result = {
|
|
19
|
+
TAG: "Ok",
|
|
20
|
+
_0: undefined
|
|
21
|
+
};
|
|
22
|
+
} catch (raw_e) {
|
|
23
|
+
let e = Primitive_exceptions.internalToException(raw_e);
|
|
24
|
+
result = {
|
|
25
|
+
TAG: "Error",
|
|
26
|
+
_0: e
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
console.log = original;
|
|
30
|
+
if (result.TAG === "Ok") {
|
|
31
|
+
return Stdlib_Array.filterMap(captured, s => {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(s);
|
|
34
|
+
} catch (exn) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
throw result._0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function field(j, path) {
|
|
43
|
+
return Stdlib_Option.flatMap(Stdlib_Array.reduce(path, j, (at, key) => Stdlib_Option.flatMap(Stdlib_Option.flatMap(at, Stdlib_JSON.Decode.object), d => d[key])), v => {
|
|
44
|
+
switch (typeof v) {
|
|
45
|
+
case "string" :
|
|
46
|
+
return v;
|
|
47
|
+
case "number" :
|
|
48
|
+
return v.toString();
|
|
49
|
+
default:
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function outcomeLines(lines) {
|
|
56
|
+
return lines.filter(l => {
|
|
57
|
+
let match = field(l, ["message"]);
|
|
58
|
+
if (match === undefined) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
switch (match) {
|
|
62
|
+
case "command accepted" :
|
|
63
|
+
case "command rejected" :
|
|
64
|
+
return true;
|
|
65
|
+
default:
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
globalThis.test("an accepted command is written with its ids, entity and event count", () => {
|
|
72
|
+
let lines = outcomeLines(captureLogs(() => CommandTopic_Helpers$ReventlessCore.fireCommandOutcome("Order", "c-1", "c-1", undefined, {
|
|
73
|
+
TAG: "OutcomeAccepted",
|
|
74
|
+
entityId: "o-1",
|
|
75
|
+
eventCount: 2
|
|
76
|
+
})));
|
|
77
|
+
globalThis.expect(lines.map(l => [
|
|
78
|
+
field(l, ["message"]),
|
|
79
|
+
field(l, ["comp"]),
|
|
80
|
+
field(l, ["msgId"]),
|
|
81
|
+
field(l, ["correlationId"]),
|
|
82
|
+
field(l, [
|
|
83
|
+
"detail",
|
|
84
|
+
"entityId"
|
|
85
|
+
]),
|
|
86
|
+
field(l, [
|
|
87
|
+
"detail",
|
|
88
|
+
"eventCount"
|
|
89
|
+
])
|
|
90
|
+
])).toEqual([[
|
|
91
|
+
"command accepted",
|
|
92
|
+
"CommandTopic(Order)",
|
|
93
|
+
"c-1",
|
|
94
|
+
"c-1",
|
|
95
|
+
"o-1",
|
|
96
|
+
"2"
|
|
97
|
+
]]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
globalThis.test("a rejected command is written with its code and cause", () => {
|
|
101
|
+
let lines = outcomeLines(captureLogs(() => CommandTopic_Helpers$ReventlessCore.fireCommandOutcome("Order", "c-2", "k-1", "e-1", {
|
|
102
|
+
TAG: "OutcomeRejected",
|
|
103
|
+
errorCode: "Forbidden",
|
|
104
|
+
errorDetail: "secret",
|
|
105
|
+
cause: "AccessRefusal"
|
|
106
|
+
})));
|
|
107
|
+
globalThis.expect(lines.map(l => [
|
|
108
|
+
field(l, ["message"]),
|
|
109
|
+
field(l, ["level"]),
|
|
110
|
+
field(l, ["causationId"]),
|
|
111
|
+
field(l, [
|
|
112
|
+
"detail",
|
|
113
|
+
"errorCode"
|
|
114
|
+
]),
|
|
115
|
+
field(l, [
|
|
116
|
+
"detail",
|
|
117
|
+
"cause"
|
|
118
|
+
]),
|
|
119
|
+
field(l, [
|
|
120
|
+
"detail",
|
|
121
|
+
"errorDetail"
|
|
122
|
+
])
|
|
123
|
+
])).toEqual([[
|
|
124
|
+
"command rejected",
|
|
125
|
+
"INFO",
|
|
126
|
+
"e-1",
|
|
127
|
+
"Forbidden",
|
|
128
|
+
"ACCESS",
|
|
129
|
+
undefined
|
|
130
|
+
]]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
globalThis.test("a platform that carries outcomes another way writes no line", () => {
|
|
134
|
+
CommandTopic_Helpers$ReventlessCore.outcomeLines.contents = false;
|
|
135
|
+
let lines = outcomeLines(captureLogs(() => CommandTopic_Helpers$ReventlessCore.fireCommandOutcome("Order", "c-3", undefined, undefined, {
|
|
136
|
+
TAG: "OutcomeAccepted",
|
|
137
|
+
entityId: undefined,
|
|
138
|
+
eventCount: 1
|
|
139
|
+
})));
|
|
140
|
+
CommandTopic_Helpers$ReventlessCore.outcomeLines.contents = true;
|
|
141
|
+
globalThis.expect(lines).toEqual([]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
export {
|
|
145
|
+
captureLogs,
|
|
146
|
+
field,
|
|
147
|
+
outcomeLines,
|
|
148
|
+
}
|
|
149
|
+
/* Not a pure module */
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// A view row carries the change that last wrote it: the flow, the event and the time, stamped
|
|
2
|
+
// by the encoder from what the projection is applying, and passed over when the row is read.
|
|
3
|
+
|
|
4
|
+
open JestGlobals
|
|
5
|
+
open QueryDbFixtures
|
|
6
|
+
|
|
7
|
+
module Ops = QueryDb_Operations.Make(
|
|
8
|
+
ItemQueryDbSpec,
|
|
9
|
+
{
|
|
10
|
+
let jsonOps = mockJsonOps
|
|
11
|
+
},
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
let _ = beforeEach(() => reset())
|
|
15
|
+
|
|
16
|
+
let ops: QueryDb.operations<string, ItemQueryDbSpec.state> = {
|
|
17
|
+
load: Ops.load,
|
|
18
|
+
loadStream: Ops.loadStream,
|
|
19
|
+
save: Ops.save,
|
|
20
|
+
saveBatch: Ops.saveBatch,
|
|
21
|
+
count: Ops.count,
|
|
22
|
+
delete: Ops.delete,
|
|
23
|
+
deleteBatch: Ops.deleteBatch,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let changeOf = (id: string): option<(option<string>, option<string>)> =>
|
|
27
|
+
store.contents
|
|
28
|
+
->Dict.get(id)
|
|
29
|
+
->Option.flatMap(rows => rows->Array.get(0))
|
|
30
|
+
->Option.flatMap(JSON.Decode.object)
|
|
31
|
+
->Option.flatMap(d => d->Dict.get(RowChange.key))
|
|
32
|
+
->Option.flatMap(JSON.Decode.object)
|
|
33
|
+
->Option.map(c => (
|
|
34
|
+
c->Dict.get("flow")->Option.flatMap(JSON.Decode.string),
|
|
35
|
+
c->Dict.get("msgId")->Option.flatMap(JSON.Decode.string),
|
|
36
|
+
))
|
|
37
|
+
|
|
38
|
+
let source = (~flow="flow-1", msgId): Projection.changeSource => {msgId, correlationId: flow}
|
|
39
|
+
|
|
40
|
+
describe("a row's last change", () => {
|
|
41
|
+
testPromise("is the last of the events the projection applied", async () => {
|
|
42
|
+
let _ = await Projection.handleAction(
|
|
43
|
+
~sources=[source("m-1"), source("m-2")],
|
|
44
|
+
Create("a", ({name: "A", count: 1}: ItemQueryDbSpec.state)),
|
|
45
|
+
ops,
|
|
46
|
+
None,
|
|
47
|
+
)
|
|
48
|
+
expect(changeOf("a"))->toEqual(Some((Some("flow-1"), Some("m-2"))))
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
testPromise("is not written by a save outside a projection", async () => {
|
|
52
|
+
let _ = await Ops.save("a", ({name: "A", count: 1}: ItemQueryDbSpec.state), Init, None)
|
|
53
|
+
expect(changeOf("a"))->toEqual(None)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
testPromise("is each row's own when a projection writes several at once", async () => {
|
|
57
|
+
let _ = await Promise.all([
|
|
58
|
+
Projection.handleAction(
|
|
59
|
+
~sources=[source(~flow="flow-a", "m-a")],
|
|
60
|
+
Create("a", ({name: "A", count: 1}: ItemQueryDbSpec.state)),
|
|
61
|
+
ops,
|
|
62
|
+
None,
|
|
63
|
+
),
|
|
64
|
+
Projection.handleAction(
|
|
65
|
+
~sources=[source(~flow="flow-b", "m-b")],
|
|
66
|
+
Create("b", ({name: "B", count: 1}: ItemQueryDbSpec.state)),
|
|
67
|
+
ops,
|
|
68
|
+
None,
|
|
69
|
+
),
|
|
70
|
+
])
|
|
71
|
+
expect((changeOf("a"), changeOf("b")))->toEqual((
|
|
72
|
+
Some((Some("flow-a"), Some("m-a"))),
|
|
73
|
+
Some((Some("flow-b"), Some("m-b"))),
|
|
74
|
+
))
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
testPromise("is passed over when the row is read back", async () => {
|
|
78
|
+
let _ = await Projection.handleAction(
|
|
79
|
+
~sources=[source("m-1")],
|
|
80
|
+
Create("a", ({name: "A", count: 1}: ItemQueryDbSpec.state)),
|
|
81
|
+
ops,
|
|
82
|
+
None,
|
|
83
|
+
)
|
|
84
|
+
let rows = await Ops.loadStream("a")->Stream.runCollect->Effect.runPromise
|
|
85
|
+
expect(rows->Array.map(r => (r.name, r.count)))->toEqual([("A", 1)])
|
|
86
|
+
})
|
|
87
|
+
})
|