Files
ai_academy/internal/handler/initialization_model_tenant_test.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

116 lines
3.7 KiB
Go

package handler
import (
"context"
"testing"
"academy/internal/types"
"academy/internal/types/interfaces"
)
// The initialization endpoint's model creation used to insert rows without a
// tenant: toModel() carries no TenantID, so models landed at tenant_id = 0
// where the (tenant_id = ? OR is_builtin) read filter made them invisible to
// every tenant — including the one that just configured the KB — leaving the
// KB bound to unreadable model IDs (issue #3333).
type stubTenantStampModelService struct {
interfaces.ModelService
created []*types.Model
updated []*types.Model
getModelByID func(ctx context.Context, id string) (*types.Model, error)
}
func (s *stubTenantStampModelService) GetModelByID(ctx context.Context, id string) (*types.Model, error) {
if s.getModelByID != nil {
return s.getModelByID(ctx, id)
}
return nil, nil // default: force the create path
}
func (s *stubTenantStampModelService) CreateModel(ctx context.Context, model *types.Model) error {
m := *model
s.created = append(s.created, &m)
return nil
}
func (s *stubTenantStampModelService) UpdateModel(ctx context.Context, model *types.Model) error {
m := *model
s.updated = append(s.updated, &m)
return nil
}
func newTenantStampRequest() *InitializationRequest {
req := &InitializationRequest{}
req.LLM.Source = "remote"
req.LLM.ModelName = "qwen2.5"
req.LLM.BaseURL = "http://ollama.internal/v1"
req.LLM.APIKey = "ollama"
req.Embedding.Source = "remote"
req.Embedding.ModelName = "bge-m3"
req.Embedding.BaseURL = "http://ollama.internal/v1"
req.Embedding.APIKey = "ollama"
return req
}
func TestProcessInitializationModelsStampsKBTenantOnCreatedModels(t *testing.T) {
stub := &stubTenantStampModelService{}
h := &InitializationHandler{modelService: stub}
kb := &types.KnowledgeBase{ID: "kb-1", TenantID: 10042}
models, err := h.processInitializationModels(context.Background(), kb, "kb-1", newTenantStampRequest())
if err != nil {
t.Fatalf("processInitializationModels: %v", err)
}
if len(models) != 2 || len(stub.created) != 2 {
t.Fatalf("created = %d models (returned %d), want 2", len(stub.created), len(models))
}
for _, m := range stub.created {
if m.TenantID != 10042 {
t.Fatalf("created model %q has tenant_id = %d, want 10042 (the KB's tenant)", m.Name, m.TenantID)
}
}
}
func TestProcessInitializationModelsKeepsExistingModelTenant(t *testing.T) {
// The reuse path (update of an already-stored model) must not rewrite the
// stored row's tenant: only freshly created rows get stamped.
stored := &types.Model{
ID: "m-existing", Type: types.ModelTypeKnowledgeQA, Name: "old-name",
TenantID: 10042, Source: types.ModelSourceRemote,
}
svc := &stubTenantStampModelService{}
svc.getModelByID = func(ctx context.Context, id string) (*types.Model, error) {
if id == "m-existing" {
return stored, nil
}
return nil, nil
}
h := &InitializationHandler{modelService: svc}
kb := &types.KnowledgeBase{
ID: "kb-1",
TenantID: 10042,
SummaryModelID: "m-existing", // LLM slot reuses; embedding slot creates
}
models, err := h.processInitializationModels(context.Background(), kb, "kb-1", newTenantStampRequest())
if err != nil {
t.Fatalf("processInitializationModels: %v", err)
}
if len(svc.created) != 1 {
t.Fatalf("created = %d models, want exactly 1 (embedding only)", len(svc.created))
}
if svc.created[0].Type != types.ModelTypeEmbedding || svc.created[0].TenantID != 10042 {
t.Fatalf("unexpected created model: type=%v tenant=%d", svc.created[0].Type, svc.created[0].TenantID)
}
foundReuse := false
for _, m := range models {
if m.ID == "m-existing" {
foundReuse = true
}
}
if !foundReuse {
t.Fatal("existing model not returned by the reuse path")
}
}