AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CategoryGuideTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Tests\Feature\Ai;
  3. use App\Enums\StoreCategory;
  4. use App\Models\Store;
  5. use App\Models\User;
  6. use App\StorePlan\Ai\CategoryGuides;
  7. use App\StorePlan\Ai\PlanPrompt;
  8. use App\StorePlan\PlanSchema;
  9. use App\StorePlan\SamplePlan;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Tests\Support\ScriptedDriver;
  12. use Tests\TestCase;
  13. class CategoryGuideTest extends TestCase
  14. {
  15. use RefreshDatabase;
  16. public function test_every_category_except_other_has_a_guide()
  17. {
  18. foreach (StoreCategory::cases() as $category) {
  19. $guide = CategoryGuides::for($category);
  20. if ($category === StoreCategory::Other) {
  21. $this->assertNull($guide);
  22. } else {
  23. $this->assertNotNull($guide, $category->value);
  24. $this->assertStringContainsString('Possible directions', $guide);
  25. }
  26. }
  27. }
  28. public function test_guides_only_name_fonts_and_layouts_the_store_really_has()
  29. {
  30. $variants = array_merge(...array_values(PlanSchema::SECTIONS));
  31. foreach (StoreCategory::cases() as $category) {
  32. $guide = (string) CategoryGuides::for($category);
  33. // Font lists are written in brackets: (cormorant-garamond, playfair-display).
  34. preg_match_all('/\(([a-z0-9-]+(?:, [a-z0-9-]+)*(?:,? (?:or|and) [a-z0-9-]+)?)(?: body)?\)/', $guide, $lists);
  35. foreach ($lists[1] as $list) {
  36. foreach (preg_split('/,? (?:or|and) |, /', $list) as $font) {
  37. $this->assertArrayHasKey($font, PlanSchema::FONTS, "{$category->value}: unknown font [$font]");
  38. }
  39. }
  40. // Layouts are written in quotes: "carousel".
  41. preg_match_all('/"([a-z-]+)"/', $guide, $quoted);
  42. foreach ($quoted[1] as $layout) {
  43. $this->assertContains($layout, $variants, "{$category->value}: unknown layout [$layout]");
  44. }
  45. }
  46. }
  47. public function test_the_ai_gets_the_guide_for_the_store_category()
  48. {
  49. ScriptedDriver::use([(string) json_encode(SamplePlan::forName('Kedai Ali'))]);
  50. $user = User::factory()->create();
  51. $store = Store::factory()->for($user)->create(['category' => StoreCategory::Food]);
  52. $this->actingAs($user)->post(route('stores.generate', $store->slug));
  53. $prompt = ScriptedDriver::$requests[0]->prompt;
  54. $this->assertStringContainsString('Design guide for Food & beverage stores', $prompt);
  55. $this->assertStringContainsString('Home kitchen', $prompt);
  56. $this->assertStringNotContainsString('Make this design clearly different', $prompt);
  57. }
  58. public function test_other_stores_get_no_guide()
  59. {
  60. $store = Store::factory()->create(['category' => StoreCategory::Other]);
  61. $this->assertSame('', PlanPrompt::categoryGuide($store));
  62. $this->assertStringNotContainsString('Design guide', PlanPrompt::generatePrompt($store));
  63. }
  64. public function test_the_ai_is_told_not_to_repeat_the_merchants_other_stores_or_the_current_design()
  65. {
  66. $user = User::factory()->create();
  67. $first = Store::factory()->for($user)->create(['name' => 'Sambal Nenek']);
  68. $first->plan = SamplePlan::for($first);
  69. $first->save();
  70. // Someone else's store is never mentioned.
  71. $stranger = Store::factory()->create(['name' => 'Not Yours']);
  72. $stranger->plan = SamplePlan::for($stranger);
  73. $stranger->save();
  74. $second = Store::factory()->for($user)->create(['name' => 'Kopi Pagi']);
  75. $prompt = PlanPrompt::generatePrompt($second);
  76. $this->assertStringContainsString('Make this design clearly different', $prompt);
  77. $this->assertStringContainsString('The merchant\'s store "Sambal Nenek": background #FBF7F0', $prompt);
  78. $this->assertStringContainsString('fonts fraunces / dm-sans', $prompt);
  79. $this->assertStringContainsString('home sections: hero split, trust_bar row', $prompt);
  80. $this->assertStringNotContainsString('Not Yours', $prompt);
  81. // A redesign also avoids the store's own current look.
  82. $second->plan = SamplePlan::for($second);
  83. $second->save();
  84. $this->assertStringContainsString("This store's current design, which the merchant wants replaced", PlanPrompt::generatePrompt($second));
  85. }
  86. }