@reventlessdev/reventless-core 3.0.0-alpha.324 → 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 +15 -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 +11 -3
- package/src/components/Aggregate/Aggregate_Callback.res.mjs +8 -6
- 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 +205 -1
- package/tests/commandgenerator/OwnerActingTest.res.mjs +361 -0
- 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
|
@@ -404,7 +404,9 @@ let paths = [
|
|
|
404
404
|
let _ = beforeEach(() => {
|
|
405
405
|
Reventless.OwnerScope.setElevatedGroups(["Admin"])
|
|
406
406
|
observed := []
|
|
407
|
-
CommandTopic_Helpers.registerCommandOutcome(r =>
|
|
407
|
+
CommandTopic_Helpers.registerCommandOutcome(r =>
|
|
408
|
+
observed := observed.contents->Array.concat([r])
|
|
409
|
+
)->ignore
|
|
408
410
|
})
|
|
409
411
|
|
|
410
412
|
let _ = afterEach(() => CommandTopic_Helpers.clearCommandOutcome())
|
|
@@ -605,3 +607,205 @@ describe("acting on an owned thing — aggregate specifics:", () => {
|
|
|
605
607
|
expect(snapshotReads.contents)->toBe(0)
|
|
606
608
|
})
|
|
607
609
|
})
|
|
610
|
+
|
|
611
|
+
// ─── An aggregate owned by its id ───────────────────────────────────────────
|
|
612
|
+
|
|
613
|
+
// Keyed by its holder's account id, and records no owner in any event: the id
|
|
614
|
+
// says whose it is, from the first command on.
|
|
615
|
+
module ProfileSpec = {
|
|
616
|
+
module Id = Reventless.Id.Owned(Reventless.Id.StringPure)
|
|
617
|
+
let name = "OwnedProfile"
|
|
618
|
+
|
|
619
|
+
@schema
|
|
620
|
+
type command =
|
|
621
|
+
| Create({name: string})
|
|
622
|
+
| Rename({newName: string})
|
|
623
|
+
|
|
624
|
+
@schema
|
|
625
|
+
type event =
|
|
626
|
+
| Created({name: string})
|
|
627
|
+
| Renamed({newName: string})
|
|
628
|
+
|
|
629
|
+
@schema
|
|
630
|
+
type error =
|
|
631
|
+
| AlreadyCreated
|
|
632
|
+
| NotFound
|
|
633
|
+
|
|
634
|
+
let moduleUrl: string = %raw(`import.meta.url`)
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
module ProfileDecision = {
|
|
638
|
+
module Spec = ProfileSpec
|
|
639
|
+
|
|
640
|
+
@schema
|
|
641
|
+
type state = Absent | Present({name: string})
|
|
642
|
+
|
|
643
|
+
let initialState = Absent
|
|
644
|
+
|
|
645
|
+
// Kept on: the id carries the owner, so a snapshot loses nothing.
|
|
646
|
+
let snapshot = Some({Reventless.Snapshot.interval: 1, stateSchema})
|
|
647
|
+
|
|
648
|
+
let moduleUrl = ProfileSpec.moduleUrl
|
|
649
|
+
|
|
650
|
+
let evolve = (_state, event: ProfileSpec.event) =>
|
|
651
|
+
switch event {
|
|
652
|
+
| Created({name}) => Present({name: name})
|
|
653
|
+
| Renamed({newName}) => Present({name: newName})
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
let decide = (state, command: ProfileSpec.command): result<
|
|
657
|
+
array<ProfileSpec.event>,
|
|
658
|
+
ProfileSpec.error,
|
|
659
|
+
> =>
|
|
660
|
+
switch (state, command) {
|
|
661
|
+
| (Absent, Create({name})) => Ok([ProfileSpec.Created({name: name})])
|
|
662
|
+
| (Absent, Rename(_)) => Error(NotFound)
|
|
663
|
+
| (Present(_), Create(_)) => Error(AlreadyCreated)
|
|
664
|
+
| (Present(_), Rename({newName})) => Ok([ProfileSpec.Renamed({newName: newName})])
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
let profileEvents: ref<array<Message.event'<string, ProfileSpec.event>>> = ref([])
|
|
669
|
+
let profileSnapshotReads = ref(0)
|
|
670
|
+
|
|
671
|
+
module ProfileOps = {
|
|
672
|
+
module Spec = ProfileSpec
|
|
673
|
+
module EventLog = {
|
|
674
|
+
module Spec = {
|
|
675
|
+
module Id = ProfileSpec.Id
|
|
676
|
+
let name = "OwnedProfileEventLog"
|
|
677
|
+
@schema
|
|
678
|
+
type event = ProfileSpec.event
|
|
679
|
+
}
|
|
680
|
+
type operations = {
|
|
681
|
+
append: (
|
|
682
|
+
int,
|
|
683
|
+
string,
|
|
684
|
+
array<Message.event'<string, ProfileSpec.event>>,
|
|
685
|
+
) => promise<result<unit, OuterEventLog.appendError>>,
|
|
686
|
+
replay: string => promise<array<ProfileSpec.event>>,
|
|
687
|
+
replayStream: (string, ~fromSeq: int=?) => Stream.t<ProfileSpec.event, string, unit>,
|
|
688
|
+
replayEnvelopes: (
|
|
689
|
+
string,
|
|
690
|
+
~fromSeq: int=?,
|
|
691
|
+
) => Stream.t<Message.event'<string, ProfileSpec.event>, string, unit>,
|
|
692
|
+
appendStream: (
|
|
693
|
+
int,
|
|
694
|
+
string,
|
|
695
|
+
Stream.t<ProfileSpec.event, string, unit>,
|
|
696
|
+
) => Effect.t<unit, string, unit>,
|
|
697
|
+
latestSnapshot: string => promise<result<option<OuterEventLog.snapshot>, string>>,
|
|
698
|
+
writeSnapshot: (string, OuterEventLog.snapshot) => promise<result<unit, string>>,
|
|
699
|
+
}
|
|
700
|
+
type component = Component.t<OuterEventLog.t, OuterEventLog.outputs, operations>
|
|
701
|
+
let make = (~name as _: string, ~owner as _=?, ~opts as _=?): component => Obj.magic(0)
|
|
702
|
+
}
|
|
703
|
+
let ofId = id => profileEvents.contents->Array.filter(e => e.id == id)->Array.map(e => e.event)
|
|
704
|
+
let eventLog: EventLog.operations = {
|
|
705
|
+
append: async (_seqNr, _id, newEvents) => {
|
|
706
|
+
profileEvents := profileEvents.contents->Array.concat(newEvents)
|
|
707
|
+
Ok()
|
|
708
|
+
},
|
|
709
|
+
replay: async id => ofId(id),
|
|
710
|
+
replayStream: (id, ~fromSeq as _=?) => ofId(id)->Stream.fromIterable,
|
|
711
|
+
replayEnvelopes: (id, ~fromSeq as _=?) =>
|
|
712
|
+
profileEvents.contents->Array.filter(e => e.id == id)->Stream.fromIterable,
|
|
713
|
+
appendStream: (_seqNr, _id, stream) => stream->Stream.runDrain,
|
|
714
|
+
latestSnapshot: async _ => {
|
|
715
|
+
profileSnapshotReads := profileSnapshotReads.contents + 1
|
|
716
|
+
Ok(None)
|
|
717
|
+
},
|
|
718
|
+
writeSnapshot: async (_, _) => Ok(),
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
module ProfileHandler = Aggregate_Callback.Make(ProfileSpec, ProfileDecision, ProfileOps)
|
|
723
|
+
|
|
724
|
+
let profileGenerate = generator(
|
|
725
|
+
~serviceName=ProfileSpec.name,
|
|
726
|
+
~commandSchema=ProfileSpec.commandSchema->S.castToUnknown,
|
|
727
|
+
~componentKind=CommandGenerator_Callback.Aggregate,
|
|
728
|
+
)
|
|
729
|
+
|
|
730
|
+
let deliverProfile = async (cmd: Message.commandJson) => {
|
|
731
|
+
let item = {
|
|
732
|
+
ReventlessInfra.CommandTopic.command: cross(Inline, cmd)->Message.decodeCommand'(
|
|
733
|
+
ProfileSpec.Id.schema,
|
|
734
|
+
ProfileSpec.commandSchema,
|
|
735
|
+
),
|
|
736
|
+
reference: cmd.meta.msgId,
|
|
737
|
+
}
|
|
738
|
+
let _ = await ProfileHandler.handleCommands(Stream.fromIterable([item]))->Effect.runPromise
|
|
739
|
+
cmd.meta.msgId
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
let profileCommand = (~command, ~id, ~identity, ~args=[]) =>
|
|
743
|
+
profileGenerate->generateOne(~command, ~args=Array.concat([("id", str(id))], args), ~identity)
|
|
744
|
+
|
|
745
|
+
describe("acting on an aggregate owned by its id:", () => {
|
|
746
|
+
let _ = beforeEach(() => {
|
|
747
|
+
profileEvents := []
|
|
748
|
+
profileSnapshotReads := 0
|
|
749
|
+
ProfileHandler.resetCache()
|
|
750
|
+
})
|
|
751
|
+
|
|
752
|
+
let create = async (~id, ~identity) =>
|
|
753
|
+
await deliverProfile(
|
|
754
|
+
await profileCommand(~command="Create", ~id, ~identity, ~args=[("name", str("Ann"))]),
|
|
755
|
+
)
|
|
756
|
+
|
|
757
|
+
// The creating command is checked too: there is no history to wait for.
|
|
758
|
+
testPromise("only the holder of the id, or an operator, may create it", async () => {
|
|
759
|
+
let mine = await create(~id="cust-A", ~identity=owner)
|
|
760
|
+
let theirs = await create(~id="cust-C", ~identity=stranger)
|
|
761
|
+
let ops = await create(~id="cust-D", ~identity=operator)
|
|
762
|
+
expect((outcomeOf(mine), outcomeOf(theirs), outcomeOf(ops)))->toEqual((
|
|
763
|
+
Accepted,
|
|
764
|
+
forbidden,
|
|
765
|
+
Accepted,
|
|
766
|
+
))
|
|
767
|
+
})
|
|
768
|
+
|
|
769
|
+
let renameAs = async identity =>
|
|
770
|
+
await deliverProfile(
|
|
771
|
+
await profileCommand(
|
|
772
|
+
~command="Rename",
|
|
773
|
+
~id="cust-A",
|
|
774
|
+
~identity,
|
|
775
|
+
~args=[("newName", str("Renamed"))],
|
|
776
|
+
),
|
|
777
|
+
)
|
|
778
|
+
|
|
779
|
+
let table: array<(string, Reventless.Identity.t, outcome)> = [
|
|
780
|
+
("the holder of the id", owner, Accepted),
|
|
781
|
+
("another owner", stranger, forbidden),
|
|
782
|
+
("an operator, on the holder's behalf", operator, Accepted),
|
|
783
|
+
("the platform's own IAM traffic", iam, Accepted),
|
|
784
|
+
("an anonymous caller", Reventless.Identity.anonymous, forbidden),
|
|
785
|
+
]
|
|
786
|
+
table->Array.forEach(((who, identity, expected)) =>
|
|
787
|
+
testPromise(
|
|
788
|
+
`${who}: ${expected == Accepted ? "accepted" : "refused before decide"}`,
|
|
789
|
+
async () => {
|
|
790
|
+
let _ = await create(~id="cust-A", ~identity=owner)
|
|
791
|
+
expect(outcomeOf(await renameAs(identity)))->toEqual(expected)
|
|
792
|
+
},
|
|
793
|
+
)
|
|
794
|
+
)
|
|
795
|
+
|
|
796
|
+
testPromise("an internal route, with no claim, is not checked", async () => {
|
|
797
|
+
let _ = await create(~id="cust-A", ~identity=owner)
|
|
798
|
+
let cmd = await profileCommand(
|
|
799
|
+
~command="Rename",
|
|
800
|
+
~id="cust-A",
|
|
801
|
+
~identity=stranger,
|
|
802
|
+
~args=[("newName", str("Renamed"))],
|
|
803
|
+
)
|
|
804
|
+
expect(outcomeOf(await deliverProfile(withoutClaim(cmd))))->toEqual(Accepted)
|
|
805
|
+
})
|
|
806
|
+
|
|
807
|
+
testPromise("an aggregate owned by its id keeps its snapshots", async () => {
|
|
808
|
+
let _ = await create(~id="cust-A", ~identity=owner)
|
|
809
|
+
expect(profileSnapshotReads.contents)->toBe(1)
|
|
810
|
+
})
|
|
811
|
+
})
|
|
@@ -979,6 +979,358 @@ globalThis.describe("acting on an owned thing — aggregate specifics:", () => {
|
|
|
979
979
|
});
|
|
980
980
|
});
|
|
981
981
|
|
|
982
|
+
let Id = Id$Reventless.Owned({
|
|
983
|
+
schema: Id$Reventless.StringPure.schema,
|
|
984
|
+
make: prim => prim,
|
|
985
|
+
makeFromString: prim => prim,
|
|
986
|
+
toString: prim => prim,
|
|
987
|
+
cmp: Id$Reventless.StringPure.cmp
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
let name$3 = "OwnedProfile";
|
|
991
|
+
|
|
992
|
+
let commandSchema$2 = Sury.union([
|
|
993
|
+
Sury.$schema(s => ({
|
|
994
|
+
TAG: "Create",
|
|
995
|
+
name: s.m(Sury.string)
|
|
996
|
+
})),
|
|
997
|
+
Sury.$schema(s => ({
|
|
998
|
+
TAG: "Rename",
|
|
999
|
+
newName: s.m(Sury.string)
|
|
1000
|
+
}))
|
|
1001
|
+
]);
|
|
1002
|
+
|
|
1003
|
+
let eventSchema$2 = Sury.union([
|
|
1004
|
+
Sury.$schema(s => ({
|
|
1005
|
+
TAG: "Created",
|
|
1006
|
+
name: s.m(Sury.string)
|
|
1007
|
+
})),
|
|
1008
|
+
Sury.$schema(s => ({
|
|
1009
|
+
TAG: "Renamed",
|
|
1010
|
+
newName: s.m(Sury.string)
|
|
1011
|
+
}))
|
|
1012
|
+
]);
|
|
1013
|
+
|
|
1014
|
+
let errorSchema$2 = Sury.union([
|
|
1015
|
+
Sury.literal("AlreadyCreated"),
|
|
1016
|
+
Sury.literal("NotFound")
|
|
1017
|
+
]);
|
|
1018
|
+
|
|
1019
|
+
let moduleUrl$2 = import.meta.url;
|
|
1020
|
+
|
|
1021
|
+
function authorizationOf$2(param) {
|
|
1022
|
+
return "AllowAuthenticated";
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
function commandTransition$2(param) {
|
|
1026
|
+
return "Undeclared";
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
let traits$2 = [];
|
|
1030
|
+
|
|
1031
|
+
let ProfileSpec = {
|
|
1032
|
+
Id: Id,
|
|
1033
|
+
name: name$3,
|
|
1034
|
+
commandSchema: commandSchema$2,
|
|
1035
|
+
eventSchema: eventSchema$2,
|
|
1036
|
+
errorSchema: errorSchema$2,
|
|
1037
|
+
moduleUrl: moduleUrl$2,
|
|
1038
|
+
authorizationOf: authorizationOf$2,
|
|
1039
|
+
commandTransition: commandTransition$2,
|
|
1040
|
+
traits: traits$2
|
|
1041
|
+
};
|
|
1042
|
+
|
|
1043
|
+
let stateSchema$1 = Sury.union([
|
|
1044
|
+
Sury.literal("Absent"),
|
|
1045
|
+
Sury.$schema(s => ({
|
|
1046
|
+
TAG: "Present",
|
|
1047
|
+
name: s.m(Sury.string)
|
|
1048
|
+
}))
|
|
1049
|
+
]);
|
|
1050
|
+
|
|
1051
|
+
let snapshot$1 = {
|
|
1052
|
+
interval: 1,
|
|
1053
|
+
stateSchema: stateSchema$1
|
|
1054
|
+
};
|
|
1055
|
+
|
|
1056
|
+
function evolve$2(_state, event) {
|
|
1057
|
+
if (event.TAG === "Created") {
|
|
1058
|
+
return {
|
|
1059
|
+
TAG: "Present",
|
|
1060
|
+
name: event.name
|
|
1061
|
+
};
|
|
1062
|
+
} else {
|
|
1063
|
+
return {
|
|
1064
|
+
TAG: "Present",
|
|
1065
|
+
name: event.newName
|
|
1066
|
+
};
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
function decide$2(state, command) {
|
|
1071
|
+
if (typeof state !== "object") {
|
|
1072
|
+
if (command.TAG === "Create") {
|
|
1073
|
+
return {
|
|
1074
|
+
TAG: "Ok",
|
|
1075
|
+
_0: [{
|
|
1076
|
+
TAG: "Created",
|
|
1077
|
+
name: command.name
|
|
1078
|
+
}]
|
|
1079
|
+
};
|
|
1080
|
+
} else {
|
|
1081
|
+
return {
|
|
1082
|
+
TAG: "Error",
|
|
1083
|
+
_0: "NotFound"
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
1086
|
+
} else if (command.TAG === "Create") {
|
|
1087
|
+
return {
|
|
1088
|
+
TAG: "Error",
|
|
1089
|
+
_0: "AlreadyCreated"
|
|
1090
|
+
};
|
|
1091
|
+
} else {
|
|
1092
|
+
return {
|
|
1093
|
+
TAG: "Ok",
|
|
1094
|
+
_0: [{
|
|
1095
|
+
TAG: "Renamed",
|
|
1096
|
+
newName: command.newName
|
|
1097
|
+
}]
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
let ProfileDecision = {
|
|
1103
|
+
Spec: undefined,
|
|
1104
|
+
stateSchema: stateSchema$1,
|
|
1105
|
+
initialState: "Absent",
|
|
1106
|
+
snapshot: snapshot$1,
|
|
1107
|
+
moduleUrl: moduleUrl$2,
|
|
1108
|
+
evolve: evolve$2,
|
|
1109
|
+
decide: decide$2
|
|
1110
|
+
};
|
|
1111
|
+
|
|
1112
|
+
let profileEvents = {
|
|
1113
|
+
contents: []
|
|
1114
|
+
};
|
|
1115
|
+
|
|
1116
|
+
let profileSnapshotReads = {
|
|
1117
|
+
contents: 0
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
let name$4 = "OwnedProfileEventLog";
|
|
1121
|
+
|
|
1122
|
+
let Spec$1 = {
|
|
1123
|
+
Id: undefined,
|
|
1124
|
+
name: name$4,
|
|
1125
|
+
eventSchema: eventSchema$2
|
|
1126
|
+
};
|
|
1127
|
+
|
|
1128
|
+
function make$1(param, param$1, param$2) {
|
|
1129
|
+
return 0;
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
let EventLog$1 = {
|
|
1133
|
+
Spec: Spec$1,
|
|
1134
|
+
make: make$1
|
|
1135
|
+
};
|
|
1136
|
+
|
|
1137
|
+
function ofId$1(id) {
|
|
1138
|
+
return profileEvents.contents.filter(e => e.id === id).map(e => e.event);
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
async function eventLog_append$1(_seqNr, _id, newEvents) {
|
|
1142
|
+
profileEvents.contents = profileEvents.contents.concat(newEvents);
|
|
1143
|
+
return {
|
|
1144
|
+
TAG: "Ok",
|
|
1145
|
+
_0: undefined
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
let eventLog_replay$1 = ofId$1;
|
|
1150
|
+
|
|
1151
|
+
function eventLog_replayStream$1(id, param) {
|
|
1152
|
+
return Stream.fromIterable(ofId$1(id));
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
function eventLog_replayEnvelopes$1(id, param) {
|
|
1156
|
+
return Stream.fromIterable(profileEvents.contents.filter(e => e.id === id));
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
function eventLog_appendStream$1(_seqNr, _id, stream) {
|
|
1160
|
+
return Stream.runDrain(stream);
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
async function eventLog_latestSnapshot$1(param) {
|
|
1164
|
+
profileSnapshotReads.contents = profileSnapshotReads.contents + 1 | 0;
|
|
1165
|
+
return {
|
|
1166
|
+
TAG: "Ok",
|
|
1167
|
+
_0: undefined
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
async function eventLog_writeSnapshot$1(param, param$1) {
|
|
1172
|
+
return {
|
|
1173
|
+
TAG: "Ok",
|
|
1174
|
+
_0: undefined
|
|
1175
|
+
};
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
let eventLog$1 = {
|
|
1179
|
+
append: eventLog_append$1,
|
|
1180
|
+
replay: eventLog_replay$1,
|
|
1181
|
+
replayStream: eventLog_replayStream$1,
|
|
1182
|
+
replayEnvelopes: eventLog_replayEnvelopes$1,
|
|
1183
|
+
appendStream: eventLog_appendStream$1,
|
|
1184
|
+
latestSnapshot: eventLog_latestSnapshot$1,
|
|
1185
|
+
writeSnapshot: eventLog_writeSnapshot$1
|
|
1186
|
+
};
|
|
1187
|
+
|
|
1188
|
+
let ProfileOps = {
|
|
1189
|
+
Spec: undefined,
|
|
1190
|
+
EventLog: EventLog$1,
|
|
1191
|
+
ofId: ofId$1,
|
|
1192
|
+
eventLog: eventLog$1
|
|
1193
|
+
};
|
|
1194
|
+
|
|
1195
|
+
let ProfileHandler = Aggregate_Callback$ReventlessCore.Make({
|
|
1196
|
+
Id: Id,
|
|
1197
|
+
name: name$3,
|
|
1198
|
+
eventSchema: eventSchema$2,
|
|
1199
|
+
errorSchema: errorSchema$2,
|
|
1200
|
+
commandSchema: commandSchema$2,
|
|
1201
|
+
moduleUrl: moduleUrl$2,
|
|
1202
|
+
authorizationOf: authorizationOf$2,
|
|
1203
|
+
commandTransition: commandTransition$2,
|
|
1204
|
+
traits: traits$2
|
|
1205
|
+
})({
|
|
1206
|
+
initialState: "Absent",
|
|
1207
|
+
evolve: evolve$2,
|
|
1208
|
+
decide: decide$2,
|
|
1209
|
+
snapshot: snapshot$1,
|
|
1210
|
+
moduleUrl: moduleUrl$2
|
|
1211
|
+
})({
|
|
1212
|
+
Spec: {
|
|
1213
|
+
Id: Id,
|
|
1214
|
+
name: name$3,
|
|
1215
|
+
eventSchema: eventSchema$2,
|
|
1216
|
+
errorSchema: errorSchema$2,
|
|
1217
|
+
commandSchema: commandSchema$2,
|
|
1218
|
+
moduleUrl: moduleUrl$2,
|
|
1219
|
+
authorizationOf: authorizationOf$2,
|
|
1220
|
+
commandTransition: commandTransition$2,
|
|
1221
|
+
traits: traits$2
|
|
1222
|
+
},
|
|
1223
|
+
EventLog: {
|
|
1224
|
+
Spec: {
|
|
1225
|
+
Id: Id,
|
|
1226
|
+
name: name$4,
|
|
1227
|
+
eventSchema: eventSchema$2
|
|
1228
|
+
},
|
|
1229
|
+
make: make$1
|
|
1230
|
+
},
|
|
1231
|
+
eventLog: eventLog$1
|
|
1232
|
+
});
|
|
1233
|
+
|
|
1234
|
+
let profileGenerate = generator(name$3, commandSchema$2, "Aggregate");
|
|
1235
|
+
|
|
1236
|
+
async function deliverProfile(cmd) {
|
|
1237
|
+
let item_command = Message$ReventlessCore.decodeCommand$p(CommandTopic_Helpers$ReventlessCore.encodeCommandJson(cmd), Id.schema, commandSchema$2);
|
|
1238
|
+
let item_reference = cmd.meta.msgId;
|
|
1239
|
+
let item = {
|
|
1240
|
+
command: item_command,
|
|
1241
|
+
reference: item_reference
|
|
1242
|
+
};
|
|
1243
|
+
await Effect.runPromise(ProfileHandler.handleCommands(Stream.fromIterable([item])));
|
|
1244
|
+
return cmd.meta.msgId;
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
function profileCommand(command, id, identity, argsOpt) {
|
|
1248
|
+
let args = argsOpt !== undefined ? argsOpt : [];
|
|
1249
|
+
return generateOne(profileGenerate, command, [[
|
|
1250
|
+
"id",
|
|
1251
|
+
id
|
|
1252
|
+
]].concat(args), identity);
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
globalThis.describe("acting on an aggregate owned by its id:", () => {
|
|
1256
|
+
globalThis.beforeEach(() => {
|
|
1257
|
+
profileEvents.contents = [];
|
|
1258
|
+
profileSnapshotReads.contents = 0;
|
|
1259
|
+
ProfileHandler.resetCache();
|
|
1260
|
+
});
|
|
1261
|
+
let create = async (id, identity) => await deliverProfile(await profileCommand("Create", id, identity, [[
|
|
1262
|
+
"name",
|
|
1263
|
+
"Ann"
|
|
1264
|
+
]]));
|
|
1265
|
+
globalThis.test("only the holder of the id, or an operator, may create it", async () => {
|
|
1266
|
+
let mine = await create("cust-A", owner);
|
|
1267
|
+
let theirs = await create("cust-C", stranger);
|
|
1268
|
+
let ops = await create("cust-D", operator);
|
|
1269
|
+
globalThis.expect([
|
|
1270
|
+
outcomeOf(mine),
|
|
1271
|
+
outcomeOf(theirs),
|
|
1272
|
+
outcomeOf(ops)
|
|
1273
|
+
]).toEqual([
|
|
1274
|
+
"Accepted",
|
|
1275
|
+
forbidden,
|
|
1276
|
+
"Accepted"
|
|
1277
|
+
]);
|
|
1278
|
+
});
|
|
1279
|
+
let renameAs = async identity => await deliverProfile(await profileCommand("Rename", "cust-A", identity, [[
|
|
1280
|
+
"newName",
|
|
1281
|
+
"Renamed"
|
|
1282
|
+
]]));
|
|
1283
|
+
let table = [
|
|
1284
|
+
[
|
|
1285
|
+
"the holder of the id",
|
|
1286
|
+
owner,
|
|
1287
|
+
"Accepted"
|
|
1288
|
+
],
|
|
1289
|
+
[
|
|
1290
|
+
"another owner",
|
|
1291
|
+
stranger,
|
|
1292
|
+
forbidden
|
|
1293
|
+
],
|
|
1294
|
+
[
|
|
1295
|
+
"an operator, on the holder's behalf",
|
|
1296
|
+
operator,
|
|
1297
|
+
"Accepted"
|
|
1298
|
+
],
|
|
1299
|
+
[
|
|
1300
|
+
"the platform's own IAM traffic",
|
|
1301
|
+
iam,
|
|
1302
|
+
"Accepted"
|
|
1303
|
+
],
|
|
1304
|
+
[
|
|
1305
|
+
"an anonymous caller",
|
|
1306
|
+
Identity$Reventless.anonymous,
|
|
1307
|
+
forbidden
|
|
1308
|
+
]
|
|
1309
|
+
];
|
|
1310
|
+
table.forEach(param => {
|
|
1311
|
+
let expected = param[2];
|
|
1312
|
+
let identity = param[1];
|
|
1313
|
+
globalThis.test(param[0] + `: ` + (
|
|
1314
|
+
expected === "Accepted" ? "accepted" : "refused before decide"
|
|
1315
|
+
), async () => {
|
|
1316
|
+
await create("cust-A", owner);
|
|
1317
|
+
globalThis.expect(outcomeOf(await renameAs(identity))).toEqual(expected);
|
|
1318
|
+
});
|
|
1319
|
+
});
|
|
1320
|
+
globalThis.test("an internal route, with no claim, is not checked", async () => {
|
|
1321
|
+
await create("cust-A", owner);
|
|
1322
|
+
let cmd = await profileCommand("Rename", "cust-A", stranger, [[
|
|
1323
|
+
"newName",
|
|
1324
|
+
"Renamed"
|
|
1325
|
+
]]);
|
|
1326
|
+
globalThis.expect(outcomeOf(await deliverProfile(withoutClaim(cmd)))).toEqual("Accepted");
|
|
1327
|
+
});
|
|
1328
|
+
globalThis.test("an aggregate owned by its id keeps its snapshots", async () => {
|
|
1329
|
+
await create("cust-A", owner);
|
|
1330
|
+
globalThis.expect(profileSnapshotReads.contents).toBe(1);
|
|
1331
|
+
});
|
|
1332
|
+
});
|
|
1333
|
+
|
|
982
1334
|
let OuterEventLog;
|
|
983
1335
|
|
|
984
1336
|
export {
|
|
@@ -1019,5 +1371,14 @@ export {
|
|
|
1019
1371
|
paths,
|
|
1020
1372
|
forbidden,
|
|
1021
1373
|
table,
|
|
1374
|
+
ProfileSpec,
|
|
1375
|
+
ProfileDecision,
|
|
1376
|
+
profileEvents,
|
|
1377
|
+
profileSnapshotReads,
|
|
1378
|
+
ProfileOps,
|
|
1379
|
+
ProfileHandler,
|
|
1380
|
+
profileGenerate,
|
|
1381
|
+
deliverProfile,
|
|
1382
|
+
profileCommand,
|
|
1022
1383
|
}
|
|
1023
1384
|
/* moduleUrl Not a pure module */
|
|
@@ -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
|
})
|