|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "log/slog" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/GoYoko/web" |
| 15 | + "github.com/google/uuid" |
| 16 | + |
| 17 | + "github.com/chaitin/MonkeyCode/backend/consts" |
| 18 | + taskflowpkg "github.com/chaitin/MonkeyCode/backend/pkg/taskflow" |
| 19 | +) |
| 20 | + |
| 21 | +type taskLogStoreRepoStub struct { |
| 22 | + store consts.LogStore |
| 23 | + err error |
| 24 | +} |
| 25 | + |
| 26 | +func (s *taskLogStoreRepoStub) GetLogStore(context.Context, uuid.UUID) (consts.LogStore, error) { |
| 27 | + return s.store, s.err |
| 28 | +} |
| 29 | + |
| 30 | +func TestInternalHostHandler_GetTaskLogStore_EmptyMeansLoki(t *testing.T) { |
| 31 | + h := &InternalHostHandler{ |
| 32 | + logger: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| 33 | + taskRepo: &taskLogStoreRepoStub{}, |
| 34 | + } |
| 35 | + req := taskflowpkg.GetTaskLogStoreReq{TaskID: uuid.New()} |
| 36 | + resp := callGetTaskLogStore(t, h, req) |
| 37 | + if resp.LogStore != string(consts.LogStoreLoki) { |
| 38 | + t.Fatalf("log_store = %q, want %q", resp.LogStore, consts.LogStoreLoki) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestInternalHostHandler_GetTaskLogStore_ClickHousePassthrough(t *testing.T) { |
| 43 | + h := &InternalHostHandler{ |
| 44 | + logger: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| 45 | + taskRepo: &taskLogStoreRepoStub{store: consts.LogStoreClickHouse}, |
| 46 | + } |
| 47 | + req := taskflowpkg.GetTaskLogStoreReq{TaskID: uuid.New()} |
| 48 | + resp := callGetTaskLogStore(t, h, req) |
| 49 | + if resp.LogStore != string(consts.LogStoreClickHouse) { |
| 50 | + t.Fatalf("log_store = %q, want %q", resp.LogStore, consts.LogStoreClickHouse) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestInternalHostHandler_GetTaskLogStore_RepoError(t *testing.T) { |
| 55 | + h := &InternalHostHandler{ |
| 56 | + logger: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| 57 | + taskRepo: &taskLogStoreRepoStub{err: errors.New("boom")}, |
| 58 | + } |
| 59 | + req := taskflowpkg.GetTaskLogStoreReq{TaskID: uuid.New()} |
| 60 | + body, err := json.Marshal(req) |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + w := web.New() |
| 65 | + w.POST("/internal/task-log-store", web.BindHandler(h.GetTaskLogStore)) |
| 66 | + rec := httptest.NewRecorder() |
| 67 | + httpReq := httptest.NewRequest(http.MethodPost, "/internal/task-log-store", strings.NewReader(string(body))) |
| 68 | + httpReq.Header.Set("Content-Type", "application/json") |
| 69 | + w.Echo().ServeHTTP(rec, httpReq) |
| 70 | + |
| 71 | + var resp web.Resp |
| 72 | + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { |
| 73 | + t.Fatalf("unmarshal resp: %v", err) |
| 74 | + } |
| 75 | + if resp.Code == 0 { |
| 76 | + t.Fatalf("response = %+v, want error", resp) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func callGetTaskLogStore(t *testing.T, h *InternalHostHandler, req taskflowpkg.GetTaskLogStoreReq) taskflowpkg.GetTaskLogStoreResp { |
| 81 | + t.Helper() |
| 82 | + |
| 83 | + body, err := json.Marshal(req) |
| 84 | + if err != nil { |
| 85 | + t.Fatal(err) |
| 86 | + } |
| 87 | + |
| 88 | + w := web.New() |
| 89 | + w.POST("/internal/task-log-store", web.BindHandler(h.GetTaskLogStore)) |
| 90 | + |
| 91 | + rec := httptest.NewRecorder() |
| 92 | + httpReq := httptest.NewRequest(http.MethodPost, "/internal/task-log-store", strings.NewReader(string(body))) |
| 93 | + httpReq.Header.Set("Content-Type", "application/json") |
| 94 | + w.Echo().ServeHTTP(rec, httpReq) |
| 95 | + |
| 96 | + if rec.Code != http.StatusOK { |
| 97 | + t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String()) |
| 98 | + } |
| 99 | + |
| 100 | + var resp web.Resp |
| 101 | + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { |
| 102 | + t.Fatalf("unmarshal resp: %v", err) |
| 103 | + } |
| 104 | + data, err := json.Marshal(resp.Data) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("marshal resp data: %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + var out taskflowpkg.GetTaskLogStoreResp |
| 110 | + if err := json.Unmarshal(data, &out); err != nil { |
| 111 | + t.Fatalf("unmarshal typed resp: %v", err) |
| 112 | + } |
| 113 | + return out |
| 114 | +} |
0 commit comments