withoutMiddleware(EnsurePlatformSession::class); $this->owner = User::create([ 'public_id' => 'care-modules-owner', 'name' => 'Owner', 'email' => 'modules-owner@example.com', ]); $this->organization = Organization::create([ 'owner_ref' => $this->owner->public_id, 'name' => 'Modules Clinic', 'slug' => 'modules-clinic', 'settings' => [ 'onboarded' => true, 'facility_type' => 'clinic', 'plan' => 'pro', 'plan_expires_at' => now()->addMonth()->toIso8601String(), ], ]); Member::create([ 'owner_ref' => $this->owner->public_id, 'organization_id' => $this->organization->id, 'user_ref' => $this->owner->public_id, 'role' => 'hospital_admin', ]); $this->branch = Branch::create([ 'owner_ref' => $this->owner->public_id, 'organization_id' => $this->organization->id, 'name' => 'Main', 'is_active' => true, ]); } public function test_activating_dentistry_creates_department_and_queue_stubs(): void { Http::fake(); $service = app(SpecialtyModuleService::class); $service->activate($this->organization, $this->owner->public_id, 'dentistry'); $this->organization->refresh(); $this->assertTrue((bool) data_get($this->organization->settings, 'specialty_modules.dentistry')); $this->assertDatabaseHas('care_departments', [ 'branch_id' => $this->branch->id, 'type' => 'dental', 'name' => 'Dentistry', 'is_active' => true, ]); $stubs = data_get($this->organization->settings, 'specialty_module_provisioning.dentistry.queues'); $this->assertIsArray($stubs); $this->assertNotEmpty($stubs); $this->assertSame('Dentistry', $stubs[0]['name']); $this->assertSame($this->branch->id, $stubs[0]['branch_id']); } public function test_activating_with_queue_integration_creates_real_counters(): void { config([ 'care.queue.api_url' => 'https://queue.test/api/v1', 'care.queue.api_key' => 'care-test-key', ]); $this->organization->update([ 'settings' => array_merge($this->organization->settings ?? [], [ 'queue_integration_enabled' => true, ]), ]); $queueUuid = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'; $counterUuid = 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb'; Http::fake([ 'https://queue.test/api/v1/integrations/status*' => Http::response([ 'data' => [ 'provisioned' => true, 'integrations' => ['care' => true], ], ], 200), 'https://queue.test/api/v1/branches*' => Http::sequence() ->push(['data' => ['id' => 1, 'name' => 'Main', 'is_active' => true]], 200) ->push(['data' => ['id' => 1, 'name' => 'Main', 'is_active' => true]], 200), 'https://queue.test/api/v1/queues*' => Http::response([ 'data' => [ 'uuid' => $queueUuid, 'name' => 'Dentistry', 'prefix' => 'DEN', 'external_key' => 'care:specialty:dentistry:queue:'.$this->branch->id, ], ], 201), 'https://queue.test/api/v1/counters*' => Http::response([ 'data' => [ 'uuid' => $counterUuid, 'name' => 'Dentistry counter', 'branch' => 'Main', 'queues' => [['uuid' => $queueUuid, 'name' => 'Dentistry']], ], ], 201), ]); app(SpecialtyModuleService::class)->activate( $this->organization->fresh(), $this->owner->public_id, 'dentistry', ); $this->organization->refresh(); $stub = data_get($this->organization->settings, 'specialty_module_provisioning.dentistry.queues.0'); $this->assertTrue((bool) ($stub['synced'] ?? false)); $this->assertSame($queueUuid, $stub['queue_uuid'] ?? null); $this->assertSame($counterUuid, $stub['counter_uuid'] ?? null); Http::assertSent(function ($request) { return $request->method() === 'POST' && str_contains($request->url(), '/queues') && ($request['external_key'] ?? null) === 'care:specialty:dentistry:queue:'.$this->branch->id; }); } public function test_reactivation_is_idempotent_for_queue_external_keys(): void { config([ 'care.queue.api_url' => 'https://queue.test/api/v1', 'care.queue.api_key' => 'care-test-key', ]); $this->organization->update([ 'settings' => array_merge($this->organization->settings ?? [], [ 'queue_integration_enabled' => true, 'specialty_module_provisioning' => [ 'dentistry' => [ 'active' => false, 'queues' => [[ 'module' => 'dentistry', 'branch_id' => $this->branch->id, 'branch_name' => 'Main', 'name' => 'Dentistry', 'prefix' => 'DEN', 'active' => false, 'synced' => true, 'queue_uuid' => 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'counter_uuid' => 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'queue_external_key' => 'care:specialty:dentistry:queue:'.$this->branch->id, 'counter_external_key' => 'care:specialty:dentistry:counter:'.$this->branch->id, ]], ], ], ]), ]); Http::fake([ 'https://queue.test/api/v1/integrations/status*' => Http::response([ 'data' => ['provisioned' => true, 'integrations' => ['care' => true]], ], 200), 'https://queue.test/api/v1/branches*' => Http::response([ 'data' => ['id' => 1, 'name' => 'Main', 'is_active' => true], ], 200), 'https://queue.test/api/v1/queues*' => Http::response([ 'data' => [ 'uuid' => 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'name' => 'Dentistry', 'external_key' => 'care:specialty:dentistry:queue:'.$this->branch->id, ], ], 200), 'https://queue.test/api/v1/counters*' => Http::response([ 'data' => [ 'uuid' => 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'name' => 'Dentistry counter', ], ], 200), ]); app(SpecialtyModuleService::class)->activate( $this->organization->fresh(), $this->owner->public_id, 'dentistry', ); $this->organization->refresh(); $stub = data_get($this->organization->settings, 'specialty_module_provisioning.dentistry.queues.0'); $this->assertSame('care:specialty:dentistry:queue:'.$this->branch->id, $stub['queue_external_key']); $this->assertSame('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', $stub['queue_uuid']); } public function test_deactivating_hides_department_without_deleting(): void { Http::fake(); $service = app(SpecialtyModuleService::class); $service->activate($this->organization, $this->owner->public_id, 'dentistry'); $department = Department::query()->where('type', 'dental')->firstOrFail(); $service->deactivate($this->organization->fresh(), $this->owner->public_id, 'dentistry'); $this->organization->refresh(); $this->assertFalse((bool) data_get($this->organization->settings, 'specialty_modules.dentistry')); $this->assertFalse($department->fresh()->is_active); $this->assertDatabaseHas('care_departments', ['id' => $department->id]); } public function test_free_plan_cannot_activate_modules_via_settings(): void { $this->organization->update([ 'settings' => array_merge($this->organization->settings ?? [], [ 'plan' => 'free', 'plan_expires_at' => null, ]), ]); $this->actingAs($this->owner) ->put(route('care.settings.modules.update'), [ 'modules' => ['dentistry' => '1'], ]) ->assertRedirect(route('care.pro.index')) ->assertSessionHas('upsell'); } public function test_modules_settings_page_lists_catalog(): void { $this->actingAs($this->owner) ->get(route('care.settings.modules')) ->assertOk() ->assertSee('Dentistry') ->assertSee('Eye care') ->assertSee('Physiotherapy') ->assertSee('Maternity') ->assertSee('Radiology'); } public function test_specialty_page_visible_when_module_enabled(): void { Http::fake(); app(SpecialtyModuleService::class)->activate($this->organization, $this->owner->public_id, 'dentistry'); $this->actingAs($this->owner) ->get(route('care.specialty.show', 'dentistry')) ->assertOk() ->assertSee('Dentistry') ->assertSee('Departments'); } public function test_unassigned_doctor_cannot_open_specialty_module(): void { Http::fake(); app(SpecialtyModuleService::class)->activate($this->organization, $this->owner->public_id, 'dentistry'); $doctor = User::create([ 'public_id' => 'unassigned-doc', 'name' => 'General Doctor', 'email' => 'general-doc@example.com', ]); Member::create([ 'owner_ref' => $this->owner->public_id, 'organization_id' => $this->organization->id, 'user_ref' => $doctor->public_id, 'role' => 'doctor', 'branch_id' => $this->branch->id, ]); $this->actingAs($doctor) ->get(route('care.specialty.show', 'dentistry')) ->assertForbidden(); } public function test_assigned_specialty_doctor_can_open_module(): void { Http::fake(); app(SpecialtyModuleService::class)->activate($this->organization, $this->owner->public_id, 'dentistry'); $department = Department::query()->where('type', 'dental')->firstOrFail(); $doctor = User::create([ 'public_id' => 'dental-doc', 'name' => 'Dental Doctor', 'email' => 'dental-doc@example.com', ]); $member = Member::create([ 'owner_ref' => $this->owner->public_id, 'organization_id' => $this->organization->id, 'user_ref' => $doctor->public_id, 'role' => 'doctor', 'branch_id' => $this->branch->id, ]); \App\Models\Practitioner::create([ 'owner_ref' => $this->owner->public_id, 'organization_id' => $this->organization->id, 'branch_id' => $this->branch->id, 'department_id' => $department->id, 'member_id' => $member->id, 'user_ref' => $doctor->public_id, 'name' => 'Dental Doctor', 'specialty' => 'Dentistry', 'is_active' => true, ]); $this->actingAs($doctor) ->get(route('care.specialty.show', 'dentistry')) ->assertOk() ->assertSee('Dentistry'); $this->assertTrue( app(SpecialtyModuleService::class)->memberCanAccess( $this->organization->fresh(), $member, 'dentistry', ), ); $this->assertFalse( app(SpecialtyModuleService::class)->memberCanAccess( $this->organization->fresh(), $member, 'maternity', ), ); } }