feat: redesign architecture doc with interactive SVG
This commit is contained in:
1 parent
86339b4d0d
commit
5fc61e0bad
5 files changed
+1711
-341
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,359 @@
|
||||
package forge
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func writeJSON(t *testing.T, w http.ResponseWriter, v any) {
|
||||
t.Helper()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignedIssuesExcludesPRs(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("GET /repos/o/r/issues", func(w http.ResponseWriter, r *http.Request) {
|
||||
if got := r.URL.Query().Get("assignee"); got != "archie-bot" {
|
||||
t.Errorf("assignee = %q", got)
|
||||
}
|
||||
writeJSON(t, w, []map[string]any{
|
||||
{"number": 1, "title": "real issue", "body": "b", "labels": []map[string]any{{"name": "bug"}}},
|
||||
{"number": 2, "title": "a pr", "pull_request": map[string]any{"url": "x"}},
|
||||
})
|
||||
})
|
||||
|
||||
issues, err := c.AssignedIssues(t.Context(), "o", "r", "archie-bot")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(issues) != 1 || issues[0].Number != 1 || issues[0].Title != "real issue" || len(issues[0].Labels) != 1 || issues[0].Labels[0] != "bug" {
|
||||
t.Fatalf("issues = %+v", issues)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssuesWithLabelExcludesPRs(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("GET /repos/o/r/issues", func(w http.ResponseWriter, r *http.Request) {
|
||||
if got := r.URL.Query().Get("labels"); got != "archie" {
|
||||
t.Errorf("labels = %q", got)
|
||||
}
|
||||
writeJSON(t, w, []map[string]any{
|
||||
{"number": 3, "title": "labelled", "body": "b"},
|
||||
})
|
||||
})
|
||||
|
||||
issues, err := c.IssuesWithLabel(t.Context(), "o", "r", "archie")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(issues) != 1 || issues[0].Number != 3 {
|
||||
t.Fatalf("issues = %+v", issues)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComment(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("POST /repos/o/r/issues/5/comments", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]string
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["body"] != "hello" {
|
||||
t.Errorf("comment body = %q", body["body"])
|
||||
}
|
||||
writeJSON(t, w, map[string]any{"id": 42})
|
||||
})
|
||||
|
||||
id, err := c.Comment(t.Context(), "o", "r", 5, "hello")
|
||||
if err != nil || id != 42 {
|
||||
t.Fatalf("Comment = (%d, %v)", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepliesAfterExcludesBotAndOldComments(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("GET /repos/o/r/issues/5/comments", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, []map[string]any{
|
||||
{"id": 1, "user": map[string]any{"login": "human"}, "body": "old, ignored"},
|
||||
{"id": 2, "user": map[string]any{"login": "archie-bot"}, "body": "bot comment, excluded"},
|
||||
{"id": 3, "user": map[string]any{"login": "human"}, "body": "the reply"},
|
||||
})
|
||||
})
|
||||
|
||||
replies, err := c.RepliesAfter(t.Context(), "o", "r", 5, 1, "archie-bot")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(replies) != 1 || replies[0].ID != 3 || replies[0].Body != "the reply" || replies[0].User != "human" {
|
||||
t.Fatalf("replies = %+v", replies)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatePR(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("POST /repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]any{"number": 9})
|
||||
})
|
||||
|
||||
n, err := c.CreatePR(t.Context(), "o", "r", "title", "head", "base", "body")
|
||||
if err != nil || n != 9 {
|
||||
t.Fatalf("CreatePR = (%d, %v)", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPRState(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
json map[string]any
|
||||
want string
|
||||
}{
|
||||
{"merged", map[string]any{"merged": true, "state": "closed"}, "merged"},
|
||||
{"open", map[string]any{"merged": false, "state": "open"}, "open"},
|
||||
{"closed", map[string]any{"merged": false, "state": "closed"}, "closed"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("GET /repos/o/r/pulls/7", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, tt.json)
|
||||
})
|
||||
got, err := c.PRState(t.Context(), "o", "r", 7)
|
||||
if err != nil || got != tt.want {
|
||||
t.Fatalf("PRState = (%q, %v), want %q", got, err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseIssueWithComment(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
var commented, edited bool
|
||||
mux.HandleFunc("POST /repos/o/r/issues/5/comments", func(w http.ResponseWriter, r *http.Request) {
|
||||
commented = true
|
||||
writeJSON(t, w, map[string]any{"id": 1})
|
||||
})
|
||||
mux.HandleFunc("PATCH /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
||||
edited = true
|
||||
var body map[string]string
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["state"] != "closed" {
|
||||
t.Errorf("state = %q", body["state"])
|
||||
}
|
||||
writeJSON(t, w, map[string]any{"number": 5, "state": "closed"})
|
||||
})
|
||||
|
||||
if err := c.CloseIssue(t.Context(), "o", "r", 5, "won't do"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !commented || !edited {
|
||||
t.Fatalf("commented=%v edited=%v", commented, edited)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseIssueWithoutComment(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("POST /repos/o/r/issues/5/comments", func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Fatal("should not comment when comment is empty")
|
||||
})
|
||||
mux.HandleFunc("PATCH /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]any{"number": 5, "state": "closed"})
|
||||
})
|
||||
|
||||
if err := c.CloseIssue(t.Context(), "o", "r", 5, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReact(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("POST /repos/o/r/issues/5/reactions", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]string
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["content"] != "+1" {
|
||||
t.Errorf("content = %q", body["content"])
|
||||
}
|
||||
writeJSON(t, w, map[string]any{"id": 1, "content": "+1"})
|
||||
})
|
||||
|
||||
if err := c.React(t.Context(), "o", "r", 5, "+1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetStateLabelSwapsLabels(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
var removed, added, created []string
|
||||
|
||||
mux.HandleFunc("GET /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]any{
|
||||
"number": 5,
|
||||
"labels": []map[string]any{{"name": "archie:queued"}, {"name": "bug"}},
|
||||
})
|
||||
})
|
||||
mux.HandleFunc("DELETE /repos/o/r/issues/5/labels/archie:queued", func(w http.ResponseWriter, r *http.Request) {
|
||||
removed = append(removed, "archie:queued")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
mux.HandleFunc("POST /repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]string
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
created = append(created, body["name"])
|
||||
writeJSON(t, w, map[string]any{"name": body["name"], "color": body["color"]})
|
||||
})
|
||||
mux.HandleFunc("POST /repos/o/r/issues/5/labels", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body []string
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
added = append(added, body...)
|
||||
writeJSON(t, w, []map[string]any{})
|
||||
})
|
||||
|
||||
known := []string{"archie:queued", "archie:working", "archie:waiting", "archie:pr", "archie:parked"}
|
||||
c.SetStateLabel(t.Context(), "o", "r", 5, "archie:working", known)
|
||||
|
||||
if len(removed) != 1 || removed[0] != "archie:queued" {
|
||||
t.Fatalf("removed = %v", removed)
|
||||
}
|
||||
if len(created) != 1 || created[0] != "archie:working" {
|
||||
t.Fatalf("created = %v", created)
|
||||
}
|
||||
if len(added) != 1 || added[0] != "archie:working" {
|
||||
t.Fatalf("added = %v", added)
|
||||
}
|
||||
|
||||
// Second call for the same label reuses the ensured-labels cache and
|
||||
// skips label creation.
|
||||
mux.HandleFunc("GET /repos/o/r/issues/6", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]any{"number": 6, "labels": []map[string]any{}})
|
||||
})
|
||||
mux.HandleFunc("POST /repos/o/r/issues/6/labels", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body []string
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
added = append(added, body...)
|
||||
writeJSON(t, w, []map[string]any{})
|
||||
})
|
||||
c.SetStateLabel(t.Context(), "o", "r", 6, "archie:working", known)
|
||||
if len(created) != 1 {
|
||||
t.Fatalf("expected label creation to be cached, created = %v", created)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetStateLabelAlreadyPresentIsNoop(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("GET /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]any{
|
||||
"number": 5,
|
||||
"labels": []map[string]any{{"name": "archie:working"}},
|
||||
})
|
||||
})
|
||||
mux.HandleFunc("POST /repos/o/r/issues/5/labels", func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Fatal("should not add a label that's already present")
|
||||
})
|
||||
mux.HandleFunc("POST /repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Fatal("should not create a label that's already present")
|
||||
})
|
||||
|
||||
c.SetStateLabel(t.Context(), "o", "r", 5, "archie:working", []string{"archie:working"})
|
||||
}
|
||||
|
||||
func TestSetStateLabelEmptyClearsAll(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
var removed []string
|
||||
mux.HandleFunc("GET /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]any{
|
||||
"number": 5,
|
||||
"labels": []map[string]any{{"name": "archie:working"}},
|
||||
})
|
||||
})
|
||||
mux.HandleFunc("DELETE /repos/o/r/issues/5/labels/archie:working", func(w http.ResponseWriter, r *http.Request) {
|
||||
removed = append(removed, "archie:working")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
c.SetStateLabel(t.Context(), "o", "r", 5, "", []string{"archie:working"})
|
||||
if len(removed) != 1 {
|
||||
t.Fatalf("removed = %v", removed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyPush(t *testing.T) {
|
||||
t.Run("has push", func(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("GET /repos/o/r", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]any{"permissions": map[string]bool{"push": true}})
|
||||
})
|
||||
if err := c.VerifyPush(t.Context(), "o", "r"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no push", func(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("GET /repos/o/r", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, map[string]any{"permissions": map[string]bool{"push": false}})
|
||||
})
|
||||
if err := c.VerifyPush(t.Context(), "o", "r"); err == nil {
|
||||
t.Fatal("expected error for missing push permission")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("api error", func(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("GET /repos/o/r", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
})
|
||||
if err := c.VerifyPush(t.Context(), "o", "r"); err == nil {
|
||||
t.Fatal("expected error for 404")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAcceptInvitations(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
var accepted []string
|
||||
mux.HandleFunc("GET /user/repository_invitations", func(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(t, w, []map[string]any{
|
||||
{"id": 1, "repository": map[string]any{"full_name": "o/ok"}},
|
||||
{"id": 2, "repository": map[string]any{"full_name": "o/fails"}},
|
||||
})
|
||||
})
|
||||
mux.HandleFunc("PATCH /user/repository_invitations/1", func(w http.ResponseWriter, r *http.Request) {
|
||||
accepted = append(accepted, "1")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
mux.HandleFunc("PATCH /user/repository_invitations/2", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
})
|
||||
|
||||
// A single failing invitation must not stop the others from being
|
||||
// processed or fail the whole call.
|
||||
if err := c.AcceptInvitations(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(accepted) != 1 || accepted[0] != "1" {
|
||||
t.Fatalf("accepted = %v", accepted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAcceptInvitationsListError(t *testing.T) {
|
||||
c, mux := newTestClient(t)
|
||||
mux.HandleFunc("GET /user/repository_invitations", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
})
|
||||
if err := c.AcceptInvitations(t.Context()); err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package forge
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-github/v78/github"
|
||||
)
|
||||
|
||||
// newTestClient wires a GitHubClient at a local httptest server so its
|
||||
// methods can be exercised without hitting the real GitHub API.
|
||||
func newTestClient(t *testing.T) (*GitHubClient, *http.ServeMux) {
|
||||
t.Helper()
|
||||
mux := http.NewServeMux()
|
||||
srv := httptest.NewServer(mux)
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
gh := github.NewClient(nil)
|
||||
base, err := url.Parse(srv.URL + "/")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gh.BaseURL = base
|
||||
gh.UploadURL = base
|
||||
|
||||
return &GitHubClient{gh: gh, log: slog.New(slog.NewTextHandler(io.Discard, nil))}, mux
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
func TestTaskByIssue(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if got, err := s.TaskByIssue(ctx, "sam", "archie", 1); err != nil || got != nil {
|
||||
t.Fatalf("TaskByIssue on empty store = (%+v, %v)", got, err)
|
||||
}
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "sam", "archie", 1, "title", "body", "bug"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := s.TaskByIssue(ctx, "sam", "archie", 1)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("TaskByIssue = (%+v, %v)", got, err)
|
||||
}
|
||||
if got.Title != "title" || got.IssueNumber != 1 {
|
||||
t.Fatalf("TaskByIssue = %+v", got)
|
||||
}
|
||||
|
||||
if got, err := s.TaskByIssue(ctx, "sam", "archie", 2); err != nil || got != nil {
|
||||
t.Fatalf("TaskByIssue for missing issue = (%+v, %v)", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWaitingTasksAndRequeue(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "sam", "archie", 1, "t", "b", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim = (%v, %v)", task, err)
|
||||
}
|
||||
|
||||
if waiting, err := s.WaitingTasks(ctx); err != nil || len(waiting) != 0 {
|
||||
t.Fatalf("WaitingTasks before transition = (%+v, %v)", waiting, err)
|
||||
}
|
||||
|
||||
if err := s.Transition(ctx, task.ID, StatusRunning, StatusWaitingHuman, "needs input"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
waiting, err := s.WaitingTasks(ctx)
|
||||
if err != nil || len(waiting) != 1 || waiting[0].ID != task.ID {
|
||||
t.Fatalf("WaitingTasks = (%+v, %v)", waiting, err)
|
||||
}
|
||||
|
||||
// Requeue with a workflow forces it (waiting_human -> approved -> implement).
|
||||
if err := s.Requeue(ctx, task.ID, StatusWaitingHuman, "implement"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
requeued, err := s.TaskByIssue(ctx, "sam", "archie", 1)
|
||||
if err != nil || requeued == nil || requeued.Status != StatusQueued || requeued.Workflow != "implement" {
|
||||
t.Fatalf("after forced requeue = %+v, %v", requeued, err)
|
||||
}
|
||||
|
||||
// Requeue with an empty workflow keeps the task's current workflow
|
||||
// (retrying a parked task).
|
||||
task2, err := s.ClaimNext(ctx)
|
||||
if err != nil || task2 == nil {
|
||||
t.Fatalf("second claim = (%v, %v)", task2, err)
|
||||
}
|
||||
task2.Workflow = "feasibility"
|
||||
if err := s.Update(ctx, task2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Transition(ctx, task2.ID, StatusRunning, StatusParked, "parked"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Requeue(ctx, task2.ID, StatusParked, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
retried, err := s.TaskByIssue(ctx, "sam", "archie", 1)
|
||||
if err != nil || retried == nil || retried.Status != StatusQueued || retried.Workflow != "feasibility" {
|
||||
t.Fatalf("after empty-workflow requeue = %+v, %v", retried, err)
|
||||
}
|
||||
if retried.ParkReason != "" {
|
||||
t.Fatalf("Requeue must clear park_reason, got %q", retried.ParkReason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTasksListingAndStatusCounts(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
for i, status := range []string{StatusQueued, StatusRunning, StatusPROpen} {
|
||||
if _, err := s.EnqueueIssue(ctx, "sam", "archie", i+1, "t", "b", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.TaskByIssue(ctx, "sam", "archie", i+1)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("TaskByIssue(%d) = (%+v, %v)", i+1, task, err)
|
||||
}
|
||||
if status != StatusQueued {
|
||||
if err := s.Transition(ctx, task.ID, StatusQueued, status, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks, err := s.Tasks(ctx, 10)
|
||||
if err != nil || len(tasks) != 3 {
|
||||
t.Fatalf("Tasks = (%d, %v)", len(tasks), err)
|
||||
}
|
||||
|
||||
limited, err := s.Tasks(ctx, 2)
|
||||
if err != nil || len(limited) != 2 {
|
||||
t.Fatalf("Tasks with limit = (%d, %v)", len(limited), err)
|
||||
}
|
||||
|
||||
counts, err := s.StatusCounts(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]int{StatusQueued: 1, StatusRunning: 1, StatusPROpen: 1}
|
||||
for status, n := range want {
|
||||
if counts[status] != n {
|
||||
t.Fatalf("StatusCounts[%s] = %d, want %d (all: %+v)", status, counts[status], n, counts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkflowStats(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "sam", "archie", 1, "t", "b", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim = (%v, %v)", task, err)
|
||||
}
|
||||
task.Workflow = "implement"
|
||||
task.TokensUsed = 1000
|
||||
task.Iterations = 4
|
||||
if err := s.Update(ctx, task); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Transition(ctx, task.ID, StatusRunning, StatusMerged, "merged"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
stats, err := s.WorkflowStats(ctx)
|
||||
if err != nil || len(stats) != 1 {
|
||||
t.Fatalf("WorkflowStats = (%+v, %v)", stats, err)
|
||||
}
|
||||
got := stats[0]
|
||||
if got.Workflow != "implement" || got.Runs != 1 || got.Merged != 1 || got.AvgTokens != 1000 || got.TotalToken != 1000 {
|
||||
t.Fatalf("WorkflowStats[0] = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecentEvents(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
for range 3 {
|
||||
if _, err := s.InsertEvent(ctx, events.Event{Kind: "log", Detail: "line"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
recent, err := s.RecentEvents(ctx, 2)
|
||||
if err != nil || len(recent) != 2 {
|
||||
t.Fatalf("RecentEvents = (%d, %v)", len(recent), err)
|
||||
}
|
||||
// Newest first.
|
||||
if recent[0].ID <= recent[1].ID {
|
||||
t.Fatalf("RecentEvents not newest-first: %+v", recent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokensByDay(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
today := time.Now().UTC()
|
||||
yesterday := today.AddDate(0, 0, -1)
|
||||
|
||||
if _, err := s.InsertEvent(ctx, events.Event{
|
||||
Kind: "agent_finish", At: today, Data: map[string]any{"tokens": 100},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.InsertEvent(ctx, events.Event{
|
||||
Kind: "agent_finish", At: today, Data: map[string]any{"tokens": 50},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.InsertEvent(ctx, events.Event{
|
||||
Kind: "agent_finish", At: yesterday, Data: map[string]any{"tokens": 25},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
byDay, err := s.TokensByDay(ctx, 14)
|
||||
if err != nil || len(byDay) != 2 {
|
||||
t.Fatalf("TokensByDay = (%+v, %v)", byDay, err)
|
||||
}
|
||||
// Newest day first.
|
||||
if byDay[0].Day != today.Format("2006-01-02") || byDay[0].Tokens != 150 {
|
||||
t.Fatalf("TokensByDay[0] = %+v", byDay[0])
|
||||
}
|
||||
if byDay[1].Day != yesterday.Format("2006-01-02") || byDay[1].Tokens != 25 {
|
||||
t.Fatalf("TokensByDay[1] = %+v", byDay[1])
|
||||
}
|
||||
|
||||
limited, err := s.TokensByDay(ctx, 1)
|
||||
if err != nil || len(limited) != 1 {
|
||||
t.Fatalf("TokensByDay with limit = (%+v, %v)", limited, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
)
|
||||
|
||||
func newTestServer(t *testing.T) *Server {
|
||||
t.Helper()
|
||||
s, err := store.Open(filepath.Join(t.TempDir(), "test.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
return &Server{Store: s, Log: slog.New(slog.NewTextHandler(io.Discard, nil))}
|
||||
}
|
||||
|
||||
func TestHandleSummary(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
ctx := t.Context()
|
||||
if _, err := srv.Store.EnqueueIssue(ctx, "sam", "archie", 1, "t", "b", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/summary", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
|
||||
}
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, key := range []string{"statuses", "workflows", "stages", "tokens_by_day"} {
|
||||
if _, ok := got[key]; !ok {
|
||||
t.Errorf("summary missing key %q: %+v", key, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleTasks(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
ctx := t.Context()
|
||||
if _, err := srv.Store.EnqueueIssue(ctx, "sam", "archie", 1, "hello", "b", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/tasks", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
|
||||
}
|
||||
var got []store.Task
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Title != "hello" {
|
||||
t.Fatalf("tasks = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleTask(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
ctx := t.Context()
|
||||
if _, err := srv.Store.EnqueueIssue(ctx, "sam", "archie", 1, "t", "b", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := srv.Store.TaskByIssue(ctx, "sam", "archie", 1)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("TaskByIssue = (%+v, %v)", task, err)
|
||||
}
|
||||
if _, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "stage_start", TaskID: task.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/tasks/"+strconv.FormatInt(task.ID, 10), nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
|
||||
}
|
||||
var got []events.Event
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Kind != "stage_start" {
|
||||
t.Fatalf("task events = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleTaskBadID(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
req := httptest.NewRequest("GET", "/api/tasks/not-a-number", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleIndex(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", w.Code)
|
||||
}
|
||||
if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/html") {
|
||||
t.Fatalf("content-type = %q", ct)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleSSEBacklogAndLive(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
ctx := t.Context()
|
||||
id, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "backlog"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ts := httptest.NewServer(srv.Handler())
|
||||
t.Cleanup(ts.Close)
|
||||
|
||||
resp, err := http.Get(ts.URL + "/events")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = resp.Body.Close() })
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(resp.Body)
|
||||
line, err := readLineUntil(reader, "backlog")
|
||||
if err != nil {
|
||||
t.Fatalf("backlog event not received: %v (%q)", err, line)
|
||||
}
|
||||
|
||||
// Now broadcast a live event and confirm it's delivered too. Give
|
||||
// the SSE handler a moment to register its subscriber.
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
srv.Broadcast(events.Event{ID: id + 1, Kind: "log", Detail: "live"})
|
||||
|
||||
if _, err := readLineUntil(reader, "live"); err != nil {
|
||||
t.Fatalf("live event not received: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// readLineUntil scans SSE "data:" lines until one contains want.
|
||||
func readLineUntil(r *bufio.Reader, want string) (string, error) {
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
line, err := r.ReadString('\n')
|
||||
if err != nil {
|
||||
return line, err
|
||||
}
|
||||
if strings.HasPrefix(line, "data:") && strings.Contains(line, want) {
|
||||
return line, nil
|
||||
}
|
||||
}
|
||||
return "", io.EOF
|
||||
}
|
||||
Reference in new issue
Block a user