<?php

namespace Tests\Feature;

use App\Campaigns\Manager\CampaignSynchronizer;
use App\Campaigns\Manager\Editing\EditCapabilities;
use App\Campaigns\Manager\Editing\EditRefused;
use App\Campaigns\Manager\Editing\EntityEditor;
use App\Campaigns\Manager\Editing\Writers\MetaEntityWriter;
use App\Livewire\CampaignManager\EditEntity;
use App\Models\Campaign;
use App\Models\CampaignChange;
use App\Models\CampaignEntityEdit;
use App\Models\CampaignPlatformEntity;
use App\Models\User;
use App\Services\GoogleAdsService;
use App\Services\LinkedIn\LinkedIn;
use App\Services\Meta\Meta;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\ValidationException;
use Livewire\Livewire;
use Mockery;
use RuntimeException;
use Tests\TestCase;

/**
 * Budgets and bids changed from the campaign manager.
 *
 * Only the budget an object already holds can be changed, inside the platform
 * minimum and this product's ceiling, and large moves have to be acknowledged.
 */
class CampaignManagerBudgetEditTest extends TestCase
{
    use RefreshDatabase;

    private User $user;

    /** @var array{campaign: array<string, mixed>, adsets: list<array<string, mixed>>, ads: list<array<string, mixed>>} */
    private array $meta = ['campaign' => [], 'adsets' => [], 'ads' => []];

    /** @var list<array{id: string, fields: array<string, mixed>}> */
    private array $writes = [];

    protected function setUp(): void
    {
        parent::setUp();

        config([
            'platforms.meta.token' => 'test-token',
            'platforms.meta.mcp.enabled' => false,
            'platforms.guardrails.allowed_ad_accounts' => ['meta:*', 'google:*', 'linkedin:*'],
            'platforms.guardrails.paused' => false,
            'platforms.guardrails.change_cooldown_seconds' => 0,
            'platforms.guardrails.max_changes_per_day' => 50,
            'platforms.guardrails.budget_ceiling_currency' => 'USD',
            'platforms.guardrails.max_daily_budget' => 500,
            'platforms.guardrails.confirm_daily_budget_above' => 200,
            'platforms.guardrails.max_daily_budget_by_currency' => ['JPY' => 70000],
            'campaign-manager.edit_platforms' => ['meta', 'google', 'linkedin'],
            'campaign-manager.edit_categories' => ['name', 'budget', 'bid'],
            'campaign-manager.confirm_change_percent' => 20,
            'services.google_ads.refresh_token' => 'token',
            'services.google_ads.customer_id' => '3460855874',
        ]);

        $this->user = User::factory()->create();

        $this->meta['campaign'] = [
            'id' => '120252047635070626', 'name' => 'Winter Sale', 'status' => 'ACTIVE',
            'effective_status' => 'ACTIVE', 'objective' => 'OUTCOME_SALES', 'account_id' => '111',
        ];
        $this->meta['adsets'] = [
            ['id' => '2001', 'name' => 'US mobile', 'status' => 'ACTIVE', 'daily_budget' => '2500', 'bid_strategy' => 'COST_CAP', 'bid_amount' => '400', 'is_budget_schedule_enabled' => false],
        ];

        Http::fake(function (Request $request) {
            $path = (string) parse_url($request->url(), PHP_URL_PATH);

            if ($request->method() === 'POST') {
                $id = basename($path);
                $fields = array_diff_key($request->data(), ['access_token' => true]);
                $this->writes[] = ['id' => $id, 'fields' => $fields];
                $this->applyMeta($id, $fields);

                return Http::response(['success' => true]);
            }

            return match (true) {
                str_ends_with($path, '/adsets') => Http::response(['data' => $this->meta['adsets']]),
                str_ends_with($path, '/ads') => Http::response(['data' => $this->meta['ads']]),
                default => Http::response($this->meta['campaign']),
            };
        });
    }

    /** @param  array<string, mixed>  $fields */
    private function applyMeta(string $id, array $fields): void
    {
        if ($id === $this->meta['campaign']['id']) {
            $this->meta['campaign'] = [...$this->meta['campaign'], ...$fields];
        }

        foreach ($this->meta['adsets'] as $i => $row) {
            if ($row['id'] === $id) {
                $this->meta['adsets'][$i] = [...$row, ...$fields];
            }
        }
    }

    private function campaign(array $attributes = []): Campaign
    {
        return Campaign::create([
            'user_id' => $this->user->id,
            'name' => 'Winter Sale',
            'platform' => 'meta',
            'status' => 'published',
            'ad_account_id' => 'act_111',
            'currency' => 'USD',
            'external_campaign_id' => '120252047635070626',
            ...$attributes,
        ]);
    }

    private function read(?Campaign $campaign = null): Campaign
    {
        $campaign ??= $this->campaign();
        app(CampaignSynchronizer::class)->sync($campaign);

        return $campaign;
    }

    private function entity(Campaign $campaign, string $externalId): CampaignPlatformEntity
    {
        return $campaign->platformEntities()->where('external_id', $externalId)->sole();
    }

    private function editor(): EntityEditor
    {
        return app(EntityEditor::class);
    }

    private function capabilities(): EditCapabilities
    {
        return app(EditCapabilities::class);
    }

    /** @param  array<string, mixed>  $values */
    private function assertRejected(Campaign $campaign, CampaignPlatformEntity $entity, array $values, string $field, string $message): void
    {
        try {
            $this->editor()->propose($campaign, $entity, $this->user, $values);
            $this->fail('Accepted '.json_encode($values));
        } catch (ValidationException $exception) {
            $this->assertStringContainsString($message, $exception->errors()[$field][0] ?? '');
        }
    }

    // ------------------------------------------------------------------ Meta

    public function test_an_ad_set_daily_budget_is_sent_in_minor_units_and_read_back(): void
    {
        $campaign = $this->read();
        $adSet = $this->entity($campaign, '2001');

        $edit = $this->editor()->propose($campaign, $adSet, $this->user, ['daily_budget' => '28.00']);
        $this->assertEquals(['daily_budget' => ['before' => 25.0, 'after' => 28.0]], $edit->changes);
        $this->assertSame([], $this->capabilities()->confirmations($adSet, $edit->changes));

        $edit = $this->editor()->apply($edit, $this->user);

        $this->assertSame(CampaignEntityEdit::Applied, $edit->state, (string) $edit->error);
        $this->assertSame([['id' => '2001', 'fields' => ['daily_budget' => 2800]]], $this->writes);
        $this->assertEquals(['ad_set:2001' => ['daily_budget' => 28.0]], CampaignChange::sole()->changes);
        $this->assertSame(28.0, EditCapabilities::current($adSet->refresh(), 'daily_budget'));
    }

    public function test_a_yen_budget_is_whole_yen_and_not_multiplied_by_a_hundred(): void
    {
        $this->meta['adsets'][0]['daily_budget'] = '2500';
        $campaign = $this->read($this->campaign(['currency' => 'JPY']));
        $adSet = $this->entity($campaign, '2001');

        $this->assertRejected($campaign, $adSet, ['daily_budget' => '2800.50'], 'daily_budget', 'Use a whole amount; JPY');

        $edit = $this->editor()->apply($this->editor()->propose($campaign, $adSet, $this->user, ['daily_budget' => '2800']), $this->user);

        $this->assertSame(CampaignEntityEdit::Applied, $edit->state, (string) $edit->error);
        $this->assertEquals(['daily_budget' => '2800'], $this->writes[0]['fields']);
    }

    public function test_amounts_outside_the_platform_minimum_and_the_ceiling_are_rejected(): void
    {
        $campaign = $this->read();
        $adSet = $this->entity($campaign, '2001');

        $this->assertRejected($campaign, $adSet, ['daily_budget' => '4.99'], 'daily_budget', 'Meta needs at least 5.00 USD a day');
        $this->assertRejected($campaign, $adSet, ['daily_budget' => '500.01'], 'daily_budget', 'more than the 500.00 USD a day');
        $this->assertRejected($campaign, $adSet, ['daily_budget' => '30.005'], 'daily_budget', 'at most 2 decimal places');
        $this->assertRejected($campaign, $adSet, ['daily_budget' => '0'], 'daily_budget', 'greater than 0');
        $this->assertRejected($campaign, $adSet, ['bid_amount' => '501'], 'bid_amount', 'A bid above 500.00 USD');

        $this->assertSame(0, CampaignEntityEdit::count());
    }

    public function test_a_budget_held_on_the_campaign_cannot_be_changed_on_an_ad_set(): void
    {
        $this->meta['campaign']['daily_budget'] = '10000';
        unset($this->meta['adsets'][0]['daily_budget']);
        $campaign = $this->read();
        $adSet = $this->entity($campaign, '2001');

        $this->assertNotContains('daily_budget', $this->capabilities()->editableFields($adSet));
        $this->assertStringContainsString('held on the campaign', $this->capabilities()->lockedFields($adSet)['daily_budget']);
        $this->assertSame(['name', 'daily_budget'], $this->capabilities()->editableFields($this->entity($campaign, '120252047635070626')));

        $this->expectException(EditRefused::class);
        $this->editor()->propose($campaign, $adSet, $this->user, ['daily_budget' => '30']);
    }

    public function test_a_large_change_is_not_sent_until_it_is_acknowledged(): void
    {
        $campaign = $this->read();
        $edit = $this->editor()->propose($campaign, $this->entity($campaign, '2001'), $this->user, ['daily_budget' => '40']);

        try {
            $this->editor()->apply($edit, $this->user);
            $this->fail('A +60% budget change was sent without being acknowledged.');
        } catch (EditRefused $exception) {
            $this->assertStringContainsString('acknowledged', $exception->getMessage());
        }

        $this->assertSame([], $this->writes);
        $this->assertTrue($edit->refresh()->isOpen());

        $this->assertSame(CampaignEntityEdit::Applied, $this->editor()->apply($edit, $this->user, acknowledged: true)->state);
        $this->assertEquals('4000', $this->writes[0]['fields']['daily_budget']);
    }

    public function test_a_ceiling_lowered_after_proposing_stops_the_change_at_apply(): void
    {
        $campaign = $this->read();
        $edit = $this->editor()->propose($campaign, $this->entity($campaign, '2001'), $this->user, ['daily_budget' => '29']);

        config(['platforms.guardrails.max_daily_budget' => 26]);

        try {
            $this->editor()->apply($edit, $this->user, acknowledged: true);
            $this->fail('A budget above the current ceiling was sent.');
        } catch (EditRefused $exception) {
            $this->assertStringContainsString('Nothing was sent', $exception->getMessage());
        }

        $this->assertSame([], $this->writes);
    }

    public function test_a_lifetime_budget_is_checked_per_day_over_the_run_and_needs_an_end_date(): void
    {
        $this->meta['adsets'][0] = [
            'id' => '2001', 'name' => 'US mobile', 'status' => 'ACTIVE', 'lifetime_budget' => '30000',
            'start_time' => '2026-11-01T00:00:00+0000', 'end_time' => '2026-11-11T00:00:00+0000',
        ];
        $campaign = $this->read();
        $adSet = $this->entity($campaign, '2001');

        $this->assertSame(['name', 'lifetime_budget'], $this->capabilities()->editableFields($adSet));
        $this->assertRejected($campaign, $adSet, ['lifetime_budget' => '5001'], 'lifetime_budget', '500.10 a day over 10 days');
        $this->assertRejected($campaign, $adSet, ['lifetime_budget' => '49'], 'lifetime_budget', 'Meta needs at least 5.00 USD a day');

        $edit = $this->editor()->apply($this->editor()->propose($campaign, $adSet, $this->user, ['lifetime_budget' => '330']), $this->user);
        $this->assertSame(CampaignEntityEdit::Applied, $edit->state, (string) $edit->error);
        $this->assertEquals(['lifetime_budget' => '33000'], $this->writes[0]['fields']);

        $this->meta['adsets'][0]['end_time'] = null;
        $this->read($campaign);

        $this->assertStringContainsString('no end date', $this->capabilities()->lockedFields($adSet->refresh())['lifetime_budget']);
    }

    public function test_a_bid_amount_is_only_offered_for_strategies_that_use_one(): void
    {
        $campaign = $this->read();
        $adSet = $this->entity($campaign, '2001');

        $this->assertContains('bid_amount', $this->capabilities()->editableFields($adSet));

        $this->meta['adsets'][0]['bid_strategy'] = 'LOWEST_COST_WITHOUT_CAP';
        $this->read($campaign);

        $this->assertNotContains('bid_amount', $this->capabilities()->editableFields($adSet->refresh()));
        $this->assertStringContainsString('does not use a bid amount', $this->capabilities()->lockedFields($adSet)['bid_amount']);
    }

    public function test_budgets_and_bids_each_need_their_own_switch(): void
    {
        config(['campaign-manager.edit_categories' => ['name', 'bid']]);
        $campaign = $this->read();
        $adSet = $this->entity($campaign, '2001');

        $this->assertSame(['name', 'bid_amount'], $this->capabilities()->editableFields($adSet));
        $this->assertStringContainsString('Budget changes are not switched on', $this->capabilities()->lockedFields($adSet)['daily_budget']);

        $this->expectException(EditRefused::class);
        $this->editor()->propose($campaign, $adSet, $this->user, ['daily_budget' => '30']);
    }

    // ---------------------------------------------------------------- Google

    /** @param  array<string, mixed>  $profile */
    private function google(array &$profile, array &$groups = []): Mockery\MockInterface
    {
        $google = Mockery::mock(GoogleAdsService::class);
        $google->shouldReceive('readManagerCriteria')->andReturn([]);
        $google->shouldReceive('readManagerAudiences')->andReturn([]);
        $google->shouldReceive('readCampaignProfile')->andReturnUsing(function () use (&$profile): array {
            return $profile;
        });
        $google->shouldReceive('readCampaignStructure')->andReturnUsing(function () use (&$groups): array {
            return ['ad_groups' => $groups, 'ads' => [], 'asset_groups' => []];
        });
        $this->app->instance(GoogleAdsService::class, $google);

        return $google;
    }

    private function googleCampaign(): Campaign
    {
        return $this->read($this->campaign(['platform' => 'google', 'ad_account_id' => '3460855874', 'external_campaign_id' => '987']));
    }

    public function test_a_google_campaign_budget_is_changed_on_its_budget_resource(): void
    {
        $profile = ['id' => '987', 'name' => 'Search', 'status' => 'ENABLED', 'daily_budget' => 40.0,
            'budget_resource' => 'customers/3460855874/campaignBudgets/555', 'budget_reference_count' => 1];
        $google = $this->google($profile);
        $google->shouldReceive('updateLiveBudget')->once()
            ->withArgs(fn ($connection, string $id, float $amount): bool => $id === '555' && $amount === 45.0)
            ->andReturnUsing(function () use (&$profile): void {
                $profile['daily_budget'] = 45.0;
            });

        $campaign = $this->googleCampaign();
        $edit = $this->editor()->propose($campaign, $this->entity($campaign, '987'), $this->user, ['daily_budget' => '45']);

        $this->assertSame(CampaignEntityEdit::Applied, $this->editor()->apply($edit, $this->user)->state);
    }

    public function test_a_shared_google_budget_is_not_offered(): void
    {
        $profile = ['id' => '987', 'name' => 'Search', 'status' => 'ENABLED', 'daily_budget' => 40.0,
            'budget_resource' => 'customers/3460855874/campaignBudgets/555', 'budget_reference_count' => 3];
        $this->google($profile)->shouldNotReceive('updateLiveBudget');

        $campaign = $this->googleCampaign();
        $entity = $this->entity($campaign, '987');

        $this->assertSame(['name'], $this->capabilities()->editableFields($entity));
        $this->assertStringContainsString('shared by 3 campaigns', $this->capabilities()->lockedFields($entity)['daily_budget']);
    }

    public function test_a_google_change_that_fails_halfway_says_what_landed(): void
    {
        $profile = ['id' => '987', 'name' => 'Search', 'status' => 'ENABLED', 'daily_budget' => 40.0,
            'budget_resource' => 'customers/3460855874/campaignBudgets/555', 'budget_reference_count' => 1];
        $google = $this->google($profile);
        $google->shouldReceive('updateLiveBudget')->once()->andReturnUsing(function () use (&$profile): void {
            $profile['daily_budget'] = 45.0;
        });
        $google->shouldReceive('updateLiveCampaign')->once()->andThrow(new RuntimeException('DUPLICATE_CAMPAIGN_NAME'));

        $campaign = $this->googleCampaign();
        $edit = $this->editor()->propose($campaign, $this->entity($campaign, '987'), $this->user, ['name' => 'Search 2', 'daily_budget' => '45']);
        $edit = $this->editor()->apply($edit, $this->user);

        $this->assertSame(CampaignEntityEdit::Failed, $edit->state);
        $this->assertStringContainsString('DUPLICATE_CAMPAIGN_NAME', (string) $edit->error);
        $this->assertStringContainsString('Partly applied: daily budget did change', (string) $edit->error);
        $this->assertEquals(['name' => 'Search', 'daily_budget' => 45], $edit->readback);
    }

    public function test_a_google_ad_group_cpc_bid_is_changed(): void
    {
        $profile = ['id' => '987', 'name' => 'Search', 'status' => 'ENABLED'];
        $groups = [['id' => '61', 'name' => 'Brand', 'status' => 'ENABLED', 'cpc_bid' => 1.2]];
        $google = $this->google($profile, $groups);
        $google->shouldReceive('updateAdGroup')->once()
            ->withArgs(fn ($connection, string $id, array $fields): bool => $id === '61' && $fields === ['cpc_bid' => 1.35])
            ->andReturnUsing(function () use (&$groups): void {
                $groups[0]['cpc_bid'] = 1.35;
            });

        $campaign = $this->googleCampaign();
        $edit = $this->editor()->propose($campaign, $this->entity($campaign, '61'), $this->user, ['cpc_bid' => '1.35']);

        $this->assertStringContainsString('Any bid change can put delivery back into learning', implode(' ', app(EditCapabilities::class)->confirmations($edit->entity, $edit->changes)));

        try {
            $this->editor()->apply($edit, $this->user);
            $this->fail('A bid change went out without being acknowledged.');
        } catch (EditRefused $refused) {
            $this->assertStringContainsString('needs to be acknowledged', $refused->getMessage());
        }

        $this->assertSame(CampaignEntityEdit::Applied, $this->editor()->apply($edit->refresh(), $this->user, acknowledged: true)->state);
    }

    // -------------------------------------------------------------- LinkedIn

    public function test_a_linkedin_daily_budget_keeps_the_platform_money_shape(): void
    {
        $live = ['id' => 555, 'name' => 'Lead gen', 'status' => 'ACTIVE', 'campaignGroup' => 'urn:li:sponsoredCampaignGroup:44',
            'costType' => 'CPC', 'dailyBudget' => ['amount' => '50.00', 'currencyCode' => 'USD'], 'unitCost' => ['amount' => '8.00', 'currencyCode' => 'USD']];

        $linkedin = Mockery::mock(LinkedIn::class);
        $linkedin->shouldReceive('campaignConversions')->andReturn([]);
        $linkedin->shouldReceive('on')->andReturnSelf();
        $linkedin->shouldReceive('campaign')->andReturnUsing(function () use (&$live): array {
            return $live;
        });
        $linkedin->shouldReceive('campaignGroup')->andReturn(['id' => 44, 'name' => 'Q4', 'status' => 'ACTIVE']);
        $linkedin->shouldReceive('creatives')->andReturn(['elements' => [], 'next_page_token' => null]);
        $linkedin->shouldReceive('updateEntity')->once()
            ->with('urn:li:sponsoredAccount:999', 'campaign', '555', ['dailyBudget' => ['amount' => '55.00', 'currencyCode' => 'USD']])
            ->andReturnUsing(function () use (&$live): void {
                $live['dailyBudget']['amount'] = '55.00';
            });
        $this->app->instance(LinkedIn::class, $linkedin);

        $campaign = $this->read($this->campaign(['platform' => 'linkedin', 'ad_account_id' => 'urn:li:sponsoredAccount:999', 'external_campaign_id' => '555']));
        $entity = $this->entity($campaign, '555');

        $this->assertSame(['name', 'daily_budget', 'unit_cost'], $this->capabilities()->editableFields($entity));
        $this->assertSame(['name'], $this->capabilities()->editableFields($this->entity($campaign, '44')));
        $this->assertRejected($campaign, $entity, ['daily_budget' => '9.99'], 'daily_budget', 'LinkedIn needs at least 10.00 USD a day');

        $edit = $this->editor()->apply($this->editor()->propose($campaign, $entity, $this->user, ['daily_budget' => '55']), $this->user);

        $this->assertSame(CampaignEntityEdit::Applied, $edit->state, (string) $edit->error);
        $this->assertEquals(['daily_budget' => 55.0], $edit->readback);
    }

    public function test_a_linkedin_amount_in_another_currency_is_not_offered(): void
    {
        $linkedin = Mockery::mock(LinkedIn::class);
        $linkedin->shouldReceive('campaignConversions')->andReturn([]);
        $linkedin->shouldReceive('on')->andReturnSelf();
        $linkedin->shouldReceive('campaign')->andReturn(['id' => 555, 'name' => 'Lead gen', 'status' => 'ACTIVE',
            'campaignGroup' => 'urn:li:sponsoredCampaignGroup:44', 'dailyBudget' => ['amount' => '50.00', 'currencyCode' => 'EUR']]);
        $linkedin->shouldReceive('campaignGroup')->andReturn(['id' => 44, 'name' => 'Q4', 'status' => 'ACTIVE']);
        $linkedin->shouldReceive('creatives')->andReturn(['elements' => [], 'next_page_token' => null]);
        $linkedin->shouldNotReceive('updateEntity');
        $this->app->instance(LinkedIn::class, $linkedin);

        $campaign = $this->read($this->campaign(['platform' => 'linkedin', 'ad_account_id' => 'urn:li:sponsoredAccount:999', 'external_campaign_id' => '555']));
        $entity = $this->entity($campaign, '555');

        $this->assertSame(['name'], $this->capabilities()->editableFields($entity));
        $this->assertStringContainsString('reports this amount in EUR', $this->capabilities()->lockedFields($entity)['daily_budget']);
    }

    // ---------------------------------------------------------------- wizard

    public function test_the_wizard_asks_for_acknowledgement_before_a_large_budget_change(): void
    {
        $campaign = $this->read();
        $adSet = $this->entity($campaign, '2001');

        $wizard = Livewire::actingAs($this->user)
            ->test(EditEntity::class, ['campaign' => $campaign, 'entity' => $adSet])
            ->assertSet('values.daily_budget', '25.00')
            ->assertSee('USD')
            ->set('values.daily_budget', '40')
            ->call('review')
            ->assertHasNoErrors()
            ->assertSee('+60%')
            ->assertSee('back into learning')
            ->call('confirm')
            ->assertSee('needs to be acknowledged');

        $this->assertSame([], $this->writes);

        $wizard->set('acknowledged', true)->call('confirm')->assertSee('Done.');

        $this->assertEquals('4000', $this->writes[0]['fields']['daily_budget']);
        $this->actingAs($this->user)->get(route('campaign-manager.show', $campaign))
            ->assertSee('25.00 → 40.00 USD')
            ->assertSee('Edited here');
    }

    public function test_a_meta_change_is_written_live_and_never_staged_as_an_ads_manager_draft(): void
    {
        $campaign = $this->read();
        $adSet = $this->entity($campaign, '2001');

        $meta = Mockery::mock(Meta::class);
        $meta->shouldReceive('on')->andReturnSelf();
        $meta->shouldNotReceive('updateEntity');
        $meta->shouldReceive('updateLiveEntity')->once()->with('act_111', '2001', ['daily_budget' => 2800]);

        (new MetaEntityWriter($meta))->write($campaign, $adSet, ['daily_budget' => 28.0], 'test');
    }

    public function test_the_wizard_replaces_an_old_error_when_the_amount_is_corrected(): void
    {
        $campaign = $this->read();

        Livewire::actingAs($this->user)
            ->test(EditEntity::class, ['campaign' => $campaign, 'entity' => $this->entity($campaign, '2001')])
            ->set('values.daily_budget', '4')
            ->call('review')
            ->assertHasErrors(['values.daily_budget'])
            ->assertSee('needs at least 5.00 USD')
            ->set('values.daily_budget', '99999')
            ->call('review')
            ->assertDontSee('needs at least 5.00 USD')
            ->assertSee('this product allows')
            ->set('values.daily_budget', '28')
            ->call('review')
            ->assertHasNoErrors()
            ->assertSee('+12%');
    }

    public function test_the_wizard_shows_why_a_budget_cannot_be_changed(): void
    {
        $this->meta['campaign']['daily_budget'] = '10000';
        unset($this->meta['adsets'][0]['daily_budget']);
        $campaign = $this->read();

        Livewire::actingAs($this->user)
            ->test(EditEntity::class, ['campaign' => $campaign, 'entity' => $this->entity($campaign, '2001')])
            ->assertSee('Not changeable here')
            ->assertSee('held on the campaign');
    }
}
