Repository navigation
Expand file tree
/
Copy pathInvoiceNotificationTest.php
More file actions
818 lines (681 loc) · 27 KB
/
Copy pathInvoiceNotificationTest.php
File metadata and controls
818 lines (681 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
<?php
namespace Tests\Feature;
use App\Events\InvoicePaid;
use App\Models\Client;
use App\Models\Invoice;
use App\Models\InvoiceDelivery;
use App\Models\InvoicePayment;
use App\Models\User;
use App\Services\InvoiceAlertService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class InvoiceNotificationTest extends TestCase
{
use RefreshDatabase;
public function test_owner_paid_notice_dispatches_when_invoice_paid(): void
{
Queue::fake();
$owner = User::factory()->create();
$client = Client::create([
'user_id' => $owner->id,
'name' => 'Client Test',
'email' => 'client@example.com',
]);
$invoice = Invoice::create([
'user_id' => $owner->id,
'client_id' => $client->id,
'number' => 'INV-1001',
'amount_usd' => 200,
'btc_rate' => 40_000,
'amount_btc' => 0.005,
'payment_address' => 'tb1qq0example',
'status' => 'sent',
'invoice_date' => now()->toDateString(),
]);
event(new InvoicePaid($invoice));
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'issuer_paid_notice',
]);
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'receipt',
]);
}
public function test_owner_paid_notice_dispatches_even_when_client_email_is_missing(): void
{
Queue::fake();
$owner = User::factory()->create();
$client = Client::create([
'user_id' => $owner->id,
'name' => 'Client Test',
'email' => '',
]);
$invoice = Invoice::create([
'user_id' => $owner->id,
'client_id' => $client->id,
'number' => 'INV-1001B',
'amount_usd' => 200,
'btc_rate' => 40_000,
'amount_btc' => 0.005,
'payment_address' => 'tb1qq0example-b',
'status' => 'paid',
'invoice_date' => now()->toDateString(),
]);
event(new InvoicePaid($invoice));
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'issuer_paid_notice',
]);
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'receipt',
]);
}
public function test_invoice_paid_event_does_not_auto_queue_client_receipt_when_multiple_active_payments_exist(): void
{
Queue::fake();
$owner = User::factory()->create();
$client = Client::create([
'user_id' => $owner->id,
'name' => 'Client Test',
'email' => 'client@example.com',
]);
$invoice = Invoice::create([
'user_id' => $owner->id,
'client_id' => $client->id,
'number' => 'INV-1001C',
'amount_usd' => 200,
'btc_rate' => 40_000,
'amount_btc' => 0.005,
'payment_address' => 'tb1qq0example-c',
'status' => 'paid',
'invoice_date' => now()->toDateString(),
]);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-auto-hold-1',
'sats_received' => 250_000,
'detected_at' => Carbon::now()->subMinutes(10),
'confirmed_at' => Carbon::now()->subMinutes(10),
]);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-auto-hold-2',
'sats_received' => 250_000,
'detected_at' => Carbon::now(),
'confirmed_at' => Carbon::now(),
]);
event(new InvoicePaid($invoice->fresh(['client', 'user', 'payments', 'sourcePayments', 'deliveries'])));
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'issuer_paid_notice',
'status' => 'queued',
]);
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'receipt',
]);
}
public function test_invoice_paid_event_does_not_auto_queue_client_receipt_when_correction_state_touches_history(): void
{
Queue::fake();
$owner = User::factory()->create();
$client = Client::create([
'user_id' => $owner->id,
'name' => 'Client Test',
'email' => 'client@example.com',
]);
$invoice = Invoice::create([
'user_id' => $owner->id,
'client_id' => $client->id,
'number' => 'INV-1001D',
'amount_usd' => 200,
'btc_rate' => 40_000,
'amount_btc' => 0.005,
'payment_address' => 'tb1qq0example-d',
'status' => 'paid',
'invoice_date' => now()->toDateString(),
]);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-auto-hold-ignored-active',
'sats_received' => 500_000,
'detected_at' => Carbon::now(),
'confirmed_at' => Carbon::now(),
]);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-auto-hold-ignored',
'sats_received' => 100_000,
'detected_at' => Carbon::now()->subMinutes(5),
'confirmed_at' => Carbon::now()->subMinutes(5),
'ignored_at' => Carbon::now()->subMinute(),
'ignored_by_user_id' => $owner->id,
'ignore_reason' => 'Wrong invoice',
]);
event(new InvoicePaid($invoice->fresh(['client', 'user', 'payments', 'sourcePayments', 'deliveries'])));
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'receipt',
]);
}
public function test_overpayment_alert_sent_to_client_and_owner(): void
{
Queue::fake();
[$invoice] = $this->makeInvoiceWithClient();
$expectedSats = (int) round($invoice->amount_btc * Invoice::SATS_PER_BTC);
$overpaySats = (int) round($expectedSats * 1.2);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-overpay',
'sats_received' => $overpaySats,
'detected_at' => Carbon::now(),
'confirmed_at' => Carbon::now(),
]);
$invoice->refresh()->refreshPaymentState();
app(InvoiceAlertService::class)->checkPaymentThresholds($invoice->fresh('payments'));
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'client_overpay_alert',
'context_key' => 'tx-overpay',
]);
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'issuer_overpay_alert',
'context_key' => 'tx-overpay',
]);
}
public function test_underpayment_alert_sent_to_client_and_owner(): void
{
Queue::fake();
[$invoice] = $this->makeInvoiceWithClient();
$expectedSats = (int) round($invoice->amount_btc * Invoice::SATS_PER_BTC);
$partialSats = (int) round($expectedSats * 0.6);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-underpay',
'sats_received' => $partialSats,
'detected_at' => Carbon::now(),
'confirmed_at' => Carbon::now(),
]);
$invoice->refresh()->refreshPaymentState();
app(InvoiceAlertService::class)->checkPaymentThresholds($invoice->fresh('payments'));
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'client_underpay_alert',
'context_key' => 'tx-underpay',
]);
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'issuer_underpay_alert',
'context_key' => 'tx-underpay',
]);
}
/**
* #154: a payment in flight is not evidence of underpayment. The alert gate
* counted confirmed payments only while treating any payment row as
* activity, so a fully-paid invoice read as 100% short between detection
* and its first confirmation.
*/
public function test_no_underpayment_alert_while_a_full_payment_is_unconfirmed(): void
{
Queue::fake();
[$invoice] = $this->makeInvoiceWithClient();
$expectedSats = (int) round($invoice->amount_btc * Invoice::SATS_PER_BTC);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-full-unconfirmed',
'sats_received' => $expectedSats,
'detected_at' => Carbon::now(),
'confirmed_at' => null,
]);
$invoice->refresh()->refreshPaymentState();
app(InvoiceAlertService::class)->checkPaymentThresholds($invoice->fresh('payments'));
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'client_underpay_alert',
]);
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'issuer_underpay_alert',
]);
}
public function test_underpayment_alert_still_fires_for_a_short_unconfirmed_payment(): void
{
Queue::fake();
[$invoice] = $this->makeInvoiceWithClient();
$expectedSats = (int) round($invoice->amount_btc * Invoice::SATS_PER_BTC);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-short-unconfirmed',
'sats_received' => (int) round($expectedSats * 0.6),
'detected_at' => Carbon::now(),
'confirmed_at' => null,
]);
$invoice->refresh()->refreshPaymentState();
app(InvoiceAlertService::class)->checkPaymentThresholds($invoice->fresh('payments'));
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'client_underpay_alert',
'context_key' => 'tx-short-unconfirmed',
]);
}
public function test_resolving_small_balance_marks_paid_and_skips_underpay_nags(): void
{
Queue::fake();
Cache::put(\App\Services\BtcRate::CACHE_KEY, [
'rate_usd' => 40_000,
'as_of' => Carbon::now(),
'source' => 'test',
]);
[$invoice, $owner, $client] = $this->makeInvoiceWithClient();
$invoice->forceFill([
'status' => 'sent',
'amount_usd' => 200,
'btc_rate' => 40_000,
'amount_btc' => 0.005,
])->save();
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-partial',
'sats_received' => (int) round((199 / 40_000) * Invoice::SATS_PER_BTC),
'detected_at' => Carbon::now(),
'confirmed_at' => Carbon::now(),
'usd_rate' => 40_000,
'fiat_amount' => 199,
]);
$invoice->refresh()->refreshPaymentState();
$clientDelivery = $invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'client_underpay_alert',
'status' => 'queued',
'recipient' => $client->email,
'dispatched_at' => now(),
]);
$ownerDelivery = $invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'issuer_underpay_alert',
'status' => 'queued',
'recipient' => $owner->email,
'dispatched_at' => now(),
]);
$this->actingAs($owner)
->post(route('invoices.payments.adjustments.resolve', $invoice))
->assertRedirect();
$invoice->refresh();
$this->assertSame('paid', $invoice->status);
$this->assertSame(0, $invoice->outstanding_sats);
$this->assertDatabaseHas('invoice_deliveries', [
'id' => $clientDelivery->id,
'status' => 'skipped',
]);
$this->assertDatabaseHas('invoice_deliveries', [
'id' => $ownerDelivery->id,
'status' => 'skipped',
]);
}
public function test_resolving_overpayment_skips_queued_overpay_alerts(): void
{
Queue::fake();
[$invoice, $owner, $client] = $this->makeInvoiceWithClient();
$expectedSats = (int) round($invoice->amount_btc * Invoice::SATS_PER_BTC);
$overpaySats = (int) round($expectedSats * 1.2);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-overpay-resolved',
'sats_received' => $overpaySats,
'detected_at' => Carbon::now(),
'confirmed_at' => Carbon::now(),
]);
$invoice->refresh()->refreshPaymentState();
$clientDelivery = $invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'client_overpay_alert',
'status' => 'queued',
'recipient' => $client->email,
'dispatched_at' => now(),
]);
$ownerDelivery = $invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'issuer_overpay_alert',
'status' => 'queued',
'recipient' => $owner->email,
'dispatched_at' => now(),
]);
$invoice->forceFill([
'amount_usd' => 300,
'amount_btc' => 0.006,
])->save();
app(InvoiceAlertService::class)->skipInvalidQueuedDeliveries(
$invoice->fresh('payments'),
'Skipped after invoice change.'
);
$this->assertDatabaseHas('invoice_deliveries', [
'id' => $clientDelivery->id,
'status' => 'skipped',
'error_message' => 'Skipped after invoice change. Overpayment alert no longer applies.',
]);
$this->assertDatabaseHas('invoice_deliveries', [
'id' => $ownerDelivery->id,
'status' => 'skipped',
'error_message' => 'Skipped after invoice change. Overpayment alert no longer applies.',
]);
}
public function test_past_due_command_sends_alerts(): void
{
Queue::fake();
[$invoice] = $this->makeInvoiceWithClient();
$invoice->forceFill([
'status' => 'sent',
'due_date' => now()->subDays(3)->toDateString(),
])->save();
$this->artisan('invoices:send-past-due-alerts')->assertExitCode(0);
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'past_due_issuer',
'context_key' => 'past_due_1',
]);
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'past_due_client',
'context_key' => 'past_due_1',
]);
}
public function test_past_due_slot_not_resent_after_already_queued(): void
{
Queue::fake();
[$invoice, $owner, $client] = $this->makeInvoiceWithClient();
$invoice->forceFill([
'status' => 'sent',
'due_date' => now()->subDays(3)->toDateString(),
])->save();
// Simulate slot 1 already queued
$invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'past_due_issuer',
'status' => 'queued',
'recipient' => $owner->email,
'context_key' => 'past_due_1',
'dispatched_at' => now()->subHour(),
]);
$this->artisan('invoices:send-past-due-alerts')->assertExitCode(0);
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'past_due_issuer',
'status' => 'queued',
'context_key' => 'past_due_2',
]);
}
public function test_past_due_slot_2_not_sent_before_day_7(): void
{
Queue::fake();
[$invoice, $owner, $client] = $this->makeInvoiceWithClient();
$invoice->forceFill([
'status' => 'sent',
'due_date' => now()->subDays(3)->toDateString(),
])->save();
// Simulate slot 1 already sent
$invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'past_due_issuer',
'status' => 'sent',
'recipient' => $owner->email,
'context_key' => 'past_due_1',
'dispatched_at' => now()->subDays(3),
]);
$invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'past_due_client',
'status' => 'sent',
'recipient' => $client->email,
'context_key' => 'past_due_1',
'dispatched_at' => now()->subDays(3),
]);
$this->artisan('invoices:send-past-due-alerts')->assertExitCode(0);
// Day 3 is past threshold for slot 1 (already sent) but not slot 2 (needs day 7)
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'context_key' => 'past_due_2',
]);
}
public function test_past_due_slot_2_fires_after_day_7_when_slot_1_sent(): void
{
Queue::fake();
[$invoice, $owner, $client] = $this->makeInvoiceWithClient();
$invoice->forceFill([
'status' => 'sent',
'due_date' => now()->subDays(8)->toDateString(),
])->save();
// Simulate slot 1 already sent
$invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'past_due_issuer',
'status' => 'sent',
'recipient' => $owner->email,
'context_key' => 'past_due_1',
'dispatched_at' => now()->subDays(7),
]);
$invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'past_due_client',
'status' => 'sent',
'recipient' => $client->email,
'context_key' => 'past_due_1',
'dispatched_at' => now()->subDays(7),
]);
$this->artisan('invoices:send-past-due-alerts')->assertExitCode(0);
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'past_due_issuer',
'context_key' => 'past_due_2',
'status' => 'queued',
]);
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'past_due_client',
'context_key' => 'past_due_2',
'status' => 'queued',
]);
}
public function test_past_due_catchup_sends_only_one_slot_per_run(): void
{
Queue::fake();
[$invoice] = $this->makeInvoiceWithClient();
$invoice->forceFill([
'status' => 'sent',
'due_date' => now()->subDays(15)->toDateString(),
])->save();
// No past-due deliveries yet — invoice has been overdue 15 days unnoticed
$this->artisan('invoices:send-past-due-alerts')->assertExitCode(0);
// Only slot 1 should fire on first run, not slot 2 or 3
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'context_key' => 'past_due_1',
'status' => 'queued',
]);
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'context_key' => 'past_due_2',
]);
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'context_key' => 'past_due_3',
]);
}
public function test_alert_skipped_when_failed_delivery_exists_for_same_trigger(): void
{
Queue::fake();
[$invoice] = $this->makeInvoiceWithClient();
$expectedSats = (int) round($invoice->amount_btc * Invoice::SATS_PER_BTC);
$partialSats = (int) round($expectedSats * 0.6);
InvoicePayment::create([
'invoice_id' => $invoice->id,
'txid' => 'tx-underpay-failed',
'sats_received' => $partialSats,
'detected_at' => Carbon::now(),
'confirmed_at' => Carbon::now(),
]);
$invoice->refresh()->refreshPaymentState();
// Simulate a previously failed delivery for the same txid trigger
$invoice->deliveries()->create([
'user_id' => $invoice->user_id,
'type' => 'client_underpay_alert',
'status' => 'failed',
'recipient' => 'client@example.com',
'context_key' => 'tx-underpay-failed',
'dispatched_at' => now()->subHour(),
]);
app(InvoiceAlertService::class)->checkPaymentThresholds($invoice->fresh('payments'));
// The deliveryExists() guard fires before queue() — no new row is created at all.
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'client_underpay_alert',
'status' => 'queued',
]);
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'client_underpay_alert',
'status' => 'skipped',
]);
}
// -------------------------------------------------------------------------
// Past-due delivery deduplication fix tests
// -------------------------------------------------------------------------
/**
* Test A — A second sendPastDueAlerts() call against an invoice that already
* has all 3 past-due slots with status = 'sent' creates no new InvoiceDelivery rows.
*/
public function test_past_due_second_call_creates_no_rows_when_all_slots_already_sent(): void
{
Queue::fake();
[$invoice, $owner, $client] = $this->makeInvoiceWithClient();
$invoice->forceFill([
'status' => 'sent',
'due_date' => now()->subDays(20)->toDateString(),
])->save();
// Seed all 6 delivery rows (issuer + client × 3 slots) as 'sent'
foreach (['past_due_1', 'past_due_2', 'past_due_3'] as $contextKey) {
$invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'past_due_issuer',
'status' => 'sent',
'recipient' => $owner->email,
'context_key' => $contextKey,
'dispatched_at' => now()->subDays(5),
]);
$invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'past_due_client',
'status' => 'sent',
'recipient' => $client->email,
'context_key' => $contextKey,
'dispatched_at' => now()->subDays(5),
]);
}
$countBefore = InvoiceDelivery::count();
app(InvoiceAlertService::class)->sendPastDueAlerts($invoice->fresh(['client', 'user', 'deliveries']));
$this->assertSame($countBefore, InvoiceDelivery::count());
}
/**
* Test B — A fresh past-due invoice (no existing deliveries) produces a
* 'queued' delivery row for slot 1 on the first sendPastDueAlerts() call.
*/
public function test_past_due_first_call_queues_slot_1_for_fresh_invoice(): void
{
Queue::fake();
[$invoice, $owner, $client] = $this->makeInvoiceWithClient();
$invoice->forceFill([
'status' => 'sent',
'due_date' => now()->subDays(2)->toDateString(),
])->save();
app(InvoiceAlertService::class)->sendPastDueAlerts($invoice->fresh(['client', 'user', 'deliveries']));
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'past_due_issuer',
'context_key' => 'past_due_1',
'status' => 'queued',
]);
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'past_due_client',
'context_key' => 'past_due_1',
'status' => 'queued',
]);
}
/**
* Test C — When slot 1 is already 'sent' but slot 2 has no existing row and
* the invoice is 7+ days past due, sendPastDueAlerts() queues slot 2, not slot 1 again.
*/
public function test_past_due_queues_slot_2_not_slot_1_when_slot_1_already_sent_and_7_days_past_due(): void
{
Queue::fake();
[$invoice, $owner, $client] = $this->makeInvoiceWithClient();
$invoice->forceFill([
'status' => 'sent',
'due_date' => now()->subDays(8)->toDateString(),
])->save();
// Slot 1 already sent for both recipients
$invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'past_due_issuer',
'status' => 'sent',
'recipient' => $owner->email,
'context_key' => 'past_due_1',
'dispatched_at' => now()->subDays(7),
]);
$invoice->deliveries()->create([
'user_id' => $owner->id,
'type' => 'past_due_client',
'status' => 'sent',
'recipient' => $client->email,
'context_key' => 'past_due_1',
'dispatched_at' => now()->subDays(7),
]);
app(InvoiceAlertService::class)->sendPastDueAlerts($invoice->fresh(['client', 'user', 'deliveries']));
// Slot 2 should be queued
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'past_due_issuer',
'context_key' => 'past_due_2',
'status' => 'queued',
]);
$this->assertDatabaseHas('invoice_deliveries', [
'invoice_id' => $invoice->id,
'type' => 'past_due_client',
'context_key' => 'past_due_2',
'status' => 'queued',
]);
// No additional slot 1 row should exist beyond the two 'sent' rows seeded above
$this->assertDatabaseMissing('invoice_deliveries', [
'invoice_id' => $invoice->id,
'context_key' => 'past_due_1',
'status' => 'queued',
]);
}
private function makeInvoiceWithClient(): array
{
$owner = User::factory()->create(['email' => 'owner@example.com']);
$client = Client::create([
'user_id' => $owner->id,
'name' => 'Acme',
'email' => 'client@example.com',
]);
$invoice = Invoice::create([
'user_id' => $owner->id,
'client_id' => $client->id,
'number' => 'INV-2001',
'amount_usd' => 250,
'btc_rate' => 50_000,
'amount_btc' => 0.005,
'payment_address' => 'tb1qq0example',
'status' => 'sent',
'invoice_date' => now()->toDateString(),
'due_date' => now()->addDays(3)->toDateString(),
]);
return [$invoice, $owner, $client];
}
}