|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/RandomCodeSpace/docscontext/internal/config" |
| 12 | + "github.com/RandomCodeSpace/docscontext/internal/store" |
| 13 | +) |
| 14 | + |
| 15 | +// newClaimsRouter builds a router with a seeded store that has entities |
| 16 | +// and claims, so the REST layer can be exercised end-to-end. |
| 17 | +func newClaimsRouter(t *testing.T) http.Handler { |
| 18 | + t.Helper() |
| 19 | + dir := t.TempDir() |
| 20 | + st, err := store.Open(filepath.Join(dir, "claims_router.db")) |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("store.Open: %v", err) |
| 23 | + } |
| 24 | + t.Cleanup(func() { _ = st.Close() }) |
| 25 | + |
| 26 | + ctx := context.Background() |
| 27 | + if err := st.UpsertDocument(ctx, &store.Document{ |
| 28 | + ID: "d1", Path: "/tmp/d1.md", DocType: "md", FileHash: "d1h", IsLatest: true, |
| 29 | + }); err != nil { |
| 30 | + t.Fatalf("UpsertDocument: %v", err) |
| 31 | + } |
| 32 | + if err := st.UpsertEntity(ctx, &store.Entity{ID: "e1", Name: "Alpha"}); err != nil { |
| 33 | + t.Fatalf("UpsertEntity: %v", err) |
| 34 | + } |
| 35 | + claims := []*store.Claim{ |
| 36 | + {ID: "c1", EntityID: "e1", Claim: "fact-1", Status: "verified", DocID: "d1"}, |
| 37 | + {ID: "c2", EntityID: "e1", Claim: "fact-2", Status: "pending", DocID: "d1"}, |
| 38 | + } |
| 39 | + if err := st.BatchInsertClaims(ctx, claims); err != nil { |
| 40 | + t.Fatalf("BatchInsertClaims: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + cfg := &config.Config{} |
| 44 | + cfg.DataDir = dir |
| 45 | + return NewRouter(st, nil, nil, cfg, nil) |
| 46 | +} |
| 47 | + |
| 48 | +func TestClaimsHandlers(t *testing.T) { |
| 49 | + t.Run("claims_for_entity_happy_path", func(t *testing.T) { |
| 50 | + h := newClaimsRouter(t) |
| 51 | + req := httptest.NewRequest(http.MethodGet, "/api/entities/e1/claims", nil) |
| 52 | + rec := httptest.NewRecorder() |
| 53 | + h.ServeHTTP(rec, req) |
| 54 | + if rec.Code != http.StatusOK { |
| 55 | + t.Fatalf("status = %d, want 200", rec.Code) |
| 56 | + } |
| 57 | + var claims []store.Claim |
| 58 | + if err := json.NewDecoder(rec.Body).Decode(&claims); err != nil { |
| 59 | + t.Fatalf("decode: %v", err) |
| 60 | + } |
| 61 | + if len(claims) != 2 { |
| 62 | + t.Errorf("len = %d, want 2", len(claims)) |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("claims_for_unknown_entity_returns_empty_array", func(t *testing.T) { |
| 67 | + h := newClaimsRouter(t) |
| 68 | + req := httptest.NewRequest(http.MethodGet, "/api/entities/nope/claims", nil) |
| 69 | + rec := httptest.NewRecorder() |
| 70 | + h.ServeHTTP(rec, req) |
| 71 | + if rec.Code != http.StatusOK { |
| 72 | + t.Fatalf("status = %d, want 200", rec.Code) |
| 73 | + } |
| 74 | + body := rec.Body.String() |
| 75 | + if body != "[]\n" { |
| 76 | + t.Errorf("body = %q, want %q", body, "[]\n") |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("list_claims_status_filter", func(t *testing.T) { |
| 81 | + h := newClaimsRouter(t) |
| 82 | + req := httptest.NewRequest(http.MethodGet, "/api/claims?status=verified", nil) |
| 83 | + rec := httptest.NewRecorder() |
| 84 | + h.ServeHTTP(rec, req) |
| 85 | + if rec.Code != http.StatusOK { |
| 86 | + t.Fatalf("status = %d, want 200", rec.Code) |
| 87 | + } |
| 88 | + var claims []store.Claim |
| 89 | + if err := json.NewDecoder(rec.Body).Decode(&claims); err != nil { |
| 90 | + t.Fatalf("decode: %v", err) |
| 91 | + } |
| 92 | + if len(claims) != 1 { |
| 93 | + t.Errorf("len = %d, want 1", len(claims)) |
| 94 | + } |
| 95 | + if len(claims) > 0 && claims[0].Status != "verified" { |
| 96 | + t.Errorf("status = %q, want verified", claims[0].Status) |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("list_claims_limit_bound", func(t *testing.T) { |
| 101 | + h := newClaimsRouter(t) |
| 102 | + req := httptest.NewRequest(http.MethodGet, "/api/claims?limit=1", nil) |
| 103 | + rec := httptest.NewRecorder() |
| 104 | + h.ServeHTTP(rec, req) |
| 105 | + if rec.Code != http.StatusOK { |
| 106 | + t.Fatalf("status = %d, want 200", rec.Code) |
| 107 | + } |
| 108 | + var claims []store.Claim |
| 109 | + if err := json.NewDecoder(rec.Body).Decode(&claims); err != nil { |
| 110 | + t.Fatalf("decode: %v", err) |
| 111 | + } |
| 112 | + if len(claims) != 1 { |
| 113 | + t.Errorf("len = %d, want 1", len(claims)) |
| 114 | + } |
| 115 | + }) |
| 116 | +} |
0 commit comments