@reventlessdev/reventless-core 3.0.0-alpha.325 → 3.0.0-alpha.326
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 +8 -0
- package/package.json +7 -7
- package/src/Message.res +46 -0
- package/src/Message.res.mjs +45 -0
- package/src/Projection.res +269 -131
- package/src/Projection.res.mjs +338 -224
- package/src/ViewChange.res +194 -0
- package/src/ViewChange.res.mjs +235 -0
- package/src/components/Aggregate/Aggregate_Callback.res +6 -1
- package/src/components/Aggregate/Aggregate_Callback.res.mjs +6 -5
- package/src/components/AutomationSlice/AutomationSlice_Callback.res +21 -9
- package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +19 -8
- package/src/components/CommandTopic/CommandTopic.res.mjs +5 -2
- package/src/components/CommandTopic/CommandTopic_Helpers.res +56 -26
- package/src/components/CommandTopic/CommandTopic_Helpers.res.mjs +35 -16
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res +6 -2
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res.mjs +6 -2
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +25 -12
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +20 -7
- package/src/components/ReadModel/ReadModel_Callback.res +23 -3
- package/src/components/ReadModel/ReadModel_Callback.res.mjs +14 -2
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +7 -0
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +7 -7
- package/src/components/StateViewSlice/StateViewSlice_Builder.res +14 -2
- package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +13 -2
- package/src/components/StateViewSlice/StateViewSlice_Callback.res +29 -4
- package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +9 -2
- package/src/util/EffectLogger.res +30 -29
- package/src/util/EffectLogger.res.mjs +13 -22
- package/src/util/Logger.res +231 -35
- package/src/util/Logger.res.mjs +263 -42
- package/tests/commandgenerator/OwnerActingTest.res +3 -1
- package/tests/commandtopic/CommandOutcomeHookTest.res +74 -2
- package/tests/commandtopic/CommandOutcomeHookTest.res.mjs +92 -0
- package/tests/commandtopic/CommandTopicHelpersRejectionTest.res.mjs +4 -4
- package/tests/logger/LoggerSinkTest.res +205 -0
- package/tests/logger/LoggerSinkTest.res.mjs +209 -0
- package/tests/projection/ViewChangeTest.res +173 -0
- package/tests/projection/ViewChangeTest.res.mjs +292 -0
|
@@ -81,7 +81,9 @@ let seen = () =>
|
|
|
81
81
|
)
|
|
82
82
|
|
|
83
83
|
let record = () =>
|
|
84
|
-
CommandTopic_Helpers.registerCommandOutcome(r =>
|
|
84
|
+
CommandTopic_Helpers.registerCommandOutcome(r =>
|
|
85
|
+
observed := observed.contents->Array.concat([r])
|
|
86
|
+
)->ignore
|
|
85
87
|
|
|
86
88
|
let _ = beforeEach(() => {
|
|
87
89
|
AggregateFixtures.mock.reset()
|
|
@@ -291,7 +293,9 @@ describe("CommandTopic command-outcome hook:", () => {
|
|
|
291
293
|
testPromise(
|
|
292
294
|
"a throwing hook is swallowed and the command still succeeds",
|
|
293
295
|
async () => {
|
|
294
|
-
CommandTopic_Helpers.registerCommandOutcome(
|
|
296
|
+
CommandTopic_Helpers.registerCommandOutcome(
|
|
297
|
+
_ => JsError.throwWithMessage("hook exploded"),
|
|
298
|
+
)->ignore
|
|
295
299
|
let results = await runAgg("ref-1", AggregateFixtures.AggSpec.Create({name: "Widget"}))
|
|
296
300
|
|
|
297
301
|
// The outcome the hook observes has already happened — an observer must not undo it.
|
|
@@ -299,5 +303,73 @@ describe("CommandTopic command-outcome hook:", () => {
|
|
|
299
303
|
expect(AggregateFixtures.mock.getAll()->Array.length)->toBe(1)
|
|
300
304
|
},
|
|
301
305
|
)
|
|
306
|
+
|
|
307
|
+
testPromise(
|
|
308
|
+
"two listeners both hear every outcome",
|
|
309
|
+
async () => {
|
|
310
|
+
let second: ref<array<string>> = ref([])
|
|
311
|
+
record()
|
|
312
|
+
CommandTopic_Helpers.registerCommandOutcome(
|
|
313
|
+
r => second := second.contents->Array.concat([r.reference]),
|
|
314
|
+
)->ignore
|
|
315
|
+
let _ = await runAgg("ref-1", AggregateFixtures.AggSpec.Create({name: "Widget"}))
|
|
316
|
+
let _ = await runAgg("ref-2", AggregateFixtures.AggSpec.Create({name: "Widget"}))
|
|
317
|
+
|
|
318
|
+
expect(seen())->toEqual([
|
|
319
|
+
("TestAggregate", "ref-1", "accepted", "", 1),
|
|
320
|
+
("TestAggregate", "ref-2", "rejected/domain", "AlreadyExists", 0),
|
|
321
|
+
])
|
|
322
|
+
expect(second.contents)->toEqual(["ref-1", "ref-2"])
|
|
323
|
+
},
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
testPromise(
|
|
327
|
+
"a throwing listener does not stop the others",
|
|
328
|
+
async () => {
|
|
329
|
+
CommandTopic_Helpers.registerCommandOutcome(
|
|
330
|
+
_ => JsError.throwWithMessage("hook exploded"),
|
|
331
|
+
)->ignore
|
|
332
|
+
record()
|
|
333
|
+
let _ = await runAgg("ref-1", AggregateFixtures.AggSpec.Create({name: "Widget"}))
|
|
334
|
+
|
|
335
|
+
expect(seen())->toEqual([("TestAggregate", "ref-1", "accepted", "", 1)])
|
|
336
|
+
},
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
testPromise(
|
|
340
|
+
"the removal a registration returns removes only that listener",
|
|
341
|
+
async () => {
|
|
342
|
+
let removed: ref<int> = ref(0)
|
|
343
|
+
let remove = CommandTopic_Helpers.registerCommandOutcome(
|
|
344
|
+
_ => removed := removed.contents + 1,
|
|
345
|
+
)
|
|
346
|
+
record()
|
|
347
|
+
remove()
|
|
348
|
+
let _ = await runAgg("ref-1", AggregateFixtures.AggSpec.Create({name: "Widget"}))
|
|
349
|
+
|
|
350
|
+
expect(removed.contents)->toBe(0)
|
|
351
|
+
expect(seen())->toEqual([("TestAggregate", "ref-1", "accepted", "", 1)])
|
|
352
|
+
},
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
testPromise(
|
|
356
|
+
"an outcome carries the command's correlation id",
|
|
357
|
+
async () => {
|
|
358
|
+
record()
|
|
359
|
+
let _ = await runAgg("ref-1", AggregateFixtures.AggSpec.Create({name: "Widget"}))
|
|
360
|
+
let _ = await runSlice(
|
|
361
|
+
"ref-2",
|
|
362
|
+
DcbFixtures.TestCommandSpec.CreateItem({itemId: "item-1", name: "Test"}),
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
expect(observed.contents->Array.map(r => r.correlationId->Option.isSome))->toEqual([
|
|
366
|
+
true,
|
|
367
|
+
true,
|
|
368
|
+
])
|
|
369
|
+
expect((observed.contents->Array.getUnsafe(1)).correlationId)->toEqual(
|
|
370
|
+
Some(DcbFixtures.testMeta.correlationId),
|
|
371
|
+
)
|
|
372
|
+
},
|
|
373
|
+
)
|
|
302
374
|
})
|
|
303
375
|
})
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
3
|
import * as Id$Reventless from "@reventlessdev/reventless-spec/src/types/Id.res.mjs";
|
|
4
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
4
5
|
import * as Effect from "effect/Effect";
|
|
5
6
|
import * as Stream from "effect/Stream";
|
|
6
7
|
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
|
|
@@ -365,6 +366,97 @@ globalThis.describe("CommandTopic command-outcome hook:", () => {
|
|
|
365
366
|
}]);
|
|
366
367
|
globalThis.expect(AggregateFixtures$ReventlessCore.mock.getAll().length).toBe(1);
|
|
367
368
|
});
|
|
369
|
+
globalThis.test("two listeners both hear every outcome", async () => {
|
|
370
|
+
let second = {
|
|
371
|
+
contents: []
|
|
372
|
+
};
|
|
373
|
+
record();
|
|
374
|
+
CommandTopic_Helpers$ReventlessCore.registerCommandOutcome(r => {
|
|
375
|
+
second.contents = second.contents.concat([r.reference]);
|
|
376
|
+
});
|
|
377
|
+
await runAgg(undefined, "ref-1", {
|
|
378
|
+
TAG: "Create",
|
|
379
|
+
name: "Widget"
|
|
380
|
+
});
|
|
381
|
+
await runAgg(undefined, "ref-2", {
|
|
382
|
+
TAG: "Create",
|
|
383
|
+
name: "Widget"
|
|
384
|
+
});
|
|
385
|
+
globalThis.expect(seen()).toEqual([
|
|
386
|
+
[
|
|
387
|
+
"TestAggregate",
|
|
388
|
+
"ref-1",
|
|
389
|
+
"accepted",
|
|
390
|
+
"",
|
|
391
|
+
1
|
|
392
|
+
],
|
|
393
|
+
[
|
|
394
|
+
"TestAggregate",
|
|
395
|
+
"ref-2",
|
|
396
|
+
"rejected/domain",
|
|
397
|
+
"AlreadyExists",
|
|
398
|
+
0
|
|
399
|
+
]
|
|
400
|
+
]);
|
|
401
|
+
globalThis.expect(second.contents).toEqual([
|
|
402
|
+
"ref-1",
|
|
403
|
+
"ref-2"
|
|
404
|
+
]);
|
|
405
|
+
});
|
|
406
|
+
globalThis.test("a throwing listener does not stop the others", async () => {
|
|
407
|
+
CommandTopic_Helpers$ReventlessCore.registerCommandOutcome(param => Stdlib_JsError.throwWithMessage("hook exploded"));
|
|
408
|
+
record();
|
|
409
|
+
await runAgg(undefined, "ref-1", {
|
|
410
|
+
TAG: "Create",
|
|
411
|
+
name: "Widget"
|
|
412
|
+
});
|
|
413
|
+
globalThis.expect(seen()).toEqual([[
|
|
414
|
+
"TestAggregate",
|
|
415
|
+
"ref-1",
|
|
416
|
+
"accepted",
|
|
417
|
+
"",
|
|
418
|
+
1
|
|
419
|
+
]]);
|
|
420
|
+
});
|
|
421
|
+
globalThis.test("the removal a registration returns removes only that listener", async () => {
|
|
422
|
+
let removed = {
|
|
423
|
+
contents: 0
|
|
424
|
+
};
|
|
425
|
+
let remove = CommandTopic_Helpers$ReventlessCore.registerCommandOutcome(param => {
|
|
426
|
+
removed.contents = removed.contents + 1 | 0;
|
|
427
|
+
});
|
|
428
|
+
record();
|
|
429
|
+
remove();
|
|
430
|
+
await runAgg(undefined, "ref-1", {
|
|
431
|
+
TAG: "Create",
|
|
432
|
+
name: "Widget"
|
|
433
|
+
});
|
|
434
|
+
globalThis.expect(removed.contents).toBe(0);
|
|
435
|
+
globalThis.expect(seen()).toEqual([[
|
|
436
|
+
"TestAggregate",
|
|
437
|
+
"ref-1",
|
|
438
|
+
"accepted",
|
|
439
|
+
"",
|
|
440
|
+
1
|
|
441
|
+
]]);
|
|
442
|
+
});
|
|
443
|
+
globalThis.test("an outcome carries the command's correlation id", async () => {
|
|
444
|
+
record();
|
|
445
|
+
await runAgg(undefined, "ref-1", {
|
|
446
|
+
TAG: "Create",
|
|
447
|
+
name: "Widget"
|
|
448
|
+
});
|
|
449
|
+
await runSlice("ref-2", {
|
|
450
|
+
TAG: "CreateItem",
|
|
451
|
+
itemId: "item-1",
|
|
452
|
+
name: "Test"
|
|
453
|
+
});
|
|
454
|
+
globalThis.expect(observed.contents.map(r => Stdlib_Option.isSome(r.correlationId))).toEqual([
|
|
455
|
+
true,
|
|
456
|
+
true
|
|
457
|
+
]);
|
|
458
|
+
globalThis.expect(observed.contents[1].correlationId).toEqual(DcbFixtures$ReventlessCore.testMeta.correlationId);
|
|
459
|
+
});
|
|
368
460
|
});
|
|
369
461
|
});
|
|
370
462
|
|
|
@@ -32,11 +32,11 @@ globalThis.describe("CommandTopic_Helpers.runInlineAndCollect:", () => {
|
|
|
32
32
|
globalThis.test("Rejected from rejectedResultChannel beats Accepted from acceptedResultChannel", async () => {
|
|
33
33
|
let handler = stream => Effect.map(Stream.runCollect(stream), items => {
|
|
34
34
|
items.forEach(item => {
|
|
35
|
-
CommandTopic_Helpers$ReventlessCore.reportAccepted("TestService", item.reference, {
|
|
35
|
+
CommandTopic_Helpers$ReventlessCore.reportAccepted("TestService", undefined, item.reference, {
|
|
36
36
|
entityId: "agg-1",
|
|
37
37
|
eventCount: 0
|
|
38
38
|
});
|
|
39
|
-
CommandTopic_Helpers$ReventlessCore.reportRejected("TestService", "DomainRejection", item.reference, {
|
|
39
|
+
CommandTopic_Helpers$ReventlessCore.reportRejected("TestService", "DomainRejection", undefined, item.reference, {
|
|
40
40
|
errorCode: "AlreadyExists",
|
|
41
41
|
errorDetail: ""
|
|
42
42
|
});
|
|
@@ -64,7 +64,7 @@ globalThis.describe("CommandTopic_Helpers.runInlineAndCollect:", () => {
|
|
|
64
64
|
});
|
|
65
65
|
globalThis.test("Rejected from rejectedResultChannel beats Error result (synthesized Conflict)", async () => {
|
|
66
66
|
let handler = stream => Effect.map(Stream.runCollect(stream), items => {
|
|
67
|
-
items.forEach(item => CommandTopic_Helpers$ReventlessCore.reportRejected("TestService", "DomainRejection", item.reference, {
|
|
67
|
+
items.forEach(item => CommandTopic_Helpers$ReventlessCore.reportRejected("TestService", "DomainRejection", undefined, item.reference, {
|
|
68
68
|
errorCode: "BusinessRuleViolated",
|
|
69
69
|
errorDetail: "{\"reason\":\"x\"}"
|
|
70
70
|
}));
|
|
@@ -106,7 +106,7 @@ globalThis.describe("CommandTopic_Helpers.runInlineAndCollect:", () => {
|
|
|
106
106
|
});
|
|
107
107
|
globalThis.test("a reported access refusal keeps its cause", async () => {
|
|
108
108
|
let handler = stream => Effect.map(Stream.runCollect(stream), items => {
|
|
109
|
-
items.forEach(item => CommandTopic_Helpers$ReventlessCore.reportRejected("TestService", "AccessRefusal", item.reference, {
|
|
109
|
+
items.forEach(item => CommandTopic_Helpers$ReventlessCore.reportRejected("TestService", "AccessRefusal", undefined, item.reference, {
|
|
110
110
|
errorCode: "Forbidden",
|
|
111
111
|
errorDetail: "not the owner"
|
|
112
112
|
}));
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// A registered sink receives every record that passed its level, as the JSON
|
|
2
|
+
// line prints it, and a level set while running decides what passes.
|
|
3
|
+
|
|
4
|
+
open JestGlobals
|
|
5
|
+
|
|
6
|
+
type consoleObj = {mutable log: string => unit, mutable error: string => unit}
|
|
7
|
+
@val external _console: consoleObj = "console"
|
|
8
|
+
|
|
9
|
+
let capture = (fn: unit => unit): (array<string>, array<string>) => {
|
|
10
|
+
let out: array<string> = []
|
|
11
|
+
let err: array<string> = []
|
|
12
|
+
let originalLog = _console.log
|
|
13
|
+
let originalError = _console.error
|
|
14
|
+
_console.log = s => out->Array.push(s)
|
|
15
|
+
_console.error = s => err->Array.push(s)
|
|
16
|
+
let result = try {
|
|
17
|
+
fn()
|
|
18
|
+
Ok()
|
|
19
|
+
} catch {
|
|
20
|
+
| e => Error(e)
|
|
21
|
+
}
|
|
22
|
+
_console.log = originalLog
|
|
23
|
+
_console.error = originalError
|
|
24
|
+
switch result {
|
|
25
|
+
| Ok() => (out, err)
|
|
26
|
+
| Error(e) => throw(e)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let collecting = (): (array<Logger.record>, unit => unit) => {
|
|
31
|
+
let got = []
|
|
32
|
+
let remove = Logger.addSink(r => got->Array.push(r))
|
|
33
|
+
(got, remove)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("Logger sinks", () => {
|
|
37
|
+
beforeAll(() => {
|
|
38
|
+
NodeProcess.env->Dict.set("REVENTLESS_LOG_FORMAT", "json")
|
|
39
|
+
Reventless.AnsiStyle.reload()
|
|
40
|
+
})
|
|
41
|
+
afterAll(() => {
|
|
42
|
+
NodeProcess.env->Dict.set("REVENTLESS_LOG_FORMAT", "text")
|
|
43
|
+
Reventless.AnsiStyle.reload()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
testSync("a sink's record has the JSON line's fields, field for field", () => {
|
|
47
|
+
let (got, remove) = collecting()
|
|
48
|
+
let detail = Dict.fromArray([("orderId", JSON.Encode.string("o-1"))])->JSON.Encode.object
|
|
49
|
+
let (lines, _) = capture(
|
|
50
|
+
() =>
|
|
51
|
+
Logger.emit(
|
|
52
|
+
~level=Warn,
|
|
53
|
+
~comp="Aggregate(Order)",
|
|
54
|
+
~plugin="Ordering",
|
|
55
|
+
~detail,
|
|
56
|
+
~annotations=Dict.fromArray([("correlationId", "c-1")]),
|
|
57
|
+
"refused",
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
remove()
|
|
61
|
+
expect(got->Array.length)->toBe(1)
|
|
62
|
+
expect(lines->Array.length)->toBe(1)
|
|
63
|
+
let record = got->Array.getUnsafe(0)
|
|
64
|
+
expect(lines->Array.getUnsafe(0)->JSON.parseOrThrow)->toEqual(record->Logger.recordToJson)
|
|
65
|
+
expect(record.plugin)->toEqual(Some("Ordering"))
|
|
66
|
+
expect(record.annotations->Dict.get("correlationId"))->toEqual(Some("c-1"))
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
testSync("the record's detail is redacted before a sink sees it", () => {
|
|
70
|
+
let (got, remove) = collecting()
|
|
71
|
+
let detail =
|
|
72
|
+
Dict.fromArray([
|
|
73
|
+
("name", Dict.fromArray([("$pii", JSON.Encode.string("Ada Secret"))])->JSON.Encode.object),
|
|
74
|
+
])->JSON.Encode.object
|
|
75
|
+
let _ = capture(() => Logger.emit(~level=Info, ~comp="Platform", ~detail, "joined"))
|
|
76
|
+
remove()
|
|
77
|
+
let printed = got->Array.getUnsafe(0)->Logger.recordToJson->JSON.stringify
|
|
78
|
+
expect(printed->String.includes("Ada Secret"))->toBe(false)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
testSync("a sink that throws is noted once, and the others still get every record", () => {
|
|
82
|
+
let removeBad = Logger.addSink(_ => throw(Failure("boom")))
|
|
83
|
+
let (got, remove) = collecting()
|
|
84
|
+
let (_, errors) = capture(
|
|
85
|
+
() => {
|
|
86
|
+
Logger.emit(~level=Info, ~comp="Platform", "one")
|
|
87
|
+
Logger.emit(~level=Info, ~comp="Platform", "two")
|
|
88
|
+
},
|
|
89
|
+
)
|
|
90
|
+
removeBad()
|
|
91
|
+
remove()
|
|
92
|
+
expect(got->Array.map(r => r.message))->toEqual(["one", "two"])
|
|
93
|
+
expect(errors->Array.filter(e => e->String.includes("log sink"))->Array.length)->toBe(1)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
testSync("a removed sink gets nothing more", () => {
|
|
97
|
+
let (got, remove) = collecting()
|
|
98
|
+
remove()
|
|
99
|
+
let _ = capture(() => Logger.emit(~level=Info, ~comp="Platform", "after"))
|
|
100
|
+
expect(got->Array.length)->toBe(0)
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
describe("Logger levels set while running", () => {
|
|
105
|
+
afterEach(() => Logger.clearLevels())
|
|
106
|
+
|
|
107
|
+
// LOG_LEVEL is read live; each case sets the base it needs.
|
|
108
|
+
let withLogLevel = (value: string, fn: unit => unit) => {
|
|
109
|
+
let before = NodeProcess.env->Dict.get("LOG_LEVEL")
|
|
110
|
+
NodeProcess.env->Dict.set("LOG_LEVEL", value)
|
|
111
|
+
fn()
|
|
112
|
+
switch before {
|
|
113
|
+
| Some(v) => NodeProcess.env->Dict.set("LOG_LEVEL", v)
|
|
114
|
+
| None => NodeProcess.env->Dict.delete("LOG_LEVEL")
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
testSync(
|
|
119
|
+
"the component's level wins over the plugin's, the plugin's over the default, the default over LOG_LEVEL",
|
|
120
|
+
() =>
|
|
121
|
+
withLogLevel(
|
|
122
|
+
"error",
|
|
123
|
+
() => {
|
|
124
|
+
Reventless.LogPrefix.registerComponentPlugin(
|
|
125
|
+
~componentName="Order",
|
|
126
|
+
~pluginName="Ordering",
|
|
127
|
+
)
|
|
128
|
+
let comp = "Aggregate(Order)"
|
|
129
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Error))
|
|
130
|
+
Logger.setLevel(Some(Logger.Warn))
|
|
131
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Warn))
|
|
132
|
+
Logger.setLevel(~plugin="Ordering", Some(Logger.Info))
|
|
133
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Info))
|
|
134
|
+
expect(Logger.minLevelFor(~comp="Aggregate(Unrelated)", ()))->toEqual(Some(Logger.Warn))
|
|
135
|
+
Logger.setLevel(~comp, Some(Logger.Debug))
|
|
136
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Debug))
|
|
137
|
+
// Cleared, the next wider scope applies again.
|
|
138
|
+
Logger.setLevel(~comp, None)
|
|
139
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Info))
|
|
140
|
+
},
|
|
141
|
+
),
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
testSync("a component of one plugin wins over the component everywhere", () => {
|
|
145
|
+
Logger.setLevel(~comp="Projection", Some(Logger.Warn))
|
|
146
|
+
Logger.setLevel(~plugin="Ordering", ~comp="Projection", Some(Logger.Debug))
|
|
147
|
+
expect(Logger.minLevelFor(~plugin="Ordering", ~comp="Projection", ()))->toEqual(
|
|
148
|
+
Some(Logger.Debug),
|
|
149
|
+
)
|
|
150
|
+
expect(Logger.minLevelFor(~plugin="Catalog", ~comp="Projection", ()))->toEqual(
|
|
151
|
+
Some(Logger.Warn),
|
|
152
|
+
)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
testSync("a line below its level never reaches a sink; raised, it does", () =>
|
|
156
|
+
withLogLevel(
|
|
157
|
+
"info",
|
|
158
|
+
() => {
|
|
159
|
+
let log = Logger.fromEnv()
|
|
160
|
+
let (got, remove) = collecting()
|
|
161
|
+
let _ = capture(
|
|
162
|
+
() => {
|
|
163
|
+
log.debug(~comp="Quiet", "not yet")
|
|
164
|
+
Logger.setLevel(~comp="Quiet", Some(Logger.Debug))
|
|
165
|
+
log.debug(~comp="Quiet", "now")
|
|
166
|
+
log.debug(~comp="Other", "still not")
|
|
167
|
+
Logger.setLevel(~comp="Loud", Some(Logger.Error))
|
|
168
|
+
log.warn(~comp="Loud", "dropped")
|
|
169
|
+
},
|
|
170
|
+
)
|
|
171
|
+
remove()
|
|
172
|
+
expect(got->Array.map(r => r.message))->toEqual(["now"])
|
|
173
|
+
},
|
|
174
|
+
)
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
testSync("the Effect helpers follow a component's level", () =>
|
|
178
|
+
withLogLevel(
|
|
179
|
+
"info",
|
|
180
|
+
() => {
|
|
181
|
+
let (got, remove) = collecting()
|
|
182
|
+
let _ = capture(
|
|
183
|
+
() => {
|
|
184
|
+
EffectLogger.logDebug(~comp="Quiet", "not yet")->Effect.runSync
|
|
185
|
+
Logger.setLevel(~comp="Quiet", Some(Logger.Debug))
|
|
186
|
+
EffectLogger.logDebug(~comp="Quiet", "now")->Effect.runSync
|
|
187
|
+
Logger.setLevel(~comp="Loud", Some(Logger.Error))
|
|
188
|
+
EffectLogger.logWarn(~comp="Loud", "dropped")->Effect.runSync
|
|
189
|
+
},
|
|
190
|
+
)
|
|
191
|
+
remove()
|
|
192
|
+
expect(got->Array.map(r => r.message))->toEqual(["now"])
|
|
193
|
+
},
|
|
194
|
+
)
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
testSync("levelSettings lists what was set", () => {
|
|
198
|
+
Logger.setLevel(Some(Logger.Warn))
|
|
199
|
+
Logger.setLevel(~plugin="Ordering", ~comp="Projection", Some(Logger.Debug))
|
|
200
|
+
expect(Logger.levelSettings())->toEqual([
|
|
201
|
+
{Logger.level: Logger.Warn},
|
|
202
|
+
{plugin: "Ordering", comp: "Projection", level: Logger.Debug},
|
|
203
|
+
])
|
|
204
|
+
})
|
|
205
|
+
})
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as AnsiStyle$Reventless from "@reventlessdev/reventless-spec/src/AnsiStyle.res.mjs";
|
|
6
|
+
import * as LogPrefix$Reventless from "@reventlessdev/reventless-spec/src/LogPrefix.res.mjs";
|
|
7
|
+
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
8
|
+
import * as Logger$ReventlessCore from "../../src/util/Logger.res.mjs";
|
|
9
|
+
import * as EffectLogger$ReventlessCore from "../../src/util/EffectLogger.res.mjs";
|
|
10
|
+
|
|
11
|
+
function capture(fn) {
|
|
12
|
+
let out = [];
|
|
13
|
+
let err = [];
|
|
14
|
+
let originalLog = console.log;
|
|
15
|
+
let originalError = console.error;
|
|
16
|
+
console.log = s => {
|
|
17
|
+
out.push(s);
|
|
18
|
+
};
|
|
19
|
+
console.error = s => {
|
|
20
|
+
err.push(s);
|
|
21
|
+
};
|
|
22
|
+
let result;
|
|
23
|
+
try {
|
|
24
|
+
fn();
|
|
25
|
+
result = {
|
|
26
|
+
TAG: "Ok",
|
|
27
|
+
_0: undefined
|
|
28
|
+
};
|
|
29
|
+
} catch (raw_e) {
|
|
30
|
+
let e = Primitive_exceptions.internalToException(raw_e);
|
|
31
|
+
result = {
|
|
32
|
+
TAG: "Error",
|
|
33
|
+
_0: e
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
console.log = originalLog;
|
|
37
|
+
console.error = originalError;
|
|
38
|
+
if (result.TAG === "Ok") {
|
|
39
|
+
return [
|
|
40
|
+
out,
|
|
41
|
+
err
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
throw result._0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function collecting() {
|
|
48
|
+
let got = [];
|
|
49
|
+
let remove = Logger$ReventlessCore.addSink(r => {
|
|
50
|
+
got.push(r);
|
|
51
|
+
});
|
|
52
|
+
return [
|
|
53
|
+
got,
|
|
54
|
+
remove
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
globalThis.describe("Logger sinks", () => {
|
|
59
|
+
globalThis.beforeAll(() => {
|
|
60
|
+
process.env["REVENTLESS_LOG_FORMAT"] = "json";
|
|
61
|
+
AnsiStyle$Reventless.reload();
|
|
62
|
+
});
|
|
63
|
+
globalThis.afterAll(() => {
|
|
64
|
+
process.env["REVENTLESS_LOG_FORMAT"] = "text";
|
|
65
|
+
AnsiStyle$Reventless.reload();
|
|
66
|
+
});
|
|
67
|
+
globalThis.test("a sink's record has the JSON line's fields, field for field", () => {
|
|
68
|
+
let match = collecting();
|
|
69
|
+
let got = match[0];
|
|
70
|
+
let detail = Object.fromEntries([[
|
|
71
|
+
"orderId",
|
|
72
|
+
"o-1"
|
|
73
|
+
]]);
|
|
74
|
+
let match$1 = capture(() => Logger$ReventlessCore.emit("Warn", "Aggregate(Order)", "Ordering", detail, Object.fromEntries([[
|
|
75
|
+
"correlationId",
|
|
76
|
+
"c-1"
|
|
77
|
+
]]), "refused"));
|
|
78
|
+
let lines = match$1[0];
|
|
79
|
+
match[1]();
|
|
80
|
+
globalThis.expect(got.length).toBe(1);
|
|
81
|
+
globalThis.expect(lines.length).toBe(1);
|
|
82
|
+
let record = got[0];
|
|
83
|
+
globalThis.expect(JSON.parse(lines[0])).toEqual(Logger$ReventlessCore.recordToJson(record));
|
|
84
|
+
globalThis.expect(record.plugin).toEqual("Ordering");
|
|
85
|
+
globalThis.expect(record.annotations["correlationId"]).toEqual("c-1");
|
|
86
|
+
});
|
|
87
|
+
globalThis.test("the record's detail is redacted before a sink sees it", () => {
|
|
88
|
+
let match = collecting();
|
|
89
|
+
let detail = Object.fromEntries([[
|
|
90
|
+
"name",
|
|
91
|
+
Object.fromEntries([[
|
|
92
|
+
"$pii",
|
|
93
|
+
"Ada Secret"
|
|
94
|
+
]])
|
|
95
|
+
]]);
|
|
96
|
+
capture(() => Logger$ReventlessCore.emit("Info", "Platform", undefined, detail, undefined, "joined"));
|
|
97
|
+
match[1]();
|
|
98
|
+
let printed = JSON.stringify(Logger$ReventlessCore.recordToJson(match[0][0]));
|
|
99
|
+
globalThis.expect(printed.includes("Ada Secret")).toBe(false);
|
|
100
|
+
});
|
|
101
|
+
globalThis.test("a sink that throws is noted once, and the others still get every record", () => {
|
|
102
|
+
let removeBad = Logger$ReventlessCore.addSink(param => {
|
|
103
|
+
throw {
|
|
104
|
+
RE_EXN_ID: "Failure",
|
|
105
|
+
_1: "boom",
|
|
106
|
+
Error: new Error()
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
let match = collecting();
|
|
110
|
+
let match$1 = capture(() => {
|
|
111
|
+
Logger$ReventlessCore.emit("Info", "Platform", undefined, undefined, undefined, "one");
|
|
112
|
+
Logger$ReventlessCore.emit("Info", "Platform", undefined, undefined, undefined, "two");
|
|
113
|
+
});
|
|
114
|
+
removeBad();
|
|
115
|
+
match[1]();
|
|
116
|
+
globalThis.expect(match[0].map(r => r.message)).toEqual([
|
|
117
|
+
"one",
|
|
118
|
+
"two"
|
|
119
|
+
]);
|
|
120
|
+
globalThis.expect(match$1[1].filter(e => e.includes("log sink")).length).toBe(1);
|
|
121
|
+
});
|
|
122
|
+
globalThis.test("a removed sink gets nothing more", () => {
|
|
123
|
+
let match = collecting();
|
|
124
|
+
match[1]();
|
|
125
|
+
capture(() => Logger$ReventlessCore.emit("Info", "Platform", undefined, undefined, undefined, "after"));
|
|
126
|
+
globalThis.expect(match[0].length).toBe(0);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
globalThis.describe("Logger levels set while running", () => {
|
|
131
|
+
globalThis.afterEach(() => Logger$ReventlessCore.clearLevels());
|
|
132
|
+
let withLogLevel = (value, fn) => {
|
|
133
|
+
let before = process.env["LOG_LEVEL"];
|
|
134
|
+
process.env["LOG_LEVEL"] = value;
|
|
135
|
+
fn();
|
|
136
|
+
if (before !== undefined) {
|
|
137
|
+
process.env["LOG_LEVEL"] = before;
|
|
138
|
+
return;
|
|
139
|
+
} else {
|
|
140
|
+
return Stdlib_Dict.$$delete(process.env, "LOG_LEVEL");
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
globalThis.test("the component's level wins over the plugin's, the plugin's over the default, the default over LOG_LEVEL", () => withLogLevel("error", () => {
|
|
144
|
+
LogPrefix$Reventless.registerComponentPlugin("Order", "Ordering");
|
|
145
|
+
let comp = "Aggregate(Order)";
|
|
146
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Error");
|
|
147
|
+
Logger$ReventlessCore.setLevel(undefined, undefined, "Warn");
|
|
148
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Warn");
|
|
149
|
+
Logger$ReventlessCore.setLevel("Ordering", undefined, "Info");
|
|
150
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Info");
|
|
151
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, "Aggregate(Unrelated)", undefined)).toEqual("Warn");
|
|
152
|
+
Logger$ReventlessCore.setLevel(undefined, comp, "Debug");
|
|
153
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Debug");
|
|
154
|
+
Logger$ReventlessCore.setLevel(undefined, comp, undefined);
|
|
155
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Info");
|
|
156
|
+
}));
|
|
157
|
+
globalThis.test("a component of one plugin wins over the component everywhere", () => {
|
|
158
|
+
Logger$ReventlessCore.setLevel(undefined, "Projection", "Warn");
|
|
159
|
+
Logger$ReventlessCore.setLevel("Ordering", "Projection", "Debug");
|
|
160
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor("Ordering", "Projection", undefined)).toEqual("Debug");
|
|
161
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor("Catalog", "Projection", undefined)).toEqual("Warn");
|
|
162
|
+
});
|
|
163
|
+
globalThis.test("a line below its level never reaches a sink; raised, it does", () => withLogLevel("info", () => {
|
|
164
|
+
let log = Logger$ReventlessCore.fromEnv();
|
|
165
|
+
let match = collecting();
|
|
166
|
+
capture(() => {
|
|
167
|
+
log.debug("Quiet", undefined, "not yet");
|
|
168
|
+
Logger$ReventlessCore.setLevel(undefined, "Quiet", "Debug");
|
|
169
|
+
log.debug("Quiet", undefined, "now");
|
|
170
|
+
log.debug("Other", undefined, "still not");
|
|
171
|
+
Logger$ReventlessCore.setLevel(undefined, "Loud", "Error");
|
|
172
|
+
log.warn("Loud", undefined, "dropped");
|
|
173
|
+
});
|
|
174
|
+
match[1]();
|
|
175
|
+
globalThis.expect(match[0].map(r => r.message)).toEqual(["now"]);
|
|
176
|
+
}));
|
|
177
|
+
globalThis.test("the Effect helpers follow a component's level", () => withLogLevel("info", () => {
|
|
178
|
+
let match = collecting();
|
|
179
|
+
capture(() => {
|
|
180
|
+
Effect.runSync(EffectLogger$ReventlessCore.logDebug("Quiet", undefined, "not yet"));
|
|
181
|
+
Logger$ReventlessCore.setLevel(undefined, "Quiet", "Debug");
|
|
182
|
+
Effect.runSync(EffectLogger$ReventlessCore.logDebug("Quiet", undefined, "now"));
|
|
183
|
+
Logger$ReventlessCore.setLevel(undefined, "Loud", "Error");
|
|
184
|
+
Effect.runSync(EffectLogger$ReventlessCore.logWarn("Loud", undefined, "dropped"));
|
|
185
|
+
});
|
|
186
|
+
match[1]();
|
|
187
|
+
globalThis.expect(match[0].map(r => r.message)).toEqual(["now"]);
|
|
188
|
+
}));
|
|
189
|
+
globalThis.test("levelSettings lists what was set", () => {
|
|
190
|
+
Logger$ReventlessCore.setLevel(undefined, undefined, "Warn");
|
|
191
|
+
Logger$ReventlessCore.setLevel("Ordering", "Projection", "Debug");
|
|
192
|
+
globalThis.expect(Logger$ReventlessCore.levelSettings()).toEqual([
|
|
193
|
+
{
|
|
194
|
+
level: "Warn"
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
plugin: "Ordering",
|
|
198
|
+
comp: "Projection",
|
|
199
|
+
level: "Debug"
|
|
200
|
+
}
|
|
201
|
+
]);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
export {
|
|
206
|
+
capture,
|
|
207
|
+
collecting,
|
|
208
|
+
}
|
|
209
|
+
/* Not a pure module */
|