| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Enums\GenerationStatus;
- use App\Models\Product;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\Ai\PlanPrompt;
- use App\StorePlan\FeaturedProducts;
- 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 FeaturedProductTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- private string $sectionId;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- $this->user = User::factory()->create();
- $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
- $this->store->plan = SamplePlan::for($this->store);
- $this->store->save();
- $this->sectionId = FeaturedProducts::sections($this->store->plan)[0]['section_id'];
- $this->actingAs($this->user);
- }
-
- private function choose(?string $productSlug, ?string $sectionId = null)
- {
- return $this->put(route('stores.featured.update', [$this->store->slug, $sectionId ?? $this->sectionId]), [
- 'product_slug' => $productSlug,
- ]);
- }
-
- public function test_the_design_tab_lists_featured_sections_and_active_products()
- {
- Product::factory()->for($this->store)->create(['name' => 'Sambal Hijau', 'price' => 1290]);
- Product::factory()->for($this->store)->draft()->create(['name' => 'Hidden']);
- Product::factory()->create(['name' => 'Another store']);
-
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->has('featuredSections', 1)
- ->where('featuredSections.0.section_id', $this->sectionId)
- ->where('featuredSections.0.name', 'The Family Trio')
- ->where('featuredSections.0.product_slug', null)
- ->has('productOptions', 1)
- ->where('productOptions.0.name', 'Sambal Hijau')
- ->where('productOptions.0.price', 12.9));
- }
-
- public function test_the_merchant_can_link_a_featured_section_to_a_real_product()
- {
- $product = Product::factory()->for($this->store)->create(['name' => 'Sambal Hijau', 'price' => 1290, 'compare_at_price' => 1590]);
-
- $this->choose($product->slug)->assertRedirect(route('stores.show', $this->store->slug));
-
- $plan = $this->store->fresh()->plan;
- $this->assertSame([$product->slug], FeaturedProducts::slugs($plan));
- $this->assertTrue(app(PlanValidator::class)->passes($plan));
-
- // The store gets the real product's details, whatever the AI wrote.
- $this->get(route('stores.preview', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where("featuredProducts.{$product->slug}.name", 'Sambal Hijau')
- ->where("featuredProducts.{$product->slug}.price", 12.9)
- ->where("featuredProducts.{$product->slug}.compare_at_price", 15.9));
-
- // Choosing nothing removes the link again.
- $this->choose(null);
- $this->assertSame([], FeaturedProducts::slugs($this->store->fresh()->plan));
- }
-
- public function test_a_price_change_shows_straight_away_without_touching_the_design()
- {
- $product = Product::factory()->for($this->store)->create(['price' => 1290]);
- $this->choose($product->slug);
-
- $product->update(['price' => 990]);
-
- $this->get(route('stores.preview', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where("featuredProducts.{$product->slug}.price", 9.9));
- }
-
- public function test_a_linked_product_always_reaches_the_store_even_with_many_products()
- {
- $featured = Product::factory()->for($this->store)->create(['name' => 'Oldest']);
- Product::factory()->for($this->store)->count(65)->create();
- $this->choose($featured->slug);
-
- $this->get(route('stores.preview', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->has('products', 60)
- ->where("featuredProducts.{$featured->slug}.name", 'Oldest'));
- }
-
- public function test_a_hidden_or_deleted_product_is_not_shown()
- {
- $product = Product::factory()->for($this->store)->create();
- $this->choose($product->slug);
-
- $product->update(['status' => 'draft']);
-
- $this->get(route('stores.preview', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('featuredProducts', []));
- }
-
- public function test_only_this_stores_active_products_can_be_chosen()
- {
- $draft = Product::factory()->for($this->store)->draft()->create();
- $elsewhere = Product::factory()->create();
-
- $this->choose($draft->slug)->assertSessionHasErrors('product_slug');
- $this->choose($elsewhere->slug)->assertSessionHasErrors('product_slug');
- $this->choose('no-such-product')->assertSessionHasErrors('product_slug');
- $this->choose('x', sectionId: 'not-a-featured-section')->assertNotFound();
-
- $this->assertSame([], FeaturedProducts::slugs($this->store->fresh()->plan));
- }
-
- public function test_another_merchant_cannot_change_the_featured_product()
- {
- $product = Product::factory()->for($this->store)->create();
-
- $this->actingAs(User::factory()->create());
-
- $this->choose($product->slug)->assertNotFound();
- $this->assertSame([], FeaturedProducts::slugs($this->store->fresh()->plan));
- }
-
- public function test_on_a_live_store_the_new_link_waits_for_publish_changes()
- {
- $product = Product::factory()->for($this->store)->create();
- $this->post(route('stores.publish', $this->store->slug));
-
- $this->choose($product->slug);
-
- $store = $this->store->fresh();
- $this->assertSame([], FeaturedProducts::slugs($store->published_plan));
- $this->assertTrue($store->hasUnpublishedChanges());
- }
-
- public function test_product_web_address_names_must_look_right()
- {
- $plan = FeaturedProducts::link($this->store->plan, $this->sectionId, 'Bad Slug!');
-
- $this->assertFalse(app(PlanValidator::class)->passes($plan));
- $this->assertTrue(app(PlanValidator::class)->passes(FeaturedProducts::link($this->store->plan, $this->sectionId, 'sambal-hijau-2')));
- }
-
- public function test_the_ai_is_given_the_product_list_and_can_link_a_product()
- {
- $product = Product::factory()->for($this->store)->create(['name' => 'Sambal Hijau', 'price' => 1290]);
-
- ScriptedDriver::use([(string) json_encode([
- 'reply' => 'I featured Sambal Hijau.',
- 'update' => [['id' => $this->sectionId, 'content' => ['productSlug' => $product->slug]]],
- ])]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Feature the green sambal']);
-
- $this->assertSame(GenerationStatus::Completed, $this->store->generations()->firstOrFail()->status);
- $this->assertSame([$product->slug], FeaturedProducts::slugs($this->store->fresh()->plan));
- $this->assertStringContainsString($product->slug.': Sambal Hijau (RM 12.90)', ScriptedDriver::$requests[0]->prompt);
- }
-
- public function test_a_made_up_product_from_the_ai_is_sent_back_to_be_fixed()
- {
- $product = Product::factory()->for($this->store)->create();
- $answer = fn (string $slug) => (string) json_encode([
- 'reply' => 'Done.',
- 'update' => [['id' => $this->sectionId, 'content' => ['productSlug' => $slug]]],
- ]);
-
- ScriptedDriver::use([$answer('imaginary-product'), $answer($product->slug)]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Feature my best product']);
-
- $this->assertSame([$product->slug], FeaturedProducts::slugs($this->store->fresh()->plan));
- $this->assertStringContainsString('imaginary-product', ScriptedDriver::$requests[1]->prompt);
- $this->assertStringContainsString('not products of this store', ScriptedDriver::$requests[1]->prompt);
- }
-
- public function test_an_edit_that_changes_nothing_is_marked_so_even_if_the_ai_says_done()
- {
- ScriptedDriver::use([
- '{"reply": "Done, I featured the moisturiser."}',
- (string) json_encode(['reply' => 'Darker now.', 'theme' => ['colors' => ['primary' => '#111111']]]),
- ]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Feature the moisturiser']);
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Make it darker']);
-
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('edits.0.reply', 'Darker now.')
- ->where('edits.0.changed', true)
- ->where('edits.1.reply', 'Done, I featured the moisturiser.')
- ->where('edits.1.changed', false));
- }
-
- public function test_the_ai_gets_no_product_list_when_the_store_has_no_products()
- {
- $this->assertSame('', PlanPrompt::productList($this->store));
- $this->assertStringNotContainsString('real products', PlanPrompt::generatePrompt($this->store));
- }
- }
|