AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

OverviewTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace Tests\Feature\Dashboard;
  3. use App\Dashboard\StorePulse;
  4. use App\Enums\StoreStatus;
  5. use App\Models\DashboardPulse;
  6. use App\Models\Order;
  7. use App\Models\OrderItem;
  8. use App\Models\Product;
  9. use App\Models\Store;
  10. use App\Models\User;
  11. use App\StorePlan\SamplePlan;
  12. use Illuminate\Foundation\Testing\RefreshDatabase;
  13. use Inertia\Testing\AssertableInertia as Assert;
  14. use Tests\TestCase;
  15. class OverviewTest extends TestCase
  16. {
  17. use RefreshDatabase;
  18. private function paidOrder(Store $store, int $total, string $paidAt = 'now', bool $shipped = false): Order
  19. {
  20. return Order::factory()->paid()->for($store)->create([
  21. 'total' => $total,
  22. 'paid_at' => now()->modify($paidAt),
  23. 'shipped_at' => $shipped ? now() : null,
  24. ]);
  25. }
  26. public function test_a_new_merchant_sees_the_first_sale_steps_and_no_ai_note()
  27. {
  28. $user = User::factory()->create();
  29. Store::factory()->for($user)->create(['plan' => SamplePlan::forName('Sambal Nenek')]);
  30. $this->actingAs($user)->get(route('dashboard'))
  31. ->assertInertia(fn (Assert $page) => $page
  32. ->component('dashboard')
  33. ->where('hasSales', false)
  34. ->where('stats', null)
  35. ->where('pulse', null)
  36. ->where('gettingStarted.steps.create', true)
  37. ->where('gettingStarted.steps.design', true)
  38. ->where('gettingStarted.steps.products', false)
  39. ->where('gettingStarted.steps.publish', false));
  40. $this->assertDatabaseCount('dashboard_pulses', 0);
  41. }
  42. public function test_the_numbers_count_only_paid_orders_of_the_merchants_own_stores()
  43. {
  44. $user = User::factory()->create();
  45. $store = Store::factory()->for($user)->create();
  46. $this->paidOrder($store, 5000, '-2 days');
  47. $this->paidOrder($store, 3000, '-1 day', shipped: true);
  48. Order::factory()->for($store)->create(['total' => 9900]); // Not paid.
  49. $this->paidOrder(Store::factory()->create(), 7000); // Someone else's store.
  50. $this->paidOrder($store, 4000, '-40 days'); // Before the 30 days.
  51. $this->actingAs($user)->get(route('dashboard'))
  52. ->assertInertia(fn (Assert $page) => $page
  53. ->where('hasSales', true)
  54. ->where('stats.range', '30d')
  55. ->where('stats.sales', 80)
  56. ->where('stats.orders', 2)
  57. ->where('stats.average', 40)
  58. ->where('stats.previous_sales', 40)
  59. ->where('stats.sales_change', 100)
  60. // Waiting to ship counts every unshipped paid order, however old.
  61. ->where('stats.to_ship.count', 2)
  62. ->has('stats.series', 30)
  63. ->has('stats.recent_orders', 3));
  64. }
  65. public function test_the_range_and_store_filters_change_the_numbers()
  66. {
  67. $user = User::factory()->create();
  68. $first = Store::factory()->for($user)->create(['name' => 'First']);
  69. $second = Store::factory()->for($user)->create(['name' => 'Second']);
  70. $this->paidOrder($first, 1000);
  71. $this->paidOrder($second, 2000, '-3 days');
  72. $this->actingAs($user)->get(route('dashboard', ['range' => 'today']))
  73. ->assertInertia(fn (Assert $page) => $page
  74. ->where('filters.range', 'today')
  75. ->where('stats.sales', 10));
  76. $this->actingAs($user)->get(route('dashboard', ['store' => $second->slug]))
  77. ->assertInertia(fn (Assert $page) => $page
  78. ->where('filters.store', $second->slug)
  79. ->where('stats.sales', 20)
  80. ->where('stats.by_store.1.selected', true));
  81. // A store that is not theirs, or a made-up range, falls back to all stores and 30 days.
  82. $this->actingAs($user)->get(route('dashboard', ['store' => Store::factory()->create()->slug, 'range' => 'forever']))
  83. ->assertInertia(fn (Assert $page) => $page
  84. ->where('filters.store', null)
  85. ->where('filters.range', '30d')
  86. ->where('stats.sales', 30));
  87. }
  88. public function test_top_products_join_the_options_of_one_product()
  89. {
  90. $user = User::factory()->create();
  91. $store = Store::factory()->for($user)->create();
  92. $product = Product::factory()->for($store)->create(['name' => 'Sambal Hijau']);
  93. $order = $this->paidOrder($store, 3000);
  94. foreach (['Sambal Hijau (250g)', 'Sambal Hijau (500g)'] as $name) {
  95. $item = new OrderItem([
  96. 'product_id' => $product->id,
  97. 'name' => $name,
  98. 'unit_price' => 1500,
  99. 'quantity' => 1,
  100. 'line_total' => 1500,
  101. ]);
  102. $item->forceFill(['store_id' => $store->id, 'order_id' => $order->id, 'product_id' => $product->id])->save();
  103. }
  104. $this->actingAs($user)->get(route('dashboard'))
  105. ->assertInertia(fn (Assert $page) => $page
  106. ->has('stats.top_products', 1)
  107. ->where('stats.top_products.0.name', 'Sambal Hijau')
  108. ->where('stats.top_products.0.units', 2)
  109. ->where('stats.top_products.0.revenue', 30));
  110. }
  111. public function test_needs_attention_lists_orders_to_ship_low_stock_and_unpublished_stores()
  112. {
  113. $user = User::factory()->create();
  114. $store = Store::factory()->for($user)->create(['name' => 'Kedai']);
  115. $this->paidOrder($store, 1000, '-2 days');
  116. Product::factory()->for($store)->create(['name' => 'Kerepek', 'stock' => 2]);
  117. Product::factory()->for($store)->create(['name' => 'Plenty', 'stock' => 40]);
  118. Store::factory()->for($user)->count(2)->create(['plan' => SamplePlan::forName('Draft')]);
  119. $this->actingAs($user)->get(route('dashboard'))
  120. ->assertInertia(fn (Assert $page) => $page
  121. ->has('stats.attention', 3)
  122. ->where('stats.attention.0.kind', 'ship')
  123. ->where('stats.attention.0.note', 'Oldest waited 2 days')
  124. ->where('stats.attention.1.title', 'Kerepek is almost out')
  125. // Several stores waiting to be published make one line.
  126. ->where('stats.attention.2.title', '2 stores are not published'));
  127. }
  128. public function test_the_sidebar_lists_the_stores_with_orders_to_ship()
  129. {
  130. $user = User::factory()->create();
  131. $store = Store::factory()->for($user)->create([
  132. 'status' => StoreStatus::Published,
  133. 'plan' => SamplePlan::forName('Kedai'),
  134. 'published_plan' => SamplePlan::forName('Kedai'),
  135. ]);
  136. $this->paidOrder($store, 1000);
  137. $this->paidOrder($store, 1000, shipped: true);
  138. $this->actingAs($user)->get(route('billing'))
  139. ->assertInertia(fn (Assert $page) => $page
  140. ->has('sidebar.stores', 1)
  141. ->where('sidebar.stores.0.slug', $store->slug)
  142. ->where('sidebar.stores.0.is_published', true)
  143. ->where('sidebar.stores.0.to_ship', 1)
  144. ->has('sidebar.usage'));
  145. }
  146. public function test_the_ai_note_is_written_once_a_day_and_then_read_from_the_database()
  147. {
  148. $user = User::factory()->create();
  149. $this->paidOrder(Store::factory()->for($user)->create(), 2500);
  150. $this->actingAs($user)->get(route('dashboard'))
  151. ->assertInertia(fn (Assert $page) => $page
  152. ->missing('pulse')
  153. ->loadDeferredProps(fn (Assert $reload) => $reload
  154. ->where('pulse.source', 'ai')
  155. ->where('pulse.rewrites_left', 3)
  156. ->where('pulse.rewrites_limit', 3)
  157. ->where('pulse.actions.0.kind', 'ship')
  158. ->where('pulse.actions.0.label', 'Ship 1 order')));
  159. $this->assertDatabaseCount('dashboard_pulses', 1);
  160. // Refreshing reads the saved note. Had the AI been asked again, this text would be replaced.
  161. DashboardPulse::query()->update(['summary' => 'Saved note.']);
  162. $pulse = app(StorePulse::class)->today($user, null);
  163. $this->assertSame('Saved note.', $pulse['summary']);
  164. $this->assertDatabaseCount('dashboard_pulses', 1);
  165. }
  166. public function test_write_again_replaces_the_note_until_the_daily_limit()
  167. {
  168. config(['ai.pulse.daily_rewrites' => 2]);
  169. $user = User::factory()->create();
  170. $this->paidOrder(Store::factory()->for($user)->create(), 2500);
  171. app(StorePulse::class)->today($user, null);
  172. DashboardPulse::query()->update(['summary' => 'Old note.']);
  173. $this->actingAs($user)->post(route('dashboard.pulse'))->assertRedirect();
  174. $this->assertStringStartsWith('Fake AI', DashboardPulse::query()->value('summary'));
  175. $this->actingAs($user)->post(route('dashboard.pulse'));
  176. $this->assertSame(2, DashboardPulse::query()->value('rewrites'));
  177. $this->assertSame(0, app(StorePulse::class)->rewritesLeft($user));
  178. // The limit is used up: the note stays and the merchant is told why.
  179. DashboardPulse::query()->update(['summary' => 'Kept note.']);
  180. $this->actingAs($user)->post(route('dashboard.pulse'))
  181. ->assertInertiaFlash('toast.type', 'error');
  182. $this->assertSame('Kept note.', DashboardPulse::query()->value('summary'));
  183. $this->assertSame(2, DashboardPulse::query()->value('rewrites'));
  184. }
  185. public function test_the_note_is_written_from_the_numbers_when_the_ai_is_not_available()
  186. {
  187. config(['ai.tasks.pulse.provider' => 'missing']);
  188. $user = User::factory()->create();
  189. $store = Store::factory()->for($user)->create();
  190. $this->paidOrder($store, 12000, '-1 day');
  191. $pulse = app(StorePulse::class)->today($user, null);
  192. $this->assertSame('numbers', $pulse['source']);
  193. $this->assertSame('Sales this month are RM 120. Ship the 1 waiting order first.', $pulse['summary']);
  194. // "Write again" fails without using up a turn.
  195. $this->assertFalse(app(StorePulse::class)->rewrite($user, null));
  196. $this->assertSame(3, app(StorePulse::class)->rewritesLeft($user));
  197. }
  198. public function test_each_store_view_has_its_own_note_and_others_stores_cannot_be_picked()
  199. {
  200. $user = User::factory()->create();
  201. $store = Store::factory()->for($user)->create();
  202. $this->paidOrder($store, 2500);
  203. app(StorePulse::class)->today($user, null);
  204. app(StorePulse::class)->today($user, $store);
  205. $this->assertDatabaseCount('dashboard_pulses', 2);
  206. $this->actingAs($user)->post(route('dashboard.pulse'), ['store' => Store::factory()->create()->slug]);
  207. $this->assertSame(1, (int) DashboardPulse::query()->whereNull('store_id')->value('rewrites'));
  208. }
  209. }