#!/bin/bash # Wird von tests/run.sh eingebunden: die opencode-Brücke gegen einen Nachbau. # Erwartet: $TMP, $PORT, ok/bad/contains, ./junction echo "== 18. opencode serve als Endpunkt (Brücke zu /v1/) ==" OC_PORT=$((PORT+18)) OC_JUNC=$((PORT+19)) python3 tests/opencode_stub.py $OC_PORT > "$TMP/oc-stub.log" 2>&1 & PIDS+=($!) sleep 0.6 printf 'opencode:testpasswort\n' > "$TMP/oc.pass" chmod +x "$TMP/oc.pass" cat > "$TMP/oc.pass.sh" <<'SH' #!/bin/sh cat "$1" 2>/dev/null SH # Ein Skript, das das Passwort ausgibt — so wie im Betrieb opencode_password.sh. printf '#!/bin/sh\necho opencode:testpasswort | cut -d: -f2\n' > "$TMP/oc.pass.sh" chmod +x "$TMP/oc.pass.sh" cat > "$TMP/oc.conf" < "$TMP/oc.log" 2>&1 & PIDS+=($!) sleep 0.8 # 1. Modellliste kommt als OpenAI-Liste, ohne die Varianten der Modelle R=$(curl -s -m 20 "http://127.0.0.1:$OC_JUNC/v1/models") contains "$R" '"id":"deepseek-v4.1-flash"' "the bridge lists opencode models as an OpenAI list" case "$R" in *'"id":"low"'*) bad "model variants leak into the list as models";; *) ok "model variants do not leak into the list";; esac # 2. Chat: OpenAI rein, Antwort raus R=$(curl -s -m 40 -X POST -H 'Content-Type: application/json' \ -d '{"model":"deepseek-v4.1-flash","messages":[{"role":"user","content":"hallo welt"}],"max_tokens":10}' \ "http://127.0.0.1:$OC_JUNC/v1/chat/completions") contains "$R" '"object":"chat.completion"' "a chat request becomes an OpenAI completion" contains "$R" 'ANTWORT auf: hallo welt' "the prompt reached the session and the answer came back" contains "$R" '"reasoning_content":"kurz nachgedacht"' "reasoning is carried separately from the answer" contains "$R" '"finish_reason":"stop"' "the finish reason is translated" contains "$R" '"prompt_tokens":11' "the token counts are read from the session" # 3. Das Modell kam als provider/id an R=$(curl -s -m 20 "http://127.0.0.1:$OC_JUNC/v1/models") case "$R" in *'"object":"list"'*) ok "the models call goes through the bridge, not the plain proxy";; *) bad "the models call did not go through the bridge";; esac # 4. Ein Systemprompt wird mitgeschickt, nicht verschluckt R=$(curl -s -m 40 -X POST -H 'Content-Type: application/json' \ -d '{"model":"deepseek-v4.1-flash-alt","messages":[{"role":"system","content":"sei knapp"},{"role":"user","content":"frage"}]}' \ "http://127.0.0.1:$OC_JUNC/v1/chat/completions") contains "$R" 'System: sei knapp' "a system prompt survives into the session" # 5. Reasoning effort des Clients kommt als opencode-Modellvariante an. opencode # trägt "reasoningEffort" nicht im Prompt, sondern in der Variante des Modells. OC_LAST="http://127.0.0.1:$OC_PORT/test/last" R=$(curl -s -m 40 -u opencode:testpasswort -X POST -H 'Content-Type: application/json' \ -d '{"model":"deepseek-v4.1-flash","messages":[{"role":"user","content":"denk nach"}],"reasoning_effort":"high"}' \ "http://127.0.0.1:$OC_JUNC/v1/chat/completions") contains "$R" '"object":"chat.completion"' "reasoning_effort request is served" R=$(curl -s -m 10 -u opencode:testpasswort "$OC_LAST") contains "$R" '"variant": "high"' "reasoning_effort becomes the opencode model variant" contains "$R" '"model": "opencode/deepseek-v4.1-flash"' "the variant does not become part of the model id" R=$(curl -s -m 40 -u opencode:testpasswort -X POST -H 'Content-Type: application/json' \ -d '{"model":"deepseek-v4.1-flash-alt","messages":[{"role":"user","content":"auch nachdenken"}],"reasoning":{"effort":"low"}}' \ "http://127.0.0.1:$OC_JUNC/v1/chat/completions") contains "$R" '"object":"chat.completion"' "reasoning.effort request is served" R=$(curl -s -m 10 -u opencode:testpasswort "$OC_LAST") contains "$R" '"variant": "low"' "reasoning.effort is passed through as a variant" R=$(curl -s -m 40 -u opencode:testpasswort -X POST -H 'Content-Type: application/json' \ -d '{"model":"deepseek-v4.1-flash-alt","messages":[{"role":"user","content":"nicht nachdenken"}],"thinking":{"type":"disabled"}}' \ "http://127.0.0.1:$OC_JUNC/v1/chat/completions") contains "$R" '"object":"chat.completion"' "a thinking:disabled request is served" R=$(curl -s -m 10 -u opencode:testpasswort "$OC_LAST") contains "$R" '"variant": "none"' "thinking:disabled becomes the none variant" R=$(curl -s -m 40 -u opencode:testpasswort -X POST -H 'Content-Type: application/json' \ -d '{"model":"deepseek-v4.1-flash#max","messages":[{"role":"user","content":"maximum"}],"reasoning_effort":"low"}' \ "http://127.0.0.1:$OC_JUNC/v1/chat/completions") contains "$R" '"object":"chat.completion"' "a model with #variant is served" R=$(curl -s -m 10 -u opencode:testpasswort "$OC_LAST") contains "$R" '"variant": "max"' "the explicit #variant wins over reasoning_effort" contains "$R" '"model": "opencode/deepseek-v4.1-flash"' "the #variant is not sent as part of the model id" # A rule's "as" replaces the model before the bridge starts; the variant from the # client's selector must not disappear with the old name. R=$(curl -s -m 40 -u opencode:testpasswort -X POST -H 'Content-Type: application/json' \ -d '{"model":"anything#high","messages":[{"role":"user","content":"durch as"}],"reasoning_effort":"low"}' \ "http://127.0.0.1:$OC_JUNC/v1/chat/completions") contains "$R" '"object":"chat.completion"' "a rewritten model with #variant is served" R=$(curl -s -m 10 -u opencode:testpasswort "$OC_LAST") contains "$R" '"variant": "high"' "the client's #variant survives a rule's as rewrite" contains "$R" '"model": "opencode/deepseek-v4.1-flash-alt"' "the as target is the model that reaches opencode" R=$(curl -s -m 40 -u opencode:testpasswort -X POST -H 'Content-Type: application/json' \ -d '{"model":"deepseek-v4.1-flash-alt","messages":[{"role":"user","content":"variant direkt"}],"variant":"medium"}' \ "http://127.0.0.1:$OC_JUNC/v1/chat/completions") contains "$R" '"object":"chat.completion"' "an explicit variant request is served" R=$(curl -s -m 10 -u opencode:testpasswort "$OC_LAST") contains "$R" '"variant": "medium"' "an explicit variant is passed through unchanged" R=$(curl -s -m 40 -u opencode:testpasswort -X POST -H 'Content-Type: application/json' \ -d '{"model":"deepseek-v4.1-flash-alt","messages":[{"role":"user","content":"ohne effort"}]}' \ "http://127.0.0.1:$OC_JUNC/v1/chat/completions") contains "$R" '"object":"chat.completion"' "a request without effort is served" R=$(curl -s -m 10 -u opencode:testpasswort "$OC_LAST") contains "$R" '"variant": null' "no effort means no variant is invented" # 6. Dieselbe Modell-Politik gilt auch für die opencode-Brücke. OC_POLICY=$((PORT+50)) cat > "$TMP/oc.policy.conf" < "$TMP/oc.policy.log" 2>&1 & PIDS+=($!) wait_port $OC_POLICY 10 R=$(curl -s -m 20 "http://127.0.0.1:$OC_POLICY/v1/models") contains "$R" '"id":"deepseek-v4.1-flash"' "the opencode list keeps the whitelisted model" case "$R" in *'"id":"deepseek-v4.1-flash-alt"'*) bad "the opencode list leaked a non-whitelisted model";; *) ok "the opencode list hides a non-whitelisted model";; esac CODE=$(curl -s -m 20 -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' \ -d '{"model":"deepseek-v4.1-flash-alt","messages":[{"role":"user","content":"nein"}]}' \ "http://127.0.0.1:$OC_POLICY/v1/chat/completions") [ "$CODE" = "403" ] && ok "the opencode path refuses a non-whitelisted model" \ || bad "the opencode path refused with HTTP $CODE" # 7. Falsches Passwort ist ein sauberer Fehler, kein Absturz printf '#!/bin/sh\necho falsch\n' > "$TMP/oc.bad.sh" chmod +x "$TMP/oc.bad.sh" sed "s|pass_cmd .*|pass_cmd \"$TMP/oc.bad.sh\"|; s|$OC_JUNC|$((PORT+20))|" "$TMP/oc.conf" > "$TMP/oc2.conf" ./junction --config "$TMP/oc2.conf" > "$TMP/oc2.log" 2>&1 & PIDS+=($!) sleep 0.8 R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \ -d '{"model":"x","messages":[{"role":"user","content":"hi"}]}' \ "http://127.0.0.1:$((PORT+20))/v1/chat/completions") contains "$R" 'no_endpoint_available' "a rejected password fails as a routing error, not a crash"