@reventlessdev/reventless-seed 1.0.0-alpha.2 → 1.0.0-alpha.20

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.
Files changed (158) hide show
  1. package/CHANGELOG.md +137 -0
  2. package/lib/bs/.compiler.log +2 -2
  3. package/lib/bs/.sourcedirs.json +1 -0
  4. package/lib/bs/ReventlessSeed.cmi +0 -0
  5. package/lib/bs/ReventlessSeed.cmt +0 -0
  6. package/lib/bs/ReventlessSeed.mlmap +5 -0
  7. package/lib/bs/compiler-info.json +2 -2
  8. package/lib/bs/src/Seed-ReventlessSeed.cmi +0 -0
  9. package/lib/bs/src/Seed-ReventlessSeed.cmt +0 -0
  10. package/lib/bs/src/Seed.ast +0 -0
  11. package/lib/bs/src/Seed.res +6 -3
  12. package/lib/bs/src/Seed.res.mjs +3 -0
  13. package/lib/bs/src/Seed_Client-ReventlessSeed.cmi +0 -0
  14. package/lib/bs/src/Seed_Client-ReventlessSeed.cmj +0 -0
  15. package/lib/bs/src/Seed_Client-ReventlessSeed.cmt +0 -0
  16. package/lib/bs/src/Seed_Client.ast +0 -0
  17. package/lib/bs/src/Seed_Client.res +228 -15
  18. package/lib/bs/src/Seed_Client.res.mjs +196 -23
  19. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmi +0 -0
  20. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmj +0 -0
  21. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmt +0 -0
  22. package/lib/bs/src/Seed_Connect.ast +0 -0
  23. package/lib/bs/src/Seed_Connect.res +27 -18
  24. package/lib/bs/src/Seed_Connect.res.mjs +10 -6
  25. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmi +0 -0
  26. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmj +0 -0
  27. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmt +0 -0
  28. package/lib/bs/src/Seed_Prompt.ast +0 -0
  29. package/lib/bs/src/Seed_Prompt.res +110 -35
  30. package/lib/bs/src/Seed_Prompt.res.mjs +73 -8
  31. package/lib/bs/src/Seed_Random-ReventlessSeed.cmt +0 -0
  32. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmi +0 -0
  33. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmj +0 -0
  34. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmt +0 -0
  35. package/lib/bs/src/Seed_Runner.ast +0 -0
  36. package/lib/bs/src/Seed_Runner.res +38 -7
  37. package/lib/bs/src/Seed_Runner.res.mjs +19 -1
  38. package/lib/bs/src/Seed_Types-ReventlessSeed.cmt +0 -0
  39. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmi +0 -0
  40. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmj +0 -0
  41. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmt +0 -0
  42. package/lib/bs/src/Seed_Upload.ast +0 -0
  43. package/lib/bs/src/Seed_Upload.res +40 -42
  44. package/lib/bs/src/Seed_Upload.res.mjs +65 -69
  45. package/lib/bs/src/Seed_Users-ReventlessSeed.cmi +0 -0
  46. package/lib/bs/src/Seed_Users-ReventlessSeed.cmj +0 -0
  47. package/lib/bs/src/Seed_Users-ReventlessSeed.cmt +0 -0
  48. package/lib/bs/src/Seed_Users.ast +0 -0
  49. package/lib/bs/src/Seed_Users.res +80 -0
  50. package/lib/bs/src/Seed_Users.res.mjs +90 -0
  51. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmi +0 -0
  52. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmj +0 -0
  53. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmt +0 -0
  54. package/lib/bs/tests/Seed_ClientTest.ast +0 -0
  55. package/lib/bs/tests/Seed_ClientTest.res +157 -0
  56. package/lib/bs/tests/Seed_ClientTest.res.mjs +167 -0
  57. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmi +0 -0
  58. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmj +0 -0
  59. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmt +0 -0
  60. package/lib/bs/tests/Seed_PromptTest.ast +0 -0
  61. package/lib/bs/tests/Seed_PromptTest.res +59 -0
  62. package/lib/bs/tests/Seed_PromptTest.res.mjs +63 -0
  63. package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmi +0 -0
  64. package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmj +0 -0
  65. package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmt +0 -0
  66. package/lib/bs/tests/Seed_RandomTest.ast +0 -0
  67. package/lib/bs/tests/Seed_RandomTest.res +160 -0
  68. package/lib/bs/tests/Seed_RandomTest.res.mjs +208 -0
  69. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmi +0 -0
  70. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmj +0 -0
  71. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmt +0 -0
  72. package/lib/bs/tests/Seed_UsersTest.ast +0 -0
  73. package/lib/bs/tests/Seed_UsersTest.res +92 -0
  74. package/lib/bs/tests/Seed_UsersTest.res.mjs +95 -0
  75. package/lib/ocaml/.compiler.log +2 -2
  76. package/lib/ocaml/ReventlessSeed.cmi +0 -0
  77. package/lib/ocaml/ReventlessSeed.cmt +0 -0
  78. package/lib/ocaml/ReventlessSeed.mlmap +5 -0
  79. package/lib/ocaml/Seed-ReventlessSeed.cmi +0 -0
  80. package/lib/ocaml/Seed-ReventlessSeed.cmt +0 -0
  81. package/lib/ocaml/Seed.ast +0 -0
  82. package/lib/ocaml/Seed.res +6 -3
  83. package/lib/ocaml/Seed_Client-ReventlessSeed.cmi +0 -0
  84. package/lib/ocaml/Seed_Client-ReventlessSeed.cmj +0 -0
  85. package/lib/ocaml/Seed_Client-ReventlessSeed.cmt +0 -0
  86. package/lib/ocaml/Seed_Client.ast +0 -0
  87. package/lib/ocaml/Seed_Client.res +228 -15
  88. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmi +0 -0
  89. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmj +0 -0
  90. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmt +0 -0
  91. package/lib/ocaml/Seed_ClientTest.ast +0 -0
  92. package/lib/ocaml/Seed_ClientTest.res +157 -0
  93. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmi +0 -0
  94. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmj +0 -0
  95. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmt +0 -0
  96. package/lib/ocaml/Seed_Connect.ast +0 -0
  97. package/lib/ocaml/Seed_Connect.res +27 -18
  98. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmi +0 -0
  99. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmj +0 -0
  100. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
  101. package/lib/ocaml/Seed_Prompt.ast +0 -0
  102. package/lib/ocaml/Seed_Prompt.res +110 -35
  103. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmi +0 -0
  104. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmj +0 -0
  105. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmt +0 -0
  106. package/lib/ocaml/Seed_PromptTest.ast +0 -0
  107. package/lib/ocaml/Seed_PromptTest.res +59 -0
  108. package/lib/ocaml/Seed_Random-ReventlessSeed.cmt +0 -0
  109. package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmi +0 -0
  110. package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmj +0 -0
  111. package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmt +0 -0
  112. package/lib/ocaml/Seed_RandomTest.ast +0 -0
  113. package/lib/ocaml/Seed_RandomTest.res +160 -0
  114. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
  115. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmj +0 -0
  116. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmt +0 -0
  117. package/lib/ocaml/Seed_Runner.ast +0 -0
  118. package/lib/ocaml/Seed_Runner.res +38 -7
  119. package/lib/ocaml/Seed_Types-ReventlessSeed.cmt +0 -0
  120. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmi +0 -0
  121. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmj +0 -0
  122. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmt +0 -0
  123. package/lib/ocaml/Seed_Upload.ast +0 -0
  124. package/lib/ocaml/Seed_Upload.res +40 -42
  125. package/lib/ocaml/Seed_Users-ReventlessSeed.cmi +0 -0
  126. package/lib/ocaml/Seed_Users-ReventlessSeed.cmj +0 -0
  127. package/lib/ocaml/Seed_Users-ReventlessSeed.cmt +0 -0
  128. package/lib/ocaml/Seed_Users.ast +0 -0
  129. package/lib/ocaml/Seed_Users.res +80 -0
  130. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmi +0 -0
  131. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmj +0 -0
  132. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmt +0 -0
  133. package/lib/ocaml/Seed_UsersTest.ast +0 -0
  134. package/lib/ocaml/Seed_UsersTest.res +92 -0
  135. package/package.json +7 -2
  136. package/rescript.json +9 -1
  137. package/src/Seed.res +6 -3
  138. package/src/Seed.res.mjs +3 -0
  139. package/src/Seed_Client.res +228 -15
  140. package/src/Seed_Client.res.mjs +196 -23
  141. package/src/Seed_Connect.res +27 -18
  142. package/src/Seed_Connect.res.mjs +10 -6
  143. package/src/Seed_Prompt.res +110 -35
  144. package/src/Seed_Prompt.res.mjs +73 -8
  145. package/src/Seed_Runner.res +38 -7
  146. package/src/Seed_Runner.res.mjs +19 -1
  147. package/src/Seed_Upload.res +40 -42
  148. package/src/Seed_Upload.res.mjs +65 -69
  149. package/src/Seed_Users.res +80 -0
  150. package/src/Seed_Users.res.mjs +90 -0
  151. package/tests/Seed_ClientTest.res +157 -0
  152. package/tests/Seed_ClientTest.res.mjs +167 -0
  153. package/tests/Seed_PromptTest.res +59 -0
  154. package/tests/Seed_PromptTest.res.mjs +63 -0
  155. package/tests/Seed_RandomTest.res +160 -0
  156. package/tests/Seed_RandomTest.res.mjs +208 -0
  157. package/tests/Seed_UsersTest.res +92 -0
  158. package/tests/Seed_UsersTest.res.mjs +95 -0
@@ -0,0 +1,80 @@
1
+ // The dev accounts a seed run can log in as, read from `.reventless/users.yaml`.
2
+ //
3
+ // Both platforms keep that file beside the package the seed runs from: locally
4
+ // the in-memory auth adapter loads it at startup, and on AWS it is the record of
5
+ // what was created in the Cognito pool. Either way it already holds the
6
+ // credentials an operator would otherwise retype, so the credential prompt
7
+ // offers its entries instead of asking for a password nobody remembers.
8
+ //
9
+ // Absence is normal and silent — the caller falls back to asking. Nothing here
10
+ // throws: a missing, unreadable or malformed file yields no accounts.
11
+
12
+ @module("yaml") external parseYaml: string => JSON.t = "parse"
13
+
14
+ /** One account: what to log in as, and what to send with it. */
15
+ type user = {username: string, password: string, groups: array<string>}
16
+
17
+ let asString = (json: JSON.t): option<string> =>
18
+ switch json {
19
+ | String(s) => Some(s)
20
+ | _ => None
21
+ }
22
+
23
+ let userOf = (json: JSON.t): option<user> =>
24
+ switch json {
25
+ | Object(obj) =>
26
+ switch (
27
+ obj->Dict.get("username")->Option.flatMap(asString),
28
+ obj->Dict.get("password")->Option.flatMap(asString),
29
+ ) {
30
+ | (Some(username), Some(password)) =>
31
+ let groups = switch obj->Dict.get("groups") {
32
+ | Some(JSON.Array(items)) => items->Array.filterMap(asString)
33
+ | _ => []
34
+ }
35
+ Some({username, password, groups})
36
+ | _ => None
37
+ }
38
+ | _ => None
39
+ }
40
+
41
+ /**
42
+ * The accounts a users.yaml document defines, in the order it defines them —
43
+ * the order is what makes "the first one" a stable default.
44
+ *
45
+ * An entry without both a username and a password is skipped rather than
46
+ * rejected: the file is hand-maintained, and one incomplete entry should not
47
+ * cost the operator the rest of the list.
48
+ */
49
+ let parseString = (yamlText: string): array<user> => {
50
+ let doc = try parseYaml(yamlText) catch {
51
+ | _ => JSON.Null
52
+ }
53
+ switch doc {
54
+ | Array(items) => items->Array.filterMap(userOf)
55
+ | _ => []
56
+ }
57
+ }
58
+
59
+ /** Where the file lives for the platform being seeded — the seed runs from that
60
+ package's directory, which is the same cwd the local auth adapter reads. */
61
+ let defaultPath = (): string => NodePath.join([NodeProcess.cwd(), ".reventless", "users.yaml"])
62
+
63
+ /**
64
+ * The accounts on disk, with the path they came from. `None` when there is no
65
+ * readable file with at least one usable account, which the caller reads as
66
+ * "ask for credentials instead".
67
+ */
68
+ let load = (~path: option<string>=?): option<(string, array<user>)> => {
69
+ let file = path->Option.getOr(defaultPath())
70
+ let users = try {
71
+ NodeFs.existsSync(file) ? parseString(NodeFs.readFileSync(file)) : []
72
+ } catch {
73
+ | _ => []
74
+ }
75
+ users->Array.length == 0 ? None : Some((file, users))
76
+ }
77
+
78
+ /** Menu label: the username, plus whatever groups the file records for it. */
79
+ let label = (u: user): string =>
80
+ u.groups->Array.length == 0 ? u.username : `${u.username} [${u.groups->Array.join(", ")}]`
@@ -0,0 +1,90 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as Yaml from "yaml";
4
+ import * as Nodefs from "node:fs";
5
+ import * as Nodepath from "node:path";
6
+ import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
7
+ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
8
+
9
+ function asString(json) {
10
+ if (typeof json === "string") {
11
+ return json;
12
+ }
13
+ }
14
+
15
+ function userOf(json) {
16
+ if (typeof json !== "object" || json === null || Array.isArray(json)) {
17
+ return;
18
+ }
19
+ let match = Stdlib_Option.flatMap(json["username"], asString);
20
+ let match$1 = Stdlib_Option.flatMap(json["password"], asString);
21
+ if (match === undefined) {
22
+ return;
23
+ }
24
+ if (match$1 === undefined) {
25
+ return;
26
+ }
27
+ let match$2 = json["groups"];
28
+ let groups = match$2 !== undefined ? (
29
+ Array.isArray(match$2) ? Stdlib_Array.filterMap(match$2, asString) : []
30
+ ) : [];
31
+ return {
32
+ username: match,
33
+ password: match$1,
34
+ groups: groups
35
+ };
36
+ }
37
+
38
+ function parseString(yamlText) {
39
+ let doc;
40
+ try {
41
+ doc = Yaml.parse(yamlText);
42
+ } catch (exn) {
43
+ doc = null;
44
+ }
45
+ if (Array.isArray(doc)) {
46
+ return Stdlib_Array.filterMap(doc, userOf);
47
+ } else {
48
+ return [];
49
+ }
50
+ }
51
+
52
+ function defaultPath() {
53
+ return Nodepath.join(process.cwd(), ".reventless", "users.yaml");
54
+ }
55
+
56
+ function load(path) {
57
+ let file = Stdlib_Option.getOr(path, defaultPath());
58
+ let users;
59
+ try {
60
+ users = Nodefs.existsSync(file) ? parseString(Nodefs.readFileSync(file, "utf8")) : [];
61
+ } catch (exn) {
62
+ users = [];
63
+ }
64
+ if (users.length === 0) {
65
+ return;
66
+ } else {
67
+ return [
68
+ file,
69
+ users
70
+ ];
71
+ }
72
+ }
73
+
74
+ function label(u) {
75
+ if (u.groups.length === 0) {
76
+ return u.username;
77
+ } else {
78
+ return u.username + ` [` + u.groups.join(", ") + `]`;
79
+ }
80
+ }
81
+
82
+ export {
83
+ asString,
84
+ userOf,
85
+ parseString,
86
+ defaultPath,
87
+ load,
88
+ label,
89
+ }
90
+ /* yaml Not a pure module */
Binary file
@@ -0,0 +1,157 @@
1
+ open JestGlobals
2
+
3
+ // A seed issues hundreds of commands against runtimes a preceding reset left
4
+ // cold, so the endpoint faulting on one of them is a normal event rather than an
5
+ // exceptional one. Before the retry, a single `InternalFailure` in that batch
6
+ // aborted the run and left a half-seeded store that could only be recovered by a
7
+ // full wipe and a re-run — for a request that would have succeeded on being
8
+ // asked again.
9
+ //
10
+ // What makes the retry safe is the classifier, so that is what these pin. A
11
+ // fault on the endpoint's side carries no information about the document and is
12
+ // worth resending; anything that is an *answer* — a validation error, an unknown
13
+ // field, a refused token — comes back identical however many times it is asked,
14
+ // and resending it only delays the failure the operator needs to see.
15
+
16
+ let errors = entries => JSON.Encode.array(entries->Array.map(JSON.Encode.object))
17
+
18
+ let errorOfType = errorType =>
19
+ Dict.fromArray([("errorType", JSON.Encode.string(errorType))])
20
+
21
+ describe("Seed_Client.isTransient:", () => {
22
+ testSync("resends a fault on the endpoint's own side", () =>
23
+ ["InternalFailure", "ServiceUnavailable", "Throttling", "TooManyRequestsException"]
24
+ ->Array.forEach(t =>
25
+ expect(Seed_Client.isTransient(errors([errorOfType(t)])))->toBe(true)
26
+ )
27
+ )
28
+
29
+ // The exact payload the seed aborted on.
30
+ testSync("resends the shape AppSync actually returned", () => {
31
+ let payload = JSON.parseOrThrow(`[{"errorType":"InternalFailure","message":"An internal failure occurred."}]`)
32
+ expect(Seed_Client.isTransient(payload))->toBe(true)
33
+ })
34
+
35
+ testSync("fails fast on an error that is an answer", () =>
36
+ ["ValidationException", "Unauthorized", "FieldUndefined"]->Array.forEach(t =>
37
+ expect(Seed_Client.isTransient(errors([errorOfType(t)])))->toBe(false)
38
+ )
39
+ )
40
+
41
+ // A real error sitting alongside a transient one still means the request was
42
+ // wrong, and resending it cannot make it right.
43
+ testSync("refuses to resend a batch that mixes a real error in", () =>
44
+ expect(
45
+ Seed_Client.isTransient(
46
+ errors([errorOfType("InternalFailure"), errorOfType("ValidationException")]),
47
+ ),
48
+ )->toBe(false)
49
+ )
50
+
51
+ // `Array.every` is vacuously true on an empty array, which would turn "the
52
+ // endpoint said errors but named none" into an endless resend.
53
+ testSync("treats an empty or untyped error list as final", () => {
54
+ expect(Seed_Client.isTransient(errors([])))->toBe(false)
55
+ expect(Seed_Client.isTransient(errors([Dict.fromArray([("message", JSON.Encode.string("boom"))])])))
56
+ ->toBe(false)
57
+ expect(Seed_Client.isTransient(JSON.Encode.null))->toBe(false)
58
+ })
59
+ })
60
+
61
+ // An account's listed groups are not what authorizes it — the token's are. Both
62
+ // platforms narrow a token to the single role the caller last chose, so a seed
63
+ // can log in as an account the user list shows as `[Admin, Shopper]` and present
64
+ // a token carrying `Shopper` alone. The refusal that follows names the field and
65
+ // not the caller, and the account list cannot explain it either. These pin the
66
+ // reading-back that turns that into a sentence.
67
+
68
+ let b64url = payload =>
69
+ payload
70
+ ->NodeBuffer.fromStringUtf8
71
+ ->NodeBuffer.toStringBase64Url
72
+
73
+ let jwt = claims =>
74
+ `header.${b64url(JSON.stringify(JSON.Encode.object(Dict.fromArray(claims))))}.signature`
75
+
76
+ let localToken = claims =>
77
+ `${b64url(JSON.stringify(JSON.Encode.object(Dict.fromArray(claims))))}.signature`
78
+
79
+ let strings = values => JSON.Encode.array(values->Array.map(JSON.Encode.string))
80
+
81
+ let clientWith = token => {
82
+ let c = Seed_Client.make(~config={endpoint: "http://example.invalid/graphql"})
83
+ c->Seed_Client.useToken(token)
84
+ c
85
+ }
86
+
87
+ describe("Seed_Client.effectiveGroups:", () => {
88
+ // A Cognito id token carries its payload in the second segment, a local dev
89
+ // token in the first. Neither platform should have to announce which it is.
90
+ testSync("reads the payload wherever the provider put it", () => {
91
+ expect(
92
+ clientWith(jwt([("cognito:groups", strings(["Admin", "Shopper"]))]))->Seed_Client.effectiveGroups,
93
+ )->toEqual(Some(["Admin", "Shopper"]))
94
+ expect(
95
+ clientWith(localToken([("groups", strings(["Merchandiser"]))]))->Seed_Client.effectiveGroups,
96
+ )->toEqual(Some(["Merchandiser"]))
97
+ })
98
+
99
+ // An opaque bearer is not an error — it just means this can add nothing, and
100
+ // the failure reports what it always did.
101
+ testSync("says nothing about a token it cannot read", () => {
102
+ expect(clientWith("not-a-token")->Seed_Client.effectiveGroups)->toEqual(None)
103
+ expect(
104
+ Seed_Client.make(~config={endpoint: "http://example.invalid/graphql"})
105
+ ->Seed_Client.effectiveGroups,
106
+ )->toEqual(None)
107
+ })
108
+ })
109
+
110
+ describe("Seed_Client.identitySummary:", () => {
111
+ // The case that cost the debugging: eligible in the user list, refused by the
112
+ // token. Naming what it was narrowed FROM is what points at the role switch.
113
+ testSync("names the membership a narrowed token gave up", () =>
114
+ expect(
115
+ clientWith(
116
+ jwt([
117
+ ("cognito:groups", strings(["Shopper"])),
118
+ ("availableRoles", JSON.Encode.string("Admin,Shopper")),
119
+ ]),
120
+ )->Seed_Client.identitySummary,
121
+ )->toEqual(Some("Shopper — narrowed to this role from Admin, Shopper"))
122
+ )
123
+
124
+ // An ordinary login is not narrowed and has nothing to explain.
125
+ testSync("stays quiet about an unnarrowed token", () =>
126
+ expect(
127
+ clientWith(jwt([("cognito:groups", strings(["Admin", "Shopper"]))]))
128
+ ->Seed_Client.identitySummary,
129
+ )->toEqual(Some("Admin, Shopper"))
130
+ )
131
+
132
+ // A token granting nothing reads as a sentence rather than an empty string.
133
+ testSync("says so when the token carries no groups at all", () =>
134
+ expect(
135
+ clientWith(jwt([("cognito:groups", strings([]))]))->Seed_Client.identitySummary,
136
+ )->toEqual(Some("no groups"))
137
+ )
138
+ })
139
+
140
+ describe("Seed_Client.isDenied:", () => {
141
+ // AppSync types it; the local server carries it in the message.
142
+ testSync("recognises a refusal in either platform's vocabulary", () => {
143
+ expect(Seed_Client.isDenied(errors([errorOfType("Unauthorized")])))->toBe(true)
144
+ expect(
145
+ Seed_Client.isDenied(
146
+ errors([Dict.fromArray([("message", JSON.Encode.string("Not Authorized to access X"))])]),
147
+ ),
148
+ )->toBe(true)
149
+ })
150
+
151
+ // Attaching an identity to an unrelated failure would point the reader at the
152
+ // wrong thing.
153
+ testSync("leaves every other failure alone", () => {
154
+ expect(Seed_Client.isDenied(errors([errorOfType("ValidationException")])))->toBe(false)
155
+ expect(Seed_Client.isDenied(errors([])))->toBe(false)
156
+ })
157
+ })
@@ -0,0 +1,167 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as Seed_Client$ReventlessSeed from "../src/Seed_Client.res.mjs";
4
+
5
+ function errors(entries) {
6
+ return entries.map(prim => prim);
7
+ }
8
+
9
+ function errorOfType(errorType) {
10
+ return Object.fromEntries([[
11
+ "errorType",
12
+ errorType
13
+ ]]);
14
+ }
15
+
16
+ globalThis.describe("Seed_Client.isTransient:", () => {
17
+ globalThis.test("resends a fault on the endpoint's own side", () => {
18
+ [
19
+ "InternalFailure",
20
+ "ServiceUnavailable",
21
+ "Throttling",
22
+ "TooManyRequestsException"
23
+ ].forEach(t => {
24
+ let entries = [errorOfType(t)];
25
+ globalThis.expect(Seed_Client$ReventlessSeed.isTransient(entries.map(prim => prim))).toBe(true);
26
+ });
27
+ });
28
+ globalThis.test("resends the shape AppSync actually returned", () => {
29
+ let payload = JSON.parse(`[{"errorType":"InternalFailure","message":"An internal failure occurred."}]`);
30
+ globalThis.expect(Seed_Client$ReventlessSeed.isTransient(payload)).toBe(true);
31
+ });
32
+ globalThis.test("fails fast on an error that is an answer", () => {
33
+ [
34
+ "ValidationException",
35
+ "Unauthorized",
36
+ "FieldUndefined"
37
+ ].forEach(t => {
38
+ let entries = [errorOfType(t)];
39
+ globalThis.expect(Seed_Client$ReventlessSeed.isTransient(entries.map(prim => prim))).toBe(false);
40
+ });
41
+ });
42
+ globalThis.test("refuses to resend a batch that mixes a real error in", () => {
43
+ let entries = [
44
+ errorOfType("InternalFailure"),
45
+ errorOfType("ValidationException")
46
+ ];
47
+ globalThis.expect(Seed_Client$ReventlessSeed.isTransient(entries.map(prim => prim))).toBe(false);
48
+ });
49
+ globalThis.test("treats an empty or untyped error list as final", () => {
50
+ globalThis.expect(Seed_Client$ReventlessSeed.isTransient([].map(prim => prim))).toBe(false);
51
+ let entries = [Object.fromEntries([[
52
+ "message",
53
+ "boom"
54
+ ]])];
55
+ globalThis.expect(Seed_Client$ReventlessSeed.isTransient(entries.map(prim => prim))).toBe(false);
56
+ globalThis.expect(Seed_Client$ReventlessSeed.isTransient(null)).toBe(false);
57
+ });
58
+ });
59
+
60
+ function b64url(payload) {
61
+ return Buffer.from(payload, "utf8").toString("base64url");
62
+ }
63
+
64
+ function jwt(claims) {
65
+ let payload = JSON.stringify(Object.fromEntries(claims));
66
+ return `header.` + Buffer.from(payload, "utf8").toString("base64url") + `.signature`;
67
+ }
68
+
69
+ function localToken(claims) {
70
+ let payload = JSON.stringify(Object.fromEntries(claims));
71
+ return Buffer.from(payload, "utf8").toString("base64url") + `.signature`;
72
+ }
73
+
74
+ function strings(values) {
75
+ return values.map(prim => prim);
76
+ }
77
+
78
+ function clientWith(token) {
79
+ let c = Seed_Client$ReventlessSeed.make({
80
+ endpoint: "http://example.invalid/graphql"
81
+ });
82
+ Seed_Client$ReventlessSeed.useToken(c, token);
83
+ return c;
84
+ }
85
+
86
+ globalThis.describe("Seed_Client.effectiveGroups:", () => {
87
+ globalThis.test("reads the payload wherever the provider put it", () => {
88
+ globalThis.expect(Seed_Client$ReventlessSeed.effectiveGroups(clientWith(jwt([[
89
+ "cognito:groups",
90
+ [
91
+ "Admin",
92
+ "Shopper"
93
+ ].map(prim => prim)
94
+ ]])))).toEqual([
95
+ "Admin",
96
+ "Shopper"
97
+ ]);
98
+ globalThis.expect(Seed_Client$ReventlessSeed.effectiveGroups(clientWith(localToken([[
99
+ "groups",
100
+ ["Merchandiser"].map(prim => prim)
101
+ ]])))).toEqual(["Merchandiser"]);
102
+ });
103
+ globalThis.test("says nothing about a token it cannot read", () => {
104
+ globalThis.expect(Seed_Client$ReventlessSeed.effectiveGroups(clientWith("not-a-token"))).toEqual(undefined);
105
+ globalThis.expect(Seed_Client$ReventlessSeed.effectiveGroups(Seed_Client$ReventlessSeed.make({
106
+ endpoint: "http://example.invalid/graphql"
107
+ }))).toEqual(undefined);
108
+ });
109
+ });
110
+
111
+ globalThis.describe("Seed_Client.identitySummary:", () => {
112
+ globalThis.test("names the membership a narrowed token gave up", () => {
113
+ globalThis.expect(Seed_Client$ReventlessSeed.identitySummary(clientWith(jwt([
114
+ [
115
+ "cognito:groups",
116
+ ["Shopper"].map(prim => prim)
117
+ ],
118
+ [
119
+ "availableRoles",
120
+ "Admin,Shopper"
121
+ ]
122
+ ])))).toEqual("Shopper — narrowed to this role from Admin, Shopper");
123
+ });
124
+ globalThis.test("stays quiet about an unnarrowed token", () => {
125
+ globalThis.expect(Seed_Client$ReventlessSeed.identitySummary(clientWith(jwt([[
126
+ "cognito:groups",
127
+ [
128
+ "Admin",
129
+ "Shopper"
130
+ ].map(prim => prim)
131
+ ]])))).toEqual("Admin, Shopper");
132
+ });
133
+ globalThis.test("says so when the token carries no groups at all", () => {
134
+ globalThis.expect(Seed_Client$ReventlessSeed.identitySummary(clientWith(jwt([[
135
+ "cognito:groups",
136
+ [].map(prim => prim)
137
+ ]])))).toEqual("no groups");
138
+ });
139
+ });
140
+
141
+ globalThis.describe("Seed_Client.isDenied:", () => {
142
+ globalThis.test("recognises a refusal in either platform's vocabulary", () => {
143
+ let entries = [errorOfType("Unauthorized")];
144
+ globalThis.expect(Seed_Client$ReventlessSeed.isDenied(entries.map(prim => prim))).toBe(true);
145
+ let entries$1 = [Object.fromEntries([[
146
+ "message",
147
+ "Not Authorized to access X"
148
+ ]])];
149
+ globalThis.expect(Seed_Client$ReventlessSeed.isDenied(entries$1.map(prim => prim))).toBe(true);
150
+ });
151
+ globalThis.test("leaves every other failure alone", () => {
152
+ let entries = [errorOfType("ValidationException")];
153
+ globalThis.expect(Seed_Client$ReventlessSeed.isDenied(entries.map(prim => prim))).toBe(false);
154
+ globalThis.expect(Seed_Client$ReventlessSeed.isDenied([].map(prim => prim))).toBe(false);
155
+ });
156
+ });
157
+
158
+ export {
159
+ errors,
160
+ errorOfType,
161
+ b64url,
162
+ jwt,
163
+ localToken,
164
+ strings,
165
+ clientWith,
166
+ }
167
+ /* Not a pure module */
Binary file
@@ -0,0 +1,59 @@
1
+ open JestGlobals
2
+
3
+ // `isAffirmative` is the one vocabulary for "yes", shared by a typed `[y/N]`
4
+ // reply and by the env var that stands in for it on a run with no terminal.
5
+ //
6
+ // It exists because those two drifted: the prompt took `y`, the env var took
7
+ // only the literals `1` and `yes`, and every other value — `y` included, and an
8
+ // explicit `no` with it — fell through to the interactive branch, which throws
9
+ // when there is no TTY. So the variable that exists to make the tool
10
+ // non-interactive had two spellings that worked and a crash for the rest.
11
+ //
12
+ // The suite is written around the shape of that bug rather than around the
13
+ // function: what must confirm, what must decline, and — the case that actually
14
+ // broke CI — that declining is a normal answer and not a fall-through.
15
+
16
+ describe("Seed_Prompt.isAffirmative:", () => {
17
+ testSync("takes the three spellings of yes", () => {
18
+ ["y", "yes", "1"]->Array.forEach(v =>
19
+ expect(Seed_Prompt.isAffirmative(v))->Expect.toBe(true)
20
+ )
21
+ })
22
+
23
+ // A prompt that prints `[y/N]` invites a capital as readily as a lowercase
24
+ // one, and an env var set by hand is written however the hand felt.
25
+ testSync("ignores case, so Y and YES answer as y does", () => {
26
+ ["Y", "Yes", "YES"]->Array.forEach(v =>
27
+ expect(Seed_Prompt.isAffirmative(v))->Expect.toBe(true)
28
+ )
29
+ })
30
+
31
+ testSync("ignores surrounding space", () => {
32
+ [" y", "y ", " yes "]->Array.forEach(v =>
33
+ expect(Seed_Prompt.isAffirmative(v))->Expect.toBe(true)
34
+ )
35
+ })
36
+
37
+ // The half that matters most: every one of these is an ANSWER, and the caller
38
+ // reads it as "no". None may be mistaken for "nothing was said", which is what
39
+ // sent the old code to a prompt it could not open.
40
+ testSync("declines every spelling of no, capital N included", () => {
41
+ ["n", "N", "no", "No", "NO"]->Array.forEach(v =>
42
+ expect(Seed_Prompt.isAffirmative(v))->Expect.toBe(false)
43
+ )
44
+ })
45
+
46
+ testSync("declines anything it does not recognise", () => {
47
+ ["", " ", "0", "true", "yep", "y.", "yesplease", "affirmative"]->Array.forEach(v =>
48
+ expect(Seed_Prompt.isAffirmative(v))->Expect.toBe(false)
49
+ )
50
+ })
51
+
52
+ // `true` is deliberately absent from the vocabulary above. Named here so that
53
+ // adding it is a decision someone takes against a failing test rather than a
54
+ // kindness someone slips in: the prompt this stands in for asks `[y/N]`, and a
55
+ // reply nobody would type at that prompt should not work around it either.
56
+ testSync("does not quietly grow a fourth spelling", () => {
57
+ expect(Seed_Prompt.isAffirmative("true"))->Expect.toBe(false)
58
+ })
59
+ })
@@ -0,0 +1,63 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as Seed_Prompt$ReventlessSeed from "../src/Seed_Prompt.res.mjs";
4
+
5
+ globalThis.describe("Seed_Prompt.isAffirmative:", () => {
6
+ globalThis.test("takes the three spellings of yes", () => {
7
+ [
8
+ "y",
9
+ "yes",
10
+ "1"
11
+ ].forEach(v => {
12
+ globalThis.expect(Seed_Prompt$ReventlessSeed.isAffirmative(v)).toBe(true);
13
+ });
14
+ });
15
+ globalThis.test("ignores case, so Y and YES answer as y does", () => {
16
+ [
17
+ "Y",
18
+ "Yes",
19
+ "YES"
20
+ ].forEach(v => {
21
+ globalThis.expect(Seed_Prompt$ReventlessSeed.isAffirmative(v)).toBe(true);
22
+ });
23
+ });
24
+ globalThis.test("ignores surrounding space", () => {
25
+ [
26
+ " y",
27
+ "y ",
28
+ " yes "
29
+ ].forEach(v => {
30
+ globalThis.expect(Seed_Prompt$ReventlessSeed.isAffirmative(v)).toBe(true);
31
+ });
32
+ });
33
+ globalThis.test("declines every spelling of no, capital N included", () => {
34
+ [
35
+ "n",
36
+ "N",
37
+ "no",
38
+ "No",
39
+ "NO"
40
+ ].forEach(v => {
41
+ globalThis.expect(Seed_Prompt$ReventlessSeed.isAffirmative(v)).toBe(false);
42
+ });
43
+ });
44
+ globalThis.test("declines anything it does not recognise", () => {
45
+ [
46
+ "",
47
+ " ",
48
+ "0",
49
+ "true",
50
+ "yep",
51
+ "y.",
52
+ "yesplease",
53
+ "affirmative"
54
+ ].forEach(v => {
55
+ globalThis.expect(Seed_Prompt$ReventlessSeed.isAffirmative(v)).toBe(false);
56
+ });
57
+ });
58
+ globalThis.test("does not quietly grow a fourth spelling", () => {
59
+ globalThis.expect(Seed_Prompt$ReventlessSeed.isAffirmative("true")).toBe(false);
60
+ });
61
+ });
62
+
63
+ /* Not a pure module */
Binary file