@reventlessdev/reventless-seed 1.0.0-alpha.1 → 1.0.0-alpha.11
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 +75 -0
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/.sourcedirs.json +1 -0
- package/lib/bs/ReventlessSeed.cmi +0 -0
- package/lib/bs/ReventlessSeed.cmt +0 -0
- package/lib/bs/ReventlessSeed.mlmap +3 -0
- package/lib/bs/compiler-info.json +2 -2
- package/lib/bs/src/Seed-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed.ast +0 -0
- package/lib/bs/src/Seed.res +19 -0
- package/lib/bs/src/Seed.res.mjs +6 -0
- package/lib/bs/src/Seed_Client-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Client-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Client-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Client.ast +0 -0
- package/lib/bs/src/Seed_Client.res +62 -11
- package/lib/bs/src/Seed_Client.res.mjs +51 -5
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Connect.ast +0 -0
- package/lib/bs/src/Seed_Connect.res +86 -0
- package/lib/bs/src/Seed_Connect.res.mjs +62 -0
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Prompt.ast +0 -0
- package/lib/bs/src/Seed_Prompt.res +178 -0
- package/lib/bs/src/Seed_Prompt.res.mjs +172 -0
- package/lib/bs/src/Seed_Random-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Runner.ast +0 -0
- package/lib/bs/src/Seed_Runner.res +122 -6
- package/lib/bs/src/Seed_Runner.res.mjs +87 -1
- package/lib/bs/src/Seed_Types-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Upload.ast +0 -0
- package/lib/bs/src/Seed_Upload.res +40 -42
- package/lib/bs/src/Seed_Upload.res.mjs +65 -69
- package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmj +0 -0
- package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/Seed_RandomTest.ast +0 -0
- package/lib/bs/tests/Seed_RandomTest.res +160 -0
- package/lib/bs/tests/Seed_RandomTest.res.mjs +208 -0
- package/lib/ocaml/.compiler.log +2 -2
- package/lib/ocaml/ReventlessSeed.cmi +0 -0
- package/lib/ocaml/ReventlessSeed.cmt +0 -0
- package/lib/ocaml/ReventlessSeed.mlmap +3 -0
- package/lib/ocaml/Seed-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed.ast +0 -0
- package/lib/ocaml/Seed.res +19 -0
- package/lib/ocaml/Seed_Client-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Client-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Client-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Client.ast +0 -0
- package/lib/ocaml/Seed_Client.res +62 -11
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Connect.ast +0 -0
- package/lib/ocaml/Seed_Connect.res +86 -0
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Prompt.ast +0 -0
- package/lib/ocaml/Seed_Prompt.res +178 -0
- package/lib/ocaml/Seed_Random-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_RandomTest.ast +0 -0
- package/lib/ocaml/Seed_RandomTest.res +160 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Runner.ast +0 -0
- package/lib/ocaml/Seed_Runner.res +122 -6
- package/lib/ocaml/Seed_Types-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Upload.ast +0 -0
- package/lib/ocaml/Seed_Upload.res +40 -42
- package/package.json +6 -2
- package/rescript.json +9 -1
- package/src/Seed.res +19 -0
- package/src/Seed.res.mjs +6 -0
- package/src/Seed_Client.res +62 -11
- package/src/Seed_Client.res.mjs +51 -5
- package/src/Seed_Connect.res +86 -0
- package/src/Seed_Connect.res.mjs +62 -0
- package/src/Seed_Prompt.res +178 -0
- package/src/Seed_Prompt.res.mjs +172 -0
- package/src/Seed_Runner.res +122 -6
- package/src/Seed_Runner.res.mjs +87 -1
- package/src/Seed_Upload.res +40 -42
- package/src/Seed_Upload.res.mjs +65 -69
- package/tests/Seed_RandomTest.res +160 -0
- package/tests/Seed_RandomTest.res.mjs +208 -0
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
// Seed-time asset upload against the framework's
|
|
1
|
+
// Seed-time asset upload against the framework's upload contract (route B).
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// stores and the UI renders.
|
|
3
|
+
// Minting is a GraphQL mutation on the domain API — `Upload_Presign(store, fileName,
|
|
4
|
+
// contentType)` — authenticated by the same bearer the seed already uses for commands
|
|
5
|
+
// (no separate endpoint, no anonymous surface). `uploadAsset` issues it through the
|
|
6
|
+
// shared `Seed_Client`, then PUTs the bytes to the returned `uploadUrl` and resolves
|
|
7
|
+
// `Ok(storageRef)` — the same `/{prefix}/{key}` ref a command stores and the UI renders.
|
|
8
8
|
//
|
|
9
|
-
// The seed runs under Node, so
|
|
10
|
-
// absolute URL against the
|
|
11
|
-
//
|
|
12
|
-
//
|
|
9
|
+
// The seed runs under Node, so a relative `uploadUrl` (the local dev server returns a
|
|
10
|
+
// same-origin `/{prefix}/{key}`) is resolved to an absolute URL against the client's
|
|
11
|
+
// endpoint origin before the PUT; AWS returns an absolute presigned S3 URL (used as-is).
|
|
12
|
+
// The PUT itself is unauthenticated — presigned on AWS, open on the local route.
|
|
13
13
|
|
|
14
14
|
type fetchInit = {method: string, headers: dict<string>, body: string}
|
|
15
15
|
type response
|
|
16
16
|
|
|
17
17
|
@val external fetch: (string, fetchInit) => promise<response> = "fetch"
|
|
18
|
-
@send external responseJson: response => promise<JSON.t> = "json"
|
|
19
18
|
@send external responseText: response => promise<string> = "text"
|
|
20
19
|
@get external responseOk: response => bool = "ok"
|
|
21
20
|
@get external responseStatus: response => int = "status"
|
|
22
21
|
|
|
23
|
-
// Resolve a possibly-relative URL against a base (WHATWG URL semantics: an
|
|
24
|
-
//
|
|
22
|
+
// Resolve a possibly-relative URL against a base (WHATWG URL semantics: an absolute
|
|
23
|
+
// `input` ignores `base`, a relative one is resolved against it).
|
|
25
24
|
type url
|
|
26
25
|
@new external makeUrl: (string, string) => url = "URL"
|
|
27
26
|
@get external urlHref: url => string = "href"
|
|
@@ -38,51 +37,50 @@ let asString = (json: JSON.t): option<string> =>
|
|
|
38
37
|
| _ => None
|
|
39
38
|
}
|
|
40
39
|
|
|
41
|
-
//
|
|
42
|
-
//
|
|
40
|
+
// Whether this run should skip uploads entirely. `SEED_SKIP_UPLOADS` seeds domain data
|
|
41
|
+
// fast, or skips a broken/absent upload path without editing the data set.
|
|
42
|
+
let uploadsSkipped = (): bool => Seed_Prompt.envValue("SEED_SKIP_UPLOADS")->Option.isSome
|
|
43
|
+
|
|
44
|
+
// A `fetch` that reports an unreachable endpoint as `Error` instead of throwing.
|
|
43
45
|
let tryFetch = async (url: string, init: fetchInit): result<response, string> =>
|
|
44
46
|
try Ok(await fetch(url, init)) catch {
|
|
45
47
|
| _ => Error(`cannot reach ${url}`)
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
/**
|
|
49
|
-
* Uploads `bytes` under `fileName`/`contentType`
|
|
51
|
+
* Uploads `bytes` under `fileName`/`contentType` into `store` (the qualified
|
|
52
|
+
* `{plugin}.{store}` the asset's field declares, e.g. `"Catalog.productImages"`) and
|
|
50
53
|
* resolves `Ok(storageRef)` (the stored `/{prefix}/{key}` ref), or `Error(msg)`
|
|
51
|
-
* describing the step that failed.
|
|
52
|
-
*
|
|
53
|
-
* the PUT itself is unauthenticated — presigned on AWS, open locally).
|
|
54
|
+
* describing the step that failed. Mints through the domain API's `Upload_Presign`
|
|
55
|
+
* mutation on `client`, then PUTs the bytes to the returned `uploadUrl`.
|
|
54
56
|
*/
|
|
55
57
|
let uploadAsset = async (
|
|
56
|
-
~
|
|
58
|
+
~client: Seed_Client.t,
|
|
59
|
+
~store: string,
|
|
57
60
|
~bytes: string,
|
|
58
61
|
~fileName: string,
|
|
59
62
|
~contentType: string,
|
|
60
|
-
~authToken: option<string>=?,
|
|
61
63
|
): result<string, string> => {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
let
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
64
|
+
// GraphQL string literals: JSON-quote the argument values (JSON string escaping is a
|
|
65
|
+
// superset-safe subset of GraphQL's for these ASCII-ish inputs).
|
|
66
|
+
let q = s => JSON.stringify(JSON.Encode.string(s))
|
|
67
|
+
let query = `mutation { r: Upload_Presign(store: ${q(store)}, fileName: ${q(fileName)}, contentType: ${q(contentType)}) { uploadUrl storageRef } }`
|
|
68
|
+
|
|
69
|
+
let presign = try Ok(await Seed_Client.gql(client, ~query, ~label="Upload_Presign")) catch {
|
|
70
|
+
| Seed_Types.Failed(m) => Error(m)
|
|
71
|
+
| _ => Error("Upload_Presign: unexpected error")
|
|
72
|
+
}
|
|
72
73
|
|
|
73
|
-
switch
|
|
74
|
-
| Error(m) => Error(`presign: ${m}
|
|
75
|
-
| Ok(
|
|
76
|
-
let
|
|
77
|
-
Error(`presign failed with HTTP ${(presignRes->responseStatus)->Int.toString}: ${detail}`)
|
|
78
|
-
| Ok(presignRes) =>
|
|
79
|
-
let json = await presignRes->responseJson
|
|
74
|
+
switch presign {
|
|
75
|
+
| Error(m) => Error(`presign: ${m}`)
|
|
76
|
+
| Ok(data) =>
|
|
77
|
+
let r = data->field("r")->Option.getOr(JSON.Encode.null)
|
|
80
78
|
switch (
|
|
81
|
-
|
|
82
|
-
|
|
79
|
+
r->field("uploadUrl")->Option.flatMap(asString),
|
|
80
|
+
r->field("storageRef")->Option.flatMap(asString),
|
|
83
81
|
) {
|
|
84
82
|
| (Some(uploadUrl), Some(storageRef)) =>
|
|
85
|
-
let putUrl = urlHref(makeUrl(uploadUrl,
|
|
83
|
+
let putUrl = urlHref(makeUrl(uploadUrl, Seed_Client.endpoint(client)))
|
|
86
84
|
switch await tryFetch(
|
|
87
85
|
putUrl,
|
|
88
86
|
{method: "PUT", headers: Dict.fromArray([("content-type", contentType)]), body: bytes},
|
|
@@ -93,7 +91,7 @@ let uploadAsset = async (
|
|
|
93
91
|
let detail = await putRes->responseText
|
|
94
92
|
Error(`upload PUT failed with HTTP ${(putRes->responseStatus)->Int.toString}: ${detail}`)
|
|
95
93
|
}
|
|
96
|
-
| _ => Error("
|
|
94
|
+
| _ => Error("Upload_Presign response missing uploadUrl/storageRef")
|
|
97
95
|
}
|
|
98
96
|
}
|
|
99
97
|
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
3
|
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
4
|
+
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
5
|
+
import * as Seed_Types$ReventlessSeed from "./Seed_Types.res.mjs";
|
|
6
|
+
import * as Seed_Client$ReventlessSeed from "./Seed_Client.res.mjs";
|
|
7
|
+
import * as Seed_Prompt$ReventlessSeed from "./Seed_Prompt.res.mjs";
|
|
4
8
|
|
|
5
9
|
function field(json, key) {
|
|
6
10
|
if (typeof json === "object" && json !== null && !Array.isArray(json)) {
|
|
@@ -14,6 +18,10 @@ function asString(json) {
|
|
|
14
18
|
}
|
|
15
19
|
}
|
|
16
20
|
|
|
21
|
+
function uploadsSkipped() {
|
|
22
|
+
return Stdlib_Option.isSome(Seed_Prompt$ReventlessSeed.envValue("SEED_SKIP_UPLOADS"));
|
|
23
|
+
}
|
|
24
|
+
|
|
17
25
|
async function tryFetch(url, init) {
|
|
18
26
|
try {
|
|
19
27
|
return {
|
|
@@ -28,91 +36,79 @@ async function tryFetch(url, init) {
|
|
|
28
36
|
}
|
|
29
37
|
}
|
|
30
38
|
|
|
31
|
-
async function uploadAsset(
|
|
32
|
-
let
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
method: "POST",
|
|
51
|
-
headers: presignHeaders,
|
|
52
|
-
body: presignBody
|
|
53
|
-
});
|
|
54
|
-
if (m.TAG !== "Ok") {
|
|
39
|
+
async function uploadAsset(client, store, bytes, fileName, contentType) {
|
|
40
|
+
let query = `mutation { r: Upload_Presign(store: ` + JSON.stringify(store) + `, fileName: ` + JSON.stringify(fileName) + `, contentType: ` + JSON.stringify(contentType) + `) { uploadUrl storageRef } }`;
|
|
41
|
+
let presign;
|
|
42
|
+
try {
|
|
43
|
+
presign = {
|
|
44
|
+
TAG: "Ok",
|
|
45
|
+
_0: await Seed_Client$ReventlessSeed.gql(client, query, "Upload_Presign")
|
|
46
|
+
};
|
|
47
|
+
} catch (raw_m) {
|
|
48
|
+
let m = Primitive_exceptions.internalToException(raw_m);
|
|
49
|
+
presign = m.RE_EXN_ID === Seed_Types$ReventlessSeed.Failed ? ({
|
|
50
|
+
TAG: "Error",
|
|
51
|
+
_0: m._1
|
|
52
|
+
}) : ({
|
|
53
|
+
TAG: "Error",
|
|
54
|
+
_0: "Upload_Presign: unexpected error"
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (presign.TAG !== "Ok") {
|
|
55
58
|
return {
|
|
56
59
|
TAG: "Error",
|
|
57
|
-
_0: `presign: ` +
|
|
60
|
+
_0: `presign: ` + presign._0
|
|
58
61
|
};
|
|
59
62
|
}
|
|
60
|
-
let
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
let match$1 = Stdlib_Option.flatMap(field(json, "storageRef"), asString);
|
|
65
|
-
if (match === undefined) {
|
|
66
|
-
return {
|
|
67
|
-
TAG: "Error",
|
|
68
|
-
_0: "presign response missing uploadUrl/storageRef"
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
if (match$1 === undefined) {
|
|
72
|
-
return {
|
|
73
|
-
TAG: "Error",
|
|
74
|
-
_0: "presign response missing uploadUrl/storageRef"
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
let putUrl = new URL(match, uploadEndpoint).href;
|
|
78
|
-
let m$1 = await tryFetch(putUrl, {
|
|
79
|
-
method: "PUT",
|
|
80
|
-
headers: Object.fromEntries([[
|
|
81
|
-
"content-type",
|
|
82
|
-
contentType
|
|
83
|
-
]]),
|
|
84
|
-
body: bytes
|
|
85
|
-
});
|
|
86
|
-
if (m$1.TAG !== "Ok") {
|
|
87
|
-
return {
|
|
88
|
-
TAG: "Error",
|
|
89
|
-
_0: `upload PUT: ` + m$1._0
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
let putRes = m$1._0;
|
|
93
|
-
if (putRes.ok) {
|
|
94
|
-
return {
|
|
95
|
-
TAG: "Ok",
|
|
96
|
-
_0: match$1
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
let detail = await putRes.text();
|
|
63
|
+
let r = Stdlib_Option.getOr(field(presign._0, "r"), null);
|
|
64
|
+
let match = Stdlib_Option.flatMap(field(r, "uploadUrl"), asString);
|
|
65
|
+
let match$1 = Stdlib_Option.flatMap(field(r, "storageRef"), asString);
|
|
66
|
+
if (match === undefined) {
|
|
100
67
|
return {
|
|
101
68
|
TAG: "Error",
|
|
102
|
-
_0:
|
|
69
|
+
_0: "Upload_Presign response missing uploadUrl/storageRef"
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (match$1 === undefined) {
|
|
73
|
+
return {
|
|
74
|
+
TAG: "Error",
|
|
75
|
+
_0: "Upload_Presign response missing uploadUrl/storageRef"
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
let putUrl = new URL(match, Seed_Client$ReventlessSeed.endpoint(client)).href;
|
|
79
|
+
let m$1 = await tryFetch(putUrl, {
|
|
80
|
+
method: "PUT",
|
|
81
|
+
headers: Object.fromEntries([[
|
|
82
|
+
"content-type",
|
|
83
|
+
contentType
|
|
84
|
+
]]),
|
|
85
|
+
body: bytes
|
|
86
|
+
});
|
|
87
|
+
if (m$1.TAG !== "Ok") {
|
|
88
|
+
return {
|
|
89
|
+
TAG: "Error",
|
|
90
|
+
_0: `upload PUT: ` + m$1._0
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
let putRes = m$1._0;
|
|
94
|
+
if (putRes.ok) {
|
|
95
|
+
return {
|
|
96
|
+
TAG: "Ok",
|
|
97
|
+
_0: match$1
|
|
103
98
|
};
|
|
104
99
|
}
|
|
105
|
-
let detail
|
|
100
|
+
let detail = await putRes.text();
|
|
106
101
|
return {
|
|
107
102
|
TAG: "Error",
|
|
108
|
-
_0: `
|
|
103
|
+
_0: `upload PUT failed with HTTP ` + putRes.status.toString() + `: ` + detail
|
|
109
104
|
};
|
|
110
105
|
}
|
|
111
106
|
|
|
112
107
|
export {
|
|
113
108
|
field,
|
|
114
109
|
asString,
|
|
110
|
+
uploadsSkipped,
|
|
115
111
|
tryFetch,
|
|
116
112
|
uploadAsset,
|
|
117
113
|
}
|
|
118
|
-
/*
|
|
114
|
+
/* Seed_Prompt-ReventlessSeed Not a pure module */
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
open JestGlobals
|
|
2
|
+
|
|
3
|
+
// `Seed_Random` exists so that two seed runs against a fresh store produce the
|
|
4
|
+
// same data — that is what makes a seeded store usable as a screenshot baseline
|
|
5
|
+
// or a comparison target. Its correctness is therefore not "looks random" but
|
|
6
|
+
// "repeats exactly, and stays inside the bounds it advertises".
|
|
7
|
+
//
|
|
8
|
+
// The suite is written around that: every case below fails for a generator that
|
|
9
|
+
// is subtly non-reproducible or that walks out of its range, and none of them
|
|
10
|
+
// would notice a change of algorithm that kept both properties.
|
|
11
|
+
|
|
12
|
+
describe("Seed_Random:", () => {
|
|
13
|
+
let take = (~seed, ~n) => {
|
|
14
|
+
let r = Seed_Random.make(~seed)
|
|
15
|
+
Array.make(~length=n, 0.0)->Array.map(_ => Seed_Random.float(r))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("reproducibility — the reason this module exists:", () => {
|
|
19
|
+
testSync("the same seed replays the same sequence", () =>
|
|
20
|
+
expect(take(~seed=42, ~n=25))->toEqual(take(~seed=42, ~n=25))
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
// Without this, a generator that returned a constant would satisfy the
|
|
24
|
+
// case above and nothing else in the suite would catch it.
|
|
25
|
+
testSync("different seeds diverge", () =>
|
|
26
|
+
expect(take(~seed=42, ~n=25) == take(~seed=43, ~n=25))->toBe(false)
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
testSync("a fresh generator does not continue the previous one", () => {
|
|
30
|
+
let r = Seed_Random.make(~seed=7)
|
|
31
|
+
let first = Seed_Random.float(r)
|
|
32
|
+
let _ = Seed_Random.float(r)
|
|
33
|
+
expect(Seed_Random.float(Seed_Random.make(~seed=7)))->toEqual(first)
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
describe("float stays a unit interval:", () => {
|
|
38
|
+
// mulberry32's last step is a signed 32-bit value folded back to unsigned.
|
|
39
|
+
// Get that fold wrong and roughly half the draws come out negative, which
|
|
40
|
+
// every consumer below (int, pick, sampleWeighted) silently mis-scales.
|
|
41
|
+
testSync("every draw is in [0, 1)", () =>
|
|
42
|
+
expect(take(~seed=99, ~n=500)->Array.every(f => f >= 0.0 && f < 1.0))->toBe(true)
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
testSync("and the draws are not all the same value", () => {
|
|
46
|
+
let draws = take(~seed=99, ~n=100)
|
|
47
|
+
expect(draws->Array.some(f => f != draws->Array.getUnsafe(0)))->toBe(true)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
describe("int is inclusive on both ends, as documented:", () => {
|
|
52
|
+
let draws = (~min, ~max, ~n) => {
|
|
53
|
+
let r = Seed_Random.make(~seed=5)
|
|
54
|
+
Array.make(~length=n, 0)->Array.map(_ => Seed_Random.int(r, ~min, ~max))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
testSync("never escapes the range", () =>
|
|
58
|
+
expect(draws(~min=3, ~max=7, ~n=500)->Array.every(i => i >= 3 && i <= 7))->toBe(true)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
// The documented contract is "inclusive on both ends"; an off-by-one in the
|
|
62
|
+
// `max - min + 1` scaling shows up as a bound that is never drawn.
|
|
63
|
+
testSync("reaches both bounds", () => {
|
|
64
|
+
let d = draws(~min=3, ~max=7, ~n=500)
|
|
65
|
+
expect((d->Array.includes(3), d->Array.includes(7)))->toEqual((true, true))
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
testSync("a single-value range is that value", () =>
|
|
69
|
+
expect(draws(~min=4, ~max=4, ~n=10)->Array.every(i => i == 4))->toBe(true)
|
|
70
|
+
)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe("pick:", () => {
|
|
74
|
+
testSync("returns None for an empty array", () =>
|
|
75
|
+
expect(Seed_Random.make(~seed=1)->Seed_Random.pick([]))->toEqual(None)
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
testSync("always lands on a member", () => {
|
|
79
|
+
let r = Seed_Random.make(~seed=2)
|
|
80
|
+
let xs = ["a", "b", "c", "d"]
|
|
81
|
+
let hits = Array.make(~length=200, "")->Array.map(_ =>
|
|
82
|
+
r->Seed_Random.pickOr(~fallback="MISS", xs)
|
|
83
|
+
)
|
|
84
|
+
expect(hits->Array.every(h => xs->Array.includes(h)))->toBe(true)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
testSync("pickOr falls back only when there is nothing to pick", () =>
|
|
88
|
+
expect(Seed_Random.make(~seed=3)->Seed_Random.pickOr(~fallback="fallback", []))->toBe(
|
|
89
|
+
"fallback",
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
describe("sampleWeighted is without replacement:", () => {
|
|
95
|
+
let equal = xs => xs->Array.map(x => (x, 1.0))
|
|
96
|
+
|
|
97
|
+
// The documented shuffle use: equal weights, count = length. If sampling
|
|
98
|
+
// ever replaced instead of removing, duplicates would appear here.
|
|
99
|
+
testSync("count = length yields a permutation, not a multiset", () => {
|
|
100
|
+
let xs = ["a", "b", "c", "d", "e"]
|
|
101
|
+
let out = Seed_Random.make(~seed=11)->Seed_Random.sampleWeighted(equal(xs), ~count=5)
|
|
102
|
+
let sorted = out->Array.toSorted(String.compare)
|
|
103
|
+
expect((out->Array.length, sorted))->toEqual((5, ["a", "b", "c", "d", "e"]))
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
testSync("count < length returns exactly count distinct items", () => {
|
|
107
|
+
let xs = ["a", "b", "c", "d", "e"]
|
|
108
|
+
let out = Seed_Random.make(~seed=12)->Seed_Random.sampleWeighted(equal(xs), ~count=3)
|
|
109
|
+
let distinct = Set.fromArray(out)->Set.size
|
|
110
|
+
expect((out->Array.length, distinct))->toEqual((3, 3))
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
testSync("count > length clamps to the pool", () =>
|
|
114
|
+
expect(
|
|
115
|
+
Seed_Random.make(~seed=13)
|
|
116
|
+
->Seed_Random.sampleWeighted(equal(["a", "b"]), ~count=99)
|
|
117
|
+
->Array.length,
|
|
118
|
+
)->toBe(2)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
testSync("a zero-weight entry is never chosen while others remain", () => {
|
|
122
|
+
let out =
|
|
123
|
+
Seed_Random.make(~seed=14)->Seed_Random.sampleWeighted(
|
|
124
|
+
[("never", 0.0), ("a", 1.0), ("b", 1.0)],
|
|
125
|
+
~count=2,
|
|
126
|
+
)
|
|
127
|
+
expect(out->Array.includes("never"))->toBe(false)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
testSync("is reproducible for a given seed", () => {
|
|
131
|
+
let run = () =>
|
|
132
|
+
Seed_Random.make(~seed=15)->Seed_Random.sampleWeighted(
|
|
133
|
+
equal(["a", "b", "c", "d", "e", "f"]),
|
|
134
|
+
~count=4,
|
|
135
|
+
)
|
|
136
|
+
expect(run())->toEqual(run())
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
describe("zipfWeights skews toward the head:", () => {
|
|
141
|
+
let weights = Seed_Random.zipfWeights(["a", "b", "c", "d", "e"], ~exponent=1.1)
|
|
142
|
+
|
|
143
|
+
testSync("keeps every item, in order", () =>
|
|
144
|
+
expect(weights->Array.map(((item, _)) => item))->toEqual(["a", "b", "c", "d", "e"])
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
testSync("weight decreases monotonically with rank", () => {
|
|
148
|
+
let ws = weights->Array.map(((_, w)) => w)
|
|
149
|
+
let decreasing =
|
|
150
|
+
ws->Array.everyWithIndex((w, i) => i == 0 || w < ws->Array.getUnsafe(i - 1))
|
|
151
|
+
expect(decreasing)->toBe(true)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
testSync("the leader is clear — first outweighs last several times over", () => {
|
|
155
|
+
let first = weights->Array.get(0)->Option.mapOr(0.0, ((_, w)) => w)
|
|
156
|
+
let last = weights->Array.get(4)->Option.mapOr(0.0, ((_, w)) => w)
|
|
157
|
+
expect(first > last *. 3.0)->toBe(true)
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
})
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
|
|
4
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
5
|
+
import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
|
|
6
|
+
import * as Primitive_string from "@rescript/runtime/lib/es6/Primitive_string.js";
|
|
7
|
+
import * as Seed_Random$ReventlessSeed from "../src/Seed_Random.res.mjs";
|
|
8
|
+
|
|
9
|
+
globalThis.describe("Seed_Random:", () => {
|
|
10
|
+
let take = (seed, n) => {
|
|
11
|
+
let r = Seed_Random$ReventlessSeed.make(seed);
|
|
12
|
+
return Stdlib_Array.make(n, 0.0).map(param => Seed_Random$ReventlessSeed.float(r));
|
|
13
|
+
};
|
|
14
|
+
globalThis.describe("reproducibility — the reason this module exists:", () => {
|
|
15
|
+
globalThis.test("the same seed replays the same sequence", () => {
|
|
16
|
+
globalThis.expect(take(42, 25)).toEqual(take(42, 25));
|
|
17
|
+
});
|
|
18
|
+
globalThis.test("different seeds diverge", () => {
|
|
19
|
+
globalThis.expect(Primitive_object.equal(take(42, 25), take(43, 25))).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
globalThis.test("a fresh generator does not continue the previous one", () => {
|
|
22
|
+
let r = Seed_Random$ReventlessSeed.make(7);
|
|
23
|
+
let first = Seed_Random$ReventlessSeed.float(r);
|
|
24
|
+
Seed_Random$ReventlessSeed.float(r);
|
|
25
|
+
globalThis.expect(Seed_Random$ReventlessSeed.float(Seed_Random$ReventlessSeed.make(7))).toEqual(first);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
globalThis.describe("float stays a unit interval:", () => {
|
|
29
|
+
globalThis.test("every draw is in [0, 1)", () => {
|
|
30
|
+
globalThis.expect(take(99, 500).every(f => {
|
|
31
|
+
if (f >= 0.0) {
|
|
32
|
+
return f < 1.0;
|
|
33
|
+
} else {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
})).toBe(true);
|
|
37
|
+
});
|
|
38
|
+
globalThis.test("and the draws are not all the same value", () => {
|
|
39
|
+
let draws = take(99, 100);
|
|
40
|
+
globalThis.expect(draws.some(f => f !== draws[0])).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
globalThis.describe("int is inclusive on both ends, as documented:", () => {
|
|
44
|
+
let draws = (min, max, n) => {
|
|
45
|
+
let r = Seed_Random$ReventlessSeed.make(5);
|
|
46
|
+
return Stdlib_Array.make(n, 0).map(param => Seed_Random$ReventlessSeed.int(r, min, max));
|
|
47
|
+
};
|
|
48
|
+
globalThis.test("never escapes the range", () => {
|
|
49
|
+
globalThis.expect(draws(3, 7, 500).every(i => {
|
|
50
|
+
if (i >= 3) {
|
|
51
|
+
return i <= 7;
|
|
52
|
+
} else {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
})).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
globalThis.test("reaches both bounds", () => {
|
|
58
|
+
let d = draws(3, 7, 500);
|
|
59
|
+
globalThis.expect([
|
|
60
|
+
d.includes(3),
|
|
61
|
+
d.includes(7)
|
|
62
|
+
]).toEqual([
|
|
63
|
+
true,
|
|
64
|
+
true
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
globalThis.test("a single-value range is that value", () => {
|
|
68
|
+
globalThis.expect(draws(4, 4, 10).every(i => i === 4)).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
globalThis.describe("pick:", () => {
|
|
72
|
+
globalThis.test("returns None for an empty array", () => {
|
|
73
|
+
globalThis.expect(Seed_Random$ReventlessSeed.pick(Seed_Random$ReventlessSeed.make(1), [])).toEqual(undefined);
|
|
74
|
+
});
|
|
75
|
+
globalThis.test("always lands on a member", () => {
|
|
76
|
+
let r = Seed_Random$ReventlessSeed.make(2);
|
|
77
|
+
let xs = [
|
|
78
|
+
"a",
|
|
79
|
+
"b",
|
|
80
|
+
"c",
|
|
81
|
+
"d"
|
|
82
|
+
];
|
|
83
|
+
let hits = Stdlib_Array.make(200, "").map(param => Seed_Random$ReventlessSeed.pickOr(r, "MISS", xs));
|
|
84
|
+
globalThis.expect(hits.every(h => xs.includes(h))).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
globalThis.test("pickOr falls back only when there is nothing to pick", () => {
|
|
87
|
+
globalThis.expect(Seed_Random$ReventlessSeed.pickOr(Seed_Random$ReventlessSeed.make(3), "fallback", [])).toBe("fallback");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
globalThis.describe("sampleWeighted is without replacement:", () => {
|
|
91
|
+
let equal = xs => xs.map(x => [
|
|
92
|
+
x,
|
|
93
|
+
1.0
|
|
94
|
+
]);
|
|
95
|
+
globalThis.test("count = length yields a permutation, not a multiset", () => {
|
|
96
|
+
let xs = [
|
|
97
|
+
"a",
|
|
98
|
+
"b",
|
|
99
|
+
"c",
|
|
100
|
+
"d",
|
|
101
|
+
"e"
|
|
102
|
+
];
|
|
103
|
+
let out = Seed_Random$ReventlessSeed.sampleWeighted(Seed_Random$ReventlessSeed.make(11), equal(xs), 5);
|
|
104
|
+
let sorted = out.toSorted(Primitive_string.compare);
|
|
105
|
+
globalThis.expect([
|
|
106
|
+
out.length,
|
|
107
|
+
sorted
|
|
108
|
+
]).toEqual([
|
|
109
|
+
5,
|
|
110
|
+
[
|
|
111
|
+
"a",
|
|
112
|
+
"b",
|
|
113
|
+
"c",
|
|
114
|
+
"d",
|
|
115
|
+
"e"
|
|
116
|
+
]
|
|
117
|
+
]);
|
|
118
|
+
});
|
|
119
|
+
globalThis.test("count < length returns exactly count distinct items", () => {
|
|
120
|
+
let xs = [
|
|
121
|
+
"a",
|
|
122
|
+
"b",
|
|
123
|
+
"c",
|
|
124
|
+
"d",
|
|
125
|
+
"e"
|
|
126
|
+
];
|
|
127
|
+
let out = Seed_Random$ReventlessSeed.sampleWeighted(Seed_Random$ReventlessSeed.make(12), equal(xs), 3);
|
|
128
|
+
let distinct = new Set(out).size;
|
|
129
|
+
globalThis.expect([
|
|
130
|
+
out.length,
|
|
131
|
+
distinct
|
|
132
|
+
]).toEqual([
|
|
133
|
+
3,
|
|
134
|
+
3
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
137
|
+
globalThis.test("count > length clamps to the pool", () => {
|
|
138
|
+
globalThis.expect(Seed_Random$ReventlessSeed.sampleWeighted(Seed_Random$ReventlessSeed.make(13), equal([
|
|
139
|
+
"a",
|
|
140
|
+
"b"
|
|
141
|
+
]), 99).length).toBe(2);
|
|
142
|
+
});
|
|
143
|
+
globalThis.test("a zero-weight entry is never chosen while others remain", () => {
|
|
144
|
+
let out = Seed_Random$ReventlessSeed.sampleWeighted(Seed_Random$ReventlessSeed.make(14), [
|
|
145
|
+
[
|
|
146
|
+
"never",
|
|
147
|
+
0.0
|
|
148
|
+
],
|
|
149
|
+
[
|
|
150
|
+
"a",
|
|
151
|
+
1.0
|
|
152
|
+
],
|
|
153
|
+
[
|
|
154
|
+
"b",
|
|
155
|
+
1.0
|
|
156
|
+
]
|
|
157
|
+
], 2);
|
|
158
|
+
globalThis.expect(out.includes("never")).toBe(false);
|
|
159
|
+
});
|
|
160
|
+
globalThis.test("is reproducible for a given seed", () => {
|
|
161
|
+
let run = () => Seed_Random$ReventlessSeed.sampleWeighted(Seed_Random$ReventlessSeed.make(15), equal([
|
|
162
|
+
"a",
|
|
163
|
+
"b",
|
|
164
|
+
"c",
|
|
165
|
+
"d",
|
|
166
|
+
"e",
|
|
167
|
+
"f"
|
|
168
|
+
]), 4);
|
|
169
|
+
globalThis.expect(run()).toEqual(run());
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
globalThis.describe("zipfWeights skews toward the head:", () => {
|
|
173
|
+
let weights = Seed_Random$ReventlessSeed.zipfWeights([
|
|
174
|
+
"a",
|
|
175
|
+
"b",
|
|
176
|
+
"c",
|
|
177
|
+
"d",
|
|
178
|
+
"e"
|
|
179
|
+
], 1.1);
|
|
180
|
+
globalThis.test("keeps every item, in order", () => {
|
|
181
|
+
globalThis.expect(weights.map(param => param[0])).toEqual([
|
|
182
|
+
"a",
|
|
183
|
+
"b",
|
|
184
|
+
"c",
|
|
185
|
+
"d",
|
|
186
|
+
"e"
|
|
187
|
+
]);
|
|
188
|
+
});
|
|
189
|
+
globalThis.test("weight decreases monotonically with rank", () => {
|
|
190
|
+
let ws = weights.map(param => param[1]);
|
|
191
|
+
let decreasing = ws.every((w, i) => {
|
|
192
|
+
if (i === 0) {
|
|
193
|
+
return true;
|
|
194
|
+
} else {
|
|
195
|
+
return w < ws[i - 1 | 0];
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
globalThis.expect(decreasing).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
globalThis.test("the leader is clear — first outweighs last several times over", () => {
|
|
201
|
+
let first = Stdlib_Option.mapOr(weights[0], 0.0, param => param[1]);
|
|
202
|
+
let last = Stdlib_Option.mapOr(weights[4], 0.0, param => param[1]);
|
|
203
|
+
globalThis.expect(first > last * 3.0).toBe(true);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
/* Not a pure module */
|