c@75: #!/bin/bash 
c@75: 
c@75: set -eu
c@75: 
c@75: piperdir=../piper
c@75: vampsdkdir=../vamp-plugin-sdk
c@75: schemadir="$piperdir"/json/schema
c@75: 
c@116: tmpdir=$(mktemp -d)
c@116: 
c@116: if [ ! -d "$tmpdir" ]; then
c@116:     echo "Temp directory creation failed" 1>&2
c@116:     exit 1
c@116: fi
c@116: 
c@116: trap "rm -rf $tmpdir" 0
c@116: 
c@116: reqfile="$tmpdir/req.json"
c@116: respfile="$tmpdir/resp.json"
c@116: allrespfile="$tmpdir/resp.all"
c@116: input="$tmpdir/input"
c@116: expected="$tmpdir/expected"
c@116: obtained="$tmpdir/obtained"
c@75: 
c@75: validate() {
c@75:     local file="$1"
c@75:     local schemaname="$2"
c@75:     jsonschema -i "$file" "$schemadir/$schemaname.json" 1>&2 && \
c@75:         echo "validated $schemaname" 1>&2 || \
c@75:         echo "failed to validate $schemaname" 1>&2
c@75: }
c@75: 
c@75: validate_request() {
c@75:     local json="$1"
c@75:     echo "$json" > "$reqfile"
c@75:     validate "$reqfile" "rpcrequest"
c@75: }
c@75: 
c@75: validate_response() {
c@75:     local json="$1"
c@75:     echo "$json" > "$respfile"
c@75:     validate "$respfile" "rpcresponse"
c@75: }
c@75: 
c@116: cat > "$input" <<EOF
c@75: {"method":"list"}
c@127: {"method":"list","params": {"from":["vamp-example-plugins","something-nonexistent"]}}
c@127: {"method":"list","params": {"from":["something-nonexistent"]}}
c@75: {"method":"load","id":6,"params": {"key":"vamp-example-plugins:percussiononsets","inputSampleRate":44100,"adapterFlags":["AdaptInputDomain","AdaptBufferSize"]}}
c@75: {"method":"configure","id":"weevil","params":{"handle":1,"configuration":{"blockSize": 8, "channelCount": 1, "parameterValues": {"sensitivity": 40, "threshold": 3}, "stepSize": 8}}}
c@75: {"method":"process","params": {"handle": 1, "processInput": { "timestamp": {"s": 0, "n": 0}, "inputBuffers": [ [1,2,3,4,5,6,7,8] ]}}}
c@75: {"method":"finish","params": {"handle": 1}}
c@75: EOF
c@75: 
c@75: # Expected output, apart from the plugin list which seems a bit
c@75: # fragile to check here
c@75: cat > "$expected" <<EOF
c@75: {"id": 6, "jsonrpc": "2.0", "method": "load", "result": {"defaultConfiguration": {"blockSize": 1024, "channelCount": 1, "parameterValues": {"sensitivity": 40, "threshold": 3}, "stepSize": 1024}, "handle": 1, "staticData": {"basic": {"description": "Detect percussive note onsets by identifying broadband energy rises", "identifier": "percussiononsets", "name": "Simple Percussion Onset Detector"}, "basicOutputInfo": [{"description": "Percussive note onset locations", "identifier": "onsets", "name": "Onsets"}, {"description": "Broadband energy rise detection function", "identifier": "detectionfunction", "name": "Detection Function"}], "category": ["Time", "Onsets"], "copyright": "Code copyright 2006 Queen Mary, University of London, after Dan Barry et al 2005.  Freely redistributable (BSD license)", "inputDomain": "TimeDomain", "key": "vamp-example-plugins:percussiononsets", "maker": "Vamp SDK Example Plugins", "maxChannelCount": 1, "minChannelCount": 1, "parameters": [{"basic": {"description": "Energy rise within a frequency bin necessary to count toward broadband total", "identifier": "threshold", "name": "Energy rise threshold"}, "defaultValue": 3, "extents": {"max": 20, "min": 0}, "unit": "dB", "valueNames": []}, {"basic": {"description": "Sensitivity of peak detector applied to broadband detection function", "identifier": "sensitivity", "name": "Sensitivity"}, "defaultValue": 40, "extents": {"max": 100, "min": 0}, "unit": "%", "valueNames": []}], "programs": [], "version": 2}}}
c@75: {"id": "weevil", "jsonrpc": "2.0", "method": "configure", "result": {"handle": 1, "outputList": [{"basic": {"description": "Percussive note onset locations", "identifier": "onsets", "name": "Onsets"}, "configured": {"binCount": 0, "binNames": [], "hasDuration": false, "sampleRate": 44100, "sampleType": "VariableSampleRate", "unit": ""}}, {"basic": {"description": "Broadband energy rise detection function", "identifier": "detectionfunction", "name": "Detection Function"}, "configured": {"binCount": 1, "binNames": [""], "hasDuration": false, "quantizeStep": 1, "sampleRate": 86.1328125, "sampleType": "FixedSampleRate", "unit": ""}}]}}
c@75: {"jsonrpc": "2.0", "method": "process", "result": {"features": {}, "handle": 1}}
c@75: {"jsonrpc": "2.0", "method": "finish", "result": {"features": {"detectionfunction": [{"featureValues": [0], "timestamp": {"n": 11609977, "s": 0}}]}, "handle": 1}}
c@75: EOF
c@75: 
c@116: # We run the whole test twice, once with the server in Capnp mode
c@116: # (converting to JSON using piper-convert) and once with it directly
c@116: # in JSON mode
c@75: 
c@129: for format in json capnp ; do
c@75: 
c@117:     ( export VAMP_PATH="$vampsdkdir"/examples ;
c@117:       while read request ; do
c@116:           validate_request "$request"
c@116:           echo "$request"
c@116:       done |
c@116:           if [ "$format" = "json" ]; then
c@125:               bin/piper-vamp-simple-server -d json
c@116:           else
c@116:               bin/piper-convert request -i json -o capnp |
c@125:                   bin/piper-vamp-simple-server -d capnp |
c@116:                   bin/piper-convert response -i capnp -o json
c@116:           fi |
c@116:           while read response ; do
c@116:               echo "$response" >> "$allrespfile"
c@116:               validate_response "$response"
c@116:           done
c@116:     ) < "$input"
c@116: 
c@127:     # Skip plugin lists
c@127:     tail -n +4 "$allrespfile" > "$obtained"
c@116: 
c@116:     echo "Checking response contents against expected contents..."
c@116:     if ! cmp "$obtained" "$expected"; then
c@116:         diff -u1 "$obtained" "$expected"
c@116:     else
c@116:         echo "OK"
c@116:     fi
c@116: 
c@127:     echo "Checking plugin counts from list responses..."
c@127:     
c@127:     # Now check the plugin lists, but as the descriptions etc are
c@127:     # probably a bit fragile, let's just count the number of plugins
c@127: 
c@127:     # First, with no "from" arg to the list call
c@127:     list_no_from=$(head -n +1 "$allrespfile" | fmt -1 | grep '"key"' | wc -l)
c@127: 
c@127:     # Now with a "from" arg that includes the library that exists
c@127:     list_with_good_from=$(tail -n +2 "$allrespfile" | head -n +1 | fmt -1 |
c@127:                               grep '"key"' | wc -l)
c@127:     
c@127:     # Now with a "from" arg that doesn't include any real library
c@127:     list_with_bad_from=$(tail -n +3 "$allrespfile" | head -n +1 | fmt -1 |
c@127:                              grep '"key"' | wc -l)
c@127: 
c@127:     if [ "$list_no_from" != "6" ]; then
c@127:         echo "Wrong number of plugins from list response without \"from\" arg"
c@127:         echo "Expected 6, obtained $list_no_from"
c@127:         false
c@127:     fi
c@127:     if [ "$list_with_good_from" != "6" ]; then
c@127:         echo "Wrong number of plugins from list response with good \"from\" arg"
c@127:         echo "Expected 6, obtained $list_with_good_from"
c@127:         false
c@127:     fi
c@127:     if [ "$list_with_bad_from" != "0" ]; then
c@127:         echo "Wrong number of plugins from list response with bad \"from\" arg"
c@127:         echo "Expected 0, obtained $list_with_bad_from"
c@127:         false
c@127:     fi
c@127:     echo OK
c@127:     
c@116:     rm "$allrespfile"
c@116: 
c@116: done
c@127: 
c@127: echo "Tests succeeded"  # set -e at top should ensure we don't get here otherwise