package http import ( "context" "errors" "net/http" "net/http/httptest" "strings" "testing" "primaya-api/cpone-middleware/internal/repository" ) type fakeRegistrationRepository struct { outcome repository.RegistrationOutcome err error params repository.RegistrationParams } func (f *fakeRegistrationRepository) RegisterPatient(_ context.Context, params repository.RegistrationParams) (repository.RegistrationOutcome, error) { f.params = params return f.outcome, f.err } func TestRegisterPatientUsesConfiguredDepartmentAndNormalizesPayload(t *testing.T) { repo := &fakeRegistrationRepository{outcome: repository.RegistrationOutcome{ HTTPStatus: http.StatusCreated, Success: true, Status: "registered", Message: "Registrasi berhasil disimpan", }} handler := Handler{registrationRepo: repo, departmentID: "08_01"} request := httptest.NewRequest(http.MethodPost, "/api/cpone/patients/registration", strings.NewReader(`{ "nik":"3174-0115-0190-0001", "tgl_lahir":"1990-01-15", "tanggal":"2026-09-07", "dokter_id":"driqbal", "jenis_pasien_id":"1", "jenis_reg_id":2, "periode_tarif_id":"10", "perusahaan_id":"ADMEDIKAAA", "group_tarif_id":18, "kelas_id":"Default", "channel":"cpone" }`)) recorder := httptest.NewRecorder() handler.RegisterPatient(recorder, request) if recorder.Code != http.StatusCreated { t.Fatalf("status = %d; body=%s", recorder.Code, recorder.Body.String()) } if repo.params.DepartmentID != "08_01" || repo.params.NIK != "3174011501900001" || repo.params.RegistrationType != 2 { t.Fatalf("unexpected normalized params: %+v", repo.params) } if repo.params.PatientTypeID != 1 || repo.params.TariffPeriodID != 10 || repo.params.GroupTariffID != 18 || repo.params.Channel != "CPONE" { t.Fatalf("unexpected numeric/text params: %+v", repo.params) } } func TestRegisterPatientRequiresIdentityWhenMedrecIsMissing(t *testing.T) { handler := Handler{registrationRepo: &fakeRegistrationRepository{}, departmentID: "08_01"} request := httptest.NewRequest(http.MethodPost, "/api/cpone/patients/registration", strings.NewReader(`{"tanggal":"2026-09-07"}`)) recorder := httptest.NewRecorder() handler.RegisterPatient(recorder, request) if recorder.Code != http.StatusUnprocessableEntity || !strings.Contains(recorder.Body.String(), "nik dan tgl_lahir wajib") { t.Fatalf("unexpected validation response: status=%d body=%s", recorder.Code, recorder.Body.String()) } } func TestRegisterPatientForwardsRepositoryError(t *testing.T) { handler := Handler{registrationRepo: &fakeRegistrationRepository{err: errors.New("database unavailable")}, departmentID: "08_01"} request := httptest.NewRequest(http.MethodPost, "/api/cpone/patients/registration", strings.NewReader(`{"medrec_id":"00001234"}`)) recorder := httptest.NewRecorder() handler.RegisterPatient(recorder, request) if recorder.Code != http.StatusInternalServerError || !strings.Contains(recorder.Body.String(), "database unavailable") { t.Fatalf("unexpected error response: status=%d body=%s", recorder.Code, recorder.Body.String()) } }