@stigmer/server 3.27.2 → 3.29.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 (227) hide show
  1. package/README.md +2 -0
  2. package/dist/authorization/authorizer.d.ts +3 -3
  3. package/dist/authorization/authorizer.js +3 -3
  4. package/dist/authorization/derived-tuples.js +6 -1
  5. package/dist/authorization/derived-tuples.js.map +1 -1
  6. package/dist/authorization/evaluator.d.ts +9 -10
  7. package/dist/authorization/evaluator.d.ts.map +1 -1
  8. package/dist/authorization/evaluator.js.map +1 -1
  9. package/dist/authorization/list-read-scope.d.ts.map +1 -1
  10. package/dist/authorization/list-read-scope.js +29 -18
  11. package/dist/authorization/list-read-scope.js.map +1 -1
  12. package/dist/authorization/model/bindings.d.ts +41 -0
  13. package/dist/authorization/model/bindings.d.ts.map +1 -0
  14. package/dist/authorization/model/bindings.js +89 -0
  15. package/dist/authorization/model/bindings.js.map +1 -0
  16. package/dist/authorization/model/data/authorization-model.json +3141 -0
  17. package/dist/authorization/model/default-of.d.ts +2 -2
  18. package/dist/authorization/model/execution-viewer.d.ts +3 -0
  19. package/dist/authorization/model/execution-viewer.d.ts.map +1 -0
  20. package/dist/authorization/model/execution-viewer.js +50 -0
  21. package/dist/authorization/model/execution-viewer.js.map +1 -0
  22. package/dist/authorization/model/index.d.ts +33 -21
  23. package/dist/authorization/model/index.d.ts.map +1 -1
  24. package/dist/authorization/model/index.js +74 -52
  25. package/dist/authorization/model/index.js.map +1 -1
  26. package/dist/authorization/model/openfga-json.d.ts +18 -0
  27. package/dist/authorization/model/openfga-json.d.ts.map +1 -0
  28. package/dist/authorization/model/openfga-json.js +266 -0
  29. package/dist/authorization/model/openfga-json.js.map +1 -0
  30. package/dist/authorization/model/rewrite.d.ts +39 -67
  31. package/dist/authorization/model/rewrite.d.ts.map +1 -1
  32. package/dist/authorization/model/rewrite.js +1 -117
  33. package/dist/authorization/model/rewrite.js.map +1 -1
  34. package/dist/authorization/strict-json.d.ts +20 -0
  35. package/dist/authorization/strict-json.d.ts.map +1 -0
  36. package/dist/authorization/strict-json.js +45 -0
  37. package/dist/authorization/strict-json.js.map +1 -0
  38. package/dist/boot/compose.d.ts.map +1 -1
  39. package/dist/boot/compose.js +9 -7
  40. package/dist/boot/compose.js.map +1 -1
  41. package/dist/domain/workflowexecution/list-index.d.ts +1 -1
  42. package/dist/extensions/list-read-scope.d.ts +20 -3
  43. package/dist/extensions/list-read-scope.d.ts.map +1 -1
  44. package/dist/extensions/list-read-scope.js +20 -1
  45. package/dist/extensions/list-read-scope.js.map +1 -1
  46. package/dist/index.d.ts +7 -8
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +15 -5
  49. package/dist/index.js.map +1 -1
  50. package/package.json +7 -6
  51. package/src/authorization/README.md +41 -34
  52. package/src/authorization/__tests__/authorizer.test.ts +5 -14
  53. package/src/authorization/__tests__/derived-tuples.test.ts +29 -27
  54. package/src/authorization/__tests__/evaluator.test.ts +53 -160
  55. package/src/authorization/__tests__/facts.test.ts +5 -11
  56. package/src/authorization/__tests__/list-read-scope.measure.test.ts +12 -20
  57. package/src/authorization/__tests__/list-read-scope.test.ts +15 -14
  58. package/src/authorization/__tests__/organization-directory.test.ts +2 -2
  59. package/src/authorization/__tests__/store-test-kit.ts +246 -0
  60. package/src/authorization/__tests__/store-tests.test.ts +92 -223
  61. package/src/authorization/__tests__/support.ts +30 -2
  62. package/src/authorization/__tests__/throwaway-model.ts +75 -0
  63. package/src/authorization/__tests__/wire-permissions.test.ts +141 -107
  64. package/src/authorization/authorizer.ts +3 -3
  65. package/src/authorization/derived-tuples.ts +7 -2
  66. package/src/authorization/evaluator.ts +8 -10
  67. package/src/authorization/list-read-scope.ts +37 -22
  68. package/src/authorization/model/__tests__/openfga-json.test.ts +353 -0
  69. package/src/authorization/model/__tests__/registry.test.ts +345 -277
  70. package/src/authorization/model/bindings.ts +131 -0
  71. package/src/authorization/model/data/authorization-model.json +3141 -0
  72. package/src/authorization/model/default-of.ts +2 -2
  73. package/src/authorization/model/execution-viewer.ts +56 -0
  74. package/src/authorization/model/index.ts +81 -72
  75. package/src/authorization/model/openfga-json.ts +310 -0
  76. package/src/authorization/model/rewrite.ts +39 -188
  77. package/src/authorization/strict-json.ts +52 -0
  78. package/src/boot/compose.ts +9 -7
  79. package/src/extensions/__tests__/built-in-authorization-composed.test.ts +3 -6
  80. package/src/extensions/list-read-scope.ts +32 -4
  81. package/src/index.ts +15 -44
  82. package/dist/authorization/model/agent.d.ts +0 -2
  83. package/dist/authorization/model/agent.d.ts.map +0 -1
  84. package/dist/authorization/model/agent.js +0 -43
  85. package/dist/authorization/model/agent.js.map +0 -1
  86. package/dist/authorization/model/agent_channel.d.ts +0 -2
  87. package/dist/authorization/model/agent_channel.d.ts.map +0 -1
  88. package/dist/authorization/model/agent_channel.js +0 -39
  89. package/dist/authorization/model/agent_channel.js.map +0 -1
  90. package/dist/authorization/model/agent_execution.d.ts +0 -2
  91. package/dist/authorization/model/agent_execution.d.ts.map +0 -1
  92. package/dist/authorization/model/agent_execution.js +0 -25
  93. package/dist/authorization/model/agent_execution.js.map +0 -1
  94. package/dist/authorization/model/agent_instance.d.ts +0 -2
  95. package/dist/authorization/model/agent_instance.d.ts.map +0 -1
  96. package/dist/authorization/model/agent_instance.js +0 -48
  97. package/dist/authorization/model/agent_instance.js.map +0 -1
  98. package/dist/authorization/model/agent_share.d.ts +0 -2
  99. package/dist/authorization/model/agent_share.d.ts.map +0 -1
  100. package/dist/authorization/model/agent_share.js +0 -29
  101. package/dist/authorization/model/agent_share.js.map +0 -1
  102. package/dist/authorization/model/api_key.d.ts +0 -2
  103. package/dist/authorization/model/api_key.d.ts.map +0 -1
  104. package/dist/authorization/model/api_key.js +0 -27
  105. package/dist/authorization/model/api_key.js.map +0 -1
  106. package/dist/authorization/model/artifact.d.ts +0 -2
  107. package/dist/authorization/model/artifact.d.ts.map +0 -1
  108. package/dist/authorization/model/artifact.js +0 -27
  109. package/dist/authorization/model/artifact.js.map +0 -1
  110. package/dist/authorization/model/channel_app.d.ts +0 -2
  111. package/dist/authorization/model/channel_app.d.ts.map +0 -1
  112. package/dist/authorization/model/channel_app.js +0 -28
  113. package/dist/authorization/model/channel_app.js.map +0 -1
  114. package/dist/authorization/model/environment.d.ts +0 -2
  115. package/dist/authorization/model/environment.d.ts.map +0 -1
  116. package/dist/authorization/model/environment.js +0 -33
  117. package/dist/authorization/model/environment.js.map +0 -1
  118. package/dist/authorization/model/execution_context.d.ts +0 -2
  119. package/dist/authorization/model/execution_context.d.ts.map +0 -1
  120. package/dist/authorization/model/execution_context.js +0 -19
  121. package/dist/authorization/model/execution_context.js.map +0 -1
  122. package/dist/authorization/model/iam_policy.d.ts +0 -2
  123. package/dist/authorization/model/iam_policy.d.ts.map +0 -1
  124. package/dist/authorization/model/iam_policy.js +0 -31
  125. package/dist/authorization/model/iam_policy.js.map +0 -1
  126. package/dist/authorization/model/identity_account.d.ts +0 -2
  127. package/dist/authorization/model/identity_account.d.ts.map +0 -1
  128. package/dist/authorization/model/identity_account.js +0 -25
  129. package/dist/authorization/model/identity_account.js.map +0 -1
  130. package/dist/authorization/model/mcp_server.d.ts +0 -2
  131. package/dist/authorization/model/mcp_server.d.ts.map +0 -1
  132. package/dist/authorization/model/mcp_server.js +0 -38
  133. package/dist/authorization/model/mcp_server.js.map +0 -1
  134. package/dist/authorization/model/memory.d.ts +0 -2
  135. package/dist/authorization/model/memory.d.ts.map +0 -1
  136. package/dist/authorization/model/memory.js +0 -26
  137. package/dist/authorization/model/memory.js.map +0 -1
  138. package/dist/authorization/model/oauth_app.d.ts +0 -2
  139. package/dist/authorization/model/oauth_app.d.ts.map +0 -1
  140. package/dist/authorization/model/oauth_app.js +0 -31
  141. package/dist/authorization/model/oauth_app.js.map +0 -1
  142. package/dist/authorization/model/organization.d.ts +0 -2
  143. package/dist/authorization/model/organization.d.ts.map +0 -1
  144. package/dist/authorization/model/organization.js +0 -58
  145. package/dist/authorization/model/organization.js.map +0 -1
  146. package/dist/authorization/model/platform_client.d.ts +0 -2
  147. package/dist/authorization/model/platform_client.d.ts.map +0 -1
  148. package/dist/authorization/model/platform_client.js +0 -31
  149. package/dist/authorization/model/platform_client.js.map +0 -1
  150. package/dist/authorization/model/plugin.d.ts +0 -2
  151. package/dist/authorization/model/plugin.d.ts.map +0 -1
  152. package/dist/authorization/model/plugin.js +0 -45
  153. package/dist/authorization/model/plugin.js.map +0 -1
  154. package/dist/authorization/model/schedule.d.ts +0 -2
  155. package/dist/authorization/model/schedule.d.ts.map +0 -1
  156. package/dist/authorization/model/schedule.js +0 -34
  157. package/dist/authorization/model/schedule.js.map +0 -1
  158. package/dist/authorization/model/session.d.ts +0 -2
  159. package/dist/authorization/model/session.d.ts.map +0 -1
  160. package/dist/authorization/model/session.js +0 -38
  161. package/dist/authorization/model/session.js.map +0 -1
  162. package/dist/authorization/model/skill.d.ts +0 -2
  163. package/dist/authorization/model/skill.d.ts.map +0 -1
  164. package/dist/authorization/model/skill.js +0 -39
  165. package/dist/authorization/model/skill.js.map +0 -1
  166. package/dist/authorization/model/workflow.d.ts +0 -2
  167. package/dist/authorization/model/workflow.d.ts.map +0 -1
  168. package/dist/authorization/model/workflow.js +0 -39
  169. package/dist/authorization/model/workflow.js.map +0 -1
  170. package/dist/authorization/model/workflow_execution.d.ts +0 -2
  171. package/dist/authorization/model/workflow_execution.d.ts.map +0 -1
  172. package/dist/authorization/model/workflow_execution.js +0 -33
  173. package/dist/authorization/model/workflow_execution.js.map +0 -1
  174. package/dist/authorization/model/workflow_instance.d.ts +0 -2
  175. package/dist/authorization/model/workflow_instance.d.ts.map +0 -1
  176. package/dist/authorization/model/workflow_instance.js +0 -96
  177. package/dist/authorization/model/workflow_instance.js.map +0 -1
  178. package/dist/authorization/store-test-kit.d.ts +0 -62
  179. package/dist/authorization/store-test-kit.d.ts.map +0 -1
  180. package/dist/authorization/store-test-kit.js +0 -265
  181. package/dist/authorization/store-test-kit.js.map +0 -1
  182. package/src/authorization/__tests__/enterprise-model.ts +0 -59
  183. package/src/authorization/__tests__/fixtures/fga/agent-channel-owner.fga.yaml +0 -112
  184. package/src/authorization/__tests__/fixtures/fga/agent-instance-creation.fga.yaml +0 -177
  185. package/src/authorization/__tests__/fixtures/fga/agent-share-owner.fga.yaml +0 -109
  186. package/src/authorization/__tests__/fixtures/fga/artifact-org-and-owner.fga.yaml +0 -128
  187. package/src/authorization/__tests__/fixtures/fga/blueprint-private-visibility.fga.yaml +0 -318
  188. package/src/authorization/__tests__/fixtures/fga/channel-app-owner.fga.yaml +0 -125
  189. package/src/authorization/__tests__/fixtures/fga/channel-participation.fga.yaml +0 -201
  190. package/src/authorization/__tests__/fixtures/fga/channel-session-visibility.fga.yaml +0 -187
  191. package/src/authorization/__tests__/fixtures/fga/default-instance-inheritance.fga.yaml +0 -214
  192. package/src/authorization/__tests__/fixtures/fga/mcp-server-authoring.fga.yaml +0 -129
  193. package/src/authorization/__tests__/fixtures/fga/memory-subject-only.fga.yaml +0 -72
  194. package/src/authorization/__tests__/fixtures/fga/org-admin-owner-inheritance.fga.yaml +0 -308
  195. package/src/authorization/__tests__/fixtures/fga/org-shared-environment.fga.yaml +0 -144
  196. package/src/authorization/__tests__/fixtures/fga/platform-visibility.fga.yaml +0 -199
  197. package/src/authorization/__tests__/fixtures/fga/provider-standing-viewer.fga.yaml +0 -40
  198. package/src/authorization/__tests__/fixtures/fga/reserved-label-writer.fga.yaml +0 -41
  199. package/src/authorization/__tests__/fixtures/fga/schedule-owner.fga.yaml +0 -113
  200. package/src/authorization/__tests__/fixtures/fga/schedule-session-visibility.fga.yaml +0 -214
  201. package/src/authorization/__tests__/fixtures/fga/session-personal-resource.fga.yaml +0 -117
  202. package/src/authorization/__tests__/fixtures/fga/workflow-execution-sharing.fga.yaml +0 -190
  203. package/src/authorization/model/agent.ts +0 -64
  204. package/src/authorization/model/agent_channel.ts +0 -57
  205. package/src/authorization/model/agent_execution.ts +0 -33
  206. package/src/authorization/model/agent_instance.ts +0 -66
  207. package/src/authorization/model/agent_share.ts +0 -40
  208. package/src/authorization/model/api_key.ts +0 -28
  209. package/src/authorization/model/artifact.ts +0 -38
  210. package/src/authorization/model/channel_app.ts +0 -39
  211. package/src/authorization/model/environment.ts +0 -48
  212. package/src/authorization/model/execution_context.ts +0 -20
  213. package/src/authorization/model/iam_policy.ts +0 -42
  214. package/src/authorization/model/identity_account.ts +0 -26
  215. package/src/authorization/model/mcp_server.ts +0 -59
  216. package/src/authorization/model/memory.ts +0 -27
  217. package/src/authorization/model/oauth_app.ts +0 -43
  218. package/src/authorization/model/organization.ts +0 -59
  219. package/src/authorization/model/platform_client.ts +0 -43
  220. package/src/authorization/model/plugin.ts +0 -66
  221. package/src/authorization/model/schedule.ts +0 -49
  222. package/src/authorization/model/session.ts +0 -50
  223. package/src/authorization/model/skill.ts +0 -60
  224. package/src/authorization/model/workflow.ts +0 -60
  225. package/src/authorization/model/workflow_execution.ts +0 -46
  226. package/src/authorization/model/workflow_instance.ts +0 -125
  227. package/src/authorization/store-test-kit.ts +0 -395
@@ -1,46 +0,0 @@
1
- /**
2
- * Transcript of fga/model/agentic/workflow_execution.fga — a run with an
3
- * owner of its own (the person who triggered it; `kind_meta` attribution
4
- * DIRECT, unlike agent_execution's inherited owner) and two ways in for
5
- * others: an explicit per-run grant, or the instance's opt-in run
6
- * audience through `execution_viewer from workflow_instance`, standing on
7
- * the `workflow_instance` link the derivation writes from
8
- * `spec.workflow_instance_id`. No `can_delete`: run history outlives its
9
- * instance by design. The per-run grant may name an Enterprise team
10
- * (`team#member`), which no open-source tuple ever does.
11
- */
12
- import { WorkflowExecutionSchema } from "@stigmer/protos/ai/stigmer/agentic/workflowexecution/v1/api_pb";
13
- import { ApiResourceKind } from "@stigmer/protos/ai/stigmer/commons/apiresource/apiresourcekind/api_resource_kind_pb";
14
-
15
- import {
16
- computed,
17
- declareKind,
18
- direct,
19
- from,
20
- objectOf,
21
- union,
22
- usersetOf,
23
- } from "./rewrite.js";
24
-
25
- export const workflowExecutionDeclaration = declareKind({
26
- kind: ApiResourceKind.workflow_execution,
27
- schema: WorkflowExecutionSchema,
28
- source: "fga/model/agentic/workflow_execution.fga",
29
- relations: [
30
- ["organization", direct(objectOf("organization"))],
31
- ["workflow_instance", direct(objectOf("workflow_instance"))],
32
- ["owner", direct(objectOf("identity_account"))],
33
- [
34
- "viewer",
35
- union(
36
- direct(objectOf("identity_account"), usersetOf("team", "member")),
37
- computed("owner"),
38
- from("execution_viewer", "workflow_instance"),
39
- ),
40
- ],
41
- ["can_view", computed("viewer")],
42
- ["can_edit", computed("owner")],
43
- ["can_grant_access", computed("owner")],
44
- ["can_view_access", computed("viewer")],
45
- ],
46
- });
@@ -1,125 +0,0 @@
1
- /**
2
- * Transcript of fga/model/agentic/workflow_instance.fga — the agent
3
- * instance's shape plus one relation of its own: `execution_viewer`, the
4
- * opt-in audience for the instance's RUN history, kept apart from
5
- * `viewer` so making an instance org-runnable never exposes other
6
- * people's run inputs and outputs. Consumed by workflow_execution through
7
- * `execution_viewer from workflow_instance`.
8
- *
9
- * `execution_viewer` is derived here from `spec.execution_visibility`
10
- * (the second rule `kind_meta` cannot express): `organization` derives
11
- * `#execution_viewer@organization:<org>#viewer` — the organization's full
12
- * read audience, the shape the file's comment names and the cloud's own
13
- * store test writes, with `#member` the legacy shape the line still
14
- * admits — and `private` or unset derives nothing, so each run stays its
15
- * triggerer's. The cloud has no writer for this tuple today; this
16
- * edition enforces the field as the contract states it.
17
- *
18
- * `team#member` on `viewer` is the Enterprise team grant, which no
19
- * open-source tuple ever names; `execution_viewer` is not a grantable role
20
- * and admits no team.
21
- */
22
- import { isMessage } from "@bufbuild/protobuf";
23
-
24
- import { WorkflowSchema } from "@stigmer/protos/ai/stigmer/agentic/workflow/v1/api_pb";
25
- import { WorkflowInstanceSchema } from "@stigmer/protos/ai/stigmer/agentic/workflowinstance/v1/api_pb";
26
- import { WorkflowExecutionVisibility } from "@stigmer/protos/ai/stigmer/agentic/workflowinstance/v1/spec_pb";
27
- import { ApiResourceKind } from "@stigmer/protos/ai/stigmer/commons/apiresource/apiresourcekind/api_resource_kind_pb";
28
-
29
- import { defaultOfBlueprint } from "./default-of.js";
30
- import type { DerivedRelation } from "./rewrite.js";
31
- import {
32
- computed,
33
- declareKind,
34
- direct,
35
- from,
36
- objectOf,
37
- union,
38
- usersetOf,
39
- } from "./rewrite.js";
40
-
41
- const executionViewer: DerivedRelation = (object, row) => {
42
- if (!isMessage(row, WorkflowInstanceSchema)) {
43
- return Promise.resolve([]);
44
- }
45
- const level =
46
- row.spec?.executionVisibility ?? WorkflowExecutionVisibility.unspecified;
47
- switch (level) {
48
- case WorkflowExecutionVisibility.organization: {
49
- const org = row.metadata?.org ?? "";
50
- return Promise.resolve(
51
- org === ""
52
- ? []
53
- : [
54
- {
55
- object,
56
- relation: "execution_viewer",
57
- subject: {
58
- form: "userset",
59
- object: { type: "organization", id: org },
60
- relation: "viewer",
61
- },
62
- },
63
- ],
64
- );
65
- }
66
- case WorkflowExecutionVisibility.private:
67
- case WorkflowExecutionVisibility.unspecified:
68
- return Promise.resolve([]);
69
- default: {
70
- const exhaustive: never = level;
71
- throw new Error(`unknown execution visibility: ${String(exhaustive)}`);
72
- }
73
- }
74
- };
75
-
76
- export const workflowInstanceDeclaration = declareKind({
77
- kind: ApiResourceKind.workflow_instance,
78
- schema: WorkflowInstanceSchema,
79
- source: "fga/model/agentic/workflow_instance.fga",
80
- relations: [
81
- ["organization", direct(objectOf("organization"))],
82
- ["workflow", direct(objectOf("workflow"))],
83
- ["default_of", direct(objectOf("workflow"))],
84
- ["owner", direct(objectOf("identity_account"))],
85
- [
86
- "viewer",
87
- union(
88
- direct(
89
- objectOf("identity_account"),
90
- usersetOf("organization", "member"),
91
- usersetOf("organization", "viewer"),
92
- usersetOf("team", "member"),
93
- ),
94
- computed("owner"),
95
- from("viewer", "default_of"),
96
- ),
97
- ],
98
- [
99
- "execution_viewer",
100
- direct(
101
- objectOf("identity_account"),
102
- usersetOf("organization", "member"),
103
- usersetOf("organization", "viewer"),
104
- ),
105
- ],
106
- ["can_view", computed("viewer")],
107
- ["can_execute", computed("can_view")],
108
- ["can_edit", computed("owner")],
109
- ["can_delete", computed("owner")],
110
- ["can_grant_access", computed("owner")],
111
- ["can_view_access", computed("viewer")],
112
- ],
113
- derived: [
114
- [
115
- "default_of",
116
- defaultOfBlueprint({
117
- kind: ApiResourceKind.workflow,
118
- schema: WorkflowSchema,
119
- defaultInstanceIdOf: (workflow) =>
120
- workflow.status?.defaultInstanceId ?? "",
121
- }),
122
- ],
123
- ["execution_viewer", executionViewer],
124
- ],
125
- });
@@ -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
- }