package business import ( "context" "encoding/json" "reflect" "testing" "kra/internal/biz/business" "kra/internal/service/dto" ) type alertRecordUsecaseFake struct { AlertRecordUsecase id uint64 filter business.AlertRecordFilter attachments []string } func (f *alertRecordUsecaseFake) Process(_ context.Context, id uint64, _ business.AlertAction, _ string) error { f.id = id return nil } func (f *alertRecordUsecaseFake) AddNote(_ context.Context, id uint64, _ string, attachments []string) error { f.id = id f.attachments = attachments return nil } func (f *alertRecordUsecaseFake) List(_ context.Context, filter business.AlertRecordFilter, _ ...business.ListOption) (*business.AlertRecordPage, error) { f.filter = filter return &business.AlertRecordPage{Rows: []*business.AlertRecord{}, StatusCount: map[int]int64{}, Available: true}, nil } func TestAlertRecordServiceAcceptsExactIDsAndAttachmentFormats(t *testing.T) { f := &alertRecordUsecaseFake{} s := &AlertRecordService{uc: f} for _, raw := range []string{`{"alertId":"9007199254740993"}`, `{"alertId":9007199254740993}`} { var req dto.AlertProcessRequest if err := json.Unmarshal([]byte(raw), &req); err != nil { t.Fatal(err) } if err := s.Process(context.Background(), &req, business.AlertStart); err != nil || f.id != 9007199254740993 { t.Fatal("预警 ID 在 DTO 边界失真") } } var req dto.AlertNoteRequest if err := json.Unmarshal([]byte(`{"alertId":"10","noteContent":"备注","attachmentIds":[9007199254740993,"2","/files/a.png"]}`), &req); err != nil { t.Fatal(err) } if err := s.AddNote(context.Background(), &req); err != nil || !reflect.DeepEqual(f.attachments, []string{"9007199254740993", "2", "/files/a.png"}) { t.Fatalf("附件解析错误: %+v %v", f.attachments, err) } for _, raw := range []string{`null`, `1.5`, `true`, `{"url":"bad"}`} { req.AttachmentIDs = []json.RawMessage{json.RawMessage(raw)} if err := s.AddNote(context.Background(), &req); err == nil { t.Fatalf("错误附件值被接受: %s", raw) } } } func TestAlertRecordServiceQueryNormalizationAndIDResponse(t *testing.T) { f := &alertRecordUsecaseFake{} s := &AlertRecordService{uc: f} _, err := s.List(context.Background(), &dto.AlertRecordQuery{PlatformCodes: " lx, hw,lx, ", RuleName: " 注册 "}) if err != nil || !reflect.DeepEqual(f.filter.PlatformCodes, []string{"lx", "hw"}) || f.filter.RuleName != "注册" { t.Fatal("查询参数未归一化") } raw, err := json.Marshal(alertRecordResponse(&business.AlertRecord{ID: 9007199254740993, RuleID: 9007199254740995, Status: 3})) if err != nil { t.Fatal(err) } var value map[string]any if err = json.Unmarshal(raw, &value); err != nil { t.Fatal(err) } if value["id"] != "9007199254740993" || value["ruleId"] != "9007199254740995" || value["statusLabel"] != "待审核" || value["warningColor"] != nil { t.Fatalf("响应丢失精度或改变快照语义: %s", raw) } }