Files
ladill-queue/tests/Feature/IndustryPackageTest.php
T
isaaccladandCursor a2d5a8affd
Deploy Ladill Queue / deploy (push) Successful in 1m5s
Require package reinstall after industry change and wipe old queues.
Changing industry now flags a pending reinstall with an app banner; reinstall clears queues, tickets, and service points before applying the new template.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-17 20:10:25 +00:00

302 lines
12 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Branch;
use App\Models\Counter;
use App\Models\ServiceQueue;
use App\Models\User;
use App\Models\Workflow;
use App\Services\Qms\Industry\IndustryPackageInstaller;
use App\Services\Qms\Industry\IndustryPackageRegistry;
use App\Services\Qms\OrganizationResolver;
use App\Services\Qms\QueueEngine;
use App\Services\Qms\VoiceAnnouncementService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class IndustryPackageTest extends TestCase
{
use RefreshDatabase;
public function test_registry_lists_core_packages(): void
{
$registry = app(IndustryPackageRegistry::class);
$this->assertTrue($registry->has('healthcare'));
$this->assertTrue($registry->has('banking'));
$this->assertTrue($registry->has('restaurant'));
$this->assertTrue($registry->has('custom'));
$this->assertSame('Healthcare', $registry->get('healthcare')->label());
$this->assertSame('patient', $registry->get('healthcare')->ticketEntity());
$this->assertSame('order', $registry->get('restaurant')->ticketEntity());
}
public function test_onboarding_banking_provisions_stages_and_workflow(): void
{
$user = User::create([
'public_id' => 'pkg-bank-owner',
'name' => 'Bank Owner',
'email' => 'bank-owner@example.com',
'password' => bcrypt('password'),
]);
$org = app(OrganizationResolver::class)->completeOnboarding($user, [
'organization_name' => 'Ridge Bank',
'industry' => 'banking',
'appointment_mode' => 'hybrid',
'branch_name' => 'Main Branch',
'timezone' => 'UTC',
]);
$this->assertSame('banking', data_get($org->settings, 'industry'));
$this->assertSame('banking', data_get($org->settings, 'industry_package.key'));
$this->assertSame('customer', data_get($org->settings, 'industry_package.ticket_entity'));
$branch = Branch::where('organization_id', $org->id)->first();
$this->assertNotNull($branch);
$queues = ServiceQueue::where('organization_id', $org->id)->where('branch_id', $branch->id)->get();
$this->assertGreaterThanOrEqual(3, $queues->count());
$this->assertTrue($queues->contains(fn (ServiceQueue $q) => $q->prefix === 'T'));
$teller = $queues->firstWhere('prefix', 'T');
$this->assertSame('shared_pool', $teller->routingMode());
$this->assertGreaterThanOrEqual(1, $teller->counters()->count());
$workflow = Workflow::where('organization_id', $org->id)->where('industry_template', 'banking')->first();
$this->assertNotNull($workflow);
$this->assertGreaterThanOrEqual(3, $workflow->steps()->count());
}
public function test_healthcare_uses_assigned_only_for_consultation(): void
{
$user = User::create([
'public_id' => 'pkg-care-owner',
'name' => 'Clinic Owner',
'email' => 'clinic-owner@example.com',
'password' => bcrypt('password'),
]);
// Pro plan so all healthcare stages fit under queue caps.
$org = app(OrganizationResolver::class)->completeOnboarding($user, [
'organization_name' => 'Ridge Clinic',
'industry' => 'custom',
'appointment_mode' => 'hybrid',
'branch_name' => 'Main',
'timezone' => 'UTC',
]);
$org->update([
'settings' => array_merge($org->settings ?? [], [
'plan' => 'pro',
'plan_expires_at' => now()->addMonth()->toIso8601String(),
]),
]);
$branch = Branch::where('organization_id', $org->id)->first();
$result = app(IndustryPackageInstaller::class)->install($org->fresh(), $branch, 'healthcare');
$this->assertSame('healthcare', $result['package']);
$this->assertGreaterThanOrEqual(5, $result['stages']);
$consultation = ServiceQueue::query()
->where('organization_id', $org->id)
->where('settings->stage_code', 'consultation')
->first();
$this->assertNotNull($consultation);
$this->assertSame('assigned_only', $consultation->routingMode());
$this->assertSame('C', $consultation->prefix);
}
public function test_package_install_is_idempotent(): void
{
$user = User::create([
'public_id' => 'pkg-idem-owner',
'name' => 'Idem Owner',
'email' => 'idem@example.com',
'password' => bcrypt('password'),
]);
$org = app(OrganizationResolver::class)->completeOnboarding($user, [
'organization_name' => 'Idem Org',
'industry' => 'visitor',
'appointment_mode' => 'hybrid',
'branch_name' => 'Main',
'timezone' => 'UTC',
]);
$branch = Branch::where('organization_id', $org->id)->first();
$installer = app(IndustryPackageInstaller::class);
$first = $installer->install($org, $branch, 'visitor');
$second = $installer->install($org->fresh(), $branch, 'visitor');
$this->assertSame($first['stages'], $second['stages']);
$this->assertSame(
ServiceQueue::where('organization_id', $org->id)->count(),
ServiceQueue::where('organization_id', $org->id)->count(),
);
$this->assertSame(2, ServiceQueue::where('organization_id', $org->id)->count());
}
public function test_industry_announcement_uses_package_copy(): void
{
$user = User::create([
'public_id' => 'pkg-announce-owner',
'name' => 'Announce Owner',
'email' => 'announce@example.com',
'password' => bcrypt('password'),
]);
$org = app(OrganizationResolver::class)->completeOnboarding($user, [
'organization_name' => 'Kitchen Co',
'industry' => 'restaurant',
'appointment_mode' => 'walk_in_only',
'branch_name' => 'Main',
'timezone' => 'UTC',
]);
$queue = ServiceQueue::where('organization_id', $org->id)->where('prefix', 'U')->first()
?? ServiceQueue::where('organization_id', $org->id)->first();
$counter = $queue->counters()->first() ?? Counter::where('organization_id', $org->id)->first();
$ticket = app(QueueEngine::class)->issueTicket($queue, $user->public_id, [
'source' => 'api',
'customer_name' => 'Table 4',
]);
$announcement = app(VoiceAnnouncementService::class)->announceCalled($ticket->fresh(['organization', 'serviceQueue']), $counter);
$this->assertStringContainsString($ticket->ticket_number, $announcement->message);
$this->assertTrue(
str_contains(strtolower($announcement->message), 'order')
|| str_contains(strtolower($announcement->message), 'pickup')
|| str_contains($announcement->message, $counter->displayDestination()),
);
}
public function test_care_integration_skips_stage_materialization(): void
{
$user = User::create([
'public_id' => 'pkg-care-skip',
'name' => 'Care Skip',
'email' => 'care-skip@example.com',
'password' => bcrypt('password'),
]);
$org = app(OrganizationResolver::class)->completeOnboarding($user, [
'organization_name' => 'Care Linked',
'industry' => 'custom',
'appointment_mode' => 'hybrid',
'branch_name' => 'Main',
'timezone' => 'UTC',
]);
$org->update([
'settings' => array_merge($org->settings ?? [], [
'integrations' => ['care_enabled' => true],
]),
]);
$before = ServiceQueue::where('organization_id', $org->id)->count();
$branch = Branch::where('organization_id', $org->id)->first();
$result = app(IndustryPackageInstaller::class)->install($org->fresh(), $branch, 'healthcare');
$this->assertSame('stages_managed_by_integration', $result['skipped_reason']);
$this->assertSame(0, $result['stages']);
$this->assertSame($before, ServiceQueue::where('organization_id', $org->id)->count());
$this->assertSame('patient', data_get($org->fresh()->settings, 'industry_package.ticket_entity'));
}
public function test_replace_install_clears_previous_queues_and_tickets(): void
{
$user = User::create([
'public_id' => 'pkg-replace-owner',
'name' => 'Replace Owner',
'email' => 'replace-owner@example.com',
'password' => bcrypt('password'),
]);
$org = app(OrganizationResolver::class)->completeOnboarding($user, [
'organization_name' => 'Cafe',
'industry' => 'restaurant',
'appointment_mode' => 'hybrid',
'branch_name' => 'Main',
'timezone' => 'UTC',
]);
$org->update([
'settings' => array_merge($org->settings ?? [], [
'plan' => 'pro',
'plan_expires_at' => now()->addMonth()->toIso8601String(),
]),
]);
$branch = Branch::where('organization_id', $org->id)->first();
$oldQueue = ServiceQueue::where('organization_id', $org->id)->first();
$this->assertNotNull($oldQueue);
app(QueueEngine::class)->issueTicket($oldQueue, $user->public_id, [
'customer_name' => 'Old Ticket',
'source' => 'reception',
]);
$this->assertGreaterThan(0, \App\Models\Ticket::where('organization_id', $org->id)->count());
$result = app(IndustryPackageInstaller::class)->install($org->fresh(), $branch, 'banking', [
'replace' => true,
'include_optional' => true,
]);
$this->assertTrue($result['replaced']);
$this->assertSame(0, \App\Models\Ticket::where('organization_id', $org->id)->count());
$this->assertFalse(
ServiceQueue::where('organization_id', $org->id)->where('id', $oldQueue->id)->exists()
);
$this->assertSame('banking', data_get($org->fresh()->settings, 'industry_package.key'));
$this->assertNull(data_get($org->fresh()->settings, 'industry_package_needs_reinstall'));
$this->assertGreaterThanOrEqual(3, ServiceQueue::where('organization_id', $org->id)->count());
}
public function test_changing_industry_requires_reinstall_without_auto_provision(): void
{
$this->withoutMiddleware(\App\Http\Middleware\EnsurePlatformSession::class);
$user = User::create([
'public_id' => 'pkg-settings-owner',
'name' => 'Settings Owner',
'email' => 'settings-pkg@example.com',
'password' => bcrypt('password'),
]);
$org = app(OrganizationResolver::class)->completeOnboarding($user, [
'organization_name' => 'Shop',
'industry' => 'restaurant',
'appointment_mode' => 'hybrid',
'branch_name' => 'Main',
'timezone' => 'UTC',
]);
$beforeQueues = ServiceQueue::where('organization_id', $org->id)->count();
$this->actingAs($user)
->put(route('qms.settings.update'), [
'name' => $org->name,
'timezone' => $org->timezone,
'appointment_mode' => 'hybrid',
'industry' => 'banking',
'notifications_enabled' => '1',
])
->assertRedirect();
$org->refresh();
$this->assertSame('banking', data_get($org->settings, 'industry'));
$this->assertSame('restaurant', data_get($org->settings, 'industry_package.key'));
$this->assertTrue((bool) data_get($org->settings, 'industry_package_needs_reinstall'));
$this->assertTrue(IndustryPackageInstaller::needsReinstall($org));
$this->assertSame($beforeQueues, ServiceQueue::where('organization_id', $org->id)->count());
$this->actingAs($user)
->get(route('qms.dashboard'))
->assertOk()
->assertSee('Reinstall industry package required');
}
}