stakwork / sphinx-nav-fiber

12 stars 45 forks source link

[Tests] payLsat #766

Closed tomsmith8 closed 7 months ago

tomsmith8 commented 8 months ago

Task

Add tests for buying Lsat here: https://github.com/stakwork/sphinx-nav-fiber/blob/master/src/utils/payLsat/index.ts

Example boiler plate:

import { renderHook } from '@testing-library/react-hooks';
import * as sphinx from 'sphinx-bridge';
import * as lsatJs from 'lsat-js';
import { requestProvider } from 'webln';
import { buyLsat } from '~/network/buyLsat';
import { isSphinx } from '../isSphinx';
import { payLsat } from './payLsat'; // Adjust the import path

jest.mock('sphinx-bridge');
jest.mock('lsat-js');
jest.mock('webln');
jest.mock('~/network/buyLsat');
jest.mock('../isSphinx');

describe('payLsat', () => {
  beforeEach(() => {
    localStorage.clear();
    jest.clearAllMocks();
  });

  it('handles payment through Sphinx when Sphinx is available', async () => {
    isSphinx.mockReturnValue(true);
    sphinx.setBudget.mockResolvedValue({ budget: 100 });
    sphinx.saveLsat.mockResolvedValue({ lsat: 'invoice:preimage' });
    buyLsat.mockResolvedValue();

    const { result } = renderHook(() => payLsat(jest.fn()));

    await result.current;

    expect(sphinx.setBudget).toHaveBeenCalled();
    expect(localStorage.setItem).toHaveBeenCalled();
    expect(buyLsat).toHaveBeenCalledWith(100);
  });

  it('handles payment through WebLN when Sphinx is not available', async () => {
    isSphinx.mockReturnValue(false);
    requestProvider.mockResolvedValue({
      sendPayment: jest.fn().mockResolvedValue({ preimage: 'preimage' }),
    });
    buyLsat.mockResolvedValue();

    const { result } = renderHook(() => payLsat(jest.fn()));

    await result.current;

    expect(requestProvider).toHaveBeenCalled();
    expect(localStorage.setItem).toHaveBeenCalled();
    expect(buyLsat).toHaveBeenCalledWith(50); // Assuming 50 is the fallback budget amount
  });

  it('handles LSAT payment challenges from Sphinx', async () => {
    const testError = new Error('Payment Required');
    testError.status = 402;
    testError.headers = new Headers();
    testError.headers.set('www-authenticate', 'LSAT macaroon="baseMacaroon", invoice="invoice"');

    isSphinx.mockReturnValue(true);
    sphinx.setBudget.mockResolvedValue({ budget: 100 });
    buyLsat.mockRejectedValue(testError);
    lsatJs.Lsat.fromHeader.mockReturnValue({
      invoice: 'invoice',
      baseMacaroon: 'baseMacaroon',
      id: 'id',
    });
    sphinx.saveLsat.mockResolvedValue({ lsat: 'invoice:preimage' });

    const setBudgetMock = jest.fn();
    await payLsat(setBudgetMock);

    expect(lsatJs.Lsat.fromHeader).toHaveBeenCalled();
    expect(sphinx.saveLsat).toHaveBeenCalledWith('invoice', 'baseMacaroon', window.location.host);
    expect(setBudgetMock).toHaveBeenCalledWith(100);
  });

});

Acceptance Criteria

Test sphinx.enable returns true and local storage has an LSAT:

Test sphinx.enable returns true but local storage does not have an LSAT:

Test sphinx.enable returns false:

Fog3211 commented 7 months ago

assign

Fog3211 commented 7 months ago

@tomsmith8 isSphinx() does not have else block, Can you please confirm if this is correct? It appears that when isSphinx() is false, both localStorage.removeItem and buyLsat will be executed twice. https://github.com/stakwork/sphinx-nav-fiber/blob/master/src/utils/payLsat/index.ts#L11