package staterpc import ( "reflect" "testing" "time" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) // TestTaskProtoCarriesEveryDomainField fills every field of task.Task with a // distinct non-zero value and requires the proto round trip to return it // unchanged. taskProto/taskValue assign field by field, so a field added to // the domain type and forgotten on the wire is otherwise invisible: the task // still crosses, just with that field silently zeroed. This test fails for the // forgotten field by name rather than leaving it to a caller to discover in // production (archie-core-zf8h). func TestTaskProtoCarriesEveryDomainField(t *testing.T) { stamp := time.Date(2026, 9, 21, 10, 30, 0, 0, time.UTC) want := &task.Task{} v := reflect.ValueOf(want).Elem() for i := range v.NumField() { field := v.Field(i) switch { case field.Type() == reflect.TypeFor[time.Time](): field.Set(reflect.ValueOf(stamp.Add(time.Duration(i) * time.Hour))) case field.Kind() == reflect.String: field.SetString(v.Type().Field(i).Name + "-value") case field.CanInt(): field.SetInt(int64(i) + 1) case field.Type() == reflect.TypeFor[map[string]any](): field.Set(reflect.ValueOf(map[string]any{"key": v.Type().Field(i).Name + "-value"})) default: t.Fatalf("field %s has kind %s, which this test does not know how to fill; extend it alongside the field", v.Type().Field(i).Name, field.Kind()) } } got := taskValue(taskProto(want)) if reflect.DeepEqual(got, want) { return } for i := range v.NumField() { name := v.Type().Field(i).Name gotField := reflect.ValueOf(got).Elem().Field(i).Interface() wantField := v.Field(i).Interface() if !reflect.DeepEqual(gotField, wantField) { t.Errorf("Task.%s did not survive the proto round trip: got %v, want %v", name, gotField, wantField) } } } // TestWorkflowStatProtoCarriesEveryField is the same contract for the // per-workflow metric row, and the reason it exists is the same: the dashboard // reads these counts off the wire, so a count that only the producer knows // (Completed, the runs that finished with no pull request to open) would read // as zero everywhere without anything failing. Each field gets a value of its // own, so a mapping that assigns two of them from the same source -- or drops // one -- is caught by name. func TestWorkflowStatProtoCarriesEveryField(t *testing.T) { want := storecontract.WorkflowStat{} v := reflect.ValueOf(&want).Elem() for i := range v.NumField() { field := v.Field(i) switch { case field.Kind() == reflect.String: field.SetString(v.Type().Field(i).Name + "-value") case field.Kind() == reflect.Float64: field.SetFloat(float64(i) + 0.5) case field.CanInt(): field.SetInt(int64(i) + 1) default: t.Fatalf("field %s has kind %s, which this test does not know how to fill; extend it alongside the field", v.Type().Field(i).Name, field.Kind()) } } got := reflect.ValueOf(workflowStatValue(workflowStatProto(want))) for i := range v.NumField() { name := v.Type().Field(i).Name gotField := got.Field(i).Interface() wantField := v.Field(i).Interface() if !reflect.DeepEqual(gotField, wantField) { t.Errorf("WorkflowStat.%s did not survive the proto round trip: got %v, want %v", name, gotField, wantField) } } }