create(); $this->store = Store::factory()->for($user)->create(); $this->store->plan = SamplePlan::for($this->store); $this->store->save(); $this->actingAs($user); } /** * @param array $answer * @param array $target */ private function ask(array $answer, array $target, string $instruction = 'Make this shorter'): StoreGeneration { ScriptedDriver::use([(string) json_encode($answer)]); $this->post(route('stores.edits', $this->store->slug), ['instruction' => $instruction, ...$target]) ->assertSessionHasNoErrors(); return $this->store->generations()->latest('id')->firstOrFail(); } /** * @return array */ private function section(string $id): array { foreach ($this->store->fresh()->plan['pages'][0]['sections'] as $section) { if ($section['id'] === $id) { return $section; } } $this->fail("No section {$id}."); } public function test_only_the_selected_section_changes_whatever_the_ai_sends() { $before = $this->store->plan; $edit = $this->ask([ 'reply' => 'Shorter now.', 'update' => [ ['id' => 'hero', 'content' => ['headline' => 'Short and sweet']], ['id' => 'faq', 'content' => ['heading' => 'Not asked for']], ], 'theme' => ['colors' => ['primary' => '#111111']], 'remove' => ['newsletter'], ], [ 'target_section' => 'hero', 'target_label' => 'Hero', 'target_detail' => 'Heading "Welcome to our store"', ]); $this->assertSame('completed', $edit->status->value); $this->assertTrue($edit->changed); $this->assertSame(['section' => 'hero', 'label' => 'Hero', 'detail' => 'Heading "Welcome to our store"'], $edit->target); $this->assertSame('Short and sweet', $this->section('hero')['content']['headline']); $this->assertEquals($before['theme'], $this->store->fresh()->plan['theme']); $this->assertEquals($before['pages'][0]['sections'][8], $this->section('faq')); $this->section('newsletter'); // The AI was told what was selected. $prompt = ScriptedDriver::$requests[0]->prompt; $this->assertStringContainsString('section id: "hero" (type hero, on page "home")', $prompt); $this->assertStringContainsString('Inside it they selected: Heading "Welcome to our store"', $prompt); } public function test_a_section_can_be_removed_or_followed_by_a_new_one() { $this->ask([ 'reply' => 'Added one below.', 'add' => [ ['page' => 'home', 'after' => 'about', 'section' => ['id' => 'more', 'type' => 'about', 'variant' => 'simple', 'content' => ['heading' => 'More', 'body' => 'More about us.', 'stats' => []]]], ['page' => 'home', 'after' => 'hero', 'section' => ['id' => 'elsewhere', 'type' => 'about', 'variant' => 'simple', 'content' => ['heading' => 'No', 'body' => 'Not here.', 'stats' => []]]], ], ], ['target_section' => 'about', 'target_label' => 'About'], 'Add a section below this'); $ids = array_column($this->store->fresh()->plan['pages'][0]['sections'], 'id'); $this->assertSame('more', $ids[array_search('about', $ids, true) + 1]); $this->assertNotContains('elsewhere', $ids); $this->ask(['reply' => 'Removed.', 'remove' => ['about', 'faq']], ['target_section' => 'about', 'target_label' => 'About'], 'Remove this'); $ids = array_column($this->store->fresh()->plan['pages'][0]['sections'], 'id'); $this->assertNotContains('about', $ids); $this->assertContains('faq', $ids); } public function test_the_header_can_be_selected() { $theme = $this->store->plan['theme']; $this->ask([ 'reply' => 'Renamed.', 'header' => ['content' => ['logoText' => 'New Name']], 'footer' => ['content' => ['text' => 'Not asked for']], 'theme' => ['radius' => 'none'], ], ['target_section' => 'header', 'target_label' => 'Header'], 'Change this name'); $plan = $this->store->fresh()->plan; $this->assertSame('New Name', $plan['header']['content']['logoText']); $this->assertNotSame('Not asked for', $plan['footer']['content']['text'] ?? null); $this->assertEquals($theme, $plan['theme']); } public function test_a_whole_store_change_with_a_part_selected_changes_nothing() { $edit = $this->ask( ['reply' => 'That would change the whole store.', 'theme' => ['colors' => ['primary' => '#111111']]], ['target_section' => 'products', 'target_label' => 'Products'], 'Make the colours dark', ); $this->assertFalse($edit->changed); $this->get(route('stores.show', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('edits.0.target.label', 'Products') ->where('edits.0.changed', false)); } public function test_a_section_that_no_longer_exists_is_refused() { ScriptedDriver::use([]); $this->post(route('stores.edits', $this->store->slug), [ 'instruction' => 'Change this', 'target_section' => 'gone', 'target_label' => 'FAQ', ])->assertSessionHasErrors('instruction'); $this->assertSame(0, $this->store->generations()->count()); } }