@reventlessdev/trait-attachments 1.0.0-alpha.1

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2019-2026 Martin Lorenz
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # @reventlessdev/trait-attachments
2
+
3
+ A **domain trait**: an ordered set of stored files on an entity — attach, remove,
4
+ choose the primary, caption — as domain facts. The rules are a module the host calls;
5
+ the types are the host's own:
6
+
7
+ | Part | Where |
8
+ |---|---|
9
+ | The set's rules | `src/Attachments_Rules.res` (`empty`, `op`, `fact`, `decide`, `evolve`, `primaryOf`) |
10
+ | The host contract, as a type | `src/Attachments.res` (`module type Binding`) |
11
+ | The conformance suite | `src/Attachments_Conformance.res` (`Make(Binding).register()`) |
12
+ | The emitter | `src/Attachments_Scaffold.res` (`emit(~config, ~into, ~tests)`) |
13
+
14
+ `Attachments_Rules` is compiled code a host imports at runtime, so a change to it is a
15
+ behavior change for every host: version it `fix:`/`feat:` accordingly.
16
+
17
+ The graft *is* a StateChangeSlice, so the emitter writes nearly all of it: the slice
18
+ spec, its behavior and the conformance binding, whole. Only the view's projection is
19
+ printed as a patch — the view already exists, and placing an arm in an ordered `switch`
20
+ is an AST operation a text splice gets silently wrong.
21
+
22
+ Storage is the platform's (`UploadableImage.t` names the store; mint, presign and
23
+ pending-expiry are shipped). This package owns the rules of the *set*:
24
+
25
+ - **Attach and remove are idempotent** — a ref already in the set, or already out
26
+ of it, appends nothing; a removed ref can come back.
27
+ - **The primary is one of the set.** Until one is chosen the first attached stands
28
+ in; choosing the current primary appends nothing; removing the chosen one lets
29
+ the first remaining stand in again.
30
+ - **A caption belongs to a member** — refused for a ref that is not attached,
31
+ a no-op when repeated.
32
+ - **The read model carries the primary as one string** beside the set, so a card
33
+ or a gallery tile has an image to draw without reading the set.
34
+
35
+ Unlike the geocoding trait this one writes nothing back into its host: the graft
36
+ *is* a StateChangeSlice of the host, so the contract is over that slice.
37
+
38
+ ## Grafting a host
39
+
40
+ 1. Run the emitter. Every `--key` is a field of its config, and it validates them, so
41
+ run it once to be told what it wants:
42
+
43
+ ```sh
44
+ pnpm exec graft-trait @reventlessdev/trait-attachments \
45
+ --into src/Product --tests tests/Product \
46
+ --entity Product --entityId productId --noun Image \
47
+ --file productImage --created ProductAdded --view Products
48
+ ```
49
+
50
+ The field is named for the store it draws from, and `--noun` is what this host calls
51
+ one attachment — it runs through every name the graft declares.
52
+
53
+ 2. Fill the `TODO(graft)` markers. They are the host's own policy: the events its
54
+ refusal turns on, the error it raises, and the `else if` in `decide` that raises it
55
+ ahead of the set's rules. A graft with no extra refusal is a complete graft, so
56
+ leaving them is legitimate.
57
+
58
+ 3. Paste the projection patch, and run the emitted conformance binding — 14 assertions,
59
+ over your constructors, not the trait's.
60
+
61
+ The specimen hosts are `examples/online-shop-hybrid/catalog`'s `ProductImages` and
62
+ `CategoryImages`.
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@reventlessdev/trait-attachments",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "Attachments domain trait: rules, host contract, conformance suite and emitter for an ordered set of stored files on an entity, with a primary and captions",
5
+ "license": "Apache-2.0",
6
+ "files": [
7
+ "src/**/*.res",
8
+ "src/**/*.resi",
9
+ "src/**/*.res.mjs",
10
+ "rescript.json",
11
+ "README.md"
12
+ ],
13
+ "dependencies": {
14
+ "sury": "11.0.0-rc.2",
15
+ "sury-ppx": "11.0.0-rc.2",
16
+ "@reventlessdev/reventless-gwt": "1.0.0-alpha.199",
17
+ "@reventlessdev/reventless-spec": "3.0.0-alpha.125"
18
+ },
19
+ "devDependencies": {
20
+ "rescript": "12.3.0"
21
+ },
22
+ "peerDependencies": {
23
+ "rescript": "12.3.0"
24
+ },
25
+ "publishConfig": {
26
+ "registry": "https://registry.npmjs.org"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/ReventlessDev/reventless-core.git",
31
+ "directory": "traits/attachments"
32
+ },
33
+ "scripts": {
34
+ "build": "rescript build",
35
+ "start": "rescript watch",
36
+ "clean": "rescript clean",
37
+ "rebuild": "pnpm run clean && pnpm run build",
38
+ "test": "echo \"The conformance suite runs in its hosts; see README.md.\""
39
+ }
40
+ }
package/rescript.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@reventlessdev/trait-attachments",
3
+ "package-specs": {
4
+ "module": "esmodule",
5
+ "in-source": true
6
+ },
7
+ "namespace": "TraitAttachments",
8
+ "ppx-flags": [
9
+ "sury-ppx/bin"
10
+ ],
11
+ "warnings": {
12
+ "error": "-44+101"
13
+ },
14
+ "sources": [
15
+ {
16
+ "dir": "src",
17
+ "subdirs": true
18
+ }
19
+ ],
20
+ "dependencies": [
21
+ "sury",
22
+ "@reventlessdev/reventless-spec",
23
+ "@reventlessdev/reventless-gwt"
24
+ ],
25
+ "compiler-flags": [],
26
+ "suffix": ".res.mjs"
27
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ The host contract of the attachments trait: an ordered set of stored files on
3
+ an entity, with a primary and a caption per member. Unlike the geocoding trait this
4
+ one writes nothing back — the graft *is* a StateChangeSlice of the host — so the
5
+ contract is over that slice. The rules live in `Attachments_Rules` and are asserted
6
+ through a host by `Attachments_Conformance`; the spec surface the host maps onto
7
+ them is written by `Attachments_Scaffold`.
8
+ */
9
+
10
+ /**
11
+ This trait's own account of itself — see `AddressGeocoding.declaration` for why the
12
+ version is read rather than written.
13
+
14
+ `WritesBack`: the graft's commands append this trait's facts onto the host's own
15
+ slice, which is what a host grafting it is asking for.
16
+ */
17
+ let declaration: Reventless.Trait.t = {
18
+ trait: "@reventlessdev/trait-attachments",
19
+ version: Reventless.PackageVersion.fromModuleUrl(%raw(`import.meta.url`)),
20
+ posture: WritesBack,
21
+ }
22
+
23
+ /**
24
+ The platform capabilities a host of this trait needs: **none**.
25
+
26
+ Not an oversight and not a placeholder. What this trait needs is an object
27
+ *store*, and a store need is declared by the field that carries `@storageRef` —
28
+ it travels as `pluginStructure.requiredStores`, keyed by `(plugin, store)`, and
29
+ `CapabilityNeed` deliberately excludes it. So the empty list is the true answer,
30
+ and stating it lets a listing distinguish "needs nothing brokered" from "nobody
31
+ has said".
32
+ */
33
+ let capabilityNeeds: array<Reventless.CapabilityNeed.t> = []
34
+
35
+ /** One host, bound. `ref` is the stored file's reference (a `StorageRef` today),
36
+ abstract so a richer attachment identity is a re-instantiation, not a break. */
37
+ module type Binding = {
38
+ type ref
39
+ /** Two refs that differ. */
40
+ let refA: ref
41
+ let refB: ref
42
+
43
+ module Spec: ReventlessGwt.Behavior_GWT.BehaviorSpec
44
+ module Behavior: ReventlessGwt.Behavior_GWT.Behavior with module Spec = Spec
45
+
46
+ /** History that brings the entity into existence with an empty set. */
47
+ let created: array<Spec.consumedEvent>
48
+ /** The set's own facts, as the slice consumes them. */
49
+ let attachedC: ref => Spec.consumedEvent
50
+ let removedC: ref => Spec.consumedEvent
51
+ let primarySetC: ref => Spec.consumedEvent
52
+ let altTextSetC: (ref, string) => Spec.consumedEvent
53
+
54
+ let attach: ref => Spec.command
55
+ let remove: ref => Spec.command
56
+ let setPrimary: ref => Spec.command
57
+ let setAltText: (ref, string) => Spec.command
58
+
59
+ /** The set's facts, as the slice emits them. */
60
+ let attached: ref => Spec.event
61
+ let removed: ref => Spec.event
62
+ let primarySet: ref => Spec.event
63
+ let altTextSet: (ref, string) => Spec.event
64
+ /** The refusal for a primary or caption on a ref that is not in the set. */
65
+ let notAttached: Spec.error
66
+ }
@@ -0,0 +1,19 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as PackageVersion$Reventless from "@reventlessdev/reventless-spec/src/PackageVersion.res.mjs";
4
+
5
+ let declaration_version = PackageVersion$Reventless.fromModuleUrl(import.meta.url);
6
+
7
+ let declaration = {
8
+ trait: "@reventlessdev/trait-attachments",
9
+ version: declaration_version,
10
+ posture: "WritesBack"
11
+ };
12
+
13
+ let capabilityNeeds = [];
14
+
15
+ export {
16
+ declaration,
17
+ capabilityNeeds,
18
+ }
19
+ /* declaration Not a pure module */
@@ -0,0 +1,88 @@
1
+ /**
2
+ The conformance suite, run by a host against its own graft. `Make(Binding).register()`
3
+ inside a Jest test file registers one `describe` block over the binding.
4
+ */
5
+
6
+ /** The suite's title, composed once and read twice: the suite registers it,
7
+ and `certify-trait` computes the same string to find the run's assertions in
8
+ a test report. Exported rather than inlined so neither side parses the
9
+ other's prose. */
10
+ let suiteName = (host: string) => `${host} conforms to the attachments trait`
11
+
12
+ module Make = (B: Attachments.Binding) => {
13
+ module G = ReventlessGwt.Behavior_GWT.Make(B.Spec, B.Behavior)
14
+
15
+ let withA = Array.concat(B.created, [B.attachedC(B.refA)])
16
+ let withAB = Array.concat(withA, [B.attachedC(B.refB)])
17
+
18
+ let register = () =>
19
+ G.describe(suiteName(B.Spec.name), () => {
20
+ G.test("the first attachment is appended", () =>
21
+ G.givenEvents(B.created)->G.whenCmd(B.attach(B.refA))->G.thenEvent(B.attached(B.refA))
22
+ )
23
+
24
+ G.test("attaching a ref already in the set is a no-op", () =>
25
+ G.givenEvents(withA)->G.whenCmd(B.attach(B.refA))->G.thenNoEvent
26
+ )
27
+
28
+ G.test("a second ref extends the set rather than replacing it", () =>
29
+ G.givenEvents(withA)->G.whenCmd(B.attach(B.refB))->G.thenEvent(B.attached(B.refB))
30
+ )
31
+
32
+ G.test("removing an attached ref is appended", () =>
33
+ G.givenEvents(withA)->G.whenCmd(B.remove(B.refA))->G.thenEvent(B.removed(B.refA))
34
+ )
35
+
36
+ G.test("removing a ref not in the set is a no-op", () =>
37
+ G.givenEvents(withA)->G.whenCmd(B.remove(B.refB))->G.thenNoEvent
38
+ )
39
+
40
+ G.test("a removed ref can be attached again", () =>
41
+ G.givenEvents(Array.concat(withA, [B.removedC(B.refA)]))
42
+ ->G.whenCmd(B.attach(B.refA))
43
+ ->G.thenEvent(B.attached(B.refA))
44
+ )
45
+
46
+ G.test("the primary must be in the set", () =>
47
+ G.givenEvents(withA)->G.whenCmd(B.setPrimary(B.refB))->G.thenError(B.notAttached)
48
+ )
49
+
50
+ G.test("the first attached is the primary until one is chosen", () =>
51
+ G.givenEvents(withAB)->G.whenCmd(B.setPrimary(B.refA))->G.thenNoEvent
52
+ )
53
+
54
+ G.test("choosing another primary is appended", () =>
55
+ G.givenEvents(withAB)->G.whenCmd(B.setPrimary(B.refB))->G.thenEvent(B.primarySet(B.refB))
56
+ )
57
+
58
+ G.test("choosing the current primary is a no-op", () =>
59
+ G.givenEvents(Array.concat(withAB, [B.primarySetC(B.refB)]))
60
+ ->G.whenCmd(B.setPrimary(B.refB))
61
+ ->G.thenNoEvent
62
+ )
63
+
64
+ G.test("removing the chosen primary lets the first remaining stand in", () =>
65
+ G.givenEvents(Array.concat(withAB, [B.primarySetC(B.refB), B.removedC(B.refB)]))
66
+ ->G.whenCmd(B.setPrimary(B.refA))
67
+ ->G.thenNoEvent
68
+ )
69
+
70
+ G.test("a caption needs its ref in the set", () =>
71
+ G.givenEvents(withA)
72
+ ->G.whenCmd(B.setAltText(B.refB, "side"))
73
+ ->G.thenError(B.notAttached)
74
+ )
75
+
76
+ G.test("a caption is appended", () =>
77
+ G.givenEvents(withA)
78
+ ->G.whenCmd(B.setAltText(B.refA, "front"))
79
+ ->G.thenEvent(B.altTextSet(B.refA, "front"))
80
+ )
81
+
82
+ G.test("repeating the caption is a no-op", () =>
83
+ G.givenEvents(Array.concat(withA, [B.altTextSetC(B.refA, "front")]))
84
+ ->G.whenCmd(B.setAltText(B.refA, "front"))
85
+ ->G.thenNoEvent
86
+ )
87
+ })
88
+ }
@@ -0,0 +1,49 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as Behavior_GWT$ReventlessGwt from "@reventlessdev/reventless-gwt/src/Behavior_GWT.res.mjs";
4
+
5
+ function suiteName(host) {
6
+ return host + ` conforms to the attachments trait`;
7
+ }
8
+
9
+ function Make(B) {
10
+ let $$let = B.Behavior;
11
+ let G = Behavior_GWT$ReventlessGwt.Make(B.Spec)({
12
+ initialState: $$let.initialState,
13
+ evolve: $$let.evolve,
14
+ decide: $$let.decide
15
+ });
16
+ let withA = B.created.concat([B.attachedC(B.refA)]);
17
+ let withAB = withA.concat([B.attachedC(B.refB)]);
18
+ let register = () => G.describe(suiteName(B.Spec.name), () => {
19
+ G.test("the first attachment is appended", () => G.thenEvent(G.whenCmd(G.givenEvents(B.created), B.attach(B.refA)), B.attached(B.refA)));
20
+ G.test("attaching a ref already in the set is a no-op", () => G.thenNoEvent(G.whenCmd(G.givenEvents(withA), B.attach(B.refA))));
21
+ G.test("a second ref extends the set rather than replacing it", () => G.thenEvent(G.whenCmd(G.givenEvents(withA), B.attach(B.refB)), B.attached(B.refB)));
22
+ G.test("removing an attached ref is appended", () => G.thenEvent(G.whenCmd(G.givenEvents(withA), B.remove(B.refA)), B.removed(B.refA)));
23
+ G.test("removing a ref not in the set is a no-op", () => G.thenNoEvent(G.whenCmd(G.givenEvents(withA), B.remove(B.refB))));
24
+ G.test("a removed ref can be attached again", () => G.thenEvent(G.whenCmd(G.givenEvents(withA.concat([B.removedC(B.refA)])), B.attach(B.refA)), B.attached(B.refA)));
25
+ G.test("the primary must be in the set", () => G.thenError(G.whenCmd(G.givenEvents(withA), B.setPrimary(B.refB)), B.notAttached));
26
+ G.test("the first attached is the primary until one is chosen", () => G.thenNoEvent(G.whenCmd(G.givenEvents(withAB), B.setPrimary(B.refA))));
27
+ G.test("choosing another primary is appended", () => G.thenEvent(G.whenCmd(G.givenEvents(withAB), B.setPrimary(B.refB)), B.primarySet(B.refB)));
28
+ G.test("choosing the current primary is a no-op", () => G.thenNoEvent(G.whenCmd(G.givenEvents(withAB.concat([B.primarySetC(B.refB)])), B.setPrimary(B.refB))));
29
+ G.test("removing the chosen primary lets the first remaining stand in", () => G.thenNoEvent(G.whenCmd(G.givenEvents(withAB.concat([
30
+ B.primarySetC(B.refB),
31
+ B.removedC(B.refB)
32
+ ])), B.setPrimary(B.refA))));
33
+ G.test("a caption needs its ref in the set", () => G.thenError(G.whenCmd(G.givenEvents(withA), B.setAltText(B.refB, "side")), B.notAttached));
34
+ G.test("a caption is appended", () => G.thenEvent(G.whenCmd(G.givenEvents(withA), B.setAltText(B.refA, "front")), B.altTextSet(B.refA, "front")));
35
+ G.test("repeating the caption is a no-op", () => G.thenNoEvent(G.whenCmd(G.givenEvents(withA.concat([B.altTextSetC(B.refA, "front")])), B.setAltText(B.refA, "front"))));
36
+ });
37
+ return {
38
+ G: G,
39
+ withA: withA,
40
+ withAB: withAB,
41
+ register: register
42
+ };
43
+ }
44
+
45
+ export {
46
+ suiteName,
47
+ Make,
48
+ }
49
+ /* Behavior_GWT-ReventlessGwt Not a pure module */
@@ -0,0 +1,101 @@
1
+ /**
2
+ The set's rules, compiled once and called by every host: attach and remove are
3
+ idempotent, the primary is one of the set with the first attached standing in
4
+ until one is chosen, and a caption belongs to a member.
5
+
6
+ A host maps its own constructors onto `op` and `fact` and keeps the spec surface —
7
+ the variants, their annotations, its own refusals. Nothing here knows what an
8
+ entity is. `Attachments_Conformance` asserts these rules through a host.
9
+ */
10
+
11
+ /** The stored file's reference. A `StorageRef` path today, so `string` carries it
12
+ without a wrapper the host would have to unwrap on every arm. */
13
+ type ref = string
14
+
15
+ /** Refolded per decision, never stored — a StateChangeSlice's state is. */
16
+ type t = {
17
+ attached: array<ref>,
18
+ /** Only the one chosen explicitly; see `effectivePrimary`. */
19
+ primary: option<ref>,
20
+ altTexts: array<(ref, string)>,
21
+ }
22
+
23
+ let empty = {attached: [], primary: None, altTexts: []}
24
+
25
+ /** What a host asks the set to do. */
26
+ type op =
27
+ | /** `altText` is the caption a host may supply with the file itself. */
28
+ Attach({ref: ref, altText: option<string>})
29
+ | Remove({ref: ref})
30
+ | SetPrimary({ref: ref})
31
+ | SetAltText({ref: ref, altText: string})
32
+
33
+ /** What the set decided, for the host to name in its own event. */
34
+ type fact =
35
+ | Attached({ref: ref, altText: option<string>})
36
+ | Removed({ref: ref})
37
+ | PrimarySet({ref: ref})
38
+ | AltTextSet({ref: ref, altText: string})
39
+
40
+ /** The primary a reader should show: the one chosen, else the first attached, so
41
+ a set never shows no file while it holds one. The read model applies the same
42
+ rule over its own rows, which is why this takes the two values and not `t`. */
43
+ let primaryOf = (~chosen: option<ref>, ~attached: array<ref>) =>
44
+ switch chosen {
45
+ | Some(_) as p => p
46
+ | None => attached->Array.get(0)
47
+ }
48
+
49
+ let effectivePrimary = t => primaryOf(~chosen=t.primary, ~attached=t.attached)
50
+
51
+ let altTextOf = (t, ref) => t.altTexts->Array.find(((r, _)) => r == ref)->Option.map(((_, t)) => t)
52
+
53
+ let evolve = (t, fact) =>
54
+ switch fact {
55
+ | Attached({ref, altText}) =>
56
+ t.attached->Array.includes(ref)
57
+ ? t
58
+ : {
59
+ ...t,
60
+ attached: t.attached->Array.concat([ref]),
61
+ altTexts: switch altText {
62
+ | Some(text) => t.altTexts->Array.concat([(ref, text)])
63
+ | None => t.altTexts
64
+ },
65
+ }
66
+ | Removed({ref}) => {
67
+ attached: t.attached->Array.filter(r => r != ref),
68
+ primary: t.primary == Some(ref) ? None : t.primary,
69
+ altTexts: t.altTexts->Array.filter(((r, _)) => r != ref),
70
+ }
71
+ | PrimarySet({ref}) => {...t, primary: Some(ref)}
72
+ | AltTextSet({ref, altText}) => {
73
+ ...t,
74
+ altTexts: t.altTexts->Array.filter(((r, _)) => r != ref)->Array.concat([(ref, altText)]),
75
+ }
76
+ }
77
+
78
+ /** `Ok(None)` is the no-op a retried command must produce; the one refusal the set
79
+ owns is a primary or a caption on a ref it does not hold. */
80
+ let decide = (t, op): result<option<fact>, [#NotAttached]> =>
81
+ switch op {
82
+ | Attach({ref, altText}) =>
83
+ t.attached->Array.includes(ref) ? Ok(None) : Ok(Some(Attached({ref, altText})))
84
+ | Remove({ref}) => t.attached->Array.includes(ref) ? Ok(Some(Removed({ref: ref}))) : Ok(None)
85
+ | SetPrimary({ref}) =>
86
+ if !(t.attached->Array.includes(ref)) {
87
+ Error(#NotAttached)
88
+ } else if effectivePrimary(t) == Some(ref) {
89
+ Ok(None)
90
+ } else {
91
+ Ok(Some(PrimarySet({ref: ref})))
92
+ }
93
+ | SetAltText({ref, altText}) =>
94
+ if !(t.attached->Array.includes(ref)) {
95
+ Error(#NotAttached)
96
+ } else if altTextOf(t, ref) == Some(altText) {
97
+ Ok(None)
98
+ } else {
99
+ Ok(Some(AltTextSet({ref, altText})))
100
+ }
101
+ }