@cocalc/server
Version:
CoCalc server functionality: functions used by either the hub and the next.js server
174 lines • 7.28 kB
JavaScript
"use strict";
/*
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// test produce ID and pricing
const billing_1 = require("@cocalc/util/consts/billing");
const utils_1 = require("@cocalc/util/licenses/purchase/utils");
const compute_cost_1 = require("@cocalc/util/licenses/purchase/compute-cost");
const misc_1 = require("@cocalc/util/misc");
const timecalcs_1 = require("@cocalc/util/stripe/timecalcs");
const expect_1 = __importDefault(require("expect"));
const charge_1 = require("./licenses/purchase/charge");
const product_id_1 = require("./licenses/purchase/product-id");
const consts_1 = require("@cocalc/util/licenses/purchase/consts");
// TODO: some tests are ignored if the machine is not running on UTC.
// Ideally, this is taken into account, but that's not implemented.
const isUTC = new Date().getTimezoneOffset() === 0;
describe("product id and compute cost", () => {
const info1 = {
type: "quota",
user: "academic",
upgrade: "custom",
custom_uptime: "short",
custom_ram: 1,
custom_cpu: 1,
custom_disk: 1,
custom_member: true,
subscription: "no",
start: (0, timecalcs_1.startOfDay)(new Date("2022-04-28 12:00")),
end: (0, timecalcs_1.endOfDay)(new Date("2022-05-07 12:00")),
custom_dedicated_ram: 0,
custom_dedicated_cpu: 0,
};
it.each([1, 2, 10, 15])("id with quantity %p", (quantity) => {
const id = (0, product_id_1.getProductId)({ ...info1, quantity });
(0, expect_1.default)(id).toEqual(`license_a0b0c1d1m1p10r1_v2`);
});
it.each([1, 2, 10, 15])("compute price quantity %p", (quantity) => {
const base = (0, compute_cost_1.compute_cost)({ ...info1, quantity: 1 }).cost;
const cost = (0, compute_cost_1.compute_cost)({ ...info1, quantity });
const cexp = (0, misc_1.round2)(base * quantity);
(0, expect_1.default)((0, misc_1.round2)(cost.cost)).toEqual(cexp);
(0, expect_1.default)(Math.abs((0, misc_1.round2)(cost.discounted_cost) - (0, misc_1.round2)(consts_1.COSTS.online_discount * cexp))).toBeLessThan(0.01);
});
it.each([
[1, 13333, 1],
[2, 13333, 10],
[3, 13333, 10],
[4, 13333, 50],
[5, 13333, 100],
[6, 13333, 5],
[7, 13333, 100],
[8, 14329, 5],
[9, 15921, 10],
[10, 17500, 1],
[15, 25500, 1],
])("compute price days %p → price %p", (days, price, quantity) => {
price /= 100;
const info2 = {
...info1,
quantity,
end: (0, timecalcs_1.endOfDay)(new Date(info1.start.getTime() + days * billing_1.ONE_DAY_MS)),
};
info2.cost = (0, compute_cost_1.compute_cost)(info2);
//console.log(days, info2.cost);
const unit_amount = (0, charge_1.unitAmount)(info2);
(0, expect_1.default)(unit_amount).toEqual(Math.round(price));
const total_exp = Math.round(price * quantity);
// this test checks if the displayed amount matches the invoice amount
// see notes about using "round2" in compute_cost
(0, expect_1.default)((0, utils_1.money)(info2.cost.cost, true)).toEqual(`$${total_exp / 100}`);
});
it("specific start/end date", () => {
const info2 = {
...info1,
quantity: 1,
start: new Date("2022-04-28T10:08:10.072Z"),
end: new Date("2022-05-05T10:08:10.072Z"),
};
info2.cost = (0, compute_cost_1.compute_cost)(info2);
(0, expect_1.default)((0, charge_1.unitAmount)(info2)).toEqual(133);
});
});
describe("days interval", () => {
it("entire day counts (slightly more)", () => {
const info = {
start: (0, timecalcs_1.startOfDay)(new Date("2022-04-01 12:23:00")),
end: (0, timecalcs_1.endOfDay)(new Date("2022-04-06 12:23:03")),
};
(0, expect_1.default)((0, timecalcs_1.getDays)(info)).toEqual(6);
});
it("entire day counts (slightly less)", () => {
const info = {
start: (0, timecalcs_1.startOfDay)(new Date("2022-04-01 12:23:00")),
end: (0, timecalcs_1.endOfDay)(new Date("2022-04-06 12:22:58")),
};
(0, expect_1.default)((0, timecalcs_1.getDays)(info)).toEqual(6);
});
it("works with a user's timezone in utc", () => {
const info = {
start: new Date("2022-04-30T22:00:00Z"),
end: new Date("2022-05-28T21:59:59.999Z"),
};
// this is 1 to 28th in may, full days.
(0, expect_1.default)((0, timecalcs_1.getDays)(info)).toEqual(28);
});
});
describe("start/end of day", () => {
if (!isUTC)
return;
const d = new Date("2022-04-04 14:31:00");
const s = "2022-04-04 14:31:00";
it("start", () => {
(0, expect_1.default)((0, timecalcs_1.startOfDay)(d)).toEqual(new Date("2022-04-04 00:00:00.000Z"));
});
it("end", () => {
(0, expect_1.default)((0, timecalcs_1.endOfDay)(d)).toEqual(new Date("2022-04-04 23:59:59.999Z"));
});
it("start on string", () => {
(0, expect_1.default)((0, timecalcs_1.startOfDay)(s)).toEqual(new Date("2022-04-04 00:00:00.000Z"));
});
it("end on string", () => {
(0, expect_1.default)((0, timecalcs_1.endOfDay)(s)).toEqual(new Date("2022-04-04 23:59:59.999Z"));
});
});
describe("roundToMidnight", () => {
if (!isUTC)
return;
const am = new Date("2022-04-04 1:01:00");
const pm = new Date("2022-04-04 14:31:00");
it("am/side=start", () => {
(0, expect_1.default)((0, timecalcs_1.roundToMidnight)(am, "start")).toEqual(new Date("2022-04-04T00:00:00.000Z"));
});
it("am/side=end", () => {
(0, expect_1.default)((0, timecalcs_1.roundToMidnight)(am, "end")).toEqual(new Date("2022-04-03T23:59:59.999Z"));
});
it("pm/side=start", () => {
(0, expect_1.default)((0, timecalcs_1.roundToMidnight)(pm, "start")).toEqual(new Date("2022-04-05T00:00:00.000Z"));
});
it("pm/side=end", () => {
(0, expect_1.default)((0, timecalcs_1.roundToMidnight)(pm, "end")).toEqual(new Date("2022-04-04T23:59:59.999Z"));
});
});
describe("dedicated disk", () => {
it("calculates subscription price of one disk", () => {
const pi = {
type: "disk",
start: (0, timecalcs_1.startOfDay)(new Date()),
quantity: 1,
subscription: "monthly",
dedicated_disk: {
name: "mydisk123",
speed: "balanced",
size_gb: 32,
},
};
const cost = (0, compute_cost_1.compute_cost)(pi);
(0, expect_1.default)(cost).toEqual({
cost: 8,
cost_per_project_per_month: 8,
cost_per_unit: 8,
cost_sub_month: 8,
cost_sub_year: 96,
discounted_cost: 8,
period: "monthly",
});
});
});
//# sourceMappingURL=prices.test.js.map