@saasicat/persistence-testing 1.0.0-rc.6 → 1.0.0-rc.7

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
@@ -224,11 +224,10 @@ function persistenceAdapterContract(options) {
224
224
  return;
225
225
  }
226
226
  const plan = await repository.create({
227
- projectKey: options.projectKey,
228
227
  planKey: "STANDARD",
229
228
  label: "Standard"
230
229
  });
231
- assert.equal(plan.projectKey, options.projectKey);
230
+ assert.equal(plan.planKey, "STANDARD");
232
231
  const firstDraft = await repository.createPlanVersionDraft({
233
232
  planId: "STANDARD",
234
233
  features: ["CORE"],
@@ -289,11 +288,10 @@ function persistenceAdapterContract(options) {
289
288
  return;
290
289
  }
291
290
  const bundle = await repository.create({
292
- projectKey: options.projectKey,
293
291
  bundleKey: "REPORTING",
294
292
  label: "Reporting"
295
293
  });
296
- assert.equal(bundle.projectKey, options.projectKey);
294
+ assert.equal(bundle.bundleKey, "REPORTING");
297
295
  const firstDraft = await repository.createDraft({
298
296
  bundleId: bundle.id,
299
297
  features: ["REPORTS"],
@@ -341,6 +339,375 @@ function persistenceAdapterContract(options) {
341
339
  second.id
342
340
  );
343
341
  });
342
+ test("a booking keeps the rhythm and the window it was made in", async (t) => {
343
+ const repository = harness.adapter.subscriptionBundleRepository;
344
+ const { seed } = harness;
345
+ if (!repository || !seed.createBundleVersion) {
346
+ t.skip("adapter provides no SubscriptionBundleRepository or bundle catalog");
347
+ return;
348
+ }
349
+ const { planVersionId } = await seed.createPlanVersion({
350
+ planKey: "PRO",
351
+ version: 1,
352
+ quotas: {},
353
+ features: ["CORE"],
354
+ published: true
355
+ });
356
+ const { subscriptionId } = await seed.createSubscription({
357
+ tenantId: "tenant-bundle-period",
358
+ plan: "PRO",
359
+ planVersionId,
360
+ billingCycle: "YEARLY"
361
+ });
362
+ const { bundleVersionId } = await seed.createBundleVersion({
363
+ bundleKey: "ANALYTICS",
364
+ features: ["REPORTS"]
365
+ });
366
+ const booked = await repository.add({
367
+ subscriptionId,
368
+ bundleVersionId,
369
+ startedAt: /* @__PURE__ */ new Date("2026-02-21T00:00:00.000Z"),
370
+ minimumTermEndsAt: null,
371
+ billingCycle: "MONTHLY",
372
+ currentPeriodStart: /* @__PURE__ */ new Date("2026-02-21T00:00:00.000Z"),
373
+ currentPeriodEnd: /* @__PURE__ */ new Date("2026-02-28T00:00:00.000Z")
374
+ });
375
+ assert.equal(booked.billingCycle, "MONTHLY");
376
+ const [readBack] = await repository.listBySubscription(subscriptionId);
377
+ assert.ok(readBack, "the booking must be readable back");
378
+ assert.equal(readBack.billingCycle, "MONTHLY");
379
+ assert.equal(
380
+ readBack.currentPeriodEnd?.toISOString(),
381
+ "2026-02-28T00:00:00.000Z",
382
+ "the period end must survive the round trip"
383
+ );
384
+ assert.equal(readBack.currentPeriodStart?.toISOString(), "2026-02-21T00:00:00.000Z");
385
+ const legacy = await repository.add({
386
+ subscriptionId,
387
+ bundleVersionId,
388
+ startedAt: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
389
+ minimumTermEndsAt: null
390
+ });
391
+ const legacyReadBack = await repository.findById(legacy.id);
392
+ assert.ok(legacyReadBack, "the legacy booking must be readable back");
393
+ assert.equal(legacyReadBack.billingCycle, null);
394
+ assert.equal(legacyReadBack.currentPeriodStart, null);
395
+ assert.equal(legacyReadBack.currentPeriodEnd, null);
396
+ });
397
+ test("a second cancellation of one booking is refused, not applied", async (t) => {
398
+ const repository = harness.adapter.subscriptionBundleRepository;
399
+ const { seed } = harness;
400
+ if (!repository || !seed.createBundleVersion) {
401
+ t.skip("adapter provides no SubscriptionBundleRepository or bundle catalog");
402
+ return;
403
+ }
404
+ const { planVersionId } = await seed.createPlanVersion({
405
+ planKey: "PRO",
406
+ version: 1,
407
+ quotas: {},
408
+ features: ["CORE"],
409
+ published: true
410
+ });
411
+ const { subscriptionId } = await seed.createSubscription({
412
+ tenantId: "tenant-double-cancel",
413
+ plan: "PRO",
414
+ planVersionId
415
+ });
416
+ const { bundleVersionId } = await seed.createBundleVersion({
417
+ bundleKey: "ANALYTICS",
418
+ features: ["REPORTS"]
419
+ });
420
+ const booking = await repository.add({
421
+ subscriptionId,
422
+ bundleVersionId,
423
+ startedAt: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
424
+ minimumTermEndsAt: null
425
+ });
426
+ const first = await repository.cancel(booking.id, {
427
+ canceledAt: /* @__PURE__ */ new Date("2026-03-01T00:00:00.000Z"),
428
+ canceledEffectiveAt: /* @__PURE__ */ new Date("2026-04-01T00:00:00.000Z")
429
+ });
430
+ assert.equal(first.canceledEffectiveAt?.toISOString(), "2026-04-01T00:00:00.000Z");
431
+ await assert.rejects(
432
+ () => repository.cancel(booking.id, {
433
+ canceledAt: /* @__PURE__ */ new Date("2026-03-02T00:00:00.000Z"),
434
+ canceledEffectiveAt: /* @__PURE__ */ new Date("2026-09-01T00:00:00.000Z")
435
+ }),
436
+ "a second cancellation must be refused"
437
+ );
438
+ const readBack = await repository.findById(booking.id);
439
+ assert.equal(
440
+ readBack?.canceledEffectiveAt?.toISOString(),
441
+ "2026-04-01T00:00:00.000Z",
442
+ "the first cancellation must still stand"
443
+ );
444
+ await repository.reactivate(booking.id);
445
+ const again = await repository.cancel(booking.id, {
446
+ canceledAt: /* @__PURE__ */ new Date("2026-03-02T00:00:00.000Z"),
447
+ canceledEffectiveAt: /* @__PURE__ */ new Date("2026-09-01T00:00:00.000Z")
448
+ });
449
+ assert.equal(again.canceledEffectiveAt?.toISOString(), "2026-09-01T00:00:00.000Z");
450
+ });
451
+ test("a subscription's bookings come back newest first", async (t) => {
452
+ const repository = harness.adapter.subscriptionBundleRepository;
453
+ const { seed } = harness;
454
+ if (!repository || !seed.createBundleVersion) {
455
+ t.skip("adapter provides no SubscriptionBundleRepository or bundle catalog");
456
+ return;
457
+ }
458
+ const { planVersionId } = await seed.createPlanVersion({
459
+ planKey: "PRO",
460
+ version: 1,
461
+ quotas: {},
462
+ features: ["CORE"],
463
+ published: true
464
+ });
465
+ const { subscriptionId } = await seed.createSubscription({
466
+ tenantId: "tenant-booking-order",
467
+ plan: "PRO",
468
+ planVersionId
469
+ });
470
+ for (const [key, startedAt] of [
471
+ ["OLDEST", "2026-01-01T00:00:00.000Z"],
472
+ ["MIDDLE", "2026-02-01T00:00:00.000Z"],
473
+ ["NEWEST", "2026-03-01T00:00:00.000Z"]
474
+ ]) {
475
+ const { bundleVersionId } = await seed.createBundleVersion({
476
+ bundleKey: key,
477
+ features: ["REPORTS"]
478
+ });
479
+ await repository.add({
480
+ subscriptionId,
481
+ bundleVersionId,
482
+ startedAt: new Date(startedAt),
483
+ minimumTermEndsAt: null
484
+ });
485
+ }
486
+ const listed = await repository.listBySubscription(subscriptionId);
487
+ assert.deepEqual(
488
+ listed.map((row) => row.startedAt.toISOString()),
489
+ [
490
+ "2026-03-01T00:00:00.000Z",
491
+ "2026-02-01T00:00:00.000Z",
492
+ "2026-01-01T00:00:00.000Z"
493
+ ]
494
+ );
495
+ });
496
+ test("a booking with no request date is active, whatever its effective date says", async (t) => {
497
+ const repository = harness.adapter.subscriptionBundleRepository;
498
+ const { seed } = harness;
499
+ if (!repository || !seed.createBundleVersion) {
500
+ t.skip("adapter provides no SubscriptionBundleRepository or bundle catalog");
501
+ return;
502
+ }
503
+ const { planVersionId } = await seed.createPlanVersion({
504
+ planKey: "PRO",
505
+ version: 1,
506
+ quotas: {},
507
+ features: ["CORE"],
508
+ published: true
509
+ });
510
+ const { subscriptionId } = await seed.createSubscription({
511
+ tenantId: "tenant-half-cancelled",
512
+ plan: "PRO",
513
+ planVersionId
514
+ });
515
+ const { bundleVersionId } = await seed.createBundleVersion({
516
+ bundleKey: "ANALYTICS",
517
+ features: ["REPORTS"]
518
+ });
519
+ const booking = await repository.add({
520
+ subscriptionId,
521
+ bundleVersionId,
522
+ startedAt: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
523
+ minimumTermEndsAt: null
524
+ });
525
+ await repository.cancel(booking.id, {
526
+ canceledAt: /* @__PURE__ */ new Date("2026-02-01T00:00:00.000Z"),
527
+ canceledEffectiveAt: /* @__PURE__ */ new Date("2026-03-01T00:00:00.000Z")
528
+ });
529
+ const clearRequestDate = harness.seed.clearBookingRequestDate;
530
+ if (!clearRequestDate) {
531
+ t.skip("adapter harness cannot write the half-cancelled shape");
532
+ return;
533
+ }
534
+ await clearRequestDate(booking.id);
535
+ const active = await repository.listActiveBySubscription(
536
+ subscriptionId,
537
+ /* @__PURE__ */ new Date("2026-06-01T00:00:00.000Z")
538
+ );
539
+ assert.equal(active.length, 1, "no request date means nobody asked to cancel it");
540
+ assert.equal(
541
+ await repository.countActiveByBundleVersionId(
542
+ bundleVersionId,
543
+ /* @__PURE__ */ new Date("2026-06-01T00:00:00.000Z")
544
+ ),
545
+ 1
546
+ );
547
+ });
548
+ test("discarding a draft cannot remove a version published meanwhile", async (t) => {
549
+ const catalog = harness.adapter.bundleRepository;
550
+ const discardDraft = catalog?.deleteDraft?.bind(catalog);
551
+ if (!catalog || !discardDraft) {
552
+ t.skip("adapter provides no BundleRepository");
553
+ return;
554
+ }
555
+ const bundle = await catalog.create({
556
+ bundleKey: "RACE",
557
+ label: "Race"
558
+ });
559
+ const draft = await catalog.createDraft({
560
+ bundleId: bundle.id,
561
+ features: ["REPORTS"],
562
+ quotas: {}
563
+ });
564
+ await catalog.publishDraft(draft.id, {
565
+ publishedByUserId: null,
566
+ publishedChanges: [],
567
+ nonRegressive: true,
568
+ validFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
569
+ validUntil: null
570
+ });
571
+ await assert.rejects(
572
+ () => discardDraft(draft.id),
573
+ "a published version must not be discardable"
574
+ );
575
+ assert.ok(
576
+ await catalog.findVersionById(draft.id),
577
+ "and it must still be there afterwards"
578
+ );
579
+ });
580
+ test("publishing one draft twice claims it once, and the windows stay adjacent", async (t) => {
581
+ const catalog = harness.adapter.bundleRepository;
582
+ const publish = catalog?.publishDraft?.bind(catalog);
583
+ if (!catalog || !publish) {
584
+ t.skip("adapter provides no BundleRepository");
585
+ return;
586
+ }
587
+ const bundle = await catalog.create({
588
+ bundleKey: "CLAIM",
589
+ label: "Claim"
590
+ });
591
+ const first = await catalog.createDraft({
592
+ bundleId: bundle.id,
593
+ features: ["A"],
594
+ quotas: {}
595
+ });
596
+ await publish(first.id, {
597
+ publishedByUserId: null,
598
+ publishedChanges: [],
599
+ nonRegressive: true,
600
+ validFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
601
+ validUntil: null
602
+ });
603
+ const second = await catalog.createDraft({
604
+ bundleId: bundle.id,
605
+ baseVersionId: first.id,
606
+ features: ["A", "B"],
607
+ quotas: {}
608
+ });
609
+ const publishedAt = /* @__PURE__ */ new Date("2026-03-01T00:00:00.000Z");
610
+ await publish(second.id, {
611
+ publishedByUserId: null,
612
+ publishedChanges: [],
613
+ nonRegressive: true,
614
+ validFrom: publishedAt,
615
+ validUntil: null
616
+ });
617
+ await assert.rejects(
618
+ () => publish(second.id, {
619
+ publishedByUserId: null,
620
+ publishedChanges: [],
621
+ nonRegressive: true,
622
+ validFrom: /* @__PURE__ */ new Date("2026-06-01T00:00:00.000Z"),
623
+ validUntil: null
624
+ }),
625
+ "a version that is already published must not be published again"
626
+ );
627
+ const successor = await catalog.findVersionById(second.id);
628
+ const predecessor = await catalog.findVersionById(first.id);
629
+ assert.equal(
630
+ successor?.validFrom && new Date(successor.validFrom).toISOString(),
631
+ publishedAt.toISOString(),
632
+ "the winning date must still stand"
633
+ );
634
+ assert.ok(predecessor?.supersededAt, "the predecessor must be superseded");
635
+ if (predecessor?.validUntil) {
636
+ const closesAt = new Date(predecessor.validUntil);
637
+ assert.equal(
638
+ closesAt.toISOString().slice(0, 10),
639
+ "2026-02-28",
640
+ "the predecessor must close the day before its successor opens"
641
+ );
642
+ }
643
+ });
644
+ test("a plan key names one plan for the whole installation", async (t) => {
645
+ const repository = harness.adapter.planRepository;
646
+ if (!repository) {
647
+ t.skip("adapter provides no PlanRepository");
648
+ return;
649
+ }
650
+ await repository.create({ planKey: "DOUBLE", label: "First" });
651
+ await assert.rejects(
652
+ () => repository.create({ planKey: "DOUBLE", label: "Second" }),
653
+ "a plan key is taken once"
654
+ );
655
+ });
656
+ test("a bundle key names one bundle for the whole installation", async (t) => {
657
+ const catalog = harness.adapter.bundleRepository;
658
+ if (!catalog) {
659
+ t.skip("adapter provides no BundleRepository");
660
+ return;
661
+ }
662
+ await catalog.create({ bundleKey: "DOUBLE", label: "First" });
663
+ await assert.rejects(
664
+ () => catalog.create({ bundleKey: "DOUBLE", label: "Second" }),
665
+ "a bundle key is taken once"
666
+ );
667
+ });
668
+ test("a retired plan still occupies its key", async (t) => {
669
+ const repository = harness.adapter.planRepository;
670
+ const retire = repository?.softDelete?.bind(repository);
671
+ const byKey = repository?.findByKey?.bind(repository);
672
+ if (!repository || !retire || !byKey) {
673
+ t.skip("adapter provides no PlanRepository");
674
+ return;
675
+ }
676
+ const plan = await repository.create({ planKey: "RETIRED_PLAN", label: "Retired" });
677
+ await retire(plan.id);
678
+ const stillThere = await byKey("RETIRED_PLAN");
679
+ assert.equal(stillThere?.id, plan.id, "the key is not free again");
680
+ assert.ok(stillThere?.deletedAt, "and the row says it is retired");
681
+ assert.equal(
682
+ (await repository.list({})).some((row) => row.id === plan.id),
683
+ false,
684
+ "a retired plan is not in the catalogue an operator browses"
685
+ );
686
+ });
687
+ test("a retired bundle still occupies its key", async (t) => {
688
+ const catalog = harness.adapter.bundleRepository;
689
+ const retire = catalog?.softDelete?.bind(catalog);
690
+ const byKey = catalog?.findByKey?.bind(catalog);
691
+ if (!catalog || !retire || !byKey) {
692
+ t.skip("adapter provides no BundleRepository");
693
+ return;
694
+ }
695
+ const bundle = await catalog.create({
696
+ bundleKey: "RETIRED_KEY",
697
+ label: "Retired"
698
+ });
699
+ assert.equal((await byKey("RETIRED_KEY"))?.id, bundle.id);
700
+ await retire(bundle.id);
701
+ const stillThere = await byKey("RETIRED_KEY");
702
+ assert.equal(stillThere?.id, bundle.id, "the key is not free again");
703
+ assert.ok(stillThere?.deletedAt, "and the row says it is retired");
704
+ const listed = await catalog.list({});
705
+ assert.equal(
706
+ listed.some((row) => row.id === bundle.id),
707
+ false,
708
+ "a retired bundle is not in the catalogue an operator browses"
709
+ );
710
+ });
344
711
  test("countByPlanVersionId counts current AND pending bindings in one query", async (t) => {
345
712
  const { seed, adapter } = harness;
346
713
  if (!adapter.subscriptionRepository.countByPlanVersionId) {
@@ -711,13 +1078,245 @@ function persistenceAdapterContract(options) {
711
1078
  assert.equal(seen?.id, subscriptionId);
712
1079
  assert.equal(seen?.tenantId, "tenant-a");
713
1080
  });
714
- test("immutable subscription contracts (append-only, terminate-only)", (t) => {
715
- if (!harness.adapter.subscriptionContractRepository) {
716
- t.skip("adapter provides no SubscriptionContractRepository \u2014 scenario pending");
1081
+ test("a contract keeps what was agreed, and ending it does not rewrite it", async (t) => {
1082
+ const contracts = harness.adapter.subscriptionContractRepository;
1083
+ if (!contracts) {
1084
+ t.skip("adapter provides no SubscriptionContractRepository");
1085
+ return;
1086
+ }
1087
+ const tenantId = "tenant-contract-lifecycle";
1088
+ const signedAt = /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z");
1089
+ const created = await contracts.create({
1090
+ tenantId,
1091
+ effectiveFrom: signedAt,
1092
+ priceSnapshot: {
1093
+ currency: "EUR",
1094
+ billingCycle: "monthly",
1095
+ subtotalNet: 29.9,
1096
+ discountNet: 0,
1097
+ totalNet: 29.9,
1098
+ vatRate: 19,
1099
+ totalGross: 35.58
1100
+ },
1101
+ entitlementSnapshot: {
1102
+ plan: "STANDARD",
1103
+ features: ["CORE"],
1104
+ quotas: { users: 5 }
1105
+ },
1106
+ originalBundleVersionIds: ["bundle-version-1"],
1107
+ termsSnapshot: { noticePeriodDays: 30 },
1108
+ lineItems: [
1109
+ {
1110
+ kind: "plan",
1111
+ sourceKey: "STANDARD",
1112
+ sourceVersionId: "plan-version-1",
1113
+ titleSnapshot: "Standard",
1114
+ descriptionSnapshot: "The plan as it was signed",
1115
+ quantity: 1,
1116
+ unit: null,
1117
+ priceNet: 19.9,
1118
+ priceGross: 23.68,
1119
+ billingCycle: "monthly",
1120
+ minimumTermUntil: /* @__PURE__ */ new Date("2027-01-01T00:00:00.000Z"),
1121
+ featuresSnapshot: ["CORE"],
1122
+ quotaEffectsSnapshot: { users: 5 },
1123
+ metadata: { origin: "onboarding" }
1124
+ },
1125
+ {
1126
+ kind: "bundle",
1127
+ sourceKey: "EXTRA-SEATS",
1128
+ sourceVersionId: "bundle-version-1",
1129
+ titleSnapshot: "Extra seats",
1130
+ descriptionSnapshot: null,
1131
+ quantity: 1,
1132
+ unit: "seat",
1133
+ priceNet: 10,
1134
+ priceGross: 11.9,
1135
+ billingCycle: "monthly",
1136
+ minimumTermUntil: null,
1137
+ featuresSnapshot: [],
1138
+ quotaEffectsSnapshot: { users: 5 },
1139
+ metadata: null
1140
+ }
1141
+ ]
1142
+ });
1143
+ assert.equal(created.status, "active");
1144
+ assert.equal(created.tenantId, tenantId);
1145
+ assert.equal(created.lineItems.length, 2);
1146
+ assert.deepEqual(created.originalBundleVersionIds, ["bundle-version-1"]);
1147
+ assert.deepEqual(created.termsSnapshot, { noticePeriodDays: 30 });
1148
+ const planLine = created.lineItems.find((item) => item.kind === "plan");
1149
+ assert.ok(planLine, "plan line expected");
1150
+ assert.equal(planLine.priceNet, 19.9, "money must survive the round trip unrounded");
1151
+ assert.equal(planLine.priceGross, 23.68);
1152
+ assert.equal(planLine.billingCycle, "monthly");
1153
+ assert.deepEqual(planLine.quotaEffectsSnapshot, { users: 5 });
1154
+ assert.equal(planLine.descriptionSnapshot, "The plan as it was signed");
1155
+ assert.equal(
1156
+ planLine.minimumTermUntil?.getTime(),
1157
+ (/* @__PURE__ */ new Date("2027-01-01T00:00:00.000Z")).getTime(),
1158
+ "the commitment is part of what was agreed"
1159
+ );
1160
+ assert.deepEqual(planLine.metadata, { origin: "onboarding" });
1161
+ const bundleLine = created.lineItems.find((item) => item.kind === "bundle");
1162
+ assert.ok(bundleLine, "bundle line expected");
1163
+ assert.equal(bundleLine.descriptionSnapshot, null);
1164
+ assert.equal(bundleLine.unit, "seat");
1165
+ assert.equal(bundleLine.minimumTermUntil, null);
1166
+ assert.equal(bundleLine.metadata, null);
1167
+ const readBack = await contracts.findById(created.id);
1168
+ assert.ok(readBack, "contract expected by id");
1169
+ assert.equal(
1170
+ readBack.lineItems.length,
1171
+ 2,
1172
+ "lines belong to the contract, not the call"
1173
+ );
1174
+ assert.equal(
1175
+ (await contracts.findActiveByTenantId(
1176
+ tenantId,
1177
+ /* @__PURE__ */ new Date("2026-06-01T00:00:00.000Z")
1178
+ ))?.id,
1179
+ created.id
1180
+ );
1181
+ assert.equal(
1182
+ await contracts.findActiveByTenantId(
1183
+ tenantId,
1184
+ /* @__PURE__ */ new Date("2025-12-31T23:59:59.999Z")
1185
+ ),
1186
+ null,
1187
+ "a contract is not active before it starts"
1188
+ );
1189
+ const endsAt = /* @__PURE__ */ new Date("2026-07-01T00:00:00.000Z");
1190
+ const terminated = await contracts.terminate(created.id, {
1191
+ effectiveUntil: endsAt,
1192
+ status: null
1193
+ });
1194
+ assert.equal(
1195
+ terminated.status,
1196
+ "active",
1197
+ "a null status leaves the contract in the state it had"
1198
+ );
1199
+ assert.equal(terminated.effectiveUntil?.getTime(), endsAt.getTime());
1200
+ assert.equal(terminated.lineItems.length, 2, "ending a contract keeps its lines");
1201
+ assert.equal(
1202
+ terminated.priceSnapshot.totalNet,
1203
+ created.priceSnapshot.totalNet,
1204
+ "ending a contract does not restate its price"
1205
+ );
1206
+ const afterEnd = await contracts.findById(created.id);
1207
+ assert.ok(afterEnd, "a terminated contract is still readable");
1208
+ assert.equal(afterEnd.lineItems.length, 2);
1209
+ assert.equal(
1210
+ (await contracts.findActiveByTenantId(
1211
+ tenantId,
1212
+ /* @__PURE__ */ new Date("2026-06-30T00:00:00.000Z")
1213
+ ))?.id,
1214
+ created.id,
1215
+ "active up to the moment it ends"
1216
+ );
1217
+ assert.equal(
1218
+ await contracts.findActiveByTenantId(tenantId, endsAt),
1219
+ null,
1220
+ "and not at that moment"
1221
+ );
1222
+ });
1223
+ test("a successor takes over without erasing the contract it replaces", async (t) => {
1224
+ const contracts = harness.adapter.subscriptionContractRepository;
1225
+ if (!contracts) {
1226
+ t.skip("adapter provides no SubscriptionContractRepository");
717
1227
  return;
718
1228
  }
719
- assert.fail(
720
- "SubscriptionContractRepository present but the contract kit has no scenario yet \u2014 extend the kit"
1229
+ const tenantId = "tenant-contract-succession";
1230
+ const handover = /* @__PURE__ */ new Date("2026-04-01T00:00:00.000Z");
1231
+ const lineAt = (priceNet, priceGross) => ({
1232
+ kind: "plan",
1233
+ sourceKey: "STANDARD",
1234
+ sourceVersionId: null,
1235
+ titleSnapshot: "Standard",
1236
+ descriptionSnapshot: null,
1237
+ quantity: 1,
1238
+ unit: null,
1239
+ priceNet,
1240
+ priceGross,
1241
+ billingCycle: "monthly",
1242
+ minimumTermUntil: null,
1243
+ featuresSnapshot: [],
1244
+ quotaEffectsSnapshot: {},
1245
+ metadata: null
1246
+ });
1247
+ const priceAt = (net, gross) => ({
1248
+ currency: "EUR",
1249
+ billingCycle: "monthly",
1250
+ subtotalNet: net,
1251
+ discountNet: 0,
1252
+ totalNet: net,
1253
+ vatRate: 19,
1254
+ totalGross: gross
1255
+ });
1256
+ const first = await contracts.create({
1257
+ tenantId,
1258
+ effectiveFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
1259
+ priceSnapshot: priceAt(19.9, 23.68),
1260
+ lineItems: [lineAt(19.9, 23.68)]
1261
+ });
1262
+ assert.equal(
1263
+ (await contracts.findActiveByTenantId(
1264
+ tenantId,
1265
+ /* @__PURE__ */ new Date("2026-03-31T00:00:00.000Z")
1266
+ ))?.id,
1267
+ first.id
1268
+ );
1269
+ await contracts.terminate(first.id, {
1270
+ effectiveUntil: handover,
1271
+ status: "superseded"
1272
+ });
1273
+ const second = await contracts.create({
1274
+ tenantId,
1275
+ effectiveFrom: handover,
1276
+ priceSnapshot: priceAt(24.9, 29.63),
1277
+ lineItems: [lineAt(24.9, 29.63)]
1278
+ });
1279
+ assert.equal(
1280
+ (await contracts.findActiveByTenantId(tenantId, handover))?.id,
1281
+ second.id,
1282
+ "the successor takes over at the moment the predecessor ends"
1283
+ );
1284
+ assert.equal(
1285
+ await contracts.findActiveByTenantId(
1286
+ tenantId,
1287
+ /* @__PURE__ */ new Date("2026-03-31T00:00:00.000Z")
1288
+ ),
1289
+ null,
1290
+ "a superseded contract is not live at any asOf"
1291
+ );
1292
+ const superseded = await contracts.findById(first.id);
1293
+ assert.equal(superseded?.status, "superseded");
1294
+ assert.equal(
1295
+ superseded?.priceSnapshot.totalNet,
1296
+ 19.9,
1297
+ "the replaced contract keeps the price it was signed at"
1298
+ );
1299
+ const history = await contracts.list({ tenantId });
1300
+ assert.equal(history.length, 2, "both contracts remain in the history");
1301
+ assert.deepEqual(
1302
+ history.map((contract) => contract.lineItems.map((item) => item.priceNet)),
1303
+ [[24.9], [19.9]]
1304
+ );
1305
+ assert.deepEqual(
1306
+ history.map((contract) => contract.id),
1307
+ [second.id, first.id],
1308
+ "newest first"
1309
+ );
1310
+ assert.deepEqual(
1311
+ (await contracts.list({ tenantId, asOf: /* @__PURE__ */ new Date("2026-03-31T00:00:00.000Z") })).map((contract) => contract.id),
1312
+ [first.id],
1313
+ "asOf narrows the history to what was in force then"
1314
+ );
1315
+ assert.deepEqual(
1316
+ (await contracts.list({ tenantId, status: "superseded" })).map(
1317
+ (contract) => contract.id
1318
+ ),
1319
+ [first.id]
721
1320
  );
722
1321
  });
723
1322
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saasicat/persistence-testing",
3
- "version": "1.0.0-rc.6",
3
+ "version": "1.0.0-rc.7",
4
4
  "description": "Contract test kit for SaaSiCat persistence adapters: one node:test suite that every adapter (Prisma, Drizzle, ...) must pass against a real database — locks, transaction rollback, atomic promo claims, tenant isolation, audit/MFA roundtrips.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -23,7 +23,7 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@saasicat/core": "^1.0.0-rc.6"
26
+ "@saasicat/core": "^1.0.0-rc.7"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/node": "^25.6.0",