import { type AppEnv, AttachBranch, BillingInterval, LegacyVersion, type Organization, } from "@autumn/shared"; import { expect } from "chai"; import chalk from "chalk"; import type Stripe from "stripe"; import { setupBefore } from "tests/before.js"; import { TestFeature } from "tests/setup/v2Features.js"; import { attachAndExpectCorrect } from "tests/utils/expectUtils/expectAttach.js"; import { createProducts } from "tests/utils/productUtils.js"; import type { DrizzleCli } from "@/db/initDrizzle.js"; import { AutumnInt } from "@/external/autumn/autumnCli.js"; import { constructPriceItem } from "@/internal/products/product-items/productItemUtils.js"; import { nullish } from "@/utils/genUtils.js"; import { constructArrearItem } from "@/utils/scriptUtils/constructItem.js"; import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js"; import { initCustomer } from "@/utils/scriptUtils/initCustomer.js"; import { addPrefixToProducts } from "../utils.js"; const testCase = "updateEnts4"; export const pro = constructProduct({ items: [ constructArrearItem({ featureId: TestFeature.Words, includedUsage: 10000, }), ], type: "pro", isAnnual: true, }); describe(`${chalk.yellowBright(`${testCase}: Checking price changes don't result in update ents func`)}`, () => { const customerId = testCase; const autumn: AutumnInt = new AutumnInt({ version: LegacyVersion.v1_4 }); let testClockId: string; let db: DrizzleCli, org: Organization, env: AppEnv; let stripeCli: Stripe; const curUnix = new Date().getTime(); before(async function () { await setupBefore(this); const { autumnJs } = this; db = this.db; org = this.org; env = this.env; stripeCli = this.stripeCli; const { testClockId: testClockId1 } = await initCustomer({ autumn: autumnJs, customerId, db, org, env, attachPm: "success", }); addPrefixToProducts({ products: [pro], prefix: testCase, }); await createProducts({ autumn, products: [pro], db, orgId: org.id, env, }); testClockId = testClockId1!; }); it("should attach pro annual product", async () => { await attachAndExpectCorrect({ autumn, customerId, product: pro, stripeCli, db, org, env, }); }); it("branch should not be same custom ents if base price updated", async () => { let customItems = pro.items.filter((item) => !nullish(item.feature_id)); customItems = [ ...customItems, constructPriceItem({ price: 10, interval: BillingInterval.Year, }), ]; const preview = await autumn.attachPreview({ customer_id: customerId, product_id: pro.id, is_custom: true, items: customItems, }); expect(preview.branch).to.equal(AttachBranch.SameCustom); }); });