user = User::factory()->create(); $this->store = Store::factory()->for($this->user)->create(['shipping_west' => 800, 'shipping_east' => 1500]); $this->store->plan = SamplePlan::for($this->store); $this->store->save(); $this->actingAs($this->user); } private function product(string $name, array $attributes = []): Product { return $this->store->products()->create(['name' => $name, 'price' => 1000, 'stock' => 5, 'status' => 'active', ...$attributes]); } private function page(Product $product, callable $assert): void { $this->get(route('stores.preview.product', [$this->store->slug, $product->slug])) ->assertInertia(fn (Assert $page) => $assert($page)); } public function test_the_product_form_saves_folding_sections() { $this->post(route('stores.products.store', $this->store->slug), [ 'name' => 'Sunscreen', 'status' => 'active', 'price' => '49', 'stock' => 10, 'details' => [ ['title' => ' How to use ', 'body' => 'Apply as the last step of your morning routine.'], ['title' => '', 'body' => ''], ['title' => 'Care', 'body' => 'Keep out of direct sun.'], ], ])->assertSessionHasNoErrors(); $product = $this->store->products()->firstOrFail(); $this->assertSame([ ['title' => 'How to use', 'body' => 'Apply as the last step of your morning routine.'], ['title' => 'Care', 'body' => 'Keep out of direct sun.'], ], $product->details); $this->get(route('stores.products.edit', [$this->store->slug, $product->id])) ->assertInertia(fn (Assert $page) => $page->where('product.details.1.title', 'Care')); $this->page($product, fn (Assert $page) => $page ->where('view.product.details.0.title', 'How to use') ->where('shop.shipping_west', 8) ->where('shop.shipping_east', 15)); // A title needs text and the other way round; at most 5. $url = route('stores.products.update', [$this->store->slug, $product->id]); $base = ['name' => 'Sunscreen', 'status' => 'active', 'price' => '49', 'stock' => 10]; $this->post($url, [...$base, 'details' => [['title' => 'How to use', 'body' => '']]])->assertSessionHasErrors('details.0.body'); $this->post($url, [...$base, 'details' => array_fill(0, 6, ['title' => 'A', 'body' => 'B'])])->assertSessionHasErrors('details'); // Saving with no sections removes them. $this->post($url, $base)->assertSessionHasNoErrors(); $this->assertNull($product->fresh()->details); } public function test_pairs_well_with_puts_bundle_partners_first() { $cleanser = $this->product('Cleanser'); $moisturiser = $this->product('Moisturiser'); $this->product('Lip balm', ['stock' => 0]); $draft = $this->product('Hidden', ['status' => 'draft']); $toner = $this->product('Toner'); $this->post(route('stores.products.store', $this->store->slug), [ 'name' => 'Duo', 'status' => 'active', 'price' => '70', 'is_bundle' => '1', 'bundle_items' => [['item' => (string) $cleanser->id, 'quantity' => 1], ['item' => (string) $moisturiser->id, 'quantity' => 1]], ])->assertSessionHasNoErrors(); $duo = $this->store->products()->where('name', 'Duo')->firstOrFail(); // The moisturiser shares a bundle with the cleanser, then the bundle itself, then others in stock. $this->page($cleanser, fn (Assert $page) => $page ->where('view.related.0.id', $moisturiser->id) ->where('view.related.1.id', $duo->id) ->where('view.related.2.id', $toner->id) ->has('view.related', 3)); $this->assertNotContains($draft->id, [$moisturiser->id, $duo->id, $toner->id]); } public function test_the_ai_merges_sections_by_title() { $product = $this->product('Sunscreen', ['details' => [ ['title' => 'How to use', 'body' => 'Old text.'], ['title' => 'Care', 'body' => 'Keep cool.'], ]]); ScriptedDriver::use([(string) json_encode(['reply' => 'Confirm below.', 'actions' => [[ 'type' => 'update_product', 'product' => $product->slug, 'details' => [ ['title' => 'how to use', 'body' => 'Apply generously 15 minutes before going out.'], ['title' => 'Good to know', 'body' => 'Reapply after swimming.'], ], 'remove_details' => ['Care'], ]]])]); $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Rewrite how to use, add good to know, drop care']); $edit = $this->store->generations()->firstOrFail(); $this->assertSame( 'Change "Sunscreen": sections "how to use": "Apply generously 15 minutes before going out."; "Good to know": "Reapply after swimming.", remove the section "Care"', $edit->actions[0]['summary'], ); $this->assertStringContainsString($product->slug.': Sunscreen, active, no description, sections: How to use, Care', ScriptedDriver::$requests[0]->prompt); $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]))->assertSessionHasNoErrors(); $this->assertSame([ ['title' => 'how to use', 'body' => 'Apply generously 15 minutes before going out.'], ['title' => 'Good to know', 'body' => 'Reapply after swimming.'], ], $product->fresh()->details); } public function test_a_new_product_from_chat_can_have_sections() { ScriptedDriver::use([(string) json_encode(['reply' => 'Confirm below.', 'actions' => [[ 'type' => 'create_product', 'name' => 'Lip Balm', 'price' => 15, 'stock' => 10, 'details' => [['title' => 'How to use', 'body' => 'Apply whenever lips feel dry.']], ]]])]); $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add a lip balm']); $edit = $this->store->generations()->firstOrFail(); $this->assertStringEndsWith('. Sections "How to use": "Apply whenever lips feel dry."', $edit->actions[0]['summary']); $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id])); $this->assertSame('How to use', $this->store->products()->firstOrFail()->details[0]['title']); } }