Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

18.0 KB
Raw Blame
package httpapi

import (
	"bytes"
	"context"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"path/filepath"
	"slices"
	"strings"
	"testing"

	"agentflow/internal/domain"
	"agentflow/internal/engine"
	"agentflow/internal/repo"
	"agentflow/internal/runtime"
	"agentflow/internal/tracker"

	"github.com/gin-gonic/gin"
)

func TestSharedCodeBuddyEffortsIgnoreAnOldPersonalOverride(t *testing.T) {
	st, member := sponsorFixture(t)
	stubCLIModelDiscovery(t)
	h := &assistantHandlers{
		deps: &deps{repo: st},
		generate: func(_ context.Context, _ string, user domain.User) (string, error) {
			if len(user.CodeBuddyModels) != 0 || user.ID != member.ID {
				t.Error("shared-key turn must use its own catalog and keep the caller's identity")
			}
			return `{"message":"ok","drafts":[]}`, nil
		},
		titleSummary: func(context.Context, domain.User, string) (string, error) { return "", context.Canceled },
	}
	const model = "deepseek-v4.1-flash"
	defaultOnly := []string{}
	models := []domain.CLIModel{{ID: model, ReasoningEfforts: &defaultOnly}}
	if err := st.SetPlatformCodeBuddyKey(repo.PlatformCodeBuddyKeyUpdate{APIKey: "shared-fixture", Models: []string{model}}, 0); err != nil {
		t.Fatal(err)
	}
	personal, err := st.UpdateUserCredentials(member.ID, repo.UserCredentialUpdate{CodeBuddyAPIKey: "own-fixture", CodeBuddyModels: &models})
	if err != nil {
		t.Fatal(err)
	}
	if err := validateAssistantEffort(context.Background(), personal, model, "low"); err == nil {
		t.Fatal("the personal key must still use its explicit override")
	}
	afterClear, err := st.UpdateUserCredentials(member.ID, repo.UserCredentialUpdate{CodeBuddyAPIKeyClear: true})
	if err != nil || len(afterClear.CodeBuddyModels) != 1 {
		t.Fatalf("expected the personal model catalog to remain saved after clearing its key: %v", err)
	}
	c, rec := workbenchContext(afterClear, http.MethodGet, "/api/assistant/models", &bytes.Buffer{})
	h.models(c)
	var catalog assistantModelCatalog
	if err := json.Unmarshal(rec.Body.Bytes(), &catalog); rec.Code != http.StatusOK || err != nil ||
		len(catalog.Models) != 1 || catalog.Models[0].ID != model || !slices.Contains(catalog.Models[0].Efforts, "low") {
		t.Fatalf("shared catalog: %d %s", rec.Code, rec.Body)
	}
	c, rec = workbenchContext(afterClear, http.MethodPut, "/api/assistant/sessions/shared-effort", bytes.NewBufferString(`{"model":"deepseek-v4.1-flash","effort":"low","executor":"codebuddy"}`))
	c.Params = gin.Params{{Key: "id", Value: "shared-effort"}}
	h.session(c)
	if rec.Code != http.StatusOK {
		t.Fatalf("session rejected the shared effort: %d %s", rec.Code, rec.Body)
	}
	c, rec = workbenchContext(afterClear, http.MethodPost, "/api/assistant/chat", bytes.NewBufferString(`{"sessionId":"shared-effort","model":"deepseek-v4.1-flash","effort":"low","messages":[{"role":"user","content":"Continue"}]}`))
	h.chat(c)
	if rec.Code != http.StatusOK {
		t.Fatalf("turn rejected the shared effort: %d %s", rec.Code, rec.Body)
	}
}

func sponsorFixture(t *testing.T) (*repo.Store, domain.User) {
	t.Helper()
	st, err := repo.OpenTest()
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(st.Close)
	for _, u := range []domain.User{
		{LoginName: "sponsor", CodeBuddyAPIKey: "sponsor-secret", CodeBuddyInternetEnvironment: "internal"},
		{LoginName: "newcomer", GitToken: "newcomer-git"},
	} {
		if err := st.PutUser(u); err != nil {
			t.Fatal(err)
		}
	}
	newcomer, _ := st.UserByLoginName("newcomer")
	project, _ := st.Project("platform")
	project.Members = append(project.Members, domain.ProjectMember{UserID: newcomer.ID, Role: "member"})
	if err := st.PutProject(project); err != nil {
		t.Fatal(err)
	}
	return st, newcomer
}

func sponsoredChat(h *assistantHandlers, user domain.User, model string) *httptest.ResponseRecorder {
	body := `{"projectId":"platform","model":"` + model + `","messages":[{"role":"user","content":"你好"}]}`
	req := httptest.NewRequest("POST", "/api/assistant/chat", strings.NewReader(body))
	req = req.WithContext(context.WithValue(req.Context(), requestUserContextKey{}, domainUserToAuth(user)))
	rec := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(rec)
	c.Request = req
	h.chat(c)
	return rec
}

func TestUsersWithoutKeyChatOnSponsoredModel(t *testing.T) {
	st, newcomer := sponsorFixture(t)
	stubCLIModelDiscovery(t)
	t.Setenv(assistantSponsorLoginEnv, "sponsor")
	var used domain.User
	h := &assistantHandlers{deps: &deps{repo: st}, generate: func(_ context.Context, _ string, u domain.User) (string, error) {
		used = u
		return `{"message":"你好呀","drafts":[]}`, nil
	}}

	for _, model := range []string{"", assistantSponsoredModel} {
		used = domain.User{}
		rec := sponsoredChat(h, newcomer, model)
		if rec.Code != 200 {
			t.Fatalf("model %q: %d %s", model, rec.Code, rec.Body.String())
		}
		if used.ID != newcomer.ID || used.CodeBuddyAPIKey != "sponsor-secret" || used.CodeBuddyInternetEnvironment != "internal" || used.GitToken != "newcomer-git" {
			t.Fatalf("turn must run as the newcomer with only the sponsor's CodeBuddy key: %+v", used)
		}
	}

	rec := sponsoredChat(h, newcomer, "claude-opus-5")
	if rec.Code != 400 || !strings.Contains(rec.Body.String(), "配置自己的 CodeBuddy API Key") {
		t.Fatalf("other models need an own key: %d %s", rec.Code, rec.Body.String())
	}
	if stored, _ := st.User(newcomer.ID); stored.CodeBuddyAPIKey != "" {
		t.Fatal("sponsor key must never be persisted on the newcomer")
	}

	for _, viewer := range []*domain.User{&newcomer, nil} {
		var catalog assistantModelCatalog
		mrec := httptest.NewRecorder()
		c, _ := gin.CreateTestContext(mrec)
		c.Request = httptest.NewRequest("GET", "/api/assistant/models", nil)
		want := assistantFixedCatalog().Default
		if viewer != nil {
			c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), requestUserContextKey{}, domainUserToAuth(*viewer)))
			want = assistantChatDefaultModel
		}
		h.models(c)
		// Everyone learns that a shared model exists; only the newcomer, who
		// would chat on it, gets it preselected.
		if json.Unmarshal(mrec.Body.Bytes(), &catalog) != nil || catalog.SponsoredModel != assistantSponsoredModel || catalog.Default != want {
			t.Fatalf("signed in %v: catalog = %s", viewer != nil, mrec.Body.String())
		}
		if strings.Contains(mrec.Body.String(), `"sponsor"`) || strings.Contains(mrec.Body.String(), "sponsor-secret") {
			t.Fatal("catalog must not reveal the sponsor account or key")
		}
	}
}

// A user with their own key may be on another network environment than the
// shared key, where the shared models do not exist: their catalog and default
// must stay their own, for /api/assistant/models and for the chat turn.
func TestOwnKeyKeepsItsEnvironmentDefaultWhileAKeyIsShared(t *testing.T) {
	st, member := sponsorFixture(t)
	stubCLIModelDiscovery(t)
	discoverCLIModels = func(_ context.Context, environment, _ string) []string {
		if environment == "ioa" {
			return []string{"glm-5.1", "kimi-k2.6", "hy3-ioa"}
		}
		return []string{"claude-opus-5", "gpt-5.5"}
	}
	if err := st.SetPlatformCodeBuddyKey(repo.PlatformCodeBuddyKeyUpdate{APIKey: "platform-secret", InternetEnvironment: "ioa", Models: []string{"glm-5.1", "kimi-k2.6"}}, 0); err != nil {
		t.Fatal(err)
	}
	international := ""
	if _, err := st.UpdateUserCredentials(member.ID, repo.UserCredentialUpdate{CodeBuddyAPIKey: "own-secret", CodeBuddyInternetEnvironment: &international}); err != nil {
		t.Fatal(err)
	}
	stored, _ := st.User(member.ID)
	var used domain.User
	h := &assistantHandlers{deps: &deps{repo: st}, generate: func(_ context.Context, _ string, u domain.User) (string, error) {
		used = u
		return `{"message":"ok","drafts":[]}`, nil
	}}
	operatorDefault := assistantFixedCatalog().Default

	var catalog assistantModelCatalog
	mrec := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(mrec)
	c.Request = httptest.NewRequest("GET", "/api/assistant/models", nil)
	c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), requestUserContextKey{}, domainUserToAuth(stored)))
	h.models(c)
	if json.Unmarshal(mrec.Body.Bytes(), &catalog) != nil || catalog.Default != operatorDefault {
		t.Fatalf("own-key catalog default must not be the shared default: %s", mrec.Body.String())
	}
	ids := map[string]bool{}
	for _, model := range catalog.Models {
		ids[model.ID] = true
	}
	if !ids["claude-opus-5"] || ids["glm-5.1"] || ids["kimi-k2.6"] {
		t.Fatalf("own-key catalog must list the user's environment only: %s", mrec.Body.String())
	}

	chat := h.chatCatalog(context.Background(), stored)
	if got := assistantAllowedModel("", chat); got != operatorDefault {
		t.Fatalf("a new own-key conversation resolves to %q, want %q", got, operatorDefault)
	}
	if got := assistantAllowedModel("claude-opus-5", chat); got != "claude-opus-5" {
		t.Fatalf("own-key user picking claude-opus-5 got %q", got)
	}
	if rec := sponsoredChat(h, stored, ""); rec.Code != 200 || used.CodeBuddyAPIKey != "own-secret" || used.CodeBuddyInternetEnvironment != "" {
		t.Fatalf("own-key turn: %d %s used=%+v", rec.Code, rec.Body.String(), used)
	}

	// Clearing the key moves the member onto the shared key and its models.
	if _, err := st.UpdateUserCredentials(member.ID, repo.UserCredentialUpdate{CodeBuddyAPIKeyClear: true}); err != nil {
		t.Fatal(err)
	}
	stored, _ = st.User(member.ID)
	borrowed, _, _ := h.assistantChatUser(stored)
	if got := assistantAllowedModel("", h.chatCatalog(context.Background(), borrowed)); got != "glm-5.1" {
		t.Fatalf("a shared-key conversation resolves to %q, want glm-5.1", got)
	}
}

func TestOwnKeyKeepsFreeModelChoice(t *testing.T) {
	st, newcomer := sponsorFixture(t)
	t.Setenv(assistantSponsorLoginEnv, "sponsor")
	if _, err := st.UpdateUserCredentials(newcomer.ID, repo.UserCredentialUpdate{CodeBuddyAPIKey: "own-secret"}); err != nil {
		t.Fatal(err)
	}
	var used domain.User
	h := &assistantHandlers{deps: &deps{repo: st}, generate: func(_ context.Context, _ string, u domain.User) (string, error) {
		used = u
		return `{"message":"ok","drafts":[]}`, nil
	}}
	if rec := sponsoredChat(h, newcomer, "claude-opus-5"); rec.Code != 200 || used.CodeBuddyAPIKey != "own-secret" {
		t.Fatalf("own key: %d %s key=%q", rec.Code, rec.Body.String(), used.CodeBuddyAPIKey)
	}
}

// The setup guides read bootstrap to tell new users that chat and SOP runs
// work on the shared model before they configure a key.
func TestBootstrapAdvertisesSponsoredModel(t *testing.T) {
	st, _ := sponsorFixture(t)
	h := New(st, engine.New(st, runtime.NewManager(filepath.Join(t.TempDir(), "workspaces"))), tracker.NewService(), "", "")
	t.Setenv(assistantSponsorLoginEnv, "sponsor")
	boot := requestEmptyJSON[map[string]any](t, h, http.MethodGet, "/api/bootstrap", http.StatusOK)
	if boot["sponsoredModel"] != assistantSponsoredModel {
		t.Fatalf("sponsoredModel = %v", boot["sponsoredModel"])
	}
	if err := st.SetPlatformCodeBuddyKey(repo.PlatformCodeBuddyKeyUpdate{APIKey: "platform-secret", Models: []string{"glm-5.1", "kimi-k2.6"}}, 0); err != nil {
		t.Fatal(err)
	}
	boot = requestEmptyJSON[map[string]any](t, h, http.MethodGet, "/api/bootstrap", http.StatusOK)
	if models, _ := boot["sponsoredModels"].([]any); boot["sponsoredModel"] != "glm-5.1" || len(models) != 2 {
		t.Fatalf("platform shared key = %v %v", boot["sponsoredModel"], boot["sponsoredModels"])
	}
	if err := st.ClearPlatformCodeBuddyKey(); err != nil {
		t.Fatal(err)
	}
	t.Setenv(assistantSponsorLoginEnv, "")
	boot = requestEmptyJSON[map[string]any](t, h, http.MethodGet, "/api/bootstrap", http.StatusOK)
	if boot["sponsoredModel"] != "" {
		t.Fatalf("no sponsor must advertise nothing, got %v", boot["sponsoredModel"])
	}
}

func TestNoSponsorKeepsRequiringOwnKey(t *testing.T) {
	st, newcomer := sponsorFixture(t)
	for _, login := range []string{"", "missing-user", "newcomer"} {
		t.Setenv(assistantSponsorLoginEnv, login)
		h := &assistantHandlers{deps: &deps{repo: st}, generate: func(context.Context, string, domain.User) (string, error) {
			t.Fatal("must not reach the model")
			return "", nil
		}}
		if rec := sponsoredChat(h, newcomer, ""); rec.Code != 400 || !strings.Contains(rec.Body.String(), "CodeBuddy API Key") {
			t.Fatalf("sponsor %q: %d %s", login, rec.Code, rec.Body.String())
		}
		if _, ok := h.assistantSharedKey(); ok {
			t.Fatalf("sponsor %q without a key must not be available", login)
		}
	}
}

// The key an administrator configures under 平台管理 replaces the legacy
// sponsor account, and limits shared turns to the models they allowed.
func TestPlatformSharedKeyLimitsChatToAllowedModels(t *testing.T) {
	st, newcomer := sponsorFixture(t)
	stubCLIModelDiscovery(t)
	t.Setenv(assistantSponsorLoginEnv, "sponsor")
	if err := st.SetPlatformCodeBuddyKey(repo.PlatformCodeBuddyKeyUpdate{APIKey: "platform-secret", InternetEnvironment: "ioa", Models: []string{"glm-5.1", "kimi-k2.6"}}, 0); err != nil {
		t.Fatal(err)
	}
	var used domain.User
	h := &assistantHandlers{deps: &deps{repo: st}, generate: func(_ context.Context, _ string, u domain.User) (string, error) {
		used = u
		return `{"message":"ok","drafts":[]}`, nil
	}}
	for model, want := range map[string]string{"": "glm-5.1", "kimi-k2.6": "kimi-k2.6"} {
		used = domain.User{}
		rec := sponsoredChat(h, newcomer, model)
		if rec.Code != 200 || used.CodeBuddyAPIKey != "platform-secret" || used.CodeBuddyInternetEnvironment != "ioa" {
			t.Fatalf("model %q: %d %s used=%+v", model, rec.Code, rec.Body.String(), used)
		}
		if got := assistantAllowedModel(model, h.chatCatalog(context.Background(), used)); got != want {
			t.Fatalf("model %q resolves to %q, want %q", model, got, want)
		}
	}
	for _, model := range []string{assistantSponsoredModel, "claude-opus-5"} {
		rec := sponsoredChat(h, newcomer, model)
		if rec.Code != 400 || !strings.Contains(rec.Body.String(), "glm-5.1、kimi-k2.6") {
			t.Fatalf("model %q outside the allow list: %d %s", model, rec.Code, rec.Body.String())
		}
	}

	var catalog assistantModelCatalog
	mrec := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(mrec)
	c.Request = httptest.NewRequest("GET", "/api/assistant/models", nil)
	c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), requestUserContextKey{}, domainUserToAuth(newcomer)))
	h.models(c)
	if json.Unmarshal(mrec.Body.Bytes(), &catalog) != nil || catalog.Default != "glm-5.1" || strings.Join(catalog.SponsoredModels, ",") != "glm-5.1,kimi-k2.6" || len(catalog.Models) != 2 {
		t.Fatalf("shared-key catalog = %s", mrec.Body.String())
	}
	if strings.Contains(mrec.Body.String(), "platform-secret") {
		t.Fatal("catalog must not reveal the shared key")
	}
}

func TestAssistantModelsPerExecutor(t *testing.T) {
	h := &assistantHandlers{deps: &deps{}}
	get := func(executor string) (*httptest.ResponseRecorder, assistantModelCatalog) {
		rec := httptest.NewRecorder()
		c, _ := gin.CreateTestContext(rec)
		c.Request = httptest.NewRequest("GET", "/api/assistant/models?executor="+executor, nil)
		h.models(c)
		var catalog assistantModelCatalog
		_ = json.Unmarshal(rec.Body.Bytes(), &catalog)
		return rec, catalog
	}
	for executor, family := range map[string]string{"codex": "gpt", "claude": "claude", "claude-code": "claude"} {
		rec, catalog := get(executor)
		if rec.Code != 200 || len(catalog.Models) < 2 || catalog.Default != catalog.Models[0].ID {
			t.Fatalf("%s catalog: %d %s", executor, rec.Code, rec.Body.String())
		}
		for _, model := range catalog.Models {
			if model.ID == "default" || model.Family != family {
				t.Fatalf("%s model %s family = %q", executor, model.ID, model.Family)
			}
			if len(model.Efforts) == 0 {
				t.Fatalf("%s model %s lost reasoning efforts", executor, model.ID)
			}
		}
		if catalog.SponsoredModel != "" {
			t.Fatalf("%s must not offer the CodeBuddy sponsored model", executor)
		}
	}
	for _, executor := range []string{"opencode", "pi"} {
		if rec, catalog := get(executor); rec.Code != 200 || len(catalog.Models) != 0 || catalog.Default != "" {
			t.Fatalf("%s without a configured catalog: %d %s", executor, rec.Code, rec.Body.String())
		}
	}
	if rec, _ := get("bogus"); rec.Code != 400 || !strings.Contains(rec.Body.String(), "不支持的执行器") {
		t.Fatalf("unknown executor: %d %s", rec.Code, rec.Body.String())
	}
}

// The model list is cached per instance. A turn on a model this instance has
// not listed yet (another instance served the picker, or it just restarted)
// refreshes the list instead of silently running on the default model.
func TestTurnCatalogRefreshesBeforeFallingBackToTheDefault(t *testing.T) {
	st, member := sponsorFixture(t)
	stubCLIModelDiscovery(t)
	calls := 0
	discoverCLIModels = func(_ context.Context, _, apiKey string) []string {
		calls++
		if apiKey == "own-secret" {
			return []string{"claude-opus-5.5"}
		}
		return nil
	}
	if _, err := st.UpdateUserCredentials(member.ID, repo.UserCredentialUpdate{CodeBuddyAPIKey: "own-secret"}); err != nil {
		t.Fatal(err)
	}
	stored, _ := st.User(member.ID)
	h := &assistantHandlers{deps: &deps{repo: st}}
	if got := assistantAllowedModel("claude-opus-5.5", h.chatCatalog(context.Background(), stored)); got == "claude-opus-5.5" {
		t.Fatalf("precondition: a cold cache must not list the model yet")
	}
	if got := assistantAllowedModel("claude-opus-5.5", h.turnCatalog(context.Background(), stored, "claude-opus-5.5")); got != "claude-opus-5.5" {
		t.Fatalf("picked claude-opus-5.5, turn runs on %q", got)
	}
	if calls != 1 {
		t.Fatalf("a missing model must refresh the list once, got %d discoveries", calls)
	}
	h.turnCatalog(context.Background(), stored, "claude-opus-5.5")
	h.turnCatalog(context.Background(), stored, "")
	if calls != 1 {
		t.Fatalf("a listed or empty model must not run discovery again, got %d", calls)
	}
}

// The chat picker starts a person with nothing remembered on the first listed
// model, so every chat catalog must list its default first.
func TestChatCatalogsListTheDefaultFirst(t *testing.T) {
	t.Setenv("AGENTFLOW_ASSISTANT_MODEL", "glm-5.3-flash-ioa")
	t.Setenv("AGENTFLOW_ASSISTANT_MODELS", "claude-opus-5.5,glm-5.3-flash-ioa")
	ctx := context.Background()
	shared := []string{"deepseek-v4.1-flash", "glm-5.3-flash-ioa"}
	for name, catalog := range map[string]assistantModelCatalog{
		"own key":    assistantChatCatalog(ctx, "", nil),
		"shared key": restrictAssistantCatalog(assistantChatCatalog(ctx, "", shared), shared),
	} {
		if len(catalog.Models) == 0 || catalog.Models[0].ID != catalog.Default {
			t.Fatalf("%s: default %q is not listed first in %+v", name, catalog.Default, catalog.Models)
		}
	}
}