1+ import { MIN_PAYMENT_WAIT } from '@/background/config' ;
12import { test , expect , DEFAULT_BUDGET } from './fixtures/connected' ;
23import {
34 getWalletInfoCached ,
@@ -53,7 +54,15 @@ const orderById = <T extends { id: string }>(a: T, b: T) =>
5354 a . id . localeCompare ( b . id ) ;
5455
5556test . describe ( 'should monetized site with multiple wallet address' , ( ) => {
56- test ( 'same currency' , async ( { page, popup } ) => {
57+ test ( 'same currency' , async ( { page, popup, background, context } ) => {
58+ // At rateOfPay=3600, we pay $0.01 every second. But given minimum interval
59+ // is 2s, we'll need to wait for 2s.
60+ const rateOfPay = '3600' ;
61+ const interval = MIN_PAYMENT_WAIT ;
62+ await background . evaluate ( ( rateOfPay ) => {
63+ return chrome . storage . local . set ( { rateOfPay } ) ;
64+ } , rateOfPay ) ;
65+
5766 const walletAddresses = [
5867 walletAddressUrl ,
5968 walletAddressUrlSameCurrency ,
@@ -65,11 +74,38 @@ test.describe('should monetized site with multiple wallet address', () => {
6574 ) ;
6675
6776 await test . step ( 'continuous payments' , async ( ) => {
68- await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( count ) ;
77+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 1 ) ;
78+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
79+ paymentPointer : walletAddresses [ 0 ] ,
80+ } ) ;
81+
82+ await context . clock . runFor ( interval ) ;
83+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 2 ) ;
84+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
85+ paymentPointer : walletAddresses [ 1 ] ,
86+ } ) ;
87+
6988 expect (
7089 monetizationCallback . calls . map ( ( [ ev ] ) => ev . paymentPointer ) . sort ( ) ,
7190 'monetization callback is called once for each wallet address' ,
72- ) . toEqual ( walletAddresses ) ;
91+ ) . toEqual ( [ ...walletAddresses ] . sort ( ) ) ;
92+
93+ await context . clock . runFor ( interval ) ;
94+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 3 ) ;
95+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
96+ paymentPointer : walletAddresses [ 0 ] ,
97+ } ) ;
98+
99+ await context . clock . runFor ( interval ) ;
100+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 4 ) ;
101+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
102+ paymentPointer : walletAddresses [ 1 ] ,
103+ } ) ;
104+
105+ expect (
106+ monetizationCallback . calls . map ( ( [ ev ] ) => ev . paymentPointer ) . sort ( ) ,
107+ 'monetization callback is called twice for each wallet address' ,
108+ ) . toEqual ( [ ...walletAddresses , ...walletAddresses ] . sort ( ) ) ;
73109 } ) ;
74110
75111 await setContinuousPayments ( popup , false ) ;
@@ -101,7 +137,15 @@ test.describe('should monetized site with multiple wallet address', () => {
101137 } ) ;
102138 } ) ;
103139
104- test ( 'different currencies' , async ( { page, popup, context } ) => {
140+ test ( 'different currencies' , async ( { page, popup, background, context } ) => {
141+ // At rateOfPay=3600, we pay $0.01 every second. But given minimum interval
142+ // is 2s, we'll need to wait for 2s.
143+ const rateOfPay = '3600' ;
144+ const interval = MIN_PAYMENT_WAIT ;
145+ await background . evaluate ( ( rateOfPay ) => {
146+ return chrome . storage . local . set ( { rateOfPay } ) ;
147+ } , rateOfPay ) ;
148+
105149 const walletAddressesInfo = [
106150 walletInfoSameCurrency ,
107151 walletInfoWeakerCurrency ,
@@ -116,11 +160,50 @@ test.describe('should monetized site with multiple wallet address', () => {
116160 ) ;
117161
118162 await test . step ( 'continuous payments' , async ( ) => {
119- await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( count ) ;
163+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 1 ) ;
164+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
165+ paymentPointer : walletAddresses [ 0 ] ,
166+ } ) ;
167+
168+ await context . clock . runFor ( interval ) ;
169+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 2 ) ;
170+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
171+ paymentPointer : walletAddresses [ 1 ] ,
172+ } ) ;
173+
174+ await context . clock . runFor ( interval ) ;
175+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 3 ) ;
176+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
177+ paymentPointer : walletAddresses [ 2 ] ,
178+ } ) ;
179+
120180 expect (
121181 monetizationCallback . calls . map ( ( [ ev ] ) => ev . paymentPointer ) . sort ( ) ,
122182 'monetization callback is called once for each wallet address' ,
123- ) . toEqual ( walletAddresses ) ;
183+ ) . toEqual ( [ ...walletAddresses ] . sort ( ) ) ;
184+
185+ await context . clock . runFor ( interval ) ;
186+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 4 ) ;
187+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
188+ paymentPointer : walletAddresses [ 0 ] ,
189+ } ) ;
190+
191+ await context . clock . runFor ( interval ) ;
192+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 5 ) ;
193+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
194+ paymentPointer : walletAddresses [ 1 ] ,
195+ } ) ;
196+
197+ await context . clock . runFor ( interval ) ;
198+ await expect ( monetizationCallback ) . toHaveBeenCalledTimes ( 6 ) ;
199+ await expect ( monetizationCallback ) . toHaveBeenLastCalledWithMatching ( {
200+ paymentPointer : walletAddresses [ 2 ] ,
201+ } ) ;
202+
203+ expect (
204+ monetizationCallback . calls . map ( ( [ ev ] ) => ev . paymentPointer ) . sort ( ) ,
205+ 'monetization callback is called twice for each wallet address' ,
206+ ) . toEqual ( [ ...walletAddresses , ...walletAddresses ] . sort ( ) ) ;
124207 } ) ;
125208
126209 await setContinuousPayments ( popup , false ) ;
0 commit comments