| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\Ai\PlanPrompt;
- use App\StorePlan\PlanSchema;
- use App\StorePlan\PlanValidator;
- use App\StorePlan\SamplePlan;
- use App\StorePlan\SectionLayouts;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\ScriptedDriver;
- use Tests\TestCase;
-
- class SectionLayoutTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- 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);
- }
-
- private function variantOf(string $sectionId): ?string
- {
- $plan = $this->store->fresh()->plan;
-
- if (in_array($sectionId, ['header', 'footer'], true)) {
- return $plan[$sectionId]['variant'];
- }
-
- foreach ($plan['pages'][0]['sections'] as $section) {
- if ($section['id'] === $sectionId) {
- return $section['variant'];
- }
- }
-
- return null;
- }
-
- public function test_every_layout_of_every_section_in_the_sample_passes_the_checker()
- {
- $validator = app(PlanValidator::class);
- $plan = $this->store->plan;
-
- foreach (SectionLayouts::sections($plan) as $section) {
- foreach (PlanSchema::SECTIONS[$section['type']] as $variant) {
- $this->assertTrue(
- $validator->passes(SectionLayouts::update($plan, $section['section_id'], $variant)),
- "{$section['type']} / $variant",
- );
- }
- }
-
- // The main sections now have at least 3 layouts each.
- foreach (['hero', 'product_grid', 'featured_product', 'about', 'testimonials', 'faq', 'newsletter', 'footer'] as $type) {
- $this->assertGreaterThanOrEqual(3, count(PlanSchema::SECTIONS[$type]), $type);
- }
- }
-
- public function test_the_design_tab_lists_sections_and_changes_a_layout()
- {
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('layoutSections.0.section_id', 'header')
- ->where('layoutSections.1.type', 'trust_bar')
- ->where('layoutOptions.product_grid', ['grid', 'minimal', 'carousel', 'large']));
-
- $this->put(route('stores.layouts.update', [$this->store->slug, 'products']), ['variant' => 'carousel'])
- ->assertRedirect(route('stores.show', $this->store->slug));
- $this->put(route('stores.layouts.update', [$this->store->slug, 'footer']), ['variant' => 'columns'])->assertSessionHasNoErrors();
- $this->put(route('stores.layouts.update', [$this->store->slug, 'header']), ['variant' => 'centered'])->assertSessionHasNoErrors();
-
- $this->assertSame('carousel', $this->variantOf('products'));
- $this->assertSame('columns', $this->variantOf('footer'));
- $this->assertSame('centered', $this->variantOf('header'));
- }
-
- public function test_only_real_layouts_and_sections_are_accepted()
- {
- // A layout of another section type, or a made-up one.
- $this->put(route('stores.layouts.update', [$this->store->slug, 'products']), ['variant' => 'stacked'])->assertSessionHasErrors('variant');
- $this->put(route('stores.layouts.update', [$this->store->slug, 'products']), ['variant' => 'spiral'])->assertSessionHasErrors('variant');
- // Heroes have their own box; unknown sections are not found.
- $this->put(route('stores.layouts.update', [$this->store->slug, 'hero']), ['variant' => 'split'])->assertNotFound();
- $this->put(route('stores.layouts.update', [$this->store->slug, 'nothing-here']), ['variant' => 'grid'])->assertNotFound();
-
- $this->actingAs(User::factory()->create())
- ->put(route('stores.layouts.update', [$this->store->slug, 'products']), ['variant' => 'large'])
- ->assertNotFound();
-
- $this->assertSame('grid', $this->variantOf('products'));
- }
-
- public function test_the_ai_knows_the_new_layouts_and_can_switch_to_them()
- {
- ScriptedDriver::use([(string) json_encode([
- 'reply' => 'Your products now slide in one row.',
- 'update' => [['id' => 'products', 'variant' => 'carousel']],
- ])]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Make the products slide sideways']);
-
- $this->assertSame('carousel', $this->variantOf('products'));
- $this->assertStringContainsString('variants: grid, minimal, carousel, large', ScriptedDriver::$requests[0]->system);
- $this->assertStringContainsString('Layouts to know', PlanPrompt::generateSystem());
- }
- }
|