-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_schema_knowledge_test.go
More file actions
239 lines (212 loc) · 7.93 KB
/
Copy pathapp_schema_knowledge_test.go
File metadata and controls
239 lines (212 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"context"
"errors"
"path/filepath"
"testing"
"futrixdata/platform/internal/aichat"
"futrixdata/platform/internal/console"
"futrixdata/platform/internal/datasource"
"futrixdata/platform/internal/schemaprivacy"
)
type schemaKnowledgeTestModel struct {
response string
}
func (m schemaKnowledgeTestModel) Chat(ctx context.Context, systemPrompt string, messages []aichat.Message) (string, error) {
_ = ctx
_ = systemPrompt
_ = messages
return m.response, nil
}
type schemaKnowledgeTestResolver struct {
model aichat.Model
}
func (r schemaKnowledgeTestResolver) Resolve(aiConfigID string) (aichat.Model, error) {
_ = aiConfigID
return r.model, nil
}
func TestSchemaKnowledgeManager_SyncAndReadSchema(t *testing.T) {
root := t.TempDir()
manager := newSchemaKnowledgeManager(root, nil)
ds := datasource.DataSource{ID: "ds_mysql", Name: "Mysql", Type: datasource.TypeMySQL}
entry := console.EntitySchemaCacheEntry{
UpdatedAt: 1772000000,
Entities: []string{"orders"},
Details: map[string]console.DescribeResult{
"orders": {
Columns: []console.ColumnInfo{{Name: "id", DataType: "int"}},
Indexes: []console.IndexInfo{{Name: "PRIMARY", Column: "id", Unique: true}},
},
},
}
if err := manager.SyncFromCache(context.Background(), ds, "ds_mysql", entry); err != nil {
t.Fatalf("SyncFromCache: %v", err)
}
schema, err := manager.GetSchemaKnowledge(ds, "orders")
if err != nil {
t.Fatalf("GetSchemaKnowledge: %v", err)
}
entities, _ := schema["entities"].([]schemaKnowledgeEntity)
if len(entities) != 1 || entities[0].Name != "orders" {
t.Fatalf("expected orders entity in schema knowledge, got %#v", schema["entities"])
}
if _, err := manager.GetERKnowledge(ds); err == nil {
t.Fatalf("expected missing ER knowledge error when AI provider is not configured")
}
}
func TestSchemaKnowledgeManager_GeneratesERWhenModelConfigured(t *testing.T) {
root := t.TempDir()
resolver := schemaKnowledgeTestResolver{model: schemaKnowledgeTestModel{response: "# ER\n\norders ||--o{ order_items : contains"}}
manager := newSchemaKnowledgeManager(root, resolver)
manager.SetSchemaPrivacy(nil, nil)
ds := datasource.DataSource{
ID: "ds_mysql",
Name: "Mysql",
Type: datasource.TypeMySQL,
Options: map[string]any{
schemaprivacy.OptionKey: string(schemaprivacy.ConsentAllowed),
},
}
entry := console.EntitySchemaCacheEntry{
UpdatedAt: 1772000000,
Entities: []string{"orders", "order_items"},
}
if err := manager.SyncFromCache(context.Background(), ds, "ds_mysql", entry); err != nil {
t.Fatalf("SyncFromCache: %v", err)
}
er, err := manager.GetERKnowledge(ds)
if err != nil {
t.Fatalf("GetERKnowledge: %v", err)
}
content := er["content"]
if content == nil || content == "" {
t.Fatalf("expected ER content, got %#v", er)
}
}
// failingResolver mimics the real-world case where the user has consented
// to schema egress but no AI provider has been configured (or the
// configured one is broken). Resolve must error and the ER manager must
// react by skipping the chat call entirely.
type failingResolver struct{}
func (failingResolver) Resolve(string) (aichat.Model, error) {
return nil, errors.New("no usable AI config")
}
// TestSchemaKnowledgeManager_NoAuditWhenModelUnresolvable guards against
// codex P2 r3165...: maybeGenerateER used to call schemaprivacy.Gate
// (which writes an "allowed" audit row) before resolving the model. When
// Resolve fails no schema actually goes out, so the audit row would lie
// about an egress that never happened. The fix moves Resolve ahead of
// Gate so the allowed row is only written on the path that genuinely
// reaches a model.
func TestSchemaKnowledgeManager_NoAuditWhenModelUnresolvable(t *testing.T) {
root := t.TempDir()
manager := newSchemaKnowledgeManager(root, failingResolver{})
auditPath := filepath.Join(t.TempDir(), "schema-llm-audit.jsonl")
audit := schemaprivacy.NewAuditStore(auditPath)
manager.SetSchemaPrivacy(audit, nil)
ds := datasource.DataSource{
ID: "ds_mysql",
Name: "Mysql",
Type: datasource.TypeMySQL,
Options: map[string]any{
schemaprivacy.OptionKey: string(schemaprivacy.ConsentAllowed),
},
}
entry := console.EntitySchemaCacheEntry{
UpdatedAt: 1772000000,
Entities: []string{"orders"},
}
if err := manager.SyncFromCache(context.Background(), ds, "ds_mysql", entry); err != nil {
t.Fatalf("SyncFromCache: %v", err)
}
items, err := audit.List(schemaprivacy.AuditFilter{DatasourceID: ds.ID})
if err != nil {
t.Fatalf("audit list: %v", err)
}
if len(items) != 0 {
t.Fatalf("expected no audit rows when no model resolves; got %d: %#v", len(items), items)
}
}
// TestSchemaKnowledgeManager_HonorsRevokedConsentAtSendTime guards against
// codex P1 r3171...: maybeGenerateER used to gate against the `ds` snapshot
// captured when syncSchemaKnowledgeAsync queued the work. Auto-describe and
// cache rebuild can take long enough for the user to flip the toggle from
// allowed to denied; without re-reading consent at gate time, the denial is
// ignored and schema metadata leaves the box. The fix wires a consentLookup
// the manager calls right before the consent decision.
func TestSchemaKnowledgeManager_HonorsRevokedConsentAtSendTime(t *testing.T) {
root := t.TempDir()
resolver := schemaKnowledgeTestResolver{model: schemaKnowledgeTestModel{response: "# ER"}}
manager := newSchemaKnowledgeManager(root, resolver)
auditPath := filepath.Join(t.TempDir(), "schema-llm-audit.jsonl")
audit := schemaprivacy.NewAuditStore(auditPath)
manager.SetSchemaPrivacy(audit, nil)
// The caller still holds the stale "allowed" snapshot it captured before
// the slow fetch began.
staleDS := datasource.DataSource{
ID: "ds_mysql",
Name: "Mysql",
Type: datasource.TypeMySQL,
Options: map[string]any{
schemaprivacy.OptionKey: string(schemaprivacy.ConsentAllowed),
},
}
// The store reflects the user's revocation that landed mid-fetch.
freshDS := staleDS
freshDS.Options = map[string]any{
schemaprivacy.OptionKey: string(schemaprivacy.ConsentDenied),
}
manager.SetDatasourceLookup(func(id string) (datasource.DataSource, bool) {
if id == staleDS.ID {
return freshDS, true
}
return datasource.DataSource{}, false
})
entry := console.EntitySchemaCacheEntry{
UpdatedAt: 1772000000,
Entities: []string{"orders"},
}
if err := manager.SyncFromCache(context.Background(), staleDS, "ds_mysql", entry); err != nil {
t.Fatalf("SyncFromCache: %v", err)
}
items, err := audit.List(schemaprivacy.AuditFilter{DatasourceID: staleDS.ID})
if err != nil {
t.Fatalf("audit list: %v", err)
}
if len(items) != 1 || items[0].Status != schemaprivacy.StatusDenied {
t.Fatalf("expected exactly one denied audit row when consent revoked mid-fetch, got %d: %#v", len(items), items)
}
}
// TestSchemaKnowledgeManager_AuditsDeniedEvenWithoutModel covers the
// converse: a denied datasource must still produce a denial audit row,
// regardless of whether a model can be resolved. The denial is the
// security event we cannot drop.
func TestSchemaKnowledgeManager_AuditsDeniedEvenWithoutModel(t *testing.T) {
root := t.TempDir()
manager := newSchemaKnowledgeManager(root, failingResolver{})
auditPath := filepath.Join(t.TempDir(), "schema-llm-audit.jsonl")
audit := schemaprivacy.NewAuditStore(auditPath)
manager.SetSchemaPrivacy(audit, nil)
ds := datasource.DataSource{
ID: "ds_mysql",
Name: "Mysql",
Type: datasource.TypeMySQL,
Options: map[string]any{
schemaprivacy.OptionKey: string(schemaprivacy.ConsentDenied),
},
}
entry := console.EntitySchemaCacheEntry{
UpdatedAt: 1772000000,
Entities: []string{"orders"},
}
if err := manager.SyncFromCache(context.Background(), ds, "ds_mysql", entry); err != nil {
t.Fatalf("SyncFromCache: %v", err)
}
items, err := audit.List(schemaprivacy.AuditFilter{DatasourceID: ds.ID})
if err != nil {
t.Fatalf("audit list: %v", err)
}
if len(items) != 1 || items[0].Status != schemaprivacy.StatusDenied {
t.Fatalf("expected exactly one denied audit row, got %d: %#v", len(items), items)
}
}