AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AnnouncementTest.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Models\Store;
  4. use App\Models\User;
  5. use App\StorePlan\Ai\PlanPrompt;
  6. use App\StorePlan\PlanValidator;
  7. use App\StorePlan\SamplePlan;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Inertia\Testing\AssertableInertia as Assert;
  10. use Tests\Support\ScriptedDriver;
  11. use Tests\TestCase;
  12. class AnnouncementTest extends TestCase
  13. {
  14. use RefreshDatabase;
  15. private User $user;
  16. private Store $store;
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->user = User::factory()->create();
  21. $this->store = Store::factory()->for($this->user)->create();
  22. $this->store->plan = SamplePlan::for($this->store);
  23. $this->store->save();
  24. $this->actingAs($this->user);
  25. }
  26. /**
  27. * @return array<string, mixed>
  28. */
  29. private function settings(array $overrides = []): array
  30. {
  31. return [
  32. 'whatsapp_number' => '',
  33. 'shipping_west' => '8.00',
  34. 'shipping_east' => '15.00',
  35. 'free_shipping_over' => '100',
  36. 'announcement_enabled' => '1',
  37. 'announcement_messages' => '',
  38. ...$overrides,
  39. ];
  40. }
  41. private function shop(callable $assert): void
  42. {
  43. $this->get(route('stores.preview', $this->store->slug))->assertInertia(fn (Assert $page) => $assert($page));
  44. }
  45. public function test_the_bar_fills_itself_from_free_shipping_and_running_discounts()
  46. {
  47. $this->put(route('stores.settings.update', $this->store->slug), $this->settings())->assertSessionHasNoErrors();
  48. $this->post(route('stores.automatic-discounts.store', $this->store->slug), [
  49. 'name' => 'Raya Sale', 'type' => 'percent', 'value' => '10', 'minimum_quantity' => 2,
  50. 'applies_to' => 'all', 'active' => '1',
  51. ])->assertSessionHasNoErrors();
  52. // A sale that has ended is never announced.
  53. $this->post(route('stores.automatic-discounts.store', $this->store->slug), [
  54. 'name' => 'Old Sale', 'type' => 'percent', 'value' => '50', 'applies_to' => 'all',
  55. 'starts_on' => '2020-01-01', 'ends_on' => '2020-01-31', 'active' => '1',
  56. ])->assertSessionHasNoErrors();
  57. $this->shop(fn (Assert $page) => $page
  58. ->where('shop.announcements', ['Free shipping on orders over RM 100.00', 'Raya Sale: Buy 2 or more, get 10% off'])
  59. ->where('shop.shipping_from', 8));
  60. $this->get(route('stores.settings', $this->store->slug))
  61. ->assertInertia(fn (Assert $page) => $page
  62. ->where('settings.announcement_enabled', true)
  63. ->where('automaticAnnouncements', ['Free shipping on orders over RM 100.00', 'Raya Sale: Buy 2 or more, get 10% off']));
  64. }
  65. public function test_the_merchant_can_add_lines_or_switch_it_off()
  66. {
  67. $this->put(route('stores.settings.update', $this->store->slug), $this->settings([
  68. 'free_shipping_over' => '0',
  69. 'announcement_messages' => "Raya orders close 25 March\n\n New: Sambal Bilis ",
  70. ]))->assertSessionHasNoErrors();
  71. $this->assertSame(['Raya orders close 25 March', 'New: Sambal Bilis'], $this->store->fresh()->announcement_messages);
  72. $this->shop(fn (Assert $page) => $page->where('shop.announcements', [
  73. 'Raya orders close 25 March', 'New: Sambal Bilis', 'Free shipping on every order',
  74. ]));
  75. $this->put(route('stores.settings.update', $this->store->slug), $this->settings(['announcement_enabled' => '0']));
  76. $this->shop(fn (Assert $page) => $page->where('shop.announcements', []));
  77. }
  78. public function test_own_lines_are_limited()
  79. {
  80. $url = route('stores.settings.update', $this->store->slug);
  81. $this->put($url, $this->settings(['announcement_messages' => "One\nTwo\nThree\nFour"]))->assertSessionHasErrors('announcement_messages');
  82. $this->put($url, $this->settings(['announcement_messages' => str_repeat('x', 91)]))->assertSessionHasErrors('announcement_messages');
  83. }
  84. public function test_the_bar_speaks_malay_in_a_malay_store()
  85. {
  86. $this->store->forceFill(['language' => 'ms', 'free_shipping_over' => 5000])->save();
  87. $this->shop(fn (Assert $page) => $page->where('shop.announcements', ['Penghantaran percuma untuk pesanan melebihi RM 50.00']));
  88. }
  89. public function test_the_trust_row_is_checked_like_any_section()
  90. {
  91. $validator = app(PlanValidator::class);
  92. $plan = $this->store->plan;
  93. $trust = fn (array $items, string $variant = 'row') => [...$plan, 'pages' => [[
  94. ...$plan['pages'][0],
  95. 'sections' => [...$plan['pages'][0]['sections'], ['id' => 'trust-2', 'type' => 'trust_bar', 'variant' => $variant, 'content' => ['items' => $items]]],
  96. ]]];
  97. $this->assertTrue($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'custom', 'icon' => 'leaf', 'title' => 'Made in Penang']], 'boxed')));
  98. // A custom item needs a title; kinds and icons come from fixed lists; 2 to 4 items.
  99. $this->assertFalse($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'custom']])));
  100. $this->assertFalse($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'returns']])));
  101. $this->assertFalse($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'payment', 'icon' => 'rocket']])));
  102. $this->assertFalse($validator->passes($trust([['kind' => 'shipping']])));
  103. $this->assertFalse($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'payment']], 'carousel')));
  104. }
  105. public function test_the_ai_can_add_a_trust_row_by_chat()
  106. {
  107. ScriptedDriver::use([(string) json_encode([
  108. 'reply' => 'I added a trust row under the hero.',
  109. 'add' => [['page' => 'home', 'after' => 'hero', 'section' => [
  110. 'id' => 'reassure', 'type' => 'trust_bar', 'variant' => 'row',
  111. 'content' => ['items' => [['kind' => 'shipping'], ['kind' => 'payment'], ['kind' => 'whatsapp']]],
  112. ]]],
  113. ])]);
  114. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add a trust row']);
  115. $ids = array_column($this->store->fresh()->plan['pages'][0]['sections'], 'id');
  116. $this->assertSame('reassure', $ids[array_search('hero', $ids, true) + 1]);
  117. $this->assertStringContainsString('"trust_bar"', ScriptedDriver::$requests[0]->system);
  118. $this->assertStringContainsString('Never invent return policies', app(PlanPrompt::class)::generateSystem());
  119. }
  120. }