Files
ai_academy/internal/sandbox/session_manager_terminal_test.go
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

217 lines
7.6 KiB
Go

package sandbox
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"academy/internal/types"
)
func newSessionManagerTerminalTestHarness(t *testing.T) (*SessionBoundManager, *terminalFakeClient) {
t.Helper()
return newSessionManagerTerminalTestHarnessFor(t, SandboxTypeCube)
}
func newSessionManagerTerminalTestHarnessFor(
t *testing.T, provider SandboxType,
) (*SessionBoundManager, *terminalFakeClient) {
t.Helper()
client := &terminalFakeClient{fakeRemoteClient: newFakeRemoteClient(provider)}
client.capabilities.SupportsTerminals = true
// Docker containers sit behind no gateway, so their handles carry no
// inbound token, exactly like the real adapter.
client.omitsInboundTokenCarrier = provider == SandboxTypeDocker
cfg := DefaultConfig()
cfg.CubeTemplate = "tpl-test"
cfg.DockerImage = "academy/sandbox:test"
mgr, err := NewSessionBoundManager(SessionBoundManagerConfig{
Config: cfg,
Client: client,
Store: NewMemorySessionSandboxBindingStore(),
Checker: &fakeSessionExistenceChecker{exists: true},
SkipHealthProbe: true,
})
require.NoError(t, err)
return mgr, client
}
func terminalTestContext() context.Context {
return context.WithValue(context.Background(), types.TenantIDContextKey, uint64(10000))
}
func pauseAllFakeSandboxes(t *testing.T, client *fakeRemoteClient) {
t.Helper()
client.mu.Lock()
defer client.mu.Unlock()
require.NotEmpty(t, client.sandboxes)
for _, rec := range client.sandboxes {
rec.state = RemoteStatePaused
}
}
func fakeConnectCount(t *testing.T, client *fakeRemoteClient) int {
t.Helper()
client.mu.Lock()
defer client.mu.Unlock()
return len(client.connectIDs)
}
func fakeCreateCount(t *testing.T, client *fakeRemoteClient) int {
t.Helper()
client.mu.Lock()
defer client.mu.Unlock()
return client.createCount
}
func setAllFakeSandboxState(t *testing.T, client *fakeRemoteClient, state RemoteSandboxState) {
t.Helper()
client.mu.Lock()
defer client.mu.Unlock()
require.NotEmpty(t, client.sandboxes)
for _, rec := range client.sandboxes {
rec.state = state
rec.rawState = string(state)
}
}
func clearAllFakeSandboxMetadata(t *testing.T, client *fakeRemoteClient) {
t.Helper()
client.mu.Lock()
defer client.mu.Unlock()
require.NotEmpty(t, client.sandboxes)
for _, rec := range client.sandboxes {
rec.metadata = map[string]string{}
}
}
func TestOpenSessionTerminalRefusesPausedSandboxWithoutResume(t *testing.T) {
ctx := terminalTestContext()
mgr, client := newSessionManagerTerminalTestHarness(t)
_, err := mgr.ExecShellCommand(ctx, "sess-1", "true", "", time.Second, nil)
require.NoError(t, err)
pauseAllFakeSandboxes(t, client.fakeRemoteClient)
connectsBefore := fakeConnectCount(t, client.fakeRemoteClient)
_, err = mgr.OpenSessionTerminal(ctx, "sess-1", RemoteTerminalOptions{})
require.ErrorIs(t, err, ErrSandboxPaused)
require.Equal(t, connectsBefore, fakeConnectCount(t, client.fakeRemoteClient),
"lookup-only open must List a paused sandbox instead of Connect, which would resume it")
}
// Docker never pauses: a bound container that is not running is stopped (a
// resume restarts it) or already deleted by the idle sweep (a resume replaces
// it). The UI needs to tell the user that, so Docker reports the distinct
// ErrSandboxStopped, still matching ErrSandboxPaused for every other caller.
func TestOpenSessionTerminalDockerNotRunningIsStopped(t *testing.T) {
cases := map[string]func(*testing.T, *fakeRemoteClient){
"stopped container": func(t *testing.T, c *fakeRemoteClient) {
pauseAllFakeSandboxes(t, c)
},
"deleted by the sweep (bound but missing from List)": clearAllFakeSandboxMetadata,
}
for name, makeNotRunning := range cases {
t.Run(name, func(t *testing.T) {
ctx := terminalTestContext()
mgr, client := newSessionManagerTerminalTestHarnessFor(t, SandboxTypeDocker)
_, err := mgr.ExecShellCommand(ctx, "sess-1", "true", "", time.Second, nil)
require.NoError(t, err)
makeNotRunning(t, client.fakeRemoteClient)
_, err = mgr.OpenSessionTerminal(ctx, "sess-1", RemoteTerminalOptions{})
require.ErrorIs(t, err, ErrSandboxStopped)
require.ErrorIs(t, err, ErrSandboxPaused,
"callers that only need 'ask before resuming' must keep matching")
})
}
}
func TestOpenSessionTerminalPausableBackendIsNotStopped(t *testing.T) {
ctx := terminalTestContext()
mgr, client := newSessionManagerTerminalTestHarness(t)
_, err := mgr.ExecShellCommand(ctx, "sess-1", "true", "", time.Second, nil)
require.NoError(t, err)
pauseAllFakeSandboxes(t, client.fakeRemoteClient)
_, err = mgr.OpenSessionTerminal(ctx, "sess-1", RemoteTerminalOptions{})
require.ErrorIs(t, err, ErrSandboxPaused)
require.NotErrorIs(t, err, ErrSandboxStopped,
"a paused Cube/E2B sandbox really is paused and wakes with its state")
}
func TestOpenSessionTerminalResumesPausedSandboxWhenAllowed(t *testing.T) {
ctx := terminalTestContext()
mgr, client := newSessionManagerTerminalTestHarness(t)
_, err := mgr.ExecShellCommand(ctx, "sess-1", "true", "", time.Second, nil)
require.NoError(t, err)
pauseAllFakeSandboxes(t, client.fakeRemoteClient)
connectsBefore := fakeConnectCount(t, client.fakeRemoteClient)
createsBefore := fakeCreateCount(t, client.fakeRemoteClient)
session, err := mgr.OpenSessionTerminal(ctx, "sess-1", RemoteTerminalOptions{AllowResume: true})
require.NoError(t, err)
require.NotNil(t, session)
require.Greater(t, fakeConnectCount(t, client.fakeRemoteClient), connectsBefore)
require.Equal(t, createsBefore, fakeCreateCount(t, client.fakeRemoteClient),
"resuming a paused sandbox must Connect, not provision a replacement")
}
func TestOpenSessionTerminalConnectsRunningSandbox(t *testing.T) {
ctx := terminalTestContext()
mgr, _ := newSessionManagerTerminalTestHarness(t)
_, err := mgr.ExecShellCommand(ctx, "sess-1", "true", "", time.Second, nil)
require.NoError(t, err)
session, err := mgr.OpenSessionTerminal(ctx, "sess-1", RemoteTerminalOptions{})
require.NoError(t, err)
require.NotNil(t, session)
}
func TestOpenSessionTerminalNoBindingIsNotPaused(t *testing.T) {
ctx := terminalTestContext()
mgr, _ := newSessionManagerTerminalTestHarness(t)
_, err := mgr.OpenSessionTerminal(ctx, "sess-missing", RemoteTerminalOptions{})
require.ErrorIs(t, err, ErrNoLiveSessionSandbox)
require.NotErrorIs(t, err, ErrSandboxPaused)
}
func TestOpenSessionTerminalRefusesTransitioningSandboxWithoutResume(t *testing.T) {
ctx := terminalTestContext()
mgr, client := newSessionManagerTerminalTestHarness(t)
_, err := mgr.ExecShellCommand(ctx, "sess-1", "true", "", time.Second, nil)
require.NoError(t, err)
setAllFakeSandboxState(t, client.fakeRemoteClient, RemoteStateTransitioning)
connectsBefore := fakeConnectCount(t, client.fakeRemoteClient)
_, err = mgr.OpenSessionTerminal(ctx, "sess-1", RemoteTerminalOptions{})
require.ErrorIs(t, err, ErrSandboxPaused)
require.Equal(t, connectsBefore, fakeConnectCount(t, client.fakeRemoteClient),
"pausing/resuming is Transitioning; lookup-only Connect would still resume")
}
func TestOpenSessionTerminalListMissWithBindingIsPausedNotUnbound(t *testing.T) {
ctx := terminalTestContext()
mgr, client := newSessionManagerTerminalTestHarness(t)
_, err := mgr.ExecShellCommand(ctx, "sess-1", "true", "", time.Second, nil)
require.NoError(t, err)
clearAllFakeSandboxMetadata(t, client.fakeRemoteClient)
connectsBefore := fakeConnectCount(t, client.fakeRemoteClient)
_, err = mgr.OpenSessionTerminal(ctx, "sess-1", RemoteTerminalOptions{})
require.ErrorIs(t, err, ErrSandboxPaused)
require.NotErrorIs(t, err, ErrNoLiveSessionSandbox)
require.Equal(t, connectsBefore, fakeConnectCount(t, client.fakeRemoteClient),
"a bound sandbox missing from List must not be reported as unbound")
}