AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SectionLayoutTest.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Models\Store;
  4. use App\Models\User;
  5. use App\StorePlan\Ai\PlanPrompt;
  6. use App\StorePlan\PlanSchema;
  7. use App\StorePlan\PlanValidator;
  8. use App\StorePlan\SamplePlan;
  9. use App\StorePlan\SectionLayouts;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Inertia\Testing\AssertableInertia as Assert;
  12. use Tests\Support\ScriptedDriver;
  13. use Tests\TestCase;
  14. class SectionLayoutTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. private User $user;
  18. private Store $store;
  19. protected function setUp(): void
  20. {
  21. parent::setUp();
  22. $this->user = User::factory()->create();
  23. $this->store = Store::factory()->for($this->user)->create();
  24. $this->store->plan = SamplePlan::for($this->store);
  25. $this->store->save();
  26. $this->actingAs($this->user);
  27. }
  28. private function variantOf(string $sectionId): ?string
  29. {
  30. $plan = $this->store->fresh()->plan;
  31. if (in_array($sectionId, ['header', 'footer'], true)) {
  32. return $plan[$sectionId]['variant'];
  33. }
  34. foreach ($plan['pages'][0]['sections'] as $section) {
  35. if ($section['id'] === $sectionId) {
  36. return $section['variant'];
  37. }
  38. }
  39. return null;
  40. }
  41. public function test_every_layout_of_every_section_in_the_sample_passes_the_checker()
  42. {
  43. $validator = app(PlanValidator::class);
  44. $plan = $this->store->plan;
  45. foreach (SectionLayouts::sections($plan) as $section) {
  46. foreach (PlanSchema::SECTIONS[$section['type']] as $variant) {
  47. $this->assertTrue(
  48. $validator->passes(SectionLayouts::update($plan, $section['section_id'], $variant)),
  49. "{$section['type']} / $variant",
  50. );
  51. }
  52. }
  53. // The main sections now have at least 3 layouts each.
  54. foreach (['hero', 'product_grid', 'featured_product', 'about', 'testimonials', 'faq', 'newsletter', 'footer'] as $type) {
  55. $this->assertGreaterThanOrEqual(3, count(PlanSchema::SECTIONS[$type]), $type);
  56. }
  57. }
  58. public function test_the_design_tab_lists_sections_and_changes_a_layout()
  59. {
  60. $this->get(route('stores.show', $this->store->slug))
  61. ->assertInertia(fn (Assert $page) => $page
  62. ->where('layoutSections.0.section_id', 'header')
  63. ->where('layoutSections.1.type', 'trust_bar')
  64. ->where('layoutOptions.product_grid', ['grid', 'minimal', 'carousel', 'large']));
  65. $this->put(route('stores.layouts.update', [$this->store->slug, 'products']), ['variant' => 'carousel'])
  66. ->assertRedirect(route('stores.show', $this->store->slug));
  67. $this->put(route('stores.layouts.update', [$this->store->slug, 'footer']), ['variant' => 'columns'])->assertSessionHasNoErrors();
  68. $this->put(route('stores.layouts.update', [$this->store->slug, 'header']), ['variant' => 'centered'])->assertSessionHasNoErrors();
  69. $this->assertSame('carousel', $this->variantOf('products'));
  70. $this->assertSame('columns', $this->variantOf('footer'));
  71. $this->assertSame('centered', $this->variantOf('header'));
  72. }
  73. public function test_only_real_layouts_and_sections_are_accepted()
  74. {
  75. // A layout of another section type, or a made-up one.
  76. $this->put(route('stores.layouts.update', [$this->store->slug, 'products']), ['variant' => 'stacked'])->assertSessionHasErrors('variant');
  77. $this->put(route('stores.layouts.update', [$this->store->slug, 'products']), ['variant' => 'spiral'])->assertSessionHasErrors('variant');
  78. // Heroes have their own box; unknown sections are not found.
  79. $this->put(route('stores.layouts.update', [$this->store->slug, 'hero']), ['variant' => 'split'])->assertNotFound();
  80. $this->put(route('stores.layouts.update', [$this->store->slug, 'nothing-here']), ['variant' => 'grid'])->assertNotFound();
  81. $this->actingAs(User::factory()->create())
  82. ->put(route('stores.layouts.update', [$this->store->slug, 'products']), ['variant' => 'large'])
  83. ->assertNotFound();
  84. $this->assertSame('grid', $this->variantOf('products'));
  85. }
  86. public function test_the_ai_knows_the_new_layouts_and_can_switch_to_them()
  87. {
  88. ScriptedDriver::use([(string) json_encode([
  89. 'reply' => 'Your products now slide in one row.',
  90. 'update' => [['id' => 'products', 'variant' => 'carousel']],
  91. ])]);
  92. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Make the products slide sideways']);
  93. $this->assertSame('carousel', $this->variantOf('products'));
  94. $this->assertStringContainsString('variants: grid, minimal, carousel, large', ScriptedDriver::$requests[0]->system);
  95. $this->assertStringContainsString('Layouts to know', PlanPrompt::generateSystem());
  96. }
  97. }