@@ -220,6 +220,80 @@ describe("Persistence tests", () => {
220220 } ) ;
221221} ) ;
222222
223+ describe ( "Receiver cancel tests" , ( ) => {
224+ test ( "receiver cancel from initialized" , ( ) => {
225+ const persister = new InMemoryReceiverPersister ( 1 ) ;
226+ const address = "tb1q6d3a2w975yny0asuvd9a67ner4nks58ff0q8g4" ;
227+ const ohttpKeys = payjoin . OhttpKeys . decode (
228+ new Uint8Array ( [
229+ 0x01 , 0x00 , 0x16 , 0x04 , 0xba , 0x48 , 0xc4 , 0x9c , 0x3d , 0x4a ,
230+ 0x92 , 0xa3 , 0xad , 0x00 , 0xec , 0xc6 , 0x3a , 0x02 , 0x4d , 0xa1 ,
231+ 0x0c , 0xed , 0x02 , 0x18 , 0x0c , 0x73 , 0xec , 0x12 , 0xd8 , 0xa7 ,
232+ 0xad , 0x2c , 0xc9 , 0x1b , 0xb4 , 0x83 , 0x82 , 0x4f , 0xe2 , 0xbe ,
233+ 0xe8 , 0xd2 , 0x8b , 0xfe , 0x2e , 0xb2 , 0xfc , 0x64 , 0x53 , 0xbc ,
234+ 0x4d , 0x31 , 0xcd , 0x85 , 0x1e , 0x8a , 0x65 , 0x40 , 0xe8 , 0x6c ,
235+ 0x53 , 0x82 , 0xaf , 0x58 , 0x8d , 0x37 , 0x09 , 0x57 , 0x00 , 0x04 ,
236+ 0x00 , 0x01 , 0x00 , 0x03 ,
237+ ] ) . buffer ,
238+ ) ;
239+
240+ const initialized = new payjoin . ReceiverBuilder (
241+ address ,
242+ "https://example.com" ,
243+ ohttpKeys ,
244+ )
245+ . build ( )
246+ . save ( persister ) ;
247+ const cancelTransition = initialized . cancel ( ) ;
248+ const fallbackTx = cancelTransition . save ( persister ) ;
249+ assert . strictEqual ( fallbackTx , undefined ) ;
250+
251+ const result = payjoin . replayReceiverEventLog ( persister ) ;
252+ const state = result . state ( ) ;
253+ assert . strictEqual (
254+ state . tag ,
255+ "Closed" ,
256+ "State should be Closed after cancel" ,
257+ ) ;
258+ } ) ;
259+
260+ test ( "receiver cancel async from initialized" , async ( ) => {
261+ const persister = new InMemoryReceiverPersisterAsync ( 1 ) ;
262+ const address = "tb1q6d3a2w975yny0asuvd9a67ner4nks58ff0q8g4" ;
263+ const ohttpKeys = payjoin . OhttpKeys . decode (
264+ new Uint8Array ( [
265+ 0x01 , 0x00 , 0x16 , 0x04 , 0xba , 0x48 , 0xc4 , 0x9c , 0x3d , 0x4a ,
266+ 0x92 , 0xa3 , 0xad , 0x00 , 0xec , 0xc6 , 0x3a , 0x02 , 0x4d , 0xa1 ,
267+ 0x0c , 0xed , 0x02 , 0x18 , 0x0c , 0x73 , 0xec , 0x12 , 0xd8 , 0xa7 ,
268+ 0xad , 0x2c , 0xc9 , 0x1b , 0xb4 , 0x83 , 0x82 , 0x4f , 0xe2 , 0xbe ,
269+ 0xe8 , 0xd2 , 0x8b , 0xfe , 0x2e , 0xb2 , 0xfc , 0x64 , 0x53 , 0xbc ,
270+ 0x4d , 0x31 , 0xcd , 0x85 , 0x1e , 0x8a , 0x65 , 0x40 , 0xe8 , 0x6c ,
271+ 0x53 , 0x82 , 0xaf , 0x58 , 0x8d , 0x37 , 0x09 , 0x57 , 0x00 , 0x04 ,
272+ 0x00 , 0x01 , 0x00 , 0x03 ,
273+ ] ) . buffer ,
274+ ) ;
275+
276+ const initialized = await new payjoin . ReceiverBuilder (
277+ address ,
278+ "https://example.com" ,
279+ ohttpKeys ,
280+ )
281+ . build ( )
282+ . saveAsync ( persister ) ;
283+ const cancelTransition = initialized . cancel ( ) ;
284+ const fallbackTx = await cancelTransition . saveAsync ( persister ) ;
285+ assert . strictEqual ( fallbackTx , undefined ) ;
286+
287+ const result = await payjoin . replayReceiverEventLogAsync ( persister ) ;
288+ const state = result . state ( ) ;
289+ assert . strictEqual (
290+ state . tag ,
291+ "Closed" ,
292+ "State should be Closed after cancel" ,
293+ ) ;
294+ } ) ;
295+ } ) ;
296+
223297describe ( "Async Persistence tests" , ( ) => {
224298 test ( "receiver async persistence" , async ( ) => {
225299 const persister = new InMemoryReceiverPersisterAsync ( 1 ) ;
0 commit comments