Repository navigation
Expand file tree
/
Copy pathtest-ios-offline-sync-e2e.sh
More file actions
executable file
·84 lines (71 loc) · 3.52 KB
/
Copy pathtest-ios-offline-sync-e2e.sh
File metadata and controls
executable file
·84 lines (71 loc) · 3.52 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
#!/usr/bin/env bash
set -euo pipefail
repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
app_id=${LOGSEQ_IOS_APP_ID:-com.logseq.logseq}
base_url=${LOGSEQ_E2E_BASE_URL:-http://127.0.0.1:8787}
offline_base_url=${LOGSEQ_E2E_OFFLINE_BASE_URL:-http://127.0.0.1:18787}
graph_name=${LOGSEQ_E2E_GRAPH_NAME:?set LOGSEQ_E2E_GRAPH_NAME to the cached graph name}
existing_block=${LOGSEQ_E2E_EXISTING_BLOCK:?set LOGSEQ_E2E_EXISTING_BLOCK to a cached block title}
offline_block=${LOGSEQ_E2E_OFFLINE_BLOCK:-"iOS offline $(date +%s)"}
die() {
echo "error: $*" >&2
exit 1
}
device=${LOGSEQ_IOS_SIMULATOR_UDID:-}
if [[ -z $device ]]; then
device=$(
xcrun simctl list devices booted \
| awk -F'[()]' '/Booted/ { print $2; exit }'
)
fi
[[ -n $device ]] || die "no booted iOS simulator was found"
maestro_bin=${MAESTRO_BIN:-}
if [[ -z $maestro_bin ]]; then
if command -v maestro >/dev/null 2>&1; then
maestro_bin=$(command -v maestro)
else
maestro_bin=$(find /opt/homebrew/Cellar/maestro -path '*/bin/maestro' -type f 2>/dev/null | sort -V | tail -1)
fi
fi
[[ -n $maestro_bin && -x $maestro_bin ]] || die "Maestro CLI is not installed"
app_container=$(xcrun simctl get_app_container "$device" "$app_id" data 2>/dev/null) \
|| die "install and seed the app with an online graph before running the offline E2E"
[[ -f $app_container/Documents/logseq.sqlite ]] \
|| die "the simulator does not contain a seeded Logseq database"
preferences="$app_container/Library/Preferences/$app_id.plist"
graph_id=$(plutil -extract 'logseq\.selectedGraphId' raw "$preferences" 2>/dev/null) \
|| die "the simulator does not have a selected graph"
graph_directory_name=${graph_id//-/%2D}
checkpoint="$app_container/Documents/graphs/$graph_directory_name/sync.checkpoint"
[[ -f $checkpoint ]] || die "the selected graph does not have a sync checkpoint"
cursor_before=$(jq -er '. as $checkpoint | $checkpoint[index("~:applied-server-t") + 1]' "$checkpoint") \
|| die "the selected graph checkpoint does not contain applied-server-t"
temporary_directory=$(mktemp -d "${TMPDIR:-/tmp}/logseq-offline-sync.XXXXXX")
offline_flow="$temporary_directory/offline.yaml"
reconnect_flow="$temporary_directory/reconnect.yaml"
restore_base_url() {
xcrun simctl spawn "$device" defaults write "$app_id" logseq.baseURL "$base_url" >/dev/null
rm -rf "$temporary_directory"
}
trap restore_base_url EXIT
render_flow() {
local source=$1
local destination=$2
sed \
-e "s|__LOGSEQ_E2E_GRAPH_NAME__|$graph_name|g" \
-e "s|__LOGSEQ_E2E_EXISTING_BLOCK__|$existing_block|g" \
-e "s|__LOGSEQ_E2E_OFFLINE_BLOCK__|$offline_block|g" \
"$source" > "$destination"
}
render_flow "$repo_root/tests/e2e/ios-offline-restart.yaml" "$offline_flow"
render_flow "$repo_root/tests/e2e/ios-offline-reconnect.yaml" "$reconnect_flow"
xcrun simctl terminate "$device" "$app_id" >/dev/null 2>&1 || true
xcrun simctl spawn "$device" defaults write "$app_id" logseq.baseURL "$offline_base_url"
MAESTRO_CLI_NO_ANALYTICS=1 "$maestro_bin" --device "$device" test "$offline_flow"
xcrun simctl spawn "$device" defaults write "$app_id" logseq.baseURL "$base_url"
MAESTRO_CLI_NO_ANALYTICS=1 "$maestro_bin" --device "$device" test "$reconnect_flow"
cursor_after=$(jq -er '. as $checkpoint | $checkpoint[index("~:applied-server-t") + 1]' "$checkpoint") \
|| die "the reconnected graph checkpoint does not contain applied-server-t"
(( cursor_after > cursor_before )) \
|| die "the WebSocket sync cursor did not advance after the offline block was submitted"
echo "$offline_block cursor $cursor_before -> $cursor_after"