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