import {
  require_src
} from "../chunk-EUKVZX3E.mjs";
import {
  BeepClient
} from "../chunk-RHRRSHUY.mjs";
import "../chunk-IUPPETLC.mjs";
import "../chunk-3AWHWPXV.mjs";
import "../chunk-EE7I5JHK.mjs";
import "../chunk-7Q5QQCHS.mjs";
import {
  __toESM
} from "../chunk-6DZX6EAA.mjs";
import "../chunk-YPAFAKCO.mjs";

// tests/beepClient.test.ts
var import_axios_mock_adapter = __toESM(require_src());
import axios from "axios";
describe("BeepClient", () => {
  let client;
  let mockAxios;
  beforeEach(() => {
    mockAxios = new import_axios_mock_adapter.default(axios);
    client = new BeepClient({
      apiKey: "test-api-key",
      serverUrl: "https://test-api.beep.com"
    });
  });
  afterEach(() => {
    mockAxios.restore();
  });
  it("healthCheck should check API health", async () => {
    mockAxios.onGet("/healthz").reply(200, "API is healthy");
    const result = await client.healthCheck();
    expect(result).toBeDefined();
    expect(result).toBe("API is healthy");
  });
  it("requestPayment creates an invoice with token", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(200, {
      invoiceId: "inv_test123",
      referenceKey: "ref_abc123",
      paymentUrl: "https://pay.beep.com/abc123",
      qrCode: "data:image/png;base64,abc123==",
      amount: "10.99",
      token: "USDT" /* USDT */,
      splTokenAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
      status: "pending",
      expiresAt: (/* @__PURE__ */ new Date()).toISOString(),
      receivingMerchantId: "merch_123",
      createdAt: (/* @__PURE__ */ new Date()).toISOString(),
      updatedAt: (/* @__PURE__ */ new Date()).toISOString()
    });
    const result = await client.requestPayment({
      amount: 10.99,
      token: "USDT" /* USDT */,
      description: "Test payment"
    });
    expect(result).toBeDefined();
    expect(result.invoiceId).toBe("inv_test123");
    expect(result.amount).toBe("10.99");
    expect(result.status).toBe("pending");
    expect(mockAxios.history.post.length).toBe(1);
    const requestData = JSON.parse(mockAxios.history.post[0].data);
    expect(requestData.token).toBe("USDT" /* USDT */);
    expect(requestData.amount).toBe(1099e4);
  });
  it("requestPayment falls back to splTokenAddress if token not provided", async () => {
    const splTokenAddress = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
    mockAxios.onPost("/v1/payment/request-payment").reply(200, {
      invoiceId: "inv_test123",
      merchantId: "merch_123",
      payerType: "customer_wallet",
      payerMerchantId: null,
      description: "Test payment",
      amount: "10.99",
      splTokenAddress,
      status: "pending",
      referenceKey: "ref_abc123",
      createdAt: (/* @__PURE__ */ new Date()).toISOString(),
      updatedAt: (/* @__PURE__ */ new Date()).toISOString()
    });
    const result = await client.requestPayment({
      amount: 10.99,
      splTokenAddress,
      description: "Test payment"
    });
    expect(result).toBeDefined();
    expect(result.splTokenAddress).toBe(splTokenAddress);
    expect(mockAxios.history.post.length).toBe(1);
    const requestData = JSON.parse(mockAxios.history.post[0].data);
    expect(requestData.splTokenAddress).toBe(splTokenAddress);
  });
  it("requestPayment defaults to USDT if no token or splTokenAddress provided", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(200, {
      invoiceId: "inv_test123",
      merchantId: "merch_123",
      status: "pending"
    });
    await client.requestPayment({
      amount: 10.99,
      description: "Test payment"
    });
    expect(mockAxios.history.post.length).toBe(1);
    const requestData = JSON.parse(mockAxios.history.post[0].data);
    expect(requestData.token).toBe("USDT" /* USDT */);
  });
  it("requestPayment throws error when API request fails", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(500);
    await expect(client.requestPayment({
      amount: 10.99,
      token: "USDT" /* USDT */,
      description: "Test payment"
    })).rejects.toThrow();
  });
  it("requestPayment throws error when response has no data", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(200, null);
    await expect(client.requestPayment({
      amount: 10.99,
      token: "USDT" /* USDT */,
      description: "Test payment"
    })).rejects.toThrow("No data returned from payment request");
  });
  it("converts amount to base units correctly for different tokens", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(200, {
      invoiceId: "inv_test123",
      referenceKey: "ref_abc123",
      status: "pending",
      expiresAt: (/* @__PURE__ */ new Date()).toISOString(),
      receivingMerchantId: "merch_123"
    });
    await client.requestPayment({
      amount: 0.01,
      token: "USDT" /* USDT */,
      description: "USDT payment"
    });
    const requestData = JSON.parse(mockAxios.history.post[0].data);
    expect(requestData.amount).toBe(1e4);
  });
  it("converts amount to base units correctly for USDT", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(200, {
      invoiceId: "inv_test123",
      referenceKey: "ref_abc123",
      status: "pending",
      expiresAt: (/* @__PURE__ */ new Date()).toISOString(),
      receivingMerchantId: "merch_123"
    });
    await client.requestPayment({
      amount: 1e-6,
      token: "USDT" /* USDT */,
      description: "USDT minimum payment"
    });
    const requestData = JSON.parse(mockAxios.history.post[0].data);
    expect(requestData.amount).toBe(1);
  });
  it("handles payerType correctly when provided", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(200, {
      invoiceId: "inv_test123",
      referenceKey: "ref_abc123",
      status: "pending"
    });
    await client.requestPayment({
      amount: 10.99,
      token: "USDT" /* USDT */,
      description: "Test payment",
      payerType: "merchant_wallet"
    });
    const requestData = JSON.parse(mockAxios.history.post[0].data);
    expect(requestData.payerType).toBe("merchant_wallet");
  });
  it("handles empty description by setting a default", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(200, {
      invoiceId: "inv_test123",
      referenceKey: "ref_abc123",
      status: "pending"
    });
    await client.requestPayment({
      amount: 10.99,
      token: "USDT" /* USDT */,
      description: ""
    });
    const requestData = JSON.parse(mockAxios.history.post[0].data);
    expect(requestData.description).toBe("Payment request");
  });
  it("handles undefined description by setting a default", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(200, {
      invoiceId: "inv_test123",
      referenceKey: "ref_abc123",
      status: "pending"
    });
    await client.requestPayment({
      amount: 10.99,
      token: "USDT" /* USDT */
      // description omitted
    });
    const requestData = JSON.parse(mockAxios.history.post[0].data);
    expect(requestData.description).toBe("Payment request");
  });
  it("handles large amounts correctly", async () => {
    mockAxios.onPost("/v1/payment/request-payment").reply(200, {
      invoiceId: "inv_test123",
      referenceKey: "ref_abc123",
      status: "pending"
    });
    await client.requestPayment({
      amount: 1e3,
      token: "USDT" /* USDT */,
      description: "Large payment"
    });
    const requestData = JSON.parse(mockAxios.history.post[0].data);
    expect(requestData.amount).toBe(1e9);
  });
});
