| 255 | } |
| 256 | |
| 257 | func TestState(t *testing.T) { |
| 258 | tmpDir := t.TempDir() |
| 259 | dbPath := filepath.Join(tmpDir, "test.db") |
| 260 | |
| 261 | svc, err := New(dbPath) |
| 262 | if err != nil { |
| 263 | t.Fatalf("New failed: %v", err) |
| 264 | } |
| 265 | defer func() { _ = svc.Close() }() |
| 266 | |
| 267 | ctx := context.Background() |
| 268 | |
| 269 | // Create session |
| 270 | sess, _ := svc.Create(ctx, &session.CreateRequest{ |
| 271 | AppName: "test-app", |
| 272 | UserID: "user-1", |
| 273 | AgentID: "agent-1", |
| 274 | }) |
| 275 | |
| 276 | state := sess.State() |
| 277 | |
| 278 | // Test Set and Get |
| 279 | t.Run("SetGet", func(t *testing.T) { |
| 280 | err := state.Set("key1", "value1") |
| 281 | if err != nil { |
| 282 | t.Fatalf("Set failed: %v", err) |
| 283 | } |
| 284 | |
| 285 | val, err := state.Get("key1") |
| 286 | if err != nil { |
| 287 | t.Fatalf("Get failed: %v", err) |
| 288 | } |
| 289 | |
| 290 | if val != "value1" { |
| 291 | t.Errorf("Expected 'value1', got %v", val) |
| 292 | } |
| 293 | }) |
| 294 | |
| 295 | // Test Get not found |
| 296 | t.Run("GetNotFound", func(t *testing.T) { |
| 297 | _, err := state.Get("non-existent") |
| 298 | if !errors.Is(err, session.ErrStateKeyNotExist) { |
| 299 | t.Errorf("Expected ErrStateKeyNotExist, got %v", err) |
| 300 | } |
| 301 | }) |
| 302 | |
| 303 | // Test Has |
| 304 | t.Run("Has", func(t *testing.T) { |
| 305 | _ = state.Set("exists", true) |
| 306 | |
| 307 | if !state.Has("exists") { |
| 308 | t.Error("Has should return true for existing key") |
| 309 | } |
| 310 | if state.Has("not-exists") { |
| 311 | t.Error("Has should return false for non-existing key") |
| 312 | } |
| 313 | }) |
| 314 | |