TestWorkingMemorySchema 验证 Schema 验证
(t *testing.T)
| 99 | |
| 100 | // TestWorkingMemorySchema 验证 Schema 验证 |
| 101 | func TestWorkingMemorySchema(t *testing.T) { |
| 102 | ctx := context.Background() |
| 103 | backend := backends.NewStateBackend() |
| 104 | |
| 105 | schema := &memory.JSONSchema{ |
| 106 | Type: "object", |
| 107 | Properties: map[string]*memory.JSONSchema{ |
| 108 | "name": {Type: "string"}, |
| 109 | "age": {Type: "integer"}, |
| 110 | }, |
| 111 | Required: []string{"name"}, |
| 112 | } |
| 113 | |
| 114 | manager, _ := memory.NewWorkingMemoryManager(&memory.WorkingMemoryConfig{ |
| 115 | Backend: backend, |
| 116 | BasePath: "/working_memory/", |
| 117 | Scope: memory.ScopeThread, |
| 118 | Schema: schema, |
| 119 | }) |
| 120 | |
| 121 | threadID := "test-thread" |
| 122 | resourceID := "test-resource" |
| 123 | |
| 124 | // 有效的 JSON |
| 125 | validJSON := `{"name": "Alice", "age": 30}` |
| 126 | err := manager.Update(ctx, threadID, resourceID, validJSON) |
| 127 | if err != nil { |
| 128 | t.Errorf("valid JSON should be accepted: %v", err) |
| 129 | } |
| 130 | |
| 131 | // 无效的 JSON(缺少必需字段) |
| 132 | invalidJSON := `{"age": 30}` |
| 133 | err = manager.Update(ctx, threadID, resourceID, invalidJSON) |
| 134 | if err == nil { |
| 135 | t.Error("invalid JSON should be rejected") |
| 136 | } |
| 137 | |
| 138 | t.Log("✓ Working Memory Schema 验证通过") |
| 139 | } |
nothing calls this directly
no test coverage detected