init
This commit is contained in:
232 files changed
+25416
No files matched your search
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
use App\Filament\Pages\Tenancy\EditMobileAppProfile;
|
||||
use App\Jobs\SyncCafeBazaarApp;
|
||||
use App\Models\BazaarPurchase;
|
||||
use App\Models\BazaarReview;
|
||||
use App\Models\BazaarSubscription;
|
||||
use App\Models\MobileApp;
|
||||
use App\Models\User;
|
||||
use Filament\Facades\Filament;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Livewire\Livewire;
|
||||
|
||||
beforeEach(function () {
|
||||
Http::preventStrayRequests();
|
||||
});
|
||||
|
||||
it('reconciles known Bazaar data and records synchronization health', function () {
|
||||
$app = MobileApp::factory()->create([
|
||||
'package_name' => 'ir.example.sync',
|
||||
]);
|
||||
$purchase = BazaarPurchase::factory()->for($app)->create([
|
||||
'product_id' => 'coins',
|
||||
'purchase_token' => 'purchase-token',
|
||||
'validated_at' => now(),
|
||||
]);
|
||||
$subscription = BazaarSubscription::factory()->for($app)->create([
|
||||
'subscription_id' => 'monthly',
|
||||
'purchase_token' => 'subscription-token',
|
||||
'validated_at' => now(),
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'pardakht.cafebazaar.ir/devapi/v2/api/validate/*' => Http::response([
|
||||
'consumptionState' => 1,
|
||||
'purchaseState' => 1,
|
||||
'purchaseTime' => now()->subWeek()->getTimestampMs(),
|
||||
]),
|
||||
'pardakht.cafebazaar.ir/devapi/v2/api/applications/*/subscriptions/*/purchases/*' => Http::response([
|
||||
'initiationTimestampMsec' => now()->subMonth()->getTimestampMs(),
|
||||
'validUntilTimestampMsec' => now()->subMinute()->getTimestampMs(),
|
||||
'autoRenewing' => false,
|
||||
]),
|
||||
'api.pishkhan.cafebazaar.ir/v1/apps/releases/last-*' => Http::response([], 404),
|
||||
'sandoghche.cafebazaar.ir/api/v0/reviews/*' => Http::response([
|
||||
'next' => null,
|
||||
'results' => [[
|
||||
'id' => 9001,
|
||||
'user_account_id' => 'reviewer',
|
||||
'rate' => 4,
|
||||
'text' => 'Useful',
|
||||
'app_version_code' => 7,
|
||||
'is_edited' => false,
|
||||
'publish_date' => now()->toIso8601String(),
|
||||
'developer_reply' => null,
|
||||
]],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->artisan('bazaar:sync', ['--app' => $app->package_name, '--now' => true, '--force' => true])
|
||||
->assertSuccessful();
|
||||
|
||||
expect($purchase->refresh()->status)->toBe('refunded')
|
||||
->and($subscription->refresh()->status)->toBe('expired')
|
||||
->and(BazaarReview::query()->whereBelongsTo($app)->where('bazaar_review_id', 9001)->exists())->toBeTrue()
|
||||
->and($app->refresh()->bazaar_sync_status)->toBe('succeeded')
|
||||
->and($app->bazaar_last_synced_at)->not->toBeNull()
|
||||
->and($app->bazaar_sync_summary['purchases_checked'])->toBe(1)
|
||||
->and($app->bazaar_sync_summary['subscriptions_checked'])->toBe(1)
|
||||
->and($app->bazaar_sync_summary['reviews_imported'])->toBe(1)
|
||||
->and($app->bazaar_sync_summary['release_states_checked'])->toBe(3);
|
||||
});
|
||||
|
||||
it('queues only apps whose synchronization is due', function () {
|
||||
Bus::fake();
|
||||
|
||||
$dueApp = MobileApp::factory()->create(['bazaar_last_synced_at' => now()->subHour()]);
|
||||
$freshApp = MobileApp::factory()->create(['bazaar_last_synced_at' => now()]);
|
||||
MobileApp::factory()->create(['bazaar_sync_enabled' => false]);
|
||||
|
||||
$this->artisan('bazaar:sync')->assertSuccessful();
|
||||
|
||||
Bus::assertDispatched(SyncCafeBazaarApp::class, fn (SyncCafeBazaarApp $job): bool => $job->mobileAppId === $dueApp->id);
|
||||
Bus::assertNotDispatched(SyncCafeBazaarApp::class, fn (SyncCafeBazaarApp $job): bool => $job->mobileAppId === $freshApp->id);
|
||||
});
|
||||
|
||||
it('marks a synchronization partial when Bazaar rejects an operation', function () {
|
||||
$app = MobileApp::factory()->create(['bazaar_comments_secret' => null]);
|
||||
BazaarSubscription::factory()->for($app)->create(['validated_at' => now()->subHour()]);
|
||||
|
||||
Http::fake([
|
||||
'pardakht.cafebazaar.ir/devapi/v2/api/applications/*/subscriptions/*/purchases/*' => Http::response(['error' => 'invalid_token'], 400),
|
||||
'api.pishkhan.cafebazaar.ir/v1/apps/releases/last-*' => Http::response([], 404),
|
||||
]);
|
||||
|
||||
$this->artisan('bazaar:sync', ['--app' => $app->package_name, '--now' => true, '--force' => true])
|
||||
->assertSuccessful();
|
||||
|
||||
expect($app->refresh()->bazaar_sync_status)->toBe('partial')
|
||||
->and($app->bazaar_sync_error)->toContain('1 Bazaar operation')
|
||||
->and($app->bazaar_sync_summary['failures'][0]['operation'])->toBe('subscription');
|
||||
});
|
||||
|
||||
it('shows synchronization controls on the tenant settings page', function () {
|
||||
Bus::fake();
|
||||
$user = User::factory()->create();
|
||||
$app = MobileApp::factory()->create(['bazaar_sync_status' => 'succeeded']);
|
||||
$app->users()->attach($user, ['role' => 'owner']);
|
||||
|
||||
$this->actingAs($user);
|
||||
Filament::setCurrentPanel(Filament::getPanel('admin'));
|
||||
Filament::setTenant($app);
|
||||
Filament::bootCurrentPanel();
|
||||
|
||||
Livewire::test(EditMobileAppProfile::class)
|
||||
->assertSuccessful()
|
||||
->assertSee('Cafe Bazaar synchronization')
|
||||
->assertActionVisible('syncBazaar')
|
||||
->callAction('syncBazaar')
|
||||
->assertNotified('Cafe Bazaar synchronization queued');
|
||||
|
||||
Bus::assertDispatched(SyncCafeBazaarApp::class, fn (SyncCafeBazaarApp $job): bool => $job->mobileAppId === $app->id && $job->force);
|
||||
});
|
||||
Reference in new issue
Block a user