-
Notifications
You must be signed in to change notification settings - Fork 137
Fix/cancel orders expired holdinvoice continued #884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Matobi98
wants to merge
7
commits into
lnp2pBot:main
Choose a base branch
from
Matobi98:fix/cancel-orders-expired-holdinvoice-continued
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3de394d
fix: cancel hold invoice when expiring ACTIVE orders
grunch 96593e4
chore: sync package-lock.json version to 0.15.2
grunch 92c29c2
feat: notify buyer and seller when an ACTIVE order's hold invoice is …
Matobi98 1378704
fix: retry hold invoice cancellation on failure instead of marking or…
Matobi98 6257fc5
test: cover ACTIVE hold invoice cancellation branch in cancel_orders job
Matobi98 6ec5fcd
style: run prettier on cancel_orders spec
Matobi98 ea7799f
test: cover stale-snapshot re-check guard in cancel_orders mutex
Matobi98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| const { expect } = require('chai'); | ||
| const sinon = require('sinon'); | ||
| const proxyquire = require('proxyquire'); | ||
|
|
||
| proxyquire.noCallThru(); | ||
|
|
||
| describe('Job: cancel_orders (ACTIVE hold invoice cancellation branch)', () => { | ||
| let sandbox: any; | ||
| let cancelOrders: any; | ||
|
|
||
| let orderFindStub: any; | ||
| let orderFindByIdStub: any; | ||
| let userFindOneStub: any; | ||
| let cancelHoldInvoiceStub: any; | ||
| let toBuyerHoldInvoiceExpiredMessageStub: any; | ||
| let toSellerHoldInvoiceExpiredMessageStub: any; | ||
| let orderUpdatedStub: any; | ||
| let loggerWarningStub: any; | ||
| let loggerErrorStub: any; | ||
| let getUserI18nContextStub: any; | ||
|
|
||
| let expiredOrders: any[]; | ||
| let updatedOrder: any; | ||
|
|
||
| const buyerUser = { _id: 'buyer1' }; | ||
| const sellerUser = { _id: 'seller1' }; | ||
| const i18nCtx = { t: (key: string) => key }; | ||
|
|
||
| beforeEach(() => { | ||
| sandbox = sinon.createSandbox(); | ||
|
|
||
| orderFindByIdStub = sandbox.stub().callsFake(async () => updatedOrder); | ||
|
|
||
| // The job runs three separate Order.find queries; only the "expired | ||
| // orders" query (no `$and`, no `admin_warned`) is relevant here. | ||
| orderFindStub = sandbox.stub().callsFake(async (query: any) => { | ||
| if (query && ('$and' in query || 'admin_warned' in query)) return []; | ||
| return expiredOrders; | ||
| }); | ||
|
|
||
| userFindOneStub = sandbox.stub().callsFake(async ({ _id }: any) => { | ||
| if (_id === 'buyer1') return buyerUser; | ||
| if (_id === 'seller1') return sellerUser; | ||
| return null; | ||
| }); | ||
|
|
||
| cancelHoldInvoiceStub = sandbox.stub().resolves(); | ||
| toBuyerHoldInvoiceExpiredMessageStub = sandbox.stub().resolves(); | ||
| toSellerHoldInvoiceExpiredMessageStub = sandbox.stub().resolves(); | ||
| orderUpdatedStub = sandbox.stub(); | ||
| loggerWarningStub = sandbox.stub(); | ||
| loggerErrorStub = sandbox.stub(); | ||
| getUserI18nContextStub = sandbox.stub().resolves(i18nCtx); | ||
|
|
||
| const jobModule = proxyquire('../../jobs/cancel_orders', { | ||
| '../models': { | ||
| User: { findOne: userFindOneStub }, | ||
| Order: { find: orderFindStub, findById: orderFindByIdStub }, | ||
| }, | ||
| '../bot/commands': { | ||
| cancelShowHoldInvoice: sandbox.stub().resolves(), | ||
| cancelAddInvoice: sandbox.stub().resolves(), | ||
| }, | ||
| '../ln': { cancelHoldInvoice: cancelHoldInvoiceStub }, | ||
| '../bot/messages': { | ||
| expiredOrderMessage: sandbox.stub().resolves(), | ||
| toBuyerExpiredOrderMessage: sandbox.stub().resolves(), | ||
| toSellerExpiredOrderMessage: sandbox.stub().resolves(), | ||
| toBuyerHoldInvoiceExpiredMessage: toBuyerHoldInvoiceExpiredMessageStub, | ||
| toSellerHoldInvoiceExpiredMessage: toSellerHoldInvoiceExpiredMessageStub, | ||
| }, | ||
| '../util': { | ||
| getUserI18nContext: getUserI18nContextStub, | ||
| holdInvoiceExpirationInSecs: () => ({ | ||
| expirationTimeInSecs: 0, | ||
| safetyWindowInSecs: 0, | ||
| }), | ||
| PerOrderIdMutex: { | ||
| instance: { | ||
| runExclusive: async (_id: string, cb: () => Promise<any>) => cb(), | ||
| }, | ||
| }, | ||
| }, | ||
| '../logger': { | ||
| logger: { | ||
| error: loggerErrorStub, | ||
| warning: loggerWarningStub, | ||
| info: sandbox.stub(), | ||
| }, | ||
| }, | ||
| '../bot/modules/events/orders': { orderUpdated: orderUpdatedStub }, | ||
| }); | ||
|
|
||
| cancelOrders = jobModule.default; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sandbox.restore(); | ||
| }); | ||
|
|
||
| it('marks an ACTIVE order with a hash as EXPIRED when cancelHoldInvoice succeeds', async () => { | ||
| updatedOrder = { | ||
| _id: 'order1', | ||
| id: 'order1', | ||
| status: 'ACTIVE', | ||
| hash: 'hash1', | ||
| buyer_id: 'buyer1', | ||
| seller_id: 'seller1', | ||
| save: sandbox.stub().resolves(), | ||
| }; | ||
| expiredOrders = [{ _id: 'order1' }]; | ||
|
|
||
| await cancelOrders({} as any); | ||
|
|
||
| expect(cancelHoldInvoiceStub.calledOnceWith({ hash: 'hash1' })).to.equal( | ||
| true, | ||
| ); | ||
| expect(updatedOrder.status).to.equal('EXPIRED'); | ||
| expect(updatedOrder.save.calledOnce).to.equal(true); | ||
| expect(toBuyerHoldInvoiceExpiredMessageStub.calledOnce).to.equal(true); | ||
| expect(toSellerHoldInvoiceExpiredMessageStub.calledOnce).to.equal(true); | ||
| expect(orderUpdatedStub.calledOnceWith(updatedOrder)).to.equal(true); | ||
| }); | ||
|
|
||
| it('leaves the order ACTIVE and sends no messages when cancelHoldInvoice throws', async () => { | ||
| updatedOrder = { | ||
| _id: 'order1', | ||
| id: 'order1', | ||
| status: 'ACTIVE', | ||
| hash: 'hash1', | ||
| buyer_id: 'buyer1', | ||
| seller_id: 'seller1', | ||
| save: sandbox.stub().resolves(), | ||
| }; | ||
| expiredOrders = [{ _id: 'order1' }]; | ||
| cancelHoldInvoiceStub.rejects(new Error('LND error: unable to cancel')); | ||
|
|
||
| await cancelOrders({} as any); | ||
|
|
||
| expect(cancelHoldInvoiceStub.calledOnceWith({ hash: 'hash1' })).to.equal( | ||
| true, | ||
| ); | ||
| expect(updatedOrder.status).to.equal('ACTIVE'); | ||
| expect(updatedOrder.save.called).to.equal(false); | ||
| expect(toBuyerHoldInvoiceExpiredMessageStub.called).to.equal(false); | ||
| expect(toSellerHoldInvoiceExpiredMessageStub.called).to.equal(false); | ||
| expect(orderUpdatedStub.called).to.equal(false); | ||
| }); | ||
|
|
||
| it('marks an ACTIVE order without a hash as EXPIRED without calling cancelHoldInvoice', async () => { | ||
| updatedOrder = { | ||
| _id: 'order1', | ||
| id: 'order1', | ||
| status: 'ACTIVE', | ||
| hash: null, | ||
| buyer_id: 'buyer1', | ||
| seller_id: 'seller1', | ||
| save: sandbox.stub().resolves(), | ||
| }; | ||
| expiredOrders = [{ _id: 'order1' }]; | ||
|
|
||
| await cancelOrders({} as any); | ||
|
|
||
| expect(cancelHoldInvoiceStub.called).to.equal(false); | ||
| expect(updatedOrder.status).to.equal('EXPIRED'); | ||
| expect(updatedOrder.save.calledOnce).to.equal(true); | ||
| expect(orderUpdatedStub.calledOnceWith(updatedOrder)).to.equal(true); | ||
| }); | ||
|
|
||
| it('marks a FIAT_SENT order as EXPIRED without calling cancelHoldInvoice, warning instead', async () => { | ||
| updatedOrder = { | ||
| _id: 'order1', | ||
| id: 'order1', | ||
| status: 'FIAT_SENT', | ||
| hash: 'hash1', | ||
| buyer_id: 'buyer1', | ||
| seller_id: 'seller1', | ||
| save: sandbox.stub().resolves(), | ||
| }; | ||
| expiredOrders = [{ _id: 'order1' }]; | ||
|
|
||
| await cancelOrders({} as any); | ||
|
|
||
| expect(cancelHoldInvoiceStub.called).to.equal(false); | ||
| expect(loggerWarningStub.calledOnce).to.equal(true); | ||
| expect(loggerWarningStub.firstCall.args[0]).to.match( | ||
| /dispute\/admin handling/, | ||
| ); | ||
| expect(updatedOrder.status).to.equal('EXPIRED'); | ||
| expect(updatedOrder.save.calledOnce).to.equal(true); | ||
| expect(orderUpdatedStub.calledOnceWith(updatedOrder)).to.equal(true); | ||
| }); | ||
| }); | ||
|
|
||
| export {}; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.