@reventlessdev/reventless-core 3.0.0-alpha.328 → 3.0.0-alpha.330

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 CHANGED
@@ -3,6 +3,21 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 3.0.0-alpha.330 (2026-11-05)
7
+
8
+ ### Bug Fixes
9
+
10
+ * **aws:** a line logged outside its invocation's fiber still names the plugin ([8e6f553](https://github.com/ReventlessDev/reventless-core/commit/8e6f553cc8ac2e439862bf1f3636ac86a4a22137))
11
+
12
+
13
+ # 3.0.0-alpha.329 (2026-11-05)
14
+
15
+ ### Features
16
+
17
+ * each command's outcome is written as a line of its own ([84e5e3f](https://github.com/ReventlessDev/reventless-core/commit/84e5e3fa4d77e629c6f72d9079a8f82f6b26a5ac))
18
+ * the API's line names the door a command came through ([e1f707f](https://github.com/ReventlessDev/reventless-core/commit/e1f707f2bff6e6fe29c43557e294b86a07c075fc))
19
+
20
+
6
21
  # 3.0.0-alpha.328 (2026-11-02)
7
22
 
8
23
  ### Features
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reventlessdev/reventless-core",
3
- "version": "3.0.0-alpha.328",
3
+ "version": "3.0.0-alpha.330",
4
4
  "description": "Core package for Reventless framework",
5
5
  "license": "Apache-2.0",
6
6
  "jest": {
@@ -29,16 +29,16 @@
29
29
  "sury": "11.0.0",
30
30
  "uuid": "^13.0.0",
31
31
  "@reventlessdev/rescript-effect": "1.0.0-alpha.1",
32
- "@reventlessdev/rescript-hash-object": "1.2.0-alpha.15",
33
32
  "@reventlessdev/rescript-fast-csv": "2.0.0-alpha.26",
34
- "@reventlessdev/rescript-jest": "1.0.0-alpha.13",
33
+ "@reventlessdev/rescript-hash-object": "1.2.0-alpha.15",
35
34
  "@reventlessdev/rescript-node": "2.0.0-alpha.26",
35
+ "@reventlessdev/rescript-jest": "1.0.0-alpha.13",
36
36
  "@reventlessdev/rescript-pulumi-pulumi": "3.0.0-alpha.0",
37
+ "@reventlessdev/rescript-ssh2": "2.0.0-alpha.27",
37
38
  "@reventlessdev/rescript-uuid": "2.0.0-alpha.1",
39
+ "@reventlessdev/reventless-infra": "3.0.0-alpha.223",
38
40
  "@reventlessdev/reventless-interop": "3.0.0-alpha.42",
39
- "@reventlessdev/reventless-infra": "3.0.0-alpha.222",
40
- "@reventlessdev/reventless-spec": "3.0.0-alpha.193",
41
- "@reventlessdev/rescript-ssh2": "2.0.0-alpha.27"
41
+ "@reventlessdev/reventless-spec": "3.0.0-alpha.194"
42
42
  },
43
43
  "devDependencies": {
44
44
  "rescript": "12.3.1",
@@ -46,7 +46,23 @@ let annotateInvocation = (
46
46
  | Some(count) if count > 1 => eff->Effect.annotateLogs("retryCount", count->Int.toString)
47
47
  | _ => eff
48
48
  }
49
+ // A line run on a fiber of its own (`->Effect.runSync`) misses these annotations, and inside a
50
+ // Lambda the component→plugin registry is empty: it names its plugin from the ambient one,
51
+ // set for the invocation and restored after it.
52
+ let withAmbientPlugin = eff =>
53
+ switch resolvedPlugin {
54
+ | None => eff
55
+ | Some(p) =>
56
+ Effect.sync(() => {
57
+ let before = Reventless.LogPrefix.currentPluginName.contents
58
+ Reventless.LogPrefix.currentPluginName := Some(p)
59
+ before
60
+ })->Effect.flatMap(before =>
61
+ eff->Effect.ensuring(Effect.sync(() => Reventless.LogPrefix.currentPluginName := before))
62
+ )
63
+ }
49
64
  effect
65
+ ->withAmbientPlugin
50
66
  ->Effect.annotateLogs("correlationId", cid)
51
67
  ->annotateOpt("comp", comp)
52
68
  ->annotateOpt("causationId", causationId)
@@ -22,7 +22,20 @@ function annotateInvocation(effect, correlationId, causationId, comp, pluginName
22
22
  return eff;
23
23
  }
24
24
  };
25
- return Effect.provideService(annotateRetry(annotateOpt(annotateOpt(Effect.annotateLogs(effect, "correlationId", cid), "comp", comp), "causationId", causationId)), RequestContext$ReventlessCore.tag, RequestContext$ReventlessCore.make(cid, causationId, comp, resolvedPlugin, timestamp, retryCount, identity, claims));
25
+ let withAmbientPlugin = eff => {
26
+ if (resolvedPlugin !== undefined) {
27
+ return Effect.flatMap(Effect.sync(() => {
28
+ let before = LogPrefix$Reventless.currentPluginName.contents;
29
+ LogPrefix$Reventless.currentPluginName.contents = resolvedPlugin;
30
+ return before;
31
+ }), before => Effect.ensuring(eff, Effect.sync(() => {
32
+ LogPrefix$Reventless.currentPluginName.contents = before;
33
+ })));
34
+ } else {
35
+ return eff;
36
+ }
37
+ };
38
+ return Effect.provideService(annotateRetry(annotateOpt(annotateOpt(Effect.annotateLogs(withAmbientPlugin(effect), "correlationId", cid), "comp", comp), "causationId", causationId)), RequestContext$ReventlessCore.tag, RequestContext$ReventlessCore.make(cid, causationId, comp, resolvedPlugin, timestamp, retryCount, identity, claims));
26
39
  }
27
40
 
28
41
  function runEffect(correlationId, causationId, comp, pluginName, timestamp, retryCount, identity, claims, effect) {
@@ -222,12 +222,13 @@ let makeGenerateCommand = (
222
222
  // Render `Name({fields})` without the standalone id: it's the command's
223
223
  // partition-tag value, already shown among the fields, so a leading
224
224
  // `(id)` would just duplicate it.
225
+ // `via` names the door the request came through: `Mutation.<field>`, `mcp/tools/<tool>`.
225
226
  EffectLogger.logInfo(
226
227
  ~comp=`CommandGenerator(${serviceName})`,
227
228
  ~ids=EffectLogger.idsOf(meta),
228
229
  ~detail=commandJson,
229
230
  `generated command: ${commandJson->LogFormat.cmdLabelOfJson}`,
230
- )
231
+ )->Effect.annotateLogs("via", payload.meta.info)
231
232
  )
232
233
  ->Effect.flatMap(((meta, commandJson, id)) => {
233
234
  switch commandJson->Message.decode(commandSchema) {
@@ -127,7 +127,7 @@ function makeGenerateCommand(publishJsons, publishJsonsAndWait, serviceName, com
127
127
  ];
128
128
  }), param => {
129
129
  let commandJson = param[1];
130
- return EffectLogger$ReventlessCore.logInfo(`CommandGenerator(` + serviceName + `)`, commandJson, EffectLogger$ReventlessCore.idsOf(undefined, undefined, param[0]), `generated command: ` + LogFormat$ReventlessCore.cmdLabelOfJson(commandJson));
130
+ return Effect.annotateLogs(EffectLogger$ReventlessCore.logInfo(`CommandGenerator(` + serviceName + `)`, commandJson, EffectLogger$ReventlessCore.idsOf(undefined, undefined, param[0]), `generated command: ` + LogFormat$ReventlessCore.cmdLabelOfJson(commandJson)), "via", payload.meta.info);
131
131
  }), param => {
132
132
  let id = param[2];
133
133
  let commandJson = param[1];
@@ -33,6 +33,10 @@ let registerCommandOutcome = CommandTopic_Helpers$ReventlessCore.registerCommand
33
33
 
34
34
  let clearCommandOutcome = CommandTopic_Helpers$ReventlessCore.clearCommandOutcome;
35
35
 
36
+ let outcomeLines = CommandTopic_Helpers$ReventlessCore.outcomeLines;
37
+
38
+ let logOutcome = CommandTopic_Helpers$ReventlessCore.logOutcome;
39
+
36
40
  let fireCommandOutcome = CommandTopic_Helpers$ReventlessCore.fireCommandOutcome;
37
41
 
38
42
  let acceptedResultChannel = CommandTopic_Helpers$ReventlessCore.acceptedResultChannel;
@@ -69,6 +73,8 @@ export {
69
73
  nextOutcomeHookId,
70
74
  registerCommandOutcome,
71
75
  clearCommandOutcome,
76
+ outcomeLines,
77
+ logOutcome,
72
78
  fireCommandOutcome,
73
79
  acceptedResultChannel,
74
80
  rejectedResultChannel,
@@ -86,6 +86,42 @@ let clearCommandOutcome = () => {
86
86
  commandOutcomeHooks := []
87
87
  }
88
88
 
89
+ /** Whether each outcome is also written as an info line, `command accepted` or `command
90
+ rejected`, under `CommandTopic(<component>)` with the command's ids: where a deployed
91
+ platform's tools read what became of a command. The local platform turns it off, since its
92
+ tap carries every outcome already. */
93
+ let outcomeLines = ref(true)
94
+
95
+ let logOutcome = (r: commandOutcomeReport) => {
96
+ let str = JSON.Encode.string
97
+ let (message, fields) = switch r.outcome {
98
+ | OutcomeAccepted({entityId, eventCount}) => (
99
+ "command accepted",
100
+ [("outcome", str("accepted")), ("eventCount", JSON.Encode.int(eventCount))]->Array.concat(
101
+ entityId->Option.mapOr([], e => [("entityId", str(e))]),
102
+ ),
103
+ )
104
+ | OutcomeRejected({errorCode, cause}) => (
105
+ "command rejected",
106
+ [
107
+ ("outcome", str("rejected")),
108
+ ("errorCode", str(errorCode)),
109
+ ("cause", str(refusalCauseToGraphQL(cause))),
110
+ ],
111
+ )
112
+ }
113
+ EffectLogger.logInfo(
114
+ ~comp=`CommandTopic(${r.component})`,
115
+ ~ids={
116
+ msgId: r.reference,
117
+ correlationId: ?r.correlationId,
118
+ causationId: ?r.causationId,
119
+ },
120
+ ~detail=JSON.Object(Dict.fromArray([("component", str(r.component))]->Array.concat(fields))),
121
+ message,
122
+ )->Effect.runSync
123
+ }
124
+
89
125
  // Fired from reportAccepted/reportRejected below, which both write-side kinds call
90
126
  // unconditionally — so this sees fire-and-forget dispatches too, not just the ones a
91
127
  // producer awaited on the side-channels.
@@ -100,11 +136,16 @@ let fireCommandOutcome = (
100
136
  ~correlationId: option<string>=?,
101
137
  ~causationId: option<string>=?,
102
138
  outcome: observedOutcome,
103
- ) =>
139
+ ) => {
140
+ let report = {component, reference, ?correlationId, ?causationId, outcome}
141
+ if outcomeLines.contents {
142
+ try logOutcome(report) catch {
143
+ | _ => ()
144
+ }
145
+ }
104
146
  switch commandOutcomeHooks.contents {
105
147
  | [] => ()
106
148
  | hooks =>
107
- let report = {component, reference, ?correlationId, ?causationId, outcome}
108
149
  hooks->Array.forEach(({hook}) =>
109
150
  switch hook(report) {
110
151
  | () => ()
@@ -119,6 +160,7 @@ let fireCommandOutcome = (
119
160
  }
120
161
  )
121
162
  }
163
+ }
122
164
 
123
165
  // Side-channel for publishJsonsAndWait result propagation.
124
166
  // Set by runInlineAndCollect before invoking the handler; cleared after.
@@ -46,11 +46,56 @@ function clearCommandOutcome() {
46
46
  commandOutcomeHooks.contents = [];
47
47
  }
48
48
 
49
+ let outcomeLines = {
50
+ contents: true
51
+ };
52
+
53
+ function logOutcome(r) {
54
+ let match = r.outcome;
55
+ let match$1;
56
+ match$1 = match.TAG === "OutcomeAccepted" ? [
57
+ "command accepted",
58
+ [
59
+ [
60
+ "outcome",
61
+ "accepted"
62
+ ],
63
+ [
64
+ "eventCount",
65
+ match.eventCount
66
+ ]
67
+ ].concat(Stdlib_Option.mapOr(match.entityId, [], e => [[
68
+ "entityId",
69
+ e
70
+ ]]))
71
+ ] : [
72
+ "command rejected",
73
+ [
74
+ [
75
+ "outcome",
76
+ "rejected"
77
+ ],
78
+ [
79
+ "errorCode",
80
+ match.errorCode
81
+ ],
82
+ [
83
+ "cause",
84
+ refusalCauseToGraphQL(match.cause)
85
+ ]
86
+ ]
87
+ ];
88
+ Effect.runSync(EffectLogger$ReventlessCore.logInfo(`CommandTopic(` + r.component + `)`, Object.fromEntries([[
89
+ "component",
90
+ r.component
91
+ ]].concat(match$1[1])), {
92
+ msgId: r.reference,
93
+ correlationId: r.correlationId,
94
+ causationId: r.causationId
95
+ }, match$1[0]));
96
+ }
97
+
49
98
  function fireCommandOutcome(component, reference, correlationId, causationId, outcome) {
50
- let hooks = commandOutcomeHooks.contents;
51
- if (hooks.length === 0) {
52
- return;
53
- }
54
99
  let report = {
55
100
  component: component,
56
101
  reference: reference,
@@ -58,15 +103,26 @@ function fireCommandOutcome(component, reference, correlationId, causationId, ou
58
103
  causationId: causationId,
59
104
  outcome: outcome
60
105
  };
61
- hooks.forEach(param => {
106
+ if (outcomeLines.contents) {
62
107
  try {
63
- param.hook(report);
64
- return;
65
- } catch (raw_err) {
66
- let err = Primitive_exceptions.internalToException(raw_err);
67
- return Effect.runSync(EffectLogger$ReventlessCore.logError(`CommandTopic(` + component + `)`, undefined, undefined, `command-outcome hook threw (ignored): ` + Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(err), Stdlib_JsExn.message), "unknown")));
108
+ logOutcome(report);
109
+ } catch (exn) {
110
+
68
111
  }
69
- });
112
+ }
113
+ let hooks = commandOutcomeHooks.contents;
114
+ if (hooks.length !== 0) {
115
+ hooks.forEach(param => {
116
+ try {
117
+ param.hook(report);
118
+ return;
119
+ } catch (raw_err) {
120
+ let err = Primitive_exceptions.internalToException(raw_err);
121
+ return Effect.runSync(EffectLogger$ReventlessCore.logError(`CommandTopic(` + component + `)`, undefined, undefined, `command-outcome hook threw (ignored): ` + Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(err), Stdlib_JsExn.message), "unknown")));
122
+ }
123
+ });
124
+ return;
125
+ }
70
126
  }
71
127
 
72
128
  let acceptedResultChannel = {
@@ -295,6 +351,8 @@ export {
295
351
  nextOutcomeHookId,
296
352
  registerCommandOutcome,
297
353
  clearCommandOutcome,
354
+ outcomeLines,
355
+ logOutcome,
298
356
  fireCommandOutcome,
299
357
  acceptedResultChannel,
300
358
  rejectedResultChannel,
@@ -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 */