| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\PlanSchema;
- use App\StorePlan\PlanValidator;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\ScriptedDriver;
- use Tests\TestCase;
-
- class StoreStyleTest 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);
- }
-
- public function test_a_ready_made_look_sets_style_fonts_and_corners_but_keeps_colours()
- {
- $colours = $this->store->plan['theme']['colors'];
-
- $this->put(route('stores.style.update', $this->store->slug), ['preset' => 'bold'])
- ->assertRedirect(route('stores.show', $this->store->slug));
-
- $theme = $this->store->fresh()->plan['theme'];
- $bold = PlanSchema::STYLE_PRESETS['bold'];
-
- $this->assertSame($bold['style'], $theme['style']);
- $this->assertSame($bold['fonts'], $theme['fonts']);
- $this->assertSame('none', $theme['radius']);
- $this->assertSame($colours, $theme['colors']);
-
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('style.options.buttons', 'shadow')
- ->where('style.fonts.heading', 'archivo-black')
- ->has('stylePresets', count(PlanSchema::STYLE_PRESETS)));
- }
-
- public function test_one_option_can_be_fine_tuned()
- {
- $this->put(route('stores.style.update', $this->store->slug), ['style' => ['spacing' => 'airy']])->assertSessionHasNoErrors();
-
- $style = $this->store->fresh()->plan['theme']['style'];
- $this->assertSame('airy', $style['spacing']);
- // The others keep their values.
- $this->assertSame('shadow', $style['cards']);
-
- $this->put(route('stores.style.update', $this->store->slug), ['style' => ['spacing' => 'enormous']])->assertSessionHasErrors('style.spacing');
- $this->put(route('stores.style.update', $this->store->slug), ['preset' => 'retro-80s'])->assertSessionHasErrors('preset');
-
- $this->actingAs(User::factory()->create())
- ->put(route('stores.style.update', $this->store->slug), ['preset' => 'bold'])
- ->assertNotFound();
- }
-
- public function test_old_designs_without_a_style_still_pass_and_look_the_same()
- {
- $plan = $this->store->plan;
- unset($plan['theme']['style']);
-
- $clean = app(PlanValidator::class)->validate($plan);
-
- $this->assertArrayNotHasKey('style', $clean['theme']);
-
- $this->store->plan = $clean;
- $this->store->save();
-
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('style.options', PlanSchema::STYLE_DEFAULTS));
- }
-
- public function test_the_checker_refuses_unknown_styles_and_heading_fonts_for_body_text()
- {
- $validator = app(PlanValidator::class);
- $plan = $this->store->plan;
-
- $this->assertFalse($validator->passes([...$plan, 'theme' => [...$plan['theme'], 'style' => ['buttons' => 'neon']]]));
- $this->assertFalse($validator->passes([...$plan, 'theme' => [...$plan['theme'], 'fonts' => ['heading' => 'inter', 'body' => 'bebas-neue']]]));
- $this->assertTrue($validator->passes([...$plan, 'theme' => [...$plan['theme'], 'fonts' => ['heading' => 'bebas-neue', 'body' => 'jost']]]));
-
- // Unknown style parts are thrown away.
- $clean = $validator->validate([...$plan, 'theme' => [...$plan['theme'], 'style' => ['cards' => 'hard', 'sparkles' => 'yes']]]);
- $this->assertSame(['cards' => 'hard'], $clean['theme']['style']);
- }
-
- public function test_the_ai_can_change_the_style_by_chat()
- {
- ScriptedDriver::use([(string) json_encode([
- 'reply' => 'I made the store bolder.',
- 'theme' => ['style' => ['buttons' => 'shadow', 'headings' => 'heavy'], 'fonts' => ['heading' => 'unbounded']],
- ])]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Make it look bolder and more futuristic']);
-
- $theme = $this->store->fresh()->plan['theme'];
- $this->assertSame('shadow', $theme['style']['buttons']);
- $this->assertSame('heavy', $theme['style']['headings']);
- // Untouched parts stay.
- $this->assertSame('alternate', $theme['style']['backgrounds']);
- $this->assertSame('unbounded', $theme['fonts']['heading']);
- $this->assertStringContainsString('These fonts are for headings only', ScriptedDriver::$requests[0]->system);
- }
- }
|