| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
-
- namespace Tests\Feature\Dashboard;
-
- use App\Dashboard\StorePulse;
- use App\Enums\StoreStatus;
- use App\Models\DashboardPulse;
- use App\Models\Order;
- use App\Models\OrderItem;
- use App\Models\Product;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\TestCase;
-
- class OverviewTest extends TestCase
- {
- use RefreshDatabase;
-
- private function paidOrder(Store $store, int $total, string $paidAt = 'now', bool $shipped = false): Order
- {
- return Order::factory()->paid()->for($store)->create([
- 'total' => $total,
- 'paid_at' => now()->modify($paidAt),
- 'shipped_at' => $shipped ? now() : null,
- ]);
- }
-
- public function test_a_new_merchant_sees_the_first_sale_steps_and_no_ai_note()
- {
- $user = User::factory()->create();
- Store::factory()->for($user)->create(['plan' => SamplePlan::forName('Sambal Nenek')]);
-
- $this->actingAs($user)->get(route('dashboard'))
- ->assertInertia(fn (Assert $page) => $page
- ->component('dashboard')
- ->where('hasSales', false)
- ->where('stats', null)
- ->where('pulse', null)
- ->where('gettingStarted.steps.create', true)
- ->where('gettingStarted.steps.design', true)
- ->where('gettingStarted.steps.products', false)
- ->where('gettingStarted.steps.publish', false));
-
- $this->assertDatabaseCount('dashboard_pulses', 0);
- }
-
- public function test_the_numbers_count_only_paid_orders_of_the_merchants_own_stores()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $this->paidOrder($store, 5000, '-2 days');
- $this->paidOrder($store, 3000, '-1 day', shipped: true);
- Order::factory()->for($store)->create(['total' => 9900]); // Not paid.
- $this->paidOrder(Store::factory()->create(), 7000); // Someone else's store.
- $this->paidOrder($store, 4000, '-40 days'); // Before the 30 days.
-
- $this->actingAs($user)->get(route('dashboard'))
- ->assertInertia(fn (Assert $page) => $page
- ->where('hasSales', true)
- ->where('stats.range', '30d')
- ->where('stats.sales', 80)
- ->where('stats.orders', 2)
- ->where('stats.average', 40)
- ->where('stats.previous_sales', 40)
- ->where('stats.sales_change', 100)
- // Waiting to ship counts every unshipped paid order, however old.
- ->where('stats.to_ship.count', 2)
- ->has('stats.series', 30)
- ->has('stats.recent_orders', 3));
- }
-
- public function test_the_range_and_store_filters_change_the_numbers()
- {
- $user = User::factory()->create();
- $first = Store::factory()->for($user)->create(['name' => 'First']);
- $second = Store::factory()->for($user)->create(['name' => 'Second']);
- $this->paidOrder($first, 1000);
- $this->paidOrder($second, 2000, '-3 days');
-
- $this->actingAs($user)->get(route('dashboard', ['range' => 'today']))
- ->assertInertia(fn (Assert $page) => $page
- ->where('filters.range', 'today')
- ->where('stats.sales', 10));
-
- $this->actingAs($user)->get(route('dashboard', ['store' => $second->slug]))
- ->assertInertia(fn (Assert $page) => $page
- ->where('filters.store', $second->slug)
- ->where('stats.sales', 20)
- ->where('stats.by_store.1.selected', true));
-
- // A store that is not theirs, or a made-up range, falls back to all stores and 30 days.
- $this->actingAs($user)->get(route('dashboard', ['store' => Store::factory()->create()->slug, 'range' => 'forever']))
- ->assertInertia(fn (Assert $page) => $page
- ->where('filters.store', null)
- ->where('filters.range', '30d')
- ->where('stats.sales', 30));
- }
-
- public function test_top_products_join_the_options_of_one_product()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $product = Product::factory()->for($store)->create(['name' => 'Sambal Hijau']);
- $order = $this->paidOrder($store, 3000);
-
- foreach (['Sambal Hijau (250g)', 'Sambal Hijau (500g)'] as $name) {
- $item = new OrderItem([
- 'product_id' => $product->id,
- 'name' => $name,
- 'unit_price' => 1500,
- 'quantity' => 1,
- 'line_total' => 1500,
- ]);
- $item->forceFill(['store_id' => $store->id, 'order_id' => $order->id, 'product_id' => $product->id])->save();
- }
-
- $this->actingAs($user)->get(route('dashboard'))
- ->assertInertia(fn (Assert $page) => $page
- ->has('stats.top_products', 1)
- ->where('stats.top_products.0.name', 'Sambal Hijau')
- ->where('stats.top_products.0.units', 2)
- ->where('stats.top_products.0.revenue', 30));
- }
-
- public function test_needs_attention_lists_orders_to_ship_low_stock_and_unpublished_stores()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create(['name' => 'Kedai']);
- $this->paidOrder($store, 1000, '-2 days');
- Product::factory()->for($store)->create(['name' => 'Kerepek', 'stock' => 2]);
- Product::factory()->for($store)->create(['name' => 'Plenty', 'stock' => 40]);
- Store::factory()->for($user)->count(2)->create(['plan' => SamplePlan::forName('Draft')]);
-
- $this->actingAs($user)->get(route('dashboard'))
- ->assertInertia(fn (Assert $page) => $page
- ->has('stats.attention', 3)
- ->where('stats.attention.0.kind', 'ship')
- ->where('stats.attention.0.note', 'Oldest waited 2 days')
- ->where('stats.attention.1.title', 'Kerepek is almost out')
- // Several stores waiting to be published make one line.
- ->where('stats.attention.2.title', '2 stores are not published'));
- }
-
- public function test_the_sidebar_lists_the_stores_with_orders_to_ship()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create([
- 'status' => StoreStatus::Published,
- 'plan' => SamplePlan::forName('Kedai'),
- 'published_plan' => SamplePlan::forName('Kedai'),
- ]);
- $this->paidOrder($store, 1000);
- $this->paidOrder($store, 1000, shipped: true);
-
- $this->actingAs($user)->get(route('billing'))
- ->assertInertia(fn (Assert $page) => $page
- ->has('sidebar.stores', 1)
- ->where('sidebar.stores.0.slug', $store->slug)
- ->where('sidebar.stores.0.is_published', true)
- ->where('sidebar.stores.0.to_ship', 1)
- ->has('sidebar.usage'));
- }
-
- public function test_the_ai_note_is_written_once_a_day_and_then_read_from_the_database()
- {
- $user = User::factory()->create();
- $this->paidOrder(Store::factory()->for($user)->create(), 2500);
-
- $this->actingAs($user)->get(route('dashboard'))
- ->assertInertia(fn (Assert $page) => $page
- ->missing('pulse')
- ->loadDeferredProps(fn (Assert $reload) => $reload
- ->where('pulse.source', 'ai')
- ->where('pulse.rewrites_left', 3)
- ->where('pulse.rewrites_limit', 3)
- ->where('pulse.actions.0.kind', 'ship')
- ->where('pulse.actions.0.label', 'Ship 1 order')));
-
- $this->assertDatabaseCount('dashboard_pulses', 1);
-
- // Refreshing reads the saved note. Had the AI been asked again, this text would be replaced.
- DashboardPulse::query()->update(['summary' => 'Saved note.']);
- $pulse = app(StorePulse::class)->today($user, null);
-
- $this->assertSame('Saved note.', $pulse['summary']);
- $this->assertDatabaseCount('dashboard_pulses', 1);
- }
-
- public function test_write_again_replaces_the_note_until_the_daily_limit()
- {
- config(['ai.pulse.daily_rewrites' => 2]);
- $user = User::factory()->create();
- $this->paidOrder(Store::factory()->for($user)->create(), 2500);
- app(StorePulse::class)->today($user, null);
- DashboardPulse::query()->update(['summary' => 'Old note.']);
-
- $this->actingAs($user)->post(route('dashboard.pulse'))->assertRedirect();
- $this->assertStringStartsWith('Fake AI', DashboardPulse::query()->value('summary'));
-
- $this->actingAs($user)->post(route('dashboard.pulse'));
- $this->assertSame(2, DashboardPulse::query()->value('rewrites'));
- $this->assertSame(0, app(StorePulse::class)->rewritesLeft($user));
-
- // The limit is used up: the note stays and the merchant is told why.
- DashboardPulse::query()->update(['summary' => 'Kept note.']);
- $this->actingAs($user)->post(route('dashboard.pulse'))
- ->assertInertiaFlash('toast.type', 'error');
- $this->assertSame('Kept note.', DashboardPulse::query()->value('summary'));
- $this->assertSame(2, DashboardPulse::query()->value('rewrites'));
- }
-
- public function test_the_note_is_written_from_the_numbers_when_the_ai_is_not_available()
- {
- config(['ai.tasks.pulse.provider' => 'missing']);
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $this->paidOrder($store, 12000, '-1 day');
-
- $pulse = app(StorePulse::class)->today($user, null);
-
- $this->assertSame('numbers', $pulse['source']);
- $this->assertSame('Sales this month are RM 120. Ship the 1 waiting order first.', $pulse['summary']);
-
- // "Write again" fails without using up a turn.
- $this->assertFalse(app(StorePulse::class)->rewrite($user, null));
- $this->assertSame(3, app(StorePulse::class)->rewritesLeft($user));
- }
-
- public function test_each_store_view_has_its_own_note_and_others_stores_cannot_be_picked()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $this->paidOrder($store, 2500);
-
- app(StorePulse::class)->today($user, null);
- app(StorePulse::class)->today($user, $store);
-
- $this->assertDatabaseCount('dashboard_pulses', 2);
-
- $this->actingAs($user)->post(route('dashboard.pulse'), ['store' => Store::factory()->create()->slug]);
- $this->assertSame(1, (int) DashboardPulse::query()->whereNull('store_id')->value('rewrites'));
- }
- }
|