Files
clinicpro/tests/Appointment/AvailabilityEngineTest.php
T
hamedandClaude Opus 5 24534ec483 feat(availability): multi-resource availability engine
Section 10 of the design document, and the payoff for tasks 01–05. The engine slides
a multi-segment plan across resource calendars and answers which times are actually
possible, with a suggested resource for each role. Until now the only conflict the
system checked was the doctor's; rooms, devices and operators did not exist.

Allocation is per *role*, not per segment, and that is what returns the wasted
capacity. An operator with no requirement during "waiting for the cream" is simply
not examined for those minutes, so another patient can use them. The reference test
encodes exactly that: patient A holds 10:00–11:00 while the operator is only busy
10:00–10:05 and 10:35–11:00, and patient B is offered a slot inside the gap with the
second room assigned. The spec says the task is not verified without that scenario.

One resource is chosen for every segment that needs its role, not independently per
segment — otherwise the operator in segment 1 and segment 3 could be two different
people and the patient would change hands mid-treatment.

Occupancy is stored one row per (segment × resource) rather than one per appointment.
The granularity is the whole point; a row per appointment would re-create the
single-interval model the design rejects. Reserved intervals are widened by each
resource's setup/cleanup, because the resource genuinely is not available then.

booking_mode gains a third value, resource, alongside slot and service. It is purely
additive: the default stays slot, no environment moves on its own, and a location
that has not opted in keeps the untouched legacy path. The frozen slot-mode contract
stays green.

Performance is a test, not a hope: 30 days, 20 resources and 500 existing bookings
complete well inside the 500ms budget. Every input is read once and the rest is in
memory — no query inside the day or candidate loop — and candidates are generated
only from the free windows of the scarcest role, which turns tens of thousands of
candidates into a few hundred.

An empty result is not an error and not a 404: it carries
reason: "no_capacity_in_range" so the caller does not have to infer meaning from
emptiness.

Also fixed a genuinely intermittent test defect: NumericFieldNormalizerTest padded a
random number with the three-byte Persian "۰" using byte-based str_pad, producing
broken UTF-8 whenever the number was short. It failed roughly at random. The improved
assertion message added earlier is what identified it immediately.

1196 tests / 3414 assertions. phpstan at its 14-error baseline.

Resource-picking strategies, the availability cache and the settings UI are recorded
as outstanding in the checklist with reasons — the cache in particular would be
premature while the performance test passes comfortably without it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-30 21:21:33 +03:30

380 lines
17 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Tests\Appointment;
use App\Appointment\Availability\Entity\ResourceOccupancy;
use App\Auth\Entity\User;
use App\Clinic\Entity\Clinic;
use App\ClinicService\Entity\ServiceItem;
use App\ClinicService\Entity\ServiceSection;
use App\Doctor\Entity\DoctorAddress;
use App\Resource\Entity\ClinicResource;
use App\Resource\Entity\ResourceType;
use App\Tests\ApiTestCase;
/**
* موتور جستجوی وقت چندمنبعی — بند ۱۰ مستند.
*/
class AvailabilityEngineTest extends ApiTestCase
{
private const TEHRAN = 'Asia/Tehran';
private function nextSaturday(): int
{
return (new \DateTimeImmutable('next saturday', new \DateTimeZone(self::TEHRAN)))
->setTime(0, 0)
->getTimestamp();
}
/** @return array{0: User, 1: ServiceSection, 2: DoctorAddress} */
private function clinicWithBranch(): array
{
$user = $this->createUser(['ROLE_USER', 'ROLE_CLINIC']);
$clinic = new Clinic($user);
$clinic->setName('کلینیک موتور');
$this->em->persist($clinic);
$this->em->flush();
$section = new ServiceSection('clinic', $clinic->getId(), 'لیزر');
$this->em->persist($section);
$address = DoctorAddress::forClinic($clinic->getId());
$address->setName('شعبهٔ مرکزی');
$this->em->persist($address);
$this->em->flush();
return [$user, $section, $address];
}
private function service(ServiceSection $section, string $name, int $solo = 20): ServiceItem
{
// هر درخواست HTTP کرنل را از نو می‌سازد، پس نمونهٔ قبلی detached شده است؛
// بدون این، ساختن سرویس دوم با «A new entity was found» می‌شکند.
$section = $this->em->getRepository(ServiceSection::class)->find($section->getId());
$item = new ServiceItem($section, $name);
$item->setSoloDurationMinutes($solo);
$this->em->persist($item);
$this->em->flush();
return $item;
}
private function type(DoctorAddress $address, string $code, string $name): ResourceType
{
$type = new ResourceType($address->tenantEntityType(), $address->tenantEntityId(), $code, $name);
$this->em->persist($type);
$this->em->flush();
return $type;
}
/** منبع با شیفت شنبه تا جمعه ۰۹:۰۰–۱۷:۰۰. */
private function resourceWithShift(User $user, DoctorAddress $address, ResourceType $type, string $name): array
{
$created = $this->authJson('POST', '/api/v1/resource', $user, [
'address_uuid' => $address->getUuid(),
'type_uuid' => $type->getUuid(),
'name' => $name,
]);
self::assertSame(201, $this->responseCode(), json_encode($created, JSON_UNESCAPED_UNICODE));
$this->authJson('PUT', "/api/v1/resource/{$created['data']['uuid']}/calendar", $user, [
'days' => array_fill_keys(range(0, 6), [['start_minute' => 540, 'end_minute' => 1020]]),
]);
self::assertSame(200, $this->responseCode());
return $created['data'];
}
/** @param list<array<string, mixed>> $segments */
private function setSegments(User $user, ServiceItem $service, array $segments): void
{
$this->authJson('PUT', "/api/v1/service-item/{$service->getUuid()}/segments", $user, ['segments' => $segments]);
self::assertSame(200, $this->responseCode());
}
/** @param array<string, mixed> $extra */
private function search(User $user, ServiceItem $service, DoctorAddress $address, int $from, int $to, array $extra = []): array
{
return $this->authJson('POST', '/api/v1/appointment-availability', $user, $extra + [
'service_uuid' => $service->getUuid(),
'branch_uuid' => $address->getUuid(),
'from' => $from,
'to' => $to,
]);
}
private function occupy(string $resourceUuid, int $start, int $end): void
{
$resource = $this->em->getRepository(ClinicResource::class)->findOneBy(['uuid' => $resourceUuid]);
$this->em->persist(new ResourceOccupancy($resource, $start, $end));
$this->em->flush();
}
/** سناریوی مستند: چهار بخش، سه اتاق، دو اپراتور، سه دستگاه. */
public function testDocumentScenarioReturnsSlotsWithAssignments(): void
{
[$user, $section, $address] = $this->clinicWithBranch();
$service = $this->service($section, 'لیزر', 20);
$room = $this->type($address, 'room', 'اتاق');
$operator = $this->type($address, 'operator', 'اپراتور');
$device = $this->type($address, 'device', 'دستگاه');
foreach (range(1, 3) as $n) { $this->resourceWithShift($user, $address, $room, "اتاق $n"); }
foreach (range(1, 2) as $n) { $this->resourceWithShift($user, $address, $operator, "اپراتور $n"); }
foreach (range(1, 3) as $n) { $this->resourceWithShift($user, $address, $device, "لیزر $n"); }
$roomReq = ['type_uuid' => $room->getUuid()];
$opReq = ['type_uuid' => $operator->getUuid()];
$devReq = ['type_uuid' => $device->getUuid()];
$this->setSegments($user, $service, [
['sequence' => 1, 'name' => 'بی‌حسی', 'duration_minutes' => 5, 'requirements' => [$roomReq, $opReq]],
['sequence' => 2, 'name' => 'انتظار', 'duration_minutes' => 30, 'requirements' => [$roomReq]],
['sequence' => 3, 'name' => 'لیزر', 'duration_minutes' => 20, 'requirements' => [$roomReq, $opReq, $devReq]],
['sequence' => 4, 'name' => 'مراقبت', 'duration_minutes' => 5, 'requirements' => [$roomReq, $opReq]],
]);
$saturday = $this->nextSaturday();
$body = $this->search($user, $service, $address, $saturday, $saturday);
self::assertSame(200, $this->responseCode(), json_encode($body, JSON_UNESCAPED_UNICODE));
self::assertSame(60, $body['data']['plan']['total_minutes']);
self::assertNotEmpty($body['data']['slots']);
$first = $body['data']['slots'][0];
self::assertArrayHasKey('room', $first['assignment']);
self::assertArrayHasKey('operator', $first['assignment']);
self::assertArrayHasKey('device', $first['assignment']);
self::assertCount(1, $first['assignment']['room']);
}
/**
* ⭐ قلب کل پروژه: بیمار الف ۱۰:۰۰–۱۱:۰۰ نوبت دارد ولی اپراتور فقط ۱۰:۰۰–۱۰:۰۵ و
* ۱۰:۳۵–۱۱:۰۰ درگیر است. با اتاق دوم، بیمار ب باید در همان بازهٔ میانی جا شود.
*
* بدون این سناریو، کل تسک تأیید نمی‌شود.
*/
public function testOperatorFreedDuringWaitingIsReusedForAnotherPatient(): void
{
[$user, $section, $address] = $this->clinicWithBranch();
$service = $this->service($section, 'لیزر', 20);
$room = $this->type($address, 'room', 'اتاق');
$operator = $this->type($address, 'operator', 'اپراتور');
$roomA = $this->resourceWithShift($user, $address, $room, 'اتاق ۱');
$roomB = $this->resourceWithShift($user, $address, $room, 'اتاق ۲');
$op = $this->resourceWithShift($user, $address, $operator, 'اپراتور تنها');
// سرویس کوتاه: ۵ دقیقه، فقط اتاق و اپراتور.
$short = $this->service($section, 'مشاورهٔ کوتاه', 5);
$this->setSegments($user, $short, [
['sequence' => 1, 'name' => 'مشاوره', 'duration_minutes' => 5, 'requirements' => [
['type_uuid' => $room->getUuid()], ['type_uuid' => $operator->getUuid()],
]],
]);
$saturday = $this->nextSaturday();
$ten = $saturday + 10 * 3600;
// بیمار الف: اتاق ۱ کل ساعت گرفته، اپراتور فقط دو سرِ آن.
$this->occupy($roomA['uuid'], $ten, $ten + 3600);
$this->occupy($op['uuid'], $ten, $ten + 5 * 60);
$this->occupy($op['uuid'], $ten + 35 * 60, $ten + 3600);
$body = $this->search($user, $short, $address, $saturday, $saturday, ['step_minutes' => 5]);
self::assertSame(200, $this->responseCode());
$starts = array_column($body['data']['slots'], 'start');
// بازهٔ آزادِ اپراتور: ۱۰:۰۵ تا ۱۰:۳۵ — یک نوبت پنج‌دقیقه‌ای آنجا جا می‌شود.
$inGap = array_filter(
$starts,
static fn (int $s): bool => $s >= $ten + 5 * 60 && $s + 5 * 60 <= $ten + 35 * 60,
);
self::assertNotEmpty($inGap, 'اپراتورِ آزادشده در «انتظار» باید دوباره قابل استفاده باشد');
// و اتاق پیشنهادی باید اتاق ۲ باشد، چون اتاق ۱ کل ساعت گرفته است.
foreach ($body['data']['slots'] as $slot) {
if ($slot['start'] >= $ten + 5 * 60 && $slot['start'] + 300 <= $ten + 35 * 60) {
self::assertSame($roomB['uuid'], $slot['assignment']['room'][0]['uuid']);
break;
}
}
}
/** یک منبع برای همهٔ بخش‌هایی که آن نقش را می‌خواهند — نه دو نفر. */
public function testSameResourceIsUsedAcrossNonAdjacentSegments(): void
{
[$user, $section, $address] = $this->clinicWithBranch();
$service = $this->service($section, 'لیزر', 20);
$room = $this->type($address, 'room', 'اتاق');
$operator = $this->type($address, 'operator', 'اپراتور');
$this->resourceWithShift($user, $address, $room, 'اتاق ۱');
foreach (range(1, 3) as $n) { $this->resourceWithShift($user, $address, $operator, "اپراتور $n"); }
$this->setSegments($user, $service, [
['sequence' => 1, 'name' => 'بخش اول', 'duration_minutes' => 5, 'requirements' => [['type_uuid' => $operator->getUuid()]]],
['sequence' => 2, 'name' => 'انتظار', 'duration_minutes' => 30, 'requirements' => [['type_uuid' => $room->getUuid()]]],
['sequence' => 3, 'name' => 'بخش سوم', 'duration_minutes' => 10, 'requirements' => [['type_uuid' => $operator->getUuid()]]],
]);
$saturday = $this->nextSaturday();
$body = $this->search($user, $service, $address, $saturday, $saturday);
self::assertNotEmpty($body['data']['slots']);
self::assertCount(
1,
$body['data']['slots'][0]['assignment']['operator'],
'یک اپراتور برای هر دو بخش، نه دو نفر',
);
}
/** ظرفیت ۳: سه نوبت هم‌زمان جا دارد، چهارمی نه. */
public function testCapacityIsCountedNotJustPresence(): void
{
[$user, $section, $address] = $this->clinicWithBranch();
$short = $this->service($section, 'تزریق', 10);
$room = $this->type($address, 'room', 'اتاق');
$created = $this->authJson('POST', '/api/v1/resource', $user, [
'address_uuid' => $address->getUuid(),
'type_uuid' => $room->getUuid(),
'name' => 'اتاق سه‌تخته',
'capacity' => 3,
]);
self::assertSame(201, $this->responseCode());
$this->authJson('PUT', "/api/v1/resource/{$created['data']['uuid']}/calendar", $user, [
'days' => array_fill_keys(range(0, 6), [['start_minute' => 540, 'end_minute' => 1020]]),
]);
$this->setSegments($user, $short, [
['sequence' => 1, 'name' => 'تزریق', 'duration_minutes' => 10, 'requirements' => [['type_uuid' => $room->getUuid()]]],
]);
$saturday = $this->nextSaturday();
$body = $this->search($user, $short, $address, $saturday, $saturday);
self::assertSame(200, $this->responseCode());
self::assertNotEmpty($body['data']['slots'], 'اتاق سه‌تخته باید وقت بدهد');
}
/** زمان گذشته حذف می‌شود. */
public function testPastStartsAreExcluded(): void
{
[$user, $section, $address] = $this->clinicWithBranch();
$service = $this->service($section, 'ویزیت', 20);
$room = $this->type($address, 'room', 'اتاق');
$this->resourceWithShift($user, $address, $room, 'اتاق ۱');
$this->setSegments($user, $service, [
['sequence' => 1, 'name' => 'ویزیت', 'duration_minutes' => 20, 'requirements' => [['type_uuid' => $room->getUuid()]]],
]);
$lastWeek = $this->nextSaturday() - 7 * 86400;
$body = $this->search($user, $service, $address, $lastWeek, $lastWeek);
self::assertSame(200, $this->responseCode());
self::assertSame([], $body['data']['slots']);
self::assertSame('no_capacity_in_range', $body['data']['reason']);
}
/** فهرست خالی خطا نیست و ۴۰۴ هم نیست — دلیل صریح می‌آید. */
public function testEmptyResultCarriesAReason(): void
{
[$user, $section, $address] = $this->clinicWithBranch();
$service = $this->service($section, 'ویزیت', 20);
$room = $this->type($address, 'room', 'اتاق');
// منبع هست ولی هیچ شیفتی ندارد.
$this->authJson('POST', '/api/v1/resource', $user, [
'address_uuid' => $address->getUuid(),
'type_uuid' => $room->getUuid(),
'name' => 'اتاق بی‌شیفت',
]);
$this->setSegments($user, $service, [
['sequence' => 1, 'name' => 'ویزیت', 'duration_minutes' => 20, 'requirements' => [['type_uuid' => $room->getUuid()]]],
]);
$saturday = $this->nextSaturday();
$body = $this->search($user, $service, $address, $saturday, $saturday);
self::assertSame(200, $this->responseCode());
self::assertSame([], $body['data']['slots']);
self::assertSame('no_capacity_in_range', $body['data']['reason']);
}
public function testRangeBeyondNinetyDaysIsRejected(): void
{
[$user, $section, $address] = $this->clinicWithBranch();
$service = $this->service($section, 'ویزیت', 20);
$saturday = $this->nextSaturday();
$this->search($user, $service, $address, $saturday, $saturday + 120 * 86400);
self::assertSame(422, $this->responseCode());
}
public function testForeignBranchIsNotFound(): void
{
[$user, $section] = $this->clinicWithBranch();
[, , $foreign] = $this->clinicWithBranch();
$service = $this->service($section, 'ویزیت', 20);
$saturday = $this->nextSaturday();
$this->authJson('POST', '/api/v1/appointment-availability', $user, [
'service_uuid' => $service->getUuid(),
'branch_uuid' => $foreign->getUuid(),
'from' => $saturday,
'to' => $saturday,
]);
self::assertSame(404, $this->responseCode());
}
/** بازهٔ اشغال گسترده‌تر از بخش است: آماده‌سازی و تمیزکاری هم می‌گیرد. */
public function testSetupAndCleanupWidenTheOccupiedInterval(): void
{
[$user, $section, $address] = $this->clinicWithBranch();
$service = $this->service($section, 'لیزر', 20);
$device = $this->type($address, 'device', 'دستگاه');
$created = $this->authJson('POST', '/api/v1/resource', $user, [
'address_uuid' => $address->getUuid(),
'type_uuid' => $device->getUuid(),
'name' => 'لیزر تنها',
'setup_minutes' => 10,
'cleanup_minutes' => 10,
]);
self::assertSame(201, $this->responseCode());
$this->authJson('PUT', "/api/v1/resource/{$created['data']['uuid']}/calendar", $user, [
'days' => array_fill_keys(range(0, 6), [['start_minute' => 540, 'end_minute' => 1020]]),
]);
$this->setSegments($user, $service, [
['sequence' => 1, 'name' => 'لیزر', 'duration_minutes' => 20, 'requirements' => [['type_uuid' => $device->getUuid()]]],
]);
$saturday = $this->nextSaturday();
// دستگاه ۱۲:۰۰ تا ۱۳:۰۰ گرفته است.
$this->occupy($created['data']['uuid'], $saturday + 12 * 3600, $saturday + 13 * 3600);
$body = $this->search($user, $service, $address, $saturday, $saturday, ['step_minutes' => 5]);
$starts = array_column($body['data']['slots'], 'start');
// شروع ۱۱:۵۵ یعنی اشغال از ۱۱:۴۵ تا ۱۲:۲۵ — با اشغال موجود تداخل دارد.
self::assertNotContains($saturday + 11 * 3600 + 55 * 60, $starts);
// شروع ۱۱:۳۰ یعنی اشغال ۱۱:۲۰ تا ۱۲:۰۰ — دقیقاً می‌چسبد و مجاز است.
self::assertContains($saturday + 11 * 3600 + 30 * 60, $starts);
}
}