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.

StoreGenerationTest.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Ai\AiException;
  4. use App\Enums\GenerationStatus;
  5. use App\Enums\GenerationType;
  6. use App\Jobs\RunStoreGeneration;
  7. use App\Models\Store;
  8. use App\Models\StoreGeneration;
  9. use App\Models\User;
  10. use App\StorePlan\SamplePlan;
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Illuminate\Support\Facades\Queue;
  13. use Inertia\Testing\AssertableInertia as Assert;
  14. use Tests\Support\ScriptedDriver;
  15. use Tests\TestCase;
  16. class StoreGenerationTest extends TestCase
  17. {
  18. use RefreshDatabase;
  19. public function test_a_user_can_generate_a_store_design()
  20. {
  21. $user = User::factory()->create();
  22. $store = Store::factory()->for($user)->create(['name' => 'Sambal Nenek']);
  23. $this->actingAs($user)
  24. ->post(route('stores.generate', $store->slug))
  25. ->assertRedirect(route('stores.show', $store->slug));
  26. $store->refresh();
  27. $generation = $store->generations()->firstOrFail();
  28. $this->assertSame('Sambal Nenek', $store->plan['header']['content']['logoText']);
  29. $this->assertSame(GenerationStatus::Completed, $generation->status);
  30. $this->assertSame(GenerationType::Generate, $generation->type);
  31. $this->assertFalse($generation->is_regeneration);
  32. $this->assertSame('fake', $generation->provider);
  33. $this->assertSame('test-generate-model', $generation->model);
  34. $this->actingAs($user)
  35. ->get(route('stores.show', $store->slug))
  36. ->assertInertia(fn (Assert $page) => $page
  37. ->where('store.has_design', true)
  38. ->where('generation.status', 'completed')
  39. ->where('usage.plan', 'trial')
  40. ->where('usage.regenerations.used', 0)
  41. ->where('usage.regenerations.limit', 2));
  42. }
  43. public function test_generation_runs_in_the_background_and_the_page_shows_it_waiting()
  44. {
  45. Queue::fake();
  46. $user = User::factory()->create();
  47. $store = Store::factory()->for($user)->create();
  48. $this->actingAs($user)->post(route('stores.generate', $store->slug));
  49. Queue::assertPushed(RunStoreGeneration::class, 1);
  50. $this->assertNull($store->fresh()->plan);
  51. $this->actingAs($user)
  52. ->get(route('stores.show', $store->slug))
  53. ->assertInertia(fn (Assert $page) => $page
  54. ->where('store.has_design', false)
  55. ->where('generation.status', 'queued')
  56. ->where('generation.type', 'generate'));
  57. // A second request while the first is still waiting is refused.
  58. $this->actingAs($user)->post(route('stores.generate', $store->slug));
  59. Queue::assertPushed(RunStoreGeneration::class, 1);
  60. $this->assertSame(1, $store->generations()->count());
  61. }
  62. public function test_the_first_design_is_free_and_redesigns_are_limited_by_the_plan()
  63. {
  64. $user = User::factory()->create();
  65. $store = Store::factory()->for($user)->create();
  66. // First design, then the 2 regenerations the free trial allows.
  67. foreach (range(1, 3) as $ignored) {
  68. $this->actingAs($user)->post(route('stores.generate', $store->slug));
  69. }
  70. $this->assertSame(3, $store->generations()->count());
  71. $this->assertSame(2, $store->generations()->where('is_regeneration', true)->count());
  72. $this->actingAs($user)
  73. ->post(route('stores.generate', $store->slug))
  74. ->assertRedirect(route('stores.show', $store->slug));
  75. $this->assertSame(3, $store->generations()->count());
  76. // A bigger plan allows more.
  77. $user->plan = 'starter';
  78. $user->save();
  79. $this->actingAs($user)->post(route('stores.generate', $store->slug));
  80. $this->assertSame(4, $store->generations()->count());
  81. }
  82. public function test_monthly_plans_only_count_this_month()
  83. {
  84. $user = User::factory()->create();
  85. $user->plan = 'starter';
  86. $user->save();
  87. $store = Store::factory()->for($user)->create();
  88. StoreGeneration::factory()->for($store)->regeneration()->count(3)->create(['created_at' => now()->subMonths(2)]);
  89. StoreGeneration::factory()->for($store)->regeneration()->create();
  90. StoreGeneration::factory()->for($store)->regeneration()->status(GenerationStatus::Failed)->create();
  91. StoreGeneration::factory()->for($store)->edit()->count(2)->create();
  92. $this->actingAs($user)
  93. ->get(route('stores.show', $store->slug))
  94. ->assertInertia(fn (Assert $page) => $page
  95. ->where('usage.period', 'month')
  96. ->where('usage.regenerations.used', 1)
  97. ->where('usage.regenerations.limit', 3)
  98. ->where('usage.edits.used', 2)
  99. ->where('usage.edits.limit', 100)
  100. ->has('edits', 2));
  101. }
  102. public function test_a_user_can_edit_the_design_by_chat()
  103. {
  104. $user = User::factory()->create();
  105. $store = $this->storeWithDesign($user);
  106. $this->actingAs($user)
  107. ->post(route('stores.edits', $store->slug), ['instruction' => 'Make the main colour darker'])
  108. ->assertRedirect(route('stores.show', $store->slug));
  109. $store->refresh();
  110. $edit = $store->generations()->firstOrFail();
  111. $this->assertSame('#3B2A20', $store->plan['theme']['colors']['primary']);
  112. $this->assertSame(GenerationType::Edit, $edit->type);
  113. $this->assertSame(GenerationStatus::Completed, $edit->status);
  114. $this->assertSame('Make the main colour darker', $edit->instruction);
  115. $this->assertSame('Fake AI: I made the main colour darker.', $edit->reply);
  116. $this->assertSame('test-edit-model', $edit->model);
  117. }
  118. public function test_edits_need_a_design_an_instruction_and_room_in_the_plan()
  119. {
  120. $user = User::factory()->create();
  121. $empty = Store::factory()->for($user)->create();
  122. $this->actingAs($user)
  123. ->post(route('stores.edits', $empty->slug), ['instruction' => 'Make it blue'])
  124. ->assertNotFound();
  125. $store = $this->storeWithDesign($user);
  126. $this->actingAs($user)
  127. ->post(route('stores.edits', $store->slug), ['instruction' => ''])
  128. ->assertSessionHasErrors('instruction');
  129. StoreGeneration::factory()->for($store)->edit()->count(30)->create();
  130. $this->actingAs($user)->post(route('stores.edits', $store->slug), ['instruction' => 'Make it blue']);
  131. $this->assertSame(30, $store->generations()->count());
  132. }
  133. public function test_a_bad_ai_answer_is_corrected_once()
  134. {
  135. ScriptedDriver::use([
  136. '{"version": 1, "theme": {"colors": {}}, "sections": []}',
  137. (string) json_encode(SamplePlan::forName('Fixed')),
  138. ]);
  139. $user = User::factory()->create();
  140. $store = Store::factory()->for($user)->create();
  141. $this->actingAs($user)->post(route('stores.generate', $store->slug));
  142. $generation = $store->generations()->firstOrFail();
  143. $this->assertSame(GenerationStatus::Completed, $generation->status);
  144. $this->assertSame('Fixed', $store->fresh()->plan['header']['content']['logoText']);
  145. // Tokens from both attempts are added up.
  146. $this->assertSame(200, $generation->input_tokens);
  147. $this->assertSame(400, $generation->output_tokens);
  148. $this->assertCount(2, ScriptedDriver::$requests);
  149. $this->assertStringContainsString('rejected by our checker', ScriptedDriver::$requests[1]->prompt);
  150. }
  151. public function test_an_unusable_ai_answer_never_changes_the_store()
  152. {
  153. ScriptedDriver::use(['I am not JSON.', '{"version": 99}']);
  154. $user = User::factory()->create();
  155. $store = $this->storeWithDesign($user);
  156. $before = $store->plan;
  157. $this->actingAs($user)->post(route('stores.generate', $store->slug));
  158. $generation = $store->generations()->firstOrFail();
  159. $this->assertSame($before, $store->fresh()->plan);
  160. $this->assertSame(GenerationStatus::Failed, $generation->status);
  161. $this->assertSame('The AI returned a design we could not use. Please try again.', $generation->error);
  162. // A failed attempt does not use up the plan.
  163. $this->actingAs($user)
  164. ->get(route('stores.show', $store->slug))
  165. ->assertInertia(fn (Assert $page) => $page
  166. ->where('generation.status', 'failed')
  167. ->where('usage.regenerations.used', 0));
  168. }
  169. public function test_an_edit_that_breaks_the_plan_is_rejected()
  170. {
  171. ScriptedDriver::use([
  172. '{"reply": "Done", "theme": {"colors": {"primary": "javascript:alert(1)"}}}',
  173. '{"reply": "Done", "update": [{"id": "hero", "content": {"primaryCta": {"label": "Go", "href": "javascript:alert(1)"}}}]}',
  174. ]);
  175. $user = User::factory()->create();
  176. $store = $this->storeWithDesign($user);
  177. $before = $store->plan;
  178. $this->actingAs($user)->post(route('stores.edits', $store->slug), ['instruction' => 'Do something bad']);
  179. $this->assertSame($before, $store->fresh()->plan);
  180. $this->assertSame(GenerationStatus::Failed, $store->generations()->firstOrFail()->status);
  181. }
  182. public function test_a_chat_edit_can_add_a_page_and_link_it_in_the_menu()
  183. {
  184. ScriptedDriver::use([(string) json_encode([
  185. 'reply' => 'I added an About page and linked it in the menu.',
  186. 'add_pages' => [[
  187. 'id' => 'about',
  188. 'title' => 'About us',
  189. 'sections' => [[
  190. 'id' => 'about-story',
  191. 'type' => 'about',
  192. 'variant' => 'simple',
  193. 'content' => ['heading' => 'Our story', 'body' => 'We started in a home kitchen.', 'stats' => []],
  194. ]],
  195. ]],
  196. 'header' => ['content' => ['links' => [['label' => 'About', 'href' => '/about']]]],
  197. ])]);
  198. $user = User::factory()->create();
  199. $store = $this->storeWithDesign($user);
  200. $this->actingAs($user)->post(route('stores.edits', $store->slug), ['instruction' => 'Add an About page']);
  201. $plan = $store->fresh()->plan;
  202. $this->assertSame(GenerationStatus::Completed, $store->generations()->firstOrFail()->status);
  203. $this->assertSame(['home', 'about'], array_column($plan['pages'], 'id'));
  204. $this->assertSame('/about', $plan['header']['content']['links'][0]['href']);
  205. $this->actingAs($user)->get(route('stores.preview.page', [$store->slug, 'about']))->assertOk();
  206. }
  207. public function test_a_menu_link_to_a_page_that_was_not_created_is_rejected()
  208. {
  209. $broken = '{"reply": "Done", "header": {"content": {"links": [{"label": "About", "href": "/about"}]}}}';
  210. ScriptedDriver::use([$broken, $broken]);
  211. $user = User::factory()->create();
  212. $store = $this->storeWithDesign($user);
  213. $before = $store->plan;
  214. $this->actingAs($user)->post(route('stores.edits', $store->slug), ['instruction' => 'Add an About page']);
  215. $this->assertSame($before, $store->fresh()->plan);
  216. $this->assertSame(GenerationStatus::Failed, $store->generations()->firstOrFail()->status);
  217. // The AI was told exactly what was wrong on its second try.
  218. $this->assertStringContainsString('points to a page that does not exist', ScriptedDriver::$requests[1]->prompt);
  219. }
  220. public function test_a_provider_failure_is_shown_as_a_friendly_message()
  221. {
  222. ScriptedDriver::use([new AiException('HTTP 529 overloaded, secret detail')]);
  223. $user = User::factory()->create();
  224. $store = Store::factory()->for($user)->create();
  225. $this->actingAs($user)->post(route('stores.generate', $store->slug));
  226. $generation = $store->generations()->firstOrFail();
  227. $this->assertSame(GenerationStatus::Failed, $generation->status);
  228. $this->assertSame('The AI service had a problem. Please try again in a moment.', $generation->error);
  229. $this->assertNull($store->fresh()->plan);
  230. }
  231. public function test_a_job_that_hangs_is_marked_failed_so_the_store_is_not_stuck()
  232. {
  233. $user = User::factory()->create();
  234. $store = Store::factory()->for($user)->create();
  235. StoreGeneration::factory()->for($store)->status(GenerationStatus::Running)->create(['created_at' => now()->subMinutes(11)]);
  236. $this->actingAs($user)
  237. ->get(route('stores.show', $store->slug))
  238. ->assertInertia(fn (Assert $page) => $page->where('generation.status', 'failed'));
  239. $this->actingAs($user)->post(route('stores.generate', $store->slug));
  240. $this->assertNotNull($store->fresh()->plan);
  241. }
  242. public function test_a_user_cannot_use_ai_on_someone_elses_store()
  243. {
  244. $user = User::factory()->create();
  245. $store = $this->storeWithDesign(User::factory()->create());
  246. $this->post(route('stores.generate', $store->slug))->assertRedirect(route('login'));
  247. $this->actingAs($user)->post(route('stores.generate', $store->slug))->assertNotFound();
  248. $this->actingAs($user)->post(route('stores.edits', $store->slug), ['instruction' => 'Make it blue'])->assertNotFound();
  249. $this->assertSame(0, $store->generations()->count());
  250. }
  251. private function storeWithDesign(User $user): Store
  252. {
  253. $store = Store::factory()->for($user)->create();
  254. $store->plan = SamplePlan::for($store);
  255. $store->save();
  256. return $store->refresh();
  257. }
  258. }