AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Tests\Feature\Ai;
  3. use App\Ai\AiException;
  4. use App\StorePlan\Ai\JsonAnswer;
  5. use App\StorePlan\Ai\PlanChanges;
  6. use App\StorePlan\Ai\PlanPrompt;
  7. use App\StorePlan\PlanSchema;
  8. use App\StorePlan\PlanValidator;
  9. use App\StorePlan\SamplePlan;
  10. use Tests\TestCase;
  11. class PlanChangesTest extends TestCase
  12. {
  13. public function test_it_changes_only_the_named_theme_and_content_fields()
  14. {
  15. $plan = SamplePlan::forName('Kedai');
  16. $changed = PlanChanges::apply($plan, [
  17. 'theme' => ['colors' => ['primary' => '#112233'], 'radius' => 'large'],
  18. 'update' => [['id' => 'hero', 'variant' => 'split', 'content' => ['headline' => 'New headline']]],
  19. ]);
  20. $hero = collect($changed['pages'][0]['sections'])->firstWhere('id', 'hero');
  21. $oldHero = collect($plan['pages'][0]['sections'])->firstWhere('id', 'hero');
  22. $this->assertSame('#112233', $changed['theme']['colors']['primary']);
  23. $this->assertSame($plan['theme']['colors']['background'], $changed['theme']['colors']['background']);
  24. $this->assertSame('large', $changed['theme']['radius']);
  25. $this->assertSame('split', $hero['variant']);
  26. $this->assertSame('New headline', $hero['content']['headline']);
  27. $this->assertSame($oldHero['content']['subheadline'], $hero['content']['subheadline']);
  28. $this->assertTrue(app(PlanValidator::class)->passes($changed));
  29. }
  30. public function test_it_can_add_remove_and_reorder_sections()
  31. {
  32. $plan = SamplePlan::forName('Kedai');
  33. $ids = array_column($plan['pages'][0]['sections'], 'id');
  34. $faq = collect($plan['pages'][0]['sections'])->firstWhere('type', 'faq');
  35. $changed = PlanChanges::apply($plan, [
  36. 'remove' => [$faq['id']],
  37. 'add' => [['after' => 'hero', 'section' => [...$faq, 'id' => 'faq-top']]],
  38. ]);
  39. $newIds = array_column($changed['pages'][0]['sections'], 'id');
  40. $this->assertNotContains($faq['id'], $newIds);
  41. $this->assertSame('faq-top', $newIds[array_search('hero', $newIds, true) + 1]);
  42. $this->assertCount(count($ids), $newIds);
  43. $reversed = array_reverse($newIds);
  44. $this->assertSame($reversed, array_column(PlanChanges::apply($changed, ['order' => $reversed])['pages'][0]['sections'], 'id'));
  45. // An order that leaves a section out is ignored.
  46. $this->assertSame($newIds, array_column(PlanChanges::apply($changed, ['order' => ['hero']])['pages'][0]['sections'], 'id'));
  47. }
  48. public function test_it_can_change_the_shared_header_and_footer()
  49. {
  50. $plan = SamplePlan::forName('Kedai');
  51. $changed = PlanChanges::apply($plan, [
  52. 'header' => ['variant' => 'centered', 'content' => ['logoText' => 'Kedai Baru']],
  53. 'footer' => ['content' => ['text' => 'New footer text']],
  54. ]);
  55. $this->assertSame('centered', $changed['header']['variant']);
  56. $this->assertSame('Kedai Baru', $changed['header']['content']['logoText']);
  57. $this->assertSame($plan['header']['content']['links'], $changed['header']['content']['links']);
  58. $this->assertSame('New footer text', $changed['footer']['content']['text']);
  59. $this->assertSame($plan['footer']['variant'], $changed['footer']['variant']);
  60. $this->assertTrue(app(PlanValidator::class)->passes($changed));
  61. }
  62. public function test_it_can_add_rename_and_remove_pages()
  63. {
  64. $plan = SamplePlan::forName('Kedai');
  65. $about = collect($plan['pages'][0]['sections'])->firstWhere('type', 'about');
  66. $withPage = PlanChanges::apply($plan, [
  67. 'add_pages' => [['id' => 'our-story', 'title' => 'Our story', 'sections' => [[...$about, 'id' => 'story-main']]]],
  68. 'header' => ['content' => ['links' => [['label' => 'Our story', 'href' => '/our-story']]]],
  69. // A section can be added to the new page in the same answer.
  70. 'add' => [['page' => 'our-story', 'after' => null, 'section' => [...$about, 'id' => 'story-more']]],
  71. ]);
  72. $this->assertSame(['home', 'our-story'], array_column($withPage['pages'], 'id'));
  73. $this->assertSame(['story-main', 'story-more'], array_column($withPage['pages'][1]['sections'], 'id'));
  74. $this->assertSame($plan['pages'][0], $withPage['pages'][0]);
  75. $this->assertTrue(app(PlanValidator::class)->passes($withPage));
  76. $renamed = PlanChanges::apply($withPage, ['update_pages' => [['id' => 'our-story', 'title' => 'About us']]]);
  77. $this->assertSame('About us', $renamed['pages'][1]['title']);
  78. $removed = PlanChanges::apply($renamed, [
  79. 'remove_pages' => ['our-story', 'home'],
  80. 'header' => ['content' => ['links' => []]],
  81. ]);
  82. // The home page can never be removed.
  83. $this->assertSame(['home'], array_column($removed['pages'], 'id'));
  84. $this->assertTrue(app(PlanValidator::class)->passes($removed));
  85. }
  86. public function test_a_new_page_always_gets_a_menu_link_even_if_the_ai_forgets()
  87. {
  88. $plan = SamplePlan::forName('Kedai');
  89. $about = collect($plan['pages'][0]['sections'])->firstWhere('type', 'about');
  90. $newPage = fn (string $id, string $title) => ['id' => $id, 'title' => $title, 'sections' => [[...$about, 'id' => $id.'-main']]];
  91. $headerLinks = count($plan['header']['content']['links']);
  92. $changed = PlanChanges::apply($plan, ['add_pages' => [$newPage('about', 'About us')]]);
  93. $this->assertCount($headerLinks + 1, $changed['header']['content']['links']);
  94. $this->assertSame(['label' => 'About us', 'href' => '/about'], end($changed['header']['content']['links']));
  95. $this->assertTrue(app(PlanValidator::class)->passes($changed));
  96. // No second link when the AI did add one itself.
  97. $withLink = PlanChanges::apply($plan, [
  98. 'add_pages' => [$newPage('about', 'About us')],
  99. 'footer' => ['content' => ['links' => [['label' => 'Our story', 'href' => '/about#about-main']]]],
  100. ]);
  101. $this->assertCount($headerLinks, $withLink['header']['content']['links']);
  102. $this->assertCount(1, $withLink['footer']['content']['links']);
  103. // A full header sends the link to the footer instead.
  104. $full = $plan;
  105. $full['header']['content']['links'] = array_fill(0, 6, ['label' => 'Shop', 'href' => '/shop']);
  106. $overflow = PlanChanges::apply($full, ['add_pages' => [$newPage('faq-page', 'Questions')]]);
  107. $this->assertCount(6, $overflow['header']['content']['links']);
  108. $this->assertSame('/faq-page', end($overflow['footer']['content']['links'])['href']);
  109. $this->assertTrue(app(PlanValidator::class)->passes($overflow));
  110. // Removing the page takes its links away too, so no link is left pointing nowhere.
  111. $removed = PlanChanges::apply($changed, ['remove_pages' => ['about']]);
  112. $this->assertSame($plan['header']['content']['links'], $removed['header']['content']['links']);
  113. $this->assertTrue(app(PlanValidator::class)->passes($removed));
  114. }
  115. public function test_an_empty_answer_changes_nothing()
  116. {
  117. $plan = SamplePlan::forName('Kedai');
  118. $this->assertSame($plan, PlanChanges::apply($plan, ['reply' => 'Nothing to do.']));
  119. }
  120. public function test_json_is_read_even_with_fences_or_extra_words()
  121. {
  122. $this->assertSame(['a' => 1], JsonAnswer::decode("Here you go:\n```json\n{\"a\": 1}\n```"));
  123. $this->expectException(AiException::class);
  124. JsonAnswer::decode('Sorry, I cannot do that.');
  125. }
  126. public function test_the_prompt_describes_every_section_type_and_font_from_the_schema()
  127. {
  128. $format = PlanPrompt::format();
  129. foreach (array_keys(PlanSchema::SECTIONS) as $type) {
  130. $this->assertStringContainsString("## $type\n", $format);
  131. }
  132. foreach (array_keys(PlanSchema::FONTS) as $font) {
  133. $this->assertStringContainsString($font, $format);
  134. }
  135. $this->assertStringNotContainsString('regex:', $format);
  136. $this->assertStringNotContainsString('[[FORMAT]]', PlanPrompt::generateSystem().PlanPrompt::editSystem());
  137. }
  138. }