AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PlanChangesTest.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_an_empty_answer_changes_nothing()
  63. {
  64. $plan = SamplePlan::forName('Kedai');
  65. $this->assertSame($plan, PlanChanges::apply($plan, ['reply' => 'Nothing to do.']));
  66. }
  67. public function test_json_is_read_even_with_fences_or_extra_words()
  68. {
  69. $this->assertSame(['a' => 1], JsonAnswer::decode("Here you go:\n```json\n{\"a\": 1}\n```"));
  70. $this->expectException(AiException::class);
  71. JsonAnswer::decode('Sorry, I cannot do that.');
  72. }
  73. public function test_the_prompt_describes_every_section_type_and_font_from_the_schema()
  74. {
  75. $format = PlanPrompt::format();
  76. foreach (array_keys(PlanSchema::SECTIONS) as $type) {
  77. $this->assertStringContainsString("## $type\n", $format);
  78. }
  79. foreach (array_keys(PlanSchema::FONTS) as $font) {
  80. $this->assertStringContainsString($font, $format);
  81. }
  82. $this->assertStringNotContainsString('regex:', $format);
  83. $this->assertStringNotContainsString('[[FORMAT]]', PlanPrompt::generateSystem().PlanPrompt::editSystem());
  84. }
  85. }