Bill Meet streaming hourly and storage per GB like Transfer.
Deploy Ladill Meet / deploy (push) Successful in 54s
Deploy Ladill Meet / deploy (push) Successful in 54s
Wallet debits at session end, on recording/file storage, and via daily renewals with grace period before asset deletion. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Organization;
|
||||
use App\Models\Room;
|
||||
use App\Models\Session;
|
||||
use App\Services\Meet\MeetBillingService;
|
||||
use App\Services\Meet\UsageMeteringService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MeetBillingTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function fakeAffordableBilling(): void
|
||||
{
|
||||
$base = rtrim((string) config('billing.api_url'), '/');
|
||||
|
||||
Http::fake([
|
||||
$base.'/can-afford*' => Http::response(['affordable' => true]),
|
||||
$base.'/debit' => Http::response(['ok' => true]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_session_end_records_streaming_usage_and_debits_wallet(): void
|
||||
{
|
||||
$this->fakeAffordableBilling();
|
||||
|
||||
$organization = Organization::create([
|
||||
'owner_ref' => 'owner-001',
|
||||
'name' => 'Billing Org',
|
||||
'slug' => 'billing-org',
|
||||
'timezone' => 'UTC',
|
||||
]);
|
||||
|
||||
$room = Room::create([
|
||||
'owner_ref' => 'owner-001',
|
||||
'organization_id' => $organization->id,
|
||||
'host_user_ref' => 'owner-001',
|
||||
'title' => 'Billing Room',
|
||||
'type' => 'instant',
|
||||
'status' => 'live',
|
||||
'settings' => config('meet.default_settings'),
|
||||
]);
|
||||
|
||||
$session = Session::create([
|
||||
'owner_ref' => 'owner-001',
|
||||
'room_id' => $room->id,
|
||||
'media_room_name' => 'room-test',
|
||||
'status' => 'ended',
|
||||
'started_at' => now()->subHour(),
|
||||
'ended_at' => now(),
|
||||
]);
|
||||
|
||||
app(UsageMeteringService::class)->recordSessionUsage($session);
|
||||
|
||||
$this->assertDatabaseHas('meet_usage_records', [
|
||||
'organization_id' => $organization->id,
|
||||
'metric' => 'streaming_hours',
|
||||
]);
|
||||
|
||||
Http::assertSent(fn ($request) => str_contains($request->url(), '/debit')
|
||||
&& ($request->data()['source'] ?? '') === 'meet_streaming');
|
||||
}
|
||||
|
||||
public function test_storage_renewal_enters_grace_when_wallet_is_empty(): void
|
||||
{
|
||||
$base = rtrim((string) config('billing.api_url'), '/');
|
||||
Http::fake([
|
||||
$base.'/can-afford*' => Http::response(['affordable' => false]),
|
||||
]);
|
||||
|
||||
$organization = Organization::create([
|
||||
'owner_ref' => 'owner-002',
|
||||
'name' => 'Grace Org',
|
||||
'slug' => 'grace-org',
|
||||
'timezone' => 'UTC',
|
||||
]);
|
||||
|
||||
$room = Room::create([
|
||||
'owner_ref' => 'owner-002',
|
||||
'organization_id' => $organization->id,
|
||||
'host_user_ref' => 'owner-002',
|
||||
'title' => 'Grace Room',
|
||||
'type' => 'instant',
|
||||
'status' => 'ended',
|
||||
'settings' => config('meet.default_settings'),
|
||||
]);
|
||||
|
||||
$session = Session::create([
|
||||
'owner_ref' => 'owner-002',
|
||||
'room_id' => $room->id,
|
||||
'media_room_name' => 'room-grace',
|
||||
'status' => 'ended',
|
||||
'started_at' => now()->subDay(),
|
||||
'ended_at' => now()->subDay(),
|
||||
]);
|
||||
|
||||
$file = \App\Models\SessionFile::create([
|
||||
'owner_ref' => 'owner-002',
|
||||
'room_id' => $room->id,
|
||||
'session_id' => $session->id,
|
||||
'uploaded_by_ref' => 'owner-002',
|
||||
'uploaded_by_name' => 'Host',
|
||||
'original_name' => 'notes.pdf',
|
||||
'storage_path' => 'meet-files/test/notes.pdf',
|
||||
'file_size' => 1073741824,
|
||||
'storage_paid_until' => now()->subMinute(),
|
||||
]);
|
||||
|
||||
$result = app(MeetBillingService::class)->processDueStorageRenewals();
|
||||
|
||||
$file->refresh();
|
||||
$this->assertSame(1, $result['graced']);
|
||||
$this->assertNotNull($file->storage_grace_ends_at);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use App\Models\Room;
|
||||
use App\Models\Session;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MeetWebTest extends TestCase
|
||||
@@ -45,6 +46,13 @@ class MeetWebTest extends TestCase
|
||||
'user_ref' => $this->user->public_id,
|
||||
'role' => 'owner',
|
||||
]);
|
||||
|
||||
$base = rtrim((string) config('billing.api_url'), '/');
|
||||
Http::fake([
|
||||
$base.'/can-afford*' => Http::response(['affordable' => true]),
|
||||
$base.'/debit' => Http::response(['ok' => true]),
|
||||
$base.'/balance*' => Http::response(['balance_minor' => 100000]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_health_endpoint(): void
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Session;
|
||||
use App\Services\Meet\MeetBillingService;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MeetBillingServiceTest extends TestCase
|
||||
{
|
||||
public function test_streaming_hours_are_ceiled_with_minimum_one_hour(): void
|
||||
{
|
||||
$billing = app(MeetBillingService::class);
|
||||
|
||||
$session = new Session([
|
||||
'started_at' => now()->subMinutes(10),
|
||||
'ended_at' => now(),
|
||||
]);
|
||||
|
||||
$this->assertSame(1.0, $billing->streamingHoursFor($session));
|
||||
}
|
||||
|
||||
public function test_streaming_amount_minor_matches_hourly_rate(): void
|
||||
{
|
||||
config(['meet.billing.price_per_hour_ghs' => 0.30]);
|
||||
|
||||
$billing = app(MeetBillingService::class);
|
||||
|
||||
$this->assertSame(30, $billing->amountMinorForStreamingHours(1));
|
||||
$this->assertSame(60, $billing->amountMinorForStreamingHours(2));
|
||||
}
|
||||
|
||||
public function test_storage_amount_minor_matches_gb_monthly_rate(): void
|
||||
{
|
||||
config(['meet.billing.price_per_gb_month_ghs' => 0.30]);
|
||||
|
||||
$billing = app(MeetBillingService::class);
|
||||
$oneGb = 1073741824;
|
||||
|
||||
$this->assertSame(30, $billing->amountMinorForStorageBytes($oneGb));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user