@fantastic.dev/better-stripe 0.86.0 → 0.88.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalObservation/active-scheduled.d.ts +6 -0
  4. package/dist/client/billing/renewalObservation/active-scheduled.d.ts.map +1 -0
  5. package/dist/client/billing/renewalObservation/active-scheduled.js +37 -0
  6. package/dist/client/billing/renewalObservation/active-scheduled.js.map +1 -0
  7. package/dist/client/billing/renewalObservation/common.d.ts +47 -0
  8. package/dist/client/billing/renewalObservation/common.d.ts.map +1 -0
  9. package/dist/client/billing/renewalObservation/common.js +39 -0
  10. package/dist/client/billing/renewalObservation/common.js.map +1 -0
  11. package/dist/client/billing/renewalObservation/configured-baseline.d.ts +76 -0
  12. package/dist/client/billing/renewalObservation/configured-baseline.d.ts.map +1 -0
  13. package/dist/client/billing/renewalObservation/configured-baseline.js +26 -0
  14. package/dist/client/billing/renewalObservation/configured-baseline.js.map +1 -0
  15. package/dist/client/billing/renewalObservation/coupon-discount-joins.d.ts +53 -0
  16. package/dist/client/billing/renewalObservation/coupon-discount-joins.d.ts.map +1 -0
  17. package/dist/client/billing/renewalObservation/coupon-discount-joins.js +103 -0
  18. package/dist/client/billing/renewalObservation/coupon-discount-joins.js.map +1 -0
  19. package/dist/client/billing/renewalObservation/invoice-tax-identity.d.ts +52 -0
  20. package/dist/client/billing/renewalObservation/invoice-tax-identity.d.ts.map +1 -0
  21. package/dist/client/billing/renewalObservation/invoice-tax-identity.js +49 -0
  22. package/dist/client/billing/renewalObservation/invoice-tax-identity.js.map +1 -0
  23. package/dist/client/billing/renewalObservation/pending-and-stage.d.ts +76 -0
  24. package/dist/client/billing/renewalObservation/pending-and-stage.d.ts.map +1 -0
  25. package/dist/client/billing/renewalObservation/pending-and-stage.js +36 -0
  26. package/dist/client/billing/renewalObservation/pending-and-stage.js.map +1 -0
  27. package/dist/client/billing/renewalObservation/phase-price-normalization.d.ts +57 -0
  28. package/dist/client/billing/renewalObservation/phase-price-normalization.d.ts.map +1 -0
  29. package/dist/client/billing/renewalObservation/phase-price-normalization.js +70 -0
  30. package/dist/client/billing/renewalObservation/phase-price-normalization.js.map +1 -0
  31. package/dist/client/billing/renewalObservation/planProjection.d.ts +8 -0
  32. package/dist/client/billing/renewalObservation/planProjection.d.ts.map +1 -0
  33. package/dist/client/billing/renewalObservation/planProjection.js +150 -0
  34. package/dist/client/billing/renewalObservation/planProjection.js.map +1 -0
  35. package/dist/client/billing/renewalObservation/reference-catalog.d.ts +37 -0
  36. package/dist/client/billing/renewalObservation/reference-catalog.d.ts.map +1 -0
  37. package/dist/client/billing/renewalObservation/reference-catalog.js +109 -0
  38. package/dist/client/billing/renewalObservation/reference-catalog.js.map +1 -0
  39. package/dist/client/billing/renewalObservation/schedule-templates.d.ts +74 -0
  40. package/dist/client/billing/renewalObservation/schedule-templates.d.ts.map +1 -0
  41. package/dist/client/billing/renewalObservation/schedule-templates.js +72 -0
  42. package/dist/client/billing/renewalObservation/schedule-templates.js.map +1 -0
  43. package/dist/client/billing/renewalObservation/scheduled-context.d.ts +44 -0
  44. package/dist/client/billing/renewalObservation/scheduled-context.d.ts.map +1 -0
  45. package/dist/client/billing/renewalObservation/scheduled-context.js +64 -0
  46. package/dist/client/billing/renewalObservation/scheduled-context.js.map +1 -0
  47. package/dist/client/billing/renewalObservation/scheduled-identity.d.ts +53 -0
  48. package/dist/client/billing/renewalObservation/scheduled-identity.d.ts.map +1 -0
  49. package/dist/client/billing/renewalObservation/scheduled-identity.js +95 -0
  50. package/dist/client/billing/renewalObservation/scheduled-identity.js.map +1 -0
  51. package/dist/client/billing/renewalObservation/scheduled-normalization.d.ts +59 -0
  52. package/dist/client/billing/renewalObservation/scheduled-normalization.d.ts.map +1 -0
  53. package/dist/client/billing/renewalObservation/scheduled-normalization.js +29 -0
  54. package/dist/client/billing/renewalObservation/scheduled-normalization.js.map +1 -0
  55. package/dist/client/billing/renewalObservation/settings-normalization.d.ts +54 -0
  56. package/dist/client/billing/renewalObservation/settings-normalization.d.ts.map +1 -0
  57. package/dist/client/billing/renewalObservation/settings-normalization.js +53 -0
  58. package/dist/client/billing/renewalObservation/settings-normalization.js.map +1 -0
  59. package/dist/client/billing/renewalObservation/stage-runner.d.ts +4 -0
  60. package/dist/client/billing/renewalObservation/stage-runner.d.ts.map +1 -0
  61. package/dist/client/billing/renewalObservation/stage-runner.js +34 -0
  62. package/dist/client/billing/renewalObservation/stage-runner.js.map +1 -0
  63. package/dist/client/billing/renewalObservation/weeklyPending.d.ts +13 -0
  64. package/dist/client/billing/renewalObservation/weeklyPending.d.ts.map +1 -0
  65. package/dist/client/billing/renewalObservation/weeklyPending.js +55 -0
  66. package/dist/client/billing/renewalObservation/weeklyPending.js.map +1 -0
  67. package/dist/client/billing/renewalV2/binding.d.ts +1 -1
  68. package/dist/client/billing/renewalV2/binding.d.ts.map +1 -1
  69. package/dist/client/billing/renewalV2/binding.js.map +1 -1
  70. package/dist/client/billing/renewalV2/configureInput.d.ts +1 -0
  71. package/dist/client/billing/renewalV2/configureInput.d.ts.map +1 -1
  72. package/dist/client/billing/renewalV2/configureInput.js +3 -3
  73. package/dist/client/billing/renewalV2/configureInput.js.map +1 -1
  74. package/dist/client/billing/renewalV2/configureReceipt.d.ts +19 -0
  75. package/dist/client/billing/renewalV2/configureReceipt.d.ts.map +1 -0
  76. package/dist/client/billing/renewalV2/configureReceipt.js +91 -0
  77. package/dist/client/billing/renewalV2/configureReceipt.js.map +1 -0
  78. package/dist/client/billing/renewalV2/index.d.ts +417 -0
  79. package/dist/client/billing/renewalV2/index.d.ts.map +1 -1
  80. package/dist/client/billing/renewalV2/index.js +73 -3
  81. package/dist/client/billing/renewalV2/index.js.map +1 -1
  82. package/dist/client/billing/renewalV2/observation.d.ts +19 -0
  83. package/dist/client/billing/renewalV2/observation.d.ts.map +1 -0
  84. package/dist/client/billing/renewalV2/observation.js +95 -0
  85. package/dist/client/billing/renewalV2/observation.js.map +1 -0
  86. package/dist/client/billing/renewalV2/observationInput.d.ts +77 -0
  87. package/dist/client/billing/renewalV2/observationInput.d.ts.map +1 -0
  88. package/dist/client/billing/renewalV2/observationInput.js +140 -0
  89. package/dist/client/billing/renewalV2/observationInput.js.map +1 -0
  90. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
  91. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  92. package/dist/client/renewal.d.ts +4 -2
  93. package/dist/client/renewal.d.ts.map +1 -1
  94. package/dist/client/renewal.js +2 -1
  95. package/dist/client/renewal.js.map +1 -1
  96. package/dist/client/usage/observationClient.d.ts +2 -2
  97. package/dist/client/usage/observationClient.d.ts.map +1 -1
  98. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  99. package/dist/component/balanceFunding/mutations.d.ts +16 -16
  100. package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
  101. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  102. package/docs/client-api.md +26 -0
  103. package/docs/renewal-context-reads.md +5 -1
  104. package/docs/renewal-material-observation.md +124 -0
  105. package/docs/renewal-phase-assembly.md +68 -0
  106. package/docs/renewal-price-replacements.md +3 -0
  107. package/package.json +1 -1
  108. package/src/client/billing/renewalObservation/active-scheduled.ts +67 -0
  109. package/src/client/billing/renewalObservation/common.ts +83 -0
  110. package/src/client/billing/renewalObservation/configured-baseline.ts +55 -0
  111. package/src/client/billing/renewalObservation/coupon-discount-joins.ts +163 -0
  112. package/src/client/billing/renewalObservation/invoice-tax-identity.ts +65 -0
  113. package/src/client/billing/renewalObservation/pending-and-stage.ts +89 -0
  114. package/src/client/billing/renewalObservation/phase-price-normalization.ts +106 -0
  115. package/src/client/billing/renewalObservation/planProjection.ts +177 -0
  116. package/src/client/billing/renewalObservation/reference-catalog.ts +150 -0
  117. package/src/client/billing/renewalObservation/schedule-templates.ts +118 -0
  118. package/src/client/billing/renewalObservation/scheduled-context.ts +98 -0
  119. package/src/client/billing/renewalObservation/scheduled-identity.ts +175 -0
  120. package/src/client/billing/renewalObservation/scheduled-normalization.ts +49 -0
  121. package/src/client/billing/renewalObservation/settings-normalization.ts +60 -0
  122. package/src/client/billing/renewalObservation/stage-runner.ts +30 -0
  123. package/src/client/billing/renewalObservation/weeklyPending.ts +81 -0
  124. package/src/client/billing/renewalV2/binding.ts +4 -1
  125. package/src/client/billing/renewalV2/configureInput.ts +3 -3
  126. package/src/client/billing/renewalV2/configureReceipt.test.ts +558 -0
  127. package/src/client/billing/renewalV2/configureReceipt.ts +96 -0
  128. package/src/client/billing/renewalV2/index.ts +131 -6
  129. package/src/client/billing/renewalV2/observation.boundary-review.test.ts +49 -0
  130. package/src/client/billing/renewalV2/observation.test.ts +334 -0
  131. package/src/client/billing/renewalV2/observation.ts +149 -0
  132. package/src/client/billing/renewalV2/observationInput.ts +167 -0
  133. package/src/client/renewal.ts +10 -0
@@ -0,0 +1,96 @@
1
+ import { z } from "zod";
2
+ import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
3
+ import type { ObjectData } from "../renewalAggregate/shared.js";
4
+ import { validateConfigurationFields } from "./configureInput.js";
5
+
6
+ const stop = () => {
7
+ throw Error("Invalid renewal configuration receipt input.");
8
+ };
9
+ function ownData(raw: unknown, required: string[], optional: string[] = []) {
10
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return stop();
11
+ const keys = Reflect.ownKeys(raw);
12
+ if (
13
+ keys.some(
14
+ (k) => typeof k !== "string" || ![...required, ...optional].includes(k),
15
+ ) ||
16
+ required.some((k) => !keys.includes(k))
17
+ )
18
+ return stop();
19
+ const fields: ObjectData = {};
20
+ for (const key of keys) {
21
+ const d = Object.getOwnPropertyDescriptor(raw, key);
22
+ if (!d || !d.enumerable || !("value" in d)) return stop();
23
+ fields[key as string] = d.value;
24
+ }
25
+ return fields;
26
+ }
27
+ function bareBody(raw: unknown) {
28
+ if (
29
+ raw !== null &&
30
+ typeof raw === "object" &&
31
+ Object.prototype.hasOwnProperty.call(raw, "lastResponse")
32
+ )
33
+ stop();
34
+ }
35
+ function closed(raw: unknown) {
36
+ const root = ownData(raw, [
37
+ "configuration",
38
+ "actualDispatch",
39
+ "providerSnapshot",
40
+ "transport",
41
+ ]);
42
+ validateConfigurationFields(root.configuration);
43
+ ownData(root.actualDispatch, [
44
+ "scope",
45
+ "apiVersion",
46
+ "method",
47
+ "path",
48
+ "payload",
49
+ "key",
50
+ "firstDispatchedAt",
51
+ ]);
52
+ ownData(root.transport, ["status"], ["requestId"]);
53
+ bareBody(root.providerSnapshot);
54
+ return root;
55
+ }
56
+ const input = z
57
+ .object({
58
+ configuration: z.unknown(),
59
+ actualDispatch: z
60
+ .object({
61
+ scope: z.literal("platform"),
62
+ apiVersion: z.string(),
63
+ method: z.literal("POST"),
64
+ path: z.string(),
65
+ payload: z.unknown(),
66
+ key: z.string(),
67
+ firstDispatchedAt: z
68
+ .number()
69
+ .int()
70
+ .nonnegative()
71
+ .max(Number.MAX_SAFE_INTEGER),
72
+ })
73
+ .strict(),
74
+ // Validate, but never reconstruct, the complete owned JSON body.
75
+ providerSnapshot: z.custom<ObjectData>(
76
+ (v) => v !== null && typeof v === "object" && !Array.isArray(v),
77
+ ),
78
+ transport: z
79
+ .object({
80
+ status: z.number().int().min(200).max(299),
81
+ requestId: z.string().min(1).max(255).optional(),
82
+ })
83
+ .strict(),
84
+ })
85
+ .strict();
86
+ export function captureConfigurationReceipt(raw: unknown) {
87
+ if (raw === null || typeof raw !== "object") stop();
88
+ const prototype = Object.getPrototypeOf(raw);
89
+ if (prototype !== Object.prototype && prototype !== null) stop();
90
+ const original = closed(raw);
91
+ const captured = renewalSnapshot(original);
92
+ closed(captured);
93
+ // Catch metadata introduced by reflection during capture, including hidden data.
94
+ bareBody(original.providerSnapshot);
95
+ return input.parse(captured);
96
+ }
@@ -27,6 +27,7 @@ import {
27
27
  assertAttachmentIdentity,
28
28
  } from "./receipt.js";
29
29
  import { captureConfiguration } from "./configureInput.js";
30
+ import { captureConfigurationReceipt } from "./configureReceipt.js";
30
31
 
31
32
  export type PrepareRenewalPriceReplacementV2Options =
32
33
  PrepareRenewalPriceReplacementOptions;
@@ -243,11 +244,9 @@ export type ProjectedRenewalConfigurationV2 = Omit<
243
244
  export type RenewalConfigurationProjectionFailure = Failure;
244
245
 
245
246
  /** Pure raw-only configuration candidate; provider qualification remains required. */
246
- export async function projectRenewalConfigurationV2(
247
+ export async function reconstructConfigurationV2(
247
248
  options: ProjectRenewalConfigurationV2Options,
248
- ): Promise<
249
- ProjectedRenewalConfigurationV2 | RenewalConfigurationProjectionFailure
250
- > {
249
+ ) {
251
250
  const args = captureConfiguration(options);
252
251
  let ownedScheduleId: string | undefined;
253
252
  try {
@@ -322,7 +321,7 @@ export async function projectRenewalConfigurationV2(
322
321
  schedule.default_settings,
323
322
  ),
324
323
  };
325
- return renewalSnapshot({
324
+ const result: ProjectedRenewalConfigurationV2 = renewalSnapshot({
326
325
  ...request,
327
326
  status: "projected_configure_request" as const,
328
327
  providerQualification: "required" as const,
@@ -332,7 +331,133 @@ export async function projectRenewalConfigurationV2(
332
331
  binding: await renewalBindingV2("renewal-configure", request),
333
332
  },
334
333
  });
334
+ return { result, material: { args, prepared, receipt, reads } };
335
335
  } catch (error) {
336
- return failure(error, ownedScheduleId);
336
+ return { result: failure(error, ownedScheduleId), material: undefined };
337
+ }
338
+ }
339
+
340
+ /** Pure V2 projection; the reconstruction context stays internal to fixed callers. */
341
+ export async function projectRenewalConfigurationV2(
342
+ options: ProjectRenewalConfigurationV2Options,
343
+ ): Promise<
344
+ ProjectedRenewalConfigurationV2 | RenewalConfigurationProjectionFailure
345
+ > {
346
+ return (await reconstructConfigurationV2(options)).result;
347
+ }
348
+
349
+ export type ConstructRenewalConfigurationReceiptV2Options = {
350
+ configuration: ProjectRenewalConfigurationV2Options;
351
+ actualDispatch: Pick<
352
+ ProjectedRenewalConfigurationV2,
353
+ | "scope"
354
+ | "apiVersion"
355
+ | "method"
356
+ | "path"
357
+ | "payload"
358
+ | "key"
359
+ | "firstDispatchedAt"
360
+ >;
361
+ providerSnapshot: Record<string, unknown>;
362
+ transport: { status: number; requestId?: string };
363
+ };
364
+ export type RenewalConfigurationReceiptV2 = {
365
+ version: 2;
366
+ stage: "configure_acknowledged";
367
+ scope: "platform";
368
+ verification: "owned_response_only";
369
+ apiVersion: string;
370
+ logicalBinding: string;
371
+ request: ProjectedRenewalConfigurationV2;
372
+ attachReceiptBinding: string;
373
+ scheduleId: string;
374
+ subscriptionId: string;
375
+ customerAccountId: string;
376
+ billingCustomerId: string;
377
+ livemode: boolean;
378
+ providerSnapshot: Record<string, unknown>;
379
+ transport: { status: number; requestId?: string };
380
+ providerSnapshotBinding: string;
381
+ defaultSettingsBinding?: string;
382
+ binding: string;
383
+ };
384
+
385
+ /** Unsigned operation consistency only. Does not authenticate an acknowledgement. */
386
+ export async function constructRenewalConfigurationReceiptV2(
387
+ options: ConstructRenewalConfigurationReceiptV2Options,
388
+ ): Promise<RenewalConfigurationReceiptV2> {
389
+ try {
390
+ const args = captureConfigurationReceipt(options);
391
+ const configuration =
392
+ args.configuration as ProjectRenewalConfigurationV2Options;
393
+ const request = await projectRenewalConfigurationV2(configuration);
394
+ if (request.status !== "projected_configure_request") throw Error();
395
+ const { scope, apiVersion, method, path, payload, key, firstDispatchedAt } =
396
+ request;
397
+ if (
398
+ !equal(args.actualDispatch, {
399
+ scope,
400
+ apiVersion,
401
+ method,
402
+ path,
403
+ payload,
404
+ key,
405
+ firstDispatchedAt,
406
+ })
407
+ )
408
+ throw Error();
409
+ // These raw selectors have just been revalidated through complete C1-C6.
410
+ const expected = configuration.preparation.expected;
411
+ const billingCustomerId = resourceId("cus").parse(
412
+ (configuration.billingCustomer as Record<string, unknown>).id,
413
+ );
414
+ assertAttachmentIdentity(args.providerSnapshot, {
415
+ scheduleId: request.scheduleId,
416
+ subscriptionId: expected.subscriptionId,
417
+ customerAccountId: expected.customerAccountId,
418
+ billingCustomerId,
419
+ livemode: expected.livemode,
420
+ });
421
+ const providerSnapshotBinding = await renewalBindingV2(
422
+ "renewal-configured-baseline",
423
+ args.providerSnapshot,
424
+ );
425
+ const defaultsPresent = Object.prototype.hasOwnProperty.call(
426
+ args.providerSnapshot,
427
+ "default_settings",
428
+ );
429
+ const defaults = defaultsPresent
430
+ ? {
431
+ defaultSettingsBinding: await renewalBindingV2(
432
+ "renewal-configured-defaults",
433
+ args.providerSnapshot.default_settings,
434
+ ),
435
+ }
436
+ : {};
437
+ const receipt = {
438
+ version: 2 as const,
439
+ stage: "configure_acknowledged" as const,
440
+ scope: "platform" as const,
441
+ verification: "owned_response_only" as const,
442
+ apiVersion: request.apiVersion,
443
+ logicalBinding: request.logicalBinding,
444
+ request,
445
+ attachReceiptBinding: request.attachReceiptBinding,
446
+ scheduleId: request.scheduleId,
447
+ subscriptionId: expected.subscriptionId,
448
+ customerAccountId: expected.customerAccountId,
449
+ billingCustomerId,
450
+ livemode: expected.livemode,
451
+ providerSnapshot: args.providerSnapshot,
452
+ transport: args.transport,
453
+ providerSnapshotBinding,
454
+ ...defaults,
455
+ };
456
+ return renewalSnapshot({
457
+ ...receipt,
458
+ binding: await renewalBindingV2("renewal-configure-receipt", receipt),
459
+ });
460
+ } catch {
461
+ throw Error("Invalid renewal configuration receipt input.");
337
462
  }
338
463
  }
@@ -0,0 +1,49 @@
1
+ import { expect, it } from "vitest";
2
+ import { inspectRenewalScheduleMaterialV2 } from "../../index.js";
3
+ import { observationInput } from "../../../../tests/renewalObservation/fixtures.js";
4
+
5
+ it("retains a supplied terminal string as unsigned facts only", async () => {
6
+ const raw = observationInput();
7
+ raw.reads.schedule.status = "canceled";
8
+ expect(await inspectRenewalScheduleMaterialV2(raw)).toMatchObject({
9
+ predicate: "terminal_facts_only",
10
+ providerStatus: "canceled",
11
+ effectiveness: "unknown",
12
+ acknowledgementEstablished: false,
13
+ authenticationEstablished: false,
14
+ });
15
+ });
16
+
17
+ it("does not coerce an array-shaped status into a known terminal fact", async () => {
18
+ const raw = observationInput();
19
+ (raw.reads.schedule as Record<string, unknown>).status = ["canceled"];
20
+ let result;
21
+ try {
22
+ result = await inspectRenewalScheduleMaterialV2(raw);
23
+ } catch (error) {
24
+ expect(error).toBeInstanceOf(Error);
25
+ expect((error as Error).message).toBe("Invalid raw renewal observation.");
26
+ return;
27
+ }
28
+ expect(["unknown", "conflict"]).toContain(result.predicate);
29
+ });
30
+
31
+ it.each([["canceled"], {}, null, 7, false])(
32
+ "keeps malformed bare status %j outside terminal facts",
33
+ async (status) => {
34
+ const raw = observationInput();
35
+ (raw.reads.schedule as Record<string, unknown>).status = status;
36
+ expect(await inspectRenewalScheduleMaterialV2(raw)).toMatchObject({
37
+ predicate: "unknown",
38
+ reason: "invalid_schedule_status",
39
+ });
40
+ },
41
+ );
42
+ it("retains unknown string status disposition", async () => {
43
+ const raw = observationInput();
44
+ raw.reads.schedule.status = "future_provider_status";
45
+ expect(await inspectRenewalScheduleMaterialV2(raw)).toMatchObject({
46
+ predicate: "unknown",
47
+ reason: "unsupported_schedule_status",
48
+ });
49
+ });
@@ -0,0 +1,334 @@
1
+ import { projectRenewalConfigurationV2 } from "./index.js";
2
+ import { it, expect } from "vitest";
3
+ import * as sdk from "./observation.js";
4
+ import { observationInput } from "../../../../tests/renewalObservation/fixtures.js";
5
+ const inspect = (raw: unknown) => {
6
+ expect((sdk as any).inspectRenewalScheduleMaterialV2).toBeTypeOf("function");
7
+ return (sdk as any).inspectRenewalScheduleMaterialV2(raw);
8
+ };
9
+ it("matches full classic changed-cadence weekly pending material without a configure ACK", async () => {
10
+ const raw = observationInput();
11
+ const before = structuredClone(raw);
12
+ const r = await inspect(raw);
13
+ expect(r).toMatchObject({
14
+ predicate: "pending_material_match",
15
+ acknowledgementEstablished: false,
16
+ authenticationEstablished: false,
17
+ providerQualificationRequired: true,
18
+ });
19
+ expect(raw).toEqual(before);
20
+ expect(raw.configuration.preparation.subscription).toHaveProperty(
21
+ "latest_invoice",
22
+ "in_original",
23
+ );
24
+ });
25
+ it("matches a unique full native active item only when the retained invoice pointer is coherent", async () => {
26
+ const raw = observationInput(true);
27
+ expect(raw.reads.subscription.latest_invoice).toBe("in_original");
28
+ expect(raw.reads.subscription.items.data[0].plan.object).toBe("plan");
29
+ expect(await inspect(raw)).toMatchObject({
30
+ predicate: "current_target_material_match",
31
+ observedItemId: "si_successor",
32
+ });
33
+ });
34
+ it("keeps I0 to I1 native invoice-pointer changes conflicting instead of dropping required material", async () => {
35
+ const raw = observationInput(true);
36
+ raw.reads.subscription.latest_invoice = "in_successor";
37
+ expect(raw.configuration.preparation.subscription.latest_invoice).toBe(
38
+ "in_original",
39
+ );
40
+ expect(await inspect(raw)).toMatchObject({
41
+ predicate: "conflict",
42
+ reason: "effective_full_material_drift",
43
+ });
44
+ });
45
+
46
+ it("keeps an unproved monthly pending duration unknown without treating reads as an ACK", async () => {
47
+ expect(await inspect(observationInput(false, false))).toMatchObject({
48
+ predicate: "unknown",
49
+ reason: "future_duration_not_bound",
50
+ acknowledgementEstablished: false,
51
+ });
52
+ });
53
+ it.each([-1, 1, 604800])(
54
+ "rejects a full weekly future phase whose end differs by %i seconds",
55
+ async (delta) => {
56
+ const raw = observationInput();
57
+ raw.reads.schedule.phases[1].end_date += delta;
58
+ expect(await inspect(raw)).toMatchObject({
59
+ predicate: "conflict",
60
+ reason: "target_phase_duration_mismatch",
61
+ });
62
+ },
63
+ );
64
+ it("does not use the weekly rule for flexible material", async () => {
65
+ const raw = observationInput();
66
+ const mode = {
67
+ type: "flexible",
68
+ flexible: { proration_discounts: "included" },
69
+ };
70
+ for (const sub of [
71
+ raw.configuration.preparation.subscription,
72
+ raw.configuration.attachReceipt.attachRequest.preparation.subscription,
73
+ raw.configuration.freshSubscription,
74
+ raw.reads.subscription,
75
+ ])
76
+ Object.assign(sub, { billing_mode: mode });
77
+ for (const schedule of [
78
+ raw.configuration.schedule,
79
+ raw.configuration.attachReceipt.providerSnapshot,
80
+ raw.reads.schedule,
81
+ ])
82
+ Object.assign(schedule, { billing_mode: mode });
83
+ expect(await inspect(raw)).toMatchObject({
84
+ predicate: "unknown",
85
+ reason: "future_duration_not_bound",
86
+ });
87
+ });
88
+ it("does not omit latest_invoice to manufacture a full native positive", async () => {
89
+ const raw = observationInput(true);
90
+ delete (raw.reads.subscription as Record<string, unknown>).latest_invoice;
91
+ expect(await inspect(raw)).toMatchObject({
92
+ predicate: "unknown",
93
+ reason: "missing_latest_invoice",
94
+ });
95
+ });
96
+ it("retains canceled terminal facts without assigning effectiveness", async () => {
97
+ const raw = observationInput();
98
+ raw.reads.schedule.status = "canceled";
99
+ expect(await inspect(raw)).toMatchObject({
100
+ predicate: "terminal_facts_only",
101
+ effectiveness: "unknown",
102
+ });
103
+ });
104
+ it.each(["prototype", "getter", "symbol", "hidden"])(
105
+ "rejects non-JSON %s roots before invoking a getter",
106
+ async (kind) => {
107
+ const raw: any = observationInput();
108
+ let calls = 0;
109
+ if (kind === "prototype") Object.setPrototypeOf(raw, { custom: true });
110
+ if (kind === "getter")
111
+ Object.defineProperty(raw, "reads", {
112
+ enumerable: true,
113
+ get() {
114
+ calls++;
115
+ throw Error("private");
116
+ },
117
+ });
118
+ if (kind === "symbol") raw[Symbol("private")] = true;
119
+ if (kind === "hidden")
120
+ Object.defineProperty(raw, "hidden", { value: "private" });
121
+ await expect(inspect(raw)).rejects.toThrow(
122
+ "Invalid raw renewal observation",
123
+ );
124
+ expect(calls).toBe(0);
125
+ },
126
+ );
127
+ it("rejects nested hidden SDK response metadata instead of stripping it", async () => {
128
+ const raw = observationInput();
129
+ let calls = 0;
130
+ Object.defineProperty(raw.reads.schedule, "lastResponse", {
131
+ get() {
132
+ calls++;
133
+ throw Error("private");
134
+ },
135
+ });
136
+ await expect(inspect(raw)).rejects.toThrow("Invalid raw renewal observation");
137
+ expect(calls).toBe(0);
138
+ });
139
+ it("captures reads before asynchronous digest work", async () => {
140
+ const raw = observationInput(true);
141
+ const pending = inspect(raw);
142
+ raw.reads.subscription.latest_invoice = "in_mutated";
143
+ raw.reads.schedule.phases[1].end_date++;
144
+ expect(await pending).toMatchObject({
145
+ predicate: "current_target_material_match",
146
+ });
147
+ });
148
+ it("retains unknown own special fields in the full subscription comparison", async () => {
149
+ const raw = observationInput(true);
150
+ Object.defineProperty(raw.reads.subscription, "__proto__", {
151
+ value: { retained: true },
152
+ enumerable: true,
153
+ });
154
+ expect(await inspect(raw)).toMatchObject({
155
+ predicate: "conflict",
156
+ reason: "effective_full_material_drift",
157
+ });
158
+ });
159
+ it("rejects the new API's whole input byte/node bounds", async () => {
160
+ const raw: any = observationInput();
161
+ raw.reads.subscription.extra = "🧪".repeat(130000);
162
+ await expect(inspect(raw)).rejects.toThrow();
163
+ const nodes: any = observationInput();
164
+ nodes.reads.subscription.extra = Array.from({ length: 10001 }, () => null);
165
+ await expect(inspect(nodes)).rejects.toThrow();
166
+ });
167
+
168
+ async function withReceipt(active = false, weekly = false) {
169
+ const raw: any = observationInput(active, weekly),
170
+ baseline = observationInput(false, weekly);
171
+ const request = await projectRenewalConfigurationV2(raw.configuration);
172
+ if (request.status !== "projected_configure_request") throw Error("fixture");
173
+ const { scope, apiVersion, method, path, payload, key, firstDispatchedAt } =
174
+ request;
175
+ raw.configureReceipt = {
176
+ configuration: structuredClone(raw.configuration),
177
+ actualDispatch: {
178
+ scope,
179
+ apiVersion,
180
+ method,
181
+ path,
182
+ payload,
183
+ key,
184
+ firstDispatchedAt,
185
+ },
186
+ providerSnapshot: baseline.reads.schedule,
187
+ transport: { status: 200, requestId: "req_material" },
188
+ };
189
+ return raw;
190
+ }
191
+ it("uses complete raw C9 historical material for monthly pending without authenticating it", async () => {
192
+ expect(await inspect(await withReceipt())).toMatchObject({
193
+ predicate: "pending_material_match",
194
+ acknowledgementEstablished: false,
195
+ authenticationEstablished: false,
196
+ });
197
+ });
198
+ it("does not let a C9 receipt authorize a new native invoice pointer", async () => {
199
+ const raw = await withReceipt(true);
200
+ raw.reads.subscription.latest_invoice = "in_successor";
201
+ expect(await inspect(raw)).toMatchObject({
202
+ predicate: "conflict",
203
+ reason: "effective_full_material_drift",
204
+ });
205
+ });
206
+ it("rejects differing repeated raw C9 configuration", async () => {
207
+ const raw = await withReceipt();
208
+ raw.configureReceipt.configuration.configureKey = "different";
209
+ expect(await inspect(raw)).toMatchObject({
210
+ predicate: "conflict",
211
+ reason: "invalid_repeated_configuration",
212
+ });
213
+ });
214
+ it.each([
215
+ "scope",
216
+ "apiVersion",
217
+ "method",
218
+ "path",
219
+ "payload",
220
+ "key",
221
+ "firstDispatchedAt",
222
+ ])("never trusts a tampered raw C9 dispatch %s", async (field) => {
223
+ const raw = await withReceipt();
224
+ raw.configureReceipt.actualDispatch[field] =
225
+ field === "payload" ? {} : field === "firstDispatchedAt" ? 1 : "foreign";
226
+ if (field === "scope" || field === "method")
227
+ await expect(inspect(raw)).rejects.toThrow(
228
+ "Invalid raw renewal observation.",
229
+ );
230
+ else
231
+ expect(await inspect(raw)).toMatchObject({
232
+ predicate: "unknown",
233
+ reason: "incomplete_or_unsupported_schema",
234
+ });
235
+ });
236
+ it("captures repeated C9 input before any asynchronous digest", async () => {
237
+ const raw = await withReceipt();
238
+ const pending = inspect(raw);
239
+ raw.configureReceipt.configuration.configureKey = "changed_after_capture";
240
+ expect(await pending).toMatchObject({ predicate: "pending_material_match" });
241
+ });
242
+ it("does not create a native positive by omitting both original and current invoice pointers", async () => {
243
+ const raw: any = observationInput(true);
244
+ for (const sub of [
245
+ raw.configuration.preparation.subscription,
246
+ raw.configuration.attachReceipt.attachRequest.preparation.subscription,
247
+ raw.configuration.freshSubscription,
248
+ raw.reads.subscription,
249
+ ])
250
+ delete sub.latest_invoice;
251
+ expect(await inspect(raw)).toMatchObject({
252
+ predicate: "unknown",
253
+ reason: "missing_latest_invoice",
254
+ });
255
+ });
256
+ it("rejects metadata introduced by descriptor reflection after the own-key snapshot", async () => {
257
+ const raw: any = observationInput(true),
258
+ target = raw.reads.subscription;
259
+ let introduced = false;
260
+ raw.reads.subscription = new Proxy(target, {
261
+ getOwnPropertyDescriptor(t, key) {
262
+ const d = Reflect.getOwnPropertyDescriptor(t, key);
263
+ if (key === "items" && !introduced) {
264
+ introduced = true;
265
+ Object.defineProperty(t, "lastResponse", {
266
+ value: { statusCode: 200 },
267
+ configurable: true,
268
+ });
269
+ }
270
+ return d;
271
+ },
272
+ });
273
+ await expect(inspect(raw)).rejects.toThrow(
274
+ "Invalid raw renewal observation.",
275
+ );
276
+ expect(introduced).toBe(true);
277
+ });
278
+
279
+ it("keeps multi-week pending duration unsupported without C9 baseline evidence", async () => {
280
+ expect(await inspect(observationInput(false, true, 2))).toMatchObject({
281
+ predicate: "unknown",
282
+ reason: "future_duration_not_bound",
283
+ });
284
+ });
285
+ it("retains paid settings and full Customer/Coupon unknown material despite a correct weekly end", async () => {
286
+ for (const change of [
287
+ (x: any) => {
288
+ x.reads.billingCustomer.new_unknown = "drift";
289
+ },
290
+ (x: any) => {
291
+ x.reads.coupons[0].times_redeemed++;
292
+ },
293
+ (x: any) => {
294
+ x.reads.subscription.items.data[0].plan.nickname = "drift";
295
+ },
296
+ (x: any) => {
297
+ x.reads.subscription.metadata.keep = "drift";
298
+ },
299
+ ]) {
300
+ const raw = observationInput();
301
+ change(raw);
302
+ expect((await inspect(raw)).predicate).not.toBe("pending_material_match");
303
+ }
304
+ });
305
+ it("accepts plain null-prototype and benign Proxy roots without evaluating caller code", async () => {
306
+ const raw = observationInput(true);
307
+ const nullRoot = Object.assign(Object.create(null), raw);
308
+ expect(await inspect(nullRoot)).toMatchObject({
309
+ predicate: "current_target_material_match",
310
+ });
311
+ let prototypes = 0;
312
+ const proxy = new Proxy(raw, {
313
+ getPrototypeOf(t) {
314
+ prototypes++;
315
+ return Reflect.getPrototypeOf(t);
316
+ },
317
+ });
318
+ expect(await inspect(proxy)).toMatchObject({
319
+ predicate: "current_target_material_match",
320
+ });
321
+ expect(prototypes).toBe(1);
322
+ });
323
+ it("rejects a nested toJSON function without calling it", async () => {
324
+ const raw: any = observationInput();
325
+ let calls = 0;
326
+ raw.reads.subscription.toJSON = () => {
327
+ calls++;
328
+ throw Error("private");
329
+ };
330
+ await expect(inspect(raw)).rejects.toThrow(
331
+ "Invalid raw renewal observation.",
332
+ );
333
+ expect(calls).toBe(0);
334
+ });