@sanity/workflow-engine-test 0.7.0 → 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 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(id, patch)` writes through the
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(access, args, canTokenResolve) {
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(scope, access, clock) {
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, editOptions) => {
5125
- const action = editOptions?.action ?? "update", as = editOptions?.as, writer = as ? client.withConfig({
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: (instanceId, groq, params) => workflowEngine.workflow.queryInScope({ ...scope, instanceId, groq, ...withParams(params) })
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) {
@@ -5172,8 +5184,8 @@ function createReadHelpers(scope) {
5172
5184
  { ref: subjectRef, entry: entryName, tag }
5173
5185
  )
5174
5186
  ),
5175
- instancesByStage: async (workflowName, stage, queryOptions) => {
5176
- const filter = queryOptions?.openOnly ? " && completedAt == null" : "";
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)`,
@@ -5187,7 +5199,11 @@ function createReadHelpers(scope) {
5187
5199
  function createBench(options = {}) {
5188
5200
  const tag = options.tag ?? "bench";
5189
5201
  workflowEngine.validateTag(tag);
5190
- const workflowResource = options.workflowResource ?? DEFAULT_WORKFLOW_RESOURCE, client = resolveBenchClient(options), scope = { client, tag, workflowResource }, access = resolveBenchAccess(options);
5202
+ const workflowResource = options.workflowResource ?? DEFAULT_WORKFLOW_RESOURCE, client = resolveBenchClient(options), scope = {
5203
+ client,
5204
+ tag,
5205
+ workflowResource
5206
+ }, access = resolveBenchAccess(options);
5191
5207
  let frozenNow = options.now !== void 0 ? assertIsoInstant(options.now) : void 0;
5192
5208
  const clock = () => frozenNow ?? (/* @__PURE__ */ new Date()).toISOString();
5193
5209
  return {
@@ -5205,7 +5221,7 @@ function createBench(options = {}) {
5205
5221
  throw new Error(`bench.advance: ms must be a finite number, got ${ms2}`);
5206
5222
  frozenNow = new Date(Date.parse(frozenNow ?? (/* @__PURE__ */ new Date()).toISOString()) + ms2).toISOString();
5207
5223
  },
5208
- ...createEngineWrappers(scope, access, clock),
5224
+ ...createEngineWrappers({ scope, access, clock }),
5209
5225
  ...createReadHelpers(scope),
5210
5226
  ...createGuardHelpers(client),
5211
5227
  ...createQueryHelpers(scope)