Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

23.8 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { beforeEach, describe, expect, it, vi } from "vitest";

// External boundaries only: SMTP, Stripe, the PDF renderer, the rule engine
// and the party-contact policy (each reads its own tables), the Kysely mapping
// write, Inngest, storage, env.
const sendEmail = vi.fn();
const stripeSend = vi.fn();
const evaluateSalesRules = vi.fn();
const contactRequirement = vi.fn();
const raiseMoment = vi.fn();
// The posting server function, set per test by fakeClient.
let postInvoice: (
  input: unknown
) => Promise<{ data: unknown; error: { message: string } | null }>;

vi.mock("@carbon/env", () => ({
  SUPABASE_INTERNAL_URL: "http://internal:8000",
  SUPABASE_URL: "https://db.example.com"
}));
vi.mock("@carbon/lib/email.server", () => ({
  DEFAULT_FROM: "Carbon <[email protected]>",
  sendEmail: (...args: unknown[]) => sendEmail(...args)
}));
vi.mock("@carbon/ee/rules.server", () => ({
  evaluateSalesRulesForSalesDocument: (...args: unknown[]) =>
    evaluateSalesRules(...args),
  dedupeViolations: (violations: unknown[]) => violations
}));
// The Stripe API call only: the connected-account and customer-link reads run
// for real against the fake client. The Stripe SDK module is replaced so
// loading the real reads does not construct a Stripe or Supabase client.
vi.mock("@carbon/stripe/connect.server", () => ({
  createAndSendConnectInvoice: () => {
    throw new Error("The Stripe API is not reachable in tests");
  }
}));
vi.mock("@carbon/stripe/send-sales-invoice.server", async (importOriginal) => ({
  ...(await importOriginal<
    typeof import("@carbon/stripe/send-sales-invoice.server")
  >()),
  sendPostedSalesInvoiceViaStripe: (...args: unknown[]) => stripeSend(...args)
}));
vi.mock("@carbon/ee/accounting", () => ({
  createMappingService: () => ({ link: async () => undefined })
}));
vi.mock("@carbon/lib/party-contact.server", () => ({
  checkPartyContactRequirement: (...args: unknown[]) =>
    contactRequirement(...args)
}));
vi.mock("@carbon/api/postings.server", () => ({
  posting: (_name: string, _handles: unknown, input: unknown) =>
    postInvoice(input)
}));
vi.mock("@carbon/lib/workflows", () => ({
  raiseMoment: (...args: unknown[]) => raiseMoment(...args)
}));
vi.mock("@carbon/lib/sales-invoice-document.server", () => ({
  loadSalesInvoiceDocument: async () => ({
    pdfProps: {},
    email: { company: { name: "Acme", logoLightIcon: null } },
    invoiceReadableId: "INV-1",
    fileName: "Acme - INV-1.pdf"
  }),
  renderSalesInvoicePdf: async () => Buffer.from("%PDF")
}));
vi.mock("@carbon/documents/email", () => ({
  SalesInvoiceEmail: (props: unknown) => props
}));
vi.mock("react-email", () => ({
  render: async () => "<p>invoice</p>"
}));
vi.mock("@carbon/files", () => ({
  getDocumentType: () => "PDF",
  storage: () => ({
    company: () => ({ upload: async () => ({ data: {}, error: null }) })
  })
}));

import {
  attachPostedInvoicePdf,
  companyFromAddress,
  emailPostedInvoice,
  INVOICE_POST_INTERRUPTED,
  INVOICE_SEND_NO_EMAIL,
  INVOICE_SEND_NO_STRIPE,
  invoiceEmailCc,
  invoiceSentStampFailed,
  postSalesInvoiceUnattended,
  resolveInvoiceAutomation,
  sendPostedInvoiceViaStripe
} from "./automate-invoice";

type Row = Record<string, unknown>;

/** A tiny in-memory PostgREST: filters, updates and inserts on plain rows. */
function fakeClient(
  tables: Record<string, Row[]>,
  onInvoke: (rows: Record<string, Row[]>) => {
    error: { message: string } | null;
  } = () => ({
    error: null
  }),
  /** Fails an update of `table` with this message when it returns one. */
  failUpdate: (table: string, values: Row) => string | null = () => null
) {
  const invoke = vi.fn(async () => ({ data: null, ...onInvoke(tables) }));
  postInvoice = invoke;
  const from = (table: string) => {
    const filters: Array<(row: Row) => boolean> = [];
    let update: Row | undefined;
    let insert: Row | undefined;
    const rows = () => (tables[table] ??= []);
    const execute = (single: boolean) => {
      if (insert) {
        rows().push(insert);
        return { data: insert, error: null };
      }
      const failure = update ? failUpdate(table, update) : null;
      if (failure) return { data: null, error: { message: failure } };
      const matched = rows().filter((row) => filters.every((f) => f(row)));
      if (update) for (const row of matched) Object.assign(row, update);
      return {
        data: single ? (matched[0] ?? null) : matched,
        error: null
      };
    };
    const query = {
      select: () => query,
      limit: () => query,
      eq: (key: string, value: unknown) => {
        filters.push((row) => row[key] === value);
        return query;
      },
      in: (key: string, values: unknown[]) => {
        filters.push((row) => values.includes(row[key]));
        return query;
      },
      not: (key: string) => {
        filters.push((row) => row[key] != null);
        return query;
      },
      update: (values: Row) => {
        update = values;
        return query;
      },
      insert: (values: Row) => {
        insert = values;
        return query;
      },
      single: async () => execute(true),
      maybeSingle: async () => execute(true),
      then: (resolve: (value: ReturnType<typeof execute>) => unknown) =>
        Promise.resolve(execute(false)).then(resolve)
    };
    return query;
  };
  return {
    client: { from } as never,
    tables,
    invoke
  };
}

const draftInvoice = (overrides: Row = {}): Row => ({
  id: "inv-1",
  companyId: "co-1",
  invoiceId: "INV-1",
  status: "Draft",
  automationHoldReason: null,
  customerId: "cust-1",
  invoiceCustomerContactId: "cc-1",
  opportunityId: "opp-1",
  sentAt: null,
  sentTo: null,
  sendError: null,
  ...overrides
});

const args = { companyId: "co-1", invoiceId: "inv-1" };
const db = {} as never;

beforeEach(() => {
  vi.clearAllMocks();
  evaluateSalesRules.mockResolvedValue({ violations: [] });
  contactRequirement.mockResolvedValue(null);
  sendEmail.mockResolvedValue({ data: { id: "msg-1" }, error: null });
  stripeSend.mockResolvedValue({
    stripeInvoiceId: "in_1",
    hostedInvoiceUrl: "https://invoice.stripe.com/i/1",
    invoicePdf: null
  });
});

describe("resolveInvoiceAutomation", () => {
  it("uses the contract's mode for an invoice drafted from a contract", async () => {
    const { client } = fakeClient({
      salesInvoice: [draftInvoice({ customerContractId: "cc-9" })],
      customerContracts: [
        {
          id: "cc-9",
          companyId: "co-1",
          effectiveInvoiceAutomation: "Post and Send via Stripe"
        }
      ],
      salesInvoiceLine: [
        { invoiceId: "inv-1", companyId: "co-1", rentalAgreementId: "ra-1" }
      ],
      rentalAgreements: [
        { id: "ra-1", companyId: "co-1", effectiveInvoiceAutomation: "Post" }
      ]
    });
    expect(await resolveInvoiceAutomation(client, "co-1", "inv-1")).toBe(
      "Post and Send via Stripe"
    );
  });

  it("falls back to the rental agreement's mode", async () => {
    const { client } = fakeClient({
      salesInvoice: [draftInvoice()],
      salesInvoiceLine: [
        { invoiceId: "inv-1", companyId: "co-1", rentalAgreementId: "ra-1" }
      ],
      rentalAgreements: [
        {
          id: "ra-1",
          companyId: "co-1",
          effectiveInvoiceAutomation: "Post and Email"
        }
      ]
    });
    expect(await resolveInvoiceAutomation(client, "co-1", "inv-1")).toBe(
      "Post and Email"
    );
  });
});

describe("postSalesInvoiceUnattended", () => {
  it("posts a Draft and announces it once", async () => {
    const { client, tables } = fakeClient(
      { salesInvoice: [draftInvoice()] },
      (rows) => {
        rows.salesInvoice![0]!.status = "Submitted";
        return { error: null };
      }
    );
    expect(await postSalesInvoiceUnattended({ client, db, ...args })).toEqual({
      outcome: "posted"
    });
    expect(tables.salesInvoice![0]!.status).toBe("Submitted");
    expect(raiseMoment).toHaveBeenCalledTimes(1);
  });

  it("reports an already-posted invoice as posted without posting again", async () => {
    const { client, invoke } = fakeClient({
      salesInvoice: [draftInvoice({ status: "Submitted" })]
    });
    expect(await postSalesInvoiceUnattended({ client, db, ...args })).toEqual({
      outcome: "posted"
    });
    expect(invoke).not.toHaveBeenCalled();
  });

  it("leaves a held draft alone", async () => {
    const { client, invoke, tables } = fakeClient({
      salesInvoice: [draftInvoice({ automationHoldReason: "Charges" })]
    });
    expect(await postSalesInvoiceUnattended({ client, db, ...args })).toEqual({
      outcome: "held",
      reason: "Charges"
    });
    expect(invoke).not.toHaveBeenCalled();
    expect(tables.salesInvoice![0]!.status).toBe("Draft");
  });

  it("holds on a sales rule violation and stays Draft", async () => {
    evaluateSalesRules.mockResolvedValue({
      violations: [{ message: "Not sold to Canada" }]
    });
    const { client, invoke, tables } = fakeClient({
      salesInvoice: [draftInvoice()]
    });
    const result = await postSalesInvoiceUnattended({ client, db, ...args });
    expect(result).toEqual({
      outcome: "held",
      reason: "Sales rule: Not sold to Canada"
    });
    expect(invoke).not.toHaveBeenCalled();
    expect(tables.salesInvoice![0]).toMatchObject({
      status: "Draft",
      automationHoldReason: "Sales rule: Not sold to Canada"
    });
  });

  it("holds when the customer misses its required contact", async () => {
    contactRequirement.mockResolvedValue("Acme needs a contact with an email");
    const { client, tables } = fakeClient({ salesInvoice: [draftInvoice()] });
    expect(await postSalesInvoiceUnattended({ client, db, ...args })).toEqual({
      outcome: "held",
      reason: "Acme needs a contact with an email"
    });
    expect(tables.salesInvoice![0]!.automationHoldReason).toBe(
      "Acme needs a contact with an email"
    );
  });

  it("reports a claim left Pending as held, without re-claiming it", async () => {
    const { client, invoke, tables } = fakeClient({
      salesInvoice: [draftInvoice({ status: "Pending" })]
    });
    expect(await postSalesInvoiceUnattended({ client, db, ...args })).toEqual({
      outcome: "held",
      reason: INVOICE_POST_INTERRUPTED
    });
    expect(invoke).not.toHaveBeenCalled();
    expect(tables.salesInvoice![0]!.status).toBe("Pending");
  });

  it("puts a failed post left Pending back to Draft with the reason", async () => {
    const { client, tables } = fakeClient(
      { salesInvoice: [draftInvoice()] },
      () => {
        throw new Error("gateway timeout");
      }
    );
    expect(await postSalesInvoiceUnattended({ client, db, ...args })).toEqual({
      outcome: "held",
      reason: "gateway timeout"
    });
    expect(tables.salesInvoice![0]).toMatchObject({
      status: "Draft",
      automationHoldReason: "gateway timeout"
    });
  });

  it("holds with the edge function's error when it reset the invoice", async () => {
    const { client, tables } = fakeClient(
      { salesInvoice: [draftInvoice()] },
      (rows) => {
        rows.salesInvoice![0]!.status = "Draft";
        return { error: { message: "Accounting period is closed" } };
      }
    );
    expect(await postSalesInvoiceUnattended({ client, db, ...args })).toEqual({
      outcome: "held",
      reason: "Accounting period is closed"
    });
    expect(tables.salesInvoice![0]!.automationHoldReason).toBe(
      "Accounting period is closed"
    );
    expect(raiseMoment).not.toHaveBeenCalled();
  });
});

describe("emailPostedInvoice", () => {
  const posted = (overrides: Row = {}) =>
    draftInvoice({ status: "Submitted", ...overrides });
  const baseTables = (invoice: Row, email: string | null = "[email protected]") => ({
    salesInvoice: [invoice],
    customerContact: [
      {
        id: "cc-1",
        companyId: "co-1",
        contact: { email, firstName: "Ana", lastName: "Buyer" }
      }
    ],
    company: [{ id: "co-1", name: "Acme Rentals", companyGroupId: "g-1" }],
    companySettings: [
      {
        id: "co-1",
        accountsReceivableEmail: "[email protected]",
        defaultCustomerCc: ["[email protected]"]
      }
    ],
    customer: [{ id: "cust-1", companyId: "co-1", defaultCc: [] }],
    salesInvoiceLine: [],
    document: []
  });

  it("never sends an invoice twice", async () => {
    const { client } = fakeClient(
      baseTables(posted({ sentAt: "2026-10-01T05:00:00Z" }))
    );
    expect(await emailPostedInvoice({ client, ...args })).toEqual({
      emailed: false
    });
    expect(sendEmail).not.toHaveBeenCalled();
  });

  it("records why when the contact has no email", async () => {
    const { client, tables } = fakeClient(baseTables(posted(), null));
    expect(await emailPostedInvoice({ client, ...args })).toEqual({
      emailed: false,
      sendError: INVOICE_SEND_NO_EMAIL
    });
    expect(tables.salesInvoice![0]!.sendError).toBe(INVOICE_SEND_NO_EMAIL);
    expect(sendEmail).not.toHaveBeenCalled();
  });

  it("sends from the company, replies to receivables, and stamps it sent", async () => {
    const { client, tables } = fakeClient(baseTables(posted()));
    const result = await emailPostedInvoice({ client, ...args });
    expect(result).toEqual({
      emailed: true,
      sentTo: "[email protected], [email protected], [email protected]"
    });
    const sent = sendEmail.mock.calls[0]![0];
    expect(sent).toMatchObject({
      from: '"Acme Rentals" <[email protected]>',
      to: "[email protected]",
      replyTo: "[email protected]",
      subject: "Invoice INV-1 from Acme Rentals"
    });
    expect(sent.attachments[0].filename).toBe("Acme - INV-1.pdf");
    expect(tables.salesInvoice![0]).toMatchObject({
      sentTo: "[email protected], [email protected], [email protected]",
      sendError: null
    });
    expect(tables.salesInvoice![0]!.sentAt).toEqual(expect.any(String));
    expect(tables.document).toHaveLength(1);
  });

  it("reports a send whose sent stamp failed as a send error, not a clean send", async () => {
    const { client, tables } = fakeClient(
      baseTables(posted()),
      undefined,
      (table, values) =>
        table === "salesInvoice" && "sentAt" in values
          ? "connection reset"
          : null
    );
    const sendError = invoiceSentStampFailed(
      "[email protected], [email protected], [email protected]",
      "connection reset"
    );
    expect(await emailPostedInvoice({ client, ...args })).toEqual({
      emailed: false,
      sendError
    });
    expect(tables.salesInvoice![0]).toMatchObject({ sentAt: null, sendError });
  });

  it("stamps the send error and leaves it unsent when delivery fails", async () => {
    sendEmail.mockResolvedValue({
      data: null,
      error: new Error("550 mailbox unavailable")
    });
    const { client, tables } = fakeClient(baseTables(posted()));
    expect(await emailPostedInvoice({ client, ...args })).toEqual({
      emailed: false,
      sendError: "550 mailbox unavailable"
    });
    expect(tables.salesInvoice![0]).toMatchObject({
      sentAt: null,
      sendError: "550 mailbox unavailable"
    });
  });
});

describe("sendPostedInvoiceViaStripe", () => {
  const posted = (overrides: Row = {}) =>
    draftInvoice({
      status: "Submitted",
      baseStatus: "Submitted",
      invoiceCustomerId: null,
      totalAmount: 120,
      ...overrides
    });
  // The view and its table share the row, so a stamp shows in both.
  const stripeTables = (invoice: Row, links: Row[] = []) => ({
    salesInvoice: [invoice],
    salesInvoices: [invoice],
    companyIntegration: [
      {
        id: "stripe-connect",
        companyId: "co-1",
        active: true,
        metadata: { chargesEnabled: true, stripeAccountId: "acct_1" }
      }
    ],
    externalIntegrationMapping: links
  });
  const customerLink = (entityId: string): Row => ({
    entityType: "customer",
    entityId,
    integration: "stripe-connect",
    companyId: "co-1",
    externalId: `cus_${entityId}`
  });

  it("holds the send when the billing customer has no Stripe customer", async () => {
    // Linked as the sold-to customer, but the bill-to customer is not.
    const { client, tables } = fakeClient(
      stripeTables(posted({ invoiceCustomerId: "cust-2" }), [
        customerLink("cust-1")
      ])
    );
    expect(await sendPostedInvoiceViaStripe({ client, db, ...args })).toEqual({
      emailed: false,
      sendError: INVOICE_SEND_NO_STRIPE
    });
    expect(tables.salesInvoice![0]).toMatchObject({
      sentAt: null,
      sendError: INVOICE_SEND_NO_STRIPE
    });
    expect(stripeSend).not.toHaveBeenCalled();
  });

  it("sends to the linked Stripe customer and stamps it sent via Stripe", async () => {
    const { client, tables } = fakeClient(
      stripeTables(posted(), [customerLink("cust-1")])
    );
    expect(await sendPostedInvoiceViaStripe({ client, db, ...args })).toEqual({
      emailed: true,
      sentTo: "Stripe"
    });
    expect(stripeSend.mock.calls[0]![0]).toMatchObject({
      stripeAccountId: "acct_1",
      stripeCustomerId: "cus_cust-1"
    });
    expect(tables.salesInvoice![0]).toMatchObject({
      sentTo: "Stripe",
      sendError: null
    });
    expect(tables.salesInvoice![0]!.sentAt).toEqual(expect.any(String));
  });

  it("never sends an invoice already linked to a Stripe invoice", async () => {
    const { client, tables } = fakeClient(
      stripeTables(posted(), [
        customerLink("cust-1"),
        {
          entityType: "salesInvoice",
          entityId: "inv-1",
          integration: "stripe-connect",
          companyId: "co-1",
          externalId: "in_0"
        }
      ])
    );
    expect(await sendPostedInvoiceViaStripe({ client, db, ...args })).toEqual({
      emailed: true,
      sentTo: "Stripe"
    });
    expect(stripeSend).not.toHaveBeenCalled();
    expect(tables.salesInvoice![0]!.sentTo).toBe("Stripe");
  });

  it("does not send a zero-total invoice and records no send error", async () => {
    const { client, tables } = fakeClient(
      stripeTables(posted({ totalAmount: 0 }), [customerLink("cust-1")])
    );
    expect(await sendPostedInvoiceViaStripe({ client, db, ...args })).toEqual({
      emailed: false
    });
    expect(stripeSend).not.toHaveBeenCalled();
    expect(tables.salesInvoice![0]).toMatchObject({
      sentAt: null,
      sendError: null
    });
  });

  it("stamps the Stripe error and leaves it unsent when the send fails", async () => {
    stripeSend.mockRejectedValue(new Error("No such customer: cus_cust-1"));
    const { client, tables } = fakeClient(
      stripeTables(posted(), [customerLink("cust-1")])
    );
    expect(await sendPostedInvoiceViaStripe({ client, db, ...args })).toEqual({
      emailed: false,
      sendError: "No such customer: cus_cust-1"
    });
    expect(tables.salesInvoice![0]).toMatchObject({
      sentAt: null,
      sendError: "No such customer: cus_cust-1"
    });
  });
});

describe("email headers", () => {
  it("names the company on the platform address", () => {
    expect(
      companyFromAddress("Acme Rentals", "Carbon <[email protected]>")
    ).toBe('"Acme Rentals" <[email protected]>');
    expect(companyFromAddress('Bob "B" Co', "[email protected]")).toBe(
      '"Bob B Co" <[email protected]>'
    );
  });

  it("prefers the customer's CC over the company's and never repeats the recipient", () => {
    expect(
      invoiceEmailCc({
        to: "[email protected]",
        customerDefaultCc: ["[email protected]", "[email protected]"],
        companyDefaultCc: ["[email protected]"],
        receivablesEmail: "[email protected]"
      })
    ).toEqual(["[email protected]", "[email protected]"]);
    expect(
      invoiceEmailCc({
        to: "[email protected]",
        customerDefaultCc: null,
        companyDefaultCc: ["[email protected]", "[email protected]"],
        receivablesEmail: "[email protected]"
      })
    ).toEqual(["[email protected]", "[email protected]"]);
  });
});

describe("attachPostedInvoicePdf", () => {
  const tables = (invoice: Row) => ({
    salesInvoice: [invoice],
    company: [{ id: "co-1", name: "Acme Rentals", companyGroupId: "g-1" }],
    document: [] as Row[]
  });

  it("files the posted invoice's PDF under its opportunity", async () => {
    const { client, tables: rows } = fakeClient(
      tables(draftInvoice({ status: "Submitted" }))
    );
    expect(await attachPostedInvoicePdf({ client, ...args })).toEqual({
      attached: true
    });
    expect(rows.document).toHaveLength(1);
    expect(rows.document![0]).toMatchObject({
      path: "co-1/opportunity/opp-1/Acme - INV-1.pdf",
      name: "Acme - INV-1.pdf",
      sourceDocument: "Sales Invoice",
      sourceDocumentId: "inv-1"
    });
  });

  it("files it once however often it runs", async () => {
    const { client, tables: rows } = fakeClient(
      tables(draftInvoice({ status: "Submitted" }))
    );
    await attachPostedInvoicePdf({ client, ...args });
    await attachPostedInvoicePdf({ client, ...args });
    expect(rows.document).toHaveLength(1);
  });

  it("lets the agreement's sales person read the filed PDF", async () => {
    const { client, tables: rows } = fakeClient({
      ...tables(draftInvoice({ status: "Submitted" })),
      salesInvoiceLine: [
        { invoiceId: "inv-1", companyId: "co-1", rentalAgreementId: "ra-1" }
      ],
      rentalAgreement: [
        {
          id: "ra-1",
          companyId: "co-1",
          salesPersonId: "user-sales",
          createdBy: "user-creator"
        }
      ]
    });
    await attachPostedInvoicePdf({ client, ...args });
    expect(rows.document![0]).toMatchObject({
      readGroups: ["user-sales"],
      writeGroups: ["user-sales"]
    });
  });

  it("moves a PDF filed for no one to the sales person on the next run", async () => {
    const { client, tables: rows } = fakeClient({
      ...tables(draftInvoice({ status: "Submitted" })),
      salesInvoiceLine: [
        { invoiceId: "inv-1", companyId: "co-1", rentalAgreementId: "ra-1" }
      ],
      rentalAgreement: [
        {
          id: "ra-1",
          companyId: "co-1",
          salesPersonId: null,
          createdBy: "user-creator"
        }
      ],
      document: [
        {
          id: "doc-1",
          companyId: "co-1",
          path: "co-1/opportunity/opp-1/Acme - INV-1.pdf",
          readGroups: ["system"],
          writeGroups: ["system"]
        }
      ]
    });
    await attachPostedInvoicePdf({ client, ...args });
    expect(rows.document).toHaveLength(1);
    expect(rows.document![0]).toMatchObject({
      readGroups: ["user-creator"],
      writeGroups: ["user-creator"]
    });
  });

  it("files a PDF with no recurring source for the system only", async () => {
    const { client, tables: rows } = fakeClient(
      tables(draftInvoice({ status: "Submitted" }))
    );
    await attachPostedInvoicePdf({ client, ...args });
    expect(rows.document![0]).toMatchObject({ readGroups: ["system"] });
  });

  it("files nothing for an invoice that is not posted", async () => {
    const { client, tables: rows } = fakeClient(tables(draftInvoice()));
    expect(await attachPostedInvoicePdf({ client, ...args })).toEqual({
      attached: false
    });
    expect(rows.document).toHaveLength(0);
  });

  it("an email after it reuses the filed PDF rather than filing another", async () => {
    const { client, tables: rows } = fakeClient({
      ...tables(draftInvoice({ status: "Submitted" })),
      customerContact: [
        {
          id: "cc-1",
          companyId: "co-1",
          contact: { email: "[email protected]", firstName: "Ana", lastName: "B" }
        }
      ],
      companySettings: [
        { id: "co-1", accountsReceivableEmail: null, defaultCustomerCc: [] }
      ],
      customer: [{ id: "cust-1", companyId: "co-1", defaultCc: [] }],
      salesInvoiceLine: []
    });
    await attachPostedInvoicePdf({ client, ...args });
    await emailPostedInvoice({ client, ...args });
    expect(rows.document).toHaveLength(1);
    expect(sendEmail.mock.calls[0]![0].attachments[0].filename).toBe(
      "Acme - INV-1.pdf"
    );
  });
});