first pass
This commit is contained in:
commit
7822529a38
31 files changed
+7050
No files matched your search
@@ -0,0 +1,429 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
func TestCapacity(t *testing.T) {
|
||||
c := Capacity{Usable: Size{MemoryMB: 6144, CPUs: 3}}
|
||||
def := Size{MemoryMB: 2048, CPUs: 1}
|
||||
if got := c.Slots(def); got != 3 {
|
||||
t.Fatalf("slots = %d, want 3", got)
|
||||
}
|
||||
if !c.Room(Size{4096, 2}, def) {
|
||||
t.Fatal("third container should fit")
|
||||
}
|
||||
if c.Room(Size{6144, 3}, def) {
|
||||
t.Fatal("fourth container should not fit")
|
||||
}
|
||||
if c.Room(Size{2048, 2.5}, def) {
|
||||
t.Fatal("should be blocked by CPU")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigExample(t *testing.T) {
|
||||
t.Setenv("SLACK_APP_TOKEN", "xapp-test")
|
||||
t.Setenv("SLACK_BOT_TOKEN", "xoxb-test")
|
||||
t.Setenv("ANTHROPIC_API_KEY", "sk-test")
|
||||
t.Setenv("CLUSTER_SECRET", "cluster-secret-1234567890")
|
||||
c, err := LoadConfig("config.example.toml", ModeMaster)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.IdleTimeout != 6*time.Hour || c.Container.MemoryMB != 2048 || c.MaxContainer.CPUs != 4 {
|
||||
t.Fatalf("unexpected config: %+v", c)
|
||||
}
|
||||
if c.Storage.MinMB != 5120 || c.Storage.MaxMB != 10240 || len(c.Drives) != 2 || c.Drives[0].ReserveMB != 20480 {
|
||||
t.Fatalf("unexpected storage config: %+v %+v", c.Storage, c.Drives)
|
||||
}
|
||||
if c.AgentEnv["ANTHROPIC_API_KEY"] != "sk-test" {
|
||||
t.Fatal("env var not expanded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStore(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "state.json")
|
||||
s, err := OpenStore(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = s.PutOwner(&Owner{ID: "C1", Kind: OwnerChannel, Machine: "c-c1", Size: Size{1024, 1}})
|
||||
_ = s.AddSession(&Session{Channel: "D1", ThreadTS: "1.0", OwnerID: "C1", ACPID: "a", CreatedAt: time.Now().Add(-time.Hour)})
|
||||
_ = s.AddSession(&Session{Channel: "D1", ThreadTS: "2.0", OwnerID: "C1", ACPID: "b", CreatedAt: time.Now()})
|
||||
|
||||
s2, err := OpenStore(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := s2.OwnerByMachine("c-c1"); u == nil || u.Size.MemoryMB != 1024 {
|
||||
t.Fatal("user not saved")
|
||||
}
|
||||
list := s2.Sessions("C1", 0)
|
||||
if len(list) != 2 || list[0].ACPID != "b" {
|
||||
t.Fatalf("sessions not newest-first: %+v", list)
|
||||
}
|
||||
if s2.Session("D1", "1.0").ACPID != "a" {
|
||||
t.Fatal("session lookup failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOwnership(t *testing.T) {
|
||||
dm := incoming{user: "U42", channel: "D9", dm: true}
|
||||
if id, kind := dm.owner(); id != "U42" || kind != OwnerUser {
|
||||
t.Fatalf("DM should be owned by the user, got %s %s", id, kind)
|
||||
}
|
||||
ch := incoming{user: "U42", channel: "C7"}
|
||||
if id, kind := ch.owner(); id != "C7" || kind != OwnerChannel {
|
||||
t.Fatalf("channel message should be owned by the channel, got %s %s", id, kind)
|
||||
}
|
||||
other := incoming{user: "U99", channel: "C7"}
|
||||
a, ka := ch.owner()
|
||||
b, kb := other.owner()
|
||||
if MachineName(a, ka) != MachineName(b, kb) || MachineName(a, ka) != "c-c7" {
|
||||
t.Fatal("two people in one channel should share a container")
|
||||
}
|
||||
if MachineName("U42", OwnerUser) != "u-u42" || !ours("u-u42") || !ours("c-c7") || ours("web") {
|
||||
t.Fatal("machine naming is off")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseScale(t *testing.T) {
|
||||
cur := Size{MemoryMB: 2048, CPUs: 1}
|
||||
cases := []struct {
|
||||
in string
|
||||
want Size
|
||||
}{
|
||||
{"", Size{4096, 2}},
|
||||
{"up", Size{4096, 2}},
|
||||
{"down", Size{1024, 0.5}},
|
||||
{"8G 4cpu", Size{8192, 4}},
|
||||
{"8gb", Size{8192, 1}},
|
||||
{"+2G", Size{4096, 1}},
|
||||
{"-1cpu", Size{2048, 0}},
|
||||
{"512M +0.5cpu", Size{512, 1.5}},
|
||||
{"1.5G 3 cores", Size{1536, 3}},
|
||||
{"+2 GB", Size{4096, 1}},
|
||||
{"+1cpus -512mb", Size{1536, 2}},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := parseScale(c.in, cur)
|
||||
if err != nil || got != c.want {
|
||||
t.Fatalf("scale %q = %v, %v; want %v", c.in, got, err, c.want)
|
||||
}
|
||||
}
|
||||
if _, err := parseScale("the database to 3 replicas", cur); !errors.Is(err, errNotCommand) {
|
||||
t.Fatal("a normal sentence should go to the agent")
|
||||
}
|
||||
if _, err := parseScale("4G lots", cur); err == nil || errors.Is(err, errNotCommand) {
|
||||
t.Fatal("a bad second word should be a usage error")
|
||||
}
|
||||
if _, err := parseScale("4G 8G", cur); err == nil {
|
||||
t.Fatal("memory twice should fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseKeep(t *testing.T) {
|
||||
for in, want := range map[string]time.Duration{
|
||||
"24h": 24 * time.Hour, "3d": 72 * time.Hour, "1d12h": 36 * time.Hour, "2w": 336 * time.Hour,
|
||||
"5 hours": 5 * time.Hour, "2 days": 48 * time.Hour, "1d 6h": 30 * time.Hour, "90 mins": 90 * time.Minute,
|
||||
} {
|
||||
k, err := parseKeep(in)
|
||||
if err != nil || k.limit != want {
|
||||
t.Fatalf("keep %q = %+v, %v; want %s", in, k, err, want)
|
||||
}
|
||||
}
|
||||
for _, in := range []string{"indefinite", "Indefinitely", "forever"} {
|
||||
if k, _ := parseKeep(in); !k.indefinite {
|
||||
t.Fatalf("keep %q should be indefinite", in)
|
||||
}
|
||||
}
|
||||
if k, _ := parseKeep("default"); !k.reset {
|
||||
t.Fatal("keep default should reset")
|
||||
}
|
||||
if k, _ := parseKeep(""); !k.show {
|
||||
t.Fatal("bare keep should show")
|
||||
}
|
||||
if _, err := parseKeep("the tests passing"); !errors.Is(err, errNotCommand) {
|
||||
t.Fatal("a normal sentence should go to the agent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHuman(t *testing.T) {
|
||||
for d, want := range map[time.Duration]string{
|
||||
6 * time.Hour: "6h", 36 * time.Hour: "1d12h", 90 * time.Minute: "1h30m",
|
||||
45 * time.Minute: "45m", 168 * time.Hour: "7d", 0: "0m",
|
||||
} {
|
||||
if got := human(d); got != want {
|
||||
t.Fatalf("human(%s) = %q, want %q", d, got, want)
|
||||
}
|
||||
}
|
||||
if got := (Size{8192, 4}).String(); got != "8 GB RAM, 4 CPUs" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if got := (Size{512, 0.25}).String(); got != "512 MB RAM, 0.25 CPUs" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizes(t *testing.T) {
|
||||
for in, want := range map[string]int64{"5G": 5120, "500M": 500, "1.5gb": 1536, "1T": 1048576, "20 GB": 20480} {
|
||||
if got, err := parseSizeMB(in); err != nil || got != want {
|
||||
t.Fatalf("parseSizeMB(%q) = %d, %v; want %d", in, got, err, want)
|
||||
}
|
||||
}
|
||||
if _, err := parseSizeMB("+5G"); err == nil {
|
||||
t.Fatal("a signed size isn't a config size")
|
||||
}
|
||||
for mb, want := range map[int64]string{512: "512 MB", 5120: "5 GB", 2560: "2.5 GB", 1258291: "1.2 TB"} {
|
||||
if got := formatMB(mb); got != want {
|
||||
t.Fatalf("formatMB(%d) = %q, want %q", mb, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseStorage(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
min, max int64
|
||||
}{
|
||||
{"20G", 5120, 20480},
|
||||
{"+5G", 5120, 15360},
|
||||
{"-2G", 5120, 8192},
|
||||
{"max 30G", 5120, 30720},
|
||||
{"min 8G", 8192, 10240},
|
||||
{"min +1G max 30 GB", 6144, 30720},
|
||||
{"cap 1T", 5120, 1048576},
|
||||
}
|
||||
for _, c := range cases {
|
||||
min, max, err := parseStorage(c.in, 5120, 10240)
|
||||
if err != nil || min != c.min || max != c.max {
|
||||
t.Fatalf("storage %q = %d/%d, %v; want %d/%d", c.in, min, max, err, c.min, c.max)
|
||||
}
|
||||
}
|
||||
if _, _, err := parseStorage("is full, clean up the logs", 5120, 10240); !errors.Is(err, errNotCommand) {
|
||||
t.Fatal("a normal sentence should go to the agent")
|
||||
}
|
||||
for _, in := range []string{"min", "20G 30G", "min 5G min 6G", "20G lots"} {
|
||||
if _, _, err := parseStorage(in, 5120, 10240); err == nil || errors.Is(err, errNotCommand) {
|
||||
t.Fatalf("%q should be a usage error, got %v", in, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriveMath(t *testing.T) {
|
||||
d := DriveReport{Path: "/a", UsableMB: 100_000, CommittedMB: 90_000, BookedMB: 150_000}
|
||||
if d.HeadroomMB() != 10_000 || !d.Overbooked() {
|
||||
t.Fatalf("headroom %d overbooked %v", d.HeadroomMB(), d.Overbooked())
|
||||
}
|
||||
if committed(5120, 8000) != 8000 || committed(5120, 100) != 5120 {
|
||||
t.Fatal("a home holds its guarantee or its real use, whichever is bigger")
|
||||
}
|
||||
drives := []DriveReport{d, {Path: "/b", UsableMB: 50_000, CommittedMB: 20_000}}
|
||||
if got, _ := pickDrive(drives, 5120); got != "/b" {
|
||||
t.Fatalf("should pick the drive with the most room, got %s", got)
|
||||
}
|
||||
if _, err := pickDrive(drives, 40_000); !errors.Is(err, ErrStorageFull) {
|
||||
t.Fatal("a guarantee that fits nowhere should be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseQgroupShow(t *testing.T) {
|
||||
out := "Qgroupid Referenced Exclusive Max referenced Max exclusive Path\n" +
|
||||
"-------- ---------- --------- -------------- ------------- ----\n" +
|
||||
"0/257 3221225472 3221225472 10737418240 none c-c07abc\n"
|
||||
if mb, err := parseQgroupShow(out); err != nil || mb != 3072 {
|
||||
t.Fatalf("got %d, %v", mb, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTitle(t *testing.T) {
|
||||
if got := title(" fix the build\nplease"); got != "fix the build" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if got := title(strings.Repeat("é", 80)); !strings.HasSuffix(got, "…") || len([]rune(got)) != 61 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
|
||||
func fakeGoose(t *testing.T) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/acp" || r.Header.Get("X-Secret-Key") != "s3cret" {
|
||||
http.Error(w, "nope", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
c, err := websocket.Accept(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ctx := context.Background()
|
||||
send := func(v any) { b, _ := json.Marshal(v); _ = c.Write(ctx, websocket.MessageText, b) }
|
||||
var promptID json.RawMessage
|
||||
|
||||
for {
|
||||
_, data, err := c.Read(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var m rpcMsg
|
||||
_ = json.Unmarshal(data, &m)
|
||||
switch m.Method {
|
||||
case "initialize":
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": map[string]any{
|
||||
"protocolVersion": 1, "agentCapabilities": map[string]any{"loadSession": true}}})
|
||||
case "session/new":
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": map[string]any{"sessionId": "sess-1"}})
|
||||
case "session/load":
|
||||
send(map[string]any{"jsonrpc": "2.0", "method": "session/update", "params": map[string]any{
|
||||
"sessionId": "sess-old", "update": map[string]any{"sessionUpdate": "agent_message_chunk",
|
||||
"content": map[string]any{"type": "text", "text": "old history"}}}})
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": nil})
|
||||
case "session/prompt":
|
||||
promptID = m.ID
|
||||
upd := func(u map[string]any) {
|
||||
send(map[string]any{"jsonrpc": "2.0", "method": "session/update",
|
||||
"params": map[string]any{"sessionId": "sess-1", "update": u}})
|
||||
}
|
||||
upd(map[string]any{"sessionUpdate": "agent_message_chunk", "content": map[string]any{"type": "text", "text": "Hello "}})
|
||||
upd(map[string]any{"sessionUpdate": "tool_call", "toolCallId": "t1", "title": "ls /root", "status": "pending"})
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": "perm-1", "method": "session/request_permission",
|
||||
"params": map[string]any{"sessionId": "sess-1", "options": []map[string]any{
|
||||
{"optionId": "no", "name": "Reject", "kind": "reject_once"},
|
||||
{"optionId": "yes", "name": "Allow", "kind": "allow_once"}}}})
|
||||
case "":
|
||||
var res struct {
|
||||
Outcome struct{ Outcome, OptionID string } `json:"outcome"`
|
||||
}
|
||||
_ = json.Unmarshal(m.Result, &res)
|
||||
text := "world"
|
||||
if res.Outcome.OptionID != "yes" {
|
||||
text = "PERMISSION WRONG"
|
||||
}
|
||||
send(map[string]any{"jsonrpc": "2.0", "method": "session/update", "params": map[string]any{
|
||||
"sessionId": "sess-1", "update": map[string]any{"sessionUpdate": "agent_message_chunk",
|
||||
"content": map[string]any{"type": "text", "text": text}}}})
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": promptID, "result": map[string]any{"stopReason": "end_turn"}})
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestACP(t *testing.T) {
|
||||
srv := fakeGoose(t)
|
||||
defer srv.Close()
|
||||
addr := strings.TrimPrefix(srv.URL, "http://")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if _, err := DialACP(ctx, addr, "wrong"); err == nil {
|
||||
t.Fatal("wrong secret should fail")
|
||||
}
|
||||
a, err := DialACP(ctx, addr, "s3cret")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer a.Close()
|
||||
if !a.CanLoad {
|
||||
t.Fatal("loadSession capability not read")
|
||||
}
|
||||
id, err := a.NewSession(ctx, "/root")
|
||||
if err != nil || id != "sess-1" {
|
||||
t.Fatalf("new session: %q %v", id, err)
|
||||
}
|
||||
if err := a.LoadSession(ctx, "sess-old", "/root"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var mu sync.Mutex
|
||||
var text strings.Builder
|
||||
var tools []string
|
||||
stop, err := a.Prompt(ctx, id, "hi", func(u Update) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
text.WriteString(u.Text)
|
||||
if u.Kind == "tool_call" {
|
||||
tools = append(tools, u.Title)
|
||||
}
|
||||
})
|
||||
if err != nil || stop != "end_turn" {
|
||||
t.Fatalf("prompt: %q %v", stop, err)
|
||||
}
|
||||
if text.String() != "Hello world" || len(tools) != 1 || tools[0] != "ls /root" {
|
||||
t.Fatalf("got text %q tools %v", text.String(), tools)
|
||||
}
|
||||
}
|
||||
|
||||
type fakeSlack struct {
|
||||
mu sync.Mutex
|
||||
msgs map[string]string
|
||||
order []string
|
||||
n int
|
||||
}
|
||||
|
||||
func (f *fakeSlack) handler(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
ts := r.Form.Get("ts")
|
||||
if strings.HasSuffix(r.URL.Path, "chat.postMessage") {
|
||||
f.n++
|
||||
ts = "ts" + string(rune('0'+f.n))
|
||||
f.order = append(f.order, ts)
|
||||
}
|
||||
f.msgs[ts] = r.Form.Get("text")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "channel": "D1", "ts": ts, "text": f.msgs[ts]})
|
||||
}
|
||||
|
||||
func TestStreamerSplitsLongReplies(t *testing.T) {
|
||||
f := &fakeSlack{msgs: map[string]string{"ph": ""}, order: []string{"ph"}}
|
||||
srv := httptest.NewServer(http.HandlerFunc(f.handler))
|
||||
defer srv.Close()
|
||||
api := slack.New("xoxb-test", slack.OptionAPIURL(srv.URL+"/"))
|
||||
|
||||
st := NewStreamer(api, "D1", "root", "ph")
|
||||
line := strings.Repeat("x", 99) + "\n"
|
||||
for i := 0; i < 80; i++ {
|
||||
st.Update(Update{Kind: "agent_message_chunk", Text: line})
|
||||
}
|
||||
st.Finish("end_turn", nil)
|
||||
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if len(f.order) != 3 {
|
||||
t.Fatalf("want 3 messages, got %d", len(f.order))
|
||||
}
|
||||
var all strings.Builder
|
||||
for _, ts := range f.order {
|
||||
if len(f.msgs[ts]) > slackMsgLimit {
|
||||
t.Fatalf("message %s too long: %d", ts, len(f.msgs[ts]))
|
||||
}
|
||||
all.WriteString(f.msgs[ts])
|
||||
}
|
||||
if all.Len() != 8000 {
|
||||
t.Fatalf("text lost or duplicated: %d chars", all.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMoveTarget(t *testing.T) {
|
||||
b := &Bot{}
|
||||
for in, want := range map[string]string{"": "", "auto": "", "Slave-1": "slave-1", "to node-b": "node-b"} {
|
||||
if got, ok := b.moveTarget(in); !ok || got != want {
|
||||
t.Fatalf("move %q = %q %v, want %q", in, got, ok, want)
|
||||
}
|
||||
}
|
||||
if _, ok := b.moveTarget("the files into the build folder"); ok {
|
||||
t.Fatal("a normal sentence should go to the agent")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user