1212# (default 3); catches crashes right after start
1313# --log FILE copy the app's stdout / stderr there
1414# --keep-data do not delete the temporary data directory
15+ # --seed-sql FILE SQL applied to the profile database (querya.db) before
16+ # the app under test starts: the app runs once to create
17+ # and migrate the database, is stopped, FILE is applied
18+ # with sqlite3, and the app starts again
19+ # --run COMMAND shell command to run once the window is up (the app
20+ # stays up meanwhile); the exit code is its exit code.
21+ # Gets QUERYA_MCP_ENDPOINT (this run's own endpoint file),
22+ # QUERYA_HEADLESS_DB and QUERYA_HEADLESS_DATA
1523# -h, --help this text
1624#
17- # Exit codes: 0 the app started, showed a window and stayed up; 1 it died, or no
18- # window appeared in time; 2 usage error, missing bundle or missing tool.
25+ # Exit codes: 0 the app started, showed a window and stayed up (and --run
26+ # succeeded); 1 it died, no window appeared in time, or --run failed; 2 usage
27+ # error, missing bundle or missing tool.
1928#
2029# Needs: Xvfb; xdotool or xwininfo to see the window (without them the check is
2130# "the process is still alive after --wait"); ImageMagick `import` for
@@ -28,6 +37,8 @@ wait_s=60
2837keep_s=3
2938log_out=" "
3039keep_data=0
40+ seed_sql=" "
41+ run_cmd=" "
3142
3243usage () { sed -n ' 2,/^set -u/p' " $0 " | sed ' $d' | sed ' s/^# \{0,1\}//' ; }
3344die () { printf ' headless_app: %s\n' " $* " >&2 ; exit 2; }
@@ -41,6 +52,8 @@ while [ $# -gt 0 ]; do
4152 --keep) keep_s=" ${2:- } " ; shift 2 ;;
4253 --log) log_out=" ${2:- } " ; shift 2 ;;
4354 --keep-data) keep_data=1; shift ;;
55+ --seed-sql) seed_sql=" ${2:- } " ; shift 2 ;;
56+ --run) run_cmd=" ${2:- } " ; shift 2 ;;
4457 -h | --help) usage; exit 0 ;;
4558 * ) die " unknown option: $1 (see --help)" ;;
4659 esac
5467[ -n " $bundle " ] || die " no bundle found; build one (flutter build linux) or pass --bundle DIR"
5568[ -x " $bundle /querya_desktop" ] || die " $bundle /querya_desktop is missing or not executable"
5669command -v Xvfb > /dev/null 2>&1 || die " Xvfb is not installed (apt-get install xvfb)"
70+ if [ -n " $seed_sql " ]; then
71+ [ -f " $seed_sql " ] || die " --seed-sql: $seed_sql is not a file"
72+ command -v sqlite3 > /dev/null 2>&1 || die " --seed-sql needs the sqlite3 command (apt-get install sqlite3)"
73+ fi
5774if [ -n " $screenshot " ] && ! command -v import > /dev/null 2>&1 ; then
5875 die " --screenshot needs ImageMagick's 'import' (apt-get install imagemagick)"
5976fi
@@ -125,13 +142,32 @@ cp -a "$bundle" "$work/bundle" || die "could not copy $bundle"
125142export QUERYA_PORTABLE=1
126143export NO_AT_BRIDGE=1
127144
145+ # Own endpoint file, so that a second app on the machine (or a leftover one)
146+ # can never be the one querya-mcp talks to.
147+ export QUERYA_MCP_ENDPOINT=" $work /mcp-endpoint.json"
148+
128149runner=()
129150if command -v dbus-run-session > /dev/null 2>&1 ; then
130151 runner=(dbus-run-session --)
131152fi
132- ${runner[@]+" ${runner[@]} " } " $work /bundle/querya_desktop" > " $work /app.log" 2>&1 &
133- app_pid=$!
134- say " started querya_desktop (pid $app_pid )"
153+
154+ start_app () {
155+ ${runner[@]+" ${runner[@]} " } " $work /bundle/querya_desktop" >> " $work /app.log" 2>&1 &
156+ app_pid=$!
157+ say " started querya_desktop (pid $app_pid )"
158+ }
159+
160+ stop_app () {
161+ [ -n " $app_pid " ] || return 0
162+ kill -TERM " $app_pid " 2> /dev/null
163+ for _ in $( seq 1 20) ; do
164+ kill -0 " $app_pid " 2> /dev/null || break
165+ sleep 0.5
166+ done
167+ kill -KILL " $app_pid " 2> /dev/null
168+ wait " $app_pid " 2> /dev/null
169+ app_pid=" "
170+ }
135171
136172have_window_tool=0
137173if command -v xdotool > /dev/null 2>&1 || command -v xwininfo > /dev/null 2>&1 ; then
@@ -147,21 +183,37 @@ window_up() {
147183 fi
148184}
149185
150- deadline=$(( SECONDS + wait_s))
151- shown=0
152- while [ " $SECONDS " -lt " $deadline " ]; do
153- kill -0 " $app_pid " 2> /dev/null || fail_with_log " the app exited before showing a window"
186+ wait_for_window () {
187+ local deadline=$(( SECONDS + wait_s)) shown=0
188+ while [ " $SECONDS " -lt " $deadline " ]; do
189+ kill -0 " $app_pid " 2> /dev/null || fail_with_log " the app exited before showing a window"
190+ if [ " $have_window_tool " -eq 1 ]; then
191+ if window_up; then shown=1; break ; fi
192+ fi
193+ sleep 1
194+ done
154195 if [ " $have_window_tool " -eq 1 ]; then
155- if window_up; then shown=1; break ; fi
196+ [ " $shown " -eq 1 ] || fail_with_log " no window after ${wait_s} s"
197+ say " window is up"
198+ else
199+ say " no xdotool / xwininfo: only checking that the process stays alive"
156200 fi
157- sleep 1
158- done
201+ }
159202
160- if [ " $have_window_tool " -eq 1 ]; then
161- [ " $shown " -eq 1 ] || fail_with_log " no window after ${wait_s} s"
162- say " window is up"
163- else
164- say " no xdotool / xwininfo: only checking that the process stays alive"
203+ start_app
204+ wait_for_window
205+
206+ if [ -n " $seed_sql " ]; then
207+ # The first start created and migrated the profile database. Let it settle,
208+ # stop the app (it holds the file open), seed, and start again.
209+ sleep 2
210+ stop_app
211+ db=" $( find " $work /bundle" -name querya.db -print -quit 2> /dev/null) "
212+ [ -n " $db " ] || fail_with_log " the profile database querya.db was not created"
213+ sqlite3 " $db " < " $seed_sql " || fail_with_log " could not apply $seed_sql to $db "
214+ say " seeded $( basename " $seed_sql " ) into $db "
215+ start_app
216+ wait_for_window
165217fi
166218
167219end=$(( SECONDS + keep_s))
@@ -180,5 +232,16 @@ if [ -n "$screenshot" ]; then
180232 fi
181233fi
182234
235+ if [ -n " $run_cmd " ]; then
236+ export QUERYA_HEADLESS_DB
237+ QUERYA_HEADLESS_DB=" $( find " $work /bundle" -name querya.db -print -quit 2> /dev/null) "
238+ export QUERYA_HEADLESS_DATA=" $work /bundle/QueryaData"
239+ say " running: $run_cmd "
240+ if ! bash -c " $run_cmd " ; then
241+ fail_with_log " --run command failed"
242+ fi
243+ kill -0 " $app_pid " 2> /dev/null || fail_with_log " the app died while the command ran"
244+ fi
245+
183246say " ok"
184247exit 0
0 commit comments