-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathbilling-controls.test.ts
More file actions
242 lines (236 loc) · 6.46 KB
/
Copy pathbilling-controls.test.ts
File metadata and controls
242 lines (236 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* catalogV2.update — billing_controls always fan out to every latest variant.
*
* Contract:
* omit from propagate → still copy billing_controls; items stay drifted
* sparse lane merge / clear → variant matches base next
* two variants → both latest rows get the same controls
* historical variant version → unchanged
*/
import { test } from "bun:test";
import { TestFeature } from "@tests/setup/v2Features.js";
import { initScenario } from "@tests/utils/testInitUtils/initScenario.js";
import chalk from "chalk";
import { expectCatalogResultsCorrect } from "../../../utils/expectCatalogUpdate.js";
import { uniqueTestId } from "../../../utils/uniqueTestId.js";
import {
deleteDbPlans,
expectPlanVersionsCorrect,
} from "../../utils/expectCatalogPlans.js";
import { expectVariantPlanCorrect } from "../utils/expectVariantPointer.js";
import { seedBaseWithVariant } from "../utils/seedVariantPlans.js";
const overageAllowed = [{ feature_id: TestFeature.Messages, enabled: true }];
const autoTopups = [
{
feature_id: TestFeature.Messages,
enabled: true,
threshold: 10,
quantity: 100,
},
];
const spendLimits = [
{
feature_id: TestFeature.Messages,
overage_limit: 50,
enabled: true,
},
];
const usageAlerts = [
{
feature_id: TestFeature.Messages,
enabled: true,
threshold: 80,
threshold_type: "usage_percentage" as const,
basis: "balance" as const,
},
];
test.concurrent(
`${chalk.yellowBright("catalogV2 variants settings: billing_controls fan out without propagate")}`,
async () => {
const { autumnV2_3, ctx } = await initScenario({ setup: [], actions: [] });
const baseId = uniqueTestId("cv2_var_bc");
const variantId = uniqueTestId("cv2_var_bc_eu");
await deleteDbPlans({ ctx, planIds: [baseId, variantId] });
try {
await seedBaseWithVariant({
autumn: autumnV2_3,
baseId,
variantId,
});
const billingControls = {
overage_allowed: overageAllowed,
auto_topups: autoTopups,
};
const response = await autumnV2_3.catalogV2.update({
plans: [{ plan_id: baseId, billing_controls: billingControls }],
});
expectCatalogResultsCorrect({
response,
plans: [
{ id: baseId, action: "update" },
{ id: variantId, action: "update" },
],
});
await expectVariantPlanCorrect({
ctx,
variantPlanId: variantId,
name: "Team EU",
allowances: { [TestFeature.Messages]: 200 },
featureIds: [TestFeature.Messages],
billingControls,
});
} finally {
await deleteDbPlans({ ctx, planIds: [baseId, variantId] });
}
},
);
test.concurrent(
`${chalk.yellowBright("catalogV2 variants settings: billing_controls sparse merge then clear lane")}`,
async () => {
const { autumnV2_3, ctx } = await initScenario({ setup: [], actions: [] });
const baseId = uniqueTestId("cv2_var_bc_sp");
const variantId = uniqueTestId("cv2_var_bc_sp_eu");
await deleteDbPlans({ ctx, planIds: [baseId, variantId] });
try {
await seedBaseWithVariant({
autumn: autumnV2_3,
baseId,
variantId,
});
await autumnV2_3.catalogV2.update({
plans: [
{
plan_id: baseId,
billing_controls: {
overage_allowed: overageAllowed,
spend_limits: spendLimits,
usage_alerts: usageAlerts,
},
},
],
});
await autumnV2_3.catalogV2.update({
plans: [
{
plan_id: baseId,
billing_controls: { spend_limits: [] },
},
],
});
await expectVariantPlanCorrect({
ctx,
variantPlanId: variantId,
allowances: { [TestFeature.Messages]: 200 },
billingControlsExact: {
overage_allowed: overageAllowed,
spend_limits: [],
usage_alerts: usageAlerts,
},
});
} finally {
await deleteDbPlans({ ctx, planIds: [baseId, variantId] });
}
},
);
test.concurrent(
`${chalk.yellowBright("catalogV2 variants settings: billing_controls hit every latest variant")}`,
async () => {
const { autumnV2_3, ctx } = await initScenario({ setup: [], actions: [] });
const baseId = uniqueTestId("cv2_var_bc_2");
const euId = uniqueTestId("cv2_var_bc_2_eu");
const ukId = uniqueTestId("cv2_var_bc_2_uk");
await deleteDbPlans({ ctx, planIds: [baseId, euId, ukId] });
try {
await seedBaseWithVariant({
autumn: autumnV2_3,
baseId,
variantId: euId,
});
await autumnV2_3.catalogV2.update({
plans: [
{
plan_id: baseId,
variants: [{ variant_plan_id: ukId, name: "Team UK" }],
},
],
});
const billingControls = { overage_allowed: overageAllowed };
const response = await autumnV2_3.catalogV2.update({
plans: [{ plan_id: baseId, billing_controls: billingControls }],
});
expectCatalogResultsCorrect({
response,
plans: [
{ id: baseId, action: "update" },
{ id: euId, action: "update" },
{ id: ukId, action: "update" },
],
});
await expectVariantPlanCorrect({
ctx,
variantPlanId: euId,
allowances: { [TestFeature.Messages]: 200 },
billingControls,
});
await expectVariantPlanCorrect({
ctx,
variantPlanId: ukId,
allowances: { [TestFeature.Messages]: 100 },
billingControls,
});
} finally {
await deleteDbPlans({ ctx, planIds: [baseId, euId, ukId] });
}
},
);
test.concurrent(
`${chalk.yellowBright("catalogV2 variants settings: billing_controls latest version only")}`,
async () => {
const { autumnV2_3, ctx } = await initScenario({ setup: [], actions: [] });
const baseId = uniqueTestId("cv2_var_bc_ver");
const variantId = uniqueTestId("cv2_var_bc_ver_eu");
await deleteDbPlans({ ctx, planIds: [baseId, variantId] });
try {
await seedBaseWithVariant({
autumn: autumnV2_3,
baseId,
variantId,
});
await autumnV2_3.catalogV2.update({
plans: [
{
plan_id: variantId,
versioning: "new_version",
active: true,
metadata: { stamp: "v2" },
},
],
});
const billingControls = { overage_allowed: overageAllowed };
await autumnV2_3.catalogV2.update({
plans: [{ plan_id: baseId, billing_controls: billingControls }],
});
await expectPlanVersionsCorrect({
ctx,
planId: variantId,
versions: [1, 2],
});
await expectVariantPlanCorrect({
ctx,
variantPlanId: variantId,
version: 1,
billingControlsExact: {},
allowances: { [TestFeature.Messages]: 200 },
});
await expectVariantPlanCorrect({
ctx,
variantPlanId: variantId,
version: 2,
billingControls,
allowances: { [TestFeature.Messages]: 200 },
});
} finally {
await deleteDbPlans({ ctx, planIds: [baseId, variantId] });
}
},
);