@sanity-labs/client-fake-for-test 0.4.0 → 0.5.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/dist/index.cjs CHANGED
@@ -861,7 +861,13 @@ const RECORDED_METHODS = /* @__PURE__ */ new Set([
861
861
  return { projectId, dataset };
862
862
  };
863
863
  function createTestClient(options = {}) {
864
- const defaultProjectId = options.projectId ?? DEFAULT_PROJECT_ID, defaultDataset = options.dataset ?? DEFAULT_DATASET, backend = { registry: /* @__PURE__ */ new Map(), log: [], stubs: /* @__PURE__ */ new Map(), calls: [] }, registry = backend.registry, ensureStore = (projectId, dataset) => {
864
+ const defaultProjectId = options.projectId ?? DEFAULT_PROJECT_ID, defaultDataset = options.dataset ?? DEFAULT_DATASET, backend = {
865
+ registry: /* @__PURE__ */ new Map(),
866
+ log: [],
867
+ stubs: /* @__PURE__ */ new Map(),
868
+ calls: [],
869
+ concurrentEdits: /* @__PURE__ */ new Map()
870
+ }, registry = backend.registry, ensureStore = (projectId, dataset) => {
865
871
  const key = registryKey(projectId, dataset);
866
872
  let store = registry.get(key);
867
873
  return store === void 0 && (store = createTestStore(), registry.set(key, store)), store;
@@ -935,7 +941,7 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
935
941
  const key = registryKey(projectId, dataset);
936
942
  let s = registry.get(key);
937
943
  return s === void 0 && (s = createTestStore(), registry.set(key, s)), s;
938
- })(), record = (transactionId, mutations) => {
944
+ })(), concurrentEditKey = (id) => registryKey(projectId, dataset) + " " + id, record = (transactionId, mutations) => {
939
945
  backend.log.push({ transactionId, projectId, dataset, mutations });
940
946
  }, writeResult = (documents, results, transactionId, opts) => formatMutationResult(
941
947
  documents,
@@ -963,6 +969,11 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
963
969
  };
964
970
  }, checkRevision = (id, ifRevisionId) => {
965
971
  if (ifRevisionId === void 0) return;
972
+ const editKey = concurrentEditKey(id), pendingEdits = backend.concurrentEdits.get(editKey) ?? 0;
973
+ if (pendingEdits > 0)
974
+ throw backend.concurrentEdits.set(editKey, pendingEdits - 1), fakeError(
975
+ `ifRevisionId check failed: document "${id}" was concurrently edited (simulated via simulateConcurrentEdit) \u2014 the revision-guarded mutation lost the race; re-read and retry. (This mirrors Sanity's optimistic-locking 409.)`
976
+ );
966
977
  const existing = store.get(id);
967
978
  if (existing === void 0)
968
979
  throw fakeError(
@@ -1232,6 +1243,10 @@ function makeHandle(backend, projectId, dataset, accessControl, perspective, mut
1232
1243
  reset: () => store.reset(),
1233
1244
  snapshot: () => store.snapshot(),
1234
1245
  store: () => store,
1246
+ simulateConcurrentEdit: (id, count = 1) => {
1247
+ const editKey = concurrentEditKey(id);
1248
+ backend.concurrentEdits.set(editKey, (backend.concurrentEdits.get(editKey) ?? 0) + count);
1249
+ },
1235
1250
  datasets: () => [...registry.keys()].map(parseRegistryKey),
1236
1251
  stub: (name, handler) => {
1237
1252
  backend.stubs.set(name, handler);