false]); $this->user = User::factory()->onboarded()->create(); $this->actingAs($this->user); }); test('guests are redirected to the registration page for accounts index', function () { auth()->logout(); $this->get(route('accounts.list'))->assertRedirect(route('register')); }); test('guests are redirected to the registration page for account show', function () { auth()->logout(); $account = Account::factory()->create(['user_id' => $this->user->id]); $this->get(route('accounts.show', $account))->assertRedirect(route('register')); }); test('authenticated users can visit the accounts index', function () { $this->get(route('accounts.list'))->assertOk(); }); test('accounts index returns accounts grouped by type', function () { $checking = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Checking, ]); $savings = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Savings, ]); $investment = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Investment, ]); $response = $this->get(route('accounts.list')); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Index') ->has('accounts', 3) ); }); test('accounts are ordered by type then name', function () { Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Savings, 'name' => 'A Savings', ]); Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Checking, 'name' => 'B Checking', ]); Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Checking, 'name' => 'A Checking', ]); $response = $this->get(route('accounts.list')); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Index') ->has('accounts', 3) ->where('accounts.0.type', 'checking') ->where('accounts.0.name', 'A Checking') ->where('accounts.1.type', 'checking') ->where('accounts.1.name', 'B Checking') ->where('accounts.2.type', 'savings') ); }); test('accounts index only shows user accounts', function () { $myAccount = Account::factory()->create([ 'user_id' => $this->user->id, ]); $otherAccount = Account::factory()->create([ 'user_id' => User::factory()->create()->id, ]); $response = $this->get(route('accounts.list')); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Index') ->has('accounts', 1) ->where('accounts.0.id', $myAccount->id) ); }); test('authenticated users can view their own account', function () { $account = Account::factory()->create([ 'user_id' => $this->user->id, ]); $response = $this->get(route('accounts.show', $account)); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Show') ->has('account') ->where('account.id', $account->id) ); }); test('users cannot view other users accounts', function () { $otherUser = User::factory()->create(); $account = Account::factory()->create([ 'user_id' => $otherUser->id, ]); $response = $this->get(route('accounts.show', $account)); $response->assertForbidden(); }); test('account show includes categories, accounts, and banks', function () { $account = Account::factory()->create([ 'user_id' => $this->user->id, ]); Category::factory()->count(3)->create([ 'user_id' => $this->user->id, ]); $response = $this->get(route('accounts.show', $account)); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Show') ->has('account') ->has('categories', 3) ->has('accounts', 1) ->has('banks') ); }); test('account show includes shared labels and automation rules for transaction tools', function () { $account = Account::factory()->create([ 'user_id' => $this->user->id, ]); $label = Label::factory()->create([ 'user_id' => $this->user->id, 'name' => 'Groceries', ]); $rule = AutomationRule::factory()->create([ 'user_id' => $this->user->id, 'title' => 'Apply groceries label', ]); $rule->labels()->attach($label); $response = $this->withoutVite()->get(route('accounts.show', $account)); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Show') ->has('labels', 1) ->where('labels.0.id', $label->id) ->has('automationRules', 1) ->where('automationRules.0.id', $rule->id) ); }); test('account balance evolution returns data for single account', function () { $account = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Checking, ]); AccountBalance::factory()->create([ 'account_id' => $account->id, 'balance_date' => now()->subMonthNoOverflow()->endOfMonth(), 'balance' => 100000, ]); AccountBalance::factory()->create([ 'account_id' => $account->id, 'balance_date' => now()->endOfMonth(), 'balance' => 150000, ]); $response = $this->getJson('/api/dashboard/account/'.$account->id.'/balance-evolution?'.http_build_query([ 'from' => now()->subMonthsNoOverflow(2)->startOfMonth()->toDateString(), 'to' => now()->endOfMonth()->toDateString(), ])); $response->assertOk(); $data = $response->json(); expect($data)->toHaveKeys(['data', 'account']); expect($data['data'])->toHaveCount(3); expect($data['data'][0])->toHaveKeys(['month', 'timestamp', 'value']); expect($data['account']['id'])->toBe($account->id); }); test('account balance evolution denies access to other users accounts', function () { $otherUser = User::factory()->create(); $account = Account::factory()->create([ 'user_id' => $otherUser->id, ]); $response = $this->getJson('/api/dashboard/account/'.$account->id.'/balance-evolution?'.http_build_query([ 'from' => now()->subMonth()->toDateString(), 'to' => now()->toDateString(), ])); $response->assertForbidden(); }); test('accounts index defers account metrics', function () { $account = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Checking, 'currency_code' => 'USD', ]); AccountBalance::factory()->create([ 'account_id' => $account->id, 'balance_date' => now()->subMonthNoOverflow()->startOfMonth(), 'balance' => 100000, ]); AccountBalance::factory()->create([ 'account_id' => $account->id, 'balance_date' => now()->startOfMonth(), 'balance' => 150000, ]); $response = $this->get(route('accounts.list')); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Index') ->has('accounts', 1) ->missing('accountMetrics') ->loadDeferredProps(fn ($reload) => $reload ->has('accountMetrics') ->has("accountMetrics.{$account->id}") ->has("accountMetrics.{$account->id}.currentBalance") ->has("accountMetrics.{$account->id}.previousBalance") ->has("accountMetrics.{$account->id}.diff") ->has("accountMetrics.{$account->id}.history") ->where("accountMetrics.{$account->id}.currentBalance", 150000) ->where("accountMetrics.{$account->id}.previousBalance", 100000) ->where("accountMetrics.{$account->id}.diff", 50000) ) ); }); test('accounts index deferred metrics includes invested amount for investment accounts', function () { $account = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Investment, 'currency_code' => 'USD', ]); AccountBalance::factory()->withInvestedAmount(80000)->create([ 'account_id' => $account->id, 'balance_date' => now()->startOfMonth(), 'balance' => 120000, ]); $response = $this->get(route('accounts.list')); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Index') ->missing('accountMetrics') ->loadDeferredProps(fn ($reload) => $reload ->where("accountMetrics.{$account->id}.investedAmount", 80000) ->where("accountMetrics.{$account->id}.currentBalance", 120000) ) ); }); test('accounts index deferred metrics returns null invested amount for non-investment accounts', function () { $account = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Checking, 'currency_code' => 'USD', ]); AccountBalance::factory()->create([ 'account_id' => $account->id, 'balance_date' => now()->startOfMonth(), 'balance' => 100000, ]); $response = $this->get(route('accounts.list')); $response->assertOk() ->assertInertia(fn ($page) => $page ->missing('accountMetrics') ->loadDeferredProps(fn ($reload) => $reload ->where("accountMetrics.{$account->id}.investedAmount", null) ) ); }); test('account show includes bank information', function () { $bank = Bank::factory()->create([ 'name' => 'Test Bank', 'logo' => 'https://example.com/logo.png', ]); $account = Account::factory()->create([ 'user_id' => $this->user->id, 'bank_id' => $bank->id, ]); $response = $this->get(route('accounts.show', $account)); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Show') ->has('account.bank') ->where('account.bank.name', 'Test Bank') ); }); test('real estate account show includes real estate detail with linked loan', function () { $loanAccount = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Loan, ]); $realEstateAccount = Account::factory()->realEstate()->create([ 'user_id' => $this->user->id, ]); RealEstateDetail::factory()->create([ 'account_id' => $realEstateAccount->id, 'linked_loan_account_id' => $loanAccount->id, ]); $response = $this->withoutVite()->get(route('accounts.show', $realEstateAccount)); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Show') ->has('account.real_estate_detail') ->where('account.real_estate_detail.linked_loan_account_id', $loanAccount->id) ->has('account.real_estate_detail.linked_loan_account') ->where('account.real_estate_detail.linked_loan_account.id', $loanAccount->id) ->has('account.available_loan_accounts') ); }); test('real estate account show includes current market value and loan balance for equity', function () { $loanAccount = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Loan, ]); AccountBalance::factory()->create([ 'account_id' => $loanAccount->id, 'balance_date' => now(), 'balance' => 20000000, // $200,000 ]); $realEstateAccount = Account::factory()->realEstate()->create([ 'user_id' => $this->user->id, ]); AccountBalance::factory()->create([ 'account_id' => $realEstateAccount->id, 'balance_date' => now(), 'balance' => 35000000, // $350,000 ]); RealEstateDetail::factory()->create([ 'account_id' => $realEstateAccount->id, 'linked_loan_account_id' => $loanAccount->id, ]); $response = $this->withoutVite()->get(route('accounts.show', $realEstateAccount)); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Show') ->where('account.real_estate_detail.current_market_value', 35000000) ->where('account.real_estate_detail.current_loan_balance', 20000000) ); }); test('real estate account show includes market value without linked loan', function () { $realEstateAccount = Account::factory()->realEstate()->create([ 'user_id' => $this->user->id, ]); AccountBalance::factory()->create([ 'account_id' => $realEstateAccount->id, 'balance_date' => now(), 'balance' => 35000000, ]); RealEstateDetail::factory()->create([ 'account_id' => $realEstateAccount->id, ]); $response = $this->withoutVite()->get(route('accounts.show', $realEstateAccount)); $response->assertOk() ->assertInertia(fn ($page) => $page ->component('Accounts/Show') ->where('account.real_estate_detail.current_market_value', 35000000) ->missing('account.real_estate_detail.current_loan_balance') ); }); test('real estate balance evolution includes mortgage balance data', function () { $loanAccount = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Loan, 'currency_code' => 'EUR', ]); AccountBalance::factory()->create([ 'account_id' => $loanAccount->id, 'balance_date' => now()->subMonthNoOverflow()->endOfMonth(), 'balance' => 22000000, ]); AccountBalance::factory()->create([ 'account_id' => $loanAccount->id, 'balance_date' => now()->endOfMonth(), 'balance' => 21500000, ]); $realEstateAccount = Account::factory()->realEstate()->create([ 'user_id' => $this->user->id, 'currency_code' => 'EUR', ]); AccountBalance::factory()->create([ 'account_id' => $realEstateAccount->id, 'balance_date' => now()->subMonthNoOverflow()->endOfMonth(), 'balance' => 35000000, ]); AccountBalance::factory()->create([ 'account_id' => $realEstateAccount->id, 'balance_date' => now()->endOfMonth(), 'balance' => 36000000, ]); RealEstateDetail::factory()->create([ 'account_id' => $realEstateAccount->id, 'linked_loan_account_id' => $loanAccount->id, 'revaluation_percentage' => null, ]); $response = $this->getJson('/api/dashboard/account/'.$realEstateAccount->id.'/balance-evolution?'.http_build_query([ 'from' => now()->subMonthsNoOverflow(2)->startOfMonth()->toDateString(), 'to' => now()->endOfMonth()->toDateString(), ])); $response->assertOk(); $data = $response->json(); expect($data['data'])->toHaveCount(3); expect($data['data'][0])->toHaveKey('mortgage_balance'); // The last point should have the most recent mortgage balance $lastPoint = end($data['data']); expect($lastPoint['mortgage_balance'])->toBe(21500000); expect($lastPoint['value'])->toBe(36000000); }); test('real estate balance evolution without linked loan has no mortgage data', function () { $realEstateAccount = Account::factory()->realEstate()->create([ 'user_id' => $this->user->id, ]); AccountBalance::factory()->create([ 'account_id' => $realEstateAccount->id, 'balance_date' => now()->endOfMonth(), 'balance' => 35000000, ]); RealEstateDetail::factory()->create([ 'account_id' => $realEstateAccount->id, ]); $response = $this->getJson('/api/dashboard/account/'.$realEstateAccount->id.'/balance-evolution?'.http_build_query([ 'from' => now()->subMonthsNoOverflow(2)->startOfMonth()->toDateString(), 'to' => now()->endOfMonth()->toDateString(), ])); $response->assertOk(); $data = $response->json(); expect($data['data'][0])->not->toHaveKey('mortgage_balance'); }); test('non-real-estate account balance evolution has no mortgage data', function () { $account = Account::factory()->create([ 'user_id' => $this->user->id, 'type' => AccountType::Checking, ]); AccountBalance::factory()->create([ 'account_id' => $account->id, 'balance_date' => now()->endOfMonth(), 'balance' => 100000, ]); $response = $this->getJson('/api/dashboard/account/'.$account->id.'/balance-evolution?'.http_build_query([ 'from' => now()->subMonthsNoOverflow(2)->startOfMonth()->toDateString(), 'to' => now()->endOfMonth()->toDateString(), ])); $response->assertOk(); $data = $response->json(); expect($data['data'][0])->not->toHaveKey('mortgage_balance'); });