import { afterAll, beforeEach, describe, expect, it } from "vitest"; import db from "../../app/db.server"; import { canAddLocation, countLocations, getShopTier, setShopTier } from "../../app/services/billing.server"; const shopDomain = "billing-integration-test.myshopify.com"; async function cleanup() { await db.location.deleteMany({ where: { shopDomain } }); await db.shop.deleteMany({ where: { shopDomain } }); } describe("billing.server", () => { beforeEach(cleanup); afterAll(async () => { await cleanup(); await db.$disconnect(); }); it("getShopTier defaults to free when no Shop row exists yet", async () => { expect(await getShopTier(shopDomain)).toBe("free"); }); it("setShopTier persists across reads, creating the Shop row if needed", async () => { await setShopTier(shopDomain, "growth"); expect(await getShopTier(shopDomain)).toBe("growth"); }); it("setShopTier updates an existing Shop row rather than erroring", async () => { await setShopTier(shopDomain, "starter"); await setShopTier(shopDomain, "pro"); expect(await getShopTier(shopDomain)).toBe("pro"); }); it("canAddLocation blocks a Free shop at its 1-location limit", async () => { expect(await canAddLocation(shopDomain)).toBe(true); await db.location.create({ data: { shopDomain, name: "First", address: "", timezone: "America/Toronto" }, }); expect(await countLocations(shopDomain)).toBe(1); expect(await canAddLocation(shopDomain)).toBe(false); }); it("canAddLocation is unlimited on Growth", async () => { await setShopTier(shopDomain, "growth"); for (let i = 0; i < 5; i++) { await db.location.create({ data: { shopDomain, name: `Location ${i}`, address: "", timezone: "America/Toronto" }, }); } expect(await canAddLocation(shopDomain)).toBe(true); }); });