#!/bin/sh
# Exercise the installed binary offline: shell completions, the MCP server
# (handshake and a real tool call), and the local job store.
set -eu

export HOME="$AUTOPKGTEST_TMP/home"
export XDG_CONFIG_HOME="$HOME/.config" XDG_DATA_HOME="$HOME/.local/share"
mkdir -p "$HOME"
cd "$AUTOPKGTEST_TMP"

version=$(dpkg-query -W -f '${source:Upstream-Version}' crumb)

for shell in bash zsh fish; do
	crumb completions "$shell" > "completion.$shell"
	grep -q 'crumb' "completion.$shell"
done
bash -n completion.bash

printf '%s\n' \
	'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"autopkgtest","version":"0"}}}' \
	'{"jsonrpc":"2.0","id":2,"method":"tools/list"}' \
	'{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"current_time","arguments":{"utc_offset":"+00:00"}}}' \
	| timeout 60 crumb mcp serve > mcp.out
cat mcp.out
grep -q "\"serverInfo\":{\"name\":\"crumb\",\"version\":\"$version\"}" mcp.out
grep -q '"name":"current_time"' mcp.out
grep -q '"id":3,"jsonrpc":"2.0","result":{"content":\[{"text":"Current time at UTC+00:00: ' mcp.out
grep -q '"isError":false' mcp.out

test "$(crumb jobs list)" = "[]"

if crumb no-such-command 2> usage.err; then
	echo "unknown subcommand was accepted" >&2
	exit 1
fi
grep -q '^Error: usage: crumb' usage.err
