['colors' => ['primary' => '#112233'], 'radius' => 'large'], 'update' => [['id' => 'hero', 'variant' => 'split', 'content' => ['headline' => 'New headline']]], ]); $hero = collect($changed['pages'][0]['sections'])->firstWhere('id', 'hero'); $oldHero = collect($plan['pages'][0]['sections'])->firstWhere('id', 'hero'); $this->assertSame('#112233', $changed['theme']['colors']['primary']); $this->assertSame($plan['theme']['colors']['background'], $changed['theme']['colors']['background']); $this->assertSame('large', $changed['theme']['radius']); $this->assertSame('split', $hero['variant']); $this->assertSame('New headline', $hero['content']['headline']); $this->assertSame($oldHero['content']['subheadline'], $hero['content']['subheadline']); $this->assertTrue(app(PlanValidator::class)->passes($changed)); } public function test_it_can_add_remove_and_reorder_sections() { $plan = SamplePlan::forName('Kedai'); $ids = array_column($plan['pages'][0]['sections'], 'id'); $faq = collect($plan['pages'][0]['sections'])->firstWhere('type', 'faq'); $changed = PlanChanges::apply($plan, [ 'remove' => [$faq['id']], 'add' => [['after' => 'hero', 'section' => [...$faq, 'id' => 'faq-top']]], ]); $newIds = array_column($changed['pages'][0]['sections'], 'id'); $this->assertNotContains($faq['id'], $newIds); $this->assertSame('faq-top', $newIds[array_search('hero', $newIds, true) + 1]); $this->assertCount(count($ids), $newIds); $reversed = array_reverse($newIds); $this->assertSame($reversed, array_column(PlanChanges::apply($changed, ['order' => $reversed])['pages'][0]['sections'], 'id')); // An order that leaves a section out is ignored. $this->assertSame($newIds, array_column(PlanChanges::apply($changed, ['order' => ['hero']])['pages'][0]['sections'], 'id')); } public function test_it_can_change_the_shared_header_and_footer() { $plan = SamplePlan::forName('Kedai'); $changed = PlanChanges::apply($plan, [ 'header' => ['variant' => 'centered', 'content' => ['logoText' => 'Kedai Baru']], 'footer' => ['content' => ['text' => 'New footer text']], ]); $this->assertSame('centered', $changed['header']['variant']); $this->assertSame('Kedai Baru', $changed['header']['content']['logoText']); $this->assertSame($plan['header']['content']['links'], $changed['header']['content']['links']); $this->assertSame('New footer text', $changed['footer']['content']['text']); $this->assertSame($plan['footer']['variant'], $changed['footer']['variant']); $this->assertTrue(app(PlanValidator::class)->passes($changed)); } public function test_it_can_add_rename_and_remove_pages() { $plan = SamplePlan::forName('Kedai'); $about = collect($plan['pages'][0]['sections'])->firstWhere('type', 'about'); $withPage = PlanChanges::apply($plan, [ 'add_pages' => [['id' => 'our-story', 'title' => 'Our story', 'sections' => [[...$about, 'id' => 'story-main']]]], 'header' => ['content' => ['links' => [['label' => 'Our story', 'href' => '/our-story']]]], // A section can be added to the new page in the same answer. 'add' => [['page' => 'our-story', 'after' => null, 'section' => [...$about, 'id' => 'story-more']]], ]); $this->assertSame(['home', 'our-story'], array_column($withPage['pages'], 'id')); $this->assertSame(['story-main', 'story-more'], array_column($withPage['pages'][1]['sections'], 'id')); $this->assertSame($plan['pages'][0], $withPage['pages'][0]); $this->assertTrue(app(PlanValidator::class)->passes($withPage)); $renamed = PlanChanges::apply($withPage, ['update_pages' => [['id' => 'our-story', 'title' => 'About us']]]); $this->assertSame('About us', $renamed['pages'][1]['title']); $removed = PlanChanges::apply($renamed, [ 'remove_pages' => ['our-story', 'home'], 'header' => ['content' => ['links' => []]], ]); // The home page can never be removed. $this->assertSame(['home'], array_column($removed['pages'], 'id')); $this->assertTrue(app(PlanValidator::class)->passes($removed)); } public function test_a_new_page_always_gets_a_menu_link_even_if_the_ai_forgets() { $plan = SamplePlan::forName('Kedai'); $about = collect($plan['pages'][0]['sections'])->firstWhere('type', 'about'); $newPage = fn (string $id, string $title) => ['id' => $id, 'title' => $title, 'sections' => [[...$about, 'id' => $id.'-main']]]; $headerLinks = count($plan['header']['content']['links']); $changed = PlanChanges::apply($plan, ['add_pages' => [$newPage('about', 'About us')]]); $this->assertCount($headerLinks + 1, $changed['header']['content']['links']); $this->assertSame(['label' => 'About us', 'href' => '/about'], end($changed['header']['content']['links'])); $this->assertTrue(app(PlanValidator::class)->passes($changed)); // No second link when the AI did add one itself. $withLink = PlanChanges::apply($plan, [ 'add_pages' => [$newPage('about', 'About us')], 'footer' => ['content' => ['links' => [['label' => 'Our story', 'href' => '/about#about-main']]]], ]); $this->assertCount($headerLinks, $withLink['header']['content']['links']); $this->assertCount(1, $withLink['footer']['content']['links']); // A full header sends the link to the footer instead. $full = $plan; $full['header']['content']['links'] = array_fill(0, 6, ['label' => 'Shop', 'href' => '/shop']); $overflow = PlanChanges::apply($full, ['add_pages' => [$newPage('faq-page', 'Questions')]]); $this->assertCount(6, $overflow['header']['content']['links']); $this->assertSame('/faq-page', end($overflow['footer']['content']['links'])['href']); $this->assertTrue(app(PlanValidator::class)->passes($overflow)); // Removing the page takes its links away too, so no link is left pointing nowhere. $removed = PlanChanges::apply($changed, ['remove_pages' => ['about']]); $this->assertSame($plan['header']['content']['links'], $removed['header']['content']['links']); $this->assertTrue(app(PlanValidator::class)->passes($removed)); } public function test_an_empty_answer_changes_nothing() { $plan = SamplePlan::forName('Kedai'); $this->assertSame($plan, PlanChanges::apply($plan, ['reply' => 'Nothing to do.'])); } public function test_json_is_read_even_with_fences_or_extra_words() { $this->assertSame(['a' => 1], JsonAnswer::decode("Here you go:\n```json\n{\"a\": 1}\n```")); $this->expectException(AiException::class); JsonAnswer::decode('Sorry, I cannot do that.'); } public function test_the_prompt_describes_every_section_type_and_font_from_the_schema() { $format = PlanPrompt::format(); foreach (array_keys(PlanSchema::SECTIONS) as $type) { $this->assertStringContainsString("## $type\n", $format); } foreach (array_keys(PlanSchema::FONTS) as $font) { $this->assertStringContainsString($font, $format); } $this->assertStringNotContainsString('regex:', $format); $this->assertStringNotContainsString('[[FORMAT]]', PlanPrompt::generateSystem().PlanPrompt::editSystem()); } }