Skip to content

Commit

Permalink
test: fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Jan 9, 2025
1 parent 00e5654 commit fcfa942
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
22 changes: 15 additions & 7 deletions src/bidiMapper/modules/network/NetworkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class NetworkRequest {
this.#request.info?.frameId ??
this.#request.paused?.frameId ??
this.#request.auth?.frameId ??
''
null
);
}

Expand Down Expand Up @@ -808,12 +808,20 @@ export class NetworkRequest {
this.#phaseChanged();

this.#emittedEvents[event.method] = true;
this.#eventManager.registerEvent(
Object.assign(event, {
type: 'event' as const,
}),
this.#context,
);
if (this.#context) {
this.#eventManager.registerEvent(
Object.assign(event, {
type: 'event' as const,
}),
this.#context,
);
} else {
this.#eventManager.registerGlobalEvent(
Object.assign(event, {
type: 'event' as const,
}),
);
}
}

#getBaseEventParams(phase?: Network.InterceptPhase): Network.BaseParameters {
Expand Down
53 changes: 37 additions & 16 deletions src/bidiMapper/modules/session/SubscriptionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ describe('SubscriptionManager', () => {
});

describe('unsubscribe all', () => {
// it('atomicity: does not unsubscribe when there is no subscription', () => {
// subscriptionManager.subscribe([SOME_EVENT], [], SOME_CHANNEL);
// expect(() =>
// subscriptionManager.unsubscribe(
// [SOME_EVENT, ANOTHER_EVENT],
// [],
// SOME_CHANNEL,
// ),
// ).to.throw('No subscription found');
// expect(
// subscriptionManager.getChannelsSubscribedToEvent(
// SOME_EVENT,
// SOME_CONTEXT,
// ),
// ).to.deep.equal([SOME_CHANNEL]);
// });
it('atomicity: does not unsubscribe when there is no subscription', () => {
subscriptionManager.subscribe([SOME_EVENT], [], SOME_CHANNEL);
expect(() =>
subscriptionManager.unsubscribe(
[SOME_EVENT, ANOTHER_EVENT],
[],
SOME_CHANNEL,
),
).to.throw('No subscription found');
expect(
subscriptionManager.getChannelsSubscribedToEvent(
SOME_EVENT,
SOME_CONTEXT,
),
).to.deep.equal([SOME_CHANNEL]);
});

it('happy path', () => {
subscriptionManager.subscribe([SOME_EVENT], [], SOME_CHANNEL);
Expand Down Expand Up @@ -499,6 +499,27 @@ describe('SubscriptionManager', () => {
),
).to.deep.equal([SOME_CHANNEL]);
});

it('should not unsubscribe from top-level context when event lists do not match', () => {
subscriptionManager.subscribe(
[SOME_EVENT],
[SOME_NESTED_CONTEXT],
SOME_CHANNEL,
);
expect(() => {
subscriptionManager.unsubscribe(
[SOME_EVENT, ANOTHER_EVENT],
[SOME_CONTEXT],
SOME_CHANNEL,
);
}).to.throw('No subscription found');
expect(
subscriptionManager.getChannelsSubscribedToEvent(
SOME_EVENT,
SOME_CONTEXT,
),
).to.deep.equal([SOME_CHANNEL]);
});
});

describe('cartesian product', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/bidiMapper/modules/session/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ export class SubscriptionManager {
newSubscriptions.push(subscription);
continue;
}
if (
intersection(eventNames, subscription.eventNames).size !==
eventNames.size
) {
newSubscriptions.push(subscription);
continue;
}
// Splitting context subscriptions.
const eventMap = new Map<
ChromiumBidi.EventNames,
Expand Down
14 changes: 6 additions & 8 deletions tests/session/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,15 @@ async def test_subscribeWithoutContext_bufferedEventsFromNotClosedContextsAreRet


@pytest.mark.asyncio
@pytest.mark.skip(reason="TODO: conflict with spec")
async def test_unsubscribeIsAtomic(websocket, context_id, iframe_id):
await subscribe(websocket, ["log.entryAdded"], [iframe_id])

with pytest.raises(
Exception,
match=re.compile(
str({
"error": "invalid argument",
"message": 'Cannot unsubscribe from network.responseCompleted, .*. No subscription found.'
}))):
with pytest.raises(Exception,
match=re.compile(
str({
"error": "invalid argument",
"message": 'No subscription found'
}))):
await execute_command(
websocket,
{
Expand Down

0 comments on commit fcfa942

Please sign in to comment.