package opencode import ( "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "path/filepath" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // TestConnectionServerUser pins the owned serve basic-auth user per backend: the // native serve authenticates "pipelines", the V2 API server authenticates // "opencode" (verified against the installed 2.0.20 binary, which ignores // OPENCODE_SERVER_USERNAME). A wrong user is an unauthenticated endpoint, so it // is asserted rather than assumed. func TestConnectionServerUser(t *testing.T) { if got := (&Connection{Backend: "v1"}).serverUser(); got != "pipelines" { t.Fatalf("v1 server user = %q, want pipelines", got) } if got := (&Connection{Backend: "v2"}).serverUser(); got != "opencode" { t.Fatalf("v2 server user = %q, want opencode", got) } } // TestChatV2PromptShapeAndAuth pins the outbound V2 prompt: a single "text" // field (the installed service's PromptInput schema requires "text" and rejects // a bare "parts" array) sent with the "opencode" basic-auth user. The same body // also drives the v1 translation, so no second request format exists. func TestChatV2PromptShapeAndAuth(t *testing.T) { type request struct { method, path, user, pass, body string } var got request srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data := make([]byte, r.ContentLength) _, _ = r.Body.Read(data) user, pass, _ := r.BasicAuth() got = request{method: r.Method, path: r.URL.Path, user: user, pass: pass, body: string(data)} w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`)) })) defer srv.Close() chat := NewChat(&Connection{Backend: "v2", URL: srv.URL, password: "deadbeef"}) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := chat.Prompt(ctx, "ses_1", "hello"); err != nil { t.Fatalf("prompt: %v", err) } if got.method != http.MethodPost || got.path != "/api/session/ses_1/prompt" { t.Fatalf("prompt request = %+v", got) } if got.body != `{"text":"hello"}` { t.Fatalf("prompt body = %q, want the V2 text shape", got.body) } if got.user != "opencode" || got.pass != "deadbeef" { t.Fatalf("prompt auth = %q/%q, want opencode/deadbeef", got.user, got.pass) } } // TestChatStreamRealV2Shapes replays the exact event frames the installed 2.0.20 // service emitted on /api/event during a live turn (captured, not documented): // a step start, token text delta, usage and a step end whose data carries no // step index. It asserts the existing firehose mapper surfaces them with stable // session/message ids and that the usage frame inherits the retained message id. func TestChatStreamRealV2Shapes(t *testing.T) { frames := []string{ `{"id":"evt_0","created":0,"type":"session.instructions.updated","data":{"sessionID":"ses_1","delta":{"core/instructions":"abc","core/date":"def"}}}`, `{"id":"evt_1","created":1,"type":"session.step.started","data":{"sessionID":"ses_1","agent":"build","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go","variant":"default"},"assistantMessageID":"msg_1","started":1}}`, `{"id":"evt_2","created":2,"type":"session.text.started","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0}}`, `{"id":"evt_3","created":3,"type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"Where granite meets"}}`, `{"id":"evt_4","created":4,"type":"session.usage.updated","data":{"sessionID":"ses_1","cost":0.0005,"tokens":{"input":94,"output":304}}}`, `{"id":"evt_5","created":5,"type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","finish":"stop","tokens":{"input":94,"output":304},"cost":0.0005}}`, } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet || r.URL.Path != "/api/event" { t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) return } w.Header().Set("Content-Type", "text/event-stream") for _, frame := range frames { _, _ = w.Write([]byte("data: " + frame + "\n\n")) } })) defer srv.Close() chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var got []protocol.ChatEventFrame if err := chat.Stream(ctx, "ses_1", nil, func(frame protocol.ChatEventFrame) error { got = append(got, frame) return nil }); err != nil { t.Fatalf("stream: %v", err) } // text.started carries no delta and is skipped; the four remaining frames // (step start, text delta, usage, step end) must all surface. if len(got) != 4 { t.Fatalf("frames = %d, want 4: %+v", len(got), got) } for _, frame := range got { if frame.SessionID != "ses_1" || frame.MessageID != "msg_1" { t.Fatalf("frame missing stable ids: %+v", frame) } } if got[0].Kind != protocol.ChatEventStep || got[0].Step == nil || got[0].Step.Status != protocol.ChatStepStarted { t.Fatalf("step start frame = %+v", got[0]) } if got[1].Kind != protocol.ChatEventText || got[1].Delta != "Where granite meets" { t.Fatalf("text frame = %+v", got[1]) } if got[2].Kind != protocol.ChatEventUsage || got[2].Usage == nil || got[2].Usage.InputTokens != 94 || got[2].Usage.OutputTokens != 304 { t.Fatalf("usage frame = %+v", got[2]) } if got[3].Kind != protocol.ChatEventStep || got[3].Step == nil || got[3].Step.Status != protocol.ChatStepEnded { t.Fatalf("step end frame = %+v", got[3]) } } // TestChatDeltaPartIDsNeverCollide pins the broker's part-id namespace: OpenCode // numbers one message's text and reasoning parts independently from 0, so a // message's first text delta and its first reasoning delta share an ordinal. The // two must still land on distinct part ids — otherwise the client appends the // answer into the thinking block and the server's retained state re-collides on a // resume. The id is the shared ChatDeltaPartID derivation, so it is deterministic // across a reconnect. func TestChatDeltaPartIDsNeverCollide(t *testing.T) { payloads := map[string]string{ "text": `{"type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"answer"}}`, "reasoning": `{"type":"session.reasoning.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"think"}}`, } ids := map[string]string{} for kind, payload := range payloads { last := "" frame, ok, err := NormalizeChatEvent([]byte(payload), "ses_1", &last) if err != nil || !ok { t.Fatalf("%s delta: ok=%v err=%v", kind, ok, err) } if frame.Part == nil { t.Fatalf("%s delta carried no part", kind) } if frame.Part.Type != kind { t.Fatalf("%s delta part type = %q", kind, frame.Part.Type) } ids[kind] = frame.Part.ID } if ids["text"] == ids["reasoning"] { t.Fatalf("text and reasoning share part id %q", ids["text"]) } if ids["text"] != protocol.ChatDeltaPartID("msg_1", protocol.ChatPartText, 0) { t.Fatalf("text part id = %q, want the stable derivation", ids["text"]) } if ids["reasoning"] != protocol.ChatDeltaPartID("msg_1", protocol.ChatPartReasoning, 0) { t.Fatalf("reasoning part id = %q, want the stable derivation", ids["reasoning"]) } } // TestChatToolLifecycleCarriesNameAndInput pins what the broker learns from each // verified tool frame: input.started carries the name, input.ended carries the // arguments as its `text` string, called carries the parsed input object, and // progress/success carry neither — so those frames must surface no name and no // input, leaving the retention to the consumer instead of blanking the field. func TestChatToolLifecycleCarriesNameAndInput(t *testing.T) { frames := []struct { name string payload string wantStatus string wantName string wantInput string // the JSON encoding expected, "" for absent }{ { name: "input.started names the call", payload: `{"type":"session.tool.input.started","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell"}}`, wantStatus: protocol.ChatToolStreaming, wantName: "shell", }, { name: "input.ended carries the arguments as text", payload: `{"type":"session.tool.input.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","text":"{\"command\":\"ls\"}"}}`, wantStatus: protocol.ChatToolStreaming, wantInput: `{"command":"ls"}`, }, { name: "called carries the parsed input object", payload: `{"type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","input":{"command":"ls"}}}`, wantStatus: protocol.ChatToolRunning, wantInput: `{"command":"ls"}`, }, { name: "progress carries metadata only", payload: `{"type":"session.tool.progress","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","metadata":{"exit":0}}}`, wantStatus: protocol.ChatToolRunning, }, { name: "success carries the output and neither name nor input", payload: `{"type":"session.tool.success","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","content":[{"type":"text","text":"ok"}],"metadata":{"exit":0},"executed":true}}`, wantStatus: protocol.ChatToolCompleted, }, } for _, tc := range frames { t.Run(tc.name, func(t *testing.T) { last := "" frame, ok, err := NormalizeChatEvent([]byte(tc.payload), "ses_1", &last) if err != nil || !ok { t.Fatalf("ok=%v err=%v", ok, err) } if frame.Part == nil || frame.Part.State == nil { t.Fatalf("tool frame = %+v", frame) } if frame.Part.State.Status != tc.wantStatus { t.Fatalf("status = %q, want %q", frame.Part.State.Status, tc.wantStatus) } if frame.Part.Name != tc.wantName { t.Fatalf("name = %q, want %q", frame.Part.Name, tc.wantName) } if tc.wantInput == "" { if len(frame.Part.State.Input) != 0 { t.Fatalf("input = %s, want absent", frame.Part.State.Input) } return } if !json.Valid(frame.Part.State.Input) { t.Fatalf("input %s is not valid JSON", frame.Part.State.Input) } var want, got any if err := json.Unmarshal([]byte(tc.wantInput), &want); err != nil { t.Fatal(err) } if err := json.Unmarshal(frame.Part.State.Input, &got); err != nil { t.Fatalf("input %s: %v", frame.Part.State.Input, err) } if fmt.Sprintf("%v", want) != fmt.Sprintf("%v", got) { t.Fatalf("input = %s, want %s", frame.Part.State.Input, tc.wantInput) } }) } } // TestChatToolInputFromPartialText pins the streaming case: input.ended's text // that is not yet a complete JSON value must still surface, carried as a JSON // string so the frame stays valid and the client can show the partial arguments. func TestChatToolInputFromPartialText(t *testing.T) { last := "" frame, ok, err := NormalizeChatEvent([]byte(`{"type":"session.tool.input.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","text":"{\"command\":\"l"}}`), "ses_1", &last) if err != nil || !ok { t.Fatalf("ok=%v err=%v", ok, err) } if frame.Part == nil || frame.Part.State == nil { t.Fatalf("frame = %+v", frame) } if !json.Valid(frame.Part.State.Input) { t.Fatalf("partial input %s is not valid JSON", frame.Part.State.Input) } var got string if err := json.Unmarshal(frame.Part.State.Input, &got); err != nil { t.Fatalf("partial input is not a JSON string: %s (%v)", frame.Part.State.Input, err) } if got != `{"command":"l` { t.Fatalf("partial input text = %q", got) } } // TestOpenStreamRejectsUnknownBackend pins that the shared construction path // validates the backend before any process is started, so OpenStream never // launches a serve for an unknown backend. func TestOpenStreamRejectsUnknownBackend(t *testing.T) { if _, err := OpenStream(context.Background(), "opencode", t.TempDir(), "v3"); err == nil { t.Fatal("an unknown backend must be rejected") } } // TestOpenChatRejectsUnrunnableCLI pins that OpenChat surfaces detection failure // (an executable that cannot run) instead of opening a half-configured endpoint. func TestOpenChatRejectsUnrunnableCLI(t *testing.T) { missing := filepath.Join(t.TempDir(), "no-such-opencode") if _, err := OpenChat(context.Background(), missing, ""); err == nil { t.Fatal("a missing CLI must fail detection, not open an endpoint") } }