user = User::factory()->create(); $this->store = Store::factory()->for($this->user)->create(['description' => 'Gentle skincare.']); $this->store->plan = SamplePlan::for($this->store); $this->store->save(); $this->actingAs($this->user); $this->sunscreen = $this->store->products()->create(['name' => 'Sunscreen', 'price' => 4900, 'stock' => 5, 'status' => 'active']); $this->sunscreen->images()->create(['store_id' => $this->store->id, 'path' => 'products/1/sun.jpg', 'position' => 1]); $this->cleanser = $this->store->products()->create(['name' => 'Cleanser', 'price' => 4200, 'stock' => 5, 'status' => 'active']); } private function collection(string $name, array $productIds): Collection { $this->post(route('stores.collections.store', $this->store->slug), ['name' => $name, 'product_ids' => $productIds]) ->assertSessionHasNoErrors(); return $this->store->collections()->latest('id')->firstOrFail(); } /** * The sample plan with one more section on the home page. * * @param array $section * @return array */ private function withSection(array $section): array { $plan = $this->store->plan; $plan['pages'][0]['sections'][] = $section; return $plan; } public function test_the_merchant_can_manage_collections() { $sun = $this->collection('Sun care', [$this->sunscreen->id]); $this->assertSame('sun-care', $sun->slug); $this->assertSame([$this->sunscreen->id], $sun->products->modelKeys()); $this->get(route('stores.products.index', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('collections.0.name', 'Sun care')->where('collections.0.products_count', 1)); // Renaming keeps the address, so links keep working. $this->put(route('stores.collections.update', [$this->store->slug, $sun->id]), [ 'name' => 'Sun protection', 'product_ids' => [$this->cleanser->id, $this->sunscreen->id], ])->assertSessionHasNoErrors(); $sun->refresh(); $this->assertSame('sun-care', $sun->slug); $this->assertSame([$this->cleanser->id, $this->sunscreen->id], $sun->products->modelKeys()); // Only this store's products. $foreign = Product::factory()->create(); $this->post(route('stores.collections.store', $this->store->slug), ['name' => 'Bad', 'product_ids' => [$foreign->id]]) ->assertSessionHasErrors('product_ids.0'); $this->delete(route('stores.collections.destroy', [$this->store->slug, $sun->id])); $this->assertSame(0, Collection::count()); $this->assertNotNull($this->sunscreen->fresh()); $other = Store::factory()->create(); $theirs = $other->collections()->create(['name' => 'Theirs']); $this->get(route('stores.collections.edit', [$this->store->slug, $theirs->id]))->assertNotFound(); } public function test_shoppers_can_browse_a_collection() { $sun = $this->collection('Sun care', [$this->sunscreen->id]); // A collection whose products are all hidden is not offered to shoppers. $draft = $this->store->products()->create(['name' => 'Hidden', 'price' => 100, 'stock' => 1, 'status' => 'draft']); $this->collection('Coming soon', [$draft->id]); $this->get(route('stores.preview.collection', [$this->store->slug, 'sun-care'])) ->assertInertia(fn (Assert $page) => $page ->where('view.type', 'collection') ->where('view.collection.name', 'Sun care') ->where('view.collectionProducts.0.name', 'Sunscreen') ->has('shop.collections', 1) ->where('shop.collections.0.image', '/storage/products/1/sun.jpg')); $this->get(route('stores.preview.collection', [$this->store->slug, 'nope']))->assertNotFound(); $this->assertNotNull($sun); } public function test_links_to_collections_are_allowed_and_the_address_is_reserved() { $validator = app(PlanValidator::class); $plan = $this->store->plan; $plan['header']['content']['links'][] = ['label' => 'Sun care', 'href' => '/collections/sun-care']; $this->assertTrue($validator->passes($plan)); $plan['pages'][] = ['id' => 'collections', 'title' => 'Collections', 'sections' => [$this->store->plan['pages'][0]['sections'][0]]]; $this->assertFalse($validator->passes($plan)); } public function test_shop_by_bento_and_video_sections_are_checked() { $validator = app(PlanValidator::class); $tiles = [ ['title' => 'Bestsellers', 'href' => '/shop', 'image' => 'product:sunscreen'], ['title' => 'Gift sets', 'text' => 'Ready to give', 'href' => '/collections/gifts', 'tone' => 'primary'], ['title' => 'Our story', 'href' => '#about', 'tone' => 'dark'], ]; $this->assertTrue($validator->passes($this->withSection(['id' => 'by', 'type' => 'shop_by', 'variant' => 'circles', 'content' => ['heading' => 'Shop by need', 'collections' => []]]))); $this->assertTrue($validator->passes($this->withSection(['id' => 'tiles', 'type' => 'bento', 'variant' => 'mosaic', 'content' => ['tiles' => $tiles]]))); $this->assertTrue($validator->passes($this->withSection(['id' => 'film', 'type' => 'video', 'variant' => 'split', 'content' => ['heading' => 'How we make it', 'videoUrl' => 'https://youtu.be/dQw4w9WgXcQ']]))); // Too few tiles, a colour that does not exist, a video from another site. $this->assertFalse($validator->passes($this->withSection(['id' => 'tiles', 'type' => 'bento', 'variant' => 'mosaic', 'content' => ['tiles' => array_slice($tiles, 0, 2)]]))); $this->assertFalse($validator->passes($this->withSection(['id' => 'tiles', 'type' => 'bento', 'variant' => 'even', 'content' => ['tiles' => [...array_slice($tiles, 0, 2), ['title' => 'X', 'href' => '/shop', 'tone' => 'neon']]]]))); $this->assertFalse($validator->passes($this->withSection(['id' => 'film', 'type' => 'video', 'variant' => 'wide', 'content' => ['heading' => 'Film', 'videoUrl' => 'https://evil.example/video.mp4']]))); } public function test_bento_photos_reach_the_storefront() { $this->store->plan = app(PlanValidator::class)->validate($this->withSection(['id' => 'tiles', 'type' => 'bento', 'variant' => 'mosaic', 'content' => ['tiles' => [ ['title' => 'Sunscreen', 'href' => '/products/sunscreen', 'image' => 'product:sunscreen'], ['title' => 'Shop', 'href' => '/shop', 'tone' => 'primary'], ['title' => 'Story', 'href' => '#about'], ]]])); $this->store->save(); $this->get(route('stores.preview', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('heroImages.refs.product:sunscreen', '/storage/products/1/sun.jpg')); } public function test_the_ai_can_only_use_real_collections_and_videos_the_merchant_gave() { $this->collection('Sun care', [$this->sunscreen->id]); $shopBy = fn (string $slug) => (string) json_encode(['reply' => 'Done.', 'add' => [['page' => 'home', 'after' => 'hero', 'section' => [ 'id' => 'by', 'type' => 'shop_by', 'variant' => 'tiles', 'content' => ['heading' => 'Shop by need', 'collections' => [$slug]], ]]]]); ScriptedDriver::use([$shopBy('made-up'), $shopBy('sun-care')]); $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add shop by collection']); $this->assertStringContainsString('- sun-care: Sun care (1 products)', ScriptedDriver::$requests[0]->prompt); $this->assertStringContainsString('made-up', ScriptedDriver::$requests[1]->prompt); $this->assertSame(GenerationStatus::Completed, $this->store->generations()->latest('id')->firstOrFail()->status); // A video address the merchant never wrote is refused, twice, so nothing changes. $video = (string) json_encode(['reply' => 'Done.', 'add' => [['page' => 'home', 'after' => null, 'section' => [ 'id' => 'film', 'type' => 'video', 'variant' => 'wide', 'content' => ['heading' => 'Our story', 'videoUrl' => 'https://youtu.be/dQw4w9WgXcQ'], ]]]]); ScriptedDriver::use([$video, $video]); $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add a video about us']); $this->assertSame(GenerationStatus::Failed, $this->store->generations()->latest('id')->firstOrFail()->status); // With the address in the message, it works. ScriptedDriver::use([$video]); $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add this video: https://youtu.be/dQw4w9WgXcQ']); $this->assertSame(GenerationStatus::Completed, $this->store->generations()->latest('id')->firstOrFail()->status); $this->assertSame('film', collect($this->store->fresh()->plan['pages'][0]['sections'])->last()['id']); } public function test_a_collection_can_be_created_by_chat() { ScriptedDriver::use([(string) json_encode(['reply' => 'Confirm below.', 'actions' => [[ 'type' => 'create_collection', 'name' => 'Daily essentials', 'products' => [$this->cleanser->slug, $this->sunscreen->slug], ]]])]); $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Make a daily essentials collection']); $edit = $this->store->generations()->firstOrFail(); $this->assertSame('Create the collection "Daily essentials" with: Cleanser, Sunscreen', $edit->actions[0]['summary']); $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]))->assertSessionHasNoErrors(); $this->assertSame([$this->cleanser->id, $this->sunscreen->id], Collection::firstOrFail()->products->modelKeys()); } }