Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/util/licenses/purchase/compute-cost.test.ts
6022 views
1
import { compute_cost } from "./compute-cost";
2
import { decimalMultiply } from "@cocalc/util/stripe/calc";
3
4
const MONTHLY_V1 = 27.15;
5
6
describe("compute-cost v1 pricing", () => {
7
// This is a monthly business subscription for 3 projects with 1 cpu, 2 GB ram and 3 GB disk,
8
// using v1 pricing. On the website right now it says this should cost:
9
// "Cost: USD $27.15 monthly USD $9.05 per project"
10
const monthly1 = MONTHLY_V1;
11
const info1 = {
12
version: "1",
13
end: new Date("2024-01-06T22:00:02.582Z"),
14
type: "quota",
15
user: "business",
16
boost: false,
17
start: new Date("2023-12-05T17:15:55.781Z"),
18
upgrade: "custom",
19
quantity: 3,
20
account_id: "6aae57c6-08f1-4bb5-848b-3ceb53e61ede",
21
custom_cpu: 1,
22
custom_ram: 2,
23
custom_disk: 3,
24
subscription: "monthly",
25
custom_member: true,
26
custom_uptime: "short",
27
custom_dedicated_cpu: 0,
28
custom_dedicated_ram: 0,
29
} as const;
30
31
it("computes the cost", () => {
32
const cost1 = compute_cost(info1);
33
expect(decimalMultiply(cost1.cost_sub_month, cost1.quantity)).toBe(
34
monthly1,
35
);
36
});
37
38
it("computes cost default", () => {
39
const c_v1 = compute_cost(info1);
40
const c_v3 = compute_cost({ ...info1, version: "3" });
41
// @ts-ignore
42
const cWithoutVersion = compute_cost({ ...info1, version: undefined });
43
expect(c_v1).not.toEqual(c_v3);
44
expect(c_v3).toEqual(cWithoutVersion);
45
});
46
47
it("computes correct cost with a different version of pricing params", () => {
48
const info = { ...info1 };
49
// @ts-ignore
50
info.version = "test_1";
51
const cost = compute_cost(info);
52
expect(decimalMultiply(cost.cost_sub_month, cost.quantity)).toBe(54.3);
53
});
54
});
55
56
describe("a couple more consistency checks with prod", () => {
57
// each price below comes from just configuring this on prod
58
59
it("computes the cost of a yearly academic license sub", () => {
60
const yearly = 307.08; // from prod store
61
const info = {
62
version: "1",
63
end: new Date("2024-01-06T22:00:02.582Z"),
64
type: "quota",
65
user: "academic",
66
boost: false,
67
start: new Date("2023-12-05T17:15:55.781Z"),
68
upgrade: "custom",
69
quantity: 3,
70
account_id: "6aae57c6-08f1-4bb5-848b-3ceb53e61ede",
71
custom_cpu: 2,
72
custom_ram: 2,
73
custom_disk: 3,
74
subscription: "yearly",
75
custom_member: true,
76
custom_uptime: "short",
77
custom_dedicated_cpu: 0,
78
custom_dedicated_ram: 0,
79
} as const;
80
const cost = compute_cost(info);
81
expect(decimalMultiply(cost.cost_sub_year, cost.quantity)).toBe(yearly);
82
});
83
84
it("computes the cost of a specific period academic license", () => {
85
const amount = 29.64; // from prod store
86
const info = {
87
version: "1",
88
start: new Date("2024-08-01T00:00:00.000Z"),
89
type: "quota",
90
user: "academic",
91
boost: false,
92
end: new Date("2024-08-31T00:00:00.000Z"),
93
upgrade: "custom",
94
quantity: 3,
95
account_id: "6aae57c6-08f1-4bb5-848b-3ceb53e61ede",
96
custom_cpu: 2,
97
custom_ram: 2,
98
custom_disk: 3,
99
subscription: "no",
100
custom_member: true,
101
custom_uptime: "short",
102
custom_dedicated_cpu: 0,
103
custom_dedicated_ram: 0,
104
} as const;
105
const cost = compute_cost(info);
106
expect(cost.cost).toBe(amount);
107
});
108
});
109
110
describe("compute-cost v3 pricing", () => {
111
// This is a monthly business subscription for 3 projects with 1 cpu, 2 GB ram and 3 GB disk,
112
// using v3 pricing.
113
const monthly3 = 31.5;
114
const info1 = {
115
version: "3",
116
end: new Date("2024-01-06T22:00:02.582Z"),
117
type: "quota",
118
user: "business",
119
boost: false,
120
start: new Date("2023-12-05T17:15:55.781Z"),
121
upgrade: "custom",
122
quantity: 3,
123
account_id: "6aae57c6-08f1-4bb5-848b-3ceb53e61ede",
124
custom_cpu: 1,
125
custom_ram: 2,
126
custom_disk: 3,
127
subscription: "monthly",
128
custom_member: true,
129
custom_uptime: "short",
130
custom_dedicated_cpu: 0,
131
custom_dedicated_ram: 0,
132
} as const;
133
134
it("computes the cost", () => {
135
const cost1 = compute_cost(info1);
136
expect(decimalMultiply(cost1.cost_sub_month, cost1.quantity)).toBe(
137
monthly3,
138
);
139
});
140
141
it("computes correct cost with a different version of pricing params", () => {
142
const info = { ...info1 };
143
// @ts-ignore
144
info.version = "test_1";
145
const cost = compute_cost(info);
146
expect(decimalMultiply(cost.cost_sub_month, cost.quantity)).toBe(54.3);
147
});
148
});
149
150