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 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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['sections'])->firstWhere('id', 'hero');
  21. $oldHero = collect($plan['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['sections'], 'id');
  34. $faq = collect($plan['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['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])['sections'], 'id'));
  45. // An order that leaves a section out is ignored.
  46. $this->assertSame($newIds, array_column(PlanChanges::apply($changed, ['order' => ['hero']])['sections'], 'id'));
  47. }
  48. public function test_an_empty_answer_changes_nothing()
  49. {
  50. $plan = SamplePlan::forName('Kedai');
  51. $this->assertSame($plan, PlanChanges::apply($plan, ['reply' => 'Nothing to do.']));
  52. }
  53. public function test_json_is_read_even_with_fences_or_extra_words()
  54. {
  55. $this->assertSame(['a' => 1], JsonAnswer::decode("Here you go:\n```json\n{\"a\": 1}\n```"));
  56. $this->expectException(AiException::class);
  57. JsonAnswer::decode('Sorry, I cannot do that.');
  58. }
  59. public function test_the_prompt_describes_every_section_type_and_font_from_the_schema()
  60. {
  61. $format = PlanPrompt::format();
  62. foreach (array_keys(PlanSchema::SECTIONS) as $type) {
  63. $this->assertStringContainsString("## $type\n", $format);
  64. }
  65. foreach (array_keys(PlanSchema::FONTS) as $font) {
  66. $this->assertStringContainsString($font, $format);
  67. }
  68. $this->assertStringNotContainsString('regex:', $format);
  69. $this->assertStringNotContainsString('[[FORMAT]]', PlanPrompt::generateSystem().PlanPrompt::editSystem());
  70. }
  71. }