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

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