Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/services/billing/customer-invoices.server.test.ts
T

145 lines
5.5 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it, vi } from "vitest";
import type { BillingGateway, BillingGatewayInvoice } from "@hivegrid/billing";
import {
CustomerBillingInvoiceError,
getCustomerBillingInvoiceDownloadTarget,
getCustomerBillingInvoiceDownloadUrl,
listCustomerBillingInvoices,
} from "./customer-invoices.server";
function invoice(overrides: Partial<BillingGatewayInvoice> = {}): BillingGatewayInvoice {
return {
provider: "lago",
providerInvoiceId: "invoice_1",
number: "LAG-001",
externalCustomerId: "tenant_1",
status: "finalized",
paymentStatus: "succeeded",
invoiceType: "subscription",
currency: "EUR",
totalAmountCents: 4900,
issuingDate: "2026-07-04",
fileUrl: "https://files.example.test/invoice_1.pdf",
...overrides,
};
}
function gateway(overrides: Partial<BillingGateway> = {}): BillingGateway {
return {
createCustomer: vi.fn(),
getCustomer: vi.fn(),
createSubscription: vi.fn(),
getSubscription: vi.fn(),
terminateSubscription: vi.fn(),
listInvoices: vi.fn(async () => [invoice(), invoice({ providerInvoiceId: "foreign_invoice", externalCustomerId: "tenant_2" })]),
getInvoice: vi.fn(async (providerInvoiceId: string) =>
providerInvoiceId === "invoice_1"
? invoice()
: invoice({ providerInvoiceId, externalCustomerId: "tenant_2" })
),
downloadInvoice: vi.fn(async () => invoice()),
...overrides,
} as BillingGateway;
}
describe("customer billing invoices", () => {
it("lists only invoices that belong to the current tenant", async () => {
const deps = { gateway: gateway() };
await expect(listCustomerBillingInvoices({ tenantId: "tenant_1", tenantSlug: "acme" }, deps)).resolves.toEqual([
{
id: "invoice_1",
number: "LAG-001",
issuingDate: "2026-07-04",
amountCents: 4900,
currency: "EUR",
status: "finalized",
paymentStatus: "succeeded",
invoiceType: "subscription",
downloadHref: "/acme/settings/subscription/invoices/invoice_1/download",
},
]);
expect(deps.gateway.listInvoices).toHaveBeenCalledWith({ tenantId: "tenant_1", page: 1, perPage: 50 });
});
it("surfaces a configuration error instead of pretending that the invoice list is empty", async () => {
await expect(listCustomerBillingInvoices({ tenantId: "tenant_1", tenantSlug: "acme" }, { gateway: null })).rejects.toMatchObject({
name: "CustomerBillingInvoiceError",
code: "CONFIGURATION_MISSING",
});
});
it("loads invoice history beyond the first provider page without duplicating invoices", async () => {
const firstPage = Array.from({ length: 50 }, (_, index) =>
invoice({ providerInvoiceId: `invoice_${index + 1}`, number: `LAG-${String(index + 1).padStart(3, "0")}` })
);
const listInvoices = vi.fn(async ({ page }: { page?: number }) =>
page === 1 ? firstPage : page === 2 ? [invoice({ providerInvoiceId: "invoice_51", number: "LAG-051" })] : []
);
const invoices = await listCustomerBillingInvoices(
{ tenantId: "tenant_1", tenantSlug: "acme" },
{ gateway: gateway({ listInvoices: listInvoices as BillingGateway["listInvoices"] }) }
);
expect(invoices).toHaveLength(51);
expect(invoices.at(-1)).toMatchObject({ id: "invoice_51", number: "LAG-051" });
expect(listInvoices).toHaveBeenNthCalledWith(2, { tenantId: "tenant_1", page: 2, perPage: 50 });
});
it("keeps voided invoices visible but not downloadable", async () => {
await expect(
listCustomerBillingInvoices(
{ tenantId: "tenant_1", tenantSlug: "acme" },
{ gateway: gateway({ listInvoices: vi.fn(async () => [invoice({ status: "voided" })]) }) }
)
).resolves.toMatchObject([{ id: "invoice_1", status: "voided", downloadHref: null }]);
});
it("returns a generated download URL after tenant ownership is verified", async () => {
const deps = { gateway: gateway() };
await expect(getCustomerBillingInvoiceDownloadUrl({ tenantId: "tenant_1", invoiceId: "invoice_1" }, deps)).resolves.toBe(
"https://files.example.test/invoice_1.pdf"
);
expect(deps.gateway.getInvoice).toHaveBeenCalledWith("invoice_1");
expect(deps.gateway.downloadInvoice).toHaveBeenCalledWith("invoice_1");
});
it("accepts Lago download responses without customer data after list ownership check", async () => {
await expect(
getCustomerBillingInvoiceDownloadTarget(
{ tenantId: "tenant_1", invoiceId: "invoice_1" },
{ gateway: gateway({ downloadInvoice: vi.fn(async () => invoice({ externalCustomerId: null })) }) }
)
).resolves.toEqual({
fileUrl: "https://files.example.test/invoice_1.pdf",
filename: "LAG-001.pdf",
});
});
it("rejects downloads for invoices from another tenant", async () => {
await expect(
getCustomerBillingInvoiceDownloadUrl(
{ tenantId: "tenant_1", invoiceId: "foreign_invoice" },
{ gateway: gateway() }
)
).rejects.toMatchObject({ name: "CustomerBillingInvoiceError", code: "INVOICE_NOT_FOUND" });
});
it("rejects downloads when Lago has not generated a file URL", async () => {
await expect(
getCustomerBillingInvoiceDownloadUrl(
{ tenantId: "tenant_1", invoiceId: "invoice_1" },
{
gateway: gateway({
getInvoice: vi.fn(async () => invoice({ fileUrl: null })),
downloadInvoice: vi.fn(async () => invoice({ fileUrl: null })),
}),
}
)
).rejects.toBeInstanceOf(CustomerBillingInvoiceError);
});
});