AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SelectToEditTest.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Models\Store;
  4. use App\Models\StoreGeneration;
  5. use App\Models\User;
  6. use App\StorePlan\SamplePlan;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Inertia\Testing\AssertableInertia as Assert;
  9. use Tests\Support\ScriptedDriver;
  10. use Tests\TestCase;
  11. /**
  12. * The merchant selects a part of the store in the preview, then asks the AI to change "this".
  13. */
  14. class SelectToEditTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. private Store $store;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $user = User::factory()->create();
  22. $this->store = Store::factory()->for($user)->create();
  23. $this->store->plan = SamplePlan::for($this->store);
  24. $this->store->save();
  25. $this->actingAs($user);
  26. }
  27. /**
  28. * @param array<string, mixed> $answer
  29. * @param array<string, string> $target
  30. */
  31. private function ask(array $answer, array $target, string $instruction = 'Make this shorter'): StoreGeneration
  32. {
  33. ScriptedDriver::use([(string) json_encode($answer)]);
  34. $this->post(route('stores.edits', $this->store->slug), ['instruction' => $instruction, ...$target])
  35. ->assertSessionHasNoErrors();
  36. return $this->store->generations()->latest('id')->firstOrFail();
  37. }
  38. /**
  39. * @return array<string, mixed>
  40. */
  41. private function section(string $id): array
  42. {
  43. foreach ($this->store->fresh()->plan['pages'][0]['sections'] as $section) {
  44. if ($section['id'] === $id) {
  45. return $section;
  46. }
  47. }
  48. $this->fail("No section {$id}.");
  49. }
  50. public function test_only_the_selected_section_changes_whatever_the_ai_sends()
  51. {
  52. $before = $this->store->plan;
  53. $edit = $this->ask([
  54. 'reply' => 'Shorter now.',
  55. 'update' => [
  56. ['id' => 'hero', 'content' => ['headline' => 'Short and sweet']],
  57. ['id' => 'faq', 'content' => ['heading' => 'Not asked for']],
  58. ],
  59. 'theme' => ['colors' => ['primary' => '#111111']],
  60. 'remove' => ['newsletter'],
  61. ], [
  62. 'target_section' => 'hero',
  63. 'target_label' => 'Hero',
  64. 'target_detail' => 'Heading "Welcome to our store"',
  65. ]);
  66. $this->assertSame('completed', $edit->status->value);
  67. $this->assertTrue($edit->changed);
  68. $this->assertSame(['section' => 'hero', 'label' => 'Hero', 'detail' => 'Heading "Welcome to our store"'], $edit->target);
  69. $this->assertSame('Short and sweet', $this->section('hero')['content']['headline']);
  70. $this->assertEquals($before['theme'], $this->store->fresh()->plan['theme']);
  71. $this->assertEquals($before['pages'][0]['sections'][8], $this->section('faq'));
  72. $this->section('newsletter');
  73. // The AI was told what was selected.
  74. $prompt = ScriptedDriver::$requests[0]->prompt;
  75. $this->assertStringContainsString('section id: "hero" (type hero, on page "home")', $prompt);
  76. $this->assertStringContainsString('Inside it they selected: Heading "Welcome to our store"', $prompt);
  77. }
  78. public function test_a_section_can_be_removed_or_followed_by_a_new_one()
  79. {
  80. $this->ask([
  81. 'reply' => 'Added one below.',
  82. 'add' => [
  83. ['page' => 'home', 'after' => 'about', 'section' => ['id' => 'more', 'type' => 'about', 'variant' => 'simple', 'content' => ['heading' => 'More', 'body' => 'More about us.', 'stats' => []]]],
  84. ['page' => 'home', 'after' => 'hero', 'section' => ['id' => 'elsewhere', 'type' => 'about', 'variant' => 'simple', 'content' => ['heading' => 'No', 'body' => 'Not here.', 'stats' => []]]],
  85. ],
  86. ], ['target_section' => 'about', 'target_label' => 'About'], 'Add a section below this');
  87. $ids = array_column($this->store->fresh()->plan['pages'][0]['sections'], 'id');
  88. $this->assertSame('more', $ids[array_search('about', $ids, true) + 1]);
  89. $this->assertNotContains('elsewhere', $ids);
  90. $this->ask(['reply' => 'Removed.', 'remove' => ['about', 'faq']], ['target_section' => 'about', 'target_label' => 'About'], 'Remove this');
  91. $ids = array_column($this->store->fresh()->plan['pages'][0]['sections'], 'id');
  92. $this->assertNotContains('about', $ids);
  93. $this->assertContains('faq', $ids);
  94. }
  95. public function test_the_header_can_be_selected()
  96. {
  97. $theme = $this->store->plan['theme'];
  98. $this->ask([
  99. 'reply' => 'Renamed.',
  100. 'header' => ['content' => ['logoText' => 'New Name']],
  101. 'footer' => ['content' => ['text' => 'Not asked for']],
  102. 'theme' => ['radius' => 'none'],
  103. ], ['target_section' => 'header', 'target_label' => 'Header'], 'Change this name');
  104. $plan = $this->store->fresh()->plan;
  105. $this->assertSame('New Name', $plan['header']['content']['logoText']);
  106. $this->assertNotSame('Not asked for', $plan['footer']['content']['text'] ?? null);
  107. $this->assertEquals($theme, $plan['theme']);
  108. }
  109. public function test_a_whole_store_change_with_a_part_selected_changes_nothing()
  110. {
  111. $edit = $this->ask(
  112. ['reply' => 'That would change the whole store.', 'theme' => ['colors' => ['primary' => '#111111']]],
  113. ['target_section' => 'products', 'target_label' => 'Products'],
  114. 'Make the colours dark',
  115. );
  116. $this->assertFalse($edit->changed);
  117. $this->get(route('stores.show', $this->store->slug))
  118. ->assertInertia(fn (Assert $page) => $page
  119. ->where('edits.0.target.label', 'Products')
  120. ->where('edits.0.changed', false));
  121. }
  122. public function test_a_section_that_no_longer_exists_is_refused()
  123. {
  124. ScriptedDriver::use([]);
  125. $this->post(route('stores.edits', $this->store->slug), [
  126. 'instruction' => 'Change this',
  127. 'target_section' => 'gone',
  128. 'target_label' => 'FAQ',
  129. ])->assertSessionHasErrors('instruction');
  130. $this->assertSame(0, $this->store->generations()->count());
  131. }
  132. }