| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
-
- namespace Tests\Feature\Ai;
-
- use App\Ai\AiException;
- use App\StorePlan\Ai\JsonAnswer;
- use App\StorePlan\Ai\PlanChanges;
- use App\StorePlan\Ai\PlanPrompt;
- use App\StorePlan\PlanSchema;
- use App\StorePlan\PlanValidator;
- use App\StorePlan\SamplePlan;
- use Tests\TestCase;
-
- class PlanChangesTest extends TestCase
- {
- public function test_it_changes_only_the_named_theme_and_content_fields()
- {
- $plan = SamplePlan::forName('Kedai');
-
- $changed = PlanChanges::apply($plan, [
- 'theme' => ['colors' => ['primary' => '#112233'], 'radius' => 'large'],
- 'update' => [['id' => 'hero', 'variant' => 'split', 'content' => ['headline' => 'New headline']]],
- ]);
-
- $hero = collect($changed['pages'][0]['sections'])->firstWhere('id', 'hero');
- $oldHero = collect($plan['pages'][0]['sections'])->firstWhere('id', 'hero');
-
- $this->assertSame('#112233', $changed['theme']['colors']['primary']);
- $this->assertSame($plan['theme']['colors']['background'], $changed['theme']['colors']['background']);
- $this->assertSame('large', $changed['theme']['radius']);
- $this->assertSame('split', $hero['variant']);
- $this->assertSame('New headline', $hero['content']['headline']);
- $this->assertSame($oldHero['content']['subheadline'], $hero['content']['subheadline']);
- $this->assertTrue(app(PlanValidator::class)->passes($changed));
- }
-
- public function test_it_can_add_remove_and_reorder_sections()
- {
- $plan = SamplePlan::forName('Kedai');
- $ids = array_column($plan['pages'][0]['sections'], 'id');
- $faq = collect($plan['pages'][0]['sections'])->firstWhere('type', 'faq');
-
- $changed = PlanChanges::apply($plan, [
- 'remove' => [$faq['id']],
- 'add' => [['after' => 'hero', 'section' => [...$faq, 'id' => 'faq-top']]],
- ]);
-
- $newIds = array_column($changed['pages'][0]['sections'], 'id');
-
- $this->assertNotContains($faq['id'], $newIds);
- $this->assertSame('faq-top', $newIds[array_search('hero', $newIds, true) + 1]);
- $this->assertCount(count($ids), $newIds);
-
- $reversed = array_reverse($newIds);
-
- $this->assertSame($reversed, array_column(PlanChanges::apply($changed, ['order' => $reversed])['pages'][0]['sections'], 'id'));
- // An order that leaves a section out is ignored.
- $this->assertSame($newIds, array_column(PlanChanges::apply($changed, ['order' => ['hero']])['pages'][0]['sections'], 'id'));
- }
-
- public function test_it_can_change_the_shared_header_and_footer()
- {
- $plan = SamplePlan::forName('Kedai');
-
- $changed = PlanChanges::apply($plan, [
- 'header' => ['variant' => 'centered', 'content' => ['logoText' => 'Kedai Baru']],
- 'footer' => ['content' => ['text' => 'New footer text']],
- ]);
-
- $this->assertSame('centered', $changed['header']['variant']);
- $this->assertSame('Kedai Baru', $changed['header']['content']['logoText']);
- $this->assertSame($plan['header']['content']['links'], $changed['header']['content']['links']);
- $this->assertSame('New footer text', $changed['footer']['content']['text']);
- $this->assertSame($plan['footer']['variant'], $changed['footer']['variant']);
- $this->assertTrue(app(PlanValidator::class)->passes($changed));
- }
-
- public function test_an_empty_answer_changes_nothing()
- {
- $plan = SamplePlan::forName('Kedai');
-
- $this->assertSame($plan, PlanChanges::apply($plan, ['reply' => 'Nothing to do.']));
- }
-
- public function test_json_is_read_even_with_fences_or_extra_words()
- {
- $this->assertSame(['a' => 1], JsonAnswer::decode("Here you go:\n```json\n{\"a\": 1}\n```"));
-
- $this->expectException(AiException::class);
- JsonAnswer::decode('Sorry, I cannot do that.');
- }
-
- public function test_the_prompt_describes_every_section_type_and_font_from_the_schema()
- {
- $format = PlanPrompt::format();
-
- foreach (array_keys(PlanSchema::SECTIONS) as $type) {
- $this->assertStringContainsString("## $type\n", $format);
- }
-
- foreach (array_keys(PlanSchema::FONTS) as $font) {
- $this->assertStringContainsString($font, $format);
- }
-
- $this->assertStringNotContainsString('regex:', $format);
- $this->assertStringNotContainsString('[[FORMAT]]', PlanPrompt::generateSystem().PlanPrompt::editSystem());
- }
- }
|