nandi/oripublic Fork 0
ca7e020cbb6442f047e0002eb9831ba29f8a17af
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

ws_test.go · 133 lines · 4.1 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package bridge_test

import (
	"context"
	"net/http/httptest"
	"strings"
	"testing"
	"time"

	acp "github.com/coder/acp-go-sdk"
	"github.com/coder/websocket"
	"github.com/coder/websocket/wsjson"

	"rickub.com/bots-garden/ori/internal/bridge"
)

// dialTestServer starts an HTTP test server exposing the bridge's WebSocket
// handler and connects a client to it.
func dialTestServer(t *testing.T, b *bridge.Bridge) (*websocket.Conn, context.Context) {
	t.Helper()
	server := httptest.NewServer(b.WebSocketHandler())
	t.Cleanup(server.Close)

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	t.Cleanup(cancel)

	url := "ws" + strings.TrimPrefix(server.URL, "http")
	conn, _, err := websocket.Dial(ctx, url, nil)
	if err != nil {
		t.Fatalf("websocket dial failed: %v", err)
	}
	t.Cleanup(func() { _ = conn.CloseNow() })
	return conn, ctx
}

// readUntil reads messages until the wanted type arrives.
func readUntil(t *testing.T, ctx context.Context, conn *websocket.Conn, wantType string) bridge.Outgoing {
	t.Helper()
	for {
		var msg bridge.Outgoing
		if err := wsjson.Read(ctx, conn, &msg); err != nil {
			t.Fatalf("websocket read failed while waiting for %q: %v", wantType, err)
		}
		if msg.Type == wantType {
			return msg
		}
	}
}

func TestWebSocketHelloThenPromptTurn(t *testing.T) {
	b := bridge.New(nil)
	b.SetSession(&fakeSession{})
	conn, ctx := dialTestServer(t, b)

	hello := readUntil(t, ctx, conn, bridge.OutgoingHello)
	if hello.SessionId != "sess-fake" {
		t.Errorf("hello sessionId = %q, want sess-fake", hello.SessionId)
	}

	if err := wsjson.Write(ctx, conn, bridge.Incoming{Type: bridge.IncomingPrompt, Text: "hi"}); err != nil {
		t.Fatalf("websocket write failed: %v", err)
	}
	readUntil(t, ctx, conn, bridge.OutgoingTurnStarted)
	ended := readUntil(t, ctx, conn, bridge.OutgoingTurnEnded)
	if ended.StopReason != string(acp.StopReasonEndTurn) {
		t.Errorf("turn_ended stopReason = %q, want end_turn", ended.StopReason)
	}
}

func TestWebSocketReceivesSessionUpdates(t *testing.T) {
	b := bridge.New(nil)
	b.SetSession(&fakeSession{})
	conn, ctx := dialTestServer(t, b)
	readUntil(t, ctx, conn, bridge.OutgoingHello)

	b.HandleSessionUpdate(context.Background(), acp.SessionNotification{
		SessionId: "sess-fake",
		Update:    acp.UpdateAgentMessageText("streamed"),
	})

	update := readUntil(t, ctx, conn, bridge.OutgoingSessionUpdate)
	if update.Update == nil || !strings.Contains(string(update.Update), "streamed") {
		t.Errorf("session_update payload = %s, want it to carry the streamed text", update.Update)
	}
}

func TestWebSocketPermissionRoundTrip(t *testing.T) {
	b := bridge.New(nil)
	b.SetSession(&fakeSession{})
	conn, ctx := dialTestServer(t, b)
	readUntil(t, ctx, conn, bridge.OutgoingHello)

	done := make(chan acp.RequestPermissionResponse, 1)
	go func() {
		resp, _ := b.HandlePermissionRequest(context.Background(), acp.RequestPermissionRequest{
			SessionId: "sess-fake",
			Options:   []acp.PermissionOption{{OptionId: "allow", Name: "Allow", Kind: acp.PermissionOptionKindAllowOnce}},
		})
		done <- resp
	}()

	request := readUntil(t, ctx, conn, bridge.OutgoingPermissionRequest)
	if err := wsjson.Write(ctx, conn, bridge.Incoming{
		Type:      bridge.IncomingPermissionResponse,
		RequestId: request.RequestId,
		OptionId:  "allow",
	}); err != nil {
		t.Fatalf("websocket write failed: %v", err)
	}

	resp := <-done
	if resp.Outcome.Selected == nil || resp.Outcome.Selected.OptionId != "allow" {
		t.Errorf("outcome = %+v, want selected \"allow\"", resp.Outcome)
	}
	readUntil(t, ctx, conn, bridge.OutgoingPermissionResolved)
}

func TestWebSocketLateJoinerGetsReplay(t *testing.T) {
	b := bridge.New(nil)
	b.SetSession(&fakeSession{})

	b.HandleSessionUpdate(context.Background(), acp.SessionNotification{
		SessionId: "sess-fake",
		Update:    acp.UpdateAgentMessageText("before you arrived"),
	})

	conn, ctx := dialTestServer(t, b)
	readUntil(t, ctx, conn, bridge.OutgoingHello)
	update := readUntil(t, ctx, conn, bridge.OutgoingSessionUpdate)
	if !strings.Contains(string(update.Update), "before you arrived") {
		t.Errorf("replayed update = %s, want the pre-connection chunk", update.Update)
	}
}