Files
clinicpro/assets/admin/components/CancelAppointmentDialog.test.tsx
T
hamedandClaude Opus 5 4049daf071 feat: close the last four domain events, and the panel paths they describe
Every one of the fourteen named events now has an emit point. The four that
were missing all sat on paths owned by earlier tasks:

- AppointmentCompleted fires from both status-change routes, after the row is
  saved. A rejected transition or a version conflict leaves no event; otherwise
  the completed count runs ahead of the appointments themselves.
- AppointmentRescheduled is a third event, not a replacement. A rebook is a
  confirm plus a cancel, and a consumer that only hears the cancel messages a
  patient who still has an appointment.
- ResourceBlocked / ResourceReleased are a pair. Capacity coming back has to be
  as audible as capacity going away, or the resource reads as permanently taken.

Publishing is now on the scheduler rather than an unregistered command: the
logic moved out of PublishDomainEventsCommand into OutboxPublisher so the
recurring message and the manual command share it, and the existing
worker-scheduler container consumes it. The scheduler message carries no data
on purpose — what to publish is read from the table, so an event recorded
between two ticks is not skipped. DomainEventMessage routes to async, since a
slow consumer was otherwise slowing the drain itself and its failure marked a
row failed that had in fact been delivered.

Panel work that these paths made reachable:

- Cancelling from the appointment page now goes through the policy-aware
  endpoint and shows the penalty preview before the confirm, so the operator
  does not discover the patient's penalty after the fact. The cancellation
  service writes the timeline entry itself and accepts a reason, which that
  path previously dropped on the floor.
- Rescheduling reuses the booking page under ?rebook=<uuid> — the search and
  hold steps are identical and only the final step differs. The doctor picker
  is hidden there: a reschedule is not an invitation to change doctors.
- A new GET /appointment/{uuid}/segments exposes the recorded plan. An empty
  list is not an error, it means the appointment is slot-based, and that is
  exactly what gates the resource-mode reschedule button.

AppointmentInvoiceCard no longer crashes the whole detail page when an older
invoice has no discount breakdown.

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

97 lines
3.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderWithProviders } from '../test/utils';
vi.mock('../lib/api', () => ({
api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
ApiError: class extends Error {},
}));
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
import { api } from '../lib/api';
import CancelAppointmentDialog from './CancelAppointmentDialog';
const get = api.get as ReturnType<typeof vi.fn>;
const post = api.post as ReturnType<typeof vi.fn>;
const preview = (over: Record<string, unknown> = {}) => ({
data: {
penalty_rials: 250_000,
deposit_refundable: true,
credit_refundable: true,
within_free_window: false,
notes: [],
paid_rials: 1_000_000,
...over,
},
});
describe('CancelAppointmentDialog', () => {
beforeEach(() => {
vi.clearAllMocks();
post.mockResolvedValue({ data: { waitlist_notified: 0 } });
});
/** ⭐ عددی که اپراتور می‌بیند باید همان باشد که کسر می‌شود. */
it('shows the penalty from the server before anything is cancelled', async () => {
get.mockResolvedValue(preview());
renderWithProviders(
<CancelAppointmentDialog open appointmentUuid="a-1" onClose={() => {}} />,
);
// مبلغ‌ها به تومان نمایش داده می‌شوند: ۲۵۰٬۰۰۰ ریال ⇒ ۲۵٬۰۰۰ تومان.
await waitFor(() => expect(screen.getByText(/۲۵٬۰۰۰ تومان/)).toBeInTheDocument());
expect(post).not.toHaveBeenCalled();
});
it('says "بدون جریمه" inside the free window instead of showing a zero', async () => {
get.mockResolvedValue(preview({ penalty_rials: 0, within_free_window: true }));
renderWithProviders(
<CancelAppointmentDialog open appointmentUuid="a-1" onClose={() => {}} />,
);
await waitFor(() => expect(screen.getByText('بدون جریمه')).toBeInTheDocument());
expect(screen.getByText('در بازهٔ لغو رایگان است.')).toBeInTheDocument();
});
it('sends the typed reason along with the cancellation', async () => {
get.mockResolvedValue(preview());
const user = userEvent.setup();
renderWithProviders(
<CancelAppointmentDialog open appointmentUuid="a-1" onClose={() => {}} />,
);
await waitFor(() => expect(screen.getByText(/مبلغ پرداخت‌شده/)).toBeInTheDocument());
await user.type(screen.getByLabelText(/دلیل لغو/), 'بیمار تماس گرفت');
await user.click(screen.getByRole('button', { name: 'لغو نوبت' }));
await waitFor(() =>
expect(post).toHaveBeenCalledWith('/api/v1/appointment/a-1/cancel', {
by: 'doctor',
reason: 'بیمار تماس گرفت',
}),
);
});
/** پیش‌نمایشی که نیامده نباید لغو را قفل کند — ولی باید صریح بگوید که نیامده. */
it('still allows cancelling when the preview fails, and says so', async () => {
get.mockRejectedValue(new Error('down'));
renderWithProviders(
<CancelAppointmentDialog open appointmentUuid="a-1" onClose={() => {}} />,
);
await waitFor(() =>
expect(screen.getByText(/پیامد مالی لغو در دسترس نیست/)).toBeInTheDocument(),
);
expect(screen.getByRole('button', { name: 'لغو نوبت' })).not.toBeDisabled();
});
});