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