diff --git a/src/bidiMapper/modules/session/SubscriptionManager.spec.ts b/src/bidiMapper/modules/session/SubscriptionManager.spec.ts index a67f33825..dc62a1496 100644 --- a/src/bidiMapper/modules/session/SubscriptionManager.spec.ts +++ b/src/bidiMapper/modules/session/SubscriptionManager.spec.ts @@ -203,6 +203,18 @@ describe('SubscriptionManager', () => { ).to.equal(false); }); + it('should unsubscribe by id', () => { + const {id} = subscriptionManager.subscribe( + [SOME_EVENT], + [], + SOME_CHANNEL, + ); + subscriptionManager.unsubscribeById([id]); + expect( + subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT), + ).to.equal(false); + }); + it('should not unsubscribe on error', () => { subscriptionManager.subscribe([SOME_EVENT], [], SOME_CHANNEL); expect(() => @@ -299,6 +311,18 @@ describe('SubscriptionManager', () => { ).to.equal(false); }); + it('should unsubscribe by id', () => { + const {id} = subscriptionManager.subscribe( + [SOME_EVENT], + [SOME_CONTEXT], + SOME_CHANNEL, + ); + subscriptionManager.unsubscribeById([id]); + expect( + subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT), + ).to.equal(false); + }); + it('should partially unsubscribe from a context', () => { subscriptionManager.subscribe( [SOME_EVENT], @@ -430,6 +454,46 @@ describe('SubscriptionManager', () => { }); }); + describe('unsubscribeById', () => { + it('should keep subscription if one of the IDs is not known', () => { + const {id} = subscriptionManager.subscribe( + [SOME_EVENT], + [], + SOME_CHANNEL, + ); + expect( + subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT), + ).to.equal(true); + expect(() => { + subscriptionManager.unsubscribeById([id, 'wrong']); + }).to.throw('No subscription found'); + expect( + subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT), + ).to.equal(true); + }); + + it('should throw an error if an ID is not know', () => { + expect(() => { + subscriptionManager.unsubscribeById(['wrong']); + }).to.throw('No subscription found'); + }); + + it('should throw an error if a subscription is used multiple times', () => { + const {id} = subscriptionManager.subscribe( + [SOME_EVENT], + [], + SOME_CHANNEL, + ); + expect( + subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT), + ).to.equal(true); + subscriptionManager.unsubscribeById([id]); + expect(() => { + subscriptionManager.unsubscribeById([id]); + }).to.throw('No subscription found'); + }); + }); + describe('cartesian product', () => { it('should return empty array for empty array', () => { expect(cartesianProduct([], [])).to.deep.equal([]); diff --git a/src/bidiMapper/modules/session/SubscriptionManager.ts b/src/bidiMapper/modules/session/SubscriptionManager.ts index 324863057..a3f585573 100644 --- a/src/bidiMapper/modules/session/SubscriptionManager.ts +++ b/src/bidiMapper/modules/session/SubscriptionManager.ts @@ -362,7 +362,7 @@ export class SubscriptionManager { throw new InvalidArgumentException('No subscription found'); } this.#subscriptions = this.#subscriptions.filter((subscription) => { - return subscriptionIdsSet.has(subscription.id); + return !subscriptionIdsSet.has(subscription.id); }); this.#knownSubscriptionIds = difference( this.#knownSubscriptionIds,