@fedify/testing 2.5.0-dev.2761 → 2.5.0-dev.2811

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/mod.cjs CHANGED
@@ -4821,7 +4821,7 @@ const Now = /*@__PURE__*/ Object.defineProperties({}, {
4821
4821
  }
4822
4822
  })
4823
4823
  });
4824
- const Temporal$3 = /*@__PURE__*/ Object.defineProperties({}, {
4824
+ const Temporal$4 = /*@__PURE__*/ Object.defineProperties({}, {
4825
4825
  ...createStringTagDescriptors("Temporal"),
4826
4826
  ...createPropDescriptors({
4827
4827
  PlainYearMonth,
@@ -4837,11 +4837,11 @@ const Temporal$3 = /*@__PURE__*/ Object.defineProperties({}, {
4837
4837
  });
4838
4838
  //#endregion
4839
4839
  //#region ../../node_modules/.pnpm/temporal-polyfill@1.0.1/node_modules/temporal-polyfill/index.js
4840
- const Temporal$2 = NativeTemporal || Temporal$3;
4840
+ const Temporal$3 = NativeTemporal || Temporal$4;
4841
4841
  NativeTemporal && Date.prototype.toTemporalInstant;
4842
4842
  //#endregion
4843
4843
  //#region src/kv-tester.ts
4844
- const Temporal$1 = Temporal$2;
4844
+ const Temporal$2 = Temporal$3;
4845
4845
  /**
4846
4846
  * Tests a {@link KvStore} implementation with a standard set of tests.
4847
4847
  *
@@ -4907,12 +4907,12 @@ async function testKvStore(getKvStore, onFinally, options = {}) {
4907
4907
  for (const [index, isolatedKey] of isolatedKeys.entries()) (0, node_assert_strict.strictEqual)(await store.get(isolatedKey), index);
4908
4908
  if (options.testTtl ?? true) {
4909
4909
  const expiringKey = key("ttl", "expiring");
4910
- await store.set(expiringKey, "expired", { ttl: Temporal$1.Duration.from({ milliseconds: 50 }) });
4910
+ await store.set(expiringKey, "expired", { ttl: Temporal$2.Duration.from({ milliseconds: 50 }) });
4911
4911
  await (0, es_toolkit.delay)(100);
4912
4912
  (0, node_assert_strict.strictEqual)(await store.get(expiringKey), void 0);
4913
4913
  (0, node_assert_strict.deepStrictEqual)(await collect(store.list(key("ttl"))), []);
4914
4914
  const refreshedKey = key("ttl", "refreshed");
4915
- await store.set(refreshedKey, "temporary", { ttl: Temporal$1.Duration.from({ milliseconds: 50 }) });
4915
+ await store.set(refreshedKey, "temporary", { ttl: Temporal$2.Duration.from({ milliseconds: 50 }) });
4916
4916
  await store.set(refreshedKey, "persistent");
4917
4917
  await (0, es_toolkit.delay)(100);
4918
4918
  (0, node_assert_strict.strictEqual)(await store.get(refreshedKey), "persistent");
@@ -4954,11 +4954,11 @@ async function testKvStore(getKvStore, onFinally, options = {}) {
4954
4954
  (0, node_assert_strict.strictEqual)(await store.get(casKey), void 0);
4955
4955
  if (options.testTtl ?? true) {
4956
4956
  const expiredCasKey = key("cas", "expired");
4957
- await store.set(expiredCasKey, "old", { ttl: Temporal$1.Duration.from({ milliseconds: 50 }) });
4957
+ await store.set(expiredCasKey, "old", { ttl: Temporal$2.Duration.from({ milliseconds: 50 }) });
4958
4958
  await (0, es_toolkit.delay)(100);
4959
4959
  (0, node_assert_strict.strictEqual)(await cas(expiredCasKey, void 0, "replaced"), true);
4960
4960
  const ttlCasKey = key("cas", "ttl");
4961
- (0, node_assert_strict.strictEqual)(await cas(ttlCasKey, void 0, "temporary", { ttl: Temporal$1.Duration.from({ milliseconds: 50 }) }), true);
4961
+ (0, node_assert_strict.strictEqual)(await cas(ttlCasKey, void 0, "temporary", { ttl: Temporal$2.Duration.from({ milliseconds: 50 }) }), true);
4962
4962
  await (0, es_toolkit.delay)(100);
4963
4963
  (0, node_assert_strict.strictEqual)(await store.get(ttlCasKey), void 0);
4964
4964
  }
@@ -4983,7 +4983,7 @@ function filterByPrefix(entries, prefix) {
4983
4983
  }
4984
4984
  //#endregion
4985
4985
  //#region src/mq-tester.ts
4986
- const Temporal = Temporal$2;
4986
+ const Temporal$1 = Temporal$3;
4987
4987
  /**
4988
4988
  * Tests a {@link MessageQueue} implementation with a standard set of tests.
4989
4989
  *
@@ -5042,7 +5042,7 @@ async function testMessageQueue(getMessageQueue, onFinally, options = {}) {
5042
5042
  await waitFor(() => messages.length > 0, 15e3);
5043
5043
  (0, node_assert_strict.deepStrictEqual)(messages, ["Hello, world!"]);
5044
5044
  let started = Date.now();
5045
- await mq1.enqueue("Delayed message", { delay: Temporal.Duration.from({ seconds: 3 }) });
5045
+ await mq1.enqueue("Delayed message", { delay: Temporal$1.Duration.from({ seconds: 3 }) });
5046
5046
  await waitFor(() => messages.length > 1, 15e3);
5047
5047
  (0, node_assert_strict.deepStrictEqual)(messages, ["Hello, world!", "Delayed message"]);
5048
5048
  (0, node_assert_strict.ok)(Date.now() - started >= 3e3, "Delayed message should be delivered after at least 3 seconds");
@@ -5059,7 +5059,7 @@ async function testMessageQueue(getMessageQueue, onFinally, options = {}) {
5059
5059
  while (messages.length > 0) messages.pop();
5060
5060
  started = Date.now();
5061
5061
  const delayedBatchMessages = ["Delayed batch 1", "Delayed batch 2"];
5062
- await mq1.enqueueMany(delayedBatchMessages, { delay: Temporal.Duration.from({ seconds: 2 }) });
5062
+ await mq1.enqueueMany(delayedBatchMessages, { delay: Temporal$1.Duration.from({ seconds: 2 }) });
5063
5063
  await waitFor(() => messages.length >= delayedBatchMessages.length, 15e3);
5064
5064
  (0, node_assert_strict.deepStrictEqual)(new Set(messages), new Set(delayedBatchMessages));
5065
5065
  (0, node_assert_strict.ok)(Date.now() - started >= 2e3, "Delayed batch messages should be delivered after at least 2 seconds");
@@ -5163,12 +5163,756 @@ async function waitFor(predicate, timeoutMs) {
5163
5163
  }
5164
5164
  const getRandomKey = (prefix) => `fedify_test_${prefix}_${crypto.randomUUID()}`;
5165
5165
  //#endregion
5166
+ //#region src/atomic-kv-tester.ts
5167
+ const Temporal = Temporal$3;
5168
+ /**
5169
+ * Tests an {@link AtomicKvStore} using fresh client pairs for independent cases.
5170
+ *
5171
+ * Covers ordered snapshot reads, all-or-nothing writes/deletes/CAS, absent-key
5172
+ * races, single/batch CAS interaction, logical expiry, validation, and optional
5173
+ * backend-limit probes. Baseline successful calls use at most three keys or
5174
+ * entries; choose a backend scope that supports them. Concurrent checks use
5175
+ * bounded repetition; they are
5176
+ * not an exhaustive linearizability proof. Add adapter-specific transaction
5177
+ * rollback, injected-failure, persistence, and supported-isolation tests.
5178
+ * Run `testKvStore()` separately for ordinary KV and prefix-listing behavior.
5179
+ *
5180
+ * All started racing operations settle before pair disposal, even when an
5181
+ * assertion fails. Assertion and disposal errors are retained together in
5182
+ * an {@link AggregateError}. Configure TTL timing for the adapter's documented
5183
+ * expiry precision and expected operation latency.
5184
+ *
5185
+ * @example
5186
+ * ```typescript ignore
5187
+ * import { test } from "node:test";
5188
+ * import { MemoryKvStore } from "@fedify/fedify";
5189
+ * import { testAtomicKvStore } from "@fedify/testing";
5190
+ *
5191
+ * test("Memory atomic contract", () => testAtomicKvStore(() => {
5192
+ * const store = new MemoryKvStore();
5193
+ * return { first: store, second: store, dispose() {} };
5194
+ * }, { objectKeyOrder: "ignored" }));
5195
+ * ```
5196
+ * @param createPair Creates a fresh pair sharing isolated backing state.
5197
+ * @param options Backend scope, comparison, expiry, and rejection probes.
5198
+ * @since 2.5.0
5199
+ */
5200
+ async function testAtomicKvStore(createPair, options = {}) {
5201
+ const short = options.ttl?.short ?? Temporal.Duration.from({ milliseconds: 500 });
5202
+ const wait = options.ttl?.wait ?? Temporal.Duration.from({ milliseconds: 1e3 });
5203
+ const shortMs = short.total("millisecond");
5204
+ const waitMs = wait.total("millisecond");
5205
+ if (!Number.isFinite(shortMs) || shortMs <= 0 || !Number.isFinite(waitMs) || waitMs <= shortMs || waitMs > 2147483647) throw new RangeError("TTL timing requires 0 < short < wait <= 2147483647 ms");
5206
+ const cases = [
5207
+ async ({ first, second }, key) => {
5208
+ const values = [
5209
+ "text",
5210
+ 42,
5211
+ true,
5212
+ null,
5213
+ [
5214
+ 1,
5215
+ "two",
5216
+ false
5217
+ ],
5218
+ { nested: {
5219
+ items: [{ value: "test" }],
5220
+ active: true
5221
+ } }
5222
+ ];
5223
+ const keys = values.map((_, index) => key("values", String(index)));
5224
+ for (const [index, value] of values.entries()) {
5225
+ await first.set(keys[index], value);
5226
+ (0, node_assert_strict.deepStrictEqual)(await second.getMany([keys[index]]), [value]);
5227
+ }
5228
+ (0, node_assert_strict.deepStrictEqual)(await second.getMany([
5229
+ keys[5],
5230
+ keys[3],
5231
+ copyKey(keys[5])
5232
+ ]), [
5233
+ values[5],
5234
+ null,
5235
+ values[5]
5236
+ ], "snapshot reads must preserve order and duplicates");
5237
+ (0, node_assert_strict.deepStrictEqual)(await second.getMany([key("missing"), keys[3]]), [void 0, null], "snapshot reads must preserve null and absence");
5238
+ const nested = {
5239
+ children: [{ label: "one" }],
5240
+ enabled: false
5241
+ };
5242
+ const nestedKey = key("nested-roundtrip");
5243
+ await first.set(nestedKey, nested);
5244
+ const read = await second.getMany([nestedKey]);
5245
+ (0, node_assert_strict.deepStrictEqual)(read, [nested]);
5246
+ (0, node_assert_strict.strictEqual)(await first.cas(nestedKey, read[0], { replaced: true }), true, "single-key CAS must accept a supported value returned by getMany");
5247
+ (0, node_assert_strict.deepStrictEqual)(await second.get(nestedKey), { replaced: true });
5248
+ await first.set(nestedKey, nested);
5249
+ (0, node_assert_strict.strictEqual)(await first.casMany([{
5250
+ key: nestedKey,
5251
+ expectedValue: read[0],
5252
+ newValue: { replaced: true }
5253
+ }]), true, "CAS must accept a supported value returned by getMany");
5254
+ (0, node_assert_strict.deepStrictEqual)(await second.get(nestedKey), { replaced: true });
5255
+ },
5256
+ async ({ first, second }, key) => {
5257
+ const valueKey = key("empty");
5258
+ await first.set(valueKey, "kept");
5259
+ (0, node_assert_strict.deepStrictEqual)(await second.getMany([]), []);
5260
+ (0, node_assert_strict.strictEqual)(await first.setMany([]), void 0);
5261
+ (0, node_assert_strict.strictEqual)(await first.deleteMany([]), void 0);
5262
+ (0, node_assert_strict.strictEqual)(await first.casMany([]), true);
5263
+ (0, node_assert_strict.strictEqual)(await second.get(valueKey), "kept");
5264
+ },
5265
+ async ({ first, second }, key) => {
5266
+ const a = key("duplicate", "a");
5267
+ const b = key("duplicate", "b");
5268
+ await first.setMany([{
5269
+ key: a,
5270
+ value: 1
5271
+ }, {
5272
+ key: b,
5273
+ value: 2
5274
+ }]);
5275
+ const check = async () => {
5276
+ (0, node_assert_strict.deepStrictEqual)(await readSeparately(second, [a, b]), [1, 2]);
5277
+ };
5278
+ await (0, node_assert_strict.rejects)(() => first.setMany([
5279
+ {
5280
+ key: a,
5281
+ value: 10
5282
+ },
5283
+ {
5284
+ key: b,
5285
+ value: 20
5286
+ },
5287
+ {
5288
+ key: copyKey(b),
5289
+ value: 30
5290
+ }
5291
+ ]));
5292
+ await check();
5293
+ await (0, node_assert_strict.rejects)(() => first.deleteMany([
5294
+ a,
5295
+ b,
5296
+ copyKey(b)
5297
+ ]));
5298
+ await check();
5299
+ await (0, node_assert_strict.rejects)(() => first.casMany([
5300
+ {
5301
+ key: a,
5302
+ expectedValue: 1,
5303
+ newValue: 10
5304
+ },
5305
+ {
5306
+ key: b,
5307
+ expectedValue: 2,
5308
+ newValue: 20
5309
+ },
5310
+ {
5311
+ key: copyKey(b),
5312
+ expectedValue: 2,
5313
+ newValue: 30
5314
+ }
5315
+ ]));
5316
+ await check();
5317
+ await (0, node_assert_strict.rejects)(() => first.casMany([
5318
+ {
5319
+ key: a,
5320
+ expectedValue: "wrong",
5321
+ newValue: 10
5322
+ },
5323
+ {
5324
+ key: b,
5325
+ expectedValue: 2,
5326
+ newValue: 20
5327
+ },
5328
+ {
5329
+ key: copyKey(b),
5330
+ expectedValue: 2,
5331
+ newValue: 30
5332
+ }
5333
+ ]), "duplicate CAS keys must reject even on mismatch");
5334
+ await check();
5335
+ },
5336
+ async ({ first, second }, key) => {
5337
+ const a = key("sparse", "a");
5338
+ const b = key("sparse", "b");
5339
+ await first.setMany([{
5340
+ key: a,
5341
+ value: 1
5342
+ }, {
5343
+ key: b,
5344
+ value: 2
5345
+ }]);
5346
+ const keys = new Array(2);
5347
+ keys[0] = a;
5348
+ const writes = new Array(2);
5349
+ writes[0] = {
5350
+ key: a,
5351
+ value: 3
5352
+ };
5353
+ const conditions = new Array(2);
5354
+ conditions[0] = {
5355
+ key: a,
5356
+ expectedValue: "wrong",
5357
+ newValue: 3
5358
+ };
5359
+ for (const [operation, invoke] of [
5360
+ ["getMany", () => first.getMany(keys)],
5361
+ ["setMany", () => first.setMany(writes)],
5362
+ ["deleteMany", () => first.deleteMany(keys)],
5363
+ ["casMany", () => first.casMany(conditions)]
5364
+ ]) {
5365
+ await (0, node_assert_strict.rejects)(invoke, `${operation} must reject sparse input`);
5366
+ (0, node_assert_strict.deepStrictEqual)(await readSeparately(second, [a, b]), [1, 2], `${operation} must reject sparse input without changing state`);
5367
+ }
5368
+ },
5369
+ async ({ first, second }, key) => {
5370
+ const a = key("undefined", "a");
5371
+ const b = key("undefined", "b");
5372
+ await first.setMany([{
5373
+ key: a,
5374
+ value: "before-a"
5375
+ }, {
5376
+ key: b,
5377
+ value: null
5378
+ }]);
5379
+ await (0, node_assert_strict.rejects)(() => first.set(a, void 0));
5380
+ (0, node_assert_strict.deepStrictEqual)(await readSeparately(second, [a, b]), ["before-a", null]);
5381
+ await (0, node_assert_strict.rejects)(() => first.setMany([{
5382
+ key: a,
5383
+ value: "after-a"
5384
+ }, {
5385
+ key: b,
5386
+ value: void 0
5387
+ }]));
5388
+ (0, node_assert_strict.deepStrictEqual)(await readSeparately(second, [a, b]), ["before-a", null]);
5389
+ },
5390
+ async ({ first, second }, key) => {
5391
+ const a = key("cas", "a");
5392
+ const b = key("cas", "b");
5393
+ const c = key("cas", "c");
5394
+ await first.setMany([{
5395
+ key: a,
5396
+ value: 1
5397
+ }, {
5398
+ key: b,
5399
+ value: null
5400
+ }]);
5401
+ (0, node_assert_strict.strictEqual)(await first.casMany([{
5402
+ key: a,
5403
+ expectedValue: 1,
5404
+ newValue: 10
5405
+ }, {
5406
+ key: b,
5407
+ expectedValue: "wrong",
5408
+ newValue: 20
5409
+ }]), false);
5410
+ (0, node_assert_strict.deepStrictEqual)(await readSeparately(second, [
5411
+ a,
5412
+ b,
5413
+ c
5414
+ ]), [
5415
+ 1,
5416
+ null,
5417
+ void 0
5418
+ ]);
5419
+ (0, node_assert_strict.strictEqual)(await first.casMany([
5420
+ {
5421
+ key: a,
5422
+ expectedValue: 1,
5423
+ newValue: 2
5424
+ },
5425
+ {
5426
+ key: b,
5427
+ expectedValue: null,
5428
+ newValue: void 0
5429
+ },
5430
+ {
5431
+ key: c,
5432
+ expectedValue: void 0,
5433
+ newValue: { created: true }
5434
+ }
5435
+ ]), true);
5436
+ (0, node_assert_strict.deepStrictEqual)(await second.getMany([
5437
+ a,
5438
+ b,
5439
+ c
5440
+ ]), [
5441
+ 2,
5442
+ void 0,
5443
+ { created: true }
5444
+ ]);
5445
+ await assertUnlisted(second, [b]);
5446
+ (0, node_assert_strict.strictEqual)(await first.cas(b, void 0, null), true);
5447
+ (0, node_assert_strict.strictEqual)(await second.get(b), null);
5448
+ (0, node_assert_strict.strictEqual)(await first.cas(b, null, void 0, {}), true);
5449
+ (0, node_assert_strict.strictEqual)(await first.cas(b, void 0, void 0, {}), true);
5450
+ (0, node_assert_strict.strictEqual)(await first.casMany([{
5451
+ key: b,
5452
+ expectedValue: void 0,
5453
+ newValue: void 0,
5454
+ options: {}
5455
+ }]), true);
5456
+ await assertUnlisted(second, [b]);
5457
+ await first.deleteMany([
5458
+ a,
5459
+ c,
5460
+ key("missing-delete")
5461
+ ]);
5462
+ (0, node_assert_strict.deepStrictEqual)(await second.getMany([a, c]), [void 0, void 0]);
5463
+ await assertUnlisted(second, [a, c]);
5464
+ },
5465
+ async ({ first, second }, key) => {
5466
+ const a = key("delete-ttl", "a");
5467
+ const b = key("delete-ttl", "b");
5468
+ await first.setMany([{
5469
+ key: a,
5470
+ value: 1
5471
+ }, {
5472
+ key: b,
5473
+ value: 2
5474
+ }]);
5475
+ await (0, node_assert_strict.rejects)(() => first.cas(a, 1, void 0, { ttl: short }));
5476
+ await (0, node_assert_strict.rejects)(() => first.cas(a, "wrong", void 0, { ttl: short }));
5477
+ await (0, node_assert_strict.rejects)(() => first.casMany([{
5478
+ key: a,
5479
+ expectedValue: 1,
5480
+ newValue: 10
5481
+ }, {
5482
+ key: b,
5483
+ expectedValue: 2,
5484
+ newValue: void 0,
5485
+ options: { ttl: short }
5486
+ }]));
5487
+ await (0, node_assert_strict.rejects)(() => first.casMany([{
5488
+ key: a,
5489
+ expectedValue: "wrong",
5490
+ newValue: 10
5491
+ }, {
5492
+ key: b,
5493
+ expectedValue: 2,
5494
+ newValue: void 0,
5495
+ options: { ttl: short }
5496
+ }]));
5497
+ (0, node_assert_strict.deepStrictEqual)(await readSeparately(second, [a, b]), [1, 2]);
5498
+ },
5499
+ async ({ first, second }, key) => {
5500
+ const valueKey = key("comparison");
5501
+ for (const { value, different } of [
5502
+ {
5503
+ value: 1,
5504
+ different: "1"
5505
+ },
5506
+ {
5507
+ value: [1, 2],
5508
+ different: {
5509
+ "0": 1,
5510
+ "1": 2
5511
+ }
5512
+ },
5513
+ {
5514
+ value: [1, 2],
5515
+ different: [2, 1]
5516
+ },
5517
+ {
5518
+ value: null,
5519
+ different: void 0
5520
+ }
5521
+ ]) {
5522
+ await first.set(valueKey, value);
5523
+ (0, node_assert_strict.strictEqual)(await first.cas(valueKey, different, "changed"), false);
5524
+ (0, node_assert_strict.strictEqual)(await first.casMany([{
5525
+ key: valueKey,
5526
+ expectedValue: different,
5527
+ newValue: "changed"
5528
+ }]), false);
5529
+ (0, node_assert_strict.deepStrictEqual)(await second.get(valueKey), value);
5530
+ }
5531
+ const missing = key("comparison", "missing");
5532
+ (0, node_assert_strict.strictEqual)(await first.cas(missing, null, "changed"), false, "single CAS must not match null against absence");
5533
+ (0, node_assert_strict.strictEqual)(await first.casMany([{
5534
+ key: missing,
5535
+ expectedValue: null,
5536
+ newValue: "changed"
5537
+ }]), false, "batch CAS must not match null against absence");
5538
+ (0, node_assert_strict.strictEqual)(await second.get(missing), void 0);
5539
+ const value = {
5540
+ a: 1,
5541
+ b: { c: 2 }
5542
+ };
5543
+ const reordered = {
5544
+ b: { c: 2 },
5545
+ a: 1
5546
+ };
5547
+ await first.set(valueKey, value);
5548
+ const single = await first.cas(valueKey, reordered, "changed");
5549
+ await first.set(valueKey, value);
5550
+ (0, node_assert_strict.strictEqual)(await first.casMany([{
5551
+ key: valueKey,
5552
+ expectedValue: reordered,
5553
+ newValue: "changed"
5554
+ }]), single, "single and batch CAS must use the same object comparison rules");
5555
+ if (options.objectKeyOrder != null) (0, node_assert_strict.strictEqual)(single, options.objectKeyOrder === "ignored");
5556
+ },
5557
+ async ({ first, second }, key) => {
5558
+ for (let round = 0; round < 4; round++) {
5559
+ const a = key("absent-race", String(round), "a");
5560
+ const b = key("absent-race", String(round), "b");
5561
+ const results = await runTogether([() => first.casMany([{
5562
+ key: a,
5563
+ expectedValue: void 0,
5564
+ newValue: 1
5565
+ }, {
5566
+ key: b,
5567
+ expectedValue: void 0,
5568
+ newValue: 1
5569
+ }]), () => second.casMany([{
5570
+ key: b,
5571
+ expectedValue: void 0,
5572
+ newValue: 2
5573
+ }, {
5574
+ key: a,
5575
+ expectedValue: void 0,
5576
+ newValue: 2
5577
+ }])]);
5578
+ (0, node_assert_strict.strictEqual)(results.filter(Boolean).length, 1, "only one absent-key batch may win");
5579
+ const winner = results[0] ? 1 : 2;
5580
+ (0, node_assert_strict.deepStrictEqual)(await first.getMany([a, b]), [winner, winner]);
5581
+ }
5582
+ },
5583
+ async ({ first, second }, key) => {
5584
+ for (let round = 0; round < 4; round++) {
5585
+ const a = key("opposing-race", String(round), "a");
5586
+ const b = key("opposing-race", String(round), "b");
5587
+ await first.setMany([{
5588
+ key: a,
5589
+ value: 0
5590
+ }, {
5591
+ key: b,
5592
+ value: 0
5593
+ }]);
5594
+ const results = await runTogether([() => first.casMany([{
5595
+ key: a,
5596
+ expectedValue: 0,
5597
+ newValue: 1
5598
+ }, {
5599
+ key: b,
5600
+ expectedValue: 0,
5601
+ newValue: 1
5602
+ }]), () => second.casMany([{
5603
+ key: b,
5604
+ expectedValue: 0,
5605
+ newValue: 2
5606
+ }, {
5607
+ key: a,
5608
+ expectedValue: 0,
5609
+ newValue: 2
5610
+ }])]);
5611
+ (0, node_assert_strict.strictEqual)(results.filter(Boolean).length, 1, "only one competing batch may win");
5612
+ const winner = results[0] ? 1 : 2;
5613
+ (0, node_assert_strict.deepStrictEqual)(await first.getMany([a, b]), [winner, winner]);
5614
+ }
5615
+ },
5616
+ async ({ first, second }, key) => {
5617
+ for (let round = 0; round < 4; round++) {
5618
+ const a = key("single-batch-race", String(round), "a");
5619
+ const b = key("single-batch-race", String(round), "b");
5620
+ await first.setMany([{
5621
+ key: a,
5622
+ value: 0
5623
+ }, {
5624
+ key: b,
5625
+ value: 0
5626
+ }]);
5627
+ const results = await runTogether([() => first.cas(a, 0, 1), () => second.casMany([{
5628
+ key: b,
5629
+ expectedValue: 0,
5630
+ newValue: 2
5631
+ }, {
5632
+ key: a,
5633
+ expectedValue: 0,
5634
+ newValue: 2
5635
+ }])]);
5636
+ (0, node_assert_strict.strictEqual)(results.filter(Boolean).length, 1, "single and batch CAS must compete");
5637
+ (0, node_assert_strict.deepStrictEqual)(await first.getMany([a, b]), results[0] ? [1, 0] : [2, 2]);
5638
+ }
5639
+ },
5640
+ snapshotCase("setMany"),
5641
+ snapshotCase("casMany"),
5642
+ snapshotCase("deleteMany"),
5643
+ async ({ first, second }, key) => {
5644
+ const a = key("expiry", "read");
5645
+ const b = key("expiry", "persistent");
5646
+ const c = key("expiry", "conditional");
5647
+ const d = key("expiry", "single");
5648
+ await first.set(b, "persistent");
5649
+ for (const expiring of [
5650
+ a,
5651
+ c,
5652
+ d
5653
+ ]) await first.set(expiring, "temporary", { ttl: short });
5654
+ (0, node_assert_strict.deepStrictEqual)(await second.getMany([a, b]), ["temporary", "persistent"]);
5655
+ await sleep(waitMs);
5656
+ (0, node_assert_strict.strictEqual)(await first.casMany([{
5657
+ key: c,
5658
+ expectedValue: void 0,
5659
+ newValue: "recreated"
5660
+ }, {
5661
+ key: b,
5662
+ expectedValue: "persistent",
5663
+ newValue: "updated"
5664
+ }]), true, "expired keys must compare as absent");
5665
+ (0, node_assert_strict.strictEqual)(await first.cas(d, void 0, "recreated"), true);
5666
+ (0, node_assert_strict.deepStrictEqual)(await second.getMany([a, b]), [void 0, "updated"]);
5667
+ (0, node_assert_strict.deepStrictEqual)(await second.getMany([c, d]), ["recreated", "recreated"]);
5668
+ await assertUnlisted(second, [a]);
5669
+ },
5670
+ async ({ first, second }, key) => {
5671
+ const a = key("ttl-update", "set-clear");
5672
+ const b = key("ttl-update", "set-expire");
5673
+ const c = key("ttl-update", "cas-clear");
5674
+ const d = key("ttl-update", "cas-expire");
5675
+ const e = key("ttl-update", "failed");
5676
+ const f = key("ttl-update", "mismatch");
5677
+ const g = key("ttl-update", "failed-clear");
5678
+ const h = key("ttl-update", "single-clear");
5679
+ for (const persistent of [b, d]) await first.set(persistent, "persistent");
5680
+ await first.set(f, "unchanged");
5681
+ for (const expiring of [
5682
+ a,
5683
+ c,
5684
+ e,
5685
+ g,
5686
+ h
5687
+ ]) await first.set(expiring, "temporary", { ttl: short });
5688
+ await first.setMany([{
5689
+ key: a,
5690
+ value: "persistent"
5691
+ }, {
5692
+ key: b,
5693
+ value: "temporary",
5694
+ options: { ttl: short }
5695
+ }]);
5696
+ (0, node_assert_strict.strictEqual)(await first.cas(h, "temporary", "persistent"), true);
5697
+ (0, node_assert_strict.strictEqual)(await first.casMany([{
5698
+ key: c,
5699
+ expectedValue: "temporary",
5700
+ newValue: "persistent"
5701
+ }, {
5702
+ key: d,
5703
+ expectedValue: "persistent",
5704
+ newValue: "temporary",
5705
+ options: { ttl: short }
5706
+ }]), true);
5707
+ (0, node_assert_strict.strictEqual)(await first.casMany([
5708
+ {
5709
+ key: e,
5710
+ expectedValue: "temporary",
5711
+ newValue: "incorrectly-renewed",
5712
+ options: { ttl: wait.add(short) }
5713
+ },
5714
+ {
5715
+ key: g,
5716
+ expectedValue: "temporary",
5717
+ newValue: "incorrectly-persistent"
5718
+ },
5719
+ {
5720
+ key: f,
5721
+ expectedValue: "wrong",
5722
+ newValue: "changed"
5723
+ }
5724
+ ]), false);
5725
+ (0, node_assert_strict.deepStrictEqual)(await readSeparately(second, [e, g]), ["temporary", "temporary"]);
5726
+ await sleep(waitMs);
5727
+ (0, node_assert_strict.deepStrictEqual)(await readSeparately(second, [
5728
+ a,
5729
+ b,
5730
+ c,
5731
+ d,
5732
+ e,
5733
+ f,
5734
+ g,
5735
+ h
5736
+ ]), [
5737
+ "persistent",
5738
+ void 0,
5739
+ "persistent",
5740
+ void 0,
5741
+ void 0,
5742
+ "unchanged",
5743
+ void 0,
5744
+ "persistent"
5745
+ ], "replacement must clear or replace TTLs, while failed CAS preserves TTLs");
5746
+ }
5747
+ ];
5748
+ if (options.rejections != null) {
5749
+ const probes = options.rejections;
5750
+ for (const operation of [
5751
+ "getMany",
5752
+ "setMany",
5753
+ "deleteMany",
5754
+ "casMany"
5755
+ ]) cases.splice(cases.length - 2, 0, async ({ first, second }, key) => {
5756
+ const probe = probes(key)[operation];
5757
+ if (probe == null) return;
5758
+ if (probe.length === 0) throw new TypeError("Rejection probes must attempt non-empty operations");
5759
+ let keys;
5760
+ let invoke;
5761
+ if (operation === "getMany" || operation === "deleteMany") {
5762
+ keys = probe;
5763
+ if (operation === "deleteMany") assertDistinctKeys(keys);
5764
+ for (const [index, probeKey] of keys.entries()) await first.set(probeKey, {
5765
+ before: index,
5766
+ nonce: crypto.randomUUID()
5767
+ });
5768
+ invoke = operation === "getMany" ? () => first.getMany(keys) : () => first.deleteMany(keys);
5769
+ } else if (operation === "setMany") {
5770
+ const entries = probe;
5771
+ keys = entries.map((entry) => entry.key);
5772
+ assertDistinctKeys(keys);
5773
+ for (const [index, entry] of entries.entries()) {
5774
+ if (entry.value === void 0) throw new TypeError("setMany limit probes must supply storable values");
5775
+ const before = {
5776
+ before: index,
5777
+ nonce: crypto.randomUUID()
5778
+ };
5779
+ (0, node_assert_strict.notDeepStrictEqual)(entry.value, before);
5780
+ await first.set(entry.key, before);
5781
+ }
5782
+ invoke = () => first.setMany(entries);
5783
+ } else {
5784
+ const entries = probe;
5785
+ keys = entries.map((entry) => entry.key);
5786
+ assertDistinctKeys(keys);
5787
+ for (const entry of entries) {
5788
+ (0, node_assert_strict.notDeepStrictEqual)(entry.newValue, entry.expectedValue, "every CAS limit probe entry must attempt an observable change");
5789
+ if (entry.expectedValue === void 0) await first.delete(entry.key);
5790
+ else await first.set(entry.key, entry.expectedValue);
5791
+ }
5792
+ invoke = () => first.casMany(entries);
5793
+ }
5794
+ const before = await readSeparately(second, keys);
5795
+ await (0, node_assert_strict.rejects)(invoke, `${operation} must reject unsupported scope or limits`);
5796
+ (0, node_assert_strict.deepStrictEqual)(await readSeparately(second, keys), before, `${operation} rejection must leave every key unchanged`);
5797
+ });
5798
+ }
5799
+ for (const run of cases) {
5800
+ const prefix = `fedify_atomic_${crypto.randomUUID()}`;
5801
+ const pair = await createPair();
5802
+ const key = (...segments) => {
5803
+ const raw = [prefix, ...segments];
5804
+ return options.key == null ? raw : options.key(raw);
5805
+ };
5806
+ let failed = false;
5807
+ let failure;
5808
+ let disposalFailed = false;
5809
+ let disposalFailure;
5810
+ try {
5811
+ await run(pair, key);
5812
+ } catch (error) {
5813
+ failed = true;
5814
+ failure = error;
5815
+ } finally {
5816
+ try {
5817
+ await pair.dispose();
5818
+ } catch (error) {
5819
+ disposalFailed = true;
5820
+ disposalFailure = error;
5821
+ }
5822
+ }
5823
+ if (failed && disposalFailed) throw new AggregateError([failure, disposalFailure], "Atomic KV assertions and disposal failed");
5824
+ if (failed) throw failure;
5825
+ if (disposalFailed) throw disposalFailure;
5826
+ }
5827
+ }
5828
+ function copyKey(key) {
5829
+ return [key[0], ...key.slice(1)];
5830
+ }
5831
+ function assertDistinctKeys(keys) {
5832
+ (0, node_assert_strict.strictEqual)(new Set(keys.map((key) => JSON.stringify(key))).size, keys.length, "limit/scope probes must contain distinct structural keys");
5833
+ }
5834
+ async function readSeparately(store, keys) {
5835
+ const values = [];
5836
+ for (const key of keys) values.push(await store.get(key));
5837
+ return values;
5838
+ }
5839
+ async function assertUnlisted(store, keys) {
5840
+ const encoded = new Set(keys.map((key) => JSON.stringify(key)));
5841
+ for await (const entry of store.list()) (0, node_assert_strict.ok)(!encoded.has(JSON.stringify(entry.key)), "deleted or expired keys must not appear in list()");
5842
+ }
5843
+ async function runTogether(operations) {
5844
+ const results = await Promise.allSettled(operations.map((operation) => Promise.resolve().then(operation)));
5845
+ const failures = results.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
5846
+ if (failures.length === 1) throw failures[0];
5847
+ if (failures.length > 1) throw new AggregateError(failures, "Concurrent atomic KV operations failed");
5848
+ return results.flatMap((result) => result.status === "fulfilled" ? [result.value] : []);
5849
+ }
5850
+ function snapshotCase(operation) {
5851
+ return async ({ first, second }, key) => {
5852
+ for (const readerFirst of [true, false]) for (let round = 0; round < 2; round++) {
5853
+ const a = key("snapshot", operation, String(readerFirst), String(round), "a");
5854
+ const b = key("snapshot", operation, String(readerFirst), String(round), "b");
5855
+ await first.setMany([{
5856
+ key: a,
5857
+ value: 0
5858
+ }, {
5859
+ key: b,
5860
+ value: 0
5861
+ }]);
5862
+ const write = async () => {
5863
+ for (let version = 1; version <= 8; version++) if (operation === "casMany") (0, node_assert_strict.strictEqual)(await first.casMany([{
5864
+ key: a,
5865
+ expectedValue: version - 1,
5866
+ newValue: version
5867
+ }, {
5868
+ key: b,
5869
+ expectedValue: version - 1,
5870
+ newValue: version
5871
+ }]), true);
5872
+ else {
5873
+ await first.setMany([{
5874
+ key: a,
5875
+ value: version
5876
+ }, {
5877
+ key: b,
5878
+ value: version
5879
+ }]);
5880
+ if (operation === "deleteMany") await first.deleteMany([a, b]);
5881
+ }
5882
+ };
5883
+ const read = async () => {
5884
+ for (let observation = 0; observation < 16; observation++) {
5885
+ const values = await second.getMany([
5886
+ a,
5887
+ b,
5888
+ copyKey(a)
5889
+ ]);
5890
+ (0, node_assert_strict.strictEqual)(values.length, 3);
5891
+ if (operation !== "deleteMany") (0, node_assert_strict.strictEqual)(typeof values[0], "number");
5892
+ (0, node_assert_strict.strictEqual)(values[0], values[1], `${operation} snapshot must not tear`);
5893
+ (0, node_assert_strict.strictEqual)(values[0], values[2], "duplicate snapshot keys must agree");
5894
+ }
5895
+ };
5896
+ await runTogether(readerFirst ? [read, write] : [write, read]);
5897
+ }
5898
+ };
5899
+ }
5900
+ async function sleep(milliseconds) {
5901
+ const started = performance.now();
5902
+ let remaining = milliseconds;
5903
+ while (remaining > 0) {
5904
+ await new Promise((resolve) => setTimeout(resolve, Math.min(2147483647, Math.ceil(remaining))));
5905
+ remaining = milliseconds - (performance.now() - started);
5906
+ }
5907
+ }
5908
+ //#endregion
5166
5909
  exports.createContext = createContext;
5167
5910
  exports.createFederation = createFederation;
5168
5911
  exports.createInboxContext = createInboxContext;
5169
5912
  exports.createOutboxContext = createOutboxContext;
5170
5913
  exports.createRequestContext = createRequestContext;
5171
5914
  exports.getRandomKey = getRandomKey;
5915
+ exports.testAtomicKvStore = testAtomicKvStore;
5172
5916
  exports.testKvStore = testKvStore;
5173
5917
  exports.testMessageQueue = testMessageQueue;
5174
5918
  exports.waitFor = waitFor;