@sanity-labs/client-fake-for-test 0.7.0 → 0.7.1

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/dist/index.cjs CHANGED
@@ -875,6 +875,15 @@ const RECORDED_METHODS = /* @__PURE__ */ new Set([
875
875
  "unpublishVersion",
876
876
  "request"
877
877
  ]), DEFAULT_PROJECT_ID = "test", DEFAULT_DATASET = "test";
878
+ function serializeWrite(backend, run) {
879
+ const result = backend.writeChain.then(run);
880
+ return backend.writeChain = result.then(
881
+ () => {
882
+ },
883
+ () => {
884
+ }
885
+ ), result;
886
+ }
878
887
  function requireIdentity(backend, token, site) {
879
888
  if (backend.identities.has(token)) return;
880
889
  const known = [...backend.identities.keys()];
@@ -899,7 +908,8 @@ function createTestClient(options = {}) {
899
908
  stubs: /* @__PURE__ */ new Map(),
900
909
  calls: [],
901
910
  concurrentEdits: /* @__PURE__ */ new Map(),
902
- identities: new Map(Object.entries(options.identities ?? {}))
911
+ identities: new Map(Object.entries(options.identities ?? {})),
912
+ writeChain: Promise.resolve()
903
913
  }, registry = backend.registry;
904
914
  rejectAmbiguousIdentity("createTestClient()", options), options.token !== void 0 && requireIdentity(backend, options.token, "createTestClient()");
905
915
  const ensureStore = (projectId, dataset) => {
@@ -1128,7 +1138,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1128
1138
  txnOptions?.returnDocuments ?? !1,
1129
1139
  txnOptions?.returnFirst ?? !1
1130
1140
  );
1131
- }, accessVisibleDocs = async () => {
1141
+ }, runAction = (actions) => serializeWrite(backend, async () => applyActions(store, actions)), accessVisibleDocs = async () => {
1132
1142
  const all = store.all();
1133
1143
  if (activeAccess() === void 0) return all;
1134
1144
  const out = [];
@@ -1172,7 +1182,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1172
1182
  const view = await visibleDocs(effective);
1173
1183
  return ids.map((id) => resolveDocument(view, id, effective) ?? null);
1174
1184
  },
1175
- create: async (doc, opts) => {
1185
+ create: (doc, opts) => serializeWrite(backend, async () => {
1176
1186
  if (checkRevision(doc._id, opts?.ifRevisionId), store.get(doc._id) !== void 0)
1177
1187
  throw fakeError(
1178
1188
  `create() failed: a document with _id "${doc._id}" already exists in dataset "${projectId}:${dataset}". Use createOrReplace() to overwrite it, or createIfNotExists() to leave the existing one untouched.`
@@ -1186,8 +1196,8 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1186
1196
  transactionId,
1187
1197
  opts
1188
1198
  );
1189
- },
1190
- createOrReplace: async (doc, opts) => {
1199
+ }),
1200
+ createOrReplace: (doc, opts) => serializeWrite(backend, async () => {
1191
1201
  checkRevision(doc._id, opts?.ifRevisionId);
1192
1202
  const promoted = promote2(doc), existing = store.get(doc._id);
1193
1203
  await gate(existing ?? promoted, existing !== void 0 ? "update" : "create"), await guardGate(existing !== void 0 ? "update" : "create", existing ?? null, promoted);
@@ -1200,8 +1210,8 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1200
1210
  transactionId,
1201
1211
  opts
1202
1212
  );
1203
- },
1204
- createIfNotExists: async (doc, opts) => {
1213
+ }),
1214
+ createIfNotExists: (doc, opts) => serializeWrite(backend, async () => {
1205
1215
  checkRevision(doc._id, opts?.ifRevisionId);
1206
1216
  const existing = store.get(doc._id), transactionId = opts?.transactionId ?? cryptoRandomKey();
1207
1217
  if (existing !== void 0)
@@ -1218,8 +1228,8 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1218
1228
  transactionId,
1219
1229
  opts
1220
1230
  );
1221
- },
1222
- delete: async (id, opts) => {
1231
+ }),
1232
+ delete: (id, opts) => serializeWrite(backend, async () => {
1223
1233
  checkRevision(id, opts?.ifRevisionId);
1224
1234
  const existing = store.get(id);
1225
1235
  existing !== void 0 && await gate(existing, "update"), existing !== void 0 && await guardGate("delete", existing, null);
@@ -1232,23 +1242,35 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1232
1242
  opts.returnDocuments ?? !0,
1233
1243
  opts.returnFirst ?? !0
1234
1244
  );
1235
- },
1245
+ }),
1236
1246
  patch: (documentId) => createPatch(documentId, {
1237
- apply: async (docId, ops, ifRev, options) => {
1247
+ apply: (docId, ops, ifRev, options) => serializeWrite(backend, async () => {
1238
1248
  const doc = await applyPatch(docId, ops, ifRev);
1239
1249
  return record(
1240
1250
  cryptoRandomKey(),
1241
- [{ kind: "patch", documentId: docId, ops, ...ifRev !== void 0 ? { ifRev } : {} }],
1251
+ [
1252
+ {
1253
+ kind: "patch",
1254
+ documentId: docId,
1255
+ ops,
1256
+ ...ifRev !== void 0 ? { ifRev } : {}
1257
+ }
1258
+ ],
1242
1259
  options?.tag
1243
1260
  ), doc;
1244
- }
1261
+ })
1262
+ }),
1263
+ transaction: () => createTransaction({
1264
+ apply: (mutations, txnOptions) => serializeWrite(backend, () => applyTransaction(mutations, txnOptions))
1245
1265
  }),
1246
- transaction: () => createTransaction({ apply: applyTransaction }),
1247
- mutate: async (operations, options) => applyTransaction(toTxnMutations(operations), options),
1248
- action: async (action) => applyActions(store, action),
1266
+ mutate: (operations, options) => serializeWrite(backend, () => applyTransaction(toTxnMutations(operations), options)),
1267
+ // Synchronous writes still take the lock: an action landing while a
1268
+ // guarded patch is parked in its gate would otherwise be clobbered
1269
+ // when that patch resumes over its pre-action snapshot.
1270
+ action: (action) => runAction(action),
1249
1271
  releases: {
1250
1272
  create: async ({ releaseId, metadata }) => {
1251
- const result = applyActions(store, {
1273
+ const result = await runAction({
1252
1274
  actionType: "sanity.action.release.create",
1253
1275
  releaseId,
1254
1276
  ...metadata !== void 0 ? { metadata } : {}
@@ -1259,30 +1281,30 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1259
1281
  metadata: doc?.metadata ?? { releaseType: "undecided" }
1260
1282
  };
1261
1283
  },
1262
- edit: async ({ releaseId, patch }) => applyActions(store, { actionType: "sanity.action.release.edit", releaseId, patch }),
1284
+ edit: ({ releaseId, patch }) => runAction({ actionType: "sanity.action.release.edit", releaseId, patch }),
1263
1285
  get: async ({ releaseId }) => store.get(`_.releases.${releaseId}`),
1264
1286
  fetchDocuments: async ({ releaseId }) => {
1265
1287
  const prefix = `versions.${releaseId}.`;
1266
1288
  return store.all().filter((d) => d._id.startsWith(prefix));
1267
1289
  },
1268
- schedule: async ({ releaseId, publishAt }) => applyActions(store, {
1290
+ schedule: ({ releaseId, publishAt }) => runAction({
1269
1291
  actionType: "sanity.action.release.schedule",
1270
1292
  releaseId,
1271
1293
  publishAt
1272
1294
  }),
1273
- unschedule: async ({ releaseId }) => applyActions(store, { actionType: "sanity.action.release.unschedule", releaseId }),
1274
- publish: async ({ releaseId }) => applyActions(store, { actionType: "sanity.action.release.publish", releaseId }),
1275
- archive: async ({ releaseId }) => applyActions(store, { actionType: "sanity.action.release.archive", releaseId }),
1276
- unarchive: async ({ releaseId }) => applyActions(store, { actionType: "sanity.action.release.unarchive", releaseId }),
1277
- delete: async ({ releaseId }) => applyActions(store, { actionType: "sanity.action.release.delete", releaseId })
1295
+ unschedule: ({ releaseId }) => runAction({ actionType: "sanity.action.release.unschedule", releaseId }),
1296
+ publish: ({ releaseId }) => runAction({ actionType: "sanity.action.release.publish", releaseId }),
1297
+ archive: ({ releaseId }) => runAction({ actionType: "sanity.action.release.archive", releaseId }),
1298
+ unarchive: ({ releaseId }) => runAction({ actionType: "sanity.action.release.unarchive", releaseId }),
1299
+ delete: ({ releaseId }) => runAction({ actionType: "sanity.action.release.delete", releaseId })
1278
1300
  },
1279
- createVersion: async ({ releaseId, publishedId, document }) => applyActions(store, {
1301
+ createVersion: ({ releaseId, publishedId, document }) => runAction({
1280
1302
  actionType: "sanity.action.document.version.create",
1281
1303
  releaseId,
1282
1304
  publishedId,
1283
1305
  document
1284
1306
  }),
1285
- unpublishVersion: async ({ releaseId, publishedId }) => applyActions(store, {
1307
+ unpublishVersion: ({ releaseId, publishedId }) => runAction({
1286
1308
  actionType: "sanity.action.document.version.unpublish",
1287
1309
  versionId: `versions.${releaseId}.${publishedId}`,
1288
1310
  publishedId