From 7b0b574bb37807ddaeabf000aeddae6600a9574a Mon Sep 17 00:00:00 2001 From: John Yeo Date: Wed, 26 Nov 2025 09:23:18 +0000 Subject: [PATCH] fix: testing fix on cached invoices --- scripts/testGroups/g2.sh | 1 - server/register.ts | 6 ++- .../apiCusCacheUtils/setCachedApiCustomer.ts | 6 ++- .../utils/scriptUtils/getAll/getAllOrgs.ts | 4 +- server/tests/_temp/temp1.test.ts | 37 +++++++++++++------ server/tests/attach/entities/entity2.test.ts | 12 +----- .../check/send-event/send-event1.test.ts | 2 +- .../check/send-event/send-event2.test.ts | 2 +- .../check/send-event/send-event3.test.ts | 2 +- .../check/send-event/send-event4.test.ts | 2 +- server/tests/utils/stripeUtils.ts | 2 +- shared/api/customers/apiCustomer.ts | 3 ++ 12 files changed, 47 insertions(+), 32 deletions(-) diff --git a/scripts/testGroups/g2.sh b/scripts/testGroups/g2.sh index f1f1b4cdb..aff1d0319 100755 --- a/scripts/testGroups/g2.sh +++ b/scripts/testGroups/g2.sh @@ -9,7 +9,6 @@ BUN_PARALLEL_COMPACT \ 'server/tests/attach/downgrade' \ 'server/tests/attach/free' \ 'server/tests/attach/addOn' \ - 'server/tests/attach/entities' \ 'server/tests/attach/checkout' \ 'server/tests/attach/misc' \ --max=6 \ diff --git a/server/register.ts b/server/register.ts index 948339ca8..3a3699a77 100644 --- a/server/register.ts +++ b/server/register.ts @@ -1,11 +1,15 @@ import "dotenv/config"; +import { loadLocalEnv } from "./src/utils/envUtils"; import Stripe from "stripe"; +loadLocalEnv(); + const main = async () => { const stripe = new Stripe(process.env.STRIPE_SANDBOX_SECRET_KEY || ""); + const result = await stripe.webhookEndpoints.create({ - url: "https://express.dev.useautumn.com/webhooks/connect/sandbox", + url: `${process.env.STRIPE_WEBHOOK_URL}/webhooks/connect/sandbox`, enabled_events: [ "checkout.session.completed", "customer.subscription.created", diff --git a/server/src/internal/customers/cusUtils/apiCusCacheUtils/setCachedApiCustomer.ts b/server/src/internal/customers/cusUtils/apiCusCacheUtils/setCachedApiCustomer.ts index 6bdf020f8..77f4e2061 100644 --- a/server/src/internal/customers/cusUtils/apiCusCacheUtils/setCachedApiCustomer.ts +++ b/server/src/internal/customers/cusUtils/apiCusCacheUtils/setCachedApiCustomer.ts @@ -33,7 +33,11 @@ export const setCachedApiCustomer = async ({ const ctxWithExpand = addToExpand({ ctx, - add: [CusExpand.BalancesFeature, CusExpand.SubscriptionsPlan], + add: [ + CusExpand.BalancesFeature, + CusExpand.SubscriptionsPlan, + CusExpand.Invoices, + ], }); // Build master api customer (customer-level features only) diff --git a/server/src/utils/scriptUtils/getAll/getAllOrgs.ts b/server/src/utils/scriptUtils/getAll/getAllOrgs.ts index 9ba22a3b8..13d203fac 100644 --- a/server/src/utils/scriptUtils/getAll/getAllOrgs.ts +++ b/server/src/utils/scriptUtils/getAll/getAllOrgs.ts @@ -1,10 +1,10 @@ -import { DrizzleCli } from "@/db/initDrizzle.js"; import { organizations } from "@autumn/shared"; +import type { DrizzleCli } from "@/db/initDrizzle.js"; export const getAllOrgs = async (db: DrizzleCli) => { const orgs = []; let offset = 0; - const limit = 200; + const limit = 1000; while (true) { const batch = await db diff --git a/server/tests/_temp/temp1.test.ts b/server/tests/_temp/temp1.test.ts index 8e983ad0d..444ff8092 100644 --- a/server/tests/_temp/temp1.test.ts +++ b/server/tests/_temp/temp1.test.ts @@ -15,7 +15,7 @@ import { initProductsV0 } from "../../src/utils/scriptUtils/testUtils/initProduc // UNCOMMENT FROM HERE const pro = constructProduct({ - type: "free", + type: "pro", isDefault: true, items: [ @@ -59,24 +59,37 @@ describe(`${chalk.yellowBright("temp: Testing add ons")}`, () => { products: [pro, oneOff], prefix: customerId, }); - - await autumn.attach({ - customer_id: customerId, - product_id: pro.id, - }); }); test("should attach pro product", async () => { - await autumn.attach({ + // await autumn.customers.get(customerId); + + const res = await autumn.attach({ customer_id: customerId, - product_id: oneOff.id, - }); - await autumn.attach({ - customer_id: customerId, - product_id: oneOff.id, + product_id: pro.id, }); + await autumn.attach({ + customer_id: customerId, + product_id: oneOff.id, + }); + await autumn.attach({ + customer_id: customerId, + product_id: oneOff.id, + }); const customer = await autumn.customers.get(customerId); console.log("Customer:", customer); + + // await autumn.attach({ + // customer_id: customerId, + // product_id: oneOff.id, + // }); + // await autumn.attach({ + // customer_id: customerId, + // product_id: oneOff.id, + // }); + + // const customer = await autumn.customers.get(customerId); + // console.log("Customer:", customer); }); }); diff --git a/server/tests/attach/entities/entity2.test.ts b/server/tests/attach/entities/entity2.test.ts index af39d0255..c78822842 100644 --- a/server/tests/attach/entities/entity2.test.ts +++ b/server/tests/attach/entities/entity2.test.ts @@ -1,20 +1,18 @@ import { beforeAll, describe, test } from "bun:test"; import { defaultApiVersion } from "@tests/constants.js"; import { TestFeature } from "@tests/setup/v2Features.js"; -import { hoursToFinalizeInvoice } from "@tests/utils/constants.js"; import { attachAndExpectCorrect } from "@tests/utils/expectUtils/expectAttach.js"; import { expectFeaturesCorrect } from "@tests/utils/expectUtils/expectFeaturesCorrect.js"; import { expectInvoiceAfterUsage } from "@tests/utils/expectUtils/expectSingleUse/expectUsageInvoice.js"; -import { advanceTestClock } from "@tests/utils/stripeUtils.js"; import ctx from "@tests/utils/testInitUtils/createTestContext.js"; import chalk from "chalk"; -import { addHours, addMonths } from "date-fns"; import { AutumnInt } from "@/external/autumn/autumnCli.js"; import { timeout } from "@/utils/genUtils.js"; import { constructArrearItem } from "@/utils/scriptUtils/constructItem.js"; import { constructProduct } from "@/utils/scriptUtils/createTestProducts.js"; import { initCustomerV3 } from "@/utils/scriptUtils/testUtils/initCustomerV3.js"; import { initProductsV0 } from "@/utils/scriptUtils/testUtils/initProductsV0.js"; +import { advanceToNextInvoice } from "../../utils/testAttachUtils/testAttachUtils"; const testCase = "aentity2"; @@ -85,7 +83,6 @@ describe(`${chalk.yellowBright(`attach/${testCase}: Testing attach pro annual to feature_id: TestFeature.Words, value: usage, }); - await timeout(5000); const entity = await autumn.entities.get(customerId, entityId); @@ -118,14 +115,9 @@ describe(`${chalk.yellowBright(`attach/${testCase}: Testing attach pro annual to }); test("should have correct invoice after cycle", async () => { - curUnix = await advanceTestClock({ + curUnix = await advanceToNextInvoice({ stripeCli: ctx.stripeCli, testClockId, - advanceTo: addHours( - addMonths(curUnix, 1), - hoursToFinalizeInvoice, - ).getTime(), - waitForSeconds: 30, }); await expectInvoiceAfterUsage({ diff --git a/server/tests/balances/check/send-event/send-event1.test.ts b/server/tests/balances/check/send-event/send-event1.test.ts index ab41d3c40..ff42b8804 100644 --- a/server/tests/balances/check/send-event/send-event1.test.ts +++ b/server/tests/balances/check/send-event/send-event1.test.ts @@ -13,7 +13,7 @@ import { timeout } from "../../../utils/genUtils.js"; // UNCOMMENT FROM HERE const pro = constructProduct({ type: "free", - isDefault: true, + isDefault: false, items: [ constructFeatureItem({ diff --git a/server/tests/balances/check/send-event/send-event2.test.ts b/server/tests/balances/check/send-event/send-event2.test.ts index ec4ebf0ac..20d2b5ece 100644 --- a/server/tests/balances/check/send-event/send-event2.test.ts +++ b/server/tests/balances/check/send-event/send-event2.test.ts @@ -13,7 +13,7 @@ import { initProductsV0 } from "@/utils/scriptUtils/testUtils/initProductsV0.js" // UNCOMMENT FROM HERE const pro = constructProduct({ type: "free", - isDefault: true, + isDefault: false, items: [ constructFeatureItem({ diff --git a/server/tests/balances/check/send-event/send-event3.test.ts b/server/tests/balances/check/send-event/send-event3.test.ts index c0087c38e..866be0b0b 100644 --- a/server/tests/balances/check/send-event/send-event3.test.ts +++ b/server/tests/balances/check/send-event/send-event3.test.ts @@ -19,7 +19,7 @@ import { timeout } from "../../../utils/genUtils"; // UNCOMMENT FROM HERE const pro = constructProduct({ type: "free", - isDefault: true, + isDefault: false, items: [ constructFeatureItem({ diff --git a/server/tests/balances/check/send-event/send-event4.test.ts b/server/tests/balances/check/send-event/send-event4.test.ts index 42f881231..aa1a91409 100644 --- a/server/tests/balances/check/send-event/send-event4.test.ts +++ b/server/tests/balances/check/send-event/send-event4.test.ts @@ -13,7 +13,7 @@ import { getCreditCost } from "../../../../src/internal/features/creditSystemUti // UNCOMMENT FROM HERE const pro = constructProduct({ type: "free", - isDefault: true, + isDefault: false, items: [ constructFeatureItem({ diff --git a/server/tests/utils/stripeUtils.ts b/server/tests/utils/stripeUtils.ts index 1d5db2aa1..11f30c2aa 100644 --- a/server/tests/utils/stripeUtils.ts +++ b/server/tests/utils/stripeUtils.ts @@ -26,7 +26,7 @@ export const completeCheckoutForm = async ( _isLocal?: boolean, ) => { const browser = await puppeteer.launch({ - headless: true, + headless: false, executablePath: "/Applications/Chromium.app/Contents/MacOS/Chromium", args: ["--no-sandbox", "--disable-setuid-sandbox"], }); diff --git a/shared/api/customers/apiCustomer.ts b/shared/api/customers/apiCustomer.ts index e6bf309fd..6679b1e00 100644 --- a/shared/api/customers/apiCustomer.ts +++ b/shared/api/customers/apiCustomer.ts @@ -29,7 +29,10 @@ export const ApiCustomerSchema = z.object({ stripe_id: z.string().nullable(), env: z.enum(AppEnv), metadata: z.record(z.any(), z.any()), + subscriptions: z.array(ApiSubscriptionSchema), + // scheduled_subscriptions: z.array(ApiSubscriptionSchema), + balances: z.record(z.string(), ApiBalanceSchema), ...ApiCusExpandSchema.shape, });