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)); } }