@stigmer/server 3.27.1 → 3.28.0

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 (245) hide show
  1. package/README.md +2 -0
  2. package/dist/authorization/authorizer.d.ts +7 -5
  3. package/dist/authorization/authorizer.d.ts.map +1 -1
  4. package/dist/authorization/authorizer.js +7 -5
  5. package/dist/authorization/authorizer.js.map +1 -1
  6. package/dist/authorization/derived-tuples.d.ts +3 -1
  7. package/dist/authorization/derived-tuples.d.ts.map +1 -1
  8. package/dist/authorization/derived-tuples.js +52 -2
  9. package/dist/authorization/derived-tuples.js.map +1 -1
  10. package/dist/authorization/evaluator.d.ts +9 -10
  11. package/dist/authorization/evaluator.d.ts.map +1 -1
  12. package/dist/authorization/evaluator.js.map +1 -1
  13. package/dist/authorization/list-read-scope.d.ts.map +1 -1
  14. package/dist/authorization/list-read-scope.js +29 -18
  15. package/dist/authorization/list-read-scope.js.map +1 -1
  16. package/dist/authorization/model/bindings.d.ts +41 -0
  17. package/dist/authorization/model/bindings.d.ts.map +1 -0
  18. package/dist/authorization/model/bindings.js +89 -0
  19. package/dist/authorization/model/bindings.js.map +1 -0
  20. package/dist/authorization/model/data/authorization-model.json +3114 -0
  21. package/dist/authorization/model/default-of.d.ts +2 -2
  22. package/dist/authorization/model/execution-viewer.d.ts +3 -0
  23. package/dist/authorization/model/execution-viewer.d.ts.map +1 -0
  24. package/dist/authorization/model/execution-viewer.js +50 -0
  25. package/dist/authorization/model/execution-viewer.js.map +1 -0
  26. package/dist/authorization/model/index.d.ts +33 -21
  27. package/dist/authorization/model/index.d.ts.map +1 -1
  28. package/dist/authorization/model/index.js +74 -52
  29. package/dist/authorization/model/index.js.map +1 -1
  30. package/dist/authorization/model/openfga-json.d.ts +18 -0
  31. package/dist/authorization/model/openfga-json.d.ts.map +1 -0
  32. package/dist/authorization/model/openfga-json.js +266 -0
  33. package/dist/authorization/model/openfga-json.js.map +1 -0
  34. package/dist/authorization/model/rewrite.d.ts +39 -67
  35. package/dist/authorization/model/rewrite.d.ts.map +1 -1
  36. package/dist/authorization/model/rewrite.js +1 -117
  37. package/dist/authorization/model/rewrite.js.map +1 -1
  38. package/dist/authorization/strict-json.d.ts +20 -0
  39. package/dist/authorization/strict-json.d.ts.map +1 -0
  40. package/dist/authorization/strict-json.js +45 -0
  41. package/dist/authorization/strict-json.js.map +1 -0
  42. package/dist/boot/compose.d.ts.map +1 -1
  43. package/dist/boot/compose.js +9 -7
  44. package/dist/boot/compose.js.map +1 -1
  45. package/dist/boot/list-indexes.d.ts.map +1 -1
  46. package/dist/boot/list-indexes.js +6 -0
  47. package/dist/boot/list-indexes.js.map +1 -1
  48. package/dist/domain/iampolicy/list-index.d.ts +2 -0
  49. package/dist/domain/iampolicy/list-index.d.ts.map +1 -0
  50. package/dist/domain/iampolicy/list-index.js +32 -0
  51. package/dist/domain/iampolicy/list-index.js.map +1 -0
  52. package/dist/domain/iampolicy/resource-store.d.ts.map +1 -1
  53. package/dist/domain/iampolicy/resource-store.js +24 -11
  54. package/dist/domain/iampolicy/resource-store.js.map +1 -1
  55. package/dist/domain/workflowexecution/list-index.d.ts +1 -1
  56. package/dist/extensions/list-read-scope.d.ts +20 -3
  57. package/dist/extensions/list-read-scope.d.ts.map +1 -1
  58. package/dist/extensions/list-read-scope.js +22 -2
  59. package/dist/extensions/list-read-scope.js.map +1 -1
  60. package/dist/index.d.ts +8 -8
  61. package/dist/index.d.ts.map +1 -1
  62. package/dist/index.js +20 -5
  63. package/dist/index.js.map +1 -1
  64. package/package.json +7 -6
  65. package/src/authorization/README.md +48 -39
  66. package/src/authorization/__tests__/authorizer.test.ts +5 -14
  67. package/src/authorization/__tests__/derived-tuples.test.ts +192 -17
  68. package/src/authorization/__tests__/evaluator.test.ts +53 -160
  69. package/src/authorization/__tests__/facts.test.ts +5 -11
  70. package/src/authorization/__tests__/list-read-scope.measure.test.ts +424 -62
  71. package/src/authorization/__tests__/list-read-scope.test.ts +15 -14
  72. package/src/authorization/__tests__/organization-directory.test.ts +2 -2
  73. package/src/authorization/__tests__/store-test-kit.ts +246 -0
  74. package/src/authorization/__tests__/store-tests.test.ts +91 -223
  75. package/src/authorization/__tests__/support.ts +30 -2
  76. package/src/authorization/__tests__/throwaway-model.ts +75 -0
  77. package/src/authorization/__tests__/wire-permissions.test.ts +141 -107
  78. package/src/authorization/authorizer.ts +7 -5
  79. package/src/authorization/derived-tuples.ts +105 -16
  80. package/src/authorization/evaluator.ts +8 -10
  81. package/src/authorization/list-read-scope.ts +37 -22
  82. package/src/authorization/model/__tests__/openfga-json.test.ts +353 -0
  83. package/src/authorization/model/__tests__/registry.test.ts +102 -276
  84. package/src/authorization/model/bindings.ts +131 -0
  85. package/src/authorization/model/data/authorization-model.json +3114 -0
  86. package/src/authorization/model/default-of.ts +2 -2
  87. package/src/authorization/model/execution-viewer.ts +56 -0
  88. package/src/authorization/model/index.ts +81 -72
  89. package/src/authorization/model/openfga-json.ts +310 -0
  90. package/src/authorization/model/rewrite.ts +39 -188
  91. package/src/authorization/strict-json.ts +52 -0
  92. package/src/boot/__tests__/list-indexes.test.ts +4 -0
  93. package/src/boot/compose.ts +9 -7
  94. package/src/boot/list-indexes.ts +6 -0
  95. package/src/domain/iampolicy/__tests__/resource-store.test.ts +65 -5
  96. package/src/domain/iampolicy/list-index.ts +33 -0
  97. package/src/domain/iampolicy/resource-store.ts +24 -11
  98. package/src/extensions/__tests__/built-in-authorization-composed.test.ts +3 -6
  99. package/src/extensions/list-read-scope.ts +32 -4
  100. package/src/index.ts +20 -44
  101. package/dist/authorization/model/agent.d.ts +0 -2
  102. package/dist/authorization/model/agent.d.ts.map +0 -1
  103. package/dist/authorization/model/agent.js +0 -43
  104. package/dist/authorization/model/agent.js.map +0 -1
  105. package/dist/authorization/model/agent_channel.d.ts +0 -2
  106. package/dist/authorization/model/agent_channel.d.ts.map +0 -1
  107. package/dist/authorization/model/agent_channel.js +0 -39
  108. package/dist/authorization/model/agent_channel.js.map +0 -1
  109. package/dist/authorization/model/agent_execution.d.ts +0 -2
  110. package/dist/authorization/model/agent_execution.d.ts.map +0 -1
  111. package/dist/authorization/model/agent_execution.js +0 -25
  112. package/dist/authorization/model/agent_execution.js.map +0 -1
  113. package/dist/authorization/model/agent_instance.d.ts +0 -2
  114. package/dist/authorization/model/agent_instance.d.ts.map +0 -1
  115. package/dist/authorization/model/agent_instance.js +0 -48
  116. package/dist/authorization/model/agent_instance.js.map +0 -1
  117. package/dist/authorization/model/agent_share.d.ts +0 -2
  118. package/dist/authorization/model/agent_share.d.ts.map +0 -1
  119. package/dist/authorization/model/agent_share.js +0 -29
  120. package/dist/authorization/model/agent_share.js.map +0 -1
  121. package/dist/authorization/model/api_key.d.ts +0 -2
  122. package/dist/authorization/model/api_key.d.ts.map +0 -1
  123. package/dist/authorization/model/api_key.js +0 -27
  124. package/dist/authorization/model/api_key.js.map +0 -1
  125. package/dist/authorization/model/artifact.d.ts +0 -2
  126. package/dist/authorization/model/artifact.d.ts.map +0 -1
  127. package/dist/authorization/model/artifact.js +0 -27
  128. package/dist/authorization/model/artifact.js.map +0 -1
  129. package/dist/authorization/model/channel_app.d.ts +0 -2
  130. package/dist/authorization/model/channel_app.d.ts.map +0 -1
  131. package/dist/authorization/model/channel_app.js +0 -28
  132. package/dist/authorization/model/channel_app.js.map +0 -1
  133. package/dist/authorization/model/environment.d.ts +0 -2
  134. package/dist/authorization/model/environment.d.ts.map +0 -1
  135. package/dist/authorization/model/environment.js +0 -33
  136. package/dist/authorization/model/environment.js.map +0 -1
  137. package/dist/authorization/model/execution_context.d.ts +0 -2
  138. package/dist/authorization/model/execution_context.d.ts.map +0 -1
  139. package/dist/authorization/model/execution_context.js +0 -19
  140. package/dist/authorization/model/execution_context.js.map +0 -1
  141. package/dist/authorization/model/iam_policy.d.ts +0 -2
  142. package/dist/authorization/model/iam_policy.d.ts.map +0 -1
  143. package/dist/authorization/model/iam_policy.js +0 -31
  144. package/dist/authorization/model/iam_policy.js.map +0 -1
  145. package/dist/authorization/model/identity_account.d.ts +0 -2
  146. package/dist/authorization/model/identity_account.d.ts.map +0 -1
  147. package/dist/authorization/model/identity_account.js +0 -25
  148. package/dist/authorization/model/identity_account.js.map +0 -1
  149. package/dist/authorization/model/mcp_server.d.ts +0 -2
  150. package/dist/authorization/model/mcp_server.d.ts.map +0 -1
  151. package/dist/authorization/model/mcp_server.js +0 -38
  152. package/dist/authorization/model/mcp_server.js.map +0 -1
  153. package/dist/authorization/model/memory.d.ts +0 -2
  154. package/dist/authorization/model/memory.d.ts.map +0 -1
  155. package/dist/authorization/model/memory.js +0 -26
  156. package/dist/authorization/model/memory.js.map +0 -1
  157. package/dist/authorization/model/oauth_app.d.ts +0 -2
  158. package/dist/authorization/model/oauth_app.d.ts.map +0 -1
  159. package/dist/authorization/model/oauth_app.js +0 -31
  160. package/dist/authorization/model/oauth_app.js.map +0 -1
  161. package/dist/authorization/model/organization.d.ts +0 -2
  162. package/dist/authorization/model/organization.d.ts.map +0 -1
  163. package/dist/authorization/model/organization.js +0 -58
  164. package/dist/authorization/model/organization.js.map +0 -1
  165. package/dist/authorization/model/platform_client.d.ts +0 -2
  166. package/dist/authorization/model/platform_client.d.ts.map +0 -1
  167. package/dist/authorization/model/platform_client.js +0 -31
  168. package/dist/authorization/model/platform_client.js.map +0 -1
  169. package/dist/authorization/model/plugin.d.ts +0 -2
  170. package/dist/authorization/model/plugin.d.ts.map +0 -1
  171. package/dist/authorization/model/plugin.js +0 -45
  172. package/dist/authorization/model/plugin.js.map +0 -1
  173. package/dist/authorization/model/schedule.d.ts +0 -2
  174. package/dist/authorization/model/schedule.d.ts.map +0 -1
  175. package/dist/authorization/model/schedule.js +0 -34
  176. package/dist/authorization/model/schedule.js.map +0 -1
  177. package/dist/authorization/model/session.d.ts +0 -2
  178. package/dist/authorization/model/session.d.ts.map +0 -1
  179. package/dist/authorization/model/session.js +0 -38
  180. package/dist/authorization/model/session.js.map +0 -1
  181. package/dist/authorization/model/skill.d.ts +0 -2
  182. package/dist/authorization/model/skill.d.ts.map +0 -1
  183. package/dist/authorization/model/skill.js +0 -39
  184. package/dist/authorization/model/skill.js.map +0 -1
  185. package/dist/authorization/model/workflow.d.ts +0 -2
  186. package/dist/authorization/model/workflow.d.ts.map +0 -1
  187. package/dist/authorization/model/workflow.js +0 -39
  188. package/dist/authorization/model/workflow.js.map +0 -1
  189. package/dist/authorization/model/workflow_execution.d.ts +0 -2
  190. package/dist/authorization/model/workflow_execution.d.ts.map +0 -1
  191. package/dist/authorization/model/workflow_execution.js +0 -33
  192. package/dist/authorization/model/workflow_execution.js.map +0 -1
  193. package/dist/authorization/model/workflow_instance.d.ts +0 -2
  194. package/dist/authorization/model/workflow_instance.d.ts.map +0 -1
  195. package/dist/authorization/model/workflow_instance.js +0 -96
  196. package/dist/authorization/model/workflow_instance.js.map +0 -1
  197. package/dist/authorization/store-test-kit.d.ts +0 -62
  198. package/dist/authorization/store-test-kit.d.ts.map +0 -1
  199. package/dist/authorization/store-test-kit.js +0 -265
  200. package/dist/authorization/store-test-kit.js.map +0 -1
  201. package/src/authorization/__tests__/fixtures/fga/agent-channel-owner.fga.yaml +0 -112
  202. package/src/authorization/__tests__/fixtures/fga/agent-instance-creation.fga.yaml +0 -177
  203. package/src/authorization/__tests__/fixtures/fga/agent-share-owner.fga.yaml +0 -109
  204. package/src/authorization/__tests__/fixtures/fga/artifact-org-and-owner.fga.yaml +0 -128
  205. package/src/authorization/__tests__/fixtures/fga/blueprint-private-visibility.fga.yaml +0 -318
  206. package/src/authorization/__tests__/fixtures/fga/channel-app-owner.fga.yaml +0 -125
  207. package/src/authorization/__tests__/fixtures/fga/channel-participation.fga.yaml +0 -201
  208. package/src/authorization/__tests__/fixtures/fga/channel-session-visibility.fga.yaml +0 -187
  209. package/src/authorization/__tests__/fixtures/fga/default-instance-inheritance.fga.yaml +0 -214
  210. package/src/authorization/__tests__/fixtures/fga/mcp-server-authoring.fga.yaml +0 -129
  211. package/src/authorization/__tests__/fixtures/fga/memory-subject-only.fga.yaml +0 -72
  212. package/src/authorization/__tests__/fixtures/fga/org-admin-owner-inheritance.fga.yaml +0 -308
  213. package/src/authorization/__tests__/fixtures/fga/org-shared-environment.fga.yaml +0 -144
  214. package/src/authorization/__tests__/fixtures/fga/platform-visibility.fga.yaml +0 -199
  215. package/src/authorization/__tests__/fixtures/fga/provider-standing-viewer.fga.yaml +0 -40
  216. package/src/authorization/__tests__/fixtures/fga/reserved-label-writer.fga.yaml +0 -41
  217. package/src/authorization/__tests__/fixtures/fga/schedule-owner.fga.yaml +0 -113
  218. package/src/authorization/__tests__/fixtures/fga/schedule-session-visibility.fga.yaml +0 -214
  219. package/src/authorization/__tests__/fixtures/fga/session-personal-resource.fga.yaml +0 -117
  220. package/src/authorization/__tests__/fixtures/fga/workflow-execution-sharing.fga.yaml +0 -190
  221. package/src/authorization/model/agent.ts +0 -64
  222. package/src/authorization/model/agent_channel.ts +0 -57
  223. package/src/authorization/model/agent_execution.ts +0 -33
  224. package/src/authorization/model/agent_instance.ts +0 -66
  225. package/src/authorization/model/agent_share.ts +0 -40
  226. package/src/authorization/model/api_key.ts +0 -28
  227. package/src/authorization/model/artifact.ts +0 -38
  228. package/src/authorization/model/channel_app.ts +0 -39
  229. package/src/authorization/model/environment.ts +0 -48
  230. package/src/authorization/model/execution_context.ts +0 -20
  231. package/src/authorization/model/iam_policy.ts +0 -42
  232. package/src/authorization/model/identity_account.ts +0 -26
  233. package/src/authorization/model/mcp_server.ts +0 -59
  234. package/src/authorization/model/memory.ts +0 -27
  235. package/src/authorization/model/oauth_app.ts +0 -43
  236. package/src/authorization/model/organization.ts +0 -59
  237. package/src/authorization/model/platform_client.ts +0 -43
  238. package/src/authorization/model/plugin.ts +0 -66
  239. package/src/authorization/model/schedule.ts +0 -49
  240. package/src/authorization/model/session.ts +0 -50
  241. package/src/authorization/model/skill.ts +0 -60
  242. package/src/authorization/model/workflow.ts +0 -60
  243. package/src/authorization/model/workflow_execution.ts +0 -46
  244. package/src/authorization/model/workflow_instance.ts +0 -125
  245. package/src/authorization/store-test-kit.ts +0 -395
@@ -1,395 +0,0 @@
1
- /**
2
- * The OpenFGA store-test kit — runs a `.fga.yaml` store test (the format
3
- * `fga model test` reads: fixture tuples, then named tests with `check`
4
- * and `list_objects` assertions) against the built-in evaluator, so the
5
- * cloud's own model tests — written by the model's authors and run
6
- * against real OpenFGA in the cloud's CI — are the proof that this
7
- * evaluator answers the same model the same way. Open source runs the
8
- * kit over pinned copies of those documents (authorization/__tests__/
9
- * fixtures/fga/); the cloud runs it over the LIVE documents at the
10
- * re-pin, which is what makes the copies safe to hold.
11
- *
12
- * Shape: the port-contract kits' (store/port-contract.ts) — vitest-free,
13
- * node:assert, cases the consumer's framework iterates — because a
14
- * composition consumes it through the published barrel and vitest is a
15
- * devDependency that must not enter the barrel's runtime graph.
16
- *
17
- * What is run and what is reported. One case per named test; a case
18
- * asserts every `check` in the test whose object's kind the model
19
- * declares, relation by relation, and FAILS on the first disagreement
20
- * (the message names the check in the document's own words). Assertions
21
- * the kit does not run are returned as `skipped`, each with its reason,
22
- * so a consumer pins the list and a document that gains a scenario, or a
23
- * declaration that lands, is a visible diff and never a quietly smaller
24
- * proof. Three reasons: a check whose TARGET kind the model does not
25
- * declare (`undeclared-kind`) and every `list_objects` assertion
26
- * (`list-objects`, the list scope's proof) are known when the kit is
27
- * built; the third is known only from the walk. A document may grant
28
- * through a type the model does not declare — the cloud's platform
29
- * tenancy reaches people through `identity_provider:<idp>#platform_user`,
30
- * a kind this edition does not serve — and OpenFGA answers `true` where
31
- * a model without that type resolves the userset to nobody. The kit runs
32
- * every assertion over a recording view of the document's tuples, and an
33
- * assertion whose walk READ a tuple naming an undeclared type is
34
- * reported as `undeclared-subject-type` (the types named) instead of
35
- * being asserted, whatever it would have answered: decided from the
36
- * model and the data, per assertion, so the sibling arms of the same
37
- * check block still run and a union that is settled before it reaches
38
- * such a tuple runs too. Over a model that declares every type in the
39
- * file (the cloud's drift test, built from the live model) nothing is
40
- * recorded and everything runs — the same kit, no edition branch.
41
- * `skipped` is therefore complete once every case has run; a consumer
42
- * pins it after the cases, which is the order a test file declares them.
43
- *
44
- * The parser is strict on purpose: an unknown key anywhere is a thrown
45
- * fault, so a change to the format upstream is loud here rather than
46
- * half-run. The model declares no condition, so the format's `condition`
47
- * on a tuple and `context` on a check are among the keys refused: a
48
- * document that still carries them is written against a model this
49
- * evaluator does not answer, and saying so beats running it half-blind.
50
- */
51
- import assert from "node:assert/strict";
52
-
53
- import { checkRelation } from "./evaluator.js";
54
- import type { Model } from "./model/index.js";
55
- import type { Person, Subject, Tuple, TupleSource } from "./tuples.js";
56
- import {
57
- ACCOUNT_TYPE,
58
- newInMemoryTupleSource,
59
- parseObjectRef,
60
- parseSubject,
61
- } from "./tuples.js";
62
-
63
- // ---------------------------------------------------------------------------
64
- // The document, typed exactly as the cloud's files use the format.
65
- // ---------------------------------------------------------------------------
66
-
67
- export interface StoreTestTuple {
68
- readonly user: string;
69
- readonly relation: string;
70
- readonly object: string;
71
- }
72
-
73
- export interface StoreTestCheck {
74
- readonly user: string;
75
- readonly object: string;
76
- readonly assertions: Readonly<Record<string, boolean>>;
77
- }
78
-
79
- export interface StoreTestListObjects {
80
- readonly user: string;
81
- readonly type: string;
82
- readonly assertions: Readonly<Record<string, ReadonlyArray<string>>>;
83
- }
84
-
85
- export interface StoreTest {
86
- readonly name: string;
87
- readonly check: ReadonlyArray<StoreTestCheck>;
88
- readonly list_objects: ReadonlyArray<StoreTestListObjects>;
89
- }
90
-
91
- export interface StoreTestDocument {
92
- readonly name: string;
93
- readonly tuples: ReadonlyArray<StoreTestTuple>;
94
- readonly tests: ReadonlyArray<StoreTest>;
95
- }
96
-
97
- /**
98
- * Parses a loaded YAML value into a document, refusing anything the
99
- * format the cloud's files use does not carry (`sourceName` names the
100
- * file in every message).
101
- */
102
- export function parseStoreTestDocument(
103
- value: unknown,
104
- sourceName: string,
105
- ): StoreTestDocument {
106
- const root = record(value, sourceName, [
107
- "name",
108
- "model_file",
109
- "tuples",
110
- "tests",
111
- ]);
112
- return {
113
- name: text(root["name"], `${sourceName}: name`),
114
- tuples: list(root["tuples"], `${sourceName}: tuples`).map((entry, index) =>
115
- parseTuple(entry, `${sourceName}: tuples[${index}]`),
116
- ),
117
- tests: list(root["tests"], `${sourceName}: tests`).map((entry, index) =>
118
- parseTest(entry, `${sourceName}: tests[${index}]`),
119
- ),
120
- };
121
- }
122
-
123
- function parseTuple(value: unknown, where: string): StoreTestTuple {
124
- const entry = record(value, where, ["user", "relation", "object"]);
125
- return {
126
- user: text(entry["user"], `${where}.user`),
127
- relation: text(entry["relation"], `${where}.relation`),
128
- object: text(entry["object"], `${where}.object`),
129
- };
130
- }
131
-
132
- function parseTest(value: unknown, where: string): StoreTest {
133
- const entry = record(value, where, ["name", "check", "list_objects"]);
134
- return {
135
- name: text(entry["name"], `${where}.name`),
136
- check: optionalList(entry["check"], `${where}.check`).map((item, index) =>
137
- parseCheck(item, `${where}.check[${index}]`),
138
- ),
139
- list_objects: optionalList(
140
- entry["list_objects"],
141
- `${where}.list_objects`,
142
- ).map((item, index) =>
143
- parseListObjects(item, `${where}.list_objects[${index}]`),
144
- ),
145
- };
146
- }
147
-
148
- function parseCheck(value: unknown, where: string): StoreTestCheck {
149
- const entry = record(value, where, ["user", "object", "assertions"]);
150
- const assertions = record(entry["assertions"], `${where}.assertions`);
151
- const parsed: Record<string, boolean> = {};
152
- for (const [relation, expected] of Object.entries(assertions)) {
153
- if (typeof expected !== "boolean") {
154
- throw new Error(`${where}.assertions.${relation}: expected a boolean`);
155
- }
156
- parsed[relation] = expected;
157
- }
158
- return {
159
- user: text(entry["user"], `${where}.user`),
160
- object: text(entry["object"], `${where}.object`),
161
- assertions: parsed,
162
- };
163
- }
164
-
165
- function parseListObjects(value: unknown, where: string): StoreTestListObjects {
166
- const entry = record(value, where, ["user", "type", "assertions"]);
167
- const assertions = record(entry["assertions"], `${where}.assertions`);
168
- const parsed: Record<string, ReadonlyArray<string>> = {};
169
- for (const [relation, expected] of Object.entries(assertions)) {
170
- parsed[relation] = list(expected, `${where}.assertions.${relation}`).map(
171
- (item, index) => text(item, `${where}.assertions.${relation}[${index}]`),
172
- );
173
- }
174
- return {
175
- user: text(entry["user"], `${where}.user`),
176
- type: text(entry["type"], `${where}.type`),
177
- assertions: parsed,
178
- };
179
- }
180
-
181
- function record(
182
- value: unknown,
183
- where: string,
184
- allowedKeys?: ReadonlyArray<string>,
185
- ): Record<string, unknown> {
186
- if (value === null || typeof value !== "object" || Array.isArray(value)) {
187
- throw new Error(`${where}: expected a mapping`);
188
- }
189
- const entry = value as Record<string, unknown>;
190
- if (allowedKeys !== undefined) {
191
- for (const key of Object.keys(entry)) {
192
- if (!allowedKeys.includes(key)) {
193
- throw new Error(`${where}: unknown key '${key}'`);
194
- }
195
- }
196
- }
197
- return entry;
198
- }
199
-
200
- function list(value: unknown, where: string): ReadonlyArray<unknown> {
201
- if (!Array.isArray(value)) {
202
- throw new Error(`${where}: expected a list`);
203
- }
204
- return value as ReadonlyArray<unknown>;
205
- }
206
-
207
- function optionalList(value: unknown, where: string): ReadonlyArray<unknown> {
208
- return value === undefined ? [] : list(value, where);
209
- }
210
-
211
- function text(value: unknown, where: string): string {
212
- if (typeof value !== "string") {
213
- throw new Error(`${where}: expected a string`);
214
- }
215
- return value;
216
- }
217
-
218
- // ---------------------------------------------------------------------------
219
- // The cases.
220
- // ---------------------------------------------------------------------------
221
-
222
- export interface StoreTestCase {
223
- /** `<document> :: <test name>` — the framework prints it as the test name. */
224
- readonly name: string;
225
- run(): Promise<void>;
226
- }
227
-
228
- export type StoreTestSkipReason =
229
- | "undeclared-kind"
230
- | "list-objects"
231
- | "undeclared-subject-type";
232
-
233
- /** One assertion (or assertion group) the kit did not run, in the document's words. */
234
- export interface StoreTestSkip {
235
- readonly document: string;
236
- readonly test: string;
237
- readonly subject: string;
238
- /** The check's object, or `<type>` for a list_objects assertion. */
239
- readonly object: string;
240
- /** The one relation, for a skip decided per assertion; absent for a group. */
241
- readonly relation?: string;
242
- readonly reason: StoreTestSkipReason;
243
- /** For `undeclared-subject-type`: the types the walk read that the model does not declare, sorted. */
244
- readonly types?: ReadonlyArray<string>;
245
- }
246
-
247
- export interface StoreTestKit {
248
- readonly cases: ReadonlyArray<StoreTestCase>;
249
- /** Complete once every case has run (the module header). */
250
- readonly skipped: ReadonlyArray<StoreTestSkip>;
251
- }
252
-
253
- /**
254
- * The document's tests as runnable cases over `model`, plus every
255
- * assertion group the kit does not run.
256
- */
257
- export function storeTestCases(
258
- document: StoreTestDocument,
259
- model: Model,
260
- ): StoreTestKit {
261
- const source = newInMemoryTupleSource(document.tuples.map(tupleOf));
262
- const skipped: StoreTestSkip[] = [];
263
- const cases: StoreTestCase[] = [];
264
-
265
- for (const test of document.tests) {
266
- const runnable: StoreTestCheck[] = [];
267
- for (const item of test.check) {
268
- const object = parseObjectRef(item.object);
269
- if (model.byType(object.type) === undefined) {
270
- skipped.push({
271
- document: document.name,
272
- test: test.name,
273
- subject: item.user,
274
- object: item.object,
275
- reason: "undeclared-kind",
276
- });
277
- } else {
278
- runnable.push(item);
279
- }
280
- }
281
- for (const item of test.list_objects) {
282
- skipped.push({
283
- document: document.name,
284
- test: test.name,
285
- subject: item.user,
286
- object: `<${item.type}>`,
287
- reason: "list-objects",
288
- });
289
- }
290
- if (runnable.length === 0) {
291
- continue;
292
- }
293
- cases.push({
294
- name: `${document.name} :: ${test.name}`,
295
- async run() {
296
- for (const item of runnable) {
297
- const object = parseObjectRef(item.object);
298
- const person = personOf(item.user);
299
- for (const [relation, expected] of Object.entries(item.assertions)) {
300
- const recording = recordUndeclaredTypes(source, model);
301
- const actual = await checkRelation(
302
- { model, source: recording },
303
- object,
304
- relation,
305
- person,
306
- );
307
- if (recording.reached.size > 0) {
308
- skipped.push({
309
- document: document.name,
310
- test: test.name,
311
- subject: item.user,
312
- object: item.object,
313
- relation,
314
- reason: "undeclared-subject-type",
315
- types: [...recording.reached].sort(),
316
- });
317
- continue;
318
- }
319
- assert.equal(
320
- actual,
321
- expected,
322
- `${document.name} :: ${test.name}: ${item.user} ${relation} ${item.object} expected ${String(expected)}, got ${String(actual)}`,
323
- );
324
- }
325
- }
326
- },
327
- });
328
- }
329
- return { cases, skipped };
330
- }
331
-
332
- interface RecordingTupleSource extends TupleSource {
333
- /** The undeclared types named by the subjects of the tuples the walk read. */
334
- readonly reached: ReadonlySet<string>;
335
- }
336
-
337
- /**
338
- * A view of `inner` that records, for one walk, every subject type the
339
- * evaluator read that `model` does not declare. A person
340
- * (`identity_account:<id>`) is a subject the evaluator compares, never a
341
- * type it resolves, so it is not a "reach" whatever the model declares; a
342
- * userset or a parent object of an undeclared type is.
343
- */
344
- function recordUndeclaredTypes(
345
- inner: TupleSource,
346
- model: Model,
347
- ): RecordingTupleSource {
348
- const reached = new Set<string>();
349
- return {
350
- reached,
351
- async tuplesOf(object, relation) {
352
- const tuples = await inner.tuplesOf(object, relation);
353
- for (const tuple of tuples) {
354
- const type = resolvedTypeOf(tuple.subject);
355
- if (type !== undefined && model.byType(type) === undefined) {
356
- reached.add(type);
357
- }
358
- }
359
- return tuples;
360
- },
361
- };
362
- }
363
-
364
- /** The type the evaluator would resolve a subject on, or undefined for a person. */
365
- function resolvedTypeOf(subject: Subject): string | undefined {
366
- switch (subject.form) {
367
- case "object":
368
- return subject.object.type === ACCOUNT_TYPE
369
- ? undefined
370
- : subject.object.type;
371
- case "userset":
372
- return subject.object.type;
373
- default: {
374
- const exhaustive: never = subject;
375
- throw new Error(`unknown subject form: ${JSON.stringify(exhaustive)}`);
376
- }
377
- }
378
- }
379
-
380
- function tupleOf(entry: StoreTestTuple): Tuple {
381
- return {
382
- object: parseObjectRef(entry.object),
383
- relation: entry.relation,
384
- subject: parseSubject(entry.user),
385
- };
386
- }
387
-
388
- /** The document's users are plain accounts; a user of another type is a format the kit does not run. */
389
- function personOf(user: string): Person {
390
- const object = parseObjectRef(user);
391
- if (object.type !== ACCOUNT_TYPE || object.id === "*") {
392
- throw new Error(`store test users are accounts; got '${user}'`);
393
- }
394
- return { accountId: object.id, aliases: new Set([object.id]) };
395
- }