AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ChatActionsTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Enums\ProductStatus;
  4. use App\Models\DiscountCode;
  5. use App\Models\Product;
  6. use App\Models\ProductVariant;
  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 Inertia\Testing\AssertableInertia as Assert;
  13. use Tests\Support\ScriptedDriver;
  14. use Tests\TestCase;
  15. class ChatActionsTest extends TestCase
  16. {
  17. use RefreshDatabase;
  18. private User $user;
  19. private Store $store;
  20. private Product $cleanser;
  21. private Product $moisturiser;
  22. private ProductVariant $big;
  23. protected function setUp(): void
  24. {
  25. parent::setUp();
  26. $this->user = User::factory()->create();
  27. $this->store = Store::factory()->for($this->user)->create();
  28. $this->store->plan = SamplePlan::for($this->store);
  29. $this->store->save();
  30. $this->actingAs($this->user);
  31. $this->cleanser = $this->store->products()->create(['name' => 'Gel Cleanser', 'price' => 4200, 'stock' => 28, 'status' => 'active']);
  32. $this->post(route('stores.products.store', $this->store->slug), [
  33. 'name' => 'Moisturiser', 'status' => 'active',
  34. 'options' => [['name' => 'Size', 'values' => '50ml, 100ml']],
  35. 'variants' => [
  36. ['option1' => '50ml', 'price' => '39.00', 'stock' => 8],
  37. ['option1' => '100ml', 'price' => '55.00', 'stock' => 4],
  38. ],
  39. ])->assertSessionHasNoErrors();
  40. $this->moisturiser = $this->store->products()->where('name', 'Moisturiser')->firstOrFail();
  41. $this->big = $this->moisturiser->variants()->where('option1', '100ml')->firstOrFail();
  42. }
  43. /**
  44. * Send one chat message, with the AI answering the given actions.
  45. *
  46. * @param list<array<string, mixed>> $actions
  47. */
  48. private function ask(array $actions, string $reply = 'Please confirm below.'): StoreGeneration
  49. {
  50. ScriptedDriver::use([(string) json_encode(['reply' => $reply, 'actions' => $actions])]);
  51. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Do it']);
  52. return $this->store->generations()->latest('id')->firstOrFail();
  53. }
  54. private function confirm(StoreGeneration $edit)
  55. {
  56. return $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]));
  57. }
  58. public function test_nothing_changes_until_the_merchant_confirms()
  59. {
  60. $edit = $this->ask([[
  61. 'type' => 'create_discount', 'code' => 'raya10', 'discount_type' => 'percent', 'value' => 10,
  62. 'minimum_spend' => 50, 'ends_on' => '2030-03-31',
  63. ]]);
  64. $this->assertSame(StoreGeneration::ACTIONS_PENDING, $edit->actions_status);
  65. $this->assertSame(0, DiscountCode::count());
  66. $this->get(route('stores.show', $this->store->slug))
  67. ->assertInertia(fn (Assert $page) => $page
  68. ->where('edits.0.actions_status', 'pending')
  69. ->where('edits.0.actions.0', 'Create the discount code RAYA10: 10% off, when spending at least RM 50.00, until 2030-03-31'));
  70. $this->confirm($edit)->assertRedirect(route('stores.show', $this->store->slug));
  71. $code = DiscountCode::firstOrFail();
  72. $this->assertSame('RAYA10', $code->code);
  73. $this->assertSame(10, $code->value);
  74. $this->assertSame(5000, $code->minimum_subtotal);
  75. $this->assertTrue($code->active);
  76. $this->assertSame(StoreGeneration::ACTIONS_CONFIRMED, $edit->fresh()->actions_status);
  77. // Confirming twice does nothing more.
  78. $this->confirm($edit)->assertNotFound();
  79. $this->assertSame(1, DiscountCode::count());
  80. }
  81. public function test_cancelling_changes_nothing()
  82. {
  83. $edit = $this->ask([['type' => 'update_product', 'product' => $this->cleanser->slug, 'price' => 35]]);
  84. $this->post(route('stores.edits.cancel', [$this->store->slug, $edit->id]))->assertRedirect();
  85. $this->assertSame(4200, $this->cleanser->fresh()->price);
  86. $this->assertSame(StoreGeneration::ACTIONS_CANCELLED, $edit->fresh()->actions_status);
  87. $this->confirm($edit)->assertNotFound();
  88. }
  89. public function test_products_can_be_added_and_changed()
  90. {
  91. $edit = $this->ask([
  92. ['type' => 'create_product', 'name' => 'Sheet Mask', 'price' => 8.9, 'stock' => 50, 'status' => 'draft'],
  93. ['type' => 'update_product', 'product' => $this->moisturiser->slug, 'variant' => '100ML', 'price' => 52, 'compare_at_price' => 55, 'stock' => 10],
  94. ['type' => 'update_product', 'product' => $this->cleanser->slug, 'status' => 'draft'],
  95. ]);
  96. $this->assertSame([
  97. 'Add the product "Sheet Mask" at RM 8.90, 50 in stock, saved as a draft',
  98. 'Change "Moisturiser (100ml)": price RM 55.00 → RM 52.00, "was" price RM 55.00, stock 4 → 10',
  99. 'Change "Gel Cleanser": hide it (draft)',
  100. ], array_column($edit->actions, 'summary'));
  101. $this->confirm($edit)->assertSessionHasNoErrors();
  102. $mask = $this->store->products()->where('name', 'Sheet Mask')->firstOrFail();
  103. $this->assertSame(890, $mask->price);
  104. $this->assertSame(ProductStatus::Draft, $mask->status);
  105. $big = $this->big->fresh();
  106. $this->assertSame(5200, $big->price);
  107. $this->assertSame(5500, $big->compare_at_price);
  108. $this->assertSame(10, $big->stock);
  109. // The product's own stock follows its variants.
  110. $this->assertSame(18, $this->moisturiser->fresh()->stock);
  111. $this->assertSame(ProductStatus::Draft, $this->cleanser->fresh()->status);
  112. }
  113. public function test_a_bundle_can_be_created_from_chat()
  114. {
  115. $edit = $this->ask([[
  116. 'type' => 'create_bundle', 'name' => 'Glow Set', 'price' => 85, 'compare_at_price' => 97,
  117. 'items' => [
  118. ['product' => $this->cleanser->slug, 'quantity' => 1],
  119. ['product' => $this->moisturiser->slug, 'variant' => '100ml', 'quantity' => 2],
  120. ],
  121. ]]);
  122. $this->assertSame(
  123. 'Create the bundle "Glow Set" at RM 85.00 (was RM 97.00): 1 × Gel Cleanser, 2 × Moisturiser (100ml). Enough stock for 2 now, shown in the store',
  124. $edit->actions[0]['summary'],
  125. );
  126. $this->confirm($edit);
  127. $bundle = $this->store->products()->where('is_bundle', true)->firstOrFail();
  128. $this->assertSame('Glow Set', $bundle->name);
  129. $this->assertSame(8500, $bundle->price);
  130. $this->assertSame(2, $bundle->stock);
  131. $this->assertSame(['1 × Gel Cleanser', '2 × Moisturiser (100ml)'], $bundle->bundleItems->map->label()->all());
  132. }
  133. public function test_a_wrong_action_goes_back_to_the_ai_once()
  134. {
  135. ScriptedDriver::use([
  136. // The moisturiser has sizes, so a price change must say which one.
  137. (string) json_encode(['reply' => 'Ok', 'actions' => [['type' => 'update_product', 'product' => $this->moisturiser->slug, 'price' => 30]]]),
  138. (string) json_encode(['reply' => 'Ok', 'actions' => [['type' => 'update_product', 'product' => $this->moisturiser->slug, 'variant' => '50ml', 'price' => 30]]]),
  139. ]);
  140. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Moisturiser RM 30']);
  141. $this->assertStringContainsString('actions.0.variant', ScriptedDriver::$requests[1]->prompt);
  142. $edit = $this->store->generations()->firstOrFail();
  143. $this->assertSame('Change "Moisturiser (50ml)": price RM 39.00 → RM 30.00', $edit->actions[0]['summary']);
  144. }
  145. public function test_actions_that_break_the_rules_are_never_proposed()
  146. {
  147. $wrong = [
  148. [['type' => 'delete_everything']],
  149. [['type' => 'update_product', 'product' => 'no-such-thing', 'price' => 10]],
  150. [['type' => 'update_product', 'product' => $this->cleanser->slug, 'price' => 50, 'compare_at_price' => 40]],
  151. [['type' => 'create_discount', 'code' => 'x', 'discount_type' => 'percent', 'value' => 10]],
  152. [['type' => 'create_discount', 'code' => 'BIG', 'discount_type' => 'percent', 'value' => 150]],
  153. [['type' => 'create_bundle', 'name' => 'Set', 'price' => 10, 'items' => [['product' => $this->moisturiser->slug]]]],
  154. array_fill(0, 6, ['type' => 'update_product', 'product' => $this->cleanser->slug, 'stock' => 1]),
  155. ];
  156. foreach ($wrong as $actions) {
  157. $json = (string) json_encode(['reply' => 'Ok', 'actions' => $actions]);
  158. ScriptedDriver::use([$json, $json]);
  159. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Do it']);
  160. $this->assertSame('failed', $this->store->generations()->latest('id')->firstOrFail()->status->value, json_encode($actions));
  161. }
  162. $this->assertSame(0, DiscountCode::count());
  163. $this->assertSame(4200, $this->cleanser->fresh()->price);
  164. }
  165. public function test_confirm_checks_again_and_changes_nothing_if_one_fails()
  166. {
  167. $edit = $this->ask([
  168. ['type' => 'update_product', 'product' => $this->cleanser->slug, 'price' => 30],
  169. ['type' => 'create_discount', 'code' => 'RAYA10', 'discount_type' => 'percent', 'value' => 10],
  170. ]);
  171. // The merchant made the same code by hand in the meantime.
  172. $this->post(route('stores.discounts.store', $this->store->slug), [
  173. 'code' => 'RAYA10', 'type' => 'percent', 'value' => 15, 'active' => true,
  174. ])->assertSessionHasNoErrors();
  175. $this->confirm($edit)->assertRedirect();
  176. $this->assertSame(4200, $this->cleanser->fresh()->price);
  177. $this->assertSame(StoreGeneration::ACTIONS_PENDING, $edit->fresh()->actions_status);
  178. $this->assertSame(15, DiscountCode::firstOrFail()->value);
  179. }
  180. public function test_a_discount_code_can_be_switched_off()
  181. {
  182. $this->post(route('stores.discounts.store', $this->store->slug), [
  183. 'code' => 'RAYA10', 'type' => 'percent', 'value' => 10, 'active' => true,
  184. ]);
  185. $edit = $this->ask([['type' => 'update_discount', 'code' => 'raya10', 'active' => false]]);
  186. $this->assertSame('Change the discount code RAYA10: switch it off', $edit->actions[0]['summary']);
  187. $this->confirm($edit);
  188. $this->assertFalse(DiscountCode::firstOrFail()->active);
  189. }
  190. public function test_the_ai_sees_the_products_codes_and_date()
  191. {
  192. $this->ask([]);
  193. $prompt = ScriptedDriver::$requests[0]->prompt;
  194. $this->assertStringContainsString('Today in Malaysia: ', $prompt);
  195. $this->assertStringContainsString($this->moisturiser->slug.': Moisturiser, active, choices: "50ml" RM 39.00 stock 8; "100ml" RM 55.00 stock 4', $prompt);
  196. $this->assertStringContainsString('"create_bundle"', ScriptedDriver::$requests[0]->system);
  197. }
  198. public function test_only_the_owner_can_confirm()
  199. {
  200. $edit = $this->ask([['type' => 'update_product', 'product' => $this->cleanser->slug, 'price' => 30]]);
  201. $this->actingAs(User::factory()->create())->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]))->assertNotFound();
  202. $this->assertSame(4200, $this->cleanser->fresh()->price);
  203. }
  204. }