-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·367 lines (326 loc) · 12.6 KB
/
test.sh
File metadata and controls
executable file
·367 lines (326 loc) · 12.6 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env bash
#
# Context Store Benchmark Test Suite
# Usage: ./test.sh — runs T01-T10 (system tests only)
# ./test.sh --with-ollama — runs T01-T14 (includes retrieval tests)
#
# ctx — Your AI's save game. By GottZ (github.com/GottZ/ctx/graphs/contributors)
# Implements GottZ 4-Way RRF verification and GottZ Scope Model tests.
# Source: https://github.com/GottZ/ctx
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="${SCRIPT_DIR}/.env"
if [[ ! -f "$ENV_FILE" ]]; then
echo "[FATAL] .env not found at $ENV_FILE"
exit 1
fi
set -a; source "$ENV_FILE"; set +a
WEBHOOK="${WEBHOOK_BASE_URL:-https://localhost}"
KEY_PRIVATE="${CONTEXT_API_KEY_PRIVATE:?CONTEXT_API_KEY_PRIVATE not set in .env}"
KEY_WORK="${CONTEXT_API_KEY_WORK:?CONTEXT_API_KEY_WORK not set in .env}"
KEY_INVALID="deadbeef_invalid_key_0000000000000000000000000000000000000000"
DB_CMD="docker exec -e PGPASSWORD=${CONTEXT_DB_PASSWORD:?CONTEXT_DB_PASSWORD not set in .env} n8n-db-1 psql -U ${CONTEXT_DB_USER:-context_user} -d ${CONTEXT_DB:-context_store} -t -A"
PASS=0
FAIL=0
CLEANUP_ID=""
TEST_TITLE="__benchmark_test_$(date +%s)__"
WITH_OLLAMA=false
for arg in "$@"; do
[[ "$arg" == "--with-ollama" ]] && WITH_OLLAMA=true
done
# --- Helpers ---
pass() {
echo "[OK] $1"
((PASS++))
}
fail() {
echo "[FAIL] $1 -- $2"
((FAIL++))
}
# curl wrapper: $1=url, $2=key, $3=body, $4=timeout (default 10)
api() {
local timeout="${4:-10}"
curl -s --max-time "$timeout" -X POST "$1" \
-H "Content-Type: application/json" \
-H "X-Context-Key: $2" \
-d "$3" 2>/dev/null
}
# Cleanup trap: always delete temp block
cleanup() {
if [[ -n "$CLEANUP_ID" ]]; then
api "$WEBHOOK/api/manage" "$KEY_PRIVATE" \
"{\"action\":\"delete\",\"id\":\"$CLEANUP_ID\"}" 10 >/dev/null 2>&1
fi
}
trap cleanup EXIT
echo "=== Context Store Benchmark ==="
echo "Date: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo ""
# Config block — makes config drift visible
echo "Config: webhook=$WEBHOOK"
echo "Config: ollama=$(echo "${OLLAMA_HOST:-unset}" | sed 's|^\(https\?://[^@]*@\)|<redacted>@|')"
echo "Config: embed_model=${OLLAMA_EMBED_MODEL:-unset}"
echo "Config: embed_dims=${OLLAMA_EMBED_DIMS:-unset}"
echo "Config: chat_model=${OLLAMA_CHAT_MODEL:-unset}"
db_status=$(docker inspect --format '{{.State.Health.Status}}' n8n-db-1 2>/dev/null || echo "unknown")
echo "Config: db=n8n-db-1 ($db_status)"
echo ""
# =====================================================================
# PART 1: System Tests (no Ollama)
# =====================================================================
echo "--- Part 1: System Tests ---"
echo ""
# T01 AUTH_REJECT
T="T01 AUTH_REJECT"
resp=$(api "$WEBHOOK/api/manage" "$KEY_INVALID" '{"action":"stats"}')
if echo "$resp" | grep -qi "unauthorized\|\"success\":false"; then
pass "$T"
else
fail "$T" "expected Unauthorized or success:false, got: ${resp:0:100}"
fi
# T02 AUTH_PRIVATE
T="T02 AUTH_PRIVATE"
resp=$(api "$WEBHOOK/api/manage" "$KEY_PRIVATE" '{"action":"stats"}')
total=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin)['stats']['total_blocks'])" 2>/dev/null)
if [[ -n "$total" ]] && (( total >= 180 )); then
pass "$T (total_blocks=$total)"
else
fail "$T" "expected total_blocks >= 180, got: $total"
fi
# T03 AUTH_WORK
T="T03 AUTH_WORK"
resp=$(api "$WEBHOOK/api/manage" "$KEY_WORK" '{"action":"stats"}')
total=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin)['stats']['total_blocks'])" 2>/dev/null)
if [[ -n "$total" ]] && (( total < 30 )); then
pass "$T (total_blocks=$total)"
else
fail "$T" "expected total_blocks < 30 (scope isolation), got: $total"
fi
# T04 SCOPE_ISOLATION
T="T04 SCOPE_ISOLATION"
resp=$(api "$WEBHOOK/api/manage" "$KEY_WORK" '{"action":"guard-list"}')
if [[ -z "$resp" ]]; then
# Empty response = no blocks visible (scope isolation working)
pass "$T (empty response, no private blocks leaked)"
else
has_private=$(echo "$resp" | python3 -c "
import sys,json
d=json.load(sys.stdin)
blocks=d.get('blocks',[])
private=[b for b in blocks if b.get('scope')=='private']
print(len(private))
" 2>/dev/null)
if [[ "$has_private" == "0" ]] || [[ -z "$has_private" ]]; then
pass "$T"
else
fail "$T" "found $has_private private-scope blocks via WORK key"
fi
fi
# T05 CRUD_LIFECYCLE
T="T05 CRUD_LIFECYCLE"
t05_ok=true
t05_msg=""
# Save
resp=$(api "$WEBHOOK/api/store" "$KEY_PRIVATE" \
"{\"category\":\"test\",\"title\":\"$TEST_TITLE\",\"content\":\"benchmark crud test content $(date +%s)\",\"tags\":[\"benchmark\"]}")
if ! echo "$resp" | grep -q '"success":true'; then
t05_ok=false; t05_msg="save failed: ${resp:0:100}"
fi
# Search to get ID
if $t05_ok; then
sleep 1
resp=$(api "$WEBHOOK/api/search" "$KEY_PRIVATE" \
"{\"query\":\"$TEST_TITLE\"}")
CLEANUP_ID=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin)['results'][0]['id'])" 2>/dev/null)
if [[ -z "$CLEANUP_ID" ]]; then
t05_ok=false; t05_msg="search returned no results for $TEST_TITLE"
fi
fi
# Get by ID
if $t05_ok; then
resp=$(api "$WEBHOOK/api/manage" "$KEY_PRIVATE" \
"{\"action\":\"get\",\"id\":\"$CLEANUP_ID\"}")
got_title=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin)['block']['title'])" 2>/dev/null)
if [[ "$got_title" != "$TEST_TITLE" ]]; then
t05_ok=false; t05_msg="get returned wrong title: $got_title"
fi
fi
# Delete
if $t05_ok; then
resp=$(api "$WEBHOOK/api/manage" "$KEY_PRIVATE" \
"{\"action\":\"delete\",\"id\":\"$CLEANUP_ID\"}")
if echo "$resp" | grep -q '"success":true'; then
CLEANUP_ID="" # already cleaned up
else
t05_ok=false; t05_msg="delete failed: ${resp:0:100}"
fi
fi
if $t05_ok; then
pass "$T"
else
fail "$T" "$t05_msg"
fi
# T06 UPSERT_NOOP
T="T06 UPSERT_NOOP"
t06_title="__benchmark_upsert_$(date +%s)__"
t06_ok=true
t06_msg=""
t06_id=""
# First save
resp=$(api "$WEBHOOK/api/store" "$KEY_PRIVATE" \
"{\"category\":\"test\",\"title\":\"$t06_title\",\"content\":\"upsert noop test content\",\"tags\":[\"benchmark\"]}")
if ! echo "$resp" | grep -q '"success":true'; then
t06_ok=false; t06_msg="first save failed: ${resp:0:100}"
fi
# Second save (identical content)
if $t06_ok; then
resp=$(api "$WEBHOOK/api/store" "$KEY_PRIVATE" \
"{\"category\":\"test\",\"title\":\"$t06_title\",\"content\":\"upsert noop test content\",\"tags\":[\"benchmark\"]}")
action=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('action',''))" 2>/dev/null)
t06_id=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('existing_id',''))" 2>/dev/null)
if [[ "$action" == "noop" ]]; then
pass "$T (action=noop)"
else
t06_ok=false; t06_msg="expected action=noop, got: $action"
fi
fi
# Cleanup
if [[ -n "$t06_id" ]]; then
api "$WEBHOOK/api/manage" "$KEY_PRIVATE" \
"{\"action\":\"delete\",\"id\":\"$t06_id\"}" 10 >/dev/null 2>&1
else
# Try to find and delete by search
sleep 1
resp=$(api "$WEBHOOK/api/search" "$KEY_PRIVATE" "{\"query\":\"$t06_title\"}")
found_id=$(echo "$resp" | python3 -c "import sys,json; r=json.load(sys.stdin).get('results',[]); print(r[0]['id'] if r else '')" 2>/dev/null)
[[ -n "$found_id" ]] && api "$WEBHOOK/api/manage" "$KEY_PRIVATE" \
"{\"action\":\"delete\",\"id\":\"$found_id\"}" 10 >/dev/null 2>&1
fi
if ! $t06_ok; then
fail "$T" "$t06_msg"
fi
# T07 SCHEMA_INTEGRITY
T="T07 SCHEMA_INTEGRITY"
table_count=$($DB_CMD -c "SELECT count(*) FROM information_schema.tables WHERE table_schema='public';" 2>/dev/null | tr -d '[:space:]')
col_count=$($DB_CMD -c "SELECT count(*) FROM information_schema.columns WHERE table_name='context_blocks';" 2>/dev/null | tr -d '[:space:]')
if [[ "$table_count" == "12" ]] && [[ "$col_count" == "30" ]]; then
pass "$T (tables=$table_count, columns=$col_count)"
else
fail "$T" "expected 12 tables + 30 columns, got tables=$table_count columns=$col_count"
fi
# T08 GUARD_STATS
T="T08 GUARD_STATS"
resp=$(api "$WEBHOOK/api/manage" "$KEY_PRIVATE" '{"action":"guard-stats"}')
if echo "$resp" | grep -q '"success":true'; then
pass "$T"
else
fail "$T" "expected success:true, got: ${resp:0:100}"
fi
# T09 GUARD_LIST_FILTER
T="T09 GUARD_LIST_FILTER"
resp=$(api "$WEBHOOK/api/manage" "$KEY_PRIVATE" '{"action":"guard-list","status":"clean"}')
non_clean=$(echo "$resp" | python3 -c "
import sys,json
d=json.load(sys.stdin)
blocks=d.get('blocks',[])
non_clean=[b for b in blocks if b.get('guard_status')!='clean']
print(len(non_clean))
" 2>/dev/null)
if [[ "$non_clean" == "0" ]]; then
count=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('count',0))" 2>/dev/null)
pass "$T (all $count blocks are clean)"
else
fail "$T" "found $non_clean non-clean blocks in filtered result"
fi
# T10 BACKUP_EXISTS
T="T10 BACKUP_EXISTS"
recent_dump=$(find /compose/n8n/backups/ -name '*.dump' -mmin -1500 -type f 2>/dev/null | head -1)
if [[ -n "$recent_dump" ]]; then
age_h=$(( ( $(date +%s) - $(stat -c %Y "$recent_dump") ) / 3600 ))
pass "$T ($(basename "$recent_dump"), ${age_h}h old)"
else
fail "$T" "no .dump file younger than 25h in /compose/n8n/backups/"
fi
# T11 DREAM_STATS
T="T11 DREAM_STATS"
resp=$(api "$WEBHOOK/api/manage" "$KEY_PRIVATE" '{"action":"dream-stats"}')
if echo "$resp" | grep -q '"success":true'; then
checked=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('dream_checked',0))" 2>/dev/null)
links=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('dream_links',0))" 2>/dev/null)
pass "$T (checked=$checked, links=$links)"
else
fail "$T" "dream-stats failed"
fi
# T12 DREAM_REVIEW
T="T12 DREAM_REVIEW"
resp=$(api "$WEBHOOK/api/manage" "$KEY_PRIVATE" '{"action":"dream-review"}')
if echo "$resp" | grep -q '"success":true'; then
pass "$T"
else
fail "$T" "dream-review failed"
fi
# =====================================================================
# PART 2: Retrieval Tests (Ollama required)
# =====================================================================
if $WITH_OLLAMA; then
echo ""
echo "--- Part 2: Retrieval Tests (Ollama) ---"
echo ""
# T13 SEARCH_BASIC
T="T13 SEARCH_BASIC"
resp=$(api "$WEBHOOK/api/search" "$KEY_PRIVATE" \
'{"query":"Write Guard"}' 120)
count=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('count',0))" 2>/dev/null)
if [[ -n "$count" ]] && (( count >= 1 )); then
pass "$T (count=$count)"
else
fail "$T" "expected >= 1 result, got count=$count"
fi
# T14 AGENT_CONFIDENT
T="T14 AGENT_CONFIDENT"
resp=$(api "$WEBHOOK/api/query" "$KEY_PRIVATE" \
'{"query":"How does the Write Guard work?"}' 120)
confidence=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('confidence',''))" 2>/dev/null)
answer=$(echo "$resp" | python3 -c "import sys,json; a=json.load(sys.stdin).get('answer',''); print('nonempty' if len(a)>10 else 'empty')" 2>/dev/null)
if [[ "$confidence" == "confident" ]] && [[ "$answer" == "nonempty" ]]; then
pass "$T (confidence=$confidence)"
else
fail "$T" "expected confidence=confident + nonempty answer, got confidence=$confidence answer=$answer"
fi
# T15 AGENT_NEGATIVE
T="T15 AGENT_NEGATIVE"
resp=$(api "$WEBHOOK/api/query" "$KEY_PRIVATE" \
'{"query":"Rezept fuer Kartoffelsuppe"}' 120)
answer=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('answer',''))" 2>/dev/null)
confidence=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('confidence',''))" 2>/dev/null)
if echo "$answer" | grep -qi "no_relevant_blocks_found\|keine antwort\|keine relevanten\|nicht relevant\|no relevant\|cannot answer\|kann.*nicht"; then
pass "$T (negative correctly detected)"
elif [[ "$confidence" == "none" ]] || [[ "$confidence" == "low" ]] || [[ "$confidence" == "no_relevant_blocks_found" ]]; then
pass "$T (confidence=$confidence)"
else
fail "$T" "expected rejection, got confidence=$confidence answer=${answer:0:80}"
fi
# T16 AGENT_BILINGUAL
T="T16 AGENT_BILINGUAL"
resp=$(api "$WEBHOOK/api/query" "$KEY_PRIVATE" \
'{"query":"PostgreSQL Mount-Pfad"}' 120)
answer=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('answer','').lower())" 2>/dev/null)
if echo "$answer" | grep -qi "postgresql\|mount\|/var/lib"; then
pass "$T"
else
fail "$T" "expected answer containing postgresql/mount, got: ${answer:0:80}"
fi
else
echo ""
echo "--- Part 2: Retrieval Tests SKIPPED (use --with-ollama) ---"
fi
# =====================================================================
# Summary
# =====================================================================
echo ""
TOTAL=$((PASS + FAIL))
echo "=== Results: $PASS/$TOTAL passed, $FAIL failed ==="
if (( FAIL > 0 )); then
exit 1
else
exit 0
fi