-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsds-test.sh
More file actions
executable file
·520 lines (404 loc) · 22 KB
/
sds-test.sh
File metadata and controls
executable file
·520 lines (404 loc) · 22 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env bash
# sds-test.sh
#
# Creates an "sds-test" item in a store and populates it with items and links
# that exercise every sds-command function (except server-dependent operations,
# large values, and --file).
#
# Usage:
# bash sds-test.sh [<store-id> [<npm-package> [<persistence-dir> [<server-url> [<token>]]]]]
#
# The last three arguments can also be supplied via environment variables:
# SDS_PERSISTENCE_DIR local directory for SQLite files (default: ~/.sds)
# SDS_SERVER_URL WebSocket server URL — enables store ping + sync
# SDS_TOKEN client JWT — required when SDS_SERVER_URL is set
#
# Examples:
# bash sds-test.sh
# bash sds-test.sh my-store @rozek/sds-command-yjs /tmp/sds-data
# SDS_SERVER_URL=ws://localhost:8080 SDS_TOKEN=eyJ... bash sds-test.sh
set -euo pipefail
STORE="${1:-sds-test}"
PKG="${2:-@rozek/sds-command-jj}"
PERSISTENCE_DIR="${3:-${SDS_PERSISTENCE_DIR:-}}"
SERVER_URL="${4:-${SDS_SERVER_URL:-}}"
TOKEN="${5:-${SDS_TOKEN:-}}"
if [[ -n "$SERVER_URL" && -z "$TOKEN" ]]; then
echo "Error: a server URL was given but no token — set SDS_TOKEN or pass it as 5th argument" >&2
exit 2
fi
#───────────────────────────────────────────────────────────────────────────────
# helpers
#───────────────────────────────────────────────────────────────────────────────
# extract "id" from pretty-printed sds JSON output (JSON.stringify uses 2-space
# indent and a space after ":", e.g. "id": "abc-123").
# sed always exits 0 (unlike grep), so set -o pipefail never fires here.
json_id() { printf '%s' "$1" | sed -n 's/.*"id": *"\([^"]*\)".*/\1/p'; }
# build the global option array shared by every sds invocation
GLOBAL_OPTS=(--store "$STORE")
[[ -n "$PERSISTENCE_DIR" ]] && GLOBAL_OPTS+=(--persistence-dir "$PERSISTENCE_DIR")
[[ -n "$SERVER_URL" ]] && GLOBAL_OPTS+=(--server "$SERVER_URL")
[[ -n "$TOKEN" ]] && GLOBAL_OPTS+=(--token "$TOKEN")
# run a command with visible echo
run() {
echo " \$ npx $PKG ${GLOBAL_OPTS[*]} $*"
npx "$PKG" "${GLOBAL_OPTS[@]}" "$@"
}
# run with --format json, return raw output (used when capturing IDs)
runj() { npx "$PKG" "${GLOBAL_OPTS[@]}" --format json "$@"; }
section() {
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
printf " %s\n" "$*"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
}
ok() { echo " ✓ $*"; }
#───────────────────────────────────────────────────────────────────────────────
# 1. store info (before anything exists)
#───────────────────────────────────────────────────────────────────────────────
section "1. store info (initial)"
run store info
ok "store info"
#───────────────────────────────────────────────────────────────────────────────
# 1b. network: ping + sync (only when --server / SDS_SERVER_URL is set)
#───────────────────────────────────────────────────────────────────────────────
if [[ -n "$SERVER_URL" ]]; then
section "1b. store ping + sync"
echo " — ping server:"
run store ping
ok "store ping"
echo " — sync (pull remote state before local writes):"
run store sync
ok "store sync"
echo " — sync with explicit timeout:"
run store sync --timeout 10000
ok "store sync --timeout"
fi
#───────────────────────────────────────────────────────────────────────────────
# 2. create the "sds-test" container item at root
#───────────────────────────────────────────────────────────────────────────────
section "2. Create 'sds-test' container"
JSON=$(runj entry create --label "sds-test")
TEST_ID=$(json_id "$JSON")
echo " → $JSON"
ok "entry create (item, label only) → $TEST_ID"
#───────────────────────────────────────────────────────────────────────────────
# 3. create child items inside sds-test
#───────────────────────────────────────────────────────────────────────────────
section "3. entry create — child items"
# item with label + mime + value
JSON=$(runj entry create \
--container "$TEST_ID" \
--label "Text Item" \
--mime "text/plain" \
--value "Hello, SDS!")
ITEM_TEXT_ID=$(json_id "$JSON")
ok "entry create (label + mime + value) → $ITEM_TEXT_ID"
# item with --info <json>
JSON=$(runj entry create \
--container "$TEST_ID" \
--label "Info Item (JSON)" \
--mime "application/json" \
--value '{"key":"value"}' \
--info '{"author":"alice","status":"draft","priority":1}')
ITEM_INFO_ID=$(json_id "$JSON")
ok "entry create (--info json) → $ITEM_INFO_ID"
# item with --info.<key> notation
JSON=$(runj entry create \
--container "$TEST_ID" \
--label "Info Item (keys)" \
--info.author "bob" \
--info.status "review" \
--info.count 42)
ITEM_INFOKEYS_ID=$(json_id "$JSON")
ok "entry create (--info.<key>) → $ITEM_INFOKEYS_ID"
# item inserted at the front (--at 0)
JSON=$(runj entry create \
--container "$TEST_ID" \
--label "First Item" \
--at 0)
ITEM_FIRST_ID=$(json_id "$JSON")
ok "entry create (--at 0) → $ITEM_FIRST_ID"
# sub-container inside sds-test
JSON=$(runj entry create \
--container "$TEST_ID" \
--label "Sub-Container")
SUB_ID=$(json_id "$JSON")
ok "entry create (sub-container) → $SUB_ID"
# item nested inside the sub-container
JSON=$(runj entry create \
--container "$SUB_ID" \
--label "Nested Item" \
--mime "text/plain" \
--value "I live inside Sub-Container")
ITEM_NESTED_ID=$(json_id "$JSON")
ok "entry create (item in sub-container) → $ITEM_NESTED_ID"
# item with --info-delete.<key> on create (accepted, no-op — no keys to remove)
JSON=$(runj entry create \
--container "$TEST_ID" \
--label "Delete-flag on create" \
--info.color "red" \
--info-delete.color)
ITEM_NODELETE_ID=$(json_id "$JSON")
ok "entry create (--info-delete.<key> on new entry — no-op) → $ITEM_NODELETE_ID"
#───────────────────────────────────────────────────────────────────────────────
# 4. create links inside sds-test
#───────────────────────────────────────────────────────────────────────────────
section "4. entry create — links"
# plain link (--target)
JSON=$(runj entry create \
--container "$TEST_ID" \
--target "$ITEM_TEXT_ID" \
--label "Link to Text Item")
LINK_TEXT_ID=$(json_id "$JSON")
ok "entry create (link, --target) → $LINK_TEXT_ID"
# link with --info.<key>
JSON=$(runj entry create \
--container "$TEST_ID" \
--target "$ITEM_INFO_ID" \
--label "Link with Info" \
--info.role "alias" \
--info.note "cross-reference")
LINK_INFO_ID=$(json_id "$JSON")
ok "entry create (link, --target + --info.<key>) → $LINK_INFO_ID"
# link at an explicit position inside sub-container
JSON=$(runj entry create \
--container "$SUB_ID" \
--target "$ITEM_TEXT_ID" \
--label "Pinned Link" \
--at 0)
LINK_PINNED_ID=$(json_id "$JSON")
ok "entry create (link, --at 0 in sub-container) → $LINK_PINNED_ID"
#───────────────────────────────────────────────────────────────────────────────
# 5. entry get — field selection
#───────────────────────────────────────────────────────────────────────────────
section "5. entry get"
echo " — all fields (item):"
run entry get "$ITEM_TEXT_ID"
ok "entry get (item, all fields)"
echo " — --kind:"
run entry get "$ITEM_TEXT_ID" --kind
ok "entry get --kind"
echo " — --label:"
run entry get "$ITEM_TEXT_ID" --label
ok "entry get --label"
echo " — --mime:"
run entry get "$ITEM_TEXT_ID" --mime
ok "entry get --mime"
echo " — --value:"
run entry get "$ITEM_TEXT_ID" --value
ok "entry get --value"
echo " — combined --kind --label --mime --value:"
run entry get "$ITEM_TEXT_ID" --kind --label --mime --value
ok "entry get (multiple flags)"
echo " — --info (full map):"
run entry get "$ITEM_INFO_ID" --info
ok "entry get --info"
echo " — --info.<key>:"
run entry get "$ITEM_INFO_ID" --info.author
ok "entry get --info.<key>"
echo " — all fields (link):"
run entry get "$LINK_TEXT_ID"
ok "entry get (link, all fields)"
echo " — --target (link):"
run entry get "$LINK_TEXT_ID" --target
ok "entry get --target (link)"
echo " — entry get with --format json:"
run --format json entry get "$ITEM_INFO_ID" --label --info
ok "entry get (--format json)"
#───────────────────────────────────────────────────────────────────────────────
# 6. entry list
#───────────────────────────────────────────────────────────────────────────────
section "6. entry list"
echo " — plain list:"
run entry list "$TEST_ID"
ok "entry list (plain)"
echo " — --label --mime:"
run entry list "$TEST_ID" --label --mime
ok "entry list --label --mime"
echo " — --value:"
run entry list "$TEST_ID" --value
ok "entry list --value"
echo " — --info (full map):"
run entry list "$TEST_ID" --info
ok "entry list --info"
echo " — --info.<key>:"
run entry list "$TEST_ID" --info.author
ok "entry list --info.<key>"
echo " — --only items:"
run entry list "$TEST_ID" --only items
ok "entry list --only items"
echo " — --only links:"
run entry list "$TEST_ID" --only links
ok "entry list --only links"
echo " — --recursive:"
run entry list "$TEST_ID" --recursive --label
ok "entry list --recursive"
echo " — --recursive --depth 1:"
run entry list "$TEST_ID" --recursive --depth 1 --label
ok "entry list --recursive --depth 1"
echo " — list root:"
run entry list root --label
ok "entry list root"
echo " — entry list with --format json:"
run --format json entry list "$TEST_ID" --label --mime
ok "entry list (--format json)"
#───────────────────────────────────────────────────────────────────────────────
# 7. entry update
#───────────────────────────────────────────────────────────────────────────────
section "7. entry update"
echo " — update label:"
run entry update "$ITEM_TEXT_ID" --label "Updated Text Item"
ok "entry update --label"
echo " — update mime + value:"
run entry update "$ITEM_TEXT_ID" --mime "text/markdown" --value "**Updated** content"
ok "entry update --mime --value"
echo " — update with --info json (merge):"
run entry update "$ITEM_INFO_ID" --info '{"status":"final","version":2}'
ok "entry update --info (json merge)"
echo " — update with --info.<key>:"
run entry update "$ITEM_INFOKEYS_ID" \
--info.status "approved" \
--info.reviewer "carol"
ok "entry update --info.<key>"
echo " — delete one info key:"
run entry update "$ITEM_INFO_ID" --info-delete.author
ok "entry update --info-delete.<key>"
echo " — combined: add key + delete key (delete wins on same key):"
run entry update "$ITEM_INFOKEYS_ID" \
--info.note "added" \
--info-delete.count
ok "entry update --info.<key> + --info-delete.<key>"
echo " — delete-wins conflict (same key in both --info.<key> and --info-delete.<key>):"
run entry update "$ITEM_INFOKEYS_ID" \
--info.reviewer "dave" \
--info-delete.reviewer
ok "entry update (delete-wins conflict)"
echo " — update link label:"
run entry update "$LINK_TEXT_ID" --label "Alias to Text Item"
ok "entry update (link) --label"
echo " — update link info:"
run entry update "$LINK_INFO_ID" --info.role "bookmark"
ok "entry update (link) --info.<key>"
#───────────────────────────────────────────────────────────────────────────────
# 8. entry move
#───────────────────────────────────────────────────────────────────────────────
section "8. entry move"
echo " — move ITEM_FIRST to sub-container:"
run entry move "$ITEM_FIRST_ID" --to "$SUB_ID"
ok "entry move --to"
echo " — move ITEM_FIRST back to sds-test at position 0:"
run entry move "$ITEM_FIRST_ID" --to "$TEST_ID" --at 0
ok "entry move --to --at"
#───────────────────────────────────────────────────────────────────────────────
# 9. tree show
#───────────────────────────────────────────────────────────────────────────────
section "9. tree show"
echo " — full tree:"
run tree show
ok "tree show"
echo " — tree with --depth 2:"
run tree show --depth 2
ok "tree show --depth"
echo " — tree with --format json:"
run --format json tree show --depth 1
ok "tree show (--format json)"
#───────────────────────────────────────────────────────────────────────────────
# 10. store info (mid-run)
#───────────────────────────────────────────────────────────────────────────────
section "10. store info (mid-run)"
run store info
ok "store info"
#───────────────────────────────────────────────────────────────────────────────
# 11. entry delete / trash / restore / purge
#───────────────────────────────────────────────────────────────────────────────
section "11. entry delete / trash / restore / purge"
echo " — soft-delete ITEM_NESTED (moves to trash):"
run entry delete "$ITEM_NESTED_ID"
ok "entry delete"
echo " — trash list:"
run trash list
ok "trash list"
echo " — trash list --only items:"
run trash list --only items
ok "trash list --only items"
echo " — trash list --only links:"
run trash list --only links
ok "trash list --only links"
echo " — restore ITEM_NESTED to sds-test (default container = root would be wrong, use TEST_ID):"
run entry restore "$ITEM_NESTED_ID" --to "$TEST_ID"
ok "entry restore --to"
echo " — delete ITEM_NESTED again:"
run entry delete "$ITEM_NESTED_ID"
echo " — restore with --at 0:"
run entry restore "$ITEM_NESTED_ID" --to "$TEST_ID" --at 0
ok "entry restore --to --at"
echo " — delete ITEM_NESTED once more for purge:"
run entry delete "$ITEM_NESTED_ID"
echo " — purge ITEM_NESTED permanently:"
run entry purge "$ITEM_NESTED_ID"
ok "entry purge"
#───────────────────────────────────────────────────────────────────────────────
# 12. trash purge-expired (with a very long TTL — nothing actually expires)
#───────────────────────────────────────────────────────────────────────────────
section "12. trash purge-expired"
run trash purge-expired --ttl 999999999999
ok "trash purge-expired --ttl"
#───────────────────────────────────────────────────────────────────────────────
# 13. trash purge-all
#───────────────────────────────────────────────────────────────────────────────
section "13. trash purge-all"
echo " — delete some entries first to populate trash:"
run entry delete "$LINK_PINNED_ID"
run entry delete "$LINK_INFO_ID"
run entry delete "$ITEM_NODELETE_ID"
echo " — trash list (--format json):"
run --format json trash list
ok "trash list (--format json)"
run trash purge-all
ok "trash purge-all"
#───────────────────────────────────────────────────────────────────────────────
# 14. store export / import
#───────────────────────────────────────────────────────────────────────────────
section "14. store export / import"
EXPORT_JSON="/tmp/sds-test-export.json"
EXPORT_BIN="/tmp/sds-test-export.bin"
echo " — export JSON:"
run store export --encoding json --output "$EXPORT_JSON"
ok "store export --encoding json --output"
echo " — export binary:"
run store export --encoding binary --output "$EXPORT_BIN"
ok "store export --encoding binary --output"
echo " — import from JSON file (CRDT-merge):"
run store import --input "$EXPORT_JSON"
ok "store import (json)"
echo " — import from binary file (CRDT-merge):"
run store import --input "$EXPORT_BIN"
ok "store import (binary)"
rm -f "$EXPORT_JSON" "$EXPORT_BIN"
#───────────────────────────────────────────────────────────────────────────────
# 14b. sync after writes (only when --server / SDS_SERVER_URL is set)
#───────────────────────────────────────────────────────────────────────────────
if [[ -n "$SERVER_URL" ]]; then
section "14b. store sync (push local changes)"
run store sync
ok "store sync (after writes)"
fi
#───────────────────────────────────────────────────────────────────────────────
# 15. final state
#───────────────────────────────────────────────────────────────────────────────
section "15. Final state"
run store info
run tree show
ok "final store info + tree"
#───────────────────────────────────────────────────────────────────────────────
# 16. clean up
#───────────────────────────────────────────────────────────────────────────────
section "16. Cleanup"
# run store destroy
# ok "store destroy"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " All sds-command functions exercised."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""