@saasicat/persistence-testing 1.0.0-rc.2 → 1.0.0-rc.20
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/README.md +72 -11
- package/dist/.build-stamp +1 -1
- package/dist/index.cjs +2335 -36
- package/dist/index.d.cts +106 -7
- package/dist/index.d.ts +106 -7
- package/dist/index.js +2335 -36
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -2,12 +2,276 @@
|
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
3
|
import { after, before, beforeEach, describe, test } from "node:test";
|
|
4
4
|
var LOCK_HOLD_MS = 150;
|
|
5
|
+
var OFFER = {
|
|
6
|
+
planKey: "STANDARD",
|
|
7
|
+
planVersionId: null,
|
|
8
|
+
billingCycle: "monthly",
|
|
9
|
+
priceBreakdown: {
|
|
10
|
+
currency: "EUR",
|
|
11
|
+
billingCycle: "monthly",
|
|
12
|
+
planNet: 49,
|
|
13
|
+
bundlesNet: 0,
|
|
14
|
+
regularNet: 49,
|
|
15
|
+
effectiveNet: 49,
|
|
16
|
+
vatRate: 19,
|
|
17
|
+
effectiveGross: 58.31
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
function partiesWith(subscriberId, legalName) {
|
|
21
|
+
return {
|
|
22
|
+
subscriberId,
|
|
23
|
+
subscriber: {
|
|
24
|
+
customerNumber: "K-10001",
|
|
25
|
+
legalName,
|
|
26
|
+
vatId: "DE123456789",
|
|
27
|
+
taxNumber: null,
|
|
28
|
+
addressLine1: "Hauptstra\xDFe 1",
|
|
29
|
+
addressLine2: null,
|
|
30
|
+
postalCode: "10115",
|
|
31
|
+
city: "Berlin",
|
|
32
|
+
country: "DE"
|
|
33
|
+
},
|
|
34
|
+
issuer: {
|
|
35
|
+
legalName: "Example Software GmbH",
|
|
36
|
+
vatId: "DE987654321",
|
|
37
|
+
taxNumber: "12/345/67890",
|
|
38
|
+
addressLine1: "Werkstra\xDFe 5",
|
|
39
|
+
addressLine2: null,
|
|
40
|
+
postalCode: "80331",
|
|
41
|
+
city: "M\xFCnchen",
|
|
42
|
+
country: "DE"
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function eventAt(gatewayAccount, eventId, about = {}) {
|
|
47
|
+
return {
|
|
48
|
+
gatewayAccount,
|
|
49
|
+
eventId,
|
|
50
|
+
provider: "stripe",
|
|
51
|
+
sessionId: `cs_${eventId}`,
|
|
52
|
+
kind: "payment-method-confirmed",
|
|
53
|
+
summary: { type: "card", last4: "4242" },
|
|
54
|
+
...about
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
var CARD = {
|
|
58
|
+
type: "card",
|
|
59
|
+
brand: "visa",
|
|
60
|
+
last4: "4242",
|
|
61
|
+
expiryMonth: 12,
|
|
62
|
+
expiryYear: 2030,
|
|
63
|
+
country: null,
|
|
64
|
+
bankCode: null,
|
|
65
|
+
mandateReference: null,
|
|
66
|
+
customerRef: "cus_1",
|
|
67
|
+
paymentMethodRef: "pm_card"
|
|
68
|
+
};
|
|
69
|
+
function paymentMethodFor(subscriberId, paymentMethodRef, confirmedAt, gatewayAccount = "stripe-main") {
|
|
70
|
+
return {
|
|
71
|
+
...CARD,
|
|
72
|
+
paymentMethodRef,
|
|
73
|
+
subscriberId,
|
|
74
|
+
gatewayAccount,
|
|
75
|
+
provider: "stripe",
|
|
76
|
+
confirmedAt: new Date(confirmedAt)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function subscriberFor(tenantId, legalName) {
|
|
80
|
+
return {
|
|
81
|
+
tenantId,
|
|
82
|
+
legalName,
|
|
83
|
+
vatId: null,
|
|
84
|
+
taxNumber: null,
|
|
85
|
+
addressLine1: null,
|
|
86
|
+
addressLine2: null,
|
|
87
|
+
postalCode: null,
|
|
88
|
+
city: null,
|
|
89
|
+
country: null,
|
|
90
|
+
invoiceEmail: null,
|
|
91
|
+
customerNumberPrefix: ""
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function contractFromOffer(offerId, parties) {
|
|
95
|
+
return {
|
|
96
|
+
tenantId: `tenant-${offerId}`,
|
|
97
|
+
parties,
|
|
98
|
+
effectiveFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
|
|
99
|
+
originalOfferId: offerId,
|
|
100
|
+
priceSnapshot: {
|
|
101
|
+
currency: "EUR",
|
|
102
|
+
billingCycle: "monthly",
|
|
103
|
+
subtotalNet: 49,
|
|
104
|
+
discountNet: 0,
|
|
105
|
+
totalNet: 49,
|
|
106
|
+
vatRate: 19,
|
|
107
|
+
totalGross: 58.31
|
|
108
|
+
},
|
|
109
|
+
lineItems: [
|
|
110
|
+
{
|
|
111
|
+
kind: "plan",
|
|
112
|
+
sourceKey: "STANDARD",
|
|
113
|
+
sourceVersionId: null,
|
|
114
|
+
titleSnapshot: "Standard",
|
|
115
|
+
descriptionSnapshot: null,
|
|
116
|
+
quantity: 1,
|
|
117
|
+
unit: null,
|
|
118
|
+
priceNet: 49,
|
|
119
|
+
priceGross: 58.31,
|
|
120
|
+
billingCycle: "monthly",
|
|
121
|
+
currency: "EUR",
|
|
122
|
+
taxRate: 19,
|
|
123
|
+
taxAmount: 9.31,
|
|
124
|
+
minimumTermUntil: null,
|
|
125
|
+
featuresSnapshot: [],
|
|
126
|
+
quotaEffectsSnapshot: {},
|
|
127
|
+
metadata: null
|
|
128
|
+
}
|
|
129
|
+
]
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
var CONTRACT_GAPS = {
|
|
133
|
+
atomicPlanBinding: {
|
|
134
|
+
reason: "adapter does not expose atomic plan-binding writes",
|
|
135
|
+
present: ({ adapter }) => Boolean(adapter.tenantSubscriptionWrite)
|
|
136
|
+
},
|
|
137
|
+
atomicOnboarding: {
|
|
138
|
+
reason: "adapter does not expose atomic onboarding writes",
|
|
139
|
+
present: ({ adapter }) => Boolean(adapter.tenantSubscriptionWrite?.applyOnboardingSelection)
|
|
140
|
+
},
|
|
141
|
+
promoCodes: {
|
|
142
|
+
reason: "adapter provides no PromoCodeRepository",
|
|
143
|
+
present: ({ adapter }) => Boolean(adapter.promoCodeRepository)
|
|
144
|
+
},
|
|
145
|
+
promoCodeRedemptions: {
|
|
146
|
+
reason: "adapter provides no PromoCodeRedemptionRepository",
|
|
147
|
+
present: ({ adapter }) => Boolean(adapter.promoCodeRedemptionRepository)
|
|
148
|
+
},
|
|
149
|
+
promoSubscriptionLookup: {
|
|
150
|
+
reason: "adapter provides no PromoSubscriptionLookup",
|
|
151
|
+
present: ({ adapter }) => Boolean(adapter.promoSubscriptionLookup)
|
|
152
|
+
},
|
|
153
|
+
planRepository: {
|
|
154
|
+
reason: "adapter provides no PlanRepository",
|
|
155
|
+
present: ({ adapter }) => Boolean(adapter.planRepository)
|
|
156
|
+
},
|
|
157
|
+
planLifecycle: {
|
|
158
|
+
reason: "adapter provides no time-aware PlanRepository lifecycle",
|
|
159
|
+
present: ({ adapter }) => {
|
|
160
|
+
const repository = adapter.planRepository;
|
|
161
|
+
return Boolean(
|
|
162
|
+
repository?.createPlanVersionDraft && repository.publishPlanVersionDraft && repository.findVersionById && repository.findActivePlanVersion
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
planRetirement: {
|
|
167
|
+
reason: "adapter provides no PlanRepository that retires and finds plans by key",
|
|
168
|
+
present: ({ adapter }) => Boolean(adapter.planRepository?.softDelete && adapter.planRepository.findByKey)
|
|
169
|
+
},
|
|
170
|
+
planVersionReads: {
|
|
171
|
+
reason: "adapter provides no PlanRepository that reads versions by plan key",
|
|
172
|
+
present: ({ adapter }) => {
|
|
173
|
+
const repository = adapter.planRepository;
|
|
174
|
+
return Boolean(
|
|
175
|
+
repository?.listVersions && repository.findCurrentDraft && repository.findLatestLivePlanVersion
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
planVersionRetirement: {
|
|
180
|
+
reason: "adapter provides no PlanRepository that reads versions and retires plans",
|
|
181
|
+
present: ({ adapter }) => {
|
|
182
|
+
const repository = adapter.planRepository;
|
|
183
|
+
return Boolean(
|
|
184
|
+
repository?.createPlanVersionDraft && repository.publishPlanVersionDraft && repository.listVersions && repository.findCurrentDraft && repository.findLatestLivePlanVersion && repository.softDelete
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
bundleRepository: {
|
|
189
|
+
reason: "adapter provides no BundleRepository",
|
|
190
|
+
present: ({ adapter }) => Boolean(adapter.bundleRepository)
|
|
191
|
+
},
|
|
192
|
+
bundleValidity: {
|
|
193
|
+
reason: "adapter provides no time-aware BundleRepository",
|
|
194
|
+
present: ({ adapter }) => Boolean(adapter.bundleRepository?.findActiveBundleVersion)
|
|
195
|
+
},
|
|
196
|
+
bundleDraftDiscard: {
|
|
197
|
+
reason: "adapter provides no BundleRepository that discards drafts",
|
|
198
|
+
present: ({ adapter }) => Boolean(adapter.bundleRepository?.deleteDraft)
|
|
199
|
+
},
|
|
200
|
+
bundleDraftPublish: {
|
|
201
|
+
reason: "adapter provides no BundleRepository that publishes drafts",
|
|
202
|
+
present: ({ adapter }) => Boolean(adapter.bundleRepository?.publishDraft)
|
|
203
|
+
},
|
|
204
|
+
bundleRetirement: {
|
|
205
|
+
reason: "adapter provides no BundleRepository that retires and finds bundles by key",
|
|
206
|
+
present: ({ adapter }) => Boolean(adapter.bundleRepository?.softDelete && adapter.bundleRepository.findByKey)
|
|
207
|
+
},
|
|
208
|
+
bundleBookings: {
|
|
209
|
+
reason: "adapter provides no SubscriptionBundleRepository or bundle catalog",
|
|
210
|
+
present: ({ adapter, seed }) => Boolean(adapter.subscriptionBundleRepository && seed.createBundleVersion)
|
|
211
|
+
},
|
|
212
|
+
halfCancelledBookingSeed: {
|
|
213
|
+
reason: "adapter harness cannot write the half-cancelled shape",
|
|
214
|
+
present: ({ seed }) => Boolean(seed.clearBookingRequestDate)
|
|
215
|
+
},
|
|
216
|
+
countByPlanVersionId: {
|
|
217
|
+
reason: "adapter does not implement countByPlanVersionId (fail-closed fallback)",
|
|
218
|
+
present: ({ adapter }) => Boolean(adapter.subscriptionRepository.countByPlanVersionId)
|
|
219
|
+
},
|
|
220
|
+
audit: {
|
|
221
|
+
reason: "adapter provides no AuditPort/AuditQueryPort pair",
|
|
222
|
+
present: ({ adapter }) => Boolean(adapter.audit && adapter.auditQuery)
|
|
223
|
+
},
|
|
224
|
+
mfa: {
|
|
225
|
+
reason: "adapter provides no MfaPort",
|
|
226
|
+
present: ({ adapter }) => Boolean(adapter.mfa)
|
|
227
|
+
},
|
|
228
|
+
subscriptionContracts: {
|
|
229
|
+
reason: "adapter provides no SubscriptionContractRepository, or no subscriber seed for it",
|
|
230
|
+
present: ({ adapter, seed }) => Boolean(adapter.subscriptionContractRepository && seed.createSubscriber)
|
|
231
|
+
},
|
|
232
|
+
subscribers: {
|
|
233
|
+
reason: "adapter provides no SubscriberRepository",
|
|
234
|
+
present: ({ adapter }) => Boolean(adapter.subscriberRepository)
|
|
235
|
+
},
|
|
236
|
+
paymentEventLog: {
|
|
237
|
+
reason: "adapter provides no PaymentEventLog",
|
|
238
|
+
present: ({ adapter }) => Boolean(adapter.paymentEventLog)
|
|
239
|
+
},
|
|
240
|
+
subscriberPaymentMethods: {
|
|
241
|
+
reason: "adapter provides no SubscriberPaymentMethodRepository, or no subscriber seed for it",
|
|
242
|
+
present: ({ adapter, seed }) => Boolean(adapter.subscriberPaymentMethodRepository && seed.createSubscriber)
|
|
243
|
+
},
|
|
244
|
+
checkoutOffers: {
|
|
245
|
+
reason: "adapter provides no CheckoutOfferRepository",
|
|
246
|
+
present: ({ adapter }) => Boolean(adapter.checkoutOfferRepository)
|
|
247
|
+
},
|
|
248
|
+
appliedSettings: {
|
|
249
|
+
reason: "adapter provides no AppliedSettingsPort",
|
|
250
|
+
present: ({ adapter }) => Boolean(adapter.appliedSettings)
|
|
251
|
+
}
|
|
252
|
+
};
|
|
5
253
|
function sleep(ms) {
|
|
6
254
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
7
255
|
}
|
|
8
256
|
function persistenceAdapterContract(options) {
|
|
257
|
+
const declaredGaps = new Set(options.gaps ?? []);
|
|
258
|
+
let harness;
|
|
259
|
+
function missing(t, gap) {
|
|
260
|
+
const { reason, present } = CONTRACT_GAPS[gap];
|
|
261
|
+
if (present(harness)) {
|
|
262
|
+
assert.fail(
|
|
263
|
+
`'${gap}' counts as provided, yet this scenario found a member it needs missing or not callable. Check that the harness wires the port itself; if it does, the contract's check for '${gap}' and this scenario disagree, which is a defect in @saasicat/persistence-testing.`
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
if (declaredGaps.has(gap)) {
|
|
267
|
+
t.skip(reason);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
assert.fail(
|
|
271
|
+
`${reason}. Wire it into the harness, or declare \`gaps: ['${gap}']\` in persistenceAdapterContract if the adapter deliberately does not provide it.`
|
|
272
|
+
);
|
|
273
|
+
}
|
|
9
274
|
describe(`persistence adapter contract: ${options.name}`, () => {
|
|
10
|
-
let harness;
|
|
11
275
|
before(async () => {
|
|
12
276
|
harness = await options.create();
|
|
13
277
|
});
|
|
@@ -17,6 +281,20 @@ function persistenceAdapterContract(options) {
|
|
|
17
281
|
beforeEach(async () => {
|
|
18
282
|
await harness.reset();
|
|
19
283
|
});
|
|
284
|
+
test("the declared gaps are exactly the parts the harness does not provide", () => {
|
|
285
|
+
const gaps = Object.keys(CONTRACT_GAPS);
|
|
286
|
+
const absent = gaps.filter((gap) => !CONTRACT_GAPS[gap].present(harness));
|
|
287
|
+
const known = (gap) => Object.prototype.hasOwnProperty.call(CONTRACT_GAPS, gap);
|
|
288
|
+
const unknown = [...declaredGaps].filter((gap) => !known(gap));
|
|
289
|
+
const stale = [...declaredGaps].filter((gap) => known(gap) && !absent.includes(gap));
|
|
290
|
+
const undeclared = absent.filter((gap) => !declaredGaps.has(gap));
|
|
291
|
+
const problems = [
|
|
292
|
+
unknown.length > 0 && `declared as gaps but not parts of the contract: ${unknown.join(", ")} (ContractGap lists the names)`,
|
|
293
|
+
stale.length > 0 && `declared as gaps but wired into the harness: ${stale.join(", ")}`,
|
|
294
|
+
undeclared.length > 0 && `not wired into the harness and not declared as gaps: ${undeclared.join(", ")}`
|
|
295
|
+
].filter(Boolean);
|
|
296
|
+
assert.deepEqual(problems, [], problems.join("; "));
|
|
297
|
+
});
|
|
20
298
|
test("findByTenantId returns the tenant subscription with plan-version limits", async () => {
|
|
21
299
|
const { seed, adapter } = harness;
|
|
22
300
|
const { planVersionId } = await seed.createPlanVersion({
|
|
@@ -91,7 +369,7 @@ function persistenceAdapterContract(options) {
|
|
|
91
369
|
test("immediate plan change binds plan and active PlanVersion consistently", async (t) => {
|
|
92
370
|
const { seed, adapter } = harness;
|
|
93
371
|
if (!adapter.tenantSubscriptionWrite) {
|
|
94
|
-
t
|
|
372
|
+
missing(t, "atomicPlanBinding");
|
|
95
373
|
return;
|
|
96
374
|
}
|
|
97
375
|
const oldVersion = await seed.createPlanVersion({
|
|
@@ -113,13 +391,19 @@ function persistenceAdapterContract(options) {
|
|
|
113
391
|
plan: "STARTER",
|
|
114
392
|
planVersionId: oldVersion.planVersionId
|
|
115
393
|
});
|
|
116
|
-
await adapter.tenantSubscriptionWrite.changePlanImmediate(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
394
|
+
const change = await adapter.tenantSubscriptionWrite.changePlanImmediate(
|
|
395
|
+
"tenant-plan-change",
|
|
396
|
+
{
|
|
397
|
+
planId: "PRO",
|
|
398
|
+
cycle: "YEARLY",
|
|
399
|
+
periodStart: null,
|
|
400
|
+
periodEnd: null,
|
|
401
|
+
nextStatus: null,
|
|
402
|
+
// The row has no cancellation, so this claims it.
|
|
403
|
+
expectedCanceledAt: null
|
|
404
|
+
}
|
|
405
|
+
);
|
|
406
|
+
assert.equal(change.claimed, true, "the plan write did not claim the row");
|
|
123
407
|
const changed = await adapter.subscriptionRepository.findByTenantId("tenant-plan-change");
|
|
124
408
|
assert.ok(changed, "changed subscription expected");
|
|
125
409
|
assert.equal(changed.plan, "PRO");
|
|
@@ -130,12 +414,12 @@ function persistenceAdapterContract(options) {
|
|
|
130
414
|
const { seed, adapter } = harness;
|
|
131
415
|
const writer = adapter.tenantSubscriptionWrite;
|
|
132
416
|
if (!writer?.applyOnboardingSelection) {
|
|
133
|
-
t
|
|
417
|
+
missing(t, "atomicOnboarding");
|
|
134
418
|
return;
|
|
135
419
|
}
|
|
136
420
|
const redemptions = adapter.promoCodeRedemptionRepository;
|
|
137
421
|
if (!redemptions) {
|
|
138
|
-
t
|
|
422
|
+
missing(t, "promoCodeRedemptions");
|
|
139
423
|
return;
|
|
140
424
|
}
|
|
141
425
|
const oldVersion = await seed.createPlanVersion({
|
|
@@ -170,7 +454,8 @@ function persistenceAdapterContract(options) {
|
|
|
170
454
|
cycle: "MONTHLY",
|
|
171
455
|
periodStart: null,
|
|
172
456
|
periodEnd: null,
|
|
173
|
-
nextStatus: null
|
|
457
|
+
nextStatus: null,
|
|
458
|
+
expectedCanceledAt: null
|
|
174
459
|
},
|
|
175
460
|
async (tx, callbackSubscriptionId) => {
|
|
176
461
|
assert.equal(callbackSubscriptionId, subscriptionId);
|
|
@@ -213,15 +498,14 @@ function persistenceAdapterContract(options) {
|
|
|
213
498
|
test("plan lifecycle keeps semantic identity and auto-succeeds validity windows", async (t) => {
|
|
214
499
|
const repository = harness.adapter.planRepository;
|
|
215
500
|
if (!repository?.createPlanVersionDraft || !repository.publishPlanVersionDraft || !repository.findVersionById || !repository.findActivePlanVersion) {
|
|
216
|
-
t
|
|
501
|
+
missing(t, "planLifecycle");
|
|
217
502
|
return;
|
|
218
503
|
}
|
|
219
504
|
const plan = await repository.create({
|
|
220
|
-
projectKey: options.projectKey,
|
|
221
505
|
planKey: "STANDARD",
|
|
222
506
|
label: "Standard"
|
|
223
507
|
});
|
|
224
|
-
assert.equal(plan.
|
|
508
|
+
assert.equal(plan.planKey, "STANDARD");
|
|
225
509
|
const firstDraft = await repository.createPlanVersionDraft({
|
|
226
510
|
planId: "STANDARD",
|
|
227
511
|
features: ["CORE"],
|
|
@@ -278,15 +562,14 @@ function persistenceAdapterContract(options) {
|
|
|
278
562
|
test("bundle lifecycle roundtrips validity and auto-succeeds atomically", async (t) => {
|
|
279
563
|
const repository = harness.adapter.bundleRepository;
|
|
280
564
|
if (!repository?.findActiveBundleVersion) {
|
|
281
|
-
t
|
|
565
|
+
missing(t, "bundleValidity");
|
|
282
566
|
return;
|
|
283
567
|
}
|
|
284
568
|
const bundle = await repository.create({
|
|
285
|
-
projectKey: options.projectKey,
|
|
286
569
|
bundleKey: "REPORTING",
|
|
287
570
|
label: "Reporting"
|
|
288
571
|
});
|
|
289
|
-
assert.equal(bundle.
|
|
572
|
+
assert.equal(bundle.bundleKey, "REPORTING");
|
|
290
573
|
const firstDraft = await repository.createDraft({
|
|
291
574
|
bundleId: bundle.id,
|
|
292
575
|
features: ["REPORTS"],
|
|
@@ -334,10 +617,457 @@ function persistenceAdapterContract(options) {
|
|
|
334
617
|
second.id
|
|
335
618
|
);
|
|
336
619
|
});
|
|
620
|
+
test("a booking keeps the rhythm and the window it was made in", async (t) => {
|
|
621
|
+
const repository = harness.adapter.subscriptionBundleRepository;
|
|
622
|
+
const { seed } = harness;
|
|
623
|
+
if (!repository || !seed.createBundleVersion) {
|
|
624
|
+
missing(t, "bundleBookings");
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
const { planVersionId } = await seed.createPlanVersion({
|
|
628
|
+
planKey: "PRO",
|
|
629
|
+
version: 1,
|
|
630
|
+
quotas: {},
|
|
631
|
+
features: ["CORE"],
|
|
632
|
+
published: true
|
|
633
|
+
});
|
|
634
|
+
const { subscriptionId } = await seed.createSubscription({
|
|
635
|
+
tenantId: "tenant-bundle-period",
|
|
636
|
+
plan: "PRO",
|
|
637
|
+
planVersionId,
|
|
638
|
+
billingCycle: "YEARLY"
|
|
639
|
+
});
|
|
640
|
+
const { bundleVersionId } = await seed.createBundleVersion({
|
|
641
|
+
bundleKey: "ANALYTICS",
|
|
642
|
+
features: ["REPORTS"]
|
|
643
|
+
});
|
|
644
|
+
const booked = await repository.add({
|
|
645
|
+
subscriptionId,
|
|
646
|
+
bundleVersionId,
|
|
647
|
+
startedAt: /* @__PURE__ */ new Date("2026-02-21T00:00:00.000Z"),
|
|
648
|
+
minimumTermEndsAt: null,
|
|
649
|
+
billingCycle: "MONTHLY",
|
|
650
|
+
currentPeriodStart: /* @__PURE__ */ new Date("2026-02-21T00:00:00.000Z"),
|
|
651
|
+
currentPeriodEnd: /* @__PURE__ */ new Date("2026-02-28T00:00:00.000Z")
|
|
652
|
+
});
|
|
653
|
+
assert.equal(booked.billingCycle, "MONTHLY");
|
|
654
|
+
const [readBack] = await repository.listBySubscription(subscriptionId);
|
|
655
|
+
assert.ok(readBack, "the booking must be readable back");
|
|
656
|
+
assert.equal(readBack.billingCycle, "MONTHLY");
|
|
657
|
+
assert.equal(
|
|
658
|
+
readBack.currentPeriodEnd?.toISOString(),
|
|
659
|
+
"2026-02-28T00:00:00.000Z",
|
|
660
|
+
"the period end must survive the round trip"
|
|
661
|
+
);
|
|
662
|
+
assert.equal(readBack.currentPeriodStart?.toISOString(), "2026-02-21T00:00:00.000Z");
|
|
663
|
+
const legacy = await repository.add({
|
|
664
|
+
subscriptionId,
|
|
665
|
+
bundleVersionId,
|
|
666
|
+
startedAt: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
|
|
667
|
+
minimumTermEndsAt: null
|
|
668
|
+
});
|
|
669
|
+
const legacyReadBack = await repository.findById(legacy.id);
|
|
670
|
+
assert.ok(legacyReadBack, "the legacy booking must be readable back");
|
|
671
|
+
assert.equal(legacyReadBack.billingCycle, null);
|
|
672
|
+
assert.equal(legacyReadBack.currentPeriodStart, null);
|
|
673
|
+
assert.equal(legacyReadBack.currentPeriodEnd, null);
|
|
674
|
+
});
|
|
675
|
+
test("a second cancellation of one booking is refused, not applied", async (t) => {
|
|
676
|
+
const repository = harness.adapter.subscriptionBundleRepository;
|
|
677
|
+
const { seed } = harness;
|
|
678
|
+
if (!repository || !seed.createBundleVersion) {
|
|
679
|
+
missing(t, "bundleBookings");
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
const { planVersionId } = await seed.createPlanVersion({
|
|
683
|
+
planKey: "PRO",
|
|
684
|
+
version: 1,
|
|
685
|
+
quotas: {},
|
|
686
|
+
features: ["CORE"],
|
|
687
|
+
published: true
|
|
688
|
+
});
|
|
689
|
+
const { subscriptionId } = await seed.createSubscription({
|
|
690
|
+
tenantId: "tenant-double-cancel",
|
|
691
|
+
plan: "PRO",
|
|
692
|
+
planVersionId
|
|
693
|
+
});
|
|
694
|
+
const { bundleVersionId } = await seed.createBundleVersion({
|
|
695
|
+
bundleKey: "ANALYTICS",
|
|
696
|
+
features: ["REPORTS"]
|
|
697
|
+
});
|
|
698
|
+
const booking = await repository.add({
|
|
699
|
+
subscriptionId,
|
|
700
|
+
bundleVersionId,
|
|
701
|
+
startedAt: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
|
|
702
|
+
minimumTermEndsAt: null
|
|
703
|
+
});
|
|
704
|
+
const first = await repository.cancel(booking.id, {
|
|
705
|
+
canceledAt: /* @__PURE__ */ new Date("2026-03-01T00:00:00.000Z"),
|
|
706
|
+
canceledEffectiveAt: /* @__PURE__ */ new Date("2026-04-01T00:00:00.000Z")
|
|
707
|
+
});
|
|
708
|
+
assert.equal(first.canceledEffectiveAt?.toISOString(), "2026-04-01T00:00:00.000Z");
|
|
709
|
+
await assert.rejects(
|
|
710
|
+
() => repository.cancel(booking.id, {
|
|
711
|
+
canceledAt: /* @__PURE__ */ new Date("2026-03-02T00:00:00.000Z"),
|
|
712
|
+
canceledEffectiveAt: /* @__PURE__ */ new Date("2026-09-01T00:00:00.000Z")
|
|
713
|
+
}),
|
|
714
|
+
"a second cancellation must be refused"
|
|
715
|
+
);
|
|
716
|
+
const readBack = await repository.findById(booking.id);
|
|
717
|
+
assert.equal(
|
|
718
|
+
readBack?.canceledEffectiveAt?.toISOString(),
|
|
719
|
+
"2026-04-01T00:00:00.000Z",
|
|
720
|
+
"the first cancellation must still stand"
|
|
721
|
+
);
|
|
722
|
+
await repository.reactivate(booking.id);
|
|
723
|
+
const again = await repository.cancel(booking.id, {
|
|
724
|
+
canceledAt: /* @__PURE__ */ new Date("2026-03-02T00:00:00.000Z"),
|
|
725
|
+
canceledEffectiveAt: /* @__PURE__ */ new Date("2026-09-01T00:00:00.000Z")
|
|
726
|
+
});
|
|
727
|
+
assert.equal(again.canceledEffectiveAt?.toISOString(), "2026-09-01T00:00:00.000Z");
|
|
728
|
+
});
|
|
729
|
+
test("a subscription's bookings come back newest first", async (t) => {
|
|
730
|
+
const repository = harness.adapter.subscriptionBundleRepository;
|
|
731
|
+
const { seed } = harness;
|
|
732
|
+
if (!repository || !seed.createBundleVersion) {
|
|
733
|
+
missing(t, "bundleBookings");
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
const { planVersionId } = await seed.createPlanVersion({
|
|
737
|
+
planKey: "PRO",
|
|
738
|
+
version: 1,
|
|
739
|
+
quotas: {},
|
|
740
|
+
features: ["CORE"],
|
|
741
|
+
published: true
|
|
742
|
+
});
|
|
743
|
+
const { subscriptionId } = await seed.createSubscription({
|
|
744
|
+
tenantId: "tenant-booking-order",
|
|
745
|
+
plan: "PRO",
|
|
746
|
+
planVersionId
|
|
747
|
+
});
|
|
748
|
+
for (const [key, startedAt] of [
|
|
749
|
+
["OLDEST", "2026-01-01T00:00:00.000Z"],
|
|
750
|
+
["MIDDLE", "2026-02-01T00:00:00.000Z"],
|
|
751
|
+
["NEWEST", "2026-03-01T00:00:00.000Z"]
|
|
752
|
+
]) {
|
|
753
|
+
const { bundleVersionId } = await seed.createBundleVersion({
|
|
754
|
+
bundleKey: key,
|
|
755
|
+
features: ["REPORTS"]
|
|
756
|
+
});
|
|
757
|
+
await repository.add({
|
|
758
|
+
subscriptionId,
|
|
759
|
+
bundleVersionId,
|
|
760
|
+
startedAt: new Date(startedAt),
|
|
761
|
+
minimumTermEndsAt: null
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
const listed = await repository.listBySubscription(subscriptionId);
|
|
765
|
+
assert.deepEqual(
|
|
766
|
+
listed.map((row) => row.startedAt.toISOString()),
|
|
767
|
+
[
|
|
768
|
+
"2026-03-01T00:00:00.000Z",
|
|
769
|
+
"2026-02-01T00:00:00.000Z",
|
|
770
|
+
"2026-01-01T00:00:00.000Z"
|
|
771
|
+
]
|
|
772
|
+
);
|
|
773
|
+
});
|
|
774
|
+
test("a booking with no request date is active, whatever its effective date says", async (t) => {
|
|
775
|
+
const repository = harness.adapter.subscriptionBundleRepository;
|
|
776
|
+
const { seed } = harness;
|
|
777
|
+
if (!repository || !seed.createBundleVersion) {
|
|
778
|
+
missing(t, "bundleBookings");
|
|
779
|
+
return;
|
|
780
|
+
}
|
|
781
|
+
const { planVersionId } = await seed.createPlanVersion({
|
|
782
|
+
planKey: "PRO",
|
|
783
|
+
version: 1,
|
|
784
|
+
quotas: {},
|
|
785
|
+
features: ["CORE"],
|
|
786
|
+
published: true
|
|
787
|
+
});
|
|
788
|
+
const { subscriptionId } = await seed.createSubscription({
|
|
789
|
+
tenantId: "tenant-half-cancelled",
|
|
790
|
+
plan: "PRO",
|
|
791
|
+
planVersionId
|
|
792
|
+
});
|
|
793
|
+
const { bundleVersionId } = await seed.createBundleVersion({
|
|
794
|
+
bundleKey: "ANALYTICS",
|
|
795
|
+
features: ["REPORTS"]
|
|
796
|
+
});
|
|
797
|
+
const booking = await repository.add({
|
|
798
|
+
subscriptionId,
|
|
799
|
+
bundleVersionId,
|
|
800
|
+
startedAt: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
|
|
801
|
+
minimumTermEndsAt: null
|
|
802
|
+
});
|
|
803
|
+
await repository.cancel(booking.id, {
|
|
804
|
+
canceledAt: /* @__PURE__ */ new Date("2026-02-01T00:00:00.000Z"),
|
|
805
|
+
canceledEffectiveAt: /* @__PURE__ */ new Date("2026-03-01T00:00:00.000Z")
|
|
806
|
+
});
|
|
807
|
+
const clearRequestDate = harness.seed.clearBookingRequestDate;
|
|
808
|
+
if (!clearRequestDate) {
|
|
809
|
+
missing(t, "halfCancelledBookingSeed");
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
await clearRequestDate(booking.id);
|
|
813
|
+
const active = await repository.listActiveBySubscription(
|
|
814
|
+
subscriptionId,
|
|
815
|
+
/* @__PURE__ */ new Date("2026-06-01T00:00:00.000Z")
|
|
816
|
+
);
|
|
817
|
+
assert.equal(active.length, 1, "no request date means nobody asked to cancel it");
|
|
818
|
+
assert.equal(
|
|
819
|
+
await repository.countActiveByBundleVersionId(
|
|
820
|
+
bundleVersionId,
|
|
821
|
+
/* @__PURE__ */ new Date("2026-06-01T00:00:00.000Z")
|
|
822
|
+
),
|
|
823
|
+
1
|
|
824
|
+
);
|
|
825
|
+
});
|
|
826
|
+
test("discarding a draft cannot remove a version published meanwhile", async (t) => {
|
|
827
|
+
const catalog = harness.adapter.bundleRepository;
|
|
828
|
+
const discardDraft = catalog?.deleteDraft?.bind(catalog);
|
|
829
|
+
if (!catalog || !discardDraft) {
|
|
830
|
+
missing(t, "bundleDraftDiscard");
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
const bundle = await catalog.create({
|
|
834
|
+
bundleKey: "RACE",
|
|
835
|
+
label: "Race"
|
|
836
|
+
});
|
|
837
|
+
const draft = await catalog.createDraft({
|
|
838
|
+
bundleId: bundle.id,
|
|
839
|
+
features: ["REPORTS"],
|
|
840
|
+
quotas: {}
|
|
841
|
+
});
|
|
842
|
+
await catalog.publishDraft(draft.id, {
|
|
843
|
+
publishedByUserId: null,
|
|
844
|
+
publishedChanges: [],
|
|
845
|
+
nonRegressive: true,
|
|
846
|
+
validFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
|
|
847
|
+
validUntil: null
|
|
848
|
+
});
|
|
849
|
+
await assert.rejects(
|
|
850
|
+
() => discardDraft(draft.id),
|
|
851
|
+
"a published version must not be discardable"
|
|
852
|
+
);
|
|
853
|
+
assert.ok(
|
|
854
|
+
await catalog.findVersionById(draft.id),
|
|
855
|
+
"and it must still be there afterwards"
|
|
856
|
+
);
|
|
857
|
+
});
|
|
858
|
+
test("publishing one draft twice claims it once, and the windows stay adjacent", async (t) => {
|
|
859
|
+
const catalog = harness.adapter.bundleRepository;
|
|
860
|
+
const publish = catalog?.publishDraft?.bind(catalog);
|
|
861
|
+
if (!catalog || !publish) {
|
|
862
|
+
missing(t, "bundleDraftPublish");
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
const bundle = await catalog.create({
|
|
866
|
+
bundleKey: "CLAIM",
|
|
867
|
+
label: "Claim"
|
|
868
|
+
});
|
|
869
|
+
const first = await catalog.createDraft({
|
|
870
|
+
bundleId: bundle.id,
|
|
871
|
+
features: ["A"],
|
|
872
|
+
quotas: {}
|
|
873
|
+
});
|
|
874
|
+
await publish(first.id, {
|
|
875
|
+
publishedByUserId: null,
|
|
876
|
+
publishedChanges: [],
|
|
877
|
+
nonRegressive: true,
|
|
878
|
+
validFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
|
|
879
|
+
validUntil: null
|
|
880
|
+
});
|
|
881
|
+
const second = await catalog.createDraft({
|
|
882
|
+
bundleId: bundle.id,
|
|
883
|
+
baseVersionId: first.id,
|
|
884
|
+
features: ["A", "B"],
|
|
885
|
+
quotas: {}
|
|
886
|
+
});
|
|
887
|
+
const publishedAt = /* @__PURE__ */ new Date("2026-03-01T00:00:00.000Z");
|
|
888
|
+
await publish(second.id, {
|
|
889
|
+
publishedByUserId: null,
|
|
890
|
+
publishedChanges: [],
|
|
891
|
+
nonRegressive: true,
|
|
892
|
+
validFrom: publishedAt,
|
|
893
|
+
validUntil: null
|
|
894
|
+
});
|
|
895
|
+
await assert.rejects(
|
|
896
|
+
() => publish(second.id, {
|
|
897
|
+
publishedByUserId: null,
|
|
898
|
+
publishedChanges: [],
|
|
899
|
+
nonRegressive: true,
|
|
900
|
+
validFrom: /* @__PURE__ */ new Date("2026-06-01T00:00:00.000Z"),
|
|
901
|
+
validUntil: null
|
|
902
|
+
}),
|
|
903
|
+
"a version that is already published must not be published again"
|
|
904
|
+
);
|
|
905
|
+
const successor = await catalog.findVersionById(second.id);
|
|
906
|
+
const predecessor = await catalog.findVersionById(first.id);
|
|
907
|
+
assert.equal(
|
|
908
|
+
successor?.validFrom && new Date(successor.validFrom).toISOString(),
|
|
909
|
+
publishedAt.toISOString(),
|
|
910
|
+
"the winning date must still stand"
|
|
911
|
+
);
|
|
912
|
+
assert.ok(predecessor?.supersededAt, "the predecessor must be superseded");
|
|
913
|
+
if (predecessor?.validUntil) {
|
|
914
|
+
const closesAt = new Date(predecessor.validUntil);
|
|
915
|
+
assert.equal(
|
|
916
|
+
closesAt.toISOString().slice(0, 10),
|
|
917
|
+
"2026-02-28",
|
|
918
|
+
"the predecessor must close the day before its successor opens"
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
});
|
|
922
|
+
test("a plan key names one plan for the whole installation", async (t) => {
|
|
923
|
+
const repository = harness.adapter.planRepository;
|
|
924
|
+
if (!repository) {
|
|
925
|
+
missing(t, "planRepository");
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
await repository.create({ planKey: "DOUBLE", label: "First" });
|
|
929
|
+
await assert.rejects(
|
|
930
|
+
() => repository.create({ planKey: "DOUBLE", label: "Second" }),
|
|
931
|
+
"a plan key is taken once"
|
|
932
|
+
);
|
|
933
|
+
});
|
|
934
|
+
test("a bundle key names one bundle for the whole installation", async (t) => {
|
|
935
|
+
const catalog = harness.adapter.bundleRepository;
|
|
936
|
+
if (!catalog) {
|
|
937
|
+
missing(t, "bundleRepository");
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
await catalog.create({ bundleKey: "DOUBLE", label: "First" });
|
|
941
|
+
await assert.rejects(
|
|
942
|
+
() => catalog.create({ bundleKey: "DOUBLE", label: "Second" }),
|
|
943
|
+
"a bundle key is taken once"
|
|
944
|
+
);
|
|
945
|
+
});
|
|
946
|
+
test("a retired plan still occupies its key", async (t) => {
|
|
947
|
+
const repository = harness.adapter.planRepository;
|
|
948
|
+
const retire = repository?.softDelete?.bind(repository);
|
|
949
|
+
const byKey = repository?.findByKey?.bind(repository);
|
|
950
|
+
if (!repository || !retire || !byKey) {
|
|
951
|
+
missing(t, "planRetirement");
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
const plan = await repository.create({ planKey: "RETIRED_PLAN", label: "Retired" });
|
|
955
|
+
await retire(plan.id);
|
|
956
|
+
const stillThere = await byKey("RETIRED_PLAN");
|
|
957
|
+
assert.equal(stillThere?.id, plan.id, "the key is not free again");
|
|
958
|
+
assert.ok(stillThere?.deletedAt, "and the row says it is retired");
|
|
959
|
+
assert.equal(
|
|
960
|
+
(await repository.list({})).some((row) => row.id === plan.id),
|
|
961
|
+
false,
|
|
962
|
+
"a retired plan is not in the catalogue an operator browses"
|
|
963
|
+
);
|
|
964
|
+
});
|
|
965
|
+
test("a plan key no plan has finds no versions, rather than failing", async (t) => {
|
|
966
|
+
const repository = harness.adapter.planRepository;
|
|
967
|
+
if (!repository?.listVersions || !repository.findCurrentDraft || !repository.findLatestLivePlanVersion) {
|
|
968
|
+
missing(t, "planVersionReads");
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
const entitlementVersions = harness.adapter.planVersionRepository;
|
|
972
|
+
assert.deepEqual(await repository.listVersions("NO_SUCH_PLAN"), []);
|
|
973
|
+
assert.equal(await repository.findCurrentDraft("NO_SUCH_PLAN"), null);
|
|
974
|
+
assert.equal(await repository.findLatestLivePlanVersion("NO_SUCH_PLAN"), null);
|
|
975
|
+
if (repository.findActivePlanVersion) {
|
|
976
|
+
assert.equal(
|
|
977
|
+
await repository.findActivePlanVersion("NO_SUCH_PLAN", /* @__PURE__ */ new Date()),
|
|
978
|
+
null
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
assert.equal(await entitlementVersions.findLatestLive("NO_SUCH_PLAN"), null);
|
|
982
|
+
if (entitlementVersions.findActive) {
|
|
983
|
+
assert.equal(
|
|
984
|
+
await entitlementVersions.findActive("NO_SUCH_PLAN", /* @__PURE__ */ new Date()),
|
|
985
|
+
null
|
|
986
|
+
);
|
|
987
|
+
}
|
|
988
|
+
});
|
|
989
|
+
test("retiring a plan hides none of its versions", async (t) => {
|
|
990
|
+
const repository = harness.adapter.planRepository;
|
|
991
|
+
if (!repository?.createPlanVersionDraft || !repository.publishPlanVersionDraft || !repository.listVersions || !repository.findCurrentDraft || !repository.findLatestLivePlanVersion || !repository.softDelete) {
|
|
992
|
+
missing(t, "planVersionRetirement");
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
const listVersions = repository.listVersions.bind(repository);
|
|
996
|
+
const findCurrentDraft = repository.findCurrentDraft.bind(repository);
|
|
997
|
+
const findLatestLive = repository.findLatestLivePlanVersion.bind(repository);
|
|
998
|
+
const entitlementVersions = harness.adapter.planVersionRepository;
|
|
999
|
+
const plan = await repository.create({ planKey: "RETIRING", label: "Retiring" });
|
|
1000
|
+
const firstDraft = await repository.createPlanVersionDraft({
|
|
1001
|
+
planId: "RETIRING",
|
|
1002
|
+
features: ["CORE"],
|
|
1003
|
+
quotas: { users: 5 },
|
|
1004
|
+
monthlyNet: "10.00",
|
|
1005
|
+
yearlyNet: "100.00",
|
|
1006
|
+
validFrom: "2026-01-01"
|
|
1007
|
+
});
|
|
1008
|
+
const live = await repository.publishPlanVersionDraft(firstDraft.id, {
|
|
1009
|
+
publishedByUserId: null,
|
|
1010
|
+
publishedChanges: [],
|
|
1011
|
+
nonRegressive: true,
|
|
1012
|
+
validFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
|
|
1013
|
+
validUntil: null
|
|
1014
|
+
});
|
|
1015
|
+
const openDraft = await repository.createPlanVersionDraft({
|
|
1016
|
+
planId: "RETIRING",
|
|
1017
|
+
baseVersionId: live.id,
|
|
1018
|
+
features: ["CORE", "PLUS"],
|
|
1019
|
+
quotas: { users: 10 },
|
|
1020
|
+
monthlyNet: "15.00",
|
|
1021
|
+
yearlyNet: "150.00",
|
|
1022
|
+
validFrom: "2026-06-01"
|
|
1023
|
+
});
|
|
1024
|
+
const reads = async () => ({
|
|
1025
|
+
versions: (await listVersions("RETIRING")).map((row) => row.id),
|
|
1026
|
+
draft: (await findCurrentDraft("RETIRING"))?.id ?? null,
|
|
1027
|
+
latestLive: (await findLatestLive("RETIRING"))?.id ?? null,
|
|
1028
|
+
entitlementFeatures: (await entitlementVersions.findLatestLive("RETIRING"))?.features ?? null
|
|
1029
|
+
});
|
|
1030
|
+
const beforeRetiring = await reads();
|
|
1031
|
+
assert.deepEqual(
|
|
1032
|
+
{
|
|
1033
|
+
versions: beforeRetiring.versions,
|
|
1034
|
+
draft: beforeRetiring.draft,
|
|
1035
|
+
latestLive: beforeRetiring.latestLive
|
|
1036
|
+
},
|
|
1037
|
+
{ versions: [live.id, openDraft.id], draft: openDraft.id, latestLive: live.id },
|
|
1038
|
+
"a live plan reads its versions, so the comparison below has a subject"
|
|
1039
|
+
);
|
|
1040
|
+
await repository.softDelete(plan.id);
|
|
1041
|
+
assert.deepEqual(await reads(), beforeRetiring, "retiring the plan hid its versions");
|
|
1042
|
+
});
|
|
1043
|
+
test("a retired bundle still occupies its key", async (t) => {
|
|
1044
|
+
const catalog = harness.adapter.bundleRepository;
|
|
1045
|
+
const retire = catalog?.softDelete?.bind(catalog);
|
|
1046
|
+
const byKey = catalog?.findByKey?.bind(catalog);
|
|
1047
|
+
if (!catalog || !retire || !byKey) {
|
|
1048
|
+
missing(t, "bundleRetirement");
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
const bundle = await catalog.create({
|
|
1052
|
+
bundleKey: "RETIRED_KEY",
|
|
1053
|
+
label: "Retired"
|
|
1054
|
+
});
|
|
1055
|
+
assert.equal((await byKey("RETIRED_KEY"))?.id, bundle.id);
|
|
1056
|
+
await retire(bundle.id);
|
|
1057
|
+
const stillThere = await byKey("RETIRED_KEY");
|
|
1058
|
+
assert.equal(stillThere?.id, bundle.id, "the key is not free again");
|
|
1059
|
+
assert.ok(stillThere?.deletedAt, "and the row says it is retired");
|
|
1060
|
+
const listed = await catalog.list({});
|
|
1061
|
+
assert.equal(
|
|
1062
|
+
listed.some((row) => row.id === bundle.id),
|
|
1063
|
+
false,
|
|
1064
|
+
"a retired bundle is not in the catalogue an operator browses"
|
|
1065
|
+
);
|
|
1066
|
+
});
|
|
337
1067
|
test("countByPlanVersionId counts current AND pending bindings in one query", async (t) => {
|
|
338
1068
|
const { seed, adapter } = harness;
|
|
339
1069
|
if (!adapter.subscriptionRepository.countByPlanVersionId) {
|
|
340
|
-
t
|
|
1070
|
+
missing(t, "countByPlanVersionId");
|
|
341
1071
|
return;
|
|
342
1072
|
}
|
|
343
1073
|
const v1 = await seed.createPlanVersion({
|
|
@@ -377,9 +1107,7 @@ function persistenceAdapterContract(options) {
|
|
|
377
1107
|
return;
|
|
378
1108
|
}
|
|
379
1109
|
if (!adapter.promoCodeRedemptionRepository) {
|
|
380
|
-
t
|
|
381
|
-
"adapter provides no PromoCodeRedemptionRepository (needed as tx write probe)"
|
|
382
|
-
);
|
|
1110
|
+
missing(t, "promoCodeRedemptions");
|
|
383
1111
|
return;
|
|
384
1112
|
}
|
|
385
1113
|
const redemptions = adapter.promoCodeRedemptionRepository;
|
|
@@ -462,7 +1190,7 @@ function persistenceAdapterContract(options) {
|
|
|
462
1190
|
test("concurrent claimSlot grants exactly maxRedemptions slots", async (t) => {
|
|
463
1191
|
const { seed, adapter } = harness;
|
|
464
1192
|
if (!adapter.promoCodeRepository) {
|
|
465
|
-
t
|
|
1193
|
+
missing(t, "promoCodes");
|
|
466
1194
|
return;
|
|
467
1195
|
}
|
|
468
1196
|
const promoCodes = adapter.promoCodeRepository;
|
|
@@ -483,7 +1211,7 @@ function persistenceAdapterContract(options) {
|
|
|
483
1211
|
test("claimSlot / markExhaustedIfFull / releaseSlot lifecycle", async (t) => {
|
|
484
1212
|
const { seed, adapter } = harness;
|
|
485
1213
|
if (!adapter.promoCodeRepository) {
|
|
486
|
-
t
|
|
1214
|
+
missing(t, "promoCodes");
|
|
487
1215
|
return;
|
|
488
1216
|
}
|
|
489
1217
|
const promoCodes = adapter.promoCodeRepository;
|
|
@@ -503,7 +1231,7 @@ function persistenceAdapterContract(options) {
|
|
|
503
1231
|
test("a subscription cannot redeem twice (unique guard)", async (t) => {
|
|
504
1232
|
const { seed, adapter } = harness;
|
|
505
1233
|
if (!adapter.promoCodeRedemptionRepository) {
|
|
506
|
-
t
|
|
1234
|
+
missing(t, "promoCodeRedemptions");
|
|
507
1235
|
return;
|
|
508
1236
|
}
|
|
509
1237
|
const redemptions = adapter.promoCodeRedemptionRepository;
|
|
@@ -543,7 +1271,7 @@ function persistenceAdapterContract(options) {
|
|
|
543
1271
|
test("audit write \u2192 query roundtrip with actorTag filters", async (t) => {
|
|
544
1272
|
const { adapter } = harness;
|
|
545
1273
|
if (!adapter.audit || !adapter.auditQuery) {
|
|
546
|
-
t
|
|
1274
|
+
missing(t, "audit");
|
|
547
1275
|
return;
|
|
548
1276
|
}
|
|
549
1277
|
await adapter.audit.write({
|
|
@@ -583,7 +1311,7 @@ function persistenceAdapterContract(options) {
|
|
|
583
1311
|
test("MFA secret roundtrip", async (t) => {
|
|
584
1312
|
const { adapter } = harness;
|
|
585
1313
|
if (!adapter.mfa) {
|
|
586
|
-
t
|
|
1314
|
+
missing(t, "mfa");
|
|
587
1315
|
return;
|
|
588
1316
|
}
|
|
589
1317
|
assert.equal(await adapter.mfa.getSecret("admin-1"), null);
|
|
@@ -598,7 +1326,7 @@ function persistenceAdapterContract(options) {
|
|
|
598
1326
|
test("finds the subscription the id names, not merely a subscription", async (t) => {
|
|
599
1327
|
const { adapter, seed } = harness;
|
|
600
1328
|
if (!adapter.promoSubscriptionLookup) {
|
|
601
|
-
t
|
|
1329
|
+
missing(t, "promoSubscriptionLookup");
|
|
602
1330
|
return;
|
|
603
1331
|
}
|
|
604
1332
|
const { planVersionId } = await seed.createPlanVersion({
|
|
@@ -627,7 +1355,7 @@ function persistenceAdapterContract(options) {
|
|
|
627
1355
|
test("returns null for an id that does not exist", async (t) => {
|
|
628
1356
|
const { adapter, seed } = harness;
|
|
629
1357
|
if (!adapter.promoSubscriptionLookup) {
|
|
630
|
-
t
|
|
1358
|
+
missing(t, "promoSubscriptionLookup");
|
|
631
1359
|
return;
|
|
632
1360
|
}
|
|
633
1361
|
const { planVersionId } = await seed.createPlanVersion({
|
|
@@ -648,7 +1376,7 @@ function persistenceAdapterContract(options) {
|
|
|
648
1376
|
test("carries the fields a promo rule reads: cycle and start date", async (t) => {
|
|
649
1377
|
const { adapter, seed } = harness;
|
|
650
1378
|
if (!adapter.promoSubscriptionLookup) {
|
|
651
|
-
t
|
|
1379
|
+
missing(t, "promoSubscriptionLookup");
|
|
652
1380
|
return;
|
|
653
1381
|
}
|
|
654
1382
|
const { planVersionId } = await seed.createPlanVersion({
|
|
@@ -683,7 +1411,7 @@ function persistenceAdapterContract(options) {
|
|
|
683
1411
|
test("reads inside a transaction, so validation and redemption agree", async (t) => {
|
|
684
1412
|
const { adapter, seed } = harness;
|
|
685
1413
|
if (!adapter.promoSubscriptionLookup) {
|
|
686
|
-
t
|
|
1414
|
+
missing(t, "promoSubscriptionLookup");
|
|
687
1415
|
return;
|
|
688
1416
|
}
|
|
689
1417
|
const { planVersionId } = await seed.createPlanVersion({
|
|
@@ -704,13 +1432,1584 @@ function persistenceAdapterContract(options) {
|
|
|
704
1432
|
assert.equal(seen?.id, subscriptionId);
|
|
705
1433
|
assert.equal(seen?.tenantId, "tenant-a");
|
|
706
1434
|
});
|
|
707
|
-
test("
|
|
708
|
-
|
|
709
|
-
|
|
1435
|
+
test("a contract keeps what was agreed, and ending it does not rewrite it", async (t) => {
|
|
1436
|
+
const contracts = harness.adapter.subscriptionContractRepository;
|
|
1437
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
1438
|
+
if (!contracts || !createSubscriber) {
|
|
1439
|
+
missing(t, "subscriptionContracts");
|
|
710
1440
|
return;
|
|
711
1441
|
}
|
|
712
|
-
|
|
713
|
-
|
|
1442
|
+
const tenantId = "tenant-contract-lifecycle";
|
|
1443
|
+
const signedAt = /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z");
|
|
1444
|
+
const { subscriberId } = await createSubscriber({ legalName: "Meier GmbH" });
|
|
1445
|
+
const parties = partiesWith(subscriberId, "Meier GmbH");
|
|
1446
|
+
const created = await contracts.create({
|
|
1447
|
+
tenantId,
|
|
1448
|
+
parties,
|
|
1449
|
+
effectiveFrom: signedAt,
|
|
1450
|
+
priceSnapshot: {
|
|
1451
|
+
currency: "EUR",
|
|
1452
|
+
billingCycle: "monthly",
|
|
1453
|
+
subtotalNet: 29.9,
|
|
1454
|
+
discountNet: 0,
|
|
1455
|
+
totalNet: 29.9,
|
|
1456
|
+
vatRate: 19,
|
|
1457
|
+
totalGross: 35.58
|
|
1458
|
+
},
|
|
1459
|
+
entitlementSnapshot: {
|
|
1460
|
+
plan: "STANDARD",
|
|
1461
|
+
features: ["CORE"],
|
|
1462
|
+
quotas: { users: 5 }
|
|
1463
|
+
},
|
|
1464
|
+
originalBundleVersionIds: ["bundle-version-1"],
|
|
1465
|
+
termsSnapshot: { noticePeriodDays: 30 },
|
|
1466
|
+
lineItems: [
|
|
1467
|
+
{
|
|
1468
|
+
kind: "plan",
|
|
1469
|
+
sourceKey: "STANDARD",
|
|
1470
|
+
sourceVersionId: "plan-version-1",
|
|
1471
|
+
titleSnapshot: "Standard",
|
|
1472
|
+
descriptionSnapshot: "The plan as it was signed",
|
|
1473
|
+
quantity: 1,
|
|
1474
|
+
unit: null,
|
|
1475
|
+
priceNet: 19.9,
|
|
1476
|
+
priceGross: 23.68,
|
|
1477
|
+
billingCycle: "monthly",
|
|
1478
|
+
currency: "EUR",
|
|
1479
|
+
taxRate: 19,
|
|
1480
|
+
taxAmount: 3.78,
|
|
1481
|
+
minimumTermUntil: /* @__PURE__ */ new Date("2027-01-01T00:00:00.000Z"),
|
|
1482
|
+
featuresSnapshot: ["CORE"],
|
|
1483
|
+
quotaEffectsSnapshot: { users: 5 },
|
|
1484
|
+
metadata: { origin: "onboarding" }
|
|
1485
|
+
},
|
|
1486
|
+
{
|
|
1487
|
+
kind: "bundle",
|
|
1488
|
+
sourceKey: "EXTRA-SEATS",
|
|
1489
|
+
sourceVersionId: "bundle-version-1",
|
|
1490
|
+
titleSnapshot: "Extra seats",
|
|
1491
|
+
descriptionSnapshot: null,
|
|
1492
|
+
quantity: 1,
|
|
1493
|
+
unit: "seat",
|
|
1494
|
+
priceNet: 10,
|
|
1495
|
+
priceGross: 11.9,
|
|
1496
|
+
billingCycle: "monthly",
|
|
1497
|
+
currency: "EUR",
|
|
1498
|
+
taxRate: 19,
|
|
1499
|
+
taxAmount: 1.9,
|
|
1500
|
+
minimumTermUntil: null,
|
|
1501
|
+
featuresSnapshot: [],
|
|
1502
|
+
quotaEffectsSnapshot: { users: 5 },
|
|
1503
|
+
metadata: null
|
|
1504
|
+
}
|
|
1505
|
+
]
|
|
1506
|
+
});
|
|
1507
|
+
assert.equal(created.status, "active");
|
|
1508
|
+
assert.equal(created.tenantId, tenantId);
|
|
1509
|
+
assert.equal(created.subscriberId, subscriberId);
|
|
1510
|
+
assert.deepEqual(created.subscriber, parties.subscriber);
|
|
1511
|
+
assert.deepEqual(created.issuer, parties.issuer);
|
|
1512
|
+
assert.equal(created.partiesMigrated, false);
|
|
1513
|
+
assert.equal(created.lineItems.length, 2);
|
|
1514
|
+
assert.deepEqual(created.originalBundleVersionIds, ["bundle-version-1"]);
|
|
1515
|
+
assert.deepEqual(created.termsSnapshot, { noticePeriodDays: 30 });
|
|
1516
|
+
const planLine = created.lineItems.find((item) => item.kind === "plan");
|
|
1517
|
+
assert.ok(planLine, "plan line expected");
|
|
1518
|
+
assert.equal(planLine.priceNet, 19.9, "money must survive the round trip unrounded");
|
|
1519
|
+
assert.equal(planLine.priceGross, 23.68);
|
|
1520
|
+
assert.equal(planLine.billingCycle, "monthly");
|
|
1521
|
+
assert.deepEqual(planLine.quotaEffectsSnapshot, { users: 5 });
|
|
1522
|
+
assert.equal(planLine.descriptionSnapshot, "The plan as it was signed");
|
|
1523
|
+
assert.equal(
|
|
1524
|
+
planLine.minimumTermUntil?.getTime(),
|
|
1525
|
+
(/* @__PURE__ */ new Date("2027-01-01T00:00:00.000Z")).getTime(),
|
|
1526
|
+
"the commitment is part of what was agreed"
|
|
1527
|
+
);
|
|
1528
|
+
assert.deepEqual(planLine.metadata, { origin: "onboarding" });
|
|
1529
|
+
assert.equal(planLine.currency, "EUR", "the line says what it was booked in");
|
|
1530
|
+
assert.equal(planLine.taxRate, 19, "the rate is a recorded fact, not the ratio");
|
|
1531
|
+
assert.equal(planLine.taxAmount, 3.78);
|
|
1532
|
+
assert.equal(
|
|
1533
|
+
Math.round((planLine.priceNet + planLine.taxAmount) * 100) / 100,
|
|
1534
|
+
planLine.priceGross,
|
|
1535
|
+
"the tax closes the gap between net and gross"
|
|
1536
|
+
);
|
|
1537
|
+
const bundleLine = created.lineItems.find((item) => item.kind === "bundle");
|
|
1538
|
+
assert.ok(bundleLine, "bundle line expected");
|
|
1539
|
+
assert.equal(bundleLine.descriptionSnapshot, null);
|
|
1540
|
+
assert.equal(bundleLine.unit, "seat");
|
|
1541
|
+
assert.equal(bundleLine.minimumTermUntil, null);
|
|
1542
|
+
assert.equal(bundleLine.metadata, null);
|
|
1543
|
+
const readBack = await contracts.findById(created.id);
|
|
1544
|
+
assert.ok(readBack, "contract expected by id");
|
|
1545
|
+
assert.deepEqual(readBack.subscriber, parties.subscriber, "the copy is stored");
|
|
1546
|
+
assert.deepEqual(readBack.issuer, parties.issuer);
|
|
1547
|
+
assert.equal(
|
|
1548
|
+
readBack.lineItems.length,
|
|
1549
|
+
2,
|
|
1550
|
+
"lines belong to the contract, not the call"
|
|
1551
|
+
);
|
|
1552
|
+
assert.equal(
|
|
1553
|
+
(await contracts.findActiveByTenantId(
|
|
1554
|
+
tenantId,
|
|
1555
|
+
/* @__PURE__ */ new Date("2026-06-01T00:00:00.000Z")
|
|
1556
|
+
))?.id,
|
|
1557
|
+
created.id
|
|
1558
|
+
);
|
|
1559
|
+
assert.equal(
|
|
1560
|
+
await contracts.findActiveByTenantId(
|
|
1561
|
+
tenantId,
|
|
1562
|
+
/* @__PURE__ */ new Date("2025-12-31T23:59:59.999Z")
|
|
1563
|
+
),
|
|
1564
|
+
null,
|
|
1565
|
+
"a contract is not active before it starts"
|
|
1566
|
+
);
|
|
1567
|
+
const endsAt = /* @__PURE__ */ new Date("2026-07-01T00:00:00.000Z");
|
|
1568
|
+
const terminated = await contracts.terminate(created.id, {
|
|
1569
|
+
effectiveUntil: endsAt,
|
|
1570
|
+
status: null
|
|
1571
|
+
});
|
|
1572
|
+
assert.equal(
|
|
1573
|
+
terminated.status,
|
|
1574
|
+
"active",
|
|
1575
|
+
"a null status leaves the contract in the state it had"
|
|
1576
|
+
);
|
|
1577
|
+
assert.equal(terminated.effectiveUntil?.getTime(), endsAt.getTime());
|
|
1578
|
+
assert.equal(terminated.lineItems.length, 2, "ending a contract keeps its lines");
|
|
1579
|
+
assert.equal(
|
|
1580
|
+
terminated.priceSnapshot.totalNet,
|
|
1581
|
+
created.priceSnapshot.totalNet,
|
|
1582
|
+
"ending a contract does not restate its price"
|
|
1583
|
+
);
|
|
1584
|
+
const afterEnd = await contracts.findById(created.id);
|
|
1585
|
+
assert.ok(afterEnd, "a terminated contract is still readable");
|
|
1586
|
+
assert.equal(afterEnd.lineItems.length, 2);
|
|
1587
|
+
assert.equal(
|
|
1588
|
+
(await contracts.findActiveByTenantId(
|
|
1589
|
+
tenantId,
|
|
1590
|
+
/* @__PURE__ */ new Date("2026-06-30T00:00:00.000Z")
|
|
1591
|
+
))?.id,
|
|
1592
|
+
created.id,
|
|
1593
|
+
"active up to the moment it ends"
|
|
1594
|
+
);
|
|
1595
|
+
assert.equal(
|
|
1596
|
+
await contracts.findActiveByTenantId(tenantId, endsAt),
|
|
1597
|
+
null,
|
|
1598
|
+
"and not at that moment"
|
|
1599
|
+
);
|
|
1600
|
+
});
|
|
1601
|
+
test("a successor takes over without erasing the contract it replaces", async (t) => {
|
|
1602
|
+
const contracts = harness.adapter.subscriptionContractRepository;
|
|
1603
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
1604
|
+
if (!contracts || !createSubscriber) {
|
|
1605
|
+
missing(t, "subscriptionContracts");
|
|
1606
|
+
return;
|
|
1607
|
+
}
|
|
1608
|
+
const tenantId = "tenant-contract-succession";
|
|
1609
|
+
const { subscriberId } = await createSubscriber({ legalName: "Nachfolger KG" });
|
|
1610
|
+
const parties = partiesWith(subscriberId, "Nachfolger KG");
|
|
1611
|
+
const handover = /* @__PURE__ */ new Date("2026-04-01T00:00:00.000Z");
|
|
1612
|
+
const lineAt = (priceNet, priceGross) => ({
|
|
1613
|
+
kind: "plan",
|
|
1614
|
+
sourceKey: "STANDARD",
|
|
1615
|
+
sourceVersionId: null,
|
|
1616
|
+
titleSnapshot: "Standard",
|
|
1617
|
+
descriptionSnapshot: null,
|
|
1618
|
+
quantity: 1,
|
|
1619
|
+
unit: null,
|
|
1620
|
+
priceNet,
|
|
1621
|
+
priceGross,
|
|
1622
|
+
billingCycle: "monthly",
|
|
1623
|
+
currency: "EUR",
|
|
1624
|
+
taxRate: 19,
|
|
1625
|
+
taxAmount: Math.round((priceGross - priceNet) * 100) / 100,
|
|
1626
|
+
minimumTermUntil: null,
|
|
1627
|
+
featuresSnapshot: [],
|
|
1628
|
+
quotaEffectsSnapshot: {},
|
|
1629
|
+
metadata: null
|
|
1630
|
+
});
|
|
1631
|
+
const priceAt = (net, gross) => ({
|
|
1632
|
+
currency: "EUR",
|
|
1633
|
+
billingCycle: "monthly",
|
|
1634
|
+
subtotalNet: net,
|
|
1635
|
+
discountNet: 0,
|
|
1636
|
+
totalNet: net,
|
|
1637
|
+
vatRate: 19,
|
|
1638
|
+
totalGross: gross
|
|
1639
|
+
});
|
|
1640
|
+
const first = await contracts.create({
|
|
1641
|
+
tenantId,
|
|
1642
|
+
parties,
|
|
1643
|
+
effectiveFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
|
|
1644
|
+
priceSnapshot: priceAt(19.9, 23.68),
|
|
1645
|
+
lineItems: [lineAt(19.9, 23.68)]
|
|
1646
|
+
});
|
|
1647
|
+
assert.equal(
|
|
1648
|
+
(await contracts.findActiveByTenantId(
|
|
1649
|
+
tenantId,
|
|
1650
|
+
/* @__PURE__ */ new Date("2026-03-31T00:00:00.000Z")
|
|
1651
|
+
))?.id,
|
|
1652
|
+
first.id
|
|
1653
|
+
);
|
|
1654
|
+
await contracts.terminate(first.id, {
|
|
1655
|
+
effectiveUntil: handover,
|
|
1656
|
+
status: "superseded"
|
|
1657
|
+
});
|
|
1658
|
+
const second = await contracts.create({
|
|
1659
|
+
tenantId,
|
|
1660
|
+
parties,
|
|
1661
|
+
effectiveFrom: handover,
|
|
1662
|
+
priceSnapshot: priceAt(24.9, 29.63),
|
|
1663
|
+
lineItems: [lineAt(24.9, 29.63)]
|
|
1664
|
+
});
|
|
1665
|
+
assert.equal(
|
|
1666
|
+
(await contracts.findActiveByTenantId(tenantId, handover))?.id,
|
|
1667
|
+
second.id,
|
|
1668
|
+
"the successor takes over at the moment the predecessor ends"
|
|
1669
|
+
);
|
|
1670
|
+
assert.equal(
|
|
1671
|
+
await contracts.findActiveByTenantId(
|
|
1672
|
+
tenantId,
|
|
1673
|
+
/* @__PURE__ */ new Date("2026-03-31T00:00:00.000Z")
|
|
1674
|
+
),
|
|
1675
|
+
null,
|
|
1676
|
+
"a superseded contract is not live at any asOf"
|
|
1677
|
+
);
|
|
1678
|
+
const superseded = await contracts.findById(first.id);
|
|
1679
|
+
assert.equal(superseded?.status, "superseded");
|
|
1680
|
+
assert.equal(
|
|
1681
|
+
superseded?.priceSnapshot.totalNet,
|
|
1682
|
+
19.9,
|
|
1683
|
+
"the replaced contract keeps the price it was signed at"
|
|
1684
|
+
);
|
|
1685
|
+
const history = await contracts.list({ tenantId });
|
|
1686
|
+
assert.equal(history.length, 2, "both contracts remain in the history");
|
|
1687
|
+
assert.deepEqual(
|
|
1688
|
+
history.map((contract) => contract.lineItems.map((item) => item.priceNet)),
|
|
1689
|
+
[[24.9], [19.9]]
|
|
1690
|
+
);
|
|
1691
|
+
assert.deepEqual(
|
|
1692
|
+
history.map((contract) => contract.id),
|
|
1693
|
+
[second.id, first.id],
|
|
1694
|
+
"newest first"
|
|
1695
|
+
);
|
|
1696
|
+
assert.deepEqual(
|
|
1697
|
+
(await contracts.list({ tenantId, asOf: /* @__PURE__ */ new Date("2026-03-31T00:00:00.000Z") })).map((contract) => contract.id),
|
|
1698
|
+
[first.id],
|
|
1699
|
+
"asOf narrows the history to what was in force then"
|
|
1700
|
+
);
|
|
1701
|
+
assert.deepEqual(
|
|
1702
|
+
(await contracts.list({ tenantId, status: "superseded" })).map(
|
|
1703
|
+
(contract) => contract.id
|
|
1704
|
+
),
|
|
1705
|
+
[first.id]
|
|
1706
|
+
);
|
|
1707
|
+
});
|
|
1708
|
+
test("a line keeps the currency and the tax it was booked with", async (t) => {
|
|
1709
|
+
const contracts = harness.adapter.subscriptionContractRepository;
|
|
1710
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
1711
|
+
if (!contracts || !createSubscriber) {
|
|
1712
|
+
missing(t, "subscriptionContracts");
|
|
1713
|
+
return;
|
|
1714
|
+
}
|
|
1715
|
+
const tenantId = "tenant-contract-money-facts";
|
|
1716
|
+
const { subscriberId } = await createSubscriber({ legalName: "Z\xFCrich AG" });
|
|
1717
|
+
const parties = { ...partiesWith(subscriberId, "Z\xFCrich AG"), issuer: null };
|
|
1718
|
+
const created = await contracts.create({
|
|
1719
|
+
tenantId,
|
|
1720
|
+
parties,
|
|
1721
|
+
effectiveFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z"),
|
|
1722
|
+
priceSnapshot: {
|
|
1723
|
+
currency: "CHF",
|
|
1724
|
+
billingCycle: "monthly",
|
|
1725
|
+
subtotalNet: 100,
|
|
1726
|
+
discountNet: 10,
|
|
1727
|
+
totalNet: 90,
|
|
1728
|
+
vatRate: 8.1,
|
|
1729
|
+
totalGross: 97.29
|
|
1730
|
+
},
|
|
1731
|
+
lineItems: [
|
|
1732
|
+
{
|
|
1733
|
+
kind: "plan",
|
|
1734
|
+
sourceKey: "STANDARD",
|
|
1735
|
+
sourceVersionId: null,
|
|
1736
|
+
titleSnapshot: "Standard",
|
|
1737
|
+
descriptionSnapshot: null,
|
|
1738
|
+
quantity: 1,
|
|
1739
|
+
unit: null,
|
|
1740
|
+
priceNet: 100,
|
|
1741
|
+
priceGross: 108.1,
|
|
1742
|
+
billingCycle: "monthly",
|
|
1743
|
+
// Not the installation this suite's other contracts
|
|
1744
|
+
// run under, and not a whole number of per cent —
|
|
1745
|
+
// a rate stored as an integer, or a currency taken
|
|
1746
|
+
// from a default, passes every other case here.
|
|
1747
|
+
currency: "CHF",
|
|
1748
|
+
taxRate: 8.1,
|
|
1749
|
+
taxAmount: 8.1,
|
|
1750
|
+
minimumTermUntil: null,
|
|
1751
|
+
featuresSnapshot: [],
|
|
1752
|
+
quotaEffectsSnapshot: {},
|
|
1753
|
+
metadata: null
|
|
1754
|
+
},
|
|
1755
|
+
{
|
|
1756
|
+
kind: "discount",
|
|
1757
|
+
sourceKey: "WELCOME10",
|
|
1758
|
+
sourceVersionId: null,
|
|
1759
|
+
titleSnapshot: "Welcome discount",
|
|
1760
|
+
descriptionSnapshot: null,
|
|
1761
|
+
quantity: 1,
|
|
1762
|
+
unit: null,
|
|
1763
|
+
priceNet: -10,
|
|
1764
|
+
priceGross: -10.81,
|
|
1765
|
+
billingCycle: "monthly",
|
|
1766
|
+
currency: "CHF",
|
|
1767
|
+
taxRate: 8.1,
|
|
1768
|
+
taxAmount: -0.81,
|
|
1769
|
+
minimumTermUntil: null,
|
|
1770
|
+
featuresSnapshot: [],
|
|
1771
|
+
quotaEffectsSnapshot: {},
|
|
1772
|
+
metadata: null
|
|
1773
|
+
}
|
|
1774
|
+
]
|
|
1775
|
+
});
|
|
1776
|
+
const read = await contracts.findById(created.id);
|
|
1777
|
+
const plan = read?.lineItems.find((item) => item.kind === "plan");
|
|
1778
|
+
const discount = read?.lineItems.find((item) => item.kind === "discount");
|
|
1779
|
+
assert.ok(plan && discount, "both lines expected");
|
|
1780
|
+
assert.equal(plan.currency, "CHF");
|
|
1781
|
+
assert.equal(plan.taxRate, 8.1, "a fractional rate survives the column");
|
|
1782
|
+
assert.equal(plan.taxAmount, 8.1);
|
|
1783
|
+
assert.equal(discount.currency, "CHF");
|
|
1784
|
+
assert.equal(discount.taxRate, 8.1);
|
|
1785
|
+
assert.equal(discount.taxAmount, -0.81);
|
|
1786
|
+
for (const line of [plan, discount]) {
|
|
1787
|
+
assert.equal(
|
|
1788
|
+
Math.round((line.priceNet + line.taxAmount) * 100) / 100,
|
|
1789
|
+
line.priceGross,
|
|
1790
|
+
"net plus tax is gross, on every line"
|
|
1791
|
+
);
|
|
1792
|
+
}
|
|
1793
|
+
});
|
|
1794
|
+
test("a contract written on a transaction is undone with it, and found by its offer", async (t) => {
|
|
1795
|
+
const { adapter, seed } = harness;
|
|
1796
|
+
const contracts = adapter.subscriptionContractRepository;
|
|
1797
|
+
if (!contracts || !seed.createSubscriber) {
|
|
1798
|
+
missing(t, "subscriptionContracts");
|
|
1799
|
+
return;
|
|
1800
|
+
}
|
|
1801
|
+
if (!adapter.capabilities.transactions) {
|
|
1802
|
+
t.skip("adapter declares no transaction capability");
|
|
1803
|
+
return;
|
|
1804
|
+
}
|
|
1805
|
+
const { subscriberId } = await seed.createSubscriber({ legalName: "Angebot GmbH" });
|
|
1806
|
+
const parties = partiesWith(subscriberId, "Angebot GmbH");
|
|
1807
|
+
await assert.rejects(
|
|
1808
|
+
adapter.transactionRunner.run(async (tx) => {
|
|
1809
|
+
await contracts.create(contractFromOffer("offer-rolled-back", parties), tx);
|
|
1810
|
+
throw new Error("the conclusion fails after the contract is written");
|
|
1811
|
+
})
|
|
1812
|
+
);
|
|
1813
|
+
assert.equal(
|
|
1814
|
+
await contracts.findByOriginalOfferId("offer-rolled-back"),
|
|
1815
|
+
null,
|
|
1816
|
+
"the contract outlived its transaction"
|
|
1817
|
+
);
|
|
1818
|
+
const kept = await adapter.transactionRunner.run(
|
|
1819
|
+
(tx) => contracts.create(contractFromOffer("offer-kept", parties), tx)
|
|
1820
|
+
);
|
|
1821
|
+
const found = await contracts.findByOriginalOfferId("offer-kept");
|
|
1822
|
+
assert.equal(found?.id, kept.id);
|
|
1823
|
+
assert.equal(found?.lineItems.length, 1, "with its lines");
|
|
1824
|
+
assert.equal(await contracts.findByOriginalOfferId("offer-nobody-concluded"), null);
|
|
1825
|
+
});
|
|
1826
|
+
test("a subscriber is created live for its tenant, and numbered by the database", async (t) => {
|
|
1827
|
+
const subscribers = harness.adapter.subscriberRepository;
|
|
1828
|
+
if (!subscribers) {
|
|
1829
|
+
missing(t, "subscribers");
|
|
1830
|
+
return;
|
|
1831
|
+
}
|
|
1832
|
+
const first = await subscribers.createForTenant({
|
|
1833
|
+
...subscriberFor("tenant-subscriber-first", "Erste Autohaus GmbH"),
|
|
1834
|
+
vatId: "DE123456789",
|
|
1835
|
+
addressLine1: "Hauptstra\xDFe 1",
|
|
1836
|
+
postalCode: "10115",
|
|
1837
|
+
city: "Berlin",
|
|
1838
|
+
country: "DE",
|
|
1839
|
+
invoiceEmail: "rechnung@erste.example",
|
|
1840
|
+
customerNumberPrefix: "K-"
|
|
1841
|
+
});
|
|
1842
|
+
const second = await subscribers.createForTenant(
|
|
1843
|
+
subscriberFor("tenant-subscriber-second", "Zweiter Verein e.V.")
|
|
1844
|
+
);
|
|
1845
|
+
assert.ok(first && second, "both tenants had no subscriber");
|
|
1846
|
+
const numberOf = (customerNumber) => Number(customerNumber.replace("K-", ""));
|
|
1847
|
+
assert.ok(first.customerNumber.startsWith("K-"), first.customerNumber);
|
|
1848
|
+
assert.equal(numberOf(second.customerNumber), numberOf(first.customerNumber) + 1);
|
|
1849
|
+
assert.ok(numberOf(first.customerNumber) >= 10001, first.customerNumber);
|
|
1850
|
+
const found = await subscribers.findByTenantId("tenant-subscriber-first");
|
|
1851
|
+
assert.deepEqual(
|
|
1852
|
+
found && {
|
|
1853
|
+
id: found.id,
|
|
1854
|
+
customerNumber: found.customerNumber,
|
|
1855
|
+
tenantId: found.tenantId,
|
|
1856
|
+
legalName: found.legalName,
|
|
1857
|
+
vatId: found.vatId,
|
|
1858
|
+
taxNumber: found.taxNumber,
|
|
1859
|
+
addressLine1: found.addressLine1,
|
|
1860
|
+
addressLine2: found.addressLine2,
|
|
1861
|
+
postalCode: found.postalCode,
|
|
1862
|
+
city: found.city,
|
|
1863
|
+
country: found.country,
|
|
1864
|
+
invoiceEmail: found.invoiceEmail,
|
|
1865
|
+
migrated: found.migrated
|
|
1866
|
+
},
|
|
1867
|
+
{
|
|
1868
|
+
id: first.id,
|
|
1869
|
+
customerNumber: first.customerNumber,
|
|
1870
|
+
tenantId: "tenant-subscriber-first",
|
|
1871
|
+
legalName: "Erste Autohaus GmbH",
|
|
1872
|
+
vatId: "DE123456789",
|
|
1873
|
+
// The nullable half, so an adapter writing '' is caught too.
|
|
1874
|
+
taxNumber: null,
|
|
1875
|
+
addressLine1: "Hauptstra\xDFe 1",
|
|
1876
|
+
addressLine2: null,
|
|
1877
|
+
postalCode: "10115",
|
|
1878
|
+
city: "Berlin",
|
|
1879
|
+
country: "DE",
|
|
1880
|
+
invoiceEmail: "rechnung@erste.example",
|
|
1881
|
+
migrated: false
|
|
1882
|
+
}
|
|
1883
|
+
);
|
|
1884
|
+
assert.equal(
|
|
1885
|
+
(await subscribers.findById(second.id))?.tenantId,
|
|
1886
|
+
"tenant-subscriber-second"
|
|
1887
|
+
);
|
|
1888
|
+
assert.equal(await subscribers.findByTenantId("tenant-without-subscriber"), null);
|
|
1889
|
+
assert.equal(await subscribers.findById("subscriber-nobody-created"), null);
|
|
1890
|
+
});
|
|
1891
|
+
test("a tenant has one live subscriber: a second is refused, and the transaction survives it", async (t) => {
|
|
1892
|
+
const { adapter } = harness;
|
|
1893
|
+
const subscribers = adapter.subscriberRepository;
|
|
1894
|
+
if (!subscribers) {
|
|
1895
|
+
missing(t, "subscribers");
|
|
1896
|
+
return;
|
|
1897
|
+
}
|
|
1898
|
+
if (!adapter.capabilities.transactions) {
|
|
1899
|
+
t.skip("adapter declares no transaction capability");
|
|
1900
|
+
return;
|
|
1901
|
+
}
|
|
1902
|
+
const first = await subscribers.createForTenant(
|
|
1903
|
+
subscriberFor("tenant-one-subscriber", "Einzig GmbH")
|
|
1904
|
+
);
|
|
1905
|
+
const afterRefusal = await adapter.transactionRunner.run(async (tx) => {
|
|
1906
|
+
const refused = await subscribers.createForTenant(
|
|
1907
|
+
subscriberFor("tenant-one-subscriber", "Doppelt GmbH"),
|
|
1908
|
+
tx
|
|
1909
|
+
);
|
|
1910
|
+
assert.equal(refused, null, "a second live subscriber was created");
|
|
1911
|
+
return subscribers.createForTenant(
|
|
1912
|
+
subscriberFor("tenant-after-refusal", "Danach GmbH"),
|
|
1913
|
+
tx
|
|
1914
|
+
);
|
|
1915
|
+
});
|
|
1916
|
+
assert.equal(
|
|
1917
|
+
(await subscribers.findByTenantId("tenant-one-subscriber"))?.id,
|
|
1918
|
+
first?.id
|
|
1919
|
+
);
|
|
1920
|
+
assert.equal(
|
|
1921
|
+
(await subscribers.findByTenantId("tenant-after-refusal"))?.id,
|
|
1922
|
+
afterRefusal?.id,
|
|
1923
|
+
"the transaction did not survive the refusal"
|
|
1924
|
+
);
|
|
1925
|
+
});
|
|
1926
|
+
test("creating a subscriber for one tenant twice at once ends with one", async (t) => {
|
|
1927
|
+
const subscribers = harness.adapter.subscriberRepository;
|
|
1928
|
+
if (!subscribers) {
|
|
1929
|
+
missing(t, "subscribers");
|
|
1930
|
+
return;
|
|
1931
|
+
}
|
|
1932
|
+
const results = await Promise.all([
|
|
1933
|
+
subscribers.createForTenant(subscriberFor("tenant-at-once", "Gleichzeitig A")),
|
|
1934
|
+
subscribers.createForTenant(subscriberFor("tenant-at-once", "Gleichzeitig B"))
|
|
1935
|
+
]);
|
|
1936
|
+
const created = results.filter((result) => result !== null);
|
|
1937
|
+
assert.equal(created.length, 1, `${created.length} subscribers were created`);
|
|
1938
|
+
assert.equal((await subscribers.findByTenantId("tenant-at-once"))?.id, created[0].id);
|
|
1939
|
+
});
|
|
1940
|
+
test("a subscriber written on a transaction is undone with it", async (t) => {
|
|
1941
|
+
const { adapter } = harness;
|
|
1942
|
+
const subscribers = adapter.subscriberRepository;
|
|
1943
|
+
if (!subscribers) {
|
|
1944
|
+
missing(t, "subscribers");
|
|
1945
|
+
return;
|
|
1946
|
+
}
|
|
1947
|
+
if (!adapter.capabilities.transactions) {
|
|
1948
|
+
t.skip("adapter declares no transaction capability");
|
|
1949
|
+
return;
|
|
1950
|
+
}
|
|
1951
|
+
await assert.rejects(
|
|
1952
|
+
adapter.transactionRunner.run(async (tx) => {
|
|
1953
|
+
await subscribers.createForTenant(
|
|
1954
|
+
subscriberFor("tenant-rolled-back", "Zur\xFCckgerollt GmbH"),
|
|
1955
|
+
tx
|
|
1956
|
+
);
|
|
1957
|
+
throw new Error("the tenant is not created after all");
|
|
1958
|
+
})
|
|
1959
|
+
);
|
|
1960
|
+
assert.equal(await subscribers.findByTenantId("tenant-rolled-back"), null);
|
|
1961
|
+
assert.ok(
|
|
1962
|
+
await subscribers.createForTenant(
|
|
1963
|
+
subscriberFor("tenant-rolled-back", "Zur\xFCckgerollt GmbH")
|
|
1964
|
+
)
|
|
1965
|
+
);
|
|
1966
|
+
});
|
|
1967
|
+
test("a contact change writes what it names and keeps the rest", async (t) => {
|
|
1968
|
+
const subscribers = harness.adapter.subscriberRepository;
|
|
1969
|
+
if (!subscribers) {
|
|
1970
|
+
missing(t, "subscribers");
|
|
1971
|
+
return;
|
|
1972
|
+
}
|
|
1973
|
+
const created = await subscribers.createForTenant({
|
|
1974
|
+
...subscriberFor("tenant-contact", "Kontakt GmbH"),
|
|
1975
|
+
addressLine2: "Hinterhaus",
|
|
1976
|
+
city: "Hamburg"
|
|
1977
|
+
});
|
|
1978
|
+
assert.ok(created);
|
|
1979
|
+
const changed = await subscribers.updateContact(created.id, {
|
|
1980
|
+
city: "Bremen",
|
|
1981
|
+
addressLine2: null,
|
|
1982
|
+
invoiceEmail: "buchhaltung@kontakt.example"
|
|
1983
|
+
});
|
|
1984
|
+
assert.deepEqual(
|
|
1985
|
+
changed && [
|
|
1986
|
+
changed.city,
|
|
1987
|
+
changed.addressLine2,
|
|
1988
|
+
changed.invoiceEmail,
|
|
1989
|
+
changed.legalName,
|
|
1990
|
+
changed.tenantId
|
|
1991
|
+
],
|
|
1992
|
+
["Bremen", null, "buchhaltung@kontakt.example", "Kontakt GmbH", "tenant-contact"]
|
|
1993
|
+
);
|
|
1994
|
+
assert.equal((await subscribers.findById(created.id))?.city, "Bremen");
|
|
1995
|
+
assert.equal(
|
|
1996
|
+
await subscribers.updateContact("subscriber-nobody-created", { city: "Kiel" }),
|
|
1997
|
+
null
|
|
1998
|
+
);
|
|
1999
|
+
});
|
|
2000
|
+
test("a correction records the values it replaced, and nothing when nothing moves", async (t) => {
|
|
2001
|
+
const subscribers = harness.adapter.subscriberRepository;
|
|
2002
|
+
if (!subscribers) {
|
|
2003
|
+
missing(t, "subscribers");
|
|
2004
|
+
return;
|
|
2005
|
+
}
|
|
2006
|
+
const created = await subscribers.createForTenant(
|
|
2007
|
+
subscriberFor("tenant-correction", "Mueller GmbH")
|
|
2008
|
+
);
|
|
2009
|
+
assert.ok(created);
|
|
2010
|
+
const at = (day) => new Date(Date.UTC(2026, 8, day));
|
|
2011
|
+
const first = await subscribers.correctIdentity(created.id, {
|
|
2012
|
+
corrected: { legalName: "M\xFCller GmbH", vatId: "DE123456789", taxNumber: null },
|
|
2013
|
+
reason: "Umlaut lost when the registration was typed",
|
|
2014
|
+
correctedBy: "operator:anna",
|
|
2015
|
+
correctedAt: at(1)
|
|
2016
|
+
});
|
|
2017
|
+
assert.deepEqual(
|
|
2018
|
+
first?.correction && {
|
|
2019
|
+
previous: first.correction.previous,
|
|
2020
|
+
corrected: first.correction.corrected,
|
|
2021
|
+
reason: first.correction.reason,
|
|
2022
|
+
correctedBy: first.correction.correctedBy,
|
|
2023
|
+
correctedAt: first.correction.correctedAt.getTime()
|
|
2024
|
+
},
|
|
2025
|
+
{
|
|
2026
|
+
previous: { legalName: "Mueller GmbH", vatId: null },
|
|
2027
|
+
corrected: { legalName: "M\xFCller GmbH", vatId: "DE123456789" },
|
|
2028
|
+
reason: "Umlaut lost when the registration was typed",
|
|
2029
|
+
correctedBy: "operator:anna",
|
|
2030
|
+
correctedAt: at(1).getTime()
|
|
2031
|
+
}
|
|
2032
|
+
);
|
|
2033
|
+
assert.equal(first?.subscriber.legalName, "M\xFCller GmbH");
|
|
2034
|
+
assert.equal((await subscribers.findById(created.id))?.vatId, "DE123456789");
|
|
2035
|
+
const unchanged = await subscribers.correctIdentity(created.id, {
|
|
2036
|
+
corrected: { legalName: "M\xFCller GmbH" },
|
|
2037
|
+
reason: "Clicked twice",
|
|
2038
|
+
correctedBy: "operator:anna",
|
|
2039
|
+
correctedAt: at(2)
|
|
2040
|
+
});
|
|
2041
|
+
assert.equal(
|
|
2042
|
+
unchanged?.correction,
|
|
2043
|
+
null,
|
|
2044
|
+
"a correction that moved nothing was recorded"
|
|
2045
|
+
);
|
|
2046
|
+
await subscribers.correctIdentity(created.id, {
|
|
2047
|
+
corrected: { vatId: "DE999999999" },
|
|
2048
|
+
reason: "Wrong VAT id on the first correction",
|
|
2049
|
+
correctedBy: "operator:ben",
|
|
2050
|
+
correctedAt: at(3)
|
|
2051
|
+
});
|
|
2052
|
+
const listed = await subscribers.listCorrections(created.id);
|
|
2053
|
+
assert.deepEqual(
|
|
2054
|
+
listed.map((correction) => correction.reason),
|
|
2055
|
+
[
|
|
2056
|
+
"Wrong VAT id on the first correction",
|
|
2057
|
+
"Umlaut lost when the registration was typed"
|
|
2058
|
+
],
|
|
2059
|
+
"corrections come back the latest first, and only the two that moved something"
|
|
2060
|
+
);
|
|
2061
|
+
assert.equal(
|
|
2062
|
+
await subscribers.correctIdentity("subscriber-nobody-created", {
|
|
2063
|
+
corrected: { legalName: "Niemand" },
|
|
2064
|
+
reason: "none",
|
|
2065
|
+
correctedBy: "operator:anna",
|
|
2066
|
+
correctedAt: at(4)
|
|
2067
|
+
}),
|
|
2068
|
+
null
|
|
2069
|
+
);
|
|
2070
|
+
});
|
|
2071
|
+
test("two corrections at once each record the value the other left behind", async (t) => {
|
|
2072
|
+
const { adapter } = harness;
|
|
2073
|
+
const subscribers = adapter.subscriberRepository;
|
|
2074
|
+
if (!subscribers) {
|
|
2075
|
+
missing(t, "subscribers");
|
|
2076
|
+
return;
|
|
2077
|
+
}
|
|
2078
|
+
if (!adapter.capabilities.transactions || !adapter.capabilities.pessimisticLocking) {
|
|
2079
|
+
t.skip("adapter declares no transactions or no row locks");
|
|
2080
|
+
return;
|
|
2081
|
+
}
|
|
2082
|
+
const created = await subscribers.createForTenant(
|
|
2083
|
+
subscriberFor("tenant-corrected-twice", "Original GmbH")
|
|
2084
|
+
);
|
|
2085
|
+
assert.ok(created);
|
|
2086
|
+
const correctTo = (legalName) => adapter.transactionRunner.run(async (tx) => {
|
|
2087
|
+
await subscribers.correctIdentity(
|
|
2088
|
+
created.id,
|
|
2089
|
+
{
|
|
2090
|
+
corrected: { legalName },
|
|
2091
|
+
reason: `to ${legalName}`,
|
|
2092
|
+
correctedBy: "operator:anna",
|
|
2093
|
+
correctedAt: /* @__PURE__ */ new Date()
|
|
2094
|
+
},
|
|
2095
|
+
tx
|
|
2096
|
+
);
|
|
2097
|
+
await sleep(LOCK_HOLD_MS);
|
|
2098
|
+
});
|
|
2099
|
+
await Promise.all([
|
|
2100
|
+
correctTo("Erste Korrektur GmbH"),
|
|
2101
|
+
correctTo("Zweite Korrektur GmbH")
|
|
2102
|
+
]);
|
|
2103
|
+
const listed = await subscribers.listCorrections(created.id);
|
|
2104
|
+
assert.equal(listed.length, 2);
|
|
2105
|
+
const replaced = listed.map((correction) => correction.previous.legalName).sort();
|
|
2106
|
+
const written = listed.map((correction) => correction.corrected.legalName);
|
|
2107
|
+
const current = (await subscribers.findById(created.id))?.legalName;
|
|
2108
|
+
assert.ok(replaced.includes("Original GmbH"), JSON.stringify(listed));
|
|
2109
|
+
const secondReplaced = replaced.find((name) => name !== "Original GmbH");
|
|
2110
|
+
assert.ok(
|
|
2111
|
+
secondReplaced !== void 0 && written.includes(secondReplaced),
|
|
2112
|
+
`a correction recorded a value it did not replace: ${JSON.stringify(listed)}`
|
|
2113
|
+
);
|
|
2114
|
+
assert.ok(current !== void 0 && written.includes(current));
|
|
2115
|
+
assert.notEqual(secondReplaced, current);
|
|
2116
|
+
});
|
|
2117
|
+
test("a contract keeps its subscriber's copy after the subscriber is corrected", async (t) => {
|
|
2118
|
+
const { adapter } = harness;
|
|
2119
|
+
const subscribers = adapter.subscriberRepository;
|
|
2120
|
+
const contracts = adapter.subscriptionContractRepository;
|
|
2121
|
+
if (!subscribers) {
|
|
2122
|
+
missing(t, "subscribers");
|
|
2123
|
+
return;
|
|
2124
|
+
}
|
|
2125
|
+
if (!contracts || !harness.seed.createSubscriber) {
|
|
2126
|
+
missing(t, "subscriptionContracts");
|
|
2127
|
+
return;
|
|
2128
|
+
}
|
|
2129
|
+
const subscriber = await subscribers.createForTenant(
|
|
2130
|
+
subscriberFor("tenant-copy-kept", "Vorher GmbH")
|
|
2131
|
+
);
|
|
2132
|
+
assert.ok(subscriber);
|
|
2133
|
+
const parties = partiesWith(subscriber.id, "Vorher GmbH");
|
|
2134
|
+
const contract = await contracts.create({
|
|
2135
|
+
...contractFromOffer("offer-copy-kept", parties),
|
|
2136
|
+
tenantId: "tenant-copy-kept"
|
|
2137
|
+
});
|
|
2138
|
+
await subscribers.correctIdentity(subscriber.id, {
|
|
2139
|
+
corrected: { legalName: "Nachher GmbH" },
|
|
2140
|
+
reason: "Change of name of the same company",
|
|
2141
|
+
correctedBy: "operator:anna",
|
|
2142
|
+
correctedAt: /* @__PURE__ */ new Date()
|
|
2143
|
+
});
|
|
2144
|
+
const readBack = await contracts.findById(contract.id);
|
|
2145
|
+
assert.equal(readBack?.subscriberId, subscriber.id);
|
|
2146
|
+
assert.equal(
|
|
2147
|
+
readBack?.subscriber.legalName,
|
|
2148
|
+
"Vorher GmbH",
|
|
2149
|
+
"the contract followed a correction of the live record"
|
|
2150
|
+
);
|
|
2151
|
+
});
|
|
2152
|
+
test("the contracts still running say which issuer each names", async (t) => {
|
|
2153
|
+
const { adapter } = harness;
|
|
2154
|
+
const contracts = adapter.subscriptionContractRepository;
|
|
2155
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2156
|
+
if (!contracts || !createSubscriber) {
|
|
2157
|
+
missing(t, "subscriptionContracts");
|
|
2158
|
+
return;
|
|
2159
|
+
}
|
|
2160
|
+
const { subscriberId } = await createSubscriber({ legalName: "Meier GmbH" });
|
|
2161
|
+
const parties = partiesWith(subscriberId, "Meier GmbH");
|
|
2162
|
+
const written = async (offerId, overrides) => contracts.create({ ...contractFromOffer(offerId, parties), ...overrides });
|
|
2163
|
+
const oldest = await written("running-oldest", {
|
|
2164
|
+
effectiveFrom: /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z")
|
|
2165
|
+
});
|
|
2166
|
+
const scheduled = await written("running-scheduled", {
|
|
2167
|
+
effectiveFrom: /* @__PURE__ */ new Date("2026-02-01T00:00:00.000Z"),
|
|
2168
|
+
status: "scheduled"
|
|
2169
|
+
});
|
|
2170
|
+
const lapsed = await written("running-lapsed", {
|
|
2171
|
+
effectiveFrom: /* @__PURE__ */ new Date("2026-03-01T00:00:00.000Z"),
|
|
2172
|
+
effectiveUntil: /* @__PURE__ */ new Date("2026-04-01T00:00:00.000Z")
|
|
2173
|
+
});
|
|
2174
|
+
const ending = await written("running-ending", {
|
|
2175
|
+
effectiveFrom: /* @__PURE__ */ new Date("2026-04-01T00:00:00.000Z"),
|
|
2176
|
+
effectiveUntil: /* @__PURE__ */ new Date("2026-12-01T00:00:00.000Z")
|
|
2177
|
+
});
|
|
2178
|
+
const noIssuer = await written("running-no-issuer", {
|
|
2179
|
+
effectiveFrom: /* @__PURE__ */ new Date("2026-05-01T00:00:00.000Z"),
|
|
2180
|
+
parties: { ...parties, issuer: null }
|
|
2181
|
+
});
|
|
2182
|
+
await written("running-terminated", {
|
|
2183
|
+
effectiveFrom: /* @__PURE__ */ new Date("2025-01-01T00:00:00.000Z"),
|
|
2184
|
+
status: "terminated"
|
|
2185
|
+
});
|
|
2186
|
+
await written("running-superseded", {
|
|
2187
|
+
effectiveFrom: /* @__PURE__ */ new Date("2025-02-01T00:00:00.000Z"),
|
|
2188
|
+
status: "superseded"
|
|
2189
|
+
});
|
|
2190
|
+
const asOf = /* @__PURE__ */ new Date("2026-06-01T00:00:00.000Z");
|
|
2191
|
+
const listed = await contracts.listRunningIssuers(10, asOf);
|
|
2192
|
+
assert.equal(listed.total, 4, "a contract that ended or was ended is not running");
|
|
2193
|
+
assert.deepEqual(
|
|
2194
|
+
listed.contracts.map((row) => row.id),
|
|
2195
|
+
[oldest.id, scheduled.id, ending.id, noIssuer.id],
|
|
2196
|
+
"oldest first, and the one whose term ran out is not among them"
|
|
2197
|
+
);
|
|
2198
|
+
assert.ok(
|
|
2199
|
+
!listed.contracts.some((row) => row.id === lapsed.id),
|
|
2200
|
+
"a window that closed ends a contract, whatever its status still says"
|
|
2201
|
+
);
|
|
2202
|
+
const earlier = await contracts.listRunningIssuers(
|
|
2203
|
+
10,
|
|
2204
|
+
/* @__PURE__ */ new Date("2026-03-15T00:00:00.000Z")
|
|
2205
|
+
);
|
|
2206
|
+
assert.equal(earlier.total, 5);
|
|
2207
|
+
assert.ok(earlier.contracts.some((row) => row.id === lapsed.id));
|
|
2208
|
+
assert.equal(listed.contracts[0].tenantId, oldest.tenantId);
|
|
2209
|
+
assert.equal(
|
|
2210
|
+
listed.contracts[0].effectiveFrom.getTime(),
|
|
2211
|
+
(/* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z")).getTime()
|
|
2212
|
+
);
|
|
2213
|
+
assert.equal(listed.contracts[0].issuerLegalName, "Example Software GmbH");
|
|
2214
|
+
assert.equal(
|
|
2215
|
+
listed.contracts[3].issuerLegalName,
|
|
2216
|
+
null,
|
|
2217
|
+
"a contract with no issuer copy says so rather than inventing one"
|
|
2218
|
+
);
|
|
2219
|
+
const capped = await contracts.listRunningIssuers(2, asOf);
|
|
2220
|
+
assert.equal(capped.total, 4, "the limit caps the list, not the count");
|
|
2221
|
+
assert.deepEqual(
|
|
2222
|
+
capped.contracts.map((row) => row.id),
|
|
2223
|
+
[oldest.id, scheduled.id]
|
|
2224
|
+
);
|
|
2225
|
+
const counted = await contracts.listRunningIssuers(0, asOf);
|
|
2226
|
+
assert.equal(counted.total, 4);
|
|
2227
|
+
assert.deepEqual(counted.contracts, []);
|
|
2228
|
+
});
|
|
2229
|
+
test("a gateway event is claimed once per account, and a duplicate leaves the transaction usable", async (t) => {
|
|
2230
|
+
const { adapter } = harness;
|
|
2231
|
+
const log = adapter.paymentEventLog;
|
|
2232
|
+
if (!log) {
|
|
2233
|
+
missing(t, "paymentEventLog");
|
|
2234
|
+
return;
|
|
2235
|
+
}
|
|
2236
|
+
const claims = await adapter.transactionRunner.run(async (tx) => [
|
|
2237
|
+
await log.claim(eventAt("stripe-main", "evt_1"), tx),
|
|
2238
|
+
await log.claim(eventAt("stripe-main", "evt_1"), tx),
|
|
2239
|
+
// The same identifier from another account is another event.
|
|
2240
|
+
await log.claim(eventAt("stripe-old", "evt_1"), tx),
|
|
2241
|
+
// A duplicate that raised would have aborted the transaction here.
|
|
2242
|
+
await log.claim(eventAt("stripe-main", "evt_2"), tx)
|
|
2243
|
+
]);
|
|
2244
|
+
assert.deepEqual(claims, [true, false, true, true]);
|
|
2245
|
+
const later = await adapter.transactionRunner.run(
|
|
2246
|
+
(tx) => log.claim(eventAt("stripe-main", "evt_1"), tx)
|
|
2247
|
+
);
|
|
2248
|
+
assert.equal(later, false, "a committed claim was claimed again");
|
|
2249
|
+
});
|
|
2250
|
+
test("one gateway session is confirmed once, however many events report it", async (t) => {
|
|
2251
|
+
const { adapter } = harness;
|
|
2252
|
+
const log = adapter.paymentEventLog;
|
|
2253
|
+
if (!log) {
|
|
2254
|
+
missing(t, "paymentEventLog");
|
|
2255
|
+
return;
|
|
2256
|
+
}
|
|
2257
|
+
const session = "cs_reported_twice";
|
|
2258
|
+
const claims = await adapter.transactionRunner.run(async (tx) => [
|
|
2259
|
+
await log.claim(
|
|
2260
|
+
eventAt("stripe-main", "evt_form_done", { sessionId: session }),
|
|
2261
|
+
tx
|
|
2262
|
+
),
|
|
2263
|
+
// The same session, reported again under another identifier:
|
|
2264
|
+
// recording it would set the session's payment method up twice.
|
|
2265
|
+
await log.claim(
|
|
2266
|
+
eventAt("stripe-main", "evt_method_on", { sessionId: session }),
|
|
2267
|
+
tx
|
|
2268
|
+
),
|
|
2269
|
+
// Another account's session of that name is another session.
|
|
2270
|
+
await log.claim(eventAt("stripe-old", "evt_elsewhere", { sessionId: session }), tx),
|
|
2271
|
+
// A kind that says nothing about the session being confirmed.
|
|
2272
|
+
await log.claim(
|
|
2273
|
+
eventAt("stripe-main", "evt_gave_up", {
|
|
2274
|
+
sessionId: session,
|
|
2275
|
+
kind: "payment-method-setup-failed"
|
|
2276
|
+
}),
|
|
2277
|
+
tx
|
|
2278
|
+
),
|
|
2279
|
+
// Events about no session at all do not collide with each other.
|
|
2280
|
+
await log.claim(
|
|
2281
|
+
eventAt("stripe-main", "evt_other_1", { sessionId: null, kind: "unhandled" }),
|
|
2282
|
+
tx
|
|
2283
|
+
),
|
|
2284
|
+
await log.claim(
|
|
2285
|
+
eventAt("stripe-main", "evt_other_2", { sessionId: null, kind: "unhandled" }),
|
|
2286
|
+
tx
|
|
2287
|
+
)
|
|
2288
|
+
]);
|
|
2289
|
+
assert.deepEqual(claims, [true, false, true, true, true, true]);
|
|
2290
|
+
const later = await adapter.transactionRunner.run(
|
|
2291
|
+
(tx) => log.claim(eventAt("stripe-main", "evt_late", { sessionId: session }), tx)
|
|
2292
|
+
);
|
|
2293
|
+
assert.equal(later, false, "a session already confirmed was confirmed again");
|
|
2294
|
+
});
|
|
2295
|
+
test("a claim rolled back with its transaction is free for the retry", async (t) => {
|
|
2296
|
+
const { adapter } = harness;
|
|
2297
|
+
const log = adapter.paymentEventLog;
|
|
2298
|
+
if (!log) {
|
|
2299
|
+
missing(t, "paymentEventLog");
|
|
2300
|
+
return;
|
|
2301
|
+
}
|
|
2302
|
+
await assert.rejects(
|
|
2303
|
+
adapter.transactionRunner.run(async (tx) => {
|
|
2304
|
+
assert.equal(await log.claim(eventAt("stripe-main", "evt_retry"), tx), true);
|
|
2305
|
+
throw new Error("recording what the event changes failed");
|
|
2306
|
+
}),
|
|
2307
|
+
/recording what the event changes failed/
|
|
2308
|
+
);
|
|
2309
|
+
const retry = await adapter.transactionRunner.run(
|
|
2310
|
+
(tx) => log.claim(eventAt("stripe-main", "evt_retry"), tx)
|
|
2311
|
+
);
|
|
2312
|
+
assert.equal(retry, true, "the retry was discarded as a duplicate");
|
|
2313
|
+
});
|
|
2314
|
+
test("a delivery that meets a claim still open waits for it, and answers by its outcome", async (t) => {
|
|
2315
|
+
const { adapter } = harness;
|
|
2316
|
+
const log = adapter.paymentEventLog;
|
|
2317
|
+
if (!log) {
|
|
2318
|
+
missing(t, "paymentEventLog");
|
|
2319
|
+
return;
|
|
2320
|
+
}
|
|
2321
|
+
if (!adapter.capabilities.pessimisticLocking) {
|
|
2322
|
+
t.skip(
|
|
2323
|
+
"adapter declares no pessimistic locking: an open claim cannot be waited on"
|
|
2324
|
+
);
|
|
2325
|
+
return;
|
|
2326
|
+
}
|
|
2327
|
+
const [committed, afterCommit] = await Promise.all([
|
|
2328
|
+
adapter.transactionRunner.run(async (tx) => {
|
|
2329
|
+
const claimed = await log.claim(eventAt("stripe-main", "evt_race"), tx);
|
|
2330
|
+
await sleep(LOCK_HOLD_MS);
|
|
2331
|
+
return claimed;
|
|
2332
|
+
}),
|
|
2333
|
+
sleep(LOCK_HOLD_MS / 3).then(
|
|
2334
|
+
() => adapter.transactionRunner.run(
|
|
2335
|
+
(tx) => log.claim(eventAt("stripe-main", "evt_race"), tx)
|
|
2336
|
+
)
|
|
2337
|
+
)
|
|
2338
|
+
]);
|
|
2339
|
+
assert.deepEqual([committed, afterCommit], [true, false]);
|
|
2340
|
+
const [rolledBack, afterRollback] = await Promise.allSettled([
|
|
2341
|
+
adapter.transactionRunner.run(async (tx) => {
|
|
2342
|
+
await log.claim(eventAt("stripe-main", "evt_race_back"), tx);
|
|
2343
|
+
await sleep(LOCK_HOLD_MS);
|
|
2344
|
+
throw new Error("the first delivery failed");
|
|
2345
|
+
}),
|
|
2346
|
+
sleep(LOCK_HOLD_MS / 3).then(
|
|
2347
|
+
() => adapter.transactionRunner.run(
|
|
2348
|
+
(tx) => log.claim(eventAt("stripe-main", "evt_race_back"), tx)
|
|
2349
|
+
)
|
|
2350
|
+
)
|
|
2351
|
+
]);
|
|
2352
|
+
assert.equal(rolledBack.status, "rejected");
|
|
2353
|
+
assert.deepEqual(afterRollback, { status: "fulfilled", value: true });
|
|
2354
|
+
});
|
|
2355
|
+
test("an event that changed nothing gives its session back, and stays claimed itself", async (t) => {
|
|
2356
|
+
const { adapter } = harness;
|
|
2357
|
+
const log = adapter.paymentEventLog;
|
|
2358
|
+
if (!log) {
|
|
2359
|
+
missing(t, "paymentEventLog");
|
|
2360
|
+
return;
|
|
2361
|
+
}
|
|
2362
|
+
const session = "cs_nothing_to_do";
|
|
2363
|
+
const released = await adapter.transactionRunner.run(async (tx) => {
|
|
2364
|
+
await log.claim(eventAt("stripe-main", "evt_missed", { sessionId: session }), tx);
|
|
2365
|
+
await log.releaseSession("stripe-main", "evt_missed", tx);
|
|
2366
|
+
return [
|
|
2367
|
+
// The next event about that session is handled …
|
|
2368
|
+
await log.claim(
|
|
2369
|
+
eventAt("stripe-main", "evt_correct", { sessionId: session }),
|
|
2370
|
+
tx
|
|
2371
|
+
),
|
|
2372
|
+
// … while the event that released it stays claimed.
|
|
2373
|
+
await log.claim(eventAt("stripe-main", "evt_missed", { sessionId: null }), tx)
|
|
2374
|
+
];
|
|
2375
|
+
});
|
|
2376
|
+
assert.deepEqual(released, [true, false]);
|
|
2377
|
+
const again = await adapter.transactionRunner.run(
|
|
2378
|
+
(tx) => log.claim(eventAt("stripe-main", "evt_after_correct", { sessionId: session }), tx)
|
|
2379
|
+
);
|
|
2380
|
+
assert.equal(again, false, "the session was confirmed and is not free again");
|
|
2381
|
+
});
|
|
2382
|
+
test("two events confirming one session at once end with one claim", async (t) => {
|
|
2383
|
+
const { adapter } = harness;
|
|
2384
|
+
const log = adapter.paymentEventLog;
|
|
2385
|
+
if (!log) {
|
|
2386
|
+
missing(t, "paymentEventLog");
|
|
2387
|
+
return;
|
|
2388
|
+
}
|
|
2389
|
+
if (!adapter.capabilities.pessimisticLocking) {
|
|
2390
|
+
t.skip(
|
|
2391
|
+
"adapter declares no pessimistic locking: an open claim cannot be waited on"
|
|
2392
|
+
);
|
|
2393
|
+
return;
|
|
2394
|
+
}
|
|
2395
|
+
const session = "cs_at_once";
|
|
2396
|
+
const [first, second] = await Promise.all([
|
|
2397
|
+
adapter.transactionRunner.run(async (tx) => {
|
|
2398
|
+
const claimed = await log.claim(
|
|
2399
|
+
eventAt("stripe-main", "evt_at_once_a", { sessionId: session }),
|
|
2400
|
+
tx
|
|
2401
|
+
);
|
|
2402
|
+
await sleep(LOCK_HOLD_MS);
|
|
2403
|
+
return claimed;
|
|
2404
|
+
}),
|
|
2405
|
+
sleep(LOCK_HOLD_MS / 3).then(
|
|
2406
|
+
() => adapter.transactionRunner.run(
|
|
2407
|
+
(tx) => log.claim(
|
|
2408
|
+
eventAt("stripe-main", "evt_at_once_b", { sessionId: session }),
|
|
2409
|
+
tx
|
|
2410
|
+
)
|
|
2411
|
+
)
|
|
2412
|
+
)
|
|
2413
|
+
]);
|
|
2414
|
+
assert.deepEqual([first, second], [true, false]);
|
|
2415
|
+
});
|
|
2416
|
+
test("a confirmed payment method becomes the subscriber's, with its references and masked details", async (t) => {
|
|
2417
|
+
const methods = harness.adapter.subscriberPaymentMethodRepository;
|
|
2418
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2419
|
+
if (!methods || !createSubscriber) {
|
|
2420
|
+
missing(t, "subscriberPaymentMethods");
|
|
2421
|
+
return;
|
|
2422
|
+
}
|
|
2423
|
+
const { subscriberId } = await createSubscriber({ legalName: "Karte GmbH" });
|
|
2424
|
+
const debit = {
|
|
2425
|
+
...paymentMethodFor(subscriberId, "pm_sepa", "2026-09-01T10:00:00.000Z"),
|
|
2426
|
+
type: "sepa_debit",
|
|
2427
|
+
brand: null,
|
|
2428
|
+
last4: "3000",
|
|
2429
|
+
expiryMonth: null,
|
|
2430
|
+
expiryYear: null,
|
|
2431
|
+
country: "DE",
|
|
2432
|
+
bankCode: "37040044",
|
|
2433
|
+
mandateReference: "MANDATE-1"
|
|
2434
|
+
};
|
|
2435
|
+
const result = await methods.recordConfirmed(debit);
|
|
2436
|
+
assert.equal(result.outcome, "activated");
|
|
2437
|
+
const { id, createdAt, ...stored } = result.method;
|
|
2438
|
+
assert.ok(id);
|
|
2439
|
+
assert.ok(createdAt instanceof Date);
|
|
2440
|
+
assert.deepEqual(stored, { ...debit, status: "ACTIVE", replacedAt: null });
|
|
2441
|
+
assert.deepEqual(await methods.findActive(subscriberId), result.method);
|
|
2442
|
+
assert.deepEqual(
|
|
2443
|
+
await methods.findByReference("stripe-main", "pm_sepa"),
|
|
2444
|
+
result.method
|
|
2445
|
+
);
|
|
2446
|
+
assert.equal(await methods.findByReference("stripe-old", "pm_sepa"), null);
|
|
2447
|
+
const other = await createSubscriber({ legalName: "Second Customer GmbH" });
|
|
2448
|
+
assert.equal(await methods.findActive(other.subscriberId), null);
|
|
2449
|
+
});
|
|
2450
|
+
test("a newer payment method takes over, and the one it replaced stays as history", async (t) => {
|
|
2451
|
+
const methods = harness.adapter.subscriberPaymentMethodRepository;
|
|
2452
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2453
|
+
if (!methods || !createSubscriber) {
|
|
2454
|
+
missing(t, "subscriberPaymentMethods");
|
|
2455
|
+
return;
|
|
2456
|
+
}
|
|
2457
|
+
const { subscriberId } = await createSubscriber({ legalName: "Wechsel GmbH" });
|
|
2458
|
+
await methods.recordConfirmed(
|
|
2459
|
+
paymentMethodFor(subscriberId, "pm_first", "2026-09-01T10:00:00.000Z")
|
|
2460
|
+
);
|
|
2461
|
+
const second = await methods.recordConfirmed(
|
|
2462
|
+
paymentMethodFor(subscriberId, "pm_second", "2026-09-02T10:00:00.000Z")
|
|
2463
|
+
);
|
|
2464
|
+
assert.equal(second.outcome, "activated");
|
|
2465
|
+
assert.equal((await methods.findActive(subscriberId))?.paymentMethodRef, "pm_second");
|
|
2466
|
+
const first = await methods.findByReference("stripe-main", "pm_first");
|
|
2467
|
+
assert.equal(first?.status, "REPLACED");
|
|
2468
|
+
assert.equal(first?.replacedAt?.toISOString(), "2026-09-02T10:00:00.000Z");
|
|
2469
|
+
});
|
|
2470
|
+
test("a confirmation recorded again is recognised, and changes nothing", async (t) => {
|
|
2471
|
+
const methods = harness.adapter.subscriberPaymentMethodRepository;
|
|
2472
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2473
|
+
if (!methods || !createSubscriber) {
|
|
2474
|
+
missing(t, "subscriberPaymentMethods");
|
|
2475
|
+
return;
|
|
2476
|
+
}
|
|
2477
|
+
const { subscriberId } = await createSubscriber({ legalName: "Doppelt GmbH" });
|
|
2478
|
+
const confirmation = paymentMethodFor(
|
|
2479
|
+
subscriberId,
|
|
2480
|
+
"pm_twice",
|
|
2481
|
+
"2026-09-01T10:00:00.000Z"
|
|
2482
|
+
);
|
|
2483
|
+
const first = await methods.recordConfirmed(confirmation);
|
|
2484
|
+
const again = await methods.recordConfirmed({
|
|
2485
|
+
...confirmation,
|
|
2486
|
+
confirmedAt: /* @__PURE__ */ new Date("2026-09-03T10:00:00.000Z")
|
|
2487
|
+
});
|
|
2488
|
+
assert.equal(again.outcome, "already-recorded");
|
|
2489
|
+
assert.deepEqual(again.method, first.method);
|
|
2490
|
+
assert.deepEqual(await methods.findActive(subscriberId), first.method);
|
|
2491
|
+
});
|
|
2492
|
+
test("a confirmation older than the payment method in use is recorded as already replaced", async (t) => {
|
|
2493
|
+
const methods = harness.adapter.subscriberPaymentMethodRepository;
|
|
2494
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2495
|
+
if (!methods || !createSubscriber) {
|
|
2496
|
+
missing(t, "subscriberPaymentMethods");
|
|
2497
|
+
return;
|
|
2498
|
+
}
|
|
2499
|
+
const { subscriberId } = await createSubscriber({ legalName: "Reihenfolge GmbH" });
|
|
2500
|
+
await methods.recordConfirmed(
|
|
2501
|
+
paymentMethodFor(subscriberId, "pm_later", "2026-09-02T10:00:00.000Z")
|
|
2502
|
+
);
|
|
2503
|
+
const earlier = await methods.recordConfirmed(
|
|
2504
|
+
paymentMethodFor(subscriberId, "pm_earlier", "2026-09-01T10:00:00.000Z")
|
|
2505
|
+
);
|
|
2506
|
+
assert.equal(earlier.outcome, "superseded");
|
|
2507
|
+
assert.equal(earlier.method.status, "REPLACED");
|
|
2508
|
+
assert.equal(earlier.method.replacedAt?.toISOString(), "2026-09-02T10:00:00.000Z");
|
|
2509
|
+
assert.equal((await methods.findActive(subscriberId))?.paymentMethodRef, "pm_later");
|
|
2510
|
+
});
|
|
2511
|
+
test("two confirmations for one subscriber at once leave one payment method in use", async (t) => {
|
|
2512
|
+
const methods = harness.adapter.subscriberPaymentMethodRepository;
|
|
2513
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2514
|
+
if (!methods || !createSubscriber) {
|
|
2515
|
+
missing(t, "subscriberPaymentMethods");
|
|
2516
|
+
return;
|
|
2517
|
+
}
|
|
2518
|
+
const { subscriberId } = await createSubscriber({ legalName: "Gleichzeitig GmbH" });
|
|
2519
|
+
const results = await Promise.all([
|
|
2520
|
+
methods.recordConfirmed(
|
|
2521
|
+
paymentMethodFor(subscriberId, "pm_at_once_a", "2026-09-01T10:00:00.000Z")
|
|
2522
|
+
),
|
|
2523
|
+
methods.recordConfirmed(
|
|
2524
|
+
paymentMethodFor(subscriberId, "pm_at_once_b", "2026-09-01T10:00:01.000Z")
|
|
2525
|
+
)
|
|
2526
|
+
]);
|
|
2527
|
+
for (const result of results) {
|
|
2528
|
+
assert.ok(
|
|
2529
|
+
result.outcome === "activated" || result.outcome === "superseded",
|
|
2530
|
+
result.outcome
|
|
2531
|
+
);
|
|
2532
|
+
}
|
|
2533
|
+
const statuses = await Promise.all(
|
|
2534
|
+
["pm_at_once_a", "pm_at_once_b"].map(
|
|
2535
|
+
async (ref) => (await methods.findByReference("stripe-main", ref))?.status
|
|
2536
|
+
)
|
|
2537
|
+
);
|
|
2538
|
+
assert.deepEqual(statuses, ["REPLACED", "ACTIVE"]);
|
|
2539
|
+
assert.equal(
|
|
2540
|
+
(await methods.findActive(subscriberId))?.paymentMethodRef,
|
|
2541
|
+
"pm_at_once_b"
|
|
2542
|
+
);
|
|
2543
|
+
});
|
|
2544
|
+
test("a payment method written on a transaction is undone with it", async (t) => {
|
|
2545
|
+
const { adapter } = harness;
|
|
2546
|
+
const methods = adapter.subscriberPaymentMethodRepository;
|
|
2547
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2548
|
+
if (!methods || !createSubscriber) {
|
|
2549
|
+
missing(t, "subscriberPaymentMethods");
|
|
2550
|
+
return;
|
|
2551
|
+
}
|
|
2552
|
+
const { subscriberId } = await createSubscriber({ legalName: "Rolled Back GmbH" });
|
|
2553
|
+
await assert.rejects(
|
|
2554
|
+
adapter.transactionRunner.run(async (tx) => {
|
|
2555
|
+
await methods.recordConfirmed(
|
|
2556
|
+
paymentMethodFor(
|
|
2557
|
+
subscriberId,
|
|
2558
|
+
"pm_rolled_back",
|
|
2559
|
+
"2026-09-01T10:00:00.000Z"
|
|
2560
|
+
),
|
|
2561
|
+
tx
|
|
2562
|
+
);
|
|
2563
|
+
throw new Error("the activation failed after all");
|
|
2564
|
+
}),
|
|
2565
|
+
/the activation failed after all/
|
|
2566
|
+
);
|
|
2567
|
+
assert.equal(await methods.findActive(subscriberId), null);
|
|
2568
|
+
assert.equal(await methods.findByReference("stripe-main", "pm_rolled_back"), null);
|
|
2569
|
+
});
|
|
2570
|
+
test("a payment method for a subscriber that does not exist is refused", async (t) => {
|
|
2571
|
+
const methods = harness.adapter.subscriberPaymentMethodRepository;
|
|
2572
|
+
if (!methods || !harness.seed.createSubscriber) {
|
|
2573
|
+
missing(t, "subscriberPaymentMethods");
|
|
2574
|
+
return;
|
|
2575
|
+
}
|
|
2576
|
+
await assert.rejects(
|
|
2577
|
+
methods.recordConfirmed(
|
|
2578
|
+
paymentMethodFor(
|
|
2579
|
+
"subscriber-nobody-created",
|
|
2580
|
+
"pm_nobody",
|
|
2581
|
+
"2026-09-01T10:00:00.000Z"
|
|
2582
|
+
)
|
|
2583
|
+
)
|
|
2584
|
+
);
|
|
2585
|
+
assert.equal(await methods.findByReference("stripe-main", "pm_nobody"), null);
|
|
2586
|
+
});
|
|
2587
|
+
test("the accounts in use are those holding a payment method in use, each once", async (t) => {
|
|
2588
|
+
const methods = harness.adapter.subscriberPaymentMethodRepository;
|
|
2589
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2590
|
+
if (!methods || !createSubscriber) {
|
|
2591
|
+
missing(t, "subscriberPaymentMethods");
|
|
2592
|
+
return;
|
|
2593
|
+
}
|
|
2594
|
+
assert.deepEqual(await methods.accountsInUse(), []);
|
|
2595
|
+
const moved = await createSubscriber({ legalName: "Umgezogen GmbH" });
|
|
2596
|
+
const stayed = await createSubscriber({ legalName: "Geblieben GmbH" });
|
|
2597
|
+
await methods.recordConfirmed(
|
|
2598
|
+
paymentMethodFor(
|
|
2599
|
+
moved.subscriberId,
|
|
2600
|
+
"pm_old_account",
|
|
2601
|
+
"2026-09-01T10:00:00.000Z",
|
|
2602
|
+
"stripe-old"
|
|
2603
|
+
)
|
|
2604
|
+
);
|
|
2605
|
+
await methods.recordConfirmed(
|
|
2606
|
+
paymentMethodFor(
|
|
2607
|
+
moved.subscriberId,
|
|
2608
|
+
"pm_new_account",
|
|
2609
|
+
"2026-09-02T10:00:00.000Z",
|
|
2610
|
+
"stripe-main"
|
|
2611
|
+
)
|
|
2612
|
+
);
|
|
2613
|
+
await methods.recordConfirmed(
|
|
2614
|
+
paymentMethodFor(
|
|
2615
|
+
stayed.subscriberId,
|
|
2616
|
+
"pm_main",
|
|
2617
|
+
"2026-09-02T10:00:00.000Z",
|
|
2618
|
+
"stripe-main"
|
|
2619
|
+
)
|
|
2620
|
+
);
|
|
2621
|
+
assert.deepEqual(await methods.accountsInUse(), ["stripe-main"]);
|
|
2622
|
+
});
|
|
2623
|
+
test("a setup is completed once, and only by the account, session and subscriber it was started with", async (t) => {
|
|
2624
|
+
const methods = harness.adapter.subscriberPaymentMethodRepository;
|
|
2625
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2626
|
+
if (!methods || !createSubscriber) {
|
|
2627
|
+
missing(t, "subscriberPaymentMethods");
|
|
2628
|
+
return;
|
|
2629
|
+
}
|
|
2630
|
+
const { subscriberId } = await createSubscriber({ legalName: "Setup GmbH" });
|
|
2631
|
+
const other = await createSubscriber({ legalName: "Other Tenant GmbH" });
|
|
2632
|
+
await methods.recordSetup({
|
|
2633
|
+
subscriberId,
|
|
2634
|
+
gatewayAccount: "stripe-main",
|
|
2635
|
+
sessionRef: "cs_setup",
|
|
2636
|
+
customerRef: "cus_setup",
|
|
2637
|
+
startedAt: /* @__PURE__ */ new Date("2026-09-15T10:00:00.000Z")
|
|
2638
|
+
});
|
|
2639
|
+
const at = /* @__PURE__ */ new Date("2026-09-15T10:05:00.000Z");
|
|
2640
|
+
const match = { gatewayAccount: "stripe-main", sessionRef: "cs_setup", subscriberId };
|
|
2641
|
+
assert.equal(
|
|
2642
|
+
await methods.completeSetup({ ...match, subscriberId: other.subscriberId }, at),
|
|
2643
|
+
false
|
|
2644
|
+
);
|
|
2645
|
+
assert.equal(
|
|
2646
|
+
await methods.completeSetup({ ...match, gatewayAccount: "stripe-old" }, at),
|
|
2647
|
+
false
|
|
2648
|
+
);
|
|
2649
|
+
assert.equal(
|
|
2650
|
+
await methods.completeSetup({ ...match, sessionRef: "cs_nobody_opened" }, at),
|
|
2651
|
+
false
|
|
2652
|
+
);
|
|
2653
|
+
assert.equal(await methods.completeSetup(match, at), true);
|
|
2654
|
+
assert.equal(
|
|
2655
|
+
await methods.completeSetup(match, at),
|
|
2656
|
+
false,
|
|
2657
|
+
"a setup was completed twice"
|
|
2658
|
+
);
|
|
2659
|
+
});
|
|
2660
|
+
test("a setup completed on a transaction that rolls back is open again", async (t) => {
|
|
2661
|
+
const { adapter } = harness;
|
|
2662
|
+
const methods = adapter.subscriberPaymentMethodRepository;
|
|
2663
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2664
|
+
if (!methods || !createSubscriber) {
|
|
2665
|
+
missing(t, "subscriberPaymentMethods");
|
|
2666
|
+
return;
|
|
2667
|
+
}
|
|
2668
|
+
const { subscriberId } = await createSubscriber({ legalName: "Retry GmbH" });
|
|
2669
|
+
const match = { gatewayAccount: "stripe-main", sessionRef: "cs_retry", subscriberId };
|
|
2670
|
+
await methods.recordSetup({
|
|
2671
|
+
...match,
|
|
2672
|
+
customerRef: "cus_retry",
|
|
2673
|
+
startedAt: /* @__PURE__ */ new Date("2026-09-15T10:00:00.000Z")
|
|
2674
|
+
});
|
|
2675
|
+
const at = /* @__PURE__ */ new Date("2026-09-15T10:05:00.000Z");
|
|
2676
|
+
await assert.rejects(
|
|
2677
|
+
adapter.transactionRunner.run(async (tx) => {
|
|
2678
|
+
assert.equal(await methods.completeSetup(match, at, tx), true);
|
|
2679
|
+
throw new Error("recording the payment method failed");
|
|
2680
|
+
}),
|
|
2681
|
+
/recording the payment method failed/
|
|
2682
|
+
);
|
|
2683
|
+
assert.equal(
|
|
2684
|
+
await methods.completeSetup(match, at),
|
|
2685
|
+
true,
|
|
2686
|
+
"the rollback kept the completion"
|
|
2687
|
+
);
|
|
2688
|
+
});
|
|
2689
|
+
test("one session is one setup, however often it is recorded", async (t) => {
|
|
2690
|
+
const methods = harness.adapter.subscriberPaymentMethodRepository;
|
|
2691
|
+
const createSubscriber = harness.seed.createSubscriber;
|
|
2692
|
+
if (!methods || !createSubscriber) {
|
|
2693
|
+
missing(t, "subscriberPaymentMethods");
|
|
2694
|
+
return;
|
|
2695
|
+
}
|
|
2696
|
+
const { subscriberId } = await createSubscriber({ legalName: "Once GmbH" });
|
|
2697
|
+
const setup = {
|
|
2698
|
+
subscriberId,
|
|
2699
|
+
gatewayAccount: "stripe-main",
|
|
2700
|
+
sessionRef: "cs_once",
|
|
2701
|
+
customerRef: "cus_once",
|
|
2702
|
+
startedAt: /* @__PURE__ */ new Date("2026-09-15T10:00:00.000Z")
|
|
2703
|
+
};
|
|
2704
|
+
await methods.recordSetup(setup);
|
|
2705
|
+
await assert.rejects(methods.recordSetup(setup));
|
|
2706
|
+
});
|
|
2707
|
+
test("an offer is consumed once, whoever asks first", async (t) => {
|
|
2708
|
+
const offers = harness.adapter.checkoutOfferRepository;
|
|
2709
|
+
if (!offers) {
|
|
2710
|
+
missing(t, "checkoutOffers");
|
|
2711
|
+
return;
|
|
2712
|
+
}
|
|
2713
|
+
const offer = await offers.create(OFFER);
|
|
2714
|
+
assert.equal(offer.status, "open");
|
|
2715
|
+
const attempts = await Promise.allSettled([
|
|
2716
|
+
offers.consume(offer.id),
|
|
2717
|
+
offers.consume(offer.id)
|
|
2718
|
+
]);
|
|
2719
|
+
assert.equal(
|
|
2720
|
+
attempts.filter((attempt) => attempt.status === "fulfilled").length,
|
|
2721
|
+
1,
|
|
2722
|
+
"exactly one consume wins"
|
|
2723
|
+
);
|
|
2724
|
+
const consumed = await offers.findById(offer.id);
|
|
2725
|
+
assert.equal(consumed?.status, "consumed");
|
|
2726
|
+
assert.ok(consumed?.consumedAt, "and it records when");
|
|
2727
|
+
await assert.rejects(() => offers.consume(offer.id), "a later consume is refused");
|
|
2728
|
+
assert.equal(
|
|
2729
|
+
(await offers.findById(offer.id))?.consumedAt,
|
|
2730
|
+
consumed.consumedAt,
|
|
2731
|
+
"and changes nothing"
|
|
2732
|
+
);
|
|
2733
|
+
});
|
|
2734
|
+
test("a consume on a transaction that rolls back leaves the offer open", async (t) => {
|
|
2735
|
+
const { adapter } = harness;
|
|
2736
|
+
const offers = adapter.checkoutOfferRepository;
|
|
2737
|
+
if (!offers) {
|
|
2738
|
+
missing(t, "checkoutOffers");
|
|
2739
|
+
return;
|
|
2740
|
+
}
|
|
2741
|
+
if (!adapter.capabilities.transactions) {
|
|
2742
|
+
t.skip("adapter declares no transaction capability");
|
|
2743
|
+
return;
|
|
2744
|
+
}
|
|
2745
|
+
const offer = await offers.create(OFFER);
|
|
2746
|
+
await assert.rejects(
|
|
2747
|
+
adapter.transactionRunner.run(async (tx) => {
|
|
2748
|
+
await offers.consume(offer.id, tx);
|
|
2749
|
+
throw new Error("the contract after the consume fails");
|
|
2750
|
+
})
|
|
2751
|
+
);
|
|
2752
|
+
const afterwards = await offers.findById(offer.id);
|
|
2753
|
+
assert.equal(afterwards?.status, "open", "the consume outlived its transaction");
|
|
2754
|
+
assert.equal(afterwards?.consumedAt, null);
|
|
2755
|
+
assert.equal((await offers.consume(offer.id)).status, "consumed");
|
|
2756
|
+
});
|
|
2757
|
+
const SETTINGS = {
|
|
2758
|
+
app: { name: "Demo" },
|
|
2759
|
+
currency: "EUR",
|
|
2760
|
+
vatRate: 19,
|
|
2761
|
+
tenantBilling: {
|
|
2762
|
+
cancellationNoticeDays: { monthly: 14, yearly: 90 },
|
|
2763
|
+
selfServiceBlockedPlans: { asTarget: ["ENTERPRISE"], asSource: [] }
|
|
2764
|
+
}
|
|
2765
|
+
};
|
|
2766
|
+
const SOURCE = "/srv/app/config/saas.yaml";
|
|
2767
|
+
const FIRST_START = /* @__PURE__ */ new Date("2026-09-01T06:30:00.000Z");
|
|
2768
|
+
const SECOND_START = /* @__PURE__ */ new Date("2026-09-02T06:30:00.000Z");
|
|
2769
|
+
const applied = (fingerprint, appliedAt, settings = SETTINGS) => ({ fingerprint, settings, source: SOURCE, appliedAt });
|
|
2770
|
+
const changeTo = (current, noticedAt, previous = SETTINGS) => ({ noticedAt, source: SOURCE, previous, current });
|
|
2771
|
+
const TWENTY = { ...SETTINGS, vatRate: 20 };
|
|
2772
|
+
const TWENTY_ONE = { ...SETTINGS, vatRate: 21 };
|
|
2773
|
+
test("no record before the first boot that could write one", async (t) => {
|
|
2774
|
+
const port = harness.adapter.appliedSettings;
|
|
2775
|
+
if (!port) {
|
|
2776
|
+
missing(t, "appliedSettings");
|
|
2777
|
+
return;
|
|
2778
|
+
}
|
|
2779
|
+
assert.equal(await port.readApplied(), null);
|
|
2780
|
+
assert.deepEqual(await port.listChanges(), []);
|
|
2781
|
+
});
|
|
2782
|
+
test("the record comes back as it was written \u2014 values, source and moment", async (t) => {
|
|
2783
|
+
const port = harness.adapter.appliedSettings;
|
|
2784
|
+
if (!port) {
|
|
2785
|
+
missing(t, "appliedSettings");
|
|
2786
|
+
return;
|
|
2787
|
+
}
|
|
2788
|
+
assert.equal(await port.writeApplied(applied("sha256-a", FIRST_START), null), true);
|
|
2789
|
+
const read = await port.readApplied();
|
|
2790
|
+
assert.ok(read, "a record expected");
|
|
2791
|
+
assert.equal(read.fingerprint, "sha256-a");
|
|
2792
|
+
assert.equal(read.source, SOURCE);
|
|
2793
|
+
assert.equal(read.appliedAt.toISOString(), FIRST_START.toISOString());
|
|
2794
|
+
assert.deepEqual(read.settings, SETTINGS);
|
|
2795
|
+
});
|
|
2796
|
+
test("writing again replaces the one row rather than adding a second", async (t) => {
|
|
2797
|
+
const port = harness.adapter.appliedSettings;
|
|
2798
|
+
if (!port) {
|
|
2799
|
+
missing(t, "appliedSettings");
|
|
2800
|
+
return;
|
|
2801
|
+
}
|
|
2802
|
+
await port.writeApplied(applied("sha256-a", FIRST_START), null);
|
|
2803
|
+
assert.equal(
|
|
2804
|
+
await port.writeApplied(applied("sha256-b", SECOND_START, TWENTY), "sha256-a"),
|
|
2805
|
+
true
|
|
2806
|
+
);
|
|
2807
|
+
const read = await port.readApplied();
|
|
2808
|
+
assert.equal(read?.fingerprint, "sha256-b");
|
|
2809
|
+
assert.equal(read?.appliedAt.toISOString(), SECOND_START.toISOString());
|
|
2810
|
+
assert.equal((read?.settings).vatRate, 20);
|
|
2811
|
+
});
|
|
2812
|
+
test("the first record is written once: a second writer that read none is refused", async (t) => {
|
|
2813
|
+
const port = harness.adapter.appliedSettings;
|
|
2814
|
+
if (!port) {
|
|
2815
|
+
missing(t, "appliedSettings");
|
|
2816
|
+
return;
|
|
2817
|
+
}
|
|
2818
|
+
assert.equal(await port.writeApplied(applied("sha256-a", FIRST_START), null), true);
|
|
2819
|
+
assert.equal(await port.writeApplied(applied("sha256-b", SECOND_START), null), false);
|
|
2820
|
+
assert.equal((await port.readApplied())?.fingerprint, "sha256-a");
|
|
2821
|
+
});
|
|
2822
|
+
test("a write guarded on a fingerprint the row no longer carries is refused", async (t) => {
|
|
2823
|
+
const port = harness.adapter.appliedSettings;
|
|
2824
|
+
if (!port) {
|
|
2825
|
+
missing(t, "appliedSettings");
|
|
2826
|
+
return;
|
|
2827
|
+
}
|
|
2828
|
+
await port.writeApplied(applied("sha256-a", FIRST_START), null);
|
|
2829
|
+
assert.equal(
|
|
2830
|
+
await port.writeApplied(applied("sha256-b", SECOND_START), "sha256-a"),
|
|
2831
|
+
true
|
|
2832
|
+
);
|
|
2833
|
+
assert.equal(
|
|
2834
|
+
await port.writeApplied(applied("sha256-c", SECOND_START), "sha256-a"),
|
|
2835
|
+
false
|
|
2836
|
+
);
|
|
2837
|
+
const read = await port.readApplied();
|
|
2838
|
+
assert.equal(
|
|
2839
|
+
read?.fingerprint,
|
|
2840
|
+
"sha256-b",
|
|
2841
|
+
"the row stands as the first writer left it"
|
|
2842
|
+
);
|
|
2843
|
+
assert.equal(read?.appliedAt.toISOString(), SECOND_START.toISOString());
|
|
2844
|
+
});
|
|
2845
|
+
test("a change lands with the record it supersedes, and is listed newest first", async (t) => {
|
|
2846
|
+
const port = harness.adapter.appliedSettings;
|
|
2847
|
+
if (!port) {
|
|
2848
|
+
missing(t, "appliedSettings");
|
|
2849
|
+
return;
|
|
2850
|
+
}
|
|
2851
|
+
await port.writeApplied(applied("sha256-a", FIRST_START), null);
|
|
2852
|
+
const first = await port.recordChange(
|
|
2853
|
+
changeTo(TWENTY, FIRST_START),
|
|
2854
|
+
applied("sha256-b", FIRST_START, TWENTY),
|
|
2855
|
+
"sha256-a"
|
|
2856
|
+
);
|
|
2857
|
+
const second = await port.recordChange(
|
|
2858
|
+
changeTo(TWENTY_ONE, SECOND_START, TWENTY),
|
|
2859
|
+
applied("sha256-c", SECOND_START, TWENTY_ONE),
|
|
2860
|
+
"sha256-b"
|
|
2861
|
+
);
|
|
2862
|
+
assert.ok(first && second, "both guards held");
|
|
2863
|
+
assert.ok(first.id && second.id && first.id !== second.id, "two distinct ids");
|
|
2864
|
+
assert.equal(first.acknowledgedAt, null);
|
|
2865
|
+
assert.equal(first.acknowledgedBy, null);
|
|
2866
|
+
assert.equal(
|
|
2867
|
+
(await port.readApplied())?.fingerprint,
|
|
2868
|
+
"sha256-c",
|
|
2869
|
+
"the record moved with the change"
|
|
2870
|
+
);
|
|
2871
|
+
const listed = await port.listChanges();
|
|
2872
|
+
assert.deepEqual(
|
|
2873
|
+
listed.map((c) => c.id),
|
|
2874
|
+
[second.id, first.id]
|
|
2875
|
+
);
|
|
2876
|
+
assert.deepEqual(listed[1].previous, SETTINGS);
|
|
2877
|
+
assert.equal(listed[1].current.vatRate, 20);
|
|
2878
|
+
assert.deepEqual(
|
|
2879
|
+
(await port.listChanges({ limit: 1 })).map((c) => c.id),
|
|
2880
|
+
[second.id]
|
|
2881
|
+
);
|
|
2882
|
+
});
|
|
2883
|
+
test("a change whose record has moved on is refused whole: no change, and the record as it was", async (t) => {
|
|
2884
|
+
const port = harness.adapter.appliedSettings;
|
|
2885
|
+
if (!port) {
|
|
2886
|
+
missing(t, "appliedSettings");
|
|
2887
|
+
return;
|
|
2888
|
+
}
|
|
2889
|
+
await port.writeApplied(applied("sha256-a", FIRST_START), null);
|
|
2890
|
+
const refused = await port.recordChange(
|
|
2891
|
+
changeTo(TWENTY, SECOND_START),
|
|
2892
|
+
applied("sha256-b", SECOND_START, TWENTY),
|
|
2893
|
+
"sha256-stale"
|
|
2894
|
+
);
|
|
2895
|
+
assert.equal(refused, null);
|
|
2896
|
+
assert.deepEqual(await port.listChanges(), [], "no change without its record");
|
|
2897
|
+
const read = await port.readApplied();
|
|
2898
|
+
assert.equal(read?.fingerprint, "sha256-a");
|
|
2899
|
+
assert.equal(read?.appliedAt.toISOString(), FIRST_START.toISOString());
|
|
2900
|
+
});
|
|
2901
|
+
test("starts noticing the same difference at once record it once", async (t) => {
|
|
2902
|
+
const port = harness.adapter.appliedSettings;
|
|
2903
|
+
if (!port) {
|
|
2904
|
+
missing(t, "appliedSettings");
|
|
2905
|
+
return;
|
|
2906
|
+
}
|
|
2907
|
+
await port.writeApplied(applied("sha256-a", FIRST_START), null);
|
|
2908
|
+
const attempts = await Promise.all(
|
|
2909
|
+
[1, 2, 3].map(
|
|
2910
|
+
() => port.recordChange(
|
|
2911
|
+
changeTo(TWENTY, SECOND_START),
|
|
2912
|
+
applied("sha256-b", SECOND_START, TWENTY),
|
|
2913
|
+
"sha256-a"
|
|
2914
|
+
)
|
|
2915
|
+
)
|
|
2916
|
+
);
|
|
2917
|
+
const recorded = attempts.filter((change) => change !== null);
|
|
2918
|
+
assert.equal(recorded.length, 1, "exactly one start records the change");
|
|
2919
|
+
assert.deepEqual(
|
|
2920
|
+
(await port.listChanges()).map((c) => c.id),
|
|
2921
|
+
[recorded[0]?.id]
|
|
2922
|
+
);
|
|
2923
|
+
assert.equal((await port.readApplied())?.fingerprint, "sha256-b");
|
|
2924
|
+
});
|
|
2925
|
+
test("several first starts write the record once", async (t) => {
|
|
2926
|
+
const port = harness.adapter.appliedSettings;
|
|
2927
|
+
if (!port) {
|
|
2928
|
+
missing(t, "appliedSettings");
|
|
2929
|
+
return;
|
|
2930
|
+
}
|
|
2931
|
+
const written = await Promise.all(
|
|
2932
|
+
[1, 2, 3].map(() => port.writeApplied(applied("sha256-a", FIRST_START), null))
|
|
2933
|
+
);
|
|
2934
|
+
assert.equal(written.filter(Boolean).length, 1, "exactly one start writes it");
|
|
2935
|
+
assert.equal((await port.readApplied())?.fingerprint, "sha256-a");
|
|
2936
|
+
});
|
|
2937
|
+
test("changes are listed in the order they were recorded, latest first \u2014 not by the moment they carry", async (t) => {
|
|
2938
|
+
const port = harness.adapter.appliedSettings;
|
|
2939
|
+
if (!port) {
|
|
2940
|
+
missing(t, "appliedSettings");
|
|
2941
|
+
return;
|
|
2942
|
+
}
|
|
2943
|
+
await port.writeApplied(applied("sha256-0", FIRST_START, {}), null);
|
|
2944
|
+
const earlierMove = await port.recordChange(
|
|
2945
|
+
changeTo({ vatRate: 1 }, SECOND_START, {}),
|
|
2946
|
+
applied("sha256-1", SECOND_START, { vatRate: 1 }),
|
|
2947
|
+
"sha256-0"
|
|
2948
|
+
);
|
|
2949
|
+
const laterMove = await port.recordChange(
|
|
2950
|
+
changeTo({ vatRate: 2 }, FIRST_START, { vatRate: 1 }),
|
|
2951
|
+
applied("sha256-2", FIRST_START, { vatRate: 2 }),
|
|
2952
|
+
"sha256-1"
|
|
2953
|
+
);
|
|
2954
|
+
assert.ok(earlierMove && laterMove, "both guards held");
|
|
2955
|
+
assert.deepEqual(
|
|
2956
|
+
(await port.listChanges()).map((c) => c.id),
|
|
2957
|
+
[laterMove.id, earlierMove.id]
|
|
2958
|
+
);
|
|
2959
|
+
assert.deepEqual(
|
|
2960
|
+
(await port.listChanges({ limit: 1 })).map((c) => c.id),
|
|
2961
|
+
[laterMove.id]
|
|
2962
|
+
);
|
|
2963
|
+
assert.deepEqual(
|
|
2964
|
+
(await port.listChanges()).map((c) => c.id),
|
|
2965
|
+
[laterMove.id, earlierMove.id],
|
|
2966
|
+
"the same order the second time it is asked"
|
|
2967
|
+
);
|
|
2968
|
+
});
|
|
2969
|
+
test("acknowledging a change is recorded once, and filters it out of what is owed", async (t) => {
|
|
2970
|
+
const port = harness.adapter.appliedSettings;
|
|
2971
|
+
if (!port) {
|
|
2972
|
+
missing(t, "appliedSettings");
|
|
2973
|
+
return;
|
|
2974
|
+
}
|
|
2975
|
+
await port.writeApplied(applied("sha256-a", FIRST_START), null);
|
|
2976
|
+
const change = await port.recordChange(
|
|
2977
|
+
changeTo(TWENTY, FIRST_START),
|
|
2978
|
+
applied("sha256-b", FIRST_START, TWENTY),
|
|
2979
|
+
"sha256-a"
|
|
2980
|
+
);
|
|
2981
|
+
const open = await port.recordChange(
|
|
2982
|
+
changeTo(TWENTY_ONE, SECOND_START, TWENTY),
|
|
2983
|
+
applied("sha256-c", SECOND_START, TWENTY_ONE),
|
|
2984
|
+
"sha256-b"
|
|
2985
|
+
);
|
|
2986
|
+
assert.ok(change && open, "both guards held");
|
|
2987
|
+
const seenAt = /* @__PURE__ */ new Date("2026-09-03T08:00:00.000Z");
|
|
2988
|
+
const acknowledged = await port.acknowledgeChange(
|
|
2989
|
+
change.id,
|
|
2990
|
+
"web:ops@example.com:s1",
|
|
2991
|
+
seenAt
|
|
2992
|
+
);
|
|
2993
|
+
assert.equal(acknowledged?.acknowledgedAt?.toISOString(), seenAt.toISOString());
|
|
2994
|
+
assert.equal(acknowledged?.acknowledgedBy, "web:ops@example.com:s1");
|
|
2995
|
+
const again = await port.acknowledgeChange(
|
|
2996
|
+
change.id,
|
|
2997
|
+
"web:other@example.com:s2",
|
|
2998
|
+
/* @__PURE__ */ new Date("2026-09-04T08:00:00.000Z")
|
|
2999
|
+
);
|
|
3000
|
+
assert.equal(again?.acknowledgedAt?.toISOString(), seenAt.toISOString());
|
|
3001
|
+
assert.equal(again?.acknowledgedBy, "web:ops@example.com:s1");
|
|
3002
|
+
assert.deepEqual(
|
|
3003
|
+
(await port.listChanges({ acknowledged: false })).map((c) => c.id),
|
|
3004
|
+
[open.id]
|
|
3005
|
+
);
|
|
3006
|
+
assert.deepEqual(
|
|
3007
|
+
(await port.listChanges({ acknowledged: true })).map((c) => c.id),
|
|
3008
|
+
[change.id]
|
|
3009
|
+
);
|
|
3010
|
+
assert.equal(
|
|
3011
|
+
await port.acknowledgeChange("no-such-change", "web:ops@example.com:s1", seenAt),
|
|
3012
|
+
null
|
|
714
3013
|
);
|
|
715
3014
|
});
|
|
716
3015
|
});
|