Files
ai_academy/internal/im/session_not_found_test.go
T
runny 7ec21f35b0 chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
2026-11-16 18:12:23 +08:00

50 lines
1.7 KiB
Go

package im
import (
"errors"
"fmt"
"testing"
apperrors "academy/internal/errors"
"gorm.io/gorm"
)
// TestIsSessionNotFound guards the recovery path for issue #1499.
//
// The session repository translates gorm.ErrRecordNotFound into
// apperrors.ErrSessionNotFound, so an `errors.Is(err, gorm.ErrRecordNotFound)`
// check on the value returned by SessionService.GetSession would silently
// miss — leaving the IM bot permanently unresponsive after the user deletes
// the underlying session from the UI.
func TestIsSessionNotFound(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{"app sentinel as returned by sessionService.GetSession today", apperrors.ErrSessionNotFound, true},
{"wrapped app sentinel", fmt.Errorf("get session: %w", apperrors.ErrSessionNotFound), true},
{"raw gorm sentinel (safety net)", gorm.ErrRecordNotFound, true},
{"wrapped gorm sentinel", fmt.Errorf("query session: %w", gorm.ErrRecordNotFound), true},
{"unrelated error", errors.New("boom"), false},
{"nil", nil, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isSessionNotFound(tt.err); got != tt.want {
t.Errorf("isSessionNotFound(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}
// TestErrSessionNotFoundIsNotGormErrRecordNotFound documents the invariant
// that motivated the bug: the two sentinels are distinct error values, so
// callers must explicitly match the application sentinel.
func TestErrSessionNotFoundIsNotGormErrRecordNotFound(t *testing.T) {
if errors.Is(apperrors.ErrSessionNotFound, gorm.ErrRecordNotFound) {
t.Fatal("apperrors.ErrSessionNotFound must not unwrap to gorm.ErrRecordNotFound; " +
"if this changes, the IM recovery path can be simplified accordingly")
}
}