Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/services/billing/customer-invoices.server.test.ts
T
2026-07-08 16:15:37 +02:00

118 lines
4.4 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(),
listInvoices: vi.fn(async () => [invoice(), invoice({ providerInvoiceId: "foreign_invoice", 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", perPage: 50 });
});
it("returns an empty customer invoice list when Lago is not configured", async () => {
await expect(listCustomerBillingInvoices({ tenantId: "tenant_1", tenantSlug: "acme" }, { gateway: null })).resolves.toEqual([]);
});
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.listInvoices).toHaveBeenCalledWith({ tenantId: "tenant_1", perPage: 50 });
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({ downloadInvoice: vi.fn(async () => invoice({ providerInvoiceId: "foreign_invoice", externalCustomerId: "tenant_2" })) }) }
)
).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({
listInvoices: vi.fn(async () => [invoice({ fileUrl: null })]),
downloadInvoice: vi.fn(async () => invoice({ fileUrl: null })),
}),
}
)
).rejects.toBeInstanceOf(CustomerBillingInvoiceError);
});
});