package service import ( "context" "strings" "testing" "time" "git.yuntuo.hk/k2safe/orbit/internal/fault" core "git.yuntuo.hk/k2safe/orbit/model" "git.yuntuo.hk/k2safe/orbit/platform/auth" ) type queryValidationStore struct { Store calls int input ListInput } func (s *queryValidationStore) ListTasks(_ context.Context, _ auth.Token, in ListInput) (TaskPage, error) { s.calls++ s.input = in return TaskPage{}, nil } func TestTaskQueryBoundsAndNormalization(t *testing.T) { store := &queryValidationStore{} api := New(store) from := time.Date(2026, 9, 17, 12, 0, 0, 123456000, time.FixedZone("example", 8*3600)) before := from.Add(time.Hour) base := ListInput{QueueID: core.ID(strings.Repeat("1", 32)), BusinessKey: "订单 /+% é ", CreatedFrom: &from, CreatedBefore: &before} for name, change := range map[string]func(*ListInput){ "bad_utf8": func(v *ListInput) { v.BusinessKey = "\xff" }, "long_key": func(v *ListInput) { v.BusinessKey = strings.Repeat("字", 192) }, "newline": func(v *ListInput) { v.BusinessKey = "x\n" }, "zero_time": func(v *ListInput) { z := time.Time{}; v.CreatedFrom = &z }, "nanosecond": func(v *ListInput) { z := from.Add(time.Nanosecond); v.CreatedFrom = &z }, "equal_bounds": func(v *ListInput) { v.CreatedBefore = &from }, "reverse_bounds": func(v *ListInput) { v.CreatedFrom = &before; v.CreatedBefore = &from }, } { t.Run(name, func(t *testing.T) { in := base change(&in) if _, err := api.ListTasks(context.Background(), auth.Token{}, in); fault.Code(err) != "INVALID_ARGUMENT" { t.Fatal(err) } }) } if store.calls != 0 { t.Fatal("invalid query reached persistence") } if _, err := api.ListTasks(context.Background(), auth.Token{}, base); err != nil { t.Fatal(err) } if store.input.BusinessKey != base.BusinessKey || store.input.CreatedFrom.Location() != time.UTC || !store.input.CreatedFrom.Equal(from) || store.input.Limit != 20 { t.Fatal("query normalization lost meaning") } for _, in := range []ListInput{{QueueID: base.QueueID, CreatedFrom: &from}, {QueueID: base.QueueID, CreatedBefore: &before}} { if _, err := api.ListTasks(context.Background(), auth.Token{}, in); err != nil { t.Fatal(err) } } }