@sanity/workflow-engine-test 0.6.1 → 0.8.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.
- package/README.md +2 -2
- package/dist/index.cjs +31 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +38 -30
- package/dist/index.d.ts +38 -30
- package/dist/index.js +31 -14
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ that lock the documents it governs while the stage is active, and lift when it
|
|
|
20
20
|
exits. There are two distinct things you test, and the bench gives you a helper
|
|
21
21
|
for each:
|
|
22
22
|
|
|
23
|
-
- **Write-time enforcement** — `bench.editDocument(
|
|
23
|
+
- **Write-time enforcement** — `bench.editDocument({documentId, patch})` writes through the
|
|
24
24
|
bench client, which **simulates the lake's server-side guard rejection**: a
|
|
25
25
|
write a deployed guard denies throws `MutationGuardDeniedError`. In production
|
|
26
26
|
the lake enforces this for _any_ client; the bench reproduces it on the
|
|
@@ -41,7 +41,7 @@ const bench = createBench({documents: [{_id: 'doc-1', _type: 'article', body: 'b
|
|
|
41
41
|
const active = await bench.activeGuardsForDocument('doc-1')
|
|
42
42
|
|
|
43
43
|
// Enforcement: a guard-violating write is rejected.
|
|
44
|
-
await expect(bench.editDocument('doc-1', {body: 'edited'})).rejects.toThrow(
|
|
44
|
+
await expect(bench.editDocument({documentId: 'doc-1', patch: {body: 'edited'}})).rejects.toThrow(
|
|
45
45
|
MutationGuardDeniedError,
|
|
46
46
|
)
|
|
47
47
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -5073,7 +5073,11 @@ function resolveBenchAccess(options) {
|
|
|
5073
5073
|
grants: options.grants ?? WILDCARD_GRANTS
|
|
5074
5074
|
};
|
|
5075
5075
|
}
|
|
5076
|
-
function accessFor(
|
|
5076
|
+
function accessFor({
|
|
5077
|
+
access,
|
|
5078
|
+
args,
|
|
5079
|
+
canTokenResolve
|
|
5080
|
+
}) {
|
|
5077
5081
|
if (args.access !== void 0) return args.access;
|
|
5078
5082
|
if (args.actor === void 0 && args.grants === void 0) return access;
|
|
5079
5083
|
const inheritedGrants = canTokenResolve ? void 0 : access.grants, grants = args.grants ?? inheritedGrants;
|
|
@@ -5086,10 +5090,14 @@ function stripAccessShortcuts(args) {
|
|
|
5086
5090
|
const rest = { ...args };
|
|
5087
5091
|
return delete rest.access, delete rest.actor, delete rest.grants, rest;
|
|
5088
5092
|
}
|
|
5089
|
-
function createEngineWrappers(
|
|
5093
|
+
function createEngineWrappers({
|
|
5094
|
+
scope,
|
|
5095
|
+
access,
|
|
5096
|
+
clock
|
|
5097
|
+
}) {
|
|
5090
5098
|
const canTokenResolve = "request" in scope.client, withAccess = (args) => ({
|
|
5091
5099
|
...scope,
|
|
5092
|
-
access: accessFor(access, args, canTokenResolve),
|
|
5100
|
+
access: accessFor({ access, args, canTokenResolve }),
|
|
5093
5101
|
clock,
|
|
5094
5102
|
...stripAccessShortcuts(args)
|
|
5095
5103
|
});
|
|
@@ -5121,8 +5129,8 @@ function createGuardHelpers(client) {
|
|
|
5121
5129
|
...as ? { identity: as.id } : {}
|
|
5122
5130
|
});
|
|
5123
5131
|
},
|
|
5124
|
-
editDocument: async (documentId, patch,
|
|
5125
|
-
const action =
|
|
5132
|
+
editDocument: async ({ documentId, patch, action: editAction, as }) => {
|
|
5133
|
+
const action = editAction ?? "update", writer = as ? client.withConfig({
|
|
5126
5134
|
accessControl: { currentUser: { id: as.id }, grants: WILDCARD_GRANTS }
|
|
5127
5135
|
}) : client;
|
|
5128
5136
|
return action === "delete" ? await writer.delete(documentId) : await writer.patch(documentId).set(patch).commit(), await client.getDocument(documentId);
|
|
@@ -5133,7 +5141,11 @@ function createQueryHelpers(scope) {
|
|
|
5133
5141
|
const withParams = (params) => params !== void 0 ? { params } : {};
|
|
5134
5142
|
return {
|
|
5135
5143
|
query: (groq, params) => workflowEngine.workflow.query({ ...scope, groq, ...withParams(params) }),
|
|
5136
|
-
queryInScope: (
|
|
5144
|
+
queryInScope: ({
|
|
5145
|
+
instanceId,
|
|
5146
|
+
groq,
|
|
5147
|
+
params
|
|
5148
|
+
}) => workflowEngine.workflow.queryInScope({ ...scope, instanceId, groq, ...withParams(params) })
|
|
5137
5149
|
};
|
|
5138
5150
|
}
|
|
5139
5151
|
function createReadHelpers(scope) {
|
|
@@ -5148,17 +5160,17 @@ function createReadHelpers(scope) {
|
|
|
5148
5160
|
getInstance,
|
|
5149
5161
|
currentStage: async (instanceId) => (await getInstance(instanceId)).currentStage,
|
|
5150
5162
|
pendingEffects: async (instanceId) => (await getInstance(instanceId)).pendingEffects,
|
|
5151
|
-
|
|
5163
|
+
activityStatus: async (instanceId, activity) => {
|
|
5152
5164
|
const instance = await getInstance(instanceId);
|
|
5153
5165
|
return instance.stages.find(
|
|
5154
5166
|
(s) => s.name === instance.currentStage && s.exitedAt === void 0
|
|
5155
|
-
)?.
|
|
5167
|
+
)?.activities.find((t) => t.name === activity)?.status;
|
|
5156
5168
|
},
|
|
5157
5169
|
effectsContextMap: async (instanceId) => workflowEngine.effectsContextMap(await getInstance(instanceId)),
|
|
5158
|
-
children: async (parentInstanceId,
|
|
5170
|
+
children: async (parentInstanceId, activity) => workflowEngine.workflow.children({
|
|
5159
5171
|
...scope,
|
|
5160
5172
|
instanceId: parentInstanceId,
|
|
5161
|
-
...
|
|
5173
|
+
...activity !== void 0 ? { activity } : {}
|
|
5162
5174
|
}),
|
|
5163
5175
|
instancesForSubject: async (subjectRef, entryName = "subject") => (
|
|
5164
5176
|
// Subjects live on a workflow-scope `doc.ref` field entry. Match
|
|
@@ -5172,21 +5184,26 @@ function createReadHelpers(scope) {
|
|
|
5172
5184
|
{ ref: subjectRef, entry: entryName, tag }
|
|
5173
5185
|
)
|
|
5174
5186
|
),
|
|
5175
|
-
instancesByStage: async (workflowName, stage,
|
|
5176
|
-
const filter =
|
|
5187
|
+
instancesByStage: async ({ workflowName, stage, openOnly }) => {
|
|
5188
|
+
const filter = openOnly ? " && completedAt == null" : "";
|
|
5177
5189
|
return client.fetch(
|
|
5178
5190
|
`*[_type == "${workflowEngine.WORKFLOW_INSTANCE_TYPE}" && definition == $wf && currentStage == $stage && ${workflowEngine.tagScopeFilter()}${filter}]
|
|
5179
5191
|
| order(startedAt asc)`,
|
|
5180
5192
|
{ wf: workflowName, stage, tag }
|
|
5181
5193
|
);
|
|
5182
5194
|
},
|
|
5195
|
+
instancesForDocument: async (document2) => workflowEngine.workflow.instancesForDocument({ ...scope, document: document2 }),
|
|
5183
5196
|
snapshot: () => client.snapshot()
|
|
5184
5197
|
};
|
|
5185
5198
|
}
|
|
5186
5199
|
function createBench(options = {}) {
|
|
5187
5200
|
const tag = options.tag ?? "bench";
|
|
5188
5201
|
workflowEngine.validateTag(tag);
|
|
5189
|
-
const workflowResource = options.workflowResource ?? DEFAULT_WORKFLOW_RESOURCE, client = resolveBenchClient(options), scope = {
|
|
5202
|
+
const workflowResource = options.workflowResource ?? DEFAULT_WORKFLOW_RESOURCE, client = resolveBenchClient(options), scope = {
|
|
5203
|
+
client,
|
|
5204
|
+
tag,
|
|
5205
|
+
workflowResource
|
|
5206
|
+
}, access = resolveBenchAccess(options);
|
|
5190
5207
|
let frozenNow = options.now !== void 0 ? assertIsoInstant(options.now) : void 0;
|
|
5191
5208
|
const clock = () => frozenNow ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
5192
5209
|
return {
|
|
@@ -5204,7 +5221,7 @@ function createBench(options = {}) {
|
|
|
5204
5221
|
throw new Error(`bench.advance: ms must be a finite number, got ${ms2}`);
|
|
5205
5222
|
frozenNow = new Date(Date.parse(frozenNow ?? (/* @__PURE__ */ new Date()).toISOString()) + ms2).toISOString();
|
|
5206
5223
|
},
|
|
5207
|
-
...createEngineWrappers(scope, access, clock),
|
|
5224
|
+
...createEngineWrappers({ scope, access, clock }),
|
|
5208
5225
|
...createReadHelpers(scope),
|
|
5209
5226
|
...createGuardHelpers(client),
|
|
5210
5227
|
...createQueryHelpers(scope)
|