Files
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

254 lines
9.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package session
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"academy/internal/application/service"
"academy/internal/background"
"academy/internal/types"
"academy/internal/types/interfaces"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
type httpWorkHandler struct {
interfaces.WorkHandler
status map[string]interfaces.WorkResult
cancel []string
refs []interfaces.SessionRef
opts []interfaces.StatusOptions
}
func (h *httpWorkHandler) Status(_ context.Context, ref interfaces.SessionRef,
ids []string, opts interfaces.StatusOptions,
) ([]interfaces.WorkResult, error) {
h.refs = append(h.refs, ref)
h.opts = append(h.opts, opts)
out := make([]interfaces.WorkResult, 0, len(ids))
for _, id := range ids {
if r, ok := h.status[id]; ok {
out = append(out, r)
continue
}
// A live id is not in the terminal map. Detail still has to resolve.
out = append(out, interfaces.WorkResult{ID: id, State: interfaces.WorkStateRunning})
}
return out, nil
}
func (h *httpWorkHandler) Cancel(
_ context.Context, ref interfaces.SessionRef, ids []string,
) ([]interfaces.WorkResult, error) {
h.refs = append(h.refs, ref)
h.cancel = append(h.cancel, ids...)
out := make([]interfaces.WorkResult, 0, len(ids))
for _, id := range ids {
out = append(out, interfaces.WorkResult{ID: id, State: interfaces.WorkStateCancelled, Terminal: true})
}
return out, nil
}
func (s *stubSessionService) GetOwnedSession(ctx context.Context, id string) (*types.Session, error) {
session, err := s.GetSession(ctx, id)
if err != nil || session == nil {
if err == nil {
err = errors.New("session not found")
}
return nil, err
}
// Strict owner: a caller executing as another tenant must not mutate.
if tenant, ok := types.TenantIDFromContext(ctx); ok && tenant != session.TenantID {
return nil, errors.New("session not found")
}
return session, nil
}
func newBackgroundHTTPRouter(t *testing.T, work *service.BackgroundWork,
sessionSvc interfaces.SessionService,
) *gin.Engine {
t.Helper()
gin.SetMode(gin.TestMode)
h := &Handler{background: work, sessionService: sessionSvc}
r := gin.New()
r.GET("/api/v1/sessions/:id/background-work", h.ListBackgroundWork)
r.GET("/api/v1/sessions/:id/background-work/:work_id", h.GetBackgroundWork)
r.POST("/api/v1/sessions/:session_id/background-work/:work_id/cancel", h.CancelBackgroundWork)
return r
}
func ownedSessionContext(t *testing.T, tenant uint64, sessionID string) context.Context {
t.Helper()
ctx := types.WithExecutionTenant(context.Background(), tenant)
ctx = types.WithSandboxTenantID(ctx, tenant)
return types.WithSessionID(ctx, sessionID)
}
// splitCallerContext is a read whose execution tenant and sandbox-pin tenant
// both differ from the session row. The handler must still use session.TenantID.
func splitCallerContext(t *testing.T, sessionID string) context.Context {
t.Helper()
ctx := types.WithExecutionTenant(context.Background(), 99)
ctx = types.WithSandboxTenantID(ctx, 100)
return types.WithSessionID(ctx, sessionID)
}
func TestListMergesRegistryAndInboxWithoutExec(t *testing.T) {
ref := interfaces.SessionRef{TenantID: 7, SessionID: "s"}
work := service.NewBackgroundWork(background.NewMemoryActivityRegistry(nil), background.NewMemoryNoticeInbox(nil))
handler := &httpWorkHandler{status: map[string]interfaces.WorkResult{
"sj_ab01": {
ID: "sj_ab01", State: interfaces.WorkStateSucceeded, Terminal: true, Title: "build",
},
}}
work.Handler = handler
err := work.Registry.Add(context.Background(), ref,
interfaces.BackgroundActivity{ID: "sj_0123", Kind: "shell", Title: "tests"}, time.Minute, 0)
require.NoError(t, err)
finished := time.Now().UTC()
_, err = work.Inbox.Push(context.Background(), ref, interfaces.BackgroundNotice{
ID: "sj_ab01", Kind: "shell", Text: "Background shell sj_ab01 succeeded: build.",
FinishedAt: finished,
})
require.NoError(t, err)
r := newBackgroundHTTPRouter(t, work, &stubSessionService{session: &types.Session{ID: "s", TenantID: 7}})
req := httptest.NewRequest(http.MethodGet, "/api/v1/sessions/s/background-work", nil).
WithContext(splitCallerContext(t, "s"))
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
var body struct {
Data []map[string]any `json:"data"`
}
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body))
require.Len(t, body.Data, 2, "运行中(登记表)+ 24h 内终态(信箱)")
states := map[string]any{}
titles := map[string]any{}
for _, item := range body.Data {
states[item["work_id"].(string)] = item["state"]
titles[item["work_id"].(string)] = item["title"]
}
require.Equal(t, "running", states["sj_0123"])
require.Equal(t, "succeeded", states["sj_ab01"])
require.Equal(t, "tests", titles["sj_0123"])
require.Equal(t, "build", titles["sj_ab01"])
require.NotEmpty(t, handler.opts, "终态 state 由 Status(Detailed=false) 批量补齐")
for _, opts := range handler.opts {
require.False(t, opts.Detailed)
require.Empty(t, opts.RunID)
}
require.NotEmpty(t, handler.refs)
for _, got := range handler.refs {
require.Equal(t, uint64(7), got.TenantID)
require.Equal(t, "s", got.SessionID)
}
}
func TestDetailAndCancelEnforceOwnership(t *testing.T) {
work := service.NewBackgroundWork(background.NewMemoryActivityRegistry(nil), background.NewMemoryNoticeInbox(nil))
handler := &httpWorkHandler{status: map[string]interfaces.WorkResult{}}
work.Handler = handler
svc := &stubSessionService{session: &types.Session{ID: "s", TenantID: 7}}
r := newBackgroundHTTPRouter(t, work, svc)
// Owner read goes through GetSession. Caller tenant and sandbox pin differ
// from the session row; the status ref must still be the session tenant.
req := httptest.NewRequest(http.MethodGet,
"/api/v1/sessions/s/background-work/sj_0123", nil).
WithContext(splitCallerContext(t, "s"))
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
require.Contains(t, rec.Body.String(), `"state":"running"`)
require.NotEmpty(t, handler.opts)
last := handler.opts[len(handler.opts)-1]
require.True(t, last.Detailed)
require.Empty(t, last.RunID, "a user read must not move the model cursor")
require.Equal(t, interfaces.SessionRef{TenantID: 7, SessionID: "s"}, handler.refs[len(handler.refs)-1])
// 取消走严格 owner(stub 拒绝非 owner)
req = httptest.NewRequest(http.MethodPost,
"/api/v1/sessions/s/background-work/sj_0123/cancel", nil).
WithContext(ownedSessionContext(t, 8, "s"))
rec = httptest.NewRecorder()
r.ServeHTTP(rec, req)
require.Equal(t, http.StatusNotFound, rec.Code, "GetOwnedSession 失败 → 404,不区分 403 防探测")
require.Empty(t, handler.cancel)
}
type errActivityRegistry struct {
interfaces.ActivityRegistry
}
func (errActivityRegistry) List(context.Context, interfaces.SessionRef) ([]interfaces.BackgroundActivity, error) {
return nil, errors.New("redis down")
}
func TestListStorageErrorIsNotAnEmptySuccess(t *testing.T) {
work := service.NewBackgroundWork(errActivityRegistry{}, background.NewMemoryNoticeInbox(nil))
work.Handler = &httpWorkHandler{}
r := newBackgroundHTTPRouter(t, work, &stubSessionService{session: &types.Session{ID: "s", TenantID: 7}})
req := httptest.NewRequest(http.MethodGet, "/api/v1/sessions/s/background-work", nil).
WithContext(ownedSessionContext(t, 7, "s"))
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
require.GreaterOrEqual(t, rec.Code, 500)
require.NotContains(t, rec.Body.String(), `"data":[]`)
}
func TestDisabledBackgroundWorkIsEmptyListAnd404(t *testing.T) {
sessionSvc := &stubSessionService{session: &types.Session{ID: "s", TenantID: 7}}
r := newBackgroundHTTPRouter(t, nil, sessionSvc)
req := httptest.NewRequest(http.MethodGet, "/api/v1/sessions/s/background-work", nil).
WithContext(ownedSessionContext(t, 7, "s"))
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
require.Contains(t, rec.Body.String(), `"data":[]`)
req = httptest.NewRequest(http.MethodGet,
"/api/v1/sessions/s/background-work/sj_0123", nil).
WithContext(ownedSessionContext(t, 7, "s"))
rec = httptest.NewRecorder()
r.ServeHTTP(rec, req)
require.Equal(t, http.StatusNotFound, rec.Code)
req = httptest.NewRequest(http.MethodPost,
"/api/v1/sessions/s/background-work/sj_0123/cancel", nil).
WithContext(ownedSessionContext(t, 7, "s"))
rec = httptest.NewRecorder()
r.ServeHTTP(rec, req)
require.Equal(t, http.StatusNotFound, rec.Code)
// Enabled() is false until Handler, Registry, and Inbox are all set.
// Rows already stored must not leak through a disabled bundle.
work := service.NewBackgroundWork(background.NewMemoryActivityRegistry(nil), background.NewMemoryNoticeInbox(nil))
ref := interfaces.SessionRef{TenantID: 7, SessionID: "s"}
err := work.Registry.Add(context.Background(), ref,
interfaces.BackgroundActivity{ID: "sj_0123", Kind: "shell", Title: "tests"}, time.Minute, 0)
require.NoError(t, err)
_, err = work.Inbox.Push(context.Background(), ref, interfaces.BackgroundNotice{
ID: "sj_ab01", Kind: "shell", Text: "Background shell sj_ab01 succeeded: build.",
FinishedAt: time.Now().UTC(),
})
require.NoError(t, err)
require.False(t, work.Enabled())
r = newBackgroundHTTPRouter(t, work, sessionSvc)
req = httptest.NewRequest(http.MethodGet, "/api/v1/sessions/s/background-work", nil).
WithContext(ownedSessionContext(t, 7, "s"))
rec = httptest.NewRecorder()
r.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
require.Contains(t, rec.Body.String(), `"data":[]`)
req = httptest.NewRequest(http.MethodGet,
"/api/v1/sessions/s/background-work/sj_0123", nil).
WithContext(ownedSessionContext(t, 7, "s"))
rec = httptest.NewRecorder()
r.ServeHTTP(rec, req)
require.Equal(t, http.StatusNotFound, rec.Code)
}