gomcp/internal/application/tools/session_service_test.go
DmitrL-dev 694e32be26 refactor: rename identity to syntrex, add root orchestration and CI/CD
- Rename Go module: sentinel-community/gomcp -> syntrex/gomcp (50+ files)
- Rename npm package: sentinel-dashboard -> syntrex-dashboard
- Update Cargo.toml repository URL to syntrex/syntrex
- Update all doc references from DmitrL-dev/AISecurity to syntrex
- Add root Makefile (build-all, test-all, lint-all, clean-all)
- Add MIT LICENSE
- Add .editorconfig (Go/Rust/TS/C cross-language)
- Add .github/workflows/ci.yml (Go + Rust + Dashboard)
- Add dashboard next.config.ts and .env.example
- Clean ARCHITECTURE.md: remove brain/immune/strike/micro-swarm, fix 61->67 engines
2026-03-11 15:30:49 +10:00

117 lines
3.2 KiB
Go

package tools
import (
"context"
"testing"
"github.com/syntrex/gomcp/internal/domain/session"
"github.com/syntrex/gomcp/internal/infrastructure/sqlite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newTestSessionService(t *testing.T) *SessionService {
t.Helper()
db, err := sqlite.OpenMemory()
require.NoError(t, err)
t.Cleanup(func() { db.Close() })
repo, err := sqlite.NewStateRepo(db)
require.NoError(t, err)
return NewSessionService(repo)
}
func TestSessionService_SaveState_LoadState(t *testing.T) {
svc := newTestSessionService(t)
ctx := context.Background()
state := session.NewCognitiveStateVector("test-session")
state.SetGoal("Build GoMCP", 0.3)
state.AddFact("Go 1.25", "requirement", 1.0)
require.NoError(t, svc.SaveState(ctx, state))
loaded, checksum, err := svc.LoadState(ctx, "test-session", nil)
require.NoError(t, err)
require.NotNil(t, loaded)
assert.NotEmpty(t, checksum)
assert.Equal(t, "Build GoMCP", loaded.PrimaryGoal.Description)
}
func TestSessionService_ListSessions(t *testing.T) {
svc := newTestSessionService(t)
ctx := context.Background()
s1 := session.NewCognitiveStateVector("s1")
s2 := session.NewCognitiveStateVector("s2")
require.NoError(t, svc.SaveState(ctx, s1))
require.NoError(t, svc.SaveState(ctx, s2))
sessions, err := svc.ListSessions(ctx)
require.NoError(t, err)
assert.Len(t, sessions, 2)
}
func TestSessionService_DeleteSession(t *testing.T) {
svc := newTestSessionService(t)
ctx := context.Background()
state := session.NewCognitiveStateVector("to-delete")
require.NoError(t, svc.SaveState(ctx, state))
count, err := svc.DeleteSession(ctx, "to-delete")
require.NoError(t, err)
assert.Equal(t, 1, count)
}
func TestSessionService_RestoreOrCreate_New(t *testing.T) {
svc := newTestSessionService(t)
ctx := context.Background()
state, restored, err := svc.RestoreOrCreate(ctx, "new-session")
require.NoError(t, err)
assert.False(t, restored)
assert.Equal(t, "new-session", state.SessionID)
}
func TestSessionService_RestoreOrCreate_Existing(t *testing.T) {
svc := newTestSessionService(t)
ctx := context.Background()
original := session.NewCognitiveStateVector("existing")
original.SetGoal("Saved goal", 0.5)
require.NoError(t, svc.SaveState(ctx, original))
state, restored, err := svc.RestoreOrCreate(ctx, "existing")
require.NoError(t, err)
assert.True(t, restored)
assert.Equal(t, "Saved goal", state.PrimaryGoal.Description)
}
func TestSessionService_GetCompactState(t *testing.T) {
svc := newTestSessionService(t)
ctx := context.Background()
state := session.NewCognitiveStateVector("compact")
state.SetGoal("Test compact", 0.5)
state.AddFact("fact1", "requirement", 1.0)
require.NoError(t, svc.SaveState(ctx, state))
compact, err := svc.GetCompactState(ctx, "compact", 500)
require.NoError(t, err)
assert.Contains(t, compact, "Test compact")
assert.Contains(t, compact, "fact1")
}
func TestSessionService_GetAuditLog(t *testing.T) {
svc := newTestSessionService(t)
ctx := context.Background()
state := session.NewCognitiveStateVector("audited")
require.NoError(t, svc.SaveState(ctx, state))
log, err := svc.GetAuditLog(ctx, "audited", 10)
require.NoError(t, err)
assert.GreaterOrEqual(t, len(log), 1)
}