| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Enums\ProductStatus;
- use App\Models\DiscountCode;
- use App\Models\Product;
- use App\Models\ProductVariant;
- use App\Models\Store;
- use App\Models\StoreGeneration;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\ScriptedDriver;
- use Tests\TestCase;
-
- class ChatActionsTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- private Product $cleanser;
-
- private Product $moisturiser;
-
- private ProductVariant $big;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- $this->user = User::factory()->create();
- $this->store = Store::factory()->for($this->user)->create();
- $this->store->plan = SamplePlan::for($this->store);
- $this->store->save();
- $this->actingAs($this->user);
-
- $this->cleanser = $this->store->products()->create(['name' => 'Gel Cleanser', 'price' => 4200, 'stock' => 28, 'status' => 'active']);
-
- $this->post(route('stores.products.store', $this->store->slug), [
- 'name' => 'Moisturiser', 'status' => 'active',
- 'options' => [['name' => 'Size', 'values' => '50ml, 100ml']],
- 'variants' => [
- ['option1' => '50ml', 'price' => '39.00', 'stock' => 8],
- ['option1' => '100ml', 'price' => '55.00', 'stock' => 4],
- ],
- ])->assertSessionHasNoErrors();
-
- $this->moisturiser = $this->store->products()->where('name', 'Moisturiser')->firstOrFail();
- $this->big = $this->moisturiser->variants()->where('option1', '100ml')->firstOrFail();
- }
-
- /**
- * Send one chat message, with the AI answering the given actions.
- *
- * @param list<array<string, mixed>> $actions
- */
- private function ask(array $actions, string $reply = 'Please confirm below.'): StoreGeneration
- {
- ScriptedDriver::use([(string) json_encode(['reply' => $reply, 'actions' => $actions])]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Do it']);
-
- return $this->store->generations()->latest('id')->firstOrFail();
- }
-
- private function confirm(StoreGeneration $edit)
- {
- return $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]));
- }
-
- public function test_nothing_changes_until_the_merchant_confirms()
- {
- $edit = $this->ask([[
- 'type' => 'create_discount', 'code' => 'raya10', 'discount_type' => 'percent', 'value' => 10,
- 'minimum_spend' => 50, 'ends_on' => '2030-03-31',
- ]]);
-
- $this->assertSame(StoreGeneration::ACTIONS_PENDING, $edit->actions_status);
- $this->assertSame(0, DiscountCode::count());
-
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('edits.0.actions_status', 'pending')
- ->where('edits.0.actions.0', 'Create the discount code RAYA10: 10% off, when spending at least RM 50.00, until 2030-03-31'));
-
- $this->confirm($edit)->assertRedirect(route('stores.show', $this->store->slug));
-
- $code = DiscountCode::firstOrFail();
- $this->assertSame('RAYA10', $code->code);
- $this->assertSame(10, $code->value);
- $this->assertSame(5000, $code->minimum_subtotal);
- $this->assertTrue($code->active);
- $this->assertSame(StoreGeneration::ACTIONS_CONFIRMED, $edit->fresh()->actions_status);
-
- // Confirming twice does nothing more.
- $this->confirm($edit)->assertNotFound();
- $this->assertSame(1, DiscountCode::count());
- }
-
- public function test_cancelling_changes_nothing()
- {
- $edit = $this->ask([['type' => 'update_product', 'product' => $this->cleanser->slug, 'price' => 35]]);
-
- $this->post(route('stores.edits.cancel', [$this->store->slug, $edit->id]))->assertRedirect();
-
- $this->assertSame(4200, $this->cleanser->fresh()->price);
- $this->assertSame(StoreGeneration::ACTIONS_CANCELLED, $edit->fresh()->actions_status);
- $this->confirm($edit)->assertNotFound();
- }
-
- public function test_products_can_be_added_and_changed()
- {
- $edit = $this->ask([
- ['type' => 'create_product', 'name' => 'Sheet Mask', 'price' => 8.9, 'stock' => 50, 'status' => 'draft'],
- ['type' => 'update_product', 'product' => $this->moisturiser->slug, 'variant' => '100ML', 'price' => 52, 'compare_at_price' => 55, 'stock' => 10],
- ['type' => 'update_product', 'product' => $this->cleanser->slug, 'status' => 'draft'],
- ]);
-
- $this->assertSame([
- 'Add the product "Sheet Mask" at RM 8.90, 50 in stock, saved as a draft',
- 'Change "Moisturiser (100ml)": price RM 55.00 → RM 52.00, "was" price RM 55.00, stock 4 → 10',
- 'Change "Gel Cleanser": hide it (draft)',
- ], array_column($edit->actions, 'summary'));
-
- $this->confirm($edit)->assertSessionHasNoErrors();
-
- $mask = $this->store->products()->where('name', 'Sheet Mask')->firstOrFail();
- $this->assertSame(890, $mask->price);
- $this->assertSame(ProductStatus::Draft, $mask->status);
-
- $big = $this->big->fresh();
- $this->assertSame(5200, $big->price);
- $this->assertSame(5500, $big->compare_at_price);
- $this->assertSame(10, $big->stock);
- // The product's own stock follows its variants.
- $this->assertSame(18, $this->moisturiser->fresh()->stock);
- $this->assertSame(ProductStatus::Draft, $this->cleanser->fresh()->status);
- }
-
- public function test_a_bundle_can_be_created_from_chat()
- {
- $edit = $this->ask([[
- 'type' => 'create_bundle', 'name' => 'Glow Set', 'price' => 85, 'compare_at_price' => 97,
- 'items' => [
- ['product' => $this->cleanser->slug, 'quantity' => 1],
- ['product' => $this->moisturiser->slug, 'variant' => '100ml', 'quantity' => 2],
- ],
- ]]);
-
- $this->assertSame(
- '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',
- $edit->actions[0]['summary'],
- );
-
- $this->confirm($edit);
-
- $bundle = $this->store->products()->where('is_bundle', true)->firstOrFail();
- $this->assertSame('Glow Set', $bundle->name);
- $this->assertSame(8500, $bundle->price);
- $this->assertSame(2, $bundle->stock);
- $this->assertSame(['1 × Gel Cleanser', '2 × Moisturiser (100ml)'], $bundle->bundleItems->map->label()->all());
- }
-
- public function test_a_wrong_action_goes_back_to_the_ai_once()
- {
- ScriptedDriver::use([
- // The moisturiser has sizes, so a price change must say which one.
- (string) json_encode(['reply' => 'Ok', 'actions' => [['type' => 'update_product', 'product' => $this->moisturiser->slug, 'price' => 30]]]),
- (string) json_encode(['reply' => 'Ok', 'actions' => [['type' => 'update_product', 'product' => $this->moisturiser->slug, 'variant' => '50ml', 'price' => 30]]]),
- ]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Moisturiser RM 30']);
-
- $this->assertStringContainsString('actions.0.variant', ScriptedDriver::$requests[1]->prompt);
- $edit = $this->store->generations()->firstOrFail();
- $this->assertSame('Change "Moisturiser (50ml)": price RM 39.00 → RM 30.00', $edit->actions[0]['summary']);
- }
-
- public function test_actions_that_break_the_rules_are_never_proposed()
- {
- $wrong = [
- [['type' => 'delete_everything']],
- [['type' => 'update_product', 'product' => 'no-such-thing', 'price' => 10]],
- [['type' => 'update_product', 'product' => $this->cleanser->slug, 'price' => 50, 'compare_at_price' => 40]],
- [['type' => 'create_discount', 'code' => 'x', 'discount_type' => 'percent', 'value' => 10]],
- [['type' => 'create_discount', 'code' => 'BIG', 'discount_type' => 'percent', 'value' => 150]],
- [['type' => 'create_bundle', 'name' => 'Set', 'price' => 10, 'items' => [['product' => $this->moisturiser->slug]]]],
- array_fill(0, 6, ['type' => 'update_product', 'product' => $this->cleanser->slug, 'stock' => 1]),
- ];
-
- foreach ($wrong as $actions) {
- $json = (string) json_encode(['reply' => 'Ok', 'actions' => $actions]);
- ScriptedDriver::use([$json, $json]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Do it']);
-
- $this->assertSame('failed', $this->store->generations()->latest('id')->firstOrFail()->status->value, json_encode($actions));
- }
-
- $this->assertSame(0, DiscountCode::count());
- $this->assertSame(4200, $this->cleanser->fresh()->price);
- }
-
- public function test_confirm_checks_again_and_changes_nothing_if_one_fails()
- {
- $edit = $this->ask([
- ['type' => 'update_product', 'product' => $this->cleanser->slug, 'price' => 30],
- ['type' => 'create_discount', 'code' => 'RAYA10', 'discount_type' => 'percent', 'value' => 10],
- ]);
-
- // The merchant made the same code by hand in the meantime.
- $this->post(route('stores.discounts.store', $this->store->slug), [
- 'code' => 'RAYA10', 'type' => 'percent', 'value' => 15, 'active' => true,
- ])->assertSessionHasNoErrors();
-
- $this->confirm($edit)->assertRedirect();
-
- $this->assertSame(4200, $this->cleanser->fresh()->price);
- $this->assertSame(StoreGeneration::ACTIONS_PENDING, $edit->fresh()->actions_status);
- $this->assertSame(15, DiscountCode::firstOrFail()->value);
- }
-
- public function test_a_discount_code_can_be_switched_off()
- {
- $this->post(route('stores.discounts.store', $this->store->slug), [
- 'code' => 'RAYA10', 'type' => 'percent', 'value' => 10, 'active' => true,
- ]);
-
- $edit = $this->ask([['type' => 'update_discount', 'code' => 'raya10', 'active' => false]]);
-
- $this->assertSame('Change the discount code RAYA10: switch it off', $edit->actions[0]['summary']);
- $this->confirm($edit);
-
- $this->assertFalse(DiscountCode::firstOrFail()->active);
- }
-
- public function test_the_ai_sees_the_products_codes_and_date()
- {
- $this->ask([]);
-
- $prompt = ScriptedDriver::$requests[0]->prompt;
-
- $this->assertStringContainsString('Today in Malaysia: ', $prompt);
- $this->assertStringContainsString($this->moisturiser->slug.': Moisturiser, active, choices: "50ml" RM 39.00 stock 8; "100ml" RM 55.00 stock 4', $prompt);
- $this->assertStringContainsString('"create_bundle"', ScriptedDriver::$requests[0]->system);
- }
-
- public function test_only_the_owner_can_confirm()
- {
- $edit = $this->ask([['type' => 'update_product', 'product' => $this->cleanser->slug, 'price' => 30]]);
-
- $this->actingAs(User::factory()->create())->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]))->assertNotFound();
-
- $this->assertSame(4200, $this->cleanser->fresh()->price);
- }
- }
|