| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\Ai\PlanPrompt;
- use App\StorePlan\PlanValidator;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\ScriptedDriver;
- use Tests\TestCase;
-
- class AnnouncementTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- $this->user = User::factory()->create();
- $this->store = Store::factory()->for($this->user)->create();
- $this->store->plan = SamplePlan::for($this->store);
- $this->store->save();
- $this->actingAs($this->user);
- }
-
- /**
- * @return array<string, mixed>
- */
- private function settings(array $overrides = []): array
- {
- return [
- 'whatsapp_number' => '',
- 'shipping_west' => '8.00',
- 'shipping_east' => '15.00',
- 'free_shipping_over' => '100',
- 'announcement_enabled' => '1',
- 'announcement_messages' => '',
- ...$overrides,
- ];
- }
-
- private function shop(callable $assert): void
- {
- $this->get(route('stores.preview', $this->store->slug))->assertInertia(fn (Assert $page) => $assert($page));
- }
-
- public function test_the_bar_fills_itself_from_free_shipping_and_running_discounts()
- {
- $this->put(route('stores.settings.update', $this->store->slug), $this->settings())->assertSessionHasNoErrors();
- $this->post(route('stores.automatic-discounts.store', $this->store->slug), [
- 'name' => 'Raya Sale', 'type' => 'percent', 'value' => '10', 'minimum_quantity' => 2,
- 'applies_to' => 'all', 'active' => '1',
- ])->assertSessionHasNoErrors();
- // A sale that has ended is never announced.
- $this->post(route('stores.automatic-discounts.store', $this->store->slug), [
- 'name' => 'Old Sale', 'type' => 'percent', 'value' => '50', 'applies_to' => 'all',
- 'starts_on' => '2020-01-01', 'ends_on' => '2020-01-31', 'active' => '1',
- ])->assertSessionHasNoErrors();
-
- $this->shop(fn (Assert $page) => $page
- ->where('shop.announcements', ['Free shipping on orders over RM 100.00', 'Raya Sale: Buy 2 or more, get 10% off'])
- ->where('shop.shipping_from', 8));
-
- $this->get(route('stores.settings', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('settings.announcement_enabled', true)
- ->where('automaticAnnouncements', ['Free shipping on orders over RM 100.00', 'Raya Sale: Buy 2 or more, get 10% off']));
- }
-
- public function test_the_merchant_can_add_lines_or_switch_it_off()
- {
- $this->put(route('stores.settings.update', $this->store->slug), $this->settings([
- 'free_shipping_over' => '0',
- 'announcement_messages' => "Raya orders close 25 March\n\n New: Sambal Bilis ",
- ]))->assertSessionHasNoErrors();
-
- $this->assertSame(['Raya orders close 25 March', 'New: Sambal Bilis'], $this->store->fresh()->announcement_messages);
- $this->shop(fn (Assert $page) => $page->where('shop.announcements', [
- 'Raya orders close 25 March', 'New: Sambal Bilis', 'Free shipping on every order',
- ]));
-
- $this->put(route('stores.settings.update', $this->store->slug), $this->settings(['announcement_enabled' => '0']));
- $this->shop(fn (Assert $page) => $page->where('shop.announcements', []));
- }
-
- public function test_own_lines_are_limited()
- {
- $url = route('stores.settings.update', $this->store->slug);
-
- $this->put($url, $this->settings(['announcement_messages' => "One\nTwo\nThree\nFour"]))->assertSessionHasErrors('announcement_messages');
- $this->put($url, $this->settings(['announcement_messages' => str_repeat('x', 91)]))->assertSessionHasErrors('announcement_messages');
- }
-
- public function test_the_bar_speaks_malay_in_a_malay_store()
- {
- $this->store->forceFill(['language' => 'ms', 'free_shipping_over' => 5000])->save();
-
- $this->shop(fn (Assert $page) => $page->where('shop.announcements', ['Penghantaran percuma untuk pesanan melebihi RM 50.00']));
- }
-
- public function test_the_trust_row_is_checked_like_any_section()
- {
- $validator = app(PlanValidator::class);
- $plan = $this->store->plan;
- $trust = fn (array $items, string $variant = 'row') => [...$plan, 'pages' => [[
- ...$plan['pages'][0],
- 'sections' => [...$plan['pages'][0]['sections'], ['id' => 'trust-2', 'type' => 'trust_bar', 'variant' => $variant, 'content' => ['items' => $items]]],
- ]]];
-
- $this->assertTrue($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'custom', 'icon' => 'leaf', 'title' => 'Made in Penang']], 'boxed')));
- // A custom item needs a title; kinds and icons come from fixed lists; 2 to 4 items.
- $this->assertFalse($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'custom']])));
- $this->assertFalse($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'returns']])));
- $this->assertFalse($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'payment', 'icon' => 'rocket']])));
- $this->assertFalse($validator->passes($trust([['kind' => 'shipping']])));
- $this->assertFalse($validator->passes($trust([['kind' => 'shipping'], ['kind' => 'payment']], 'carousel')));
- }
-
- public function test_the_ai_can_add_a_trust_row_by_chat()
- {
- ScriptedDriver::use([(string) json_encode([
- 'reply' => 'I added a trust row under the hero.',
- 'add' => [['page' => 'home', 'after' => 'hero', 'section' => [
- 'id' => 'reassure', 'type' => 'trust_bar', 'variant' => 'row',
- 'content' => ['items' => [['kind' => 'shipping'], ['kind' => 'payment'], ['kind' => 'whatsapp']]],
- ]]],
- ])]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add a trust row']);
-
- $ids = array_column($this->store->fresh()->plan['pages'][0]['sections'], 'id');
- $this->assertSame('reassure', $ids[array_search('hero', $ids, true) + 1]);
- $this->assertStringContainsString('"trust_bar"', ScriptedDriver::$requests[0]->system);
- $this->assertStringContainsString('Never invent return policies', app(PlanPrompt::class)::generateSystem());
- }
- }
|