TestWorkingMemoryScope 验证作用域隔离
(t *testing.T)
| 48 | |
| 49 | // TestWorkingMemoryScope 验证作用域隔离 |
| 50 | func TestWorkingMemoryScope(t *testing.T) { |
| 51 | ctx := context.Background() |
| 52 | backend := backends.NewStateBackend() |
| 53 | |
| 54 | threadManager, _ := memory.NewWorkingMemoryManager(&memory.WorkingMemoryConfig{ |
| 55 | Backend: backend, |
| 56 | BasePath: "/working_memory/", |
| 57 | Scope: memory.ScopeThread, |
| 58 | }) |
| 59 | |
| 60 | resourceManager, _ := memory.NewWorkingMemoryManager(&memory.WorkingMemoryConfig{ |
| 61 | Backend: backend, |
| 62 | BasePath: "/working_memory/", |
| 63 | Scope: memory.ScopeResource, |
| 64 | }) |
| 65 | |
| 66 | thread1 := "thread-1" |
| 67 | thread2 := "thread-2" |
| 68 | resource := "resource-1" |
| 69 | |
| 70 | // Thread scope - 不同 thread 应该隔离 |
| 71 | if err := threadManager.Update(ctx, thread1, resource, "Thread 1 content"); err != nil { |
| 72 | t.Fatalf("Failed to update thread1: %v", err) |
| 73 | } |
| 74 | if err := threadManager.Update(ctx, thread2, resource, "Thread 2 content"); err != nil { |
| 75 | t.Fatalf("Failed to update thread2: %v", err) |
| 76 | } |
| 77 | |
| 78 | content1, _ := threadManager.Get(ctx, thread1, resource) |
| 79 | content2, _ := threadManager.Get(ctx, thread2, resource) |
| 80 | |
| 81 | if content1 == content2 { |
| 82 | t.Error("thread scope should isolate different threads") |
| 83 | } |
| 84 | |
| 85 | // Resource scope - 相同 resource 应该共享 |
| 86 | if err := resourceManager.Update(ctx, thread1, resource, "Shared content"); err != nil { |
| 87 | t.Fatalf("Failed to update resource: %v", err) |
| 88 | } |
| 89 | |
| 90 | contentFromThread1, _ := resourceManager.Get(ctx, thread1, resource) |
| 91 | contentFromThread2, _ := resourceManager.Get(ctx, thread2, resource) |
| 92 | |
| 93 | if contentFromThread1 != contentFromThread2 { |
| 94 | t.Error("resource scope should share content across threads") |
| 95 | } |
| 96 | |
| 97 | t.Log("✓ Working Memory 作用域隔离验证通过") |
| 98 | } |
| 99 | |
| 100 | // TestWorkingMemorySchema 验证 Schema 验证 |
| 101 | func TestWorkingMemorySchema(t *testing.T) { |
nothing calls this directly
no test coverage detected