@reventlessdev/reventless-seed 1.0.0-alpha.16 → 1.0.0-alpha.18

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 (107) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/lib/bs/.compiler.log +2 -2
  3. package/lib/bs/ReventlessSeed.cmi +0 -0
  4. package/lib/bs/ReventlessSeed.cmt +0 -0
  5. package/lib/bs/ReventlessSeed.mlmap +3 -0
  6. package/lib/bs/compiler-info.json +1 -1
  7. package/lib/bs/src/Seed-ReventlessSeed.cmi +0 -0
  8. package/lib/bs/src/Seed-ReventlessSeed.cmt +0 -0
  9. package/lib/bs/src/Seed.ast +0 -0
  10. package/lib/bs/src/Seed.res +1 -0
  11. package/lib/bs/src/Seed.res.mjs +3 -0
  12. package/lib/bs/src/Seed_Client-ReventlessSeed.cmi +0 -0
  13. package/lib/bs/src/Seed_Client-ReventlessSeed.cmj +0 -0
  14. package/lib/bs/src/Seed_Client-ReventlessSeed.cmt +0 -0
  15. package/lib/bs/src/Seed_Client.ast +0 -0
  16. package/lib/bs/src/Seed_Client.res +67 -11
  17. package/lib/bs/src/Seed_Client.res.mjs +60 -20
  18. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmi +0 -0
  19. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmt +0 -0
  20. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmi +0 -0
  21. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmj +0 -0
  22. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmt +0 -0
  23. package/lib/bs/src/Seed_Prompt.ast +0 -0
  24. package/lib/bs/src/Seed_Prompt.res +82 -15
  25. package/lib/bs/src/Seed_Prompt.res.mjs +59 -7
  26. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmi +0 -0
  27. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmt +0 -0
  28. package/lib/bs/src/Seed_Runner.res.mjs +1 -1
  29. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmi +0 -0
  30. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmt +0 -0
  31. package/lib/bs/src/Seed_Users-ReventlessSeed.cmi +0 -0
  32. package/lib/bs/src/Seed_Users-ReventlessSeed.cmj +0 -0
  33. package/lib/bs/src/Seed_Users-ReventlessSeed.cmt +0 -0
  34. package/lib/bs/src/Seed_Users.ast +0 -0
  35. package/lib/bs/src/Seed_Users.res +80 -0
  36. package/lib/bs/src/Seed_Users.res.mjs +90 -0
  37. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmi +0 -0
  38. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmj +0 -0
  39. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmt +0 -0
  40. package/lib/bs/tests/Seed_ClientTest.ast +0 -0
  41. package/lib/bs/tests/Seed_ClientTest.res +59 -0
  42. package/lib/bs/tests/Seed_ClientTest.res.mjs +64 -0
  43. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmi +0 -0
  44. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmt +0 -0
  45. package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmt +0 -0
  46. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmi +0 -0
  47. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmj +0 -0
  48. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmt +0 -0
  49. package/lib/bs/tests/Seed_UsersTest.ast +0 -0
  50. package/lib/bs/tests/Seed_UsersTest.res +92 -0
  51. package/lib/bs/tests/Seed_UsersTest.res.mjs +95 -0
  52. package/lib/ocaml/.compiler.log +2 -2
  53. package/lib/ocaml/ReventlessSeed.cmi +0 -0
  54. package/lib/ocaml/ReventlessSeed.cmt +0 -0
  55. package/lib/ocaml/ReventlessSeed.mlmap +3 -0
  56. package/lib/ocaml/Seed-ReventlessSeed.cmi +0 -0
  57. package/lib/ocaml/Seed-ReventlessSeed.cmt +0 -0
  58. package/lib/ocaml/Seed.ast +0 -0
  59. package/lib/ocaml/Seed.res +1 -0
  60. package/lib/ocaml/Seed_Client-ReventlessSeed.cmi +0 -0
  61. package/lib/ocaml/Seed_Client-ReventlessSeed.cmj +0 -0
  62. package/lib/ocaml/Seed_Client-ReventlessSeed.cmt +0 -0
  63. package/lib/ocaml/Seed_Client.ast +0 -0
  64. package/lib/ocaml/Seed_Client.res +67 -11
  65. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmi +0 -0
  66. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmj +0 -0
  67. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmt +0 -0
  68. package/lib/ocaml/Seed_ClientTest.ast +0 -0
  69. package/lib/ocaml/Seed_ClientTest.res +59 -0
  70. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmi +0 -0
  71. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmt +0 -0
  72. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmi +0 -0
  73. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmj +0 -0
  74. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
  75. package/lib/ocaml/Seed_Prompt.ast +0 -0
  76. package/lib/ocaml/Seed_Prompt.res +82 -15
  77. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmi +0 -0
  78. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmt +0 -0
  79. package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmt +0 -0
  80. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
  81. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmt +0 -0
  82. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmi +0 -0
  83. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmt +0 -0
  84. package/lib/ocaml/Seed_Users-ReventlessSeed.cmi +0 -0
  85. package/lib/ocaml/Seed_Users-ReventlessSeed.cmj +0 -0
  86. package/lib/ocaml/Seed_Users-ReventlessSeed.cmt +0 -0
  87. package/lib/ocaml/Seed_Users.ast +0 -0
  88. package/lib/ocaml/Seed_Users.res +80 -0
  89. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmi +0 -0
  90. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmj +0 -0
  91. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmt +0 -0
  92. package/lib/ocaml/Seed_UsersTest.ast +0 -0
  93. package/lib/ocaml/Seed_UsersTest.res +92 -0
  94. package/package.json +2 -1
  95. package/src/Seed.res +1 -0
  96. package/src/Seed.res.mjs +3 -0
  97. package/src/Seed_Client.res +67 -11
  98. package/src/Seed_Client.res.mjs +60 -20
  99. package/src/Seed_Prompt.res +82 -15
  100. package/src/Seed_Prompt.res.mjs +59 -7
  101. package/src/Seed_Runner.res.mjs +1 -1
  102. package/src/Seed_Users.res +80 -0
  103. package/src/Seed_Users.res.mjs +90 -0
  104. package/tests/Seed_ClientTest.res +59 -0
  105. package/tests/Seed_ClientTest.res.mjs +64 -0
  106. package/tests/Seed_UsersTest.res +92 -0
  107. 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,59 @@
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
+ })
@@ -0,0 +1,64 @@
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
+ export {
61
+ errors,
62
+ errorOfType,
63
+ }
64
+ /* Not a pure module */
Binary file
@@ -0,0 +1,92 @@
1
+ open JestGlobals
2
+
3
+ // `.reventless/users.yaml` is hand-maintained and shared with the auth adapter,
4
+ // so the seed reads it defensively: it must survive the comment blocks both
5
+ // copies carry, must not reorder the accounts (the first one is the prompt's
6
+ // default, so order IS the contract), and must never throw — a broken file costs
7
+ // the menu, not the run.
8
+
9
+ let document = `
10
+ # Local dev users — a comment block like the real file carries.
11
+
12
+ - username: admin
13
+ password: admin
14
+ groups: [Admin, Shopper]
15
+ userId: local-admin
16
+
17
+ - username: shopper
18
+ password: Shopper-2026!
19
+ groups: [Shopper]
20
+
21
+ - username: noGroups
22
+ password: pw
23
+ `
24
+
25
+ describe("Seed_Users.parseString:", () => {
26
+ testSync("reads every account, in the order the file defines them", () => {
27
+ let users = Seed_Users.parseString(document)
28
+ expect(users->Array.map(u => u.Seed_Users.username))->Expect.toEqual([
29
+ "admin",
30
+ "shopper",
31
+ "noGroups",
32
+ ])
33
+ })
34
+
35
+ testSync("carries the password beside the username", () => {
36
+ let users = Seed_Users.parseString(document)
37
+ expect(users->Array.map(u => u.Seed_Users.password))->Expect.toEqual([
38
+ "admin",
39
+ "Shopper-2026!",
40
+ "pw",
41
+ ])
42
+ })
43
+
44
+ testSync("keeps the groups, and reads a missing groups key as none", () => {
45
+ let users = Seed_Users.parseString(document)
46
+ expect(users->Array.map(u => u.Seed_Users.groups))->Expect.toEqual([
47
+ ["Admin", "Shopper"],
48
+ ["Shopper"],
49
+ [],
50
+ ])
51
+ })
52
+
53
+ // An entry half-written by hand is the likely flaw in this file. Dropping it
54
+ // keeps the other accounts selectable; failing the parse would not.
55
+ testSync("skips an entry that names no password", () => {
56
+ let users = Seed_Users.parseString(`
57
+ - username: admin
58
+ password: admin
59
+ - username: halfWritten
60
+ groups: [Shopper]
61
+ `)
62
+ expect(users->Array.map(u => u.Seed_Users.username))->Expect.toEqual(["admin"])
63
+ })
64
+
65
+ testSync("yields no accounts for a document that is not a list", () => {
66
+ expect(Seed_Users.parseString("users: {}")->Array.length)->Expect.toBe(0)
67
+ })
68
+
69
+ // The whole point of returning accounts rather than raising: the caller falls
70
+ // back to prompting, and it can only do that if nothing here escapes.
71
+ testSync("yields no accounts for malformed YAML instead of throwing", () => {
72
+ expect(Seed_Users.parseString("- username: [unclosed")->Array.length)->Expect.toBe(0)
73
+ })
74
+ })
75
+
76
+ describe("Seed_Users.load:", () => {
77
+ testSync("is silent about a file that is not there", () => {
78
+ expect(Seed_Users.load(~path="/nonexistent/.reventless/users.yaml"))->Expect.toBe(None)
79
+ })
80
+ })
81
+
82
+ describe("Seed_Users.label:", () => {
83
+ testSync("names the groups an account carries", () => {
84
+ expect(
85
+ Seed_Users.label({username: "admin", password: "x", groups: ["Admin", "Shopper"]}),
86
+ )->Expect.toBe("admin [Admin, Shopper]")
87
+ })
88
+
89
+ testSync("is the bare username when the file records no groups", () => {
90
+ expect(Seed_Users.label({username: "admin", password: "x", groups: []}))->Expect.toBe("admin")
91
+ })
92
+ })
@@ -0,0 +1,95 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as Seed_Users$ReventlessSeed from "../src/Seed_Users.res.mjs";
4
+
5
+ let document = `
6
+ # Local dev users — a comment block like the real file carries.
7
+
8
+ - username: admin
9
+ password: admin
10
+ groups: [Admin, Shopper]
11
+ userId: local-admin
12
+
13
+ - username: shopper
14
+ password: Shopper-2026!
15
+ groups: [Shopper]
16
+
17
+ - username: noGroups
18
+ password: pw
19
+ `;
20
+
21
+ globalThis.describe("Seed_Users.parseString:", () => {
22
+ globalThis.test("reads every account, in the order the file defines them", () => {
23
+ let users = Seed_Users$ReventlessSeed.parseString(document);
24
+ globalThis.expect(users.map(u => u.username)).toEqual([
25
+ "admin",
26
+ "shopper",
27
+ "noGroups"
28
+ ]);
29
+ });
30
+ globalThis.test("carries the password beside the username", () => {
31
+ let users = Seed_Users$ReventlessSeed.parseString(document);
32
+ globalThis.expect(users.map(u => u.password)).toEqual([
33
+ "admin",
34
+ "Shopper-2026!",
35
+ "pw"
36
+ ]);
37
+ });
38
+ globalThis.test("keeps the groups, and reads a missing groups key as none", () => {
39
+ let users = Seed_Users$ReventlessSeed.parseString(document);
40
+ globalThis.expect(users.map(u => u.groups)).toEqual([
41
+ [
42
+ "Admin",
43
+ "Shopper"
44
+ ],
45
+ ["Shopper"],
46
+ []
47
+ ]);
48
+ });
49
+ globalThis.test("skips an entry that names no password", () => {
50
+ let users = Seed_Users$ReventlessSeed.parseString(`
51
+ - username: admin
52
+ password: admin
53
+ - username: halfWritten
54
+ groups: [Shopper]
55
+ `);
56
+ globalThis.expect(users.map(u => u.username)).toEqual(["admin"]);
57
+ });
58
+ globalThis.test("yields no accounts for a document that is not a list", () => {
59
+ globalThis.expect(Seed_Users$ReventlessSeed.parseString("users: {}").length).toBe(0);
60
+ });
61
+ globalThis.test("yields no accounts for malformed YAML instead of throwing", () => {
62
+ globalThis.expect(Seed_Users$ReventlessSeed.parseString("- username: [unclosed").length).toBe(0);
63
+ });
64
+ });
65
+
66
+ globalThis.describe("Seed_Users.load:", () => {
67
+ globalThis.test("is silent about a file that is not there", () => {
68
+ globalThis.expect(Seed_Users$ReventlessSeed.load("/nonexistent/.reventless/users.yaml")).toBe(undefined);
69
+ });
70
+ });
71
+
72
+ globalThis.describe("Seed_Users.label:", () => {
73
+ globalThis.test("names the groups an account carries", () => {
74
+ globalThis.expect(Seed_Users$ReventlessSeed.label({
75
+ username: "admin",
76
+ password: "x",
77
+ groups: [
78
+ "Admin",
79
+ "Shopper"
80
+ ]
81
+ })).toBe("admin [Admin, Shopper]");
82
+ });
83
+ globalThis.test("is the bare username when the file records no groups", () => {
84
+ globalThis.expect(Seed_Users$ReventlessSeed.label({
85
+ username: "admin",
86
+ password: "x",
87
+ groups: []
88
+ })).toBe("admin");
89
+ });
90
+ });
91
+
92
+ export {
93
+ document,
94
+ }
95
+ /* Not a pure module */
@@ -1,2 +1,2 @@
1
- #Start(1787013927827)
2
- #Done(1787013927856)
1
+ #Start(1787841681066)
2
+ #Done(1787841681103)
Binary file
Binary file
@@ -1,6 +1,7 @@
1
1
  randjbuildsystem
2
2
  Seed
3
3
  Seed_Client
4
+ Seed_ClientTest
4
5
  Seed_Connect
5
6
  Seed_Prompt
6
7
  Seed_PromptTest
@@ -9,3 +10,5 @@ Seed_RandomTest
9
10
  Seed_Runner
10
11
  Seed_Types
11
12
  Seed_Upload
13
+ Seed_Users
14
+ Seed_UsersTest
Binary file
Binary file
Binary file
@@ -10,6 +10,7 @@ module Client = Seed_Client
10
10
  module Runner = Seed_Runner
11
11
  module Upload = Seed_Upload
12
12
  module Prompt = Seed_Prompt
13
+ module Users = Seed_Users
13
14
  module Connect = Seed_Connect
14
15
 
15
16
  exception Failed = Seed_Types.Failed
Binary file
@@ -106,10 +106,57 @@ let login = async (t: t): unit => {
106
106
  t.token = json->field("token")->Option.flatMap(asString)
107
107
  }
108
108
 
109
+ /** Error types the endpoint returns for a fault on its own side rather than a
110
+ problem with the document. They say nothing about the request, so the same
111
+ request sent again can succeed — and across the few hundred commands a seed
112
+ issues, one of these is ordinary rather than exceptional.
113
+
114
+ Everything else — a validation error, an unknown field, a refused token — is
115
+ an answer, and the same answer comes back however many times it is asked. */
116
+ let transientErrorTypes = [
117
+ "InternalFailure",
118
+ "ServiceUnavailable",
119
+ "ServiceUnavailableError",
120
+ "Throttling",
121
+ "ThrottlingException",
122
+ "TooManyRequestsException",
123
+ "RequestTimeout",
124
+ ]
125
+
126
+ /** Every entry must be transient for the document to be worth resending: one
127
+ real error alongside a transient one still means the request was wrong. An
128
+ empty `errors` array is not a reason to retry either. */
129
+ let isTransient = (errors: JSON.t): bool =>
130
+ switch errors->JSON.Decode.array {
131
+ | None | Some([]) => false
132
+ | Some(entries) =>
133
+ entries->Array.every(e =>
134
+ switch e->nodeString("errorType") {
135
+ | Some(errorType) => transientErrorTypes->Array.includes(errorType)
136
+ | None => false
137
+ }
138
+ )
139
+ }
140
+
141
+ let attempts = 4
142
+
109
143
  /**
110
144
  * Sends one GraphQL document and returns its `data`. A GraphQL-level error
111
145
  * aborts the run: a half-seeded store is worse than an empty one, because it
112
146
  * looks like a working dataset.
147
+ *
148
+ * Which is exactly why a fault on the endpoint's own side must not abort it. A
149
+ * seed issues hundreds of commands, many of them concurrently and all of them
150
+ * against runtimes that a preceding reset left cold, so a single
151
+ * `InternalFailure` somewhere in the batch is a normal event. Treating it as
152
+ * fatal threw away every command that had already landed and demanded a full
153
+ * wipe and re-run — for a request that would have succeeded on being asked
154
+ * again. So resend those, backing off between tries, and keep failing fast on
155
+ * everything that is an answer rather than a fault.
156
+ *
157
+ * The backoff is jittered because the commands go out in concurrent batches: a
158
+ * fixed delay would have the whole batch retry in lockstep and rebuild the same
159
+ * burst that provoked the fault.
113
160
  */
114
161
  let gql = async (t: t, ~query: string, ~label: string): JSON.t => {
115
162
  let headers = Dict.fromArray([("content-type", "application/json")])
@@ -120,18 +167,27 @@ let gql = async (t: t, ~query: string, ~label: string): JSON.t => {
120
167
  let body = JSON.stringify(
121
168
  JSON.Encode.object(Dict.fromArray([("query", JSON.Encode.string(query))])),
122
169
  )
123
- let res = try await fetch(t.config.endpoint, {method: "POST", headers, body}) catch {
124
- | _ =>
125
- throw(Failed(`${label}: cannot reach ${t.config.endpoint} — is the platform running?`))
126
- }
127
- let json = await res->responseJson
128
- switch json->field("errors") {
129
- | Some(errors) =>
130
- throw(
131
- Failed(`${label} failed\n query: ${query}\n response: ${JSON.stringify(errors)}`),
132
- )
133
- | None => json->field("data")->Option.getOr(JSON.Encode.null)
170
+ let rec attempt = async (n: int): JSON.t => {
171
+ let res = try await fetch(t.config.endpoint, {method: "POST", headers, body}) catch {
172
+ | _ =>
173
+ throw(Failed(`${label}: cannot reach ${t.config.endpoint} — is the platform running?`))
174
+ }
175
+ let json = await res->responseJson
176
+ switch json->field("errors") {
177
+ | Some(errors) =>
178
+ if errors->isTransient && n < attempts {
179
+ await sleep(250 * Int.shiftLeft(1, n - 1) + Int.fromFloat(Math.random() *. 250.))
180
+ await attempt(n + 1)
181
+ } else {
182
+ let tried = n > 1 ? `\n gave up after ${n->Int.toString} attempts` : ""
183
+ throw(
184
+ Failed(`${label} failed\n query: ${query}\n response: ${JSON.stringify(errors)}${tried}`),
185
+ )
186
+ }
187
+ | None => json->field("data")->Option.getOr(JSON.Encode.null)
188
+ }
134
189
  }
190
+ await attempt(1)
135
191
  }
136
192
 
137
193
  // ── Commands ────────────────────────────────────────────────────────────────
Binary file