Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 66e00cb3b7 |
@@ -1,26 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
# sop-tier-check — verify a Gitea PR satisfies the §SOP-6 approval gate.
|
||||
#
|
||||
# Reads the PR's tier label, walks approving reviewers, and checks team
|
||||
# membership against the tier's approval expression. Passes only when
|
||||
# ALL clauses in the expression are satisfied by the set of approving
|
||||
# reviewers (AND-composition; internal#189).
|
||||
#
|
||||
# Expression syntax:
|
||||
# "team-a" — OR-set: any ONE of the comma-separated teams
|
||||
# "team-a AND team-b" — AND: BOTH must each have ≥1 approver
|
||||
# "(a,b,c)" — OR-set wrapped in parens; same as "a,b,c"
|
||||
#
|
||||
# Example: "qa AND security AND (managers,ceo)" means:
|
||||
# ≥1 approver in team "qa" AND
|
||||
# ≥1 approver in team "security" AND
|
||||
# ≥1 approver in team "managers" OR "ceo"
|
||||
#
|
||||
# Per the spec (internal#189), the hard gate here pairs with the
|
||||
# advisory gate of sop-conformance LLM-judge (internal#188): each
|
||||
# required-team click must reflect real verification (visible in review
|
||||
# body or A2A messages), not rubber-stamp APPROVE. Both gates together
|
||||
# close the "teammate clicks APPROVE without verifying" gap.
|
||||
# Reads the PR's tier label, walks approving reviewers, and checks each
|
||||
# approver's Gitea team membership against the tier's eligible-team set.
|
||||
# Marks pass only when at least one non-author approver is in an eligible
|
||||
# team.
|
||||
#
|
||||
# Invoked from `.gitea/workflows/sop-tier-check.yml`. The workflow sets
|
||||
# the env vars below; this script does no IO outside of stdout/stderr +
|
||||
@@ -35,12 +19,14 @@
|
||||
# PR_AUTHOR — login (from github.event.pull_request.user.login)
|
||||
#
|
||||
# Optional:
|
||||
# SOP_DEBUG=1 — print per-API-call diagnostic lines. Default: off.
|
||||
# SOP_LEGACY_CHECK=1 — revert to OR-gate (≥1 approver from any eligible
|
||||
# team). Grace window for PRs in-flight when the
|
||||
# new AND-composition was deployed. Expires 2026-05-17
|
||||
# (7-day burn-in window; internal#189 Phase 1).
|
||||
# Set by workflow for PRs merged before the deploy.
|
||||
# SOP_DEBUG=1 — print per-API-call diagnostic lines (HTTP codes,
|
||||
# raw response bodies). Default: off.
|
||||
#
|
||||
# Stale-status caveat: Gitea Actions does not always re-fire workflows
|
||||
# on `labeled` / `pull_request_review:submitted` events. If the
|
||||
# sop-tier-check status is stale (e.g. red after labels/approvals were
|
||||
# added), push an empty commit to the PR branch to force a synchronize
|
||||
# event, OR re-request reviews. Tracked: internal#46.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
@@ -91,58 +77,16 @@ if [ -z "$TIER" ]; then
|
||||
fi
|
||||
debug "tier=$TIER"
|
||||
|
||||
# 2. Tier → required team expression (AND-composition; internal#189)
|
||||
#
|
||||
# Expression syntax:
|
||||
# clause-a AND clause-b AND ... — ALL clauses must pass
|
||||
# team-a,team-b,team-c — OR-set: ≥1 approver in ANY of these teams
|
||||
# (team-a,team-b) — same as team-a,team-b (parens optional)
|
||||
#
|
||||
# This map is the single source of truth. Update it when the team structure
|
||||
# or policy changes. Teams referenced here but absent in Gitea are treated
|
||||
# as unachievable (would always fail) — operators notice the clear error
|
||||
# and create the missing team.
|
||||
#
|
||||
# Current Gitea teams: ceo, engineers, managers
|
||||
# Future teams (create before removing "???" fallback): qa, security, security-audit
|
||||
declare -A TIER_EXPR=(
|
||||
# tier:low — same as previous OR gate: any engineer, manager, or ceo.
|
||||
["tier:low"]="engineers,managers,ceo"
|
||||
# 2. Tier → eligible teams
|
||||
case "$TIER" in
|
||||
tier:low) ELIGIBLE="engineers managers ceo" ;;
|
||||
tier:medium) ELIGIBLE="managers ceo" ;;
|
||||
tier:high) ELIGIBLE="ceo" ;;
|
||||
esac
|
||||
debug "eligible_teams=$ELIGIBLE"
|
||||
|
||||
# tier:medium — AND of (managers) AND (engineers) AND (qa???,security???)
|
||||
# The qa+security clause requires both teams to exist; when not yet
|
||||
# created, the PR author is responsible for adding them before requesting
|
||||
# approval on a tier:medium PR. Ops: create qa + security Gitea teams
|
||||
# and update this map to remove the "???" markers (internal#189 follow-up).
|
||||
["tier:medium"]="managers AND engineers AND qa???,security???"
|
||||
|
||||
# tier:high — ceo only. The AND-composition adds no value for a
|
||||
# single-team gate, but the framework is wired for consistency.
|
||||
["tier:high"]="ceo"
|
||||
)
|
||||
|
||||
EXPR="${TIER_EXPR[$TIER]-}"
|
||||
if [ -z "$EXPR" ]; then
|
||||
echo "::error::No expression defined for tier $TIER in TIER_EXPR map."
|
||||
exit 1
|
||||
fi
|
||||
debug "expression=$EXPR"
|
||||
|
||||
# 3. Legacy OR-gate override (7-day burn-in grace window; internal#189 Phase 1)
|
||||
if [ "${SOP_LEGACY_CHECK:-}" = "1" ]; then
|
||||
LEGACY_ELIGIBLE=""
|
||||
case "$TIER" in
|
||||
tier:low) LEGACY_ELIGIBLE="engineers managers ceo" ;;
|
||||
tier:medium) LEGACY_ELIGIBLE="managers ceo" ;;
|
||||
tier:high) LEGACY_ELIGIBLE="ceo" ;;
|
||||
esac
|
||||
echo "::notice::SOP_LEGACY_CHECK=1 — using OR-gate ({$LEGACY_ELIGIBLE}) for this PR."
|
||||
ELIGIBLE="$LEGACY_ELIGIBLE"
|
||||
fi
|
||||
|
||||
# 4. Resolve all team names → IDs
|
||||
# /orgs/{org}/teams/{slug}/... endpoints don't exist on Gitea 1.22;
|
||||
# we use /teams/{id}.
|
||||
# Resolve team-name → team-id once. /orgs/{org}/teams/{slug}/... endpoints
|
||||
# don't exist on Gitea 1.22; we have to use /teams/{id}.
|
||||
ORG_TEAMS_FILE=$(mktemp)
|
||||
trap 'rm -f "$ORG_TEAMS_FILE"' EXIT
|
||||
HTTP_CODE=$(curl -sS -o "$ORG_TEAMS_FILE" -w '%{http_code}' -H "$AUTH" \
|
||||
@@ -153,194 +97,53 @@ if [ "${SOP_DEBUG:-}" = "1" ]; then
|
||||
head -c 300 "$ORG_TEAMS_FILE" >&2; echo >&2
|
||||
fi
|
||||
if [ "$HTTP_CODE" != "200" ]; then
|
||||
echo "::error::GET /orgs/${OWNER}/teams returned HTTP $HTTP_CODE — token likely lacks read:org scope."
|
||||
echo "::error::GET /orgs/${OWNER}/teams returned HTTP $HTTP_CODE — token likely lacks read:org scope. Add a SOP_TIER_CHECK_TOKEN secret with read:organization scope at the org level."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Collect every team name that appears in the expression.
|
||||
# Bash word-splitting on $EXPR splits on spaces, so "AND" appears as a
|
||||
# token. We skip it explicitly.
|
||||
declare -A TEAM_ID
|
||||
_all_teams=""
|
||||
for _raw_clause in $EXPR; do
|
||||
# Strip parens and split on comma.
|
||||
_clause=${_raw_clause//[()]/}
|
||||
for _t in $(echo "$_clause" | tr ',' '\n'); do
|
||||
_t=$(echo "$_t" | tr -d '[:space:]')
|
||||
[ -z "$_t" ] && continue
|
||||
# Skip AND / OR operator tokens (bash word-split produced them from
|
||||
# spaces in the expression string).
|
||||
[ "$_t" = "AND" ] || [ "$_t" = "OR" ] && continue
|
||||
# Skip if already in set.
|
||||
case " $_all_teams " in
|
||||
*" $_t "*) ;; # already present
|
||||
*) _all_teams="${_all_teams} $_t " ;;
|
||||
esac
|
||||
done
|
||||
done
|
||||
|
||||
for _t in $_all_teams; do
|
||||
_t=$(echo "$_t" | tr -d ' ')
|
||||
[ -z "$_t" ] && continue
|
||||
_id=$(jq -r --arg t "$_t" '.[] | select(.name==$t) | .id' <"$ORG_TEAMS_FILE" | head -1)
|
||||
if [ -z "$_id" ] || [ "$_id" = "null" ]; then
|
||||
# "??" suffix marks teams that don't exist yet (tier:medium qa/security).
|
||||
# Treat as permanently failing clause; clear error message guides ops.
|
||||
if [[ "$_t" == *"???" ]]; then
|
||||
debug "team \"$_t\" not found (expected — pending team creation per internal#189)"
|
||||
continue
|
||||
fi
|
||||
_visible=$(jq -r '.[]?.name? // empty' <"$ORG_TEAMS_FILE" 2>/dev/null | tr '\n' ' ')
|
||||
echo "::error::Team \"$_t\" referenced in tier $TIER expression but not found in org $OWNER. Teams visible: $_visible"
|
||||
for T in $ELIGIBLE; do
|
||||
ID=$(jq -r --arg t "$T" '.[] | select(.name==$t) | .id' <"$ORG_TEAMS_FILE" | head -1)
|
||||
if [ -z "$ID" ] || [ "$ID" = "null" ]; then
|
||||
VISIBLE=$(jq -r '.[]?.name? // empty' <"$ORG_TEAMS_FILE" 2>/dev/null | tr '\n' ' ')
|
||||
echo "::error::Team \"$T\" not found in org $OWNER. Teams visible: $VISIBLE"
|
||||
exit 1
|
||||
fi
|
||||
TEAM_ID[$_t]="$_id"
|
||||
debug "team-id: $_t → $_id"
|
||||
TEAM_ID[$T]="$ID"
|
||||
debug "team-id: $T → $ID"
|
||||
done
|
||||
|
||||
# 5. Read approving reviewers
|
||||
# 3. Read approving reviewers
|
||||
REVIEWS=$(curl -sS -H "$AUTH" "${API}/repos/${OWNER}/${NAME}/pulls/${PR_NUMBER}/reviews")
|
||||
APPROVERS=$(echo "$REVIEWS" | jq -r '[.[] | select(.state=="APPROVED") | .user.login] | unique | .[]')
|
||||
if [ -z "$APPROVERS" ]; then
|
||||
echo "::error::No approving reviews on this PR. Set SOP_DEBUG=1 and re-run for diagnostics."
|
||||
echo "::error::No approving reviews. Tier $TIER requires approval from {$ELIGIBLE} (non-author)."
|
||||
exit 1
|
||||
fi
|
||||
debug "approvers: $(echo "$APPROVERS" | tr '\n' ' ')"
|
||||
|
||||
# 6. For each approver: skip self-review; probe team membership by id.
|
||||
# Build $APPROVER_TEAMS[<user>]=space-surrounded team names (e.g. " managers ").
|
||||
# Pre/post spaces ensure case patterns *${_t}* match even when the name
|
||||
# is the first or last entry (bash case *word* needs delimiters on both sides).
|
||||
#
|
||||
# FALLBACK: if ALL team probes return 403 (token lacks read:org scope),
|
||||
# fall back to /orgs/{org}/members/{user}. This returns 204 for any org
|
||||
# member — a superset of team membership. Accepting it as a fallback means
|
||||
# the gate passes when the token is scoped to repo+user only (core-bot PAT).
|
||||
# This is safe because: (a) org membership is a prerequisite for every
|
||||
# eligible team; (b) the AND-composition of internal#189 still requires
|
||||
# multiple independent approvers; (c) any token with read:repository can
|
||||
# see the approving reviews, so bypass requires a colluding approver.
|
||||
declare -A APPROVER_TEAMS
|
||||
# 4. For each approver: check non-author + team membership (by id)
|
||||
OK=""
|
||||
for U in $APPROVERS; do
|
||||
[ "$U" = "$PR_AUTHOR" ] && debug "skip self-review by $U" && continue
|
||||
_any_team_success="no"
|
||||
for T in "${!TEAM_ID[@]}"; do
|
||||
if [ "$U" = "$PR_AUTHOR" ]; then
|
||||
debug "skip self-review by $U"
|
||||
continue
|
||||
fi
|
||||
for T in $ELIGIBLE; do
|
||||
ID="${TEAM_ID[$T]}"
|
||||
CODE=$(curl -sS -o /dev/null -w '%{http_code}' -H "$AUTH" \
|
||||
"${API}/teams/${ID}/members/${U}")
|
||||
debug "probe: $U in team $T (id=$ID) → HTTP $CODE"
|
||||
if [ "$CODE" = "200" ] || [ "$CODE" = "204" ]; then
|
||||
APPROVER_TEAMS[$U]="${APPROVER_TEAMS[$U]:- } ${APPROVER_TEAMS[$U]:+ }$T "
|
||||
debug "$U qualifies for team $T"
|
||||
_any_team_success="yes"
|
||||
echo "::notice::approver $U is in team $T (eligible for $TIER)"
|
||||
OK="yes"
|
||||
break
|
||||
fi
|
||||
done
|
||||
# Fallback: if every team probe returned 403, try org membership.
|
||||
# "??" teams were never resolved to IDs so they never entered the loop.
|
||||
# If the user is an org member, credit them as being in each queried team
|
||||
# (engineers, managers, ceo are all org-level). This is safe because org
|
||||
# membership is a prerequisite for all three, and bypass requires a colluding
|
||||
# approver (same risk as before the AND-composition).
|
||||
if [ "$_any_team_success" = "no" ]; then
|
||||
ORG_CODE=$(curl -sS -o /dev/null -w '%{http_code}' -H "$AUTH" \
|
||||
"${API}/orgs/${OWNER}/members/${U}")
|
||||
debug "probe: $U in org $OWNER (fallback) → HTTP $ORG_CODE"
|
||||
if [ "$ORG_CODE" = "204" ]; then
|
||||
for T in "${!TEAM_ID[@]}"; do
|
||||
APPROVER_TEAMS[$U]="${APPROVER_TEAMS[$U]:- } ${APPROVER_TEAMS[$U]:+ }$T "
|
||||
done
|
||||
debug "$U credited as org member for all queried teams (fallback — token may lack read:org)"
|
||||
fi
|
||||
fi
|
||||
[ -n "$OK" ] && break
|
||||
done
|
||||
|
||||
# 7. Evaluate the tier expression.
|
||||
#
|
||||
# legacy OR-gate: use the simplified loop from before internal#189.
|
||||
if [ -n "${LEGACY_ELIGIBLE:-}" ]; then
|
||||
OK=""
|
||||
for _u in "${!APPROVER_TEAMS[@]}"; do
|
||||
for _t2 in $LEGACY_ELIGIBLE; do
|
||||
case "${APPROVER_TEAMS[$_u]}" in
|
||||
*${_t2}*)
|
||||
echo "::notice::approver $_u is in team $_t2 (eligible for $TIER)"
|
||||
OK="yes"
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
[ -n "$OK" ] && break
|
||||
done
|
||||
if [ -z "$OK" ]; then
|
||||
echo "::error::Tier $TIER requires approval from a non-author member of {$LEGACY_ELIGIBLE}. Set SOP_DEBUG=1 to see per-probe HTTP codes."
|
||||
exit 1
|
||||
fi
|
||||
echo "::notice::sop-tier-check passed: $TIER (legacy OR-gate)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# AND-gate: evaluate the expression clause by clause.
|
||||
# _passed_clauses and _failed_clauses accumulate for the status description.
|
||||
_passed_clauses=""
|
||||
_failed_clauses=""
|
||||
|
||||
for _raw_clause in $EXPR; do
|
||||
# Normalise: strip parens, replace commas with spaces so bash word-split
|
||||
# can iterate the OR-set members. The previous form
|
||||
# _clause=$(echo ... | tr ',' '\n' | tr -d '[:space:]' | grep -v '^$')
|
||||
# collapsed every member into one concatenated token because
|
||||
# `tr -d '[:space:]'` strips the very newlines that just separated them
|
||||
# ("engineers,managers,ceo" -> "engineersmanagersceo"), so the OR-clause
|
||||
# only ever evaluated as a single nonsense team name and never matched
|
||||
# APPROVER_TEAMS. Fixed in #229: leave the comma-separated members as
|
||||
# space-separated tokens for `for _t in $_clause`.
|
||||
_no_parens=${_raw_clause//[()]/}
|
||||
_clause=${_no_parens//,/ }
|
||||
_clause_passed="no"
|
||||
_clause_names=""
|
||||
for _t in $_clause; do
|
||||
# Append (don't overwrite) team name to the human-readable accumulator.
|
||||
# The previous form `_clause_names="${_clause_names:+, }${_t}"`
|
||||
# rewrote the variable on every iteration, so the FAIL message only
|
||||
# ever showed the LAST team. Fixed: prepend prior value before the
|
||||
# comma-separator, then append the new team name.
|
||||
_clause_names="${_clause_names}${_clause_names:+, }${_t}"
|
||||
# Skip teams not yet in Gitea (qa??? / security??? placeholders).
|
||||
[[ "$_t" == *"???" ]] && debug "clause \"$_t\": skipped (team pending creation)" && continue
|
||||
[ -z "${TEAM_ID[$_t]:-}" ] && debug "clause \"$_t\": no ID resolved, skipping" && continue
|
||||
for _u in "${!APPROVER_TEAMS[@]}"; do
|
||||
# Note: APPROVER_TEAMS values are space-surrounded (e.g. " managers ").
|
||||
# Pattern *${_t}* matches team name anywhere in the space-padded string.
|
||||
case "${APPROVER_TEAMS[$_u]}" in
|
||||
*${_t}*)
|
||||
_clause_passed="yes"
|
||||
debug "clause \"$_t\": satisfied by $_u"
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
done
|
||||
|
||||
# Label for display: strip "???" from pending teams.
|
||||
_label=$(echo "$_raw_clause" | tr -d '()' | tr ',' '/' | tr -d '[:space:]' | sed 's/???//g')
|
||||
|
||||
if [ "$_clause_passed" = "yes" ]; then
|
||||
# Append (don't overwrite) — same accumulator bug as _clause_names above.
|
||||
_passed_clauses="${_passed_clauses}${_passed_clauses:+, }$_label"
|
||||
echo "::notice::clause [$_label]: PASS — satisfied by approving reviewer(s)"
|
||||
else
|
||||
_failed_clauses="${_failed_clauses}${_failed_clauses:+, }$_label"
|
||||
echo "::error::clause [$_label]: FAIL — no approving reviewer belongs to any of these teams (${_clause_names}). Set SOP_DEBUG=1 to see per-team probe results."
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -n "$_failed_clauses" ]; then
|
||||
echo ""
|
||||
echo "::error::sop-tier-check FAILED for $TIER."
|
||||
echo " Passed :${_passed_clauses}"
|
||||
echo " Missing:${_failed_clauses}"
|
||||
echo " All clauses must be satisfied. Each missing team needs an APPROVED review from one of its members."
|
||||
if [ -z "$OK" ]; then
|
||||
echo "::error::Tier $TIER requires approval from a non-author member of {$ELIGIBLE}. Got approvers: $APPROVERS — none of them satisfied team membership. Set SOP_DEBUG=1 to see per-probe HTTP codes."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "::notice::sop-tier-check PASSED: $TIER — all required clauses satisfied [${_passed_clauses}]"
|
||||
echo "::notice::sop-tier-check passed: $TIER, approver in {$ELIGIBLE}"
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Regression test for #229 — sop-tier-check tier:low OR-clause splitter.
|
||||
#
|
||||
# Bug (PR #225 → still broken after PR #231):
|
||||
# Line ~289 of sop-tier-check.sh used:
|
||||
# _clause=$(echo "$_raw_clause" | tr -d '()' | tr ',' '\n' | tr -d '[:space:]' | grep -v '^$')
|
||||
# `tr -d '[:space:]'` strips the newlines that `tr ',' '\n'` just
|
||||
# inserted, collapsing "engineers,managers,ceo" into a single token
|
||||
# "engineersmanagersceo". The for-loop then iterates ONCE on a name
|
||||
# that matches no team, so every tier:low PR fails:
|
||||
# ::error::clause [engineers/managers/ceo]: FAIL — no approving
|
||||
# reviewer belongs to any of these teamsengineersmanagersceo
|
||||
# (note also: missing separators in the error string is bug #2 —
|
||||
# `_clause_names` used "${var:+, }$x" which OVERWRITES per iteration).
|
||||
#
|
||||
# Fix shape (this PR):
|
||||
# _no_parens=${_raw_clause//[()]/}
|
||||
# _clause=${_no_parens//,/ } # comma -> space, bash word-split iterates
|
||||
# _clause_names="${_clause_names}${_clause_names:+, }${_t}" # APPEND, not overwrite
|
||||
#
|
||||
# This test extracts the splitter logic and asserts it produces the right
|
||||
# token list for each of the three tier expressions live in the script.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
assert_eq() {
|
||||
local label="$1"
|
||||
local expected="$2"
|
||||
local got="$3"
|
||||
if [ "$expected" = "$got" ]; then
|
||||
echo " PASS $label"
|
||||
PASS=$((PASS + 1))
|
||||
else
|
||||
echo " FAIL $label"
|
||||
echo " expected: <$expected>"
|
||||
echo " got: <$got>"
|
||||
FAIL=$((FAIL + 1))
|
||||
fi
|
||||
}
|
||||
|
||||
# ----- Splitter under test (mirrors the fixed sop-tier-check.sh block) -----
|
||||
split_clause() {
|
||||
local raw="$1"
|
||||
local no_parens=${raw//[()]/}
|
||||
local clause=${no_parens//,/ }
|
||||
local out=""
|
||||
for _t in $clause; do
|
||||
out="${out}${out:+|}$_t"
|
||||
done
|
||||
echo "$out"
|
||||
}
|
||||
|
||||
echo "test: tier:low OR-clause splits to 3 tokens"
|
||||
assert_eq "tier:low" "engineers|managers|ceo" "$(split_clause "engineers,managers,ceo")"
|
||||
|
||||
echo "test: tier:medium AND-expression — bash word-split on \$EXPR yields 5 tokens"
|
||||
EXPR="managers AND engineers AND qa???,security???"
|
||||
out=""
|
||||
for _raw in $EXPR; do
|
||||
out="${out}${out:+ ; }$(split_clause "$_raw")"
|
||||
done
|
||||
assert_eq "tier:medium" "managers ; AND ; engineers ; AND ; qa???|security???" "$out"
|
||||
|
||||
echo "test: tier:high single-team OR-clause"
|
||||
assert_eq "tier:high" "ceo" "$(split_clause "ceo")"
|
||||
|
||||
echo "test: paren-wrapped OR-set unwraps + splits"
|
||||
assert_eq "paren OR" "managers|ceo" "$(split_clause "(managers,ceo)")"
|
||||
|
||||
# ----- _clause_names accumulator (was overwriting per iteration) -----
|
||||
acc=""
|
||||
for t in engineers managers ceo; do
|
||||
acc="${acc}${acc:+, }${t}"
|
||||
done
|
||||
assert_eq "_clause_names append" "engineers, managers, ceo" "$acc"
|
||||
|
||||
# ----- _failed_clauses / _passed_clauses accumulator across raw clauses -----
|
||||
acc=""
|
||||
for c in clauseA clauseB clauseC; do
|
||||
acc="${acc}${acc:+, }${c}"
|
||||
done
|
||||
assert_eq "_failed_clauses append" "clauseA, clauseB, clauseC" "$acc"
|
||||
|
||||
# ----- End-to-end OR-gate: simulate APPROVER_TEAMS[core-lead]=' managers ' -----
|
||||
# The script's case pattern is *${_t}* with a space-padded value.
|
||||
APPROVER_TEAMS_VAL=" managers "
|
||||
matched=""
|
||||
for _t in $(split_clause "engineers,managers,ceo" | tr '|' ' '); do
|
||||
case "$APPROVER_TEAMS_VAL" in
|
||||
*${_t}*) matched="$_t"; break ;;
|
||||
esac
|
||||
done
|
||||
assert_eq "OR-gate matches managers" "managers" "$matched"
|
||||
|
||||
echo
|
||||
echo "------"
|
||||
echo "PASS=$PASS FAIL=$FAIL"
|
||||
[ "$FAIL" -eq 0 ]
|
||||
@@ -1,172 +0,0 @@
|
||||
name: publish-workspace-server-image
|
||||
|
||||
# Gitea Actions port of .github/workflows/publish-workspace-server-image.yml.
|
||||
#
|
||||
# Ported 2026-05-10 (issue #228). Key differences from the GitHub version:
|
||||
# - Gitea Actions reads .gitea/workflows/, not .github/workflows/
|
||||
# - Dropped `environment:` declarations — Gitea Actions does not support
|
||||
# named environments (used by GitHub OIDC token gates)
|
||||
# - Replaced `github.ref_name` (GitHub-only) with `${GITHUB_REF#refs/heads/}`
|
||||
# — Gitea Actions exposes GITHUB_REF in the same format as GitHub Actions
|
||||
# - docker/setup-buildx-action and aws-actions/configure-aws-credentials are
|
||||
# GitHub Marketplace actions; they are installed by Gitea Actions runners and
|
||||
# work identically here
|
||||
# - All other variables (GITHUB_SHA, GITHUB_REPOSITORY, GITHUB_OUTPUT,
|
||||
# secrets.*) use the same syntax as GitHub Actions
|
||||
#
|
||||
# Image tags produced:
|
||||
# :staging-<sha> — per-commit digest, stable for canary verify
|
||||
# :staging-latest — tracks most recent build on this branch
|
||||
#
|
||||
# ECR target: 153263036946.dkr.ecr.us-east-2.amazonaws.com/molecule-ai/*
|
||||
# Required secrets: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AUTO_SYNC_TOKEN
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
paths:
|
||||
- 'workspace-server/**'
|
||||
- 'canvas/**'
|
||||
- 'manifest.json'
|
||||
- 'scripts/**'
|
||||
- '.gitea/workflows/publish-workspace-server-image.yml'
|
||||
workflow_dispatch:
|
||||
|
||||
# Serialize per-branch so two rapid main pushes don't race the same
|
||||
# :staging-latest tag retag. Allow parallel runs as they produce
|
||||
# different :staging-<sha> tags and last-write-wins on :staging-latest.
|
||||
#
|
||||
# cancel-in-progress: false → in-flight builds finish; the next push's
|
||||
# build queues. This avoids a partially-pushed image.
|
||||
concurrency:
|
||||
group: publish-workspace-server-image-${{ github.ref }}
|
||||
cancel-in-progress: false
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
env:
|
||||
IMAGE_NAME: 153263036946.dkr.ecr.us-east-2.amazonaws.com/molecule-ai/platform
|
||||
TENANT_IMAGE_NAME: 153263036946.dkr.ecr.us-east-2.amazonaws.com/molecule-ai/platform-tenant
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
|
||||
# Health check: verify Docker daemon is accessible before attempting any
|
||||
# build steps. This fails loudly at step 1 when the runner's docker.sock
|
||||
# is inaccessible (e.g. permission change, daemon restart, or group-membership
|
||||
# drift) rather than silently continuing to step 2 where `docker build`
|
||||
# fails deep in the process with a cryptic ECR auth error that doesn't
|
||||
# surface the root cause. Also reports the daemon version so operator
|
||||
# can correlate with runner host logs.
|
||||
- name: Verify Docker daemon access
|
||||
run: |
|
||||
set -euo pipefail
|
||||
echo "::group::Docker daemon health check"
|
||||
docker info 2>&1 | head -5 || {
|
||||
echo "::error::Docker daemon is not accessible at /var/run/docker.sock"
|
||||
echo "::error::Check: (1) daemon is running, (2) runner user is in docker group, (3) sock permissions are 660+"
|
||||
exit 1
|
||||
}
|
||||
echo "Docker daemon OK"
|
||||
echo "::endgroup::"
|
||||
|
||||
# Pre-clone manifest deps before docker build.
|
||||
#
|
||||
# Why: workspace-template-* repos on Gitea are private. The pre-fix
|
||||
# Dockerfile.tenant ran `git clone` inside an in-image stage with no
|
||||
# auth path — every CI build failed. We clone in the trusted CI
|
||||
# context where AUTO_SYNC_TOKEN is available and Dockerfile.tenant
|
||||
# just COPYs from .tenant-bundle-deps/.
|
||||
#
|
||||
# Token: AUTO_SYNC_TOKEN is the devops-engineer persona PAT.
|
||||
# clone-manifest.sh embeds it as basic-auth for the clones, then
|
||||
# strips .git dirs — the token never enters the image.
|
||||
- name: Pre-clone manifest deps
|
||||
env:
|
||||
MOLECULE_GITEA_TOKEN: ${{ secrets.AUTO_SYNC_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if [ -z "${MOLECULE_GITEA_TOKEN}" ]; then
|
||||
echo "::error::AUTO_SYNC_TOKEN secret is empty"
|
||||
exit 1
|
||||
fi
|
||||
mkdir -p .tenant-bundle-deps
|
||||
bash scripts/clone-manifest.sh \
|
||||
manifest.json \
|
||||
.tenant-bundle-deps/workspace-configs-templates \
|
||||
.tenant-bundle-deps/org-templates \
|
||||
.tenant-bundle-deps/plugins
|
||||
ws_count=$(find .tenant-bundle-deps/workspace-configs-templates -mindepth 1 -maxdepth 1 -type d | wc -l)
|
||||
org_count=$(find .tenant-bundle-deps/org-templates -mindepth 1 -maxdepth 1 -type d | wc -l)
|
||||
plugins_count=$(find .tenant-bundle-deps/plugins -mindepth 1 -maxdepth 1 -type d | wc -l)
|
||||
echo "Cloned: ws=$ws_count org=$org_count plugins=$plugins_count"
|
||||
|
||||
- name: Compute tags
|
||||
id: tags
|
||||
run: |
|
||||
echo "sha=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
|
||||
|
||||
# Build + push platform image (inline ECR auth — mirrors the operator-host
|
||||
# approach; credentials come from GITHUB_SECRET_AWS_ACCESS_KEY_ID /
|
||||
# GITHUB_SECRET_AWS_SECRET_ACCESS_KEY in Gitea Actions).
|
||||
- name: Build & push platform image to ECR (staging-<sha> + staging-latest)
|
||||
env:
|
||||
IMAGE_NAME: ${{ env.IMAGE_NAME }}
|
||||
TAG_SHA: staging-${{ steps.tags.outputs.sha }}
|
||||
TAG_LATEST: staging-latest
|
||||
GIT_SHA: ${{ github.sha }}
|
||||
REPO: ${{ github.repository }}
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||
AWS_DEFAULT_REGION: us-east-2
|
||||
run: |
|
||||
set -euo pipefail
|
||||
ECR_REGISTRY="${IMAGE_NAME%%/*}"
|
||||
aws ecr get-login-password --region us-east-2 | \
|
||||
docker login --username AWS --password-stdin "${ECR_REGISTRY}"
|
||||
docker build \
|
||||
--file ./workspace-server/Dockerfile \
|
||||
--build-arg GIT_SHA="${GIT_SHA}" \
|
||||
--label "org.opencontainers.image.source=https://github.com/${REPO}" \
|
||||
--label "org.opencontainers.image.revision=${GIT_SHA}" \
|
||||
--label "org.opencontainers.image.description=Molecule AI platform — pending canary verify" \
|
||||
--tag "${IMAGE_NAME}:${TAG_SHA}" \
|
||||
--tag "${IMAGE_NAME}:${TAG_LATEST}" \
|
||||
.
|
||||
docker push "${IMAGE_NAME}:${TAG_SHA}"
|
||||
docker push "${IMAGE_NAME}:${TAG_LATEST}"
|
||||
|
||||
# Build + push tenant image (Go platform + Next.js canvas in one image).
|
||||
- name: Build & push tenant image to ECR (staging-<sha> + staging-latest)
|
||||
env:
|
||||
TENANT_IMAGE_NAME: ${{ env.TENANT_IMAGE_NAME }}
|
||||
TAG_SHA: staging-${{ steps.tags.outputs.sha }}
|
||||
TAG_LATEST: staging-latest
|
||||
GIT_SHA: ${{ github.sha }}
|
||||
REPO: ${{ github.repository }}
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||
AWS_DEFAULT_REGION: us-east-2
|
||||
run: |
|
||||
set -euo pipefail
|
||||
ECR_REGISTRY="${TENANT_IMAGE_NAME%%/*}"
|
||||
aws ecr get-login-password --region us-east-2 | \
|
||||
docker login --username AWS --password-stdin "${ECR_REGISTRY}"
|
||||
docker build \
|
||||
--file ./workspace-server/Dockerfile.tenant \
|
||||
--build-arg NEXT_PUBLIC_PLATFORM_URL= \
|
||||
--build-arg GIT_SHA="${GIT_SHA}" \
|
||||
--label "org.opencontainers.image.source=https://github.com/${REPO}" \
|
||||
--label "org.opencontainers.image.revision=${GIT_SHA}" \
|
||||
--label "org.opencontainers.image.description=Molecule AI tenant platform + canvas — pending canary verify" \
|
||||
--tag "${TENANT_IMAGE_NAME}:${TAG_SHA}" \
|
||||
--tag "${TENANT_IMAGE_NAME}:${TAG_LATEST}" \
|
||||
.
|
||||
docker push "${TENANT_IMAGE_NAME}:${TAG_SHA}"
|
||||
docker push "${TENANT_IMAGE_NAME}:${TAG_LATEST}"
|
||||
@@ -12,31 +12,18 @@
|
||||
# required_approving_reviews: 1
|
||||
# approving_review_teams: ["ceo", "managers", "engineers"]
|
||||
#
|
||||
# Tier → required-team expression (internal#189 AND-composition):
|
||||
# tier:low → engineers,managers,ceo (OR: any one suffices)
|
||||
# tier:medium → managers AND engineers AND qa???,security??? (AND: all required)
|
||||
# tier:high → ceo (OR: single team, wired for AND)
|
||||
#
|
||||
# "???" = teams not yet created in Gitea. When qa + security teams are
|
||||
# added, update TIER_EXPR["tier:medium"] in the script to remove the
|
||||
# markers. PRs already in-flight when qa/security are created continue
|
||||
# to work because their authors explicitly requested those reviews.
|
||||
# Tier → eligible-team mapping (mirror of dev-sop §SOP-6):
|
||||
# tier:low → engineers, managers, ceo
|
||||
# tier:medium → managers, ceo
|
||||
# tier:high → ceo
|
||||
#
|
||||
# Force-merge: Owners-team override remains available out-of-band via
|
||||
# the Gitea merge API; force-merge writes `incident.force_merge` to
|
||||
# `structure_events` per §Persistent structured logging gate (Phase 3).
|
||||
#
|
||||
# Environment variables:
|
||||
# SOP_DEBUG=1 — per-API-call diagnostic lines. Default: off.
|
||||
# SOP_LEGACY_CHECK=1 — revert to OR-gate for this run. Grace window
|
||||
# for PRs in-flight when AND-composition deployed.
|
||||
# Burn-in: remove after 2026-05-17 (7-day window).
|
||||
#
|
||||
# BURN-IN NOTE (internal#189 Phase 1): continue-on-error: true is set on
|
||||
# the tier-check job below. This prevents AND-composition from blocking
|
||||
# PRs during the 7-day burn-in. After 2026-05-17:
|
||||
# 1. Remove `continue-on-error: true` from this job block.
|
||||
# 2. Update this BURN-IN NOTE comment to mark the window closed.
|
||||
# Set `SOP_DEBUG: '1'` in the env block to enable per-API-call diagnostic
|
||||
# lines — useful when diagnosing token-scope or team-id-resolution
|
||||
# issues. Default off.
|
||||
|
||||
name: sop-tier-check
|
||||
|
||||
@@ -63,9 +50,6 @@ on:
|
||||
jobs:
|
||||
tier-check:
|
||||
runs-on: ubuntu-latest
|
||||
# BURN-IN: continue-on-error prevents AND-composition from blocking
|
||||
# PRs during the 7-day window. Remove after 2026-05-17 (internal#189).
|
||||
continue-on-error: true
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: read
|
||||
@@ -77,13 +61,6 @@ jobs:
|
||||
# works if we never check out PR HEAD. Same SHA the workflow
|
||||
# itself was loaded from.
|
||||
ref: ${{ github.event.pull_request.base.sha }}
|
||||
- name: Install jq
|
||||
# Gitea Actions runners (ubuntu-latest label) do not bundle jq.
|
||||
# The script uses jq extensively for all JSON parsing; install it
|
||||
# before the script runs. Using -qq for quiet output — diagnostic
|
||||
# info is already captured via SOP_DEBUG=1 on failure.
|
||||
run: apt-get update -qq && apt-get install -y -qq jq
|
||||
|
||||
- name: Verify tier label + reviewer team membership
|
||||
env:
|
||||
# SOP_TIER_CHECK_TOKEN is the org-level secret for the
|
||||
@@ -101,7 +78,4 @@ jobs:
|
||||
# Set to '1' for diagnostic per-API-call output. Off by default
|
||||
# so production logs aren't noisy.
|
||||
SOP_DEBUG: '0'
|
||||
# BURN-IN: set to '1' for PRs in-flight at AND-composition deploy
|
||||
# time to use the legacy OR-gate. Remove after 2026-05-17.
|
||||
SOP_LEGACY_CHECK: '0'
|
||||
run: bash .gitea/scripts/sop-tier-check.sh
|
||||
|
||||
@@ -1,34 +1,19 @@
|
||||
name: canary-verify
|
||||
|
||||
# Runs the canary smoke suite against the staging canary tenant fleet
|
||||
# after a new :staging-<sha> image lands in ECR. On green, calls the
|
||||
# CP redeploy-fleet endpoint to promote :staging-<sha> → :latest so
|
||||
# the prod tenant fleet's 5-minute auto-updater picks up the verified
|
||||
# digest. On red, :latest stays on the prior known-good digest and
|
||||
# prod is untouched.
|
||||
#
|
||||
# Registry note (2026-05-10): This workflow previously used GHCR
|
||||
# (ghcr.io/molecule-ai/platform-tenant) — that registry was retired
|
||||
# during the 2026-05-06 Gitea suspension migration when publish-
|
||||
# workspace-server-image.yml switched to the operator's ECR org
|
||||
# (153263036946.dkr.ecr.us-east-2.amazonaws.com/molecule-ai/
|
||||
# platform-tenant). The GHCR → ECR migration was never applied to
|
||||
# this file, so canary-verify was silently smoke-testing the stale
|
||||
# GHCR image while the actual staging/prod tenants ran the ECR image.
|
||||
# Result: smoke tests could not catch a broken ECR build. Fix:
|
||||
# - Wait step: reads SHA from running canary /health (tenant-
|
||||
# agnostic, works regardless of registry).
|
||||
# - Promote step: calls CP redeploy-fleet endpoint with target_tag=
|
||||
# staging-<sha>, same mechanism as redeploy-tenants-on-main.yml.
|
||||
# No longer attempts GHCR crane ops.
|
||||
# after a new :staging-<sha> image lands in GHCR. On green, promotes
|
||||
# :staging-<sha> → :latest so the prod tenant fleet's 5-minute
|
||||
# auto-updater picks up the verified digest. On red, :latest stays
|
||||
# on the prior known-good digest and prod is untouched.
|
||||
#
|
||||
# Dependencies:
|
||||
# - publish-workspace-server-image.yml publishes :staging-<sha>
|
||||
# to ECR on staging and main merges.
|
||||
# - Canary tenants are configured to pull :staging-<sha> from ECR
|
||||
# (TENANT_IMAGE env set to the ECR :staging-<sha> tag).
|
||||
# (NOT :latest) on main merge
|
||||
# - canary tenants are configured to pull :staging-<sha> as their
|
||||
# tenant image (set TENANT_IMAGE=ghcr.io/…:staging-<sha> on the
|
||||
# canary provisioner code path OR rotate via an admin endpoint)
|
||||
# - Repo secrets CANARY_TENANT_URLS / CANARY_ADMIN_TOKENS /
|
||||
# CANARY_CP_SHARED_SECRET are populated.
|
||||
# CANARY_CP_SHARED_SECRET are populated
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
@@ -42,12 +27,8 @@ permissions:
|
||||
actions: read
|
||||
|
||||
env:
|
||||
# ECR registry (post-2026-05-06 SSOT for tenant images).
|
||||
# publish-workspace-server-image.yml pushes here.
|
||||
IMAGE_NAME: 153263036946.dkr.ecr.us-east-2.amazonaws.com/molecule-ai/platform
|
||||
TENANT_IMAGE_NAME: 153263036946.dkr.ecr.us-east-2.amazonaws.com/molecule-ai/platform-tenant
|
||||
# CP endpoint for redeploy-fleet (used in promote step below).
|
||||
CP_URL: ${{ vars.CP_URL || 'https://staging-api.moleculesai.app' }}
|
||||
IMAGE_NAME: ghcr.io/molecule-ai/platform
|
||||
TENANT_IMAGE_NAME: ghcr.io/molecule-ai/platform-tenant
|
||||
|
||||
jobs:
|
||||
canary-smoke:
|
||||
@@ -71,12 +52,6 @@ jobs:
|
||||
# the new SHA (~2-3 min typical vs 6 min fixed). Falls back to
|
||||
# proceeding after 7 min even if not all canaries responded —
|
||||
# the smoke suite will catch any that didn't update.
|
||||
#
|
||||
# NOTE: The SHA is read from the running tenant's /health response,
|
||||
# NOT from a registry lookup. This is registry-agnostic and works
|
||||
# regardless of whether the tenant pulls from ECR, GHCR, or any
|
||||
# other registry — the canary is telling us what it's actually
|
||||
# running, which is the ground truth for smoke testing.
|
||||
env:
|
||||
CANARY_TENANT_URLS: ${{ secrets.CANARY_TENANT_URLS }}
|
||||
EXPECTED_SHA: ${{ steps.compute.outputs.sha }}
|
||||
@@ -158,98 +133,42 @@ jobs:
|
||||
} >> "$GITHUB_STEP_SUMMARY"
|
||||
|
||||
promote-to-latest:
|
||||
# On green, calls the CP redeploy-fleet endpoint with target_tag=
|
||||
# staging-<sha> to promote the verified ECR image. This is the same
|
||||
# mechanism as redeploy-tenants-on-main.yml — no GHCR crane ops.
|
||||
#
|
||||
# Pre-fix history: the old GHCR promote step used `crane tag` against
|
||||
# ghcr.io/molecule-ai/platform-tenant, but publish-workspace-server-
|
||||
# image.yml had already migrated to ECR on 2026-05-07 (commit
|
||||
# 10e510f5). The GHCR tags were never updated, so this step was
|
||||
# silently promoting a stale GHCR image while actual prod tenants
|
||||
# pulled from ECR. Canary smoke tests were GHCR-targeted and could
|
||||
# not catch a broken ECR build.
|
||||
# On green, retag :staging-<sha> → :latest for BOTH images.
|
||||
# crane is a lightweight registry client (no Docker daemon needed on
|
||||
# the runner) that can retag remotely with a single API call each.
|
||||
# Gated on smoke_ran=true — without a real canary fleet the smoke
|
||||
# step no-ops with success, and we don't want that to silently
|
||||
# auto-promote every main merge.
|
||||
needs: canary-smoke
|
||||
if: ${{ needs.canary-smoke.result == 'success' && needs.canary-smoke.outputs.smoke_ran == 'true' }}
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
SHA: ${{ needs.canary-smoke.outputs.sha }}
|
||||
CP_URL: ${{ vars.CP_URL || 'https://staging-api.moleculesai.app' }}
|
||||
# CP_ADMIN_API_TOKEN gates write access to the redeploy endpoint.
|
||||
# Stored at the repo level so all workflows pick it up automatically.
|
||||
CP_ADMIN_API_TOKEN: ${{ secrets.CP_ADMIN_API_TOKEN }}
|
||||
# canary_slug pin: deploy the verified :staging-<sha> to the canary
|
||||
# first (soak 120s), then fan out to the rest of the fleet.
|
||||
CANARY_SLUG: ${{ vars.CANARY_PROMOTE_SLUG || '' }}
|
||||
SOAK_SECONDS: ${{ vars.CANARY_PROMOTE_SOAK || '120' }}
|
||||
BATCH_SIZE: ${{ vars.CANARY_PROMOTE_BATCH || '3' }}
|
||||
steps:
|
||||
- name: Check CP credentials
|
||||
- uses: imjasonh/setup-crane@6da1ae018866400525525ce74ff892880c099987 # v0.5
|
||||
|
||||
- name: GHCR login
|
||||
run: |
|
||||
if [ -z "${CP_ADMIN_API_TOKEN:-}" ]; then
|
||||
echo "::error::CP_ADMIN_API_TOKEN secret is not set — promote step cannot call redeploy-fleet."
|
||||
echo "::error::Set it at: repo Settings → Actions → Variables and Secrets → New Secret."
|
||||
exit 1
|
||||
fi
|
||||
echo "${{ secrets.GITHUB_TOKEN }}" | \
|
||||
crane auth login ghcr.io -u "${{ github.actor }}" --password-stdin
|
||||
|
||||
- name: Promote verified ECR image to :latest
|
||||
- name: Retag platform :staging-<sha> → :latest
|
||||
run: |
|
||||
set -euo pipefail
|
||||
crane tag \
|
||||
"${IMAGE_NAME}:staging-${{ needs.canary-smoke.outputs.sha }}" \
|
||||
latest
|
||||
|
||||
TARGET_TAG="staging-${SHA}"
|
||||
BODY=$(jq -nc \
|
||||
--arg tag "$TARGET_TAG" \
|
||||
--argjson soak "${SOAK_SECONDS:-120}" \
|
||||
--argjson batch "${BATCH_SIZE:-3}" \
|
||||
--argjson dry false \
|
||||
'{
|
||||
target_tag: $tag,
|
||||
soak_seconds: $soak,
|
||||
batch_size: $batch,
|
||||
dry_run: $dry
|
||||
}')
|
||||
|
||||
if [ -n "${CANARY_SLUG:-}" ]; then
|
||||
BODY=$(jq '. * {canary_slug: $slug}' --arg slug "$CANARY_SLUG" <<<"$BODY")
|
||||
fi
|
||||
|
||||
echo "Calling: POST $CP_URL/cp/admin/tenants/redeploy-fleet"
|
||||
echo " target_tag: $TARGET_TAG"
|
||||
echo " body: $BODY"
|
||||
|
||||
HTTP_RESPONSE=$(mktemp)
|
||||
HTTP_CODE_FILE=$(mktemp)
|
||||
set +e
|
||||
curl -sS -o "$HTTP_RESPONSE" -w '%{http_code}' \
|
||||
-m 1200 \
|
||||
-H "Authorization: Bearer $CP_ADMIN_API_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-X POST "$CP_URL/cp/admin/tenants/redeploy-fleet" \
|
||||
-d "$BODY" >"$HTTP_CODE_FILE"
|
||||
CURL_EXIT=$?
|
||||
set -e
|
||||
|
||||
HTTP_CODE=$(cat "$HTTP_CODE_FILE" 2>/dev/null || echo "000")
|
||||
[ -z "$HTTP_CODE" ] && HTTP_CODE="000"
|
||||
|
||||
echo "HTTP $HTTP_CODE (curl exit $CURL_EXIT)"
|
||||
cat "$HTTP_RESPONSE" | jq . || cat "$HTTP_RESPONSE"
|
||||
|
||||
if [ "$HTTP_CODE" -ge 400 ]; then
|
||||
echo "::error::CP redeploy-fleet returned HTTP $HTTP_CODE — refusing to proceed."
|
||||
exit 1
|
||||
fi
|
||||
- name: Retag tenant :staging-<sha> → :latest
|
||||
run: |
|
||||
crane tag \
|
||||
"${TENANT_IMAGE_NAME}:staging-${{ needs.canary-smoke.outputs.sha }}" \
|
||||
latest
|
||||
|
||||
- name: Summary
|
||||
run: |
|
||||
{
|
||||
echo "## Canary verified — :latest promoted via CP redeploy-fleet"
|
||||
echo ""
|
||||
echo "- **Target tag:** \`staging-${{ needs.canary-smoke.outputs.sha }}\`"
|
||||
echo "- **Registry:** ECR (\`${TENANT_IMAGE_NAME}\`)"
|
||||
echo "- **Canary slug:** \`${CANARY_SLUG:-<none>}\` (soak ${SOAK_SECONDS}s)"
|
||||
echo "- **Batch size:** ${BATCH_SIZE:-3}"
|
||||
echo ""
|
||||
echo "CP redeploy-fleet is rolling out the verified image across the prod fleet."
|
||||
echo "The fleet's 5-minute health-check loop will pick up the update automatically."
|
||||
echo "## Canary verified — :latest promoted"
|
||||
echo
|
||||
echo "- \`${IMAGE_NAME}:staging-${{ needs.canary-smoke.outputs.sha }}\` → \`${IMAGE_NAME}:latest\`"
|
||||
echo "- \`${TENANT_IMAGE_NAME}:staging-${{ needs.canary-smoke.outputs.sha }}\` → \`${TENANT_IMAGE_NAME}:latest\`"
|
||||
echo
|
||||
echo "Prod tenant fleet will pick up the new digest on its next 5-min auto-update cycle."
|
||||
} >> "$GITHUB_STEP_SUMMARY"
|
||||
|
||||
@@ -54,22 +54,6 @@ jobs:
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
|
||||
|
||||
# Health check: verify Docker daemon is accessible before attempting any
|
||||
# build steps. This fails loudly at step 1 when the runner's docker.sock
|
||||
# is inaccessible rather than silently continuing to the build step
|
||||
# where docker build fails deep in ECR auth with a cryptic error.
|
||||
- name: Verify Docker daemon access
|
||||
run: |
|
||||
set -euo pipefail
|
||||
echo "::group::Docker daemon health check"
|
||||
docker info 2>&1 | head -5 || {
|
||||
echo "::error::Docker daemon is not accessible at /var/run/docker.sock"
|
||||
echo "::error::Check: (1) daemon running, (2) runner user in docker group, (3) sock perms 660+"
|
||||
exit 1
|
||||
}
|
||||
echo "Docker daemon OK"
|
||||
echo "::endgroup::"
|
||||
|
||||
- name: Compute tags
|
||||
id: tags
|
||||
shell: bash
|
||||
|
||||
@@ -180,7 +180,7 @@ jobs:
|
||||
# environment pypi-publish. The action mints a short-lived OIDC
|
||||
# token and exchanges it for a PyPI upload credential — no static
|
||||
# API token in this repo's secrets.
|
||||
uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # release/v1
|
||||
uses: pypa/gh-action-pypi-publish@release/v1
|
||||
with:
|
||||
packages-dir: ${{ runner.temp }}/runtime-build/dist/
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ name: publish-workspace-server-image
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
branches: [staging, main]
|
||||
paths:
|
||||
- 'workspace-server/**'
|
||||
- 'canvas/**'
|
||||
@@ -107,22 +107,6 @@ jobs:
|
||||
run: |
|
||||
echo "sha=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
|
||||
|
||||
# Health check: verify Docker daemon is accessible before attempting any
|
||||
# build steps. This fails loudly at step 1 when the runner's docker.sock
|
||||
# is inaccessible rather than silently continuing to the build step
|
||||
# where docker build fails deep in ECR auth with a cryptic error.
|
||||
- name: Verify Docker daemon access
|
||||
run: |
|
||||
set -euo pipefail
|
||||
echo "::group::Docker daemon health check"
|
||||
docker info 2>&1 | head -5 || {
|
||||
echo "::error::Docker daemon is not accessible at /var/run/docker.sock"
|
||||
echo "::error::Check: (1) daemon running, (2) runner user in docker group, (3) sock perms 660+"
|
||||
exit 1
|
||||
}
|
||||
echo "Docker daemon OK"
|
||||
echo "::endgroup::"
|
||||
|
||||
# Pre-clone manifest deps before docker build (Task #173 fix).
|
||||
#
|
||||
# Why pre-clone: post-2026-05-06, every workspace-template-* repo on
|
||||
|
||||
@@ -3,9 +3,9 @@ name: redeploy-tenants-on-main
|
||||
# Auto-refresh prod tenant EC2s after every main merge.
|
||||
#
|
||||
# Why this workflow exists: publish-workspace-server-image builds and
|
||||
# pushes a new platform-tenant :<sha> to ECR on every merge to main,
|
||||
# but running tenants pulled their image once at boot and never re-pull.
|
||||
# Users see stale code indefinitely.
|
||||
# pushes a new platform-tenant:latest + :<sha> to GHCR on every merge
|
||||
# to main, but running tenants pulled their image once at boot and
|
||||
# never re-pull. Users see stale code indefinitely.
|
||||
#
|
||||
# This workflow closes the gap by calling the control-plane admin
|
||||
# endpoint that performs a canary-first, batched, health-gated rolling
|
||||
@@ -13,18 +13,12 @@ name: redeploy-tenants-on-main
|
||||
# molecule-controlplane as POST /cp/admin/tenants/redeploy-fleet
|
||||
# (feat/tenant-auto-redeploy, landing alongside this workflow).
|
||||
#
|
||||
# Registry: ECR (153263036946.dkr.ecr.us-east-2.amazonaws.com/
|
||||
# molecule-ai/platform-tenant). GHCR was retired 2026-05-07 during the
|
||||
# Gitea suspension migration. The canary-verify.yml promote step now
|
||||
# uses the same redeploy-fleet endpoint (fixes the silent-GHCR gap).
|
||||
#
|
||||
# Runtime ordering:
|
||||
# 1. publish-workspace-server-image completes → new :staging-<sha> in ECR.
|
||||
# 2. This workflow fires via workflow_run, calls redeploy-fleet with
|
||||
# target_tag=staging-<sha>. No CDN propagation wait needed —
|
||||
# ECR image manifest is consistent immediately after push.
|
||||
# 3. Calls redeploy-fleet with canary_slug (if set) and a soak
|
||||
# period. Canary proves the image boots; batches follow.
|
||||
# 1. publish-workspace-server-image completes → new :latest in GHCR.
|
||||
# 2. This workflow fires via workflow_run, waits 30s for GHCR's
|
||||
# CDN to propagate the new tag to the region the tenants pull from.
|
||||
# 3. Calls redeploy-fleet with canary_slug=hongming and a 60s
|
||||
# soak. Canary proves the image boots; batches follow.
|
||||
# 4. Any failure aborts the rollout and leaves older tenants on the
|
||||
# prior image — safer default than half-and-half state.
|
||||
#
|
||||
@@ -114,11 +108,13 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 25
|
||||
steps:
|
||||
- name: Note on ECR propagation
|
||||
# ECR image manifests are consistent immediately after push — no
|
||||
# CDN cache to wait for. The old GHCR-based workflow had a 30s
|
||||
# sleep to avoid race conditions; ECR makes that unnecessary.
|
||||
run: echo "ECR image available immediately after push — proceeding."
|
||||
- name: Wait for GHCR tag propagation
|
||||
# GHCR's edge cache takes ~15-30s to consistently serve the new
|
||||
# manifest after the registry accepts the push. Without this
|
||||
# sleep, the first tenant's docker pull sometimes races and
|
||||
# fetches the previous digest; sleeping is the cheapest way to
|
||||
# reduce that without polling GHCR for the new digest.
|
||||
run: sleep 30
|
||||
|
||||
- name: Compute target tag
|
||||
id: tag
|
||||
|
||||
@@ -48,7 +48,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
|
||||
with:
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
staging trigger
|
||||
@@ -142,7 +142,7 @@ export function AuditTrailPanel({ workspaceId }: Props) {
|
||||
key={f.id}
|
||||
onClick={() => setFilter(f.id)}
|
||||
aria-pressed={filter === f.id}
|
||||
className={`px-2 py-1 text-[10px] rounded-md font-medium transition-all shrink-0 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface ${
|
||||
className={`px-2 py-1 text-[10px] rounded-md font-medium transition-all shrink-0 ${
|
||||
filter === f.id
|
||||
? "bg-surface-card text-ink ring-1 ring-zinc-600"
|
||||
: "text-ink-mid hover:text-ink-mid hover:bg-surface-card/60"
|
||||
@@ -155,7 +155,7 @@ export function AuditTrailPanel({ workspaceId }: Props) {
|
||||
<button
|
||||
type="button"
|
||||
onClick={loadEntries}
|
||||
className="px-2 py-1 text-[10px] bg-surface-card hover:bg-surface-card text-ink-mid rounded transition-colors shrink-0 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-2 py-1 text-[10px] bg-surface-card hover:bg-surface-card text-ink-mid rounded transition-colors shrink-0"
|
||||
aria-label="Refresh audit trail"
|
||||
>
|
||||
↻
|
||||
@@ -195,7 +195,7 @@ export function AuditTrailPanel({ workspaceId }: Props) {
|
||||
type="button"
|
||||
onClick={loadMore}
|
||||
disabled={loadingMore}
|
||||
className="px-4 py-2 text-[11px] bg-surface-card hover:bg-surface-card disabled:opacity-50 disabled:cursor-not-allowed text-ink-mid rounded-lg transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-4 py-2 text-[11px] bg-surface-card hover:bg-surface-card disabled:opacity-50 disabled:cursor-not-allowed text-ink-mid rounded-lg transition-colors"
|
||||
>
|
||||
{loadingMore ? "Loading…" : "Load more"}
|
||||
</button>
|
||||
|
||||
@@ -209,7 +209,7 @@ export function CommunicationOverlay() {
|
||||
type="button"
|
||||
onClick={() => setVisible(true)}
|
||||
aria-label="Show communications panel"
|
||||
className="fixed top-16 right-4 z-30 px-3 py-1.5 bg-surface-sunken/90 border border-line/50 rounded-lg text-[10px] text-ink-mid hover:text-ink transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="fixed top-16 right-4 z-30 px-3 py-1.5 bg-surface-sunken/90 border border-line/50 rounded-lg text-[10px] text-ink-mid hover:text-ink transition-colors"
|
||||
>
|
||||
<span aria-hidden="true">↗↙ </span>{comms.length > 0 ? `${comms.length} comms` : "Communications"}
|
||||
</button>
|
||||
@@ -226,7 +226,7 @@ export function CommunicationOverlay() {
|
||||
type="button"
|
||||
onClick={() => setVisible(false)}
|
||||
aria-label="Close communications panel"
|
||||
className="text-ink-mid hover:text-ink-mid text-xs focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface rounded"
|
||||
className="text-ink-mid hover:text-ink-mid text-xs"
|
||||
>
|
||||
<span aria-hidden="true">✕</span>
|
||||
</button>
|
||||
|
||||
@@ -13,8 +13,7 @@ interface Props {
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
/** Exported for unit testing — see ConversationTraceModal.test.ts */
|
||||
export function extractMessageText(body: Record<string, unknown> | null): string {
|
||||
function extractMessageText(body: Record<string, unknown> | null): string {
|
||||
if (!body) return "";
|
||||
try {
|
||||
// Simple task format from MCP server: {task: "..."}
|
||||
@@ -115,7 +114,7 @@ export function ConversationTraceModal({ open, workspaceId: _workspaceId, onClos
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Close conversation trace"
|
||||
className="text-ink-mid hover:text-ink-mid text-lg px-2 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface rounded"
|
||||
className="text-ink-mid hover:text-ink-mid text-lg px-2"
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
@@ -286,7 +285,7 @@ export function ConversationTraceModal({ open, workspaceId: _workspaceId, onClos
|
||||
<Dialog.Close asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="px-4 py-1.5 text-[12px] bg-surface-card hover:bg-surface-card text-ink-mid rounded-lg transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-4 py-1.5 text-[12px] bg-surface-card hover:bg-surface-card text-ink-mid rounded-lg transition-colors"
|
||||
>
|
||||
Close
|
||||
</button>
|
||||
|
||||
@@ -411,7 +411,7 @@ export function CreateWorkspaceButton() {
|
||||
tabIndex={tier === t.value ? 0 : -1}
|
||||
onClick={() => setTier(t.value)}
|
||||
onKeyDown={(e) => handleRadioKeyDown(e, idx)}
|
||||
className={`py-2 rounded-lg text-center transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 ${
|
||||
className={`py-2 rounded-lg text-center transition-colors ${
|
||||
tier === t.value
|
||||
? "bg-accent-strong/20 border border-accent/50 text-accent"
|
||||
: "bg-surface-card/60 border border-line/40 text-ink-mid hover:text-ink-mid hover:border-line"
|
||||
|
||||
@@ -83,7 +83,7 @@ export class ErrorBoundary extends React.Component<
|
||||
<button
|
||||
type="button"
|
||||
onClick={this.handleReload}
|
||||
className="rounded-lg bg-accent-strong hover:bg-accent px-5 py-2 text-sm font-medium text-white transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-surface"
|
||||
className="rounded-lg bg-accent-strong hover:bg-accent px-5 py-2 text-sm font-medium text-white transition-colors"
|
||||
>
|
||||
Reload
|
||||
</button>
|
||||
@@ -93,7 +93,7 @@ export class ErrorBoundary extends React.Component<
|
||||
e.preventDefault();
|
||||
this.handleReport();
|
||||
}}
|
||||
className="rounded-lg border border-line hover:border-line px-5 py-2 text-sm font-medium text-ink-mid hover:text-ink transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-surface"
|
||||
className="rounded-lg border border-line hover:border-line px-5 py-2 text-sm font-medium text-ink-mid hover:text-ink transition-colors"
|
||||
>
|
||||
Report
|
||||
</a>
|
||||
|
||||
@@ -198,7 +198,7 @@ export function ExternalConnectModal({ info, onClose }: Props) {
|
||||
role="tab"
|
||||
aria-selected={tab === t}
|
||||
onClick={() => setTab(t)}
|
||||
className={`px-3 py-2 text-sm border-b-2 -mb-px transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface ${
|
||||
className={`px-3 py-2 text-sm border-b-2 -mb-px transition-colors ${
|
||||
tab === t
|
||||
? "border-accent text-ink"
|
||||
: "border-transparent text-ink-mid hover:text-ink-mid"
|
||||
@@ -309,7 +309,7 @@ export function ExternalConnectModal({ info, onClose }: Props) {
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="px-4 py-2 text-sm rounded-lg bg-surface-card hover:bg-surface-card text-ink focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-4 py-2 text-sm rounded-lg bg-surface-card hover:bg-surface-card text-ink"
|
||||
>
|
||||
I've saved it — close
|
||||
</button>
|
||||
@@ -339,7 +339,7 @@ function SnippetBlock({
|
||||
<button
|
||||
type="button"
|
||||
onClick={onCopy}
|
||||
className="text-xs px-2 py-1 rounded bg-accent-strong/80 hover:bg-accent text-white focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="text-xs px-2 py-1 rounded bg-accent-strong/80 hover:bg-accent text-white"
|
||||
>
|
||||
{copied ? "Copied!" : "Copy"}
|
||||
</button>
|
||||
@@ -376,7 +376,7 @@ function Field({
|
||||
type="button"
|
||||
onClick={onCopy}
|
||||
disabled={!value}
|
||||
className="text-xs px-2 py-1 rounded bg-surface-card hover:bg-surface-card text-ink disabled:opacity-40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="text-xs px-2 py-1 rounded bg-surface-card hover:bg-surface-card text-ink disabled:opacity-40"
|
||||
>
|
||||
{copied ? "Copied!" : "Copy"}
|
||||
</button>
|
||||
|
||||
@@ -360,7 +360,7 @@ export function MemoryInspectorPanel({ workspaceId }: Props) {
|
||||
setDebouncedQuery('');
|
||||
}}
|
||||
aria-label="Clear search"
|
||||
className="absolute right-2 text-ink-mid hover:text-ink transition-colors text-sm leading-none focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface rounded"
|
||||
className="absolute right-2 text-ink-mid hover:text-ink transition-colors text-sm leading-none"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
@@ -381,7 +381,7 @@ export function MemoryInspectorPanel({ workspaceId }: Props) {
|
||||
type="button"
|
||||
onClick={loadEntries}
|
||||
disabled={pluginUnavailable}
|
||||
className="px-2 py-1 text-[11px] bg-surface-card hover:bg-surface-card text-ink-mid rounded transition-colors disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-2 py-1 text-[11px] bg-surface-card hover:bg-surface-card text-ink-mid rounded transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
aria-label="Refresh memories"
|
||||
>
|
||||
↻ Refresh
|
||||
@@ -515,7 +515,7 @@ function MemoryEntryRow({ entry, onDelete }: MemoryEntryRowProps) {
|
||||
{/* Header row */}
|
||||
<button
|
||||
type="button"
|
||||
className="w-full flex items-center gap-2 px-3 py-2.5 text-left hover:bg-surface-card/30 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="w-full flex items-center gap-2 px-3 py-2.5 text-left hover:bg-surface-card/30 transition-colors"
|
||||
onClick={() => setExpanded((prev) => !prev)}
|
||||
aria-expanded={expanded}
|
||||
aria-controls={bodyId}
|
||||
@@ -629,7 +629,7 @@ function MemoryEntryRow({ entry, onDelete }: MemoryEntryRowProps) {
|
||||
onDelete();
|
||||
}}
|
||||
aria-label="Forget memory"
|
||||
className="text-[10px] px-2 py-0.5 bg-red-950/40 hover:bg-red-900/50 border border-red-900/30 rounded text-bad transition-colors shrink-0 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-red-500/60 focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="text-[10px] px-2 py-0.5 bg-red-950/40 hover:bg-red-900/50 border border-red-900/30 rounded text-bad transition-colors shrink-0"
|
||||
>
|
||||
Forget
|
||||
</button>
|
||||
|
||||
@@ -632,7 +632,7 @@ function AllKeysModal({
|
||||
<div className="fixed inset-0 z-[60] flex items-center justify-center">
|
||||
<div
|
||||
className="absolute inset-0 bg-black/70 backdrop-blur-sm"
|
||||
aria-label="Dismiss modal"
|
||||
aria-hidden="true"
|
||||
onClick={onCancel}
|
||||
/>
|
||||
|
||||
@@ -706,7 +706,7 @@ function AllKeysModal({
|
||||
type="button"
|
||||
onClick={() => handleSaveKey(index)}
|
||||
disabled={!entry.value.trim() || entry.saving}
|
||||
className="px-3 py-1.5 bg-accent-strong hover:bg-accent text-[11px] rounded text-white disabled:opacity-30 transition-colors shrink-0 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-3 py-1.5 bg-accent-strong hover:bg-accent text-[11px] rounded text-white disabled:opacity-30 transition-colors shrink-0"
|
||||
>
|
||||
{entry.saving ? "..." : "Save"}
|
||||
</button>
|
||||
@@ -730,7 +730,7 @@ function AllKeysModal({
|
||||
<button
|
||||
type="button"
|
||||
onClick={onOpenSettings}
|
||||
className="text-[11px] text-accent hover:text-accent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface rounded"
|
||||
className="text-[11px] text-accent hover:text-accent transition-colors"
|
||||
>
|
||||
Open Settings Panel
|
||||
</button>
|
||||
@@ -740,7 +740,7 @@ function AllKeysModal({
|
||||
<button
|
||||
type="button"
|
||||
onClick={onCancel}
|
||||
className="px-3.5 py-1.5 text-[12px] text-ink-mid hover:text-ink bg-surface-card hover:bg-surface-card border border-line rounded-lg transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-3.5 py-1.5 text-[12px] text-ink-mid hover:text-ink bg-surface-card hover:bg-surface-card border border-line rounded-lg transition-colors"
|
||||
>
|
||||
Cancel Deploy
|
||||
</button>
|
||||
@@ -748,7 +748,7 @@ function AllKeysModal({
|
||||
type="button"
|
||||
onClick={handleAddKeysAndDeploy}
|
||||
disabled={!allSaved || anySaving}
|
||||
className="px-3.5 py-1.5 text-[12px] bg-accent-strong hover:bg-accent text-white rounded-lg transition-colors disabled:opacity-40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-3.5 py-1.5 text-[12px] bg-accent-strong hover:bg-accent text-white rounded-lg transition-colors disabled:opacity-40"
|
||||
>
|
||||
{anySaving ? "Saving..." : allSaved ? "Deploy" : "Add Keys"}
|
||||
</button>
|
||||
|
||||
@@ -308,7 +308,7 @@ export function OrgImportPreflightModal({
|
||||
type="button"
|
||||
onClick={onProceed}
|
||||
disabled={!canProceed}
|
||||
className="px-4 py-1.5 text-[11px] font-semibold rounded bg-accent hover:bg-accent-strong text-white disabled:bg-surface-card disabled:text-white-soft disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-4 py-1.5 text-[11px] font-semibold rounded bg-accent hover:bg-accent-strong text-white disabled:bg-surface-card disabled:text-white-soft disabled:cursor-not-allowed"
|
||||
>
|
||||
Import
|
||||
</button>
|
||||
@@ -428,7 +428,7 @@ function StrictEnvRow({
|
||||
type="button"
|
||||
onClick={() => onSave(envKey)}
|
||||
disabled={d?.saving || !d?.value.trim()}
|
||||
className="px-2 py-1 text-[10px] rounded bg-accent hover:bg-accent-strong text-white disabled:opacity-40 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-2 py-1 text-[10px] rounded bg-accent hover:bg-accent-strong text-white disabled:opacity-40 disabled:cursor-not-allowed"
|
||||
>
|
||||
{d?.saving ? "…" : "Save"}
|
||||
</button>
|
||||
@@ -520,7 +520,7 @@ function AnyOfEnvGroup({
|
||||
type="button"
|
||||
onClick={() => onSave(m)}
|
||||
disabled={d?.saving || !d?.value.trim()}
|
||||
className="px-2 py-1 text-[10px] rounded bg-accent hover:bg-accent-strong text-white disabled:opacity-40 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-2 py-1 text-[10px] rounded bg-accent hover:bg-accent-strong text-white disabled:opacity-40 disabled:cursor-not-allowed"
|
||||
>
|
||||
{d?.saving ? "…" : "Save"}
|
||||
</button>
|
||||
|
||||
@@ -128,7 +128,7 @@ function PlanCard({
|
||||
type="button"
|
||||
onClick={onSelect}
|
||||
disabled={loading}
|
||||
className={`mt-6 rounded-lg px-4 py-3 text-sm font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-surface ${
|
||||
className={`mt-6 rounded-lg px-4 py-3 text-sm font-medium ${
|
||||
plan.highlighted
|
||||
? "bg-accent-strong text-white hover:bg-accent disabled:bg-blue-900"
|
||||
: "border border-line bg-surface-sunken text-ink hover:bg-surface-card disabled:opacity-50"
|
||||
|
||||
@@ -437,7 +437,7 @@ export function ProviderModelSelector({
|
||||
handleModelChange(selected.models[0]?.id ?? "");
|
||||
}
|
||||
}}
|
||||
className="text-[9px] text-accent hover:text-accent mt-0.5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface rounded"
|
||||
className="text-[9px] text-accent hover:text-accent mt-0.5"
|
||||
>
|
||||
← back to model list
|
||||
</button>
|
||||
|
||||
@@ -341,7 +341,7 @@ export function ProvisioningTimeout({
|
||||
type="button"
|
||||
onClick={() => handleRetry(entry.workspaceId)}
|
||||
disabled={isRetrying || isCancelling || retryCooldown.has(entry.workspaceId)}
|
||||
className="px-3 py-1.5 bg-amber-600 hover:bg-amber-500 text-[11px] font-medium rounded-lg text-white disabled:opacity-40 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-400/70 focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-3 py-1.5 bg-amber-600 hover:bg-amber-500 text-[11px] font-medium rounded-lg text-white disabled:opacity-40 transition-colors"
|
||||
>
|
||||
{isRetrying ? "Retrying..." : retryCooldown.has(entry.workspaceId) ? "Wait..." : "Retry"}
|
||||
</button>
|
||||
@@ -349,14 +349,14 @@ export function ProvisioningTimeout({
|
||||
type="button"
|
||||
onClick={() => handleCancelRequest(entry.workspaceId)}
|
||||
disabled={isRetrying || isCancelling}
|
||||
className="px-3 py-1.5 bg-surface-card hover:bg-surface-card text-[11px] text-ink-mid rounded-lg border border-line disabled:opacity-40 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-3 py-1.5 bg-surface-card hover:bg-surface-card text-[11px] text-ink-mid rounded-lg border border-line disabled:opacity-40 transition-colors"
|
||||
>
|
||||
{isCancelling ? "Cancelling..." : "Cancel"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleViewLogs(entry.workspaceId)}
|
||||
className="px-3 py-1.5 text-[11px] text-warm hover:text-warm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-400/70 focus-visible:ring-offset-1 focus-visible:ring-offset-surface rounded"
|
||||
className="px-3 py-1.5 text-[11px] text-warm hover:text-warm transition-colors"
|
||||
>
|
||||
View Logs
|
||||
</button>
|
||||
@@ -382,14 +382,14 @@ export function ProvisioningTimeout({
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setConfirmingCancel(null)}
|
||||
className="px-3.5 py-1.5 text-[12px] text-ink-mid hover:text-ink bg-surface-card hover:bg-surface-card border border-line rounded-lg transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-3.5 py-1.5 text-[12px] text-ink-mid hover:text-ink bg-surface-card hover:bg-surface-card border border-line rounded-lg transition-colors"
|
||||
>
|
||||
Keep
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleCancelConfirm}
|
||||
className="px-3.5 py-1.5 text-[12px] bg-red-600 hover:bg-red-500 text-white rounded-lg transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-red-400/70 focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="px-3.5 py-1.5 text-[12px] bg-red-600 hover:bg-red-500 text-white rounded-lg transition-colors"
|
||||
>
|
||||
Remove Workspace
|
||||
</button>
|
||||
|
||||
@@ -181,7 +181,7 @@ export function SidePanel() {
|
||||
type="button"
|
||||
onClick={() => selectNode(null)}
|
||||
aria-label="Close workspace panel"
|
||||
className="w-7 h-7 flex items-center justify-center rounded-lg text-ink-mid hover:text-ink hover:bg-surface-card/60 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="w-7 h-7 flex items-center justify-center rounded-lg text-ink-mid hover:text-ink hover:bg-surface-card/60 transition-colors"
|
||||
>
|
||||
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true">
|
||||
<path d="M1 1l10 10M11 1L1 11" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
|
||||
|
||||
@@ -236,7 +236,7 @@ export function OrgTemplatesSection() {
|
||||
onClick={() => setExpanded((v) => !v)}
|
||||
aria-expanded={expanded}
|
||||
aria-controls="org-templates-body"
|
||||
className="flex items-center gap-1.5 text-[10px] uppercase tracking-wide text-ink-mid hover:text-ink-mid font-semibold transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface rounded"
|
||||
className="flex items-center gap-1.5 text-[10px] uppercase tracking-wide text-ink-mid hover:text-ink-mid font-semibold transition-colors"
|
||||
>
|
||||
<span
|
||||
aria-hidden="true"
|
||||
@@ -255,7 +255,7 @@ export function OrgTemplatesSection() {
|
||||
type="button"
|
||||
onClick={loadOrgs}
|
||||
aria-label="Refresh org templates"
|
||||
className="text-[10px] text-ink-mid hover:text-ink-mid focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface rounded"
|
||||
className="text-[10px] text-ink-mid hover:text-ink-mid"
|
||||
>
|
||||
↻
|
||||
</button>
|
||||
@@ -306,7 +306,7 @@ export function OrgTemplatesSection() {
|
||||
type="button"
|
||||
onClick={() => handleImport(o)}
|
||||
disabled={isImporting}
|
||||
className="w-full px-2 py-1.5 bg-accent-strong/20 hover:bg-accent-strong/30 border border-accent/30 rounded-lg text-[10px] text-accent font-medium transition-colors disabled:opacity-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="w-full px-2 py-1.5 bg-accent-strong/20 hover:bg-accent-strong/30 border border-accent/30 rounded-lg text-[10px] text-accent font-medium transition-colors disabled:opacity-50"
|
||||
>
|
||||
{isImporting ? "Importing…" : "Import org"}
|
||||
</button>
|
||||
@@ -411,7 +411,7 @@ function ImportAgentButton({ onImported }: { onImported: () => void }) {
|
||||
type="button"
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
disabled={importing}
|
||||
className="w-full px-3 py-2 bg-accent-strong/20 hover:bg-accent-strong/30 border border-accent/30 rounded-lg text-[11px] text-accent font-medium transition-colors disabled:opacity-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface"
|
||||
className="w-full px-3 py-2 bg-accent-strong/20 hover:bg-accent-strong/30 border border-accent/30 rounded-lg text-[11px] text-accent font-medium transition-colors disabled:opacity-50"
|
||||
>
|
||||
{importing ? "Importing..." : "Import Agent Folder"}
|
||||
</button>
|
||||
@@ -474,7 +474,7 @@ export function TemplatePalette() {
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen(!open)}
|
||||
className={`fixed top-4 left-4 z-40 w-9 h-9 flex items-center justify-center rounded-lg transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-surface ${
|
||||
className={`fixed top-4 left-4 z-40 w-9 h-9 flex items-center justify-center rounded-lg transition-colors ${
|
||||
open
|
||||
? "bg-accent-strong text-white"
|
||||
: "bg-surface-sunken/90 border border-line/50 text-ink-mid hover:text-ink hover:border-line"
|
||||
@@ -580,7 +580,7 @@ export function TemplatePalette() {
|
||||
<button
|
||||
type="button"
|
||||
onClick={loadTemplates}
|
||||
className="text-[10px] text-ink-mid hover:text-ink-mid transition-colors block focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface rounded"
|
||||
className="text-[10px] text-ink-mid hover:text-ink-mid transition-colors block"
|
||||
>
|
||||
Refresh templates
|
||||
</button>
|
||||
|
||||
@@ -54,7 +54,7 @@ export function ThemeToggle({ className = "" }: { className?: string }) {
|
||||
aria-label={opt.label}
|
||||
onClick={() => setTheme(opt.value)}
|
||||
className={
|
||||
"flex h-6 w-6 items-center justify-center rounded transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 focus-visible:ring-offset-surface " +
|
||||
"flex h-6 w-6 items-center justify-center rounded transition-colors " +
|
||||
(active
|
||||
? "bg-surface-elevated text-ink shadow-sm"
|
||||
: "text-ink-mid hover:text-ink-mid")
|
||||
|
||||
@@ -317,7 +317,7 @@ export function Toolbar() {
|
||||
onClick={() => setHelpOpen((open) => !open)}
|
||||
className="flex items-center justify-center w-7 h-7 bg-surface-card hover:bg-surface-card/70 border border-line rounded-lg transition-colors text-ink-mid hover:text-ink focus:outline-none focus-visible:ring-2 focus-visible:ring-accent/40"
|
||||
aria-expanded={helpOpen}
|
||||
aria-label="Open shortcuts and tips"
|
||||
aria-label="Open quick help"
|
||||
title="Help — shortcuts & quick start"
|
||||
>
|
||||
<svg width="14" height="14" viewBox="0 0 16 16" fill="none" aria-hidden="true">
|
||||
@@ -327,35 +327,24 @@ export function Toolbar() {
|
||||
</button>
|
||||
|
||||
{helpOpen && (
|
||||
<div
|
||||
role="dialog"
|
||||
aria-label="Shortcuts and tips"
|
||||
aria-modal="false"
|
||||
className="absolute right-0 top-full mt-2 w-80 rounded-xl border border-line/60 bg-surface/95 p-3 shadow-2xl shadow-black/50 backdrop-blur-md z-50"
|
||||
>
|
||||
<div className="mb-3 flex items-center justify-between">
|
||||
<span className="text-[10px] font-semibold uppercase tracking-[0.24em] text-ink-mid">Shortcuts & tips</span>
|
||||
<div className="absolute right-0 top-full mt-2 w-72 rounded-xl border border-line/60 bg-surface/95 p-3 shadow-2xl shadow-black/50 backdrop-blur-md">
|
||||
<div className="mb-2 flex items-center justify-between">
|
||||
<span className="text-[10px] font-semibold uppercase tracking-[0.24em] text-ink-mid">Quick start</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setHelpOpen(false)}
|
||||
aria-label="Close help dialog"
|
||||
className="text-[10px] text-ink-mid hover:text-ink transition-colors focus:outline-none focus-visible:underline"
|
||||
>
|
||||
Close
|
||||
</button>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<div className="space-y-2">
|
||||
<HelpRow shortcut="⌘K" text="Search workspaces and jump straight into Details or Chat." />
|
||||
<HelpRow shortcut="Esc" text="Clear selection, close menus, dismiss dialogs." />
|
||||
<HelpRow shortcut="Enter" text="Zoom into selected team and select its first child node." />
|
||||
<HelpRow shortcut="Shift+Enter" text="Select the parent of the selected node." />
|
||||
<HelpRow shortcut="⌘]" text="Bring selected node forward in the z-order." />
|
||||
<HelpRow shortcut="⌘[" text="Send selected node backward in the z-order." />
|
||||
<HelpRow shortcut="Z" text="Zoom canvas to fit a team node and all its sub-workspaces." />
|
||||
<HelpRow shortcut="Palette" text="Open the template palette to deploy a new workspace." />
|
||||
<HelpRow shortcut="Right-click" text="Use node actions for duplicate, export, restart, or delete." />
|
||||
<HelpRow shortcut="Dbl-click" text="On a team node: expand and zoom to show all sub-workspaces." />
|
||||
<HelpRow shortcut="Shift+click" text="Multi-select: add or remove a node from the batch selection." />
|
||||
<HelpRow shortcut="Chat" text="If a task is still running, the chat tab resumes that session automatically." />
|
||||
<HelpRow shortcut="Config" text="Use the Config tab for skills, model, secrets, and runtime settings." />
|
||||
<HelpRow shortcut="Dbl-click / Z" text="Zoom canvas to fit a team node and all its sub-workspaces." />
|
||||
</div>
|
||||
{/* Link to the full keyboard shortcuts dialog */}
|
||||
<button
|
||||
|
||||
@@ -1,376 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for ContextMenu component.
|
||||
*
|
||||
* Covers: null guard, node header (name + status), outside-click close,
|
||||
* Escape close, arrow-key navigation, conditional menu items by status,
|
||||
* danger items, dividers, rAF position clamping.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen, fireEvent, cleanup, act, waitFor } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { ContextMenu } from "../ContextMenu";
|
||||
import { useCanvasStore } from "@/store/canvas";
|
||||
import { showToast } from "../Toaster";
|
||||
|
||||
// ─── Mock Toaster ─────────────────────────────────────────────────────────────
|
||||
|
||||
vi.mock("../Toaster", () => ({
|
||||
showToast: vi.fn(),
|
||||
}));
|
||||
|
||||
// ─── Mock API ────────────────────────────────────────────────────────────────
|
||||
|
||||
const apiPost = vi.fn().mockResolvedValue(undefined as void);
|
||||
const apiPatch = vi.fn().mockResolvedValue(undefined as void);
|
||||
vi.mock("@/lib/api", () => ({
|
||||
api: {
|
||||
post: apiPost,
|
||||
patch: apiPatch,
|
||||
get: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
// ─── Mock store ──────────────────────────────────────────────────────────────
|
||||
|
||||
const mockStoreState = {
|
||||
contextMenu: null as {
|
||||
x: number;
|
||||
y: number;
|
||||
nodeId: string;
|
||||
nodeData: {
|
||||
name: string;
|
||||
status: string;
|
||||
tier: number;
|
||||
role: string;
|
||||
parentId?: string | null;
|
||||
collapsed?: boolean;
|
||||
};
|
||||
} | null,
|
||||
closeContextMenu: vi.fn(),
|
||||
updateNodeData: vi.fn(),
|
||||
selectNode: vi.fn(),
|
||||
setPanelTab: vi.fn(),
|
||||
nestNode: vi.fn().mockResolvedValue(undefined as void),
|
||||
setPendingDelete: vi.fn(),
|
||||
setCollapsed: vi.fn(),
|
||||
arrangeChildren: vi.fn(),
|
||||
nodes: [] as Array<{
|
||||
id: string;
|
||||
data: { parentId?: string | null };
|
||||
}>,
|
||||
};
|
||||
|
||||
vi.mock("@/store/canvas", () => ({
|
||||
useCanvasStore: Object.assign(
|
||||
(sel: (s: typeof mockStoreState) => unknown) => sel(mockStoreState),
|
||||
{ getState: () => mockStoreState },
|
||||
),
|
||||
}));
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
function openMenu(overrides?: Partial<NonNullable<typeof mockStoreState.contextMenu>>) {
|
||||
mockStoreState.contextMenu = {
|
||||
x: 100,
|
||||
y: 200,
|
||||
nodeId: "n1",
|
||||
nodeData: { name: "Alice", status: "online", tier: 4, role: "assistant" },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("ContextMenu — visibility", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.contextMenu = null;
|
||||
mockStoreState.closeContextMenu.mockClear();
|
||||
mockStoreState.updateNodeData.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
mockStoreState.nestNode.mockClear();
|
||||
mockStoreState.setPendingDelete.mockClear();
|
||||
mockStoreState.setCollapsed.mockClear();
|
||||
mockStoreState.arrangeChildren.mockClear();
|
||||
mockStoreState.nodes = [];
|
||||
apiPost.mockReset();
|
||||
apiPatch.mockReset();
|
||||
vi.mocked(showToast).mockClear();
|
||||
});
|
||||
|
||||
it("renders nothing when contextMenu is null", () => {
|
||||
mockStoreState.contextMenu = null;
|
||||
render(<ContextMenu />);
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
});
|
||||
|
||||
it("renders the menu when contextMenu is set", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
expect(screen.getByRole("menu")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("has aria-label describing the node name", () => {
|
||||
openMenu({ nodeData: { name: "Alice", status: "online", tier: 4, role: "assistant" } });
|
||||
render(<ContextMenu />);
|
||||
expect(screen.getByRole("menu").getAttribute("aria-label")).toBe("Actions for Alice");
|
||||
});
|
||||
|
||||
it("shows the node name in the header", () => {
|
||||
openMenu({ nodeData: { name: "Bob", status: "offline", tier: 2, role: "analyst" } });
|
||||
render(<ContextMenu />);
|
||||
expect(screen.getByText("Bob")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows the node status in the header", () => {
|
||||
openMenu({ nodeData: { name: "Alice", status: "failed", tier: 4, role: "assistant" } });
|
||||
render(<ContextMenu />);
|
||||
expect(screen.getByText("failed")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ContextMenu — close", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.contextMenu = null;
|
||||
mockStoreState.closeContextMenu.mockClear();
|
||||
mockStoreState.updateNodeData.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
mockStoreState.nestNode.mockClear();
|
||||
mockStoreState.setPendingDelete.mockClear();
|
||||
mockStoreState.setCollapsed.mockClear();
|
||||
mockStoreState.arrangeChildren.mockClear();
|
||||
mockStoreState.nodes = [];
|
||||
apiPost.mockReset();
|
||||
apiPatch.mockReset();
|
||||
vi.mocked(showToast).mockClear();
|
||||
});
|
||||
|
||||
it("closes when clicking outside the menu", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
fireEvent.mouseDown(document.body);
|
||||
expect(mockStoreState.closeContextMenu).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("closes when Escape is pressed", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
fireEvent.keyDown(document.body, { key: "Escape" });
|
||||
expect(mockStoreState.closeContextMenu).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("closes when Tab is pressed", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
fireEvent.keyDown(document.body, { key: "Tab" });
|
||||
expect(mockStoreState.closeContextMenu).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ContextMenu — menu items", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.contextMenu = null;
|
||||
mockStoreState.closeContextMenu.mockClear();
|
||||
mockStoreState.updateNodeData.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
mockStoreState.nestNode.mockClear();
|
||||
mockStoreState.setPendingDelete.mockClear();
|
||||
mockStoreState.setCollapsed.mockClear();
|
||||
mockStoreState.arrangeChildren.mockClear();
|
||||
mockStoreState.nodes = [];
|
||||
apiPost.mockReset();
|
||||
apiPatch.mockReset();
|
||||
vi.mocked(showToast).mockClear();
|
||||
});
|
||||
|
||||
it("shows Chat and Terminal only for online nodes", () => {
|
||||
openMenu({ nodeData: { name: "Alice", status: "online", tier: 4, role: "assistant" } });
|
||||
render(<ContextMenu />);
|
||||
expect(screen.getByRole("menuitem", { name: /chat/i })).toBeTruthy();
|
||||
expect(screen.getByRole("menuitem", { name: /terminal/i })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("hides Chat and Terminal for offline nodes", () => {
|
||||
openMenu({ nodeData: { name: "Bob", status: "offline", tier: 2, role: "analyst" } });
|
||||
render(<ContextMenu />);
|
||||
expect(screen.queryByRole("menuitem", { name: /chat/i })).toBeNull();
|
||||
expect(screen.queryByRole("menuitem", { name: /terminal/i })).toBeNull();
|
||||
});
|
||||
|
||||
it("shows Pause for online nodes (not paused)", () => {
|
||||
openMenu({ nodeData: { name: "Alice", status: "online", tier: 4, role: "assistant" } });
|
||||
render(<ContextMenu />);
|
||||
expect(screen.getByRole("menuitem", { name: /pause/i })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows Resume for paused nodes (not Pause)", () => {
|
||||
openMenu({ nodeData: { name: "Carol", status: "paused", tier: 3, role: "writer" } });
|
||||
render(<ContextMenu />);
|
||||
expect(screen.queryByRole("menuitem", { name: /pause/i })).toBeNull();
|
||||
expect(screen.getByRole("menuitem", { name: /resume/i })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows Extract from Team only for child nodes", () => {
|
||||
openMenu({ nodeData: { name: "Child", status: "online", tier: 4, role: "", parentId: "parent1" } });
|
||||
render(<ContextMenu />);
|
||||
expect(screen.getByRole("menuitem", { name: /extract/i })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("hides Extract from Team for root nodes", () => {
|
||||
openMenu({ nodeData: { name: "Root", status: "online", tier: 4, role: "", parentId: null } });
|
||||
render(<ContextMenu />);
|
||||
expect(screen.queryByRole("menuitem", { name: /extract/i })).toBeNull();
|
||||
});
|
||||
|
||||
it("shows team items only when node has children", () => {
|
||||
openMenu({ nodeData: { name: "Parent", status: "online", tier: 4, role: "" } });
|
||||
mockStoreState.nodes = [{ id: "child1", data: { parentId: "n1" } }];
|
||||
render(<ContextMenu />);
|
||||
expect(screen.getByRole("menuitem", { name: /arrange/i })).toBeTruthy();
|
||||
expect(screen.getByRole("menuitem", { name: /collapse/i })).toBeTruthy();
|
||||
expect(screen.getByRole("menuitem", { name: /zoom/i })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("hides team items when node has no children", () => {
|
||||
openMenu({ nodeData: { name: "Leaf", status: "online", tier: 4, role: "" } });
|
||||
mockStoreState.nodes = [];
|
||||
render(<ContextMenu />);
|
||||
expect(screen.queryByRole("menuitem", { name: /arrange/i })).toBeNull();
|
||||
expect(screen.queryByRole("menuitem", { name: /collapse/i })).toBeNull();
|
||||
expect(screen.queryByRole("menuitem", { name: /zoom/i })).toBeNull();
|
||||
});
|
||||
|
||||
it("shows Collapse Team when collapsed, Expand Team when expanded", () => {
|
||||
openMenu({ nodeData: { name: "Parent", status: "online", tier: 4, role: "", collapsed: true } });
|
||||
mockStoreState.nodes = [{ id: "child1", data: { parentId: "n1" } }];
|
||||
render(<ContextMenu />);
|
||||
expect(screen.getByRole("menuitem", { name: /expand/i })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("Delete item has danger styling class", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
const deleteItem = screen.getByRole("menuitem", { name: /delete/i });
|
||||
expect(deleteItem.getAttribute("class")).toMatch(/text-bad|bad/);
|
||||
});
|
||||
|
||||
it("renders role=separator for dividers", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
expect(document.body.querySelectorAll('[role="separator"]').length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ContextMenu — keyboard navigation", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.contextMenu = null;
|
||||
mockStoreState.closeContextMenu.mockClear();
|
||||
mockStoreState.updateNodeData.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
mockStoreState.nestNode.mockClear();
|
||||
mockStoreState.setPendingDelete.mockClear();
|
||||
mockStoreState.setCollapsed.mockClear();
|
||||
mockStoreState.arrangeChildren.mockClear();
|
||||
mockStoreState.nodes = [];
|
||||
apiPost.mockReset();
|
||||
apiPatch.mockReset();
|
||||
vi.mocked(showToast).mockClear();
|
||||
});
|
||||
|
||||
it("ArrowDown moves focus to next enabled menuitem", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
const menu = screen.getByRole("menu");
|
||||
// First tab goes to Details (first non-disabled item)
|
||||
fireEvent.keyDown(menu, { key: "ArrowDown" });
|
||||
const buttons = screen.getAllByRole("menuitem");
|
||||
const focusedIdx = buttons.findIndex((b) => document.activeElement === b);
|
||||
expect(focusedIdx).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
it("ArrowUp moves focus to previous enabled menuitem", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
const menu = screen.getByRole("menu");
|
||||
fireEvent.keyDown(menu, { key: "ArrowDown" });
|
||||
const beforeFocused = document.activeElement;
|
||||
fireEvent.keyDown(menu, { key: "ArrowUp" });
|
||||
// Focus should have moved
|
||||
expect(document.activeElement).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ContextMenu — item actions", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.contextMenu = null;
|
||||
mockStoreState.closeContextMenu.mockClear();
|
||||
mockStoreState.updateNodeData.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
mockStoreState.nestNode.mockClear();
|
||||
mockStoreState.setPendingDelete.mockClear();
|
||||
mockStoreState.setCollapsed.mockClear();
|
||||
mockStoreState.arrangeChildren.mockClear();
|
||||
mockStoreState.nodes = [];
|
||||
apiPost.mockReset();
|
||||
apiPatch.mockReset();
|
||||
vi.mocked(showToast).mockClear();
|
||||
});
|
||||
|
||||
it("Details selects node and opens details tab", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
fireEvent.click(screen.getByRole("menuitem", { name: /details/i }));
|
||||
expect(mockStoreState.selectNode).toHaveBeenCalledWith("n1");
|
||||
expect(mockStoreState.setPanelTab).toHaveBeenCalledWith("details");
|
||||
});
|
||||
|
||||
it("Chat selects node and opens chat tab", () => {
|
||||
openMenu({ nodeData: { name: "Alice", status: "online", tier: 4, role: "assistant" } });
|
||||
render(<ContextMenu />);
|
||||
fireEvent.click(screen.getByRole("menuitem", { name: /chat/i }));
|
||||
expect(mockStoreState.selectNode).toHaveBeenCalledWith("n1");
|
||||
expect(mockStoreState.setPanelTab).toHaveBeenCalledWith("chat");
|
||||
});
|
||||
|
||||
it("Delete calls setPendingDelete without closing immediately", () => {
|
||||
openMenu();
|
||||
render(<ContextMenu />);
|
||||
fireEvent.click(screen.getByRole("menuitem", { name: /delete/i }));
|
||||
expect(mockStoreState.setPendingDelete).toHaveBeenCalled();
|
||||
expect(mockStoreState.closeContextMenu).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("Pause calls the pause API and updates node status optimistically", async () => {
|
||||
openMenu({ nodeData: { name: "Alice", status: "online", tier: 4, role: "assistant" } });
|
||||
apiPost.mockResolvedValue(undefined);
|
||||
render(<ContextMenu />);
|
||||
fireEvent.click(screen.getByRole("menuitem", { name: /pause/i }));
|
||||
await act(async () => { /* flush */ });
|
||||
expect(apiPost).toHaveBeenCalledWith("/workspaces/n1/pause", {});
|
||||
expect(mockStoreState.updateNodeData).toHaveBeenCalledWith("n1", { status: "paused" });
|
||||
});
|
||||
|
||||
it("Resume calls the resume API", async () => {
|
||||
openMenu({ nodeData: { name: "Alice", status: "paused", tier: 4, role: "assistant" } });
|
||||
apiPost.mockResolvedValue(undefined);
|
||||
render(<ContextMenu />);
|
||||
fireEvent.click(screen.getByRole("menuitem", { name: /resume/i }));
|
||||
await act(async () => { /* flush */ });
|
||||
expect(apiPost).toHaveBeenCalledWith("/workspaces/n1/resume", {});
|
||||
});
|
||||
});
|
||||
@@ -1,156 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for ConversationTraceModal's extractMessageText helper.
|
||||
*
|
||||
* Covers: MCP simple task format, request params.message.parts extraction,
|
||||
* response result.parts extraction, result.root.text extraction, plain string
|
||||
* result, null input, malformed input, empty strings.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { extractMessageText } from "../ConversationTraceModal";
|
||||
|
||||
describe("extractMessageText — MCP simple task format", () => {
|
||||
it("extracts text from body.task field", () => {
|
||||
const body = { task: "Deploy the agent to production" };
|
||||
expect(extractMessageText(body)).toBe("Deploy the agent to production");
|
||||
});
|
||||
|
||||
it("returns empty string when body is null", () => {
|
||||
expect(extractMessageText(null)).toBe("");
|
||||
});
|
||||
|
||||
it("returns empty string when body is undefined", () => {
|
||||
expect(extractMessageText(undefined as unknown as null)).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractMessageText — request params.message format", () => {
|
||||
it("extracts text from params.message.parts[].text", () => {
|
||||
const body = {
|
||||
params: {
|
||||
message: {
|
||||
parts: [{ text: "Hello world" }],
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(extractMessageText(body)).toBe("Hello world");
|
||||
});
|
||||
|
||||
it("joins multiple parts with newlines", () => {
|
||||
const body = {
|
||||
params: {
|
||||
message: {
|
||||
parts: [
|
||||
{ text: "First part" },
|
||||
{ text: "Second part" },
|
||||
{ text: "Third part" },
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(extractMessageText(body)).toBe("First part\nSecond part\nThird part");
|
||||
});
|
||||
|
||||
it("ignores parts without text field", () => {
|
||||
const body = {
|
||||
params: {
|
||||
message: {
|
||||
parts: [{ text: "Hello" }, { other: "field" }, { text: "World" }],
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(extractMessageText(body)).toBe("Hello\nWorld");
|
||||
});
|
||||
|
||||
it("returns empty string when params.message is absent", () => {
|
||||
const body = { params: {} };
|
||||
expect(extractMessageText(body)).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractMessageText — response result format", () => {
|
||||
it("extracts text from result.parts[].text", () => {
|
||||
const body = {
|
||||
result: {
|
||||
parts: [{ text: "Agent response" }],
|
||||
},
|
||||
};
|
||||
expect(extractMessageText(body)).toBe("Agent response");
|
||||
});
|
||||
|
||||
it("extracts text from result.parts[].root.text", () => {
|
||||
const body = {
|
||||
result: {
|
||||
parts: [{ root: { text: "Root response text" } }],
|
||||
},
|
||||
};
|
||||
expect(extractMessageText(body)).toBe("Root response text");
|
||||
});
|
||||
|
||||
it("prefers parts[].text over parts[].root.text", () => {
|
||||
const body = {
|
||||
result: {
|
||||
parts: [
|
||||
{ text: "Direct text" },
|
||||
{ root: { text: "Root text" } },
|
||||
],
|
||||
},
|
||||
};
|
||||
// Both are non-empty strings, so the first one wins (filter picks the first)
|
||||
// The implementation: rText from rParts[0].text = "Direct text"
|
||||
expect(extractMessageText(body)).toBe("Direct text");
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractMessageText — plain string result", () => {
|
||||
it("returns body.result when it is a plain string", () => {
|
||||
const body = { result: "Simple string response" };
|
||||
expect(extractMessageText(body)).toBe("Simple string response");
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractMessageText — priority order", () => {
|
||||
it("prefers task format over params format", () => {
|
||||
const body = {
|
||||
task: "Task text",
|
||||
params: { message: { parts: [{ text: "Params text" }] } },
|
||||
};
|
||||
// Implementation: checks task first, returns if non-empty
|
||||
expect(extractMessageText(body)).toBe("Task text");
|
||||
});
|
||||
|
||||
it("prefers params format over result format", () => {
|
||||
const body = {
|
||||
params: { message: { parts: [{ text: "Params text" }] } },
|
||||
result: { parts: [{ text: "Result text" }] },
|
||||
};
|
||||
// Implementation: checks params.message.parts first (after task)
|
||||
expect(extractMessageText(body)).toBe("Params text");
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractMessageText — error resilience", () => {
|
||||
it("returns empty string on malformed input", () => {
|
||||
expect(extractMessageText({})).toBe("");
|
||||
expect(extractMessageText({ params: null })).toBe("");
|
||||
expect(extractMessageText({ result: null })).toBe("");
|
||||
});
|
||||
|
||||
it("returns empty string when all fields are absent", () => {
|
||||
expect(extractMessageText({ random: "field" })).toBe("");
|
||||
});
|
||||
|
||||
it("handles missing parts array gracefully", () => {
|
||||
const body = { params: { message: {} } };
|
||||
expect(extractMessageText(body)).toBe("");
|
||||
});
|
||||
|
||||
it("handles parts with undefined text gracefully", () => {
|
||||
const body = {
|
||||
result: {
|
||||
parts: [{ text: undefined }, { text: "valid" }],
|
||||
},
|
||||
};
|
||||
expect(extractMessageText(body)).toBe("valid");
|
||||
});
|
||||
});
|
||||
@@ -1,170 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for KeyValueField component.
|
||||
*
|
||||
* Covers: renders password input, type=text when revealed,
|
||||
* onChange prop, auto-trim on paste, auto-hide after 30s,
|
||||
* disabled state, aria-label.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen, fireEvent, cleanup, act } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { KeyValueField } from "../ui/KeyValueField";
|
||||
|
||||
const AUTO_HIDE_MS = 30_000;
|
||||
|
||||
describe("KeyValueField — render", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("renders a password input by default", () => {
|
||||
render(<KeyValueField value="" onChange={vi.fn()} />);
|
||||
expect(screen.getByRole("textbox").getAttribute("type")).toBe("password");
|
||||
});
|
||||
|
||||
it("renders a text input when revealed=true", () => {
|
||||
const { container } = render(<KeyValueField value="secret" onChange={vi.fn()} />);
|
||||
// Cannot use getByRole because type=text inputs may not be queryable as textbox in jsdom
|
||||
const input = container.querySelector("input");
|
||||
expect(input).toBeTruthy();
|
||||
expect(input!.getAttribute("type")).toBe("password");
|
||||
});
|
||||
|
||||
it("uses the provided aria-label", () => {
|
||||
render(<KeyValueField value="" onChange={vi.fn()} aria-label="My secret field" />);
|
||||
expect(screen.getByRole("textbox").getAttribute("aria-label")).toBe("My secret field");
|
||||
});
|
||||
|
||||
it("uses default aria-label when omitted", () => {
|
||||
render(<KeyValueField value="" onChange={vi.fn()} />);
|
||||
expect(screen.getByRole("textbox").getAttribute("aria-label")).toBe("Secret value");
|
||||
});
|
||||
|
||||
it("renders a disabled input when disabled=true", () => {
|
||||
render(<KeyValueField value="x" onChange={vi.fn()} disabled={true} />);
|
||||
expect(screen.getByRole("textbox").getAttribute("disabled")).toBe("");
|
||||
});
|
||||
|
||||
it("renders with the provided placeholder", () => {
|
||||
render(<KeyValueField value="" onChange={vi.fn()} placeholder="Enter API key" />);
|
||||
expect(screen.getByRole("textbox").getAttribute("placeholder")).toBe("Enter API key");
|
||||
});
|
||||
|
||||
it("disables spell-check on the input", () => {
|
||||
render(<KeyValueField value="" onChange={vi.fn()} />);
|
||||
expect(screen.getByRole("textbox").getAttribute("spellcheck")).toBe("false");
|
||||
});
|
||||
|
||||
it("sets autoComplete=off on the input", () => {
|
||||
render(<KeyValueField value="" onChange={vi.fn()} />);
|
||||
expect(screen.getByRole("textbox").getAttribute("autocomplete")).toBe("off");
|
||||
});
|
||||
});
|
||||
|
||||
describe("KeyValueField — onChange", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("calls onChange when input changes", () => {
|
||||
const onChange = vi.fn();
|
||||
render(<KeyValueField value="" onChange={onChange} />);
|
||||
fireEvent.change(screen.getByRole("textbox"), { target: { value: "abc" } });
|
||||
expect(onChange).toHaveBeenCalledWith("abc");
|
||||
});
|
||||
|
||||
it("trims trailing whitespace on change", () => {
|
||||
const onChange = vi.fn();
|
||||
render(<KeyValueField value="" onChange={onChange} />);
|
||||
fireEvent.change(screen.getByRole("textbox"), { target: { value: "abc " } });
|
||||
expect(onChange).toHaveBeenCalledWith("abc");
|
||||
});
|
||||
|
||||
it("trims leading whitespace on change", () => {
|
||||
const onChange = vi.fn();
|
||||
render(<KeyValueField value="" onChange={onChange} />);
|
||||
fireEvent.change(screen.getByRole("textbox"), { target: { value: " abc" } });
|
||||
expect(onChange).toHaveBeenCalledWith("abc");
|
||||
});
|
||||
|
||||
it("passes value through unchanged when no whitespace trimming needed", () => {
|
||||
const onChange = vi.fn();
|
||||
render(<KeyValueField value="" onChange={onChange} />);
|
||||
fireEvent.change(screen.getByRole("textbox"), { target: { value: "no-change" } });
|
||||
expect(onChange).toHaveBeenCalledWith("no-change");
|
||||
});
|
||||
});
|
||||
|
||||
// Paste trimming is tested via onChange (handleChange trims whitespace) and
|
||||
// the structural trim logic is exercised by the onChange tests above.
|
||||
// Full paste testing requires @testing-library/user-event which is not installed.
|
||||
|
||||
describe("KeyValueField — auto-hide timer", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("auto-hides after 30 seconds when revealed", async () => {
|
||||
const onChange = vi.fn();
|
||||
render(<KeyValueField value="secret" onChange={onChange} />);
|
||||
|
||||
// Reveal the value
|
||||
const input = document.body.querySelector("input");
|
||||
fireEvent.click(document.body.querySelector("button")!);
|
||||
// After reveal, input type should be text (not password)
|
||||
expect(input?.getAttribute("type")).not.toBe("password");
|
||||
|
||||
// Advance 30 seconds
|
||||
act(() => { vi.advanceTimersByTime(AUTO_HIDE_MS); });
|
||||
|
||||
// Value should be hidden again — the input value is managed externally
|
||||
// via `value` prop, so we check the input type flipped back to password
|
||||
// by verifying the button was clicked twice (setRevealed toggled)
|
||||
// The component's internal revealed state should be false after timer fires.
|
||||
// Since we can't read internal state, we verify the behavior by checking
|
||||
// the input type (it flips back to password after auto-hide).
|
||||
// The timer callback calls setRevealed(false) which flips type back to password.
|
||||
const typeAfter = document.body.querySelector("input")?.getAttribute("type");
|
||||
expect(typeAfter).toBe("password");
|
||||
});
|
||||
|
||||
it("does not fire auto-hide before 30 seconds", async () => {
|
||||
const onChange = vi.fn();
|
||||
render(<KeyValueField value="secret" onChange={onChange} />);
|
||||
|
||||
fireEvent.click(document.body.querySelector("button")!);
|
||||
|
||||
// Advance 29 seconds — should NOT have hidden yet
|
||||
act(() => { vi.advanceTimersByTime(AUTO_HIDE_MS - 1000); });
|
||||
|
||||
const typeAfter = document.body.querySelector("input")?.getAttribute("type");
|
||||
// Still revealed (type=text) after 29s
|
||||
expect(typeAfter).toBe("text");
|
||||
});
|
||||
|
||||
it("clears the timer when revealed flips back to false before timeout", () => {
|
||||
const onChange = vi.fn();
|
||||
render(<KeyValueField value="secret" onChange={onChange} />);
|
||||
|
||||
fireEvent.click(document.body.querySelector("button")!);
|
||||
// Hide manually before the 30s auto-hide
|
||||
fireEvent.click(document.body.querySelector("button")!);
|
||||
|
||||
// Advance full 30s — should not crash (timer already cleared)
|
||||
act(() => { vi.advanceTimersByTime(AUTO_HIDE_MS); });
|
||||
|
||||
// Still hidden (we hid it manually)
|
||||
expect(document.body.querySelector("input")?.getAttribute("type")).toBe("password");
|
||||
});
|
||||
});
|
||||
@@ -1,69 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for MissingKeysModal's providerIdForModel helper.
|
||||
*
|
||||
* Covers: model match, no match, empty modelId, whitespace-only modelId,
|
||||
* model with no required_env, models undefined, single vs multiple env vars,
|
||||
* stable sort order for env var ordering.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { providerIdForModel } from "../MissingKeysModal";
|
||||
|
||||
describe("providerIdForModel — match behavior", () => {
|
||||
it("returns sorted-joined env vars when model is found", () => {
|
||||
const models = [
|
||||
{ id: "claude-3-5-sonnet", name: "Claude 3.5 Sonnet", required_env: ["ANTHROPIC_API_KEY"] },
|
||||
];
|
||||
expect(providerIdForModel("claude-3-5-sonnet", models)).toBe("ANTHROPIC_API_KEY");
|
||||
});
|
||||
|
||||
it("returns null when model is not found", () => {
|
||||
const models = [
|
||||
{ id: "claude-3-5-sonnet", name: "Claude 3.5 Sonnet", required_env: ["ANTHROPIC_API_KEY"] },
|
||||
];
|
||||
expect(providerIdForModel("unknown-model", models)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when models is undefined", () => {
|
||||
expect(providerIdForModel("claude-3-5-sonnet", undefined)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when modelId is empty string", () => {
|
||||
const models = [{ id: "claude", name: "Claude", required_env: ["KEY"] }];
|
||||
expect(providerIdForModel("", models)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when modelId is whitespace-only", () => {
|
||||
const models = [{ id: "claude", name: "Claude", required_env: ["KEY"] }];
|
||||
expect(providerIdForModel(" ", models)).toBeNull();
|
||||
});
|
||||
|
||||
it("trims whitespace from modelId before matching", () => {
|
||||
const models = [{ id: "claude", name: "Claude", required_env: ["KEY"] }];
|
||||
expect(providerIdForModel(" claude ", models)).toBe("KEY");
|
||||
});
|
||||
});
|
||||
|
||||
describe("providerIdForModel — required_env variations", () => {
|
||||
it("returns null when model has no required_env", () => {
|
||||
const models = [{ id: "local-model", name: "Local Model", required_env: [] }];
|
||||
expect(providerIdForModel("local-model", models)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when model.required_env is undefined", () => {
|
||||
const models = [{ id: "local-model", name: "Local Model" }] as Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
required_env?: string[];
|
||||
}>;
|
||||
expect(providerIdForModel("local-model", models)).toBeNull();
|
||||
});
|
||||
|
||||
it("sorts and joins multiple required_env alphabetically", () => {
|
||||
const models = [
|
||||
{ id: "openrouter", name: "OpenRouter", required_env: ["OPENAI_API_KEY", "ANTHROPIC_API_KEY"] },
|
||||
];
|
||||
// Expected: alphabetically sorted = ANTHROPIC_API_KEY|OPENAI_API_KEY
|
||||
expect(providerIdForModel("openrouter", models)).toBe("ANTHROPIC_API_KEY|OPENAI_API_KEY");
|
||||
});
|
||||
});
|
||||
@@ -1,174 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for OnboardingWizard component.
|
||||
*
|
||||
* Covers: renders only when not dismissed, renders 4 steps, dismiss
|
||||
* button, localStorage persistence, progress bar width, step navigation,
|
||||
* auto-advance from welcome→api-key on nodes change, aria-live region.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen, fireEvent, cleanup, act, waitFor } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { OnboardingWizard } from "../OnboardingWizard";
|
||||
import { useCanvasStore } from "@/store/canvas";
|
||||
|
||||
const mockStoreState = {
|
||||
nodes: [] as Array<{ id: string; data: Record<string, unknown> }>,
|
||||
selectedNodeId: null as string | null,
|
||||
panelTab: "chat" as string,
|
||||
agentMessages: {} as Record<string, unknown[]>,
|
||||
setPanelTab: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mock("@/store/canvas", () => ({
|
||||
useCanvasStore: Object.assign(
|
||||
(sel: (s: typeof mockStoreState) => unknown) => sel(mockStoreState),
|
||||
{ getState: () => mockStoreState },
|
||||
),
|
||||
}));
|
||||
|
||||
const STORAGE_KEY = "molecule-onboarding-complete";
|
||||
|
||||
const localStorageMock = (() => {
|
||||
let store: Record<string, string> = {};
|
||||
return {
|
||||
getItem: vi.fn((key: string): string | null => store[key] ?? null),
|
||||
setItem: vi.fn((key: string, value: string) => { store[key] = value; }),
|
||||
removeItem: vi.fn((key: string) => { delete store[key]; }),
|
||||
clear: () => { store = {}; },
|
||||
getStore: () => store,
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(window, "localStorage", { value: localStorageMock });
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
localStorageMock.clear();
|
||||
vi.clearAllMocks();
|
||||
// Reset mutable store properties (mockStoreState is const, so mutate fields)
|
||||
mockStoreState.nodes = [];
|
||||
mockStoreState.selectedNodeId = null;
|
||||
mockStoreState.panelTab = "chat";
|
||||
mockStoreState.agentMessages = {};
|
||||
mockStoreState.setPanelTab = vi.fn();
|
||||
});
|
||||
|
||||
// ─── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("OnboardingWizard — visibility", () => {
|
||||
it("renders nothing when localStorage has the complete flag", () => {
|
||||
localStorageMock.getItem.mockReturnValueOnce("true");
|
||||
render(<OnboardingWizard />);
|
||||
expect(screen.queryByRole("complementary")).toBeNull();
|
||||
});
|
||||
|
||||
it("renders the wizard for first-time users (no localStorage flag)", () => {
|
||||
localStorageMock.getItem.mockReturnValueOnce(null);
|
||||
render(<OnboardingWizard />);
|
||||
expect(screen.getByRole("complementary", { name: "Onboarding guide" })).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("OnboardingWizard — steps", () => {
|
||||
beforeEach(() => {
|
||||
localStorageMock.getItem.mockReturnValue(null);
|
||||
});
|
||||
|
||||
it("renders step 1 'Welcome to Molecule AI' on first paint", () => {
|
||||
render(<OnboardingWizard />);
|
||||
expect(screen.getByText("Welcome to Molecule AI")).toBeTruthy();
|
||||
expect(screen.getByText("Step 1 of 4")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders the 'Skip guide' button", () => {
|
||||
render(<OnboardingWizard />);
|
||||
expect(screen.getByRole("button", { name: "Skip onboarding guide" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders the progress bar", () => {
|
||||
render(<OnboardingWizard />);
|
||||
// Progress bar is inside a div
|
||||
const bar = document.body.querySelector(".h-full.bg-gradient-to-r");
|
||||
expect(bar).toBeTruthy();
|
||||
// Step 1 should be 25% wide
|
||||
expect(bar?.getAttribute("style")).toContain("25%");
|
||||
});
|
||||
|
||||
it("advances to step 2 'Set your API key' when Next is clicked", () => {
|
||||
render(<OnboardingWizard />);
|
||||
expect(screen.getByText("Welcome to Molecule AI")).toBeTruthy();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
||||
expect(screen.getByText("Set your API key")).toBeTruthy();
|
||||
expect(screen.getByText("Step 2 of 4")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("advances to step 3 'Send your first message' when Next is clicked twice", () => {
|
||||
render(<OnboardingWizard />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
||||
expect(screen.getByText("Send your first message")).toBeTruthy();
|
||||
expect(screen.getByText("Step 3 of 4")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows 'Get Started' button on the last step", () => {
|
||||
render(<OnboardingWizard />);
|
||||
// Navigate to done step
|
||||
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
||||
expect(screen.getByText("You're all set!")).toBeTruthy();
|
||||
expect(screen.getByRole("button", { name: "Get Started" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("dismisses the wizard when 'Skip guide' is clicked", () => {
|
||||
render(<OnboardingWizard />);
|
||||
expect(screen.getByRole("complementary")).toBeTruthy();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Skip onboarding guide" }));
|
||||
expect(screen.queryByRole("complementary")).toBeNull();
|
||||
});
|
||||
|
||||
it("persists the dismissed state to localStorage when dismissed", () => {
|
||||
render(<OnboardingWizard />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Skip onboarding guide" }));
|
||||
expect(localStorageMock.setItem).toHaveBeenCalledWith(STORAGE_KEY, "true");
|
||||
});
|
||||
});
|
||||
|
||||
describe("OnboardingWizard — auto-advance", () => {
|
||||
beforeEach(() => {
|
||||
localStorageMock.getItem.mockReturnValue(null);
|
||||
});
|
||||
|
||||
it("auto-advances from welcome to api-key when nodes appear", async () => {
|
||||
const { unmount } = render(<OnboardingWizard />);
|
||||
expect(screen.getByText("Welcome to Molecule AI")).toBeTruthy();
|
||||
|
||||
// Simulate a node being added to the store and re-render
|
||||
mockStoreState.nodes = [{ id: "ws-1", data: {} }];
|
||||
render(<OnboardingWizard />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText("Welcome to Molecule AI")).toBeNull();
|
||||
});
|
||||
expect(screen.getByText("Set your API key")).toBeTruthy();
|
||||
unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("OnboardingWizard — accessibility", () => {
|
||||
beforeEach(() => {
|
||||
localStorageMock.getItem.mockReturnValue(null);
|
||||
});
|
||||
|
||||
it("has aria-live='polite' region for step announcements", () => {
|
||||
render(<OnboardingWizard />);
|
||||
const liveRegion = document.body.querySelector('[aria-live="polite"]');
|
||||
expect(liveRegion).toBeTruthy();
|
||||
expect(liveRegion?.textContent).toMatch(/onboarding step 1/i);
|
||||
});
|
||||
|
||||
it("has role=complementary with aria-label", () => {
|
||||
render(<OnboardingWizard />);
|
||||
expect(screen.getByRole("complementary", { name: "Onboarding guide" })).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -1,255 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for PurchaseSuccessModal component.
|
||||
*
|
||||
* Covers: no render when no URL params, renders with ?purchase_success=1,
|
||||
* portal rendering, item name from &item=, auto-dismiss after 5s,
|
||||
* manual dismiss, backdrop click close, Escape key close, URL stripping,
|
||||
* focus management.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen, fireEvent, cleanup, act } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { PurchaseSuccessModal } from "../PurchaseSuccessModal";
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
function pushUrl(url: string) {
|
||||
window.history.pushState({}, "", url);
|
||||
}
|
||||
function replaceUrl(url: string) {
|
||||
window.history.replaceState({}, "", url);
|
||||
}
|
||||
|
||||
// ─── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("PurchaseSuccessModal — render conditions", () => {
|
||||
beforeEach(() => {
|
||||
replaceUrl("http://localhost/");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("renders nothing when URL has no purchase_success param", () => {
|
||||
replaceUrl("http://localhost/");
|
||||
render(<PurchaseSuccessModal />);
|
||||
expect(screen.queryByRole("dialog")).toBeNull();
|
||||
});
|
||||
|
||||
it("renders nothing on a plain URL", () => {
|
||||
replaceUrl("http://localhost/dashboard?foo=bar");
|
||||
render(<PurchaseSuccessModal />);
|
||||
expect(screen.queryByRole("dialog")).toBeNull();
|
||||
});
|
||||
|
||||
it("renders the dialog when ?purchase_success=1 is present", async () => {
|
||||
replaceUrl("http://localhost/?purchase_success=1");
|
||||
render(<PurchaseSuccessModal />);
|
||||
// useEffect fires after mount
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.queryByRole("dialog")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders the dialog when ?purchase_success=true is present", async () => {
|
||||
replaceUrl("http://localhost/?purchase_success=true");
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.queryByRole("dialog")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders a portal attached to document.body", async () => {
|
||||
replaceUrl("http://localhost/?purchase_success=1");
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
const dialog = document.body.querySelector('[role="dialog"]');
|
||||
expect(dialog).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows the item name when &item= is present", async () => {
|
||||
replaceUrl("http://localhost/?purchase_success=1&item=MyAgent");
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.getByText("MyAgent")).toBeTruthy();
|
||||
expect(screen.getByText("Purchase successful")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows 'Your new agent' when no item param is present", async () => {
|
||||
replaceUrl("http://localhost/?purchase_success=1");
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.getByText("Your new agent")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("decodes URI-encoded item names", async () => {
|
||||
replaceUrl("http://localhost/?purchase_success=1&item=Claude%20Code%20Agent");
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.getByText("Claude Code Agent")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("PurchaseSuccessModal — dismiss", () => {
|
||||
beforeEach(() => {
|
||||
replaceUrl("http://localhost/?purchase_success=1&item=TestItem");
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("closes the dialog when the close button is clicked", async () => {
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.getByRole("dialog")).toBeTruthy();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Close" }));
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(10);
|
||||
});
|
||||
expect(screen.queryByRole("dialog")).toBeNull();
|
||||
});
|
||||
|
||||
it("closes the dialog when the backdrop is clicked", async () => {
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.getByRole("dialog")).toBeTruthy();
|
||||
// Click the backdrop (the full-screen overlay div)
|
||||
const backdrop = document.body.querySelector('[aria-hidden="true"]');
|
||||
if (backdrop) fireEvent.click(backdrop);
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(10);
|
||||
});
|
||||
expect(screen.queryByRole("dialog")).toBeNull();
|
||||
});
|
||||
|
||||
it("closes on Escape key", async () => {
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.getByRole("dialog")).toBeTruthy();
|
||||
fireEvent.keyDown(window, { key: "Escape" });
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(10);
|
||||
});
|
||||
expect(screen.queryByRole("dialog")).toBeNull();
|
||||
});
|
||||
|
||||
it("auto-dismisses after 5 seconds", async () => {
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.getByRole("dialog")).toBeTruthy();
|
||||
|
||||
// Advance 5 seconds
|
||||
act(() => { vi.advanceTimersByTime(5000); });
|
||||
await act(async () => { /* flush */ });
|
||||
expect(screen.queryByRole("dialog")).toBeNull();
|
||||
});
|
||||
|
||||
it("does not auto-dismiss before 5 seconds", async () => {
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(screen.getByRole("dialog")).toBeTruthy();
|
||||
|
||||
act(() => { vi.advanceTimersByTime(4900); });
|
||||
await act(async () => { /* flush */ });
|
||||
expect(screen.queryByRole("dialog")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("PurchaseSuccessModal — URL stripping", () => {
|
||||
beforeEach(() => {
|
||||
replaceUrl("http://localhost/?purchase_success=1&item=TestItem");
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("strips purchase_success and item params from the URL on mount", async () => {
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
const url = new URL(window.location.href);
|
||||
expect(url.searchParams.get("purchase_success")).toBeNull();
|
||||
expect(url.searchParams.get("item")).toBeNull();
|
||||
});
|
||||
|
||||
it("uses replaceState (not pushState) so back-button does not re-trigger", async () => {
|
||||
const replaceSpy = vi.spyOn(window.history, "replaceState");
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
expect(replaceSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("PurchaseSuccessModal — accessibility", () => {
|
||||
beforeEach(() => {
|
||||
replaceUrl("http://localhost/?purchase_success=1&item=TestItem");
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("has aria-modal=true on the dialog", async () => {
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
const dialog = screen.getByRole("dialog");
|
||||
expect(dialog.getAttribute("aria-modal")).toBe("true");
|
||||
});
|
||||
|
||||
it("has aria-labelledby pointing to the title", async () => {
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
});
|
||||
const dialog = screen.getByRole("dialog");
|
||||
const labelledby = dialog.getAttribute("aria-labelledby");
|
||||
expect(labelledby).toBeTruthy();
|
||||
expect(document.getElementById(labelledby!)).toBeTruthy();
|
||||
expect(document.getElementById(labelledby!)?.textContent).toMatch(/purchase successful/i);
|
||||
});
|
||||
|
||||
it("moves focus to the close button on open", async () => {
|
||||
render(<PurchaseSuccessModal />);
|
||||
await act(async () => {
|
||||
// Two rAFs for focus: one from the effect, one from the RAF wrapper
|
||||
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
|
||||
});
|
||||
expect(document.activeElement?.textContent).toMatch(/close/i);
|
||||
});
|
||||
});
|
||||
@@ -1,64 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for RevealToggle component.
|
||||
*
|
||||
* Covers: renders eye icon when hidden, eye-off when revealed,
|
||||
* aria-label, title text, onToggle callback.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen, fireEvent } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { RevealToggle } from "../ui/RevealToggle";
|
||||
|
||||
describe("RevealToggle — render", () => {
|
||||
it("renders a button element", () => {
|
||||
render(<RevealToggle revealed={false} onToggle={vi.fn()} />);
|
||||
expect(screen.getByRole("button")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("uses the provided aria-label", () => {
|
||||
render(<RevealToggle revealed={false} onToggle={vi.fn()} label="Show password" />);
|
||||
expect(screen.getByRole("button").getAttribute("aria-label")).toBe("Show password");
|
||||
});
|
||||
|
||||
it("uses default aria-label when label prop is omitted", () => {
|
||||
render(<RevealToggle revealed={false} onToggle={vi.fn()} />);
|
||||
expect(screen.getByRole("button").getAttribute("aria-label")).toBe("Toggle visibility");
|
||||
});
|
||||
|
||||
it("has title 'Show value' when revealed=false", () => {
|
||||
render(<RevealToggle revealed={false} onToggle={vi.fn()} />);
|
||||
expect(screen.getByRole("button").getAttribute("title")).toBe("Show value");
|
||||
});
|
||||
|
||||
it("has title 'Hide value' when revealed=true", () => {
|
||||
render(<RevealToggle revealed={true} onToggle={vi.fn()} />);
|
||||
expect(screen.getByRole("button").getAttribute("title")).toBe("Hide value");
|
||||
});
|
||||
});
|
||||
|
||||
describe("RevealToggle — interaction", () => {
|
||||
it("calls onToggle when clicked", () => {
|
||||
const onToggle = vi.fn();
|
||||
render(<RevealToggle revealed={false} onToggle={onToggle} />);
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
expect(onToggle).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("renders EyeIcon (eye SVG) when revealed=false", () => {
|
||||
const { container } = render(<RevealToggle revealed={false} onToggle={vi.fn()} />);
|
||||
const svg = container.querySelector("svg");
|
||||
expect(svg).toBeTruthy();
|
||||
// Eye icon has a circle path for the eye
|
||||
expect(container.innerHTML).toContain("M1 12s4-8 11-8");
|
||||
});
|
||||
|
||||
it("renders EyeOffIcon (eye-off SVG) when revealed=true", () => {
|
||||
const { container } = render(<RevealToggle revealed={true} onToggle={vi.fn()} />);
|
||||
const svg = container.querySelector("svg");
|
||||
expect(svg).toBeTruthy();
|
||||
// Eye-off has a diagonal line
|
||||
expect(container.innerHTML).toContain("x1");
|
||||
expect(container.innerHTML).toContain("y2");
|
||||
});
|
||||
});
|
||||
@@ -1,351 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for SearchDialog component.
|
||||
*
|
||||
* Covers: renders only when open, Cmd+K/Ctrl+K shortcut, Escape close,
|
||||
* focus management, text filtering (name/role/status), arrow-key
|
||||
* navigation, Enter to select, footer count, aria attributes.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen, fireEvent, cleanup, act } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { SearchDialog } from "../SearchDialog";
|
||||
import { useCanvasStore } from "@/store/canvas";
|
||||
|
||||
// ─── Mock store ──────────────────────────────────────────────────────────────
|
||||
|
||||
const mockStoreState = {
|
||||
searchOpen: false,
|
||||
setSearchOpen: vi.fn((open: boolean) => {
|
||||
mockStoreState.searchOpen = open;
|
||||
}),
|
||||
nodes: [] as Array<{
|
||||
id: string;
|
||||
data: {
|
||||
name: string;
|
||||
status: string;
|
||||
tier: number;
|
||||
role: string;
|
||||
parentId?: string | null;
|
||||
};
|
||||
}>,
|
||||
selectNode: vi.fn(),
|
||||
setPanelTab: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mock("@/store/canvas", () => ({
|
||||
useCanvasStore: Object.assign(
|
||||
(sel: (s: typeof mockStoreState) => unknown) => sel(mockStoreState),
|
||||
{ getState: () => mockStoreState },
|
||||
),
|
||||
}));
|
||||
|
||||
const STORAGE_KEY = "molecule-onboarding-complete";
|
||||
|
||||
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||
|
||||
function dispatchKeydown(key: string, meta = false, ctrl = false) {
|
||||
fireEvent.keyDown(window, {
|
||||
key,
|
||||
metaKey: meta,
|
||||
ctrlKey: ctrl,
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("SearchDialog — visibility", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.searchOpen = false;
|
||||
mockStoreState.nodes = [];
|
||||
mockStoreState.setSearchOpen.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
});
|
||||
|
||||
it("does not render when searchOpen is false", () => {
|
||||
mockStoreState.searchOpen = false;
|
||||
render(<SearchDialog />);
|
||||
expect(screen.queryByRole("dialog")).toBeNull();
|
||||
});
|
||||
|
||||
it("renders the dialog when searchOpen is true", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
expect(screen.getByRole("dialog", { name: "Search workspaces" })).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("SearchDialog — keyboard shortcuts", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.searchOpen = false;
|
||||
mockStoreState.nodes = [];
|
||||
mockStoreState.setSearchOpen.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
});
|
||||
|
||||
it("opens the dialog when Cmd+K is pressed", () => {
|
||||
render(<SearchDialog />);
|
||||
dispatchKeydown("k", true, false);
|
||||
expect(mockStoreState.setSearchOpen).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("opens the dialog when Ctrl+K is pressed", () => {
|
||||
render(<SearchDialog />);
|
||||
dispatchKeydown("k", false, true);
|
||||
expect(mockStoreState.setSearchOpen).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("clears the query when Cmd+K opens the dialog", () => {
|
||||
render(<SearchDialog />);
|
||||
dispatchKeydown("k", true, false);
|
||||
const input = screen.getByRole("combobox");
|
||||
expect(input.getAttribute("value") ?? "").toBe("");
|
||||
});
|
||||
|
||||
it("closes the dialog when Escape is pressed while open", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
dispatchKeydown("Escape");
|
||||
expect(mockStoreState.setSearchOpen).toHaveBeenCalledWith(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("SearchDialog — focus", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.searchOpen = false;
|
||||
mockStoreState.nodes = [];
|
||||
mockStoreState.setSearchOpen.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
});
|
||||
|
||||
it("focuses the input when the dialog opens", async () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
await act(async () => {
|
||||
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
|
||||
});
|
||||
expect(document.activeElement?.getAttribute("role")).toBe("combobox");
|
||||
});
|
||||
|
||||
it("input has the combobox role", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
expect(screen.getByRole("combobox")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("SearchDialog — filtering", () => {
|
||||
beforeEach(() => {
|
||||
mockStoreState.nodes = [
|
||||
{ id: "n1", data: { name: "Alice", status: "online", tier: 4, role: "assistant" } },
|
||||
{ id: "n2", data: { name: "Bob", status: "offline", tier: 2, role: "analyst" } },
|
||||
{ id: "n3", data: { name: "Carol", status: "online", tier: 3, role: "writer" } },
|
||||
];
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.searchOpen = false;
|
||||
mockStoreState.nodes = [];
|
||||
mockStoreState.setSearchOpen.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
});
|
||||
|
||||
it("shows all workspaces when query is empty", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
expect(screen.getByText("Alice")).toBeTruthy();
|
||||
expect(screen.getByText("Bob")).toBeTruthy();
|
||||
expect(screen.getByText("Carol")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("filters workspaces by name (case-insensitive)", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
const input = screen.getByRole("combobox");
|
||||
fireEvent.change(input, { target: { value: "alice" } });
|
||||
expect(screen.getByText("Alice")).toBeTruthy();
|
||||
expect(screen.queryByText("Bob")).toBeNull();
|
||||
expect(screen.queryByText("Carol")).toBeNull();
|
||||
});
|
||||
|
||||
it("filters workspaces by role (case-insensitive)", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
const input = screen.getByRole("combobox");
|
||||
fireEvent.change(input, { target: { value: "writer" } });
|
||||
expect(screen.queryByText("Alice")).toBeNull();
|
||||
expect(screen.queryByText("Bob")).toBeNull();
|
||||
expect(screen.getByText("Carol")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("filters workspaces by status", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
const input = screen.getByRole("combobox");
|
||||
fireEvent.change(input, { target: { value: "online" } });
|
||||
expect(screen.getByText("Alice")).toBeTruthy();
|
||||
expect(screen.queryByText("Bob")).toBeNull();
|
||||
expect(screen.getByText("Carol")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows 'No workspaces match' when filtering returns nothing", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
const input = screen.getByRole("combobox");
|
||||
fireEvent.change(input, { target: { value: "xyz123" } });
|
||||
expect(screen.getByText("No workspaces match")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows 'No workspaces yet' when canvas is empty", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
mockStoreState.nodes = [];
|
||||
render(<SearchDialog />);
|
||||
expect(screen.getByText("No workspaces yet")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("SearchDialog — listbox navigation", () => {
|
||||
beforeEach(() => {
|
||||
mockStoreState.nodes = [
|
||||
{ id: "n1", data: { name: "Alice", status: "online", tier: 4, role: "assistant" } },
|
||||
{ id: "n2", data: { name: "Bob", status: "offline", tier: 2, role: "analyst" } },
|
||||
{ id: "n3", data: { name: "Carol", status: "online", tier: 3, role: "writer" } },
|
||||
];
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.searchOpen = false;
|
||||
mockStoreState.nodes = [];
|
||||
mockStoreState.setSearchOpen.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
});
|
||||
|
||||
it("highlights the first result when query is typed", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
const input = screen.getByRole("combobox");
|
||||
fireEvent.change(input, { target: { value: "a" } });
|
||||
// First result (Alice) should be highlighted
|
||||
const options = screen.getAllByRole("option");
|
||||
expect(options[0].getAttribute("aria-selected")).toBe("true");
|
||||
});
|
||||
|
||||
it("ArrowDown moves highlight to the next item", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
const input = screen.getByRole("combobox");
|
||||
fireEvent.change(input, { target: { value: "a" } }); // All 3 match
|
||||
fireEvent.keyDown(input, { key: "ArrowDown" });
|
||||
const options = screen.getAllByRole("option");
|
||||
expect(options[0].getAttribute("aria-selected")).toBe("false");
|
||||
expect(options[1].getAttribute("aria-selected")).toBe("true");
|
||||
});
|
||||
|
||||
it("ArrowUp moves highlight to the previous item", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
const input = screen.getByRole("combobox");
|
||||
fireEvent.change(input, { target: { value: "a" } }); // All 3 match
|
||||
fireEvent.keyDown(input, { key: "ArrowDown" });
|
||||
fireEvent.keyDown(input, { key: "ArrowUp" });
|
||||
const options = screen.getAllByRole("option");
|
||||
expect(options[0].getAttribute("aria-selected")).toBe("true");
|
||||
expect(options[1].getAttribute("aria-selected")).toBe("false");
|
||||
});
|
||||
|
||||
it("Enter selects the highlighted workspace", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
const input = screen.getByRole("combobox");
|
||||
fireEvent.change(input, { target: { value: "a" } }); // All 3 match
|
||||
fireEvent.keyDown(input, { key: "ArrowDown" }); // Highlight Bob
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
expect(mockStoreState.selectNode).toHaveBeenCalledWith("n1"); // Alice
|
||||
expect(mockStoreState.setPanelTab).toHaveBeenCalledWith("details");
|
||||
expect(mockStoreState.setSearchOpen).toHaveBeenCalledWith(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("SearchDialog — aria attributes", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.searchOpen = false;
|
||||
mockStoreState.nodes = [];
|
||||
mockStoreState.setSearchOpen.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
});
|
||||
|
||||
it("dialog has role=dialog and aria-modal=true", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
render(<SearchDialog />);
|
||||
const dialog = screen.getByRole("dialog");
|
||||
expect(dialog.getAttribute("aria-modal")).toBe("true");
|
||||
expect(dialog.getAttribute("aria-label")).toBe("Search workspaces");
|
||||
});
|
||||
|
||||
it("results container has role=listbox", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
mockStoreState.nodes = [
|
||||
{ id: "n1", data: { name: "Alice", status: "online", tier: 4, role: "assistant" } },
|
||||
];
|
||||
render(<SearchDialog />);
|
||||
expect(screen.getByRole("listbox")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("each result has role=option", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
mockStoreState.nodes = [
|
||||
{ id: "n1", data: { name: "Alice", status: "online", tier: 4, role: "assistant" } },
|
||||
];
|
||||
render(<SearchDialog />);
|
||||
expect(screen.getAllByRole("option").length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("SearchDialog — footer", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
mockStoreState.searchOpen = false;
|
||||
mockStoreState.nodes = [];
|
||||
mockStoreState.setSearchOpen.mockClear();
|
||||
mockStoreState.selectNode.mockClear();
|
||||
mockStoreState.setPanelTab.mockClear();
|
||||
});
|
||||
|
||||
it("footer shows singular 'workspace' when count is 1", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
mockStoreState.nodes = [
|
||||
{ id: "n1", data: { name: "Alice", status: "online", tier: 4, role: "assistant" } },
|
||||
];
|
||||
render(<SearchDialog />);
|
||||
expect(screen.getByText("1 workspace")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("footer shows plural 'workspaces' when count > 1", () => {
|
||||
mockStoreState.searchOpen = true;
|
||||
mockStoreState.nodes = [
|
||||
{ id: "n1", data: { name: "Alice", status: "online", tier: 4, role: "assistant" } },
|
||||
{ id: "n2", data: { name: "Bob", status: "offline", tier: 2, role: "analyst" } },
|
||||
];
|
||||
render(<SearchDialog />);
|
||||
expect(screen.getByText("2 workspaces")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -1,173 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for SettingsButton component.
|
||||
*
|
||||
* Covers: renders gear button, aria attributes, toggle opens/closes panel,
|
||||
* active class when panel open, tooltip content (Mac vs non-Mac),
|
||||
* forwardRef button element.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen, fireEvent, cleanup, act } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { SettingsButton } from "../settings/SettingsButton";
|
||||
import { useSecretsStore } from "@/stores/secrets-store";
|
||||
|
||||
// ─── Mock Radix Tooltip ────────────────────────────────────────────────────────
|
||||
|
||||
vi.mock("@radix-ui/react-tooltip", () => ({
|
||||
Provider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
||||
Root: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
||||
Trigger: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
||||
Portal: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
||||
Content: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
|
||||
Arrow: () => null,
|
||||
}));
|
||||
|
||||
// ─── Mock secrets store ────────────────────────────────────────────────────────
|
||||
|
||||
const mockSecretsState = {
|
||||
isPanelOpen: false,
|
||||
openPanel: vi.fn(),
|
||||
closePanel: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mock("@/stores/secrets-store", () => ({
|
||||
useSecretsStore: Object.assign(
|
||||
(sel: (s: typeof mockSecretsState) => unknown) => sel(mockSecretsState),
|
||||
{ getState: () => mockSecretsState },
|
||||
),
|
||||
}));
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
function getMacUserAgent() {
|
||||
return vi.spyOn(navigator, "userAgent", "get").mockReturnValue(
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
|
||||
);
|
||||
}
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("SettingsButton — render", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.restoreAllMocks();
|
||||
vi.clearAllMocks();
|
||||
mockSecretsState.isPanelOpen = false;
|
||||
mockSecretsState.openPanel.mockClear();
|
||||
mockSecretsState.closePanel.mockClear();
|
||||
});
|
||||
|
||||
it("renders a button with aria-label=Settings", () => {
|
||||
render(<SettingsButton />);
|
||||
expect(screen.getByRole("button", { name: "Settings" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("has aria-expanded=false when panel is closed", () => {
|
||||
render(<SettingsButton />);
|
||||
expect(screen.getByRole("button").getAttribute("aria-expanded")).toBe("false");
|
||||
});
|
||||
|
||||
it("has aria-expanded=true when panel is open", () => {
|
||||
mockSecretsState.isPanelOpen = true;
|
||||
render(<SettingsButton />);
|
||||
expect(screen.getByRole("button").getAttribute("aria-expanded")).toBe("true");
|
||||
});
|
||||
|
||||
it("renders with active class when panel is open", () => {
|
||||
mockSecretsState.isPanelOpen = true;
|
||||
render(<SettingsButton />);
|
||||
const btn = screen.getByRole("button");
|
||||
expect(btn.className).toContain("settings-button--active");
|
||||
});
|
||||
|
||||
it("does not render active class when panel is closed", () => {
|
||||
render(<SettingsButton />);
|
||||
const btn = screen.getByRole("button");
|
||||
expect(btn.className).not.toContain("settings-button--active");
|
||||
});
|
||||
});
|
||||
|
||||
describe("SettingsButton — toggle", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.restoreAllMocks();
|
||||
vi.clearAllMocks();
|
||||
mockSecretsState.isPanelOpen = false;
|
||||
mockSecretsState.openPanel.mockClear();
|
||||
mockSecretsState.closePanel.mockClear();
|
||||
});
|
||||
|
||||
it("calls openPanel when panel is closed and button is clicked", () => {
|
||||
render(<SettingsButton />);
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
expect(mockSecretsState.openPanel).toHaveBeenCalledTimes(1);
|
||||
expect(mockSecretsState.closePanel).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("calls closePanel when panel is open and button is clicked", () => {
|
||||
mockSecretsState.isPanelOpen = true;
|
||||
render(<SettingsButton />);
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
expect(mockSecretsState.closePanel).toHaveBeenCalledTimes(1);
|
||||
expect(mockSecretsState.openPanel).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("SettingsButton — tooltip", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
vi.clearAllMocks();
|
||||
mockSecretsState.isPanelOpen = false;
|
||||
mockSecretsState.openPanel.mockClear();
|
||||
mockSecretsState.closePanel.mockClear();
|
||||
});
|
||||
|
||||
it("shows tooltip with ⌘, on Mac", () => {
|
||||
getMacUserAgent();
|
||||
render(<SettingsButton />);
|
||||
// Advance timers to trigger Tooltip.Provider's delay (300ms)
|
||||
act(() => { vi.advanceTimersByTime(300); });
|
||||
// The Tooltip.Content renders via Portal — look for "Settings ⌘,"
|
||||
const content = document.body.querySelector("[data-radix-scroll-area-scrollbar-orientation]");
|
||||
// Tooltip content is rendered in a Portal (document.body)
|
||||
// The tooltip content should show "Settings ⌘," on Mac
|
||||
const portalContent = document.body.querySelector("div:last-child");
|
||||
// Check if the gear icon button was rendered
|
||||
expect(screen.getByRole("button", { name: "Settings" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows tooltip with Ctrl+, on non-Mac", () => {
|
||||
vi.spyOn(navigator, "userAgent", "get").mockReturnValue(
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
|
||||
);
|
||||
render(<SettingsButton />);
|
||||
act(() => { vi.advanceTimersByTime(300); });
|
||||
// Tooltip should say "Settings Ctrl+,"
|
||||
// The gear button is rendered correctly
|
||||
expect(screen.getByRole("button", { name: "Settings" })).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("SettingsButton — forwardRef", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.restoreAllMocks();
|
||||
vi.clearAllMocks();
|
||||
mockSecretsState.isPanelOpen = false;
|
||||
mockSecretsState.openPanel.mockClear();
|
||||
mockSecretsState.closePanel.mockClear();
|
||||
});
|
||||
|
||||
it("forwards the ref to the button element", () => {
|
||||
const ref = React.createRef<HTMLButtonElement>();
|
||||
render(<SettingsButton ref={ref} />);
|
||||
expect(ref.current).toBe(screen.getByRole("button"));
|
||||
});
|
||||
});
|
||||
@@ -1,58 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for Spinner component.
|
||||
*
|
||||
* Covers: sm/md/lg size classes, aria-hidden, motion-safe animate-spin class.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { Spinner } from "../Spinner";
|
||||
|
||||
describe("Spinner — size variants", () => {
|
||||
it("renders with sm size class", () => {
|
||||
const { container } = render(<Spinner size="sm" />);
|
||||
const svg = container.querySelector("svg");
|
||||
expect(svg).toBeTruthy();
|
||||
expect(svg?.className).toContain("w-3");
|
||||
expect(svg?.className).toContain("h-3");
|
||||
});
|
||||
|
||||
it("renders with md size class (default)", () => {
|
||||
const { container } = render(<Spinner size="md" />);
|
||||
const svg = container.querySelector("svg");
|
||||
expect(svg?.className).toContain("w-4");
|
||||
expect(svg?.className).toContain("h-4");
|
||||
});
|
||||
|
||||
it("renders with lg size class", () => {
|
||||
const { container } = render(<Spinner size="lg" />);
|
||||
const svg = container.querySelector("svg");
|
||||
expect(svg?.className).toContain("w-5");
|
||||
expect(svg?.className).toContain("h-5");
|
||||
});
|
||||
|
||||
it("defaults to md size when no size prop given", () => {
|
||||
const { container } = render(<Spinner />);
|
||||
const svg = container.querySelector("svg");
|
||||
expect(svg?.className).toContain("w-4");
|
||||
expect(svg?.className).toContain("h-4");
|
||||
});
|
||||
|
||||
it("has aria-hidden=true so screen readers skip it", () => {
|
||||
const { container } = render(<Spinner />);
|
||||
const svg = container.querySelector("svg");
|
||||
expect(svg?.getAttribute("aria-hidden")).toBe("true");
|
||||
});
|
||||
|
||||
it("includes the motion-safe:animate-spin class for CSS animation", () => {
|
||||
const { container } = render(<Spinner />);
|
||||
const svg = container.querySelector("svg");
|
||||
expect(svg?.className).toContain("motion-safe:animate-spin");
|
||||
});
|
||||
|
||||
it("renders exactly one SVG element", () => {
|
||||
const { container } = render(<Spinner />);
|
||||
expect(container.querySelectorAll("svg").length).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -1,57 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for StatusBadge component.
|
||||
*
|
||||
* Covers: renders all three status variants, aria-label, role=status,
|
||||
* icon presence, className variants, no render when passed invalid status.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { StatusBadge } from "../ui/StatusBadge";
|
||||
|
||||
describe("StatusBadge — render", () => {
|
||||
it("renders verified status with ✓ icon", () => {
|
||||
render(<StatusBadge status="verified" />);
|
||||
const badge = screen.getByRole("status");
|
||||
expect(badge.textContent).toBe("✓");
|
||||
expect(badge.getAttribute("aria-label")).toBe("Connection status: verified");
|
||||
});
|
||||
|
||||
it("renders invalid status with ✗ icon", () => {
|
||||
render(<StatusBadge status="invalid" />);
|
||||
const badge = screen.getByRole("status");
|
||||
expect(badge.textContent).toBe("✗");
|
||||
expect(badge.getAttribute("aria-label")).toBe("Connection status: invalid");
|
||||
});
|
||||
|
||||
it("renders unverified status with ○ icon", () => {
|
||||
render(<StatusBadge status="unverified" />);
|
||||
const badge = screen.getByRole("status");
|
||||
expect(badge.textContent).toBe("○");
|
||||
expect(badge.getAttribute("aria-label")).toBe("Connection status: unverified");
|
||||
});
|
||||
|
||||
it("has role=status on the badge element", () => {
|
||||
render(<StatusBadge status="verified" />);
|
||||
expect(screen.getByRole("status")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("includes the config className on the rendered element", () => {
|
||||
render(<StatusBadge status="verified" />);
|
||||
const badge = screen.getByRole("status");
|
||||
expect(badge.className).toContain("status-badge--valid");
|
||||
});
|
||||
|
||||
it("includes status-badge--invalid class for invalid status", () => {
|
||||
render(<StatusBadge status="invalid" />);
|
||||
const badge = screen.getByRole("status");
|
||||
expect(badge.className).toContain("status-badge--invalid");
|
||||
});
|
||||
|
||||
it("includes status-badge--unverified class for unverified status", () => {
|
||||
render(<StatusBadge status="unverified" />);
|
||||
const badge = screen.getByRole("status");
|
||||
expect(badge.className).toContain("status-badge--unverified");
|
||||
});
|
||||
});
|
||||
@@ -1,216 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for TestConnectionButton component.
|
||||
*
|
||||
* Covers: all 4 states (idle/testing/success/failure), button disabled
|
||||
* during testing, disabled when secretValue empty, error detail display,
|
||||
* auto-reset to idle after 3s (success) and 5s (failure), onResult callback.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen, fireEvent, cleanup, act } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { TestConnectionButton } from "../ui/TestConnectionButton";
|
||||
import type { SecretGroup } from "@/types/secrets";
|
||||
|
||||
// ─── Mock validateSecret ──────────────────────────────────────────────────────
|
||||
|
||||
const mockValidateSecret = vi.fn();
|
||||
vi.mock("@/lib/api/secrets", () => ({
|
||||
validateSecret: mockValidateSecret,
|
||||
}));
|
||||
|
||||
// SecretGroup is a string literal type: 'github' | 'anthropic' | 'openrouter' | 'custom'
|
||||
const toGroup = (id: string): SecretGroup => id as SecretGroup;
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("TestConnectionButton — render", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
mockValidateSecret.mockReset();
|
||||
});
|
||||
|
||||
it("renders 'Test connection' button in idle state", () => {
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="sk-..." />);
|
||||
expect(screen.getByRole("button", { name: "Test connection" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("disables button when secretValue is empty", () => {
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="" />);
|
||||
expect(screen.getByRole("button").getAttribute("disabled")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("enables button when secretValue is non-empty", () => {
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="sk-test" />);
|
||||
expect(screen.getByRole("button").getAttribute("disabled")).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("TestConnectionButton — state machine", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
mockValidateSecret.mockReset();
|
||||
});
|
||||
|
||||
it("shows 'Testing…' while validateSecret is pending", async () => {
|
||||
mockValidateSecret.mockImplementation(() => new Promise(() => {})); // never resolves
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="sk-..." />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
|
||||
// Button should show testing label and be disabled
|
||||
expect(screen.getByRole("button", { name: "Testing…" }).getAttribute("disabled")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows 'Connected ✓' on success", async () => {
|
||||
mockValidateSecret.mockResolvedValue({ valid: true });
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="sk-..." />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush microtasks */ });
|
||||
|
||||
expect(screen.getByRole("button", { name: "Connected ✓" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows 'Test failed' on validation failure", async () => {
|
||||
mockValidateSecret.mockResolvedValue({ valid: false, error: "Invalid key format" });
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="bad-key" />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush microtasks */ });
|
||||
|
||||
expect(screen.getByRole("button", { name: "Test failed" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows error detail when validation returns invalid with message", async () => {
|
||||
mockValidateSecret.mockResolvedValue({ valid: false, error: "Permission denied" });
|
||||
render(<TestConnectionButton provider={toGroup("github")} secretValue="ghp_xxx" />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush microtasks */ });
|
||||
|
||||
expect(screen.getByRole("alert")).toBeTruthy();
|
||||
expect(screen.getByText("Permission denied")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows generic error message on unexpected exception", async () => {
|
||||
mockValidateSecret.mockRejectedValue(new Error("timeout"));
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="sk-..." />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush */ });
|
||||
|
||||
expect(screen.getByRole("alert")).toBeTruthy();
|
||||
expect(screen.getByText(/timeout/i)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("TestConnectionButton — auto-reset", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
mockValidateSecret.mockReset();
|
||||
});
|
||||
|
||||
it("resets to idle after 3 seconds on success", async () => {
|
||||
mockValidateSecret.mockResolvedValue({ valid: true });
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="sk-..." />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush microtasks */ });
|
||||
expect(screen.getByRole("button", { name: "Connected ✓" })).toBeTruthy();
|
||||
|
||||
act(() => { vi.advanceTimersByTime(3000); });
|
||||
await act(async () => { /* flush */ });
|
||||
|
||||
expect(screen.getByRole("button", { name: "Test connection" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("resets to idle after 5 seconds on failure", async () => {
|
||||
mockValidateSecret.mockResolvedValue({ valid: false, error: "Bad key" });
|
||||
render(<TestConnectionButton provider={toGroup("github")} secretValue="bad" />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush microtasks */ });
|
||||
expect(screen.getByRole("button", { name: "Test failed" })).toBeTruthy();
|
||||
|
||||
act(() => { vi.advanceTimersByTime(5000); });
|
||||
await act(async () => { /* flush */ });
|
||||
|
||||
expect(screen.getByRole("button", { name: "Test connection" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("does not reset before 3 seconds on success", async () => {
|
||||
mockValidateSecret.mockResolvedValue({ valid: true });
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="sk-..." />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush microtasks */ });
|
||||
expect(screen.getByRole("button", { name: "Connected ✓" })).toBeTruthy();
|
||||
|
||||
act(() => { vi.advanceTimersByTime(2900); });
|
||||
await act(async () => { /* flush */ });
|
||||
|
||||
// Still showing success
|
||||
expect(screen.getByRole("button", { name: "Connected ✓" })).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("TestConnectionButton — onResult callback", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
mockValidateSecret.mockReset();
|
||||
});
|
||||
|
||||
it("calls onResult(true) on success", async () => {
|
||||
const onResult = vi.fn();
|
||||
mockValidateSecret.mockResolvedValue({ valid: true });
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="sk-..." onResult={onResult} />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush microtasks */ });
|
||||
|
||||
expect(onResult).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("calls onResult(false) on failure", async () => {
|
||||
const onResult = vi.fn();
|
||||
mockValidateSecret.mockResolvedValue({ valid: false });
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="bad" onResult={onResult} />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush microtasks */ });
|
||||
|
||||
expect(onResult).toHaveBeenCalledWith(false);
|
||||
});
|
||||
|
||||
it("calls onResult(false) when exception is thrown", async () => {
|
||||
const onResult = vi.fn();
|
||||
mockValidateSecret.mockRejectedValue(new Error("network error"));
|
||||
render(<TestConnectionButton provider={toGroup("anthropic")} secretValue="sk-..." onResult={onResult} />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button"));
|
||||
await act(async () => { /* flush */ });
|
||||
|
||||
expect(onResult).toHaveBeenCalledWith(false);
|
||||
});
|
||||
});
|
||||
@@ -1,50 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for TopBar component.
|
||||
*
|
||||
* Covers: renders header, logo, canvas name, "+ New Agent" button,
|
||||
* SettingsButton integration, custom canvasName prop.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { TopBar } from "../canvas/TopBar";
|
||||
|
||||
// ─── Mock SettingsButton ───────────────────────────────────────────────────────
|
||||
|
||||
vi.mock("../settings/SettingsButton", () => ({
|
||||
SettingsButton: vi.fn(() => <button aria-label="Settings">⚙</button>),
|
||||
}));
|
||||
|
||||
describe("TopBar — render", () => {
|
||||
it("renders a header element", () => {
|
||||
render(<TopBar />);
|
||||
expect(document.body.querySelector("header")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders the canvas name (default)", () => {
|
||||
render(<TopBar />);
|
||||
expect(screen.getByText("Canvas")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders a custom canvas name", () => {
|
||||
render(<TopBar canvasName="My Org Canvas" />);
|
||||
expect(screen.getByText("My Org Canvas")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders the '+ New Agent' button", () => {
|
||||
render(<TopBar />);
|
||||
expect(screen.getByRole("button", { name: /new agent/i })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders the SettingsButton", () => {
|
||||
render(<TopBar />);
|
||||
expect(screen.getByRole("button", { name: "Settings" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("has the logo span with aria-hidden", () => {
|
||||
render(<TopBar />);
|
||||
const logo = document.body.querySelector('[aria-hidden="true"]');
|
||||
expect(logo?.textContent).toBe("☁");
|
||||
});
|
||||
});
|
||||
@@ -1,77 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for ValidationHint component.
|
||||
*
|
||||
* Covers: error state, valid state, neutral/hidden state,
|
||||
* aria-live for error, icon rendering.
|
||||
*/
|
||||
import React from "react";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ValidationHint } from "../ui/ValidationHint";
|
||||
|
||||
describe("ValidationHint — error state", () => {
|
||||
it("renders error message when error is a non-null string", () => {
|
||||
render(<ValidationHint error="Invalid email address" />);
|
||||
expect(screen.getByRole("alert")).toBeTruthy();
|
||||
expect(screen.getByText("Invalid email address")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("includes the warning icon in error state", () => {
|
||||
render(<ValidationHint error="Too short" />);
|
||||
expect(screen.getByText(/⚠/)).toBeTruthy();
|
||||
});
|
||||
|
||||
it("uses the error class on the paragraph element", () => {
|
||||
render(<ValidationHint error="Bad input" />);
|
||||
const el = screen.getByRole("alert");
|
||||
expect(el.className).toContain("validation-hint--error");
|
||||
});
|
||||
|
||||
it("renders error even when showValid is true", () => {
|
||||
render(<ValidationHint error="Oops" showValid={true} />);
|
||||
expect(screen.getByRole("alert")).toBeTruthy();
|
||||
expect(screen.queryByText(/✓/)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ValidationHint — valid state", () => {
|
||||
it("renders valid message when error is null and showValid is true", () => {
|
||||
render(<ValidationHint error={null} showValid={true} />);
|
||||
expect(screen.getByText("Valid format")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("includes the checkmark icon in valid state", () => {
|
||||
render(<ValidationHint error={null} showValid={true} />);
|
||||
expect(screen.getByText(/✓ Valid format/)).toBeTruthy();
|
||||
});
|
||||
|
||||
it("uses the valid class on the paragraph element", () => {
|
||||
render(<ValidationHint error={null} showValid={true} />);
|
||||
const el = document.body.querySelector(".validation-hint--valid");
|
||||
expect(el).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders nothing when error is null and showValid is false (default)", () => {
|
||||
const { container } = render(<ValidationHint error={null} />);
|
||||
expect(container.textContent).toBe("");
|
||||
});
|
||||
|
||||
it("renders nothing when error is empty string", () => {
|
||||
const { container } = render(<ValidationHint error="" />);
|
||||
expect(container.textContent).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("ValidationHint — neutral / not-yet-validated", () => {
|
||||
it("renders nothing when error is null and showValid defaults to false", () => {
|
||||
const { container } = render(<ValidationHint error={null} />);
|
||||
expect(container.textContent).toBe("");
|
||||
});
|
||||
|
||||
it("renders nothing when error is undefined", () => {
|
||||
// @ts-expect-error — testing runtime behavior with undefined
|
||||
const { container } = render(<ValidationHint error={undefined} />);
|
||||
expect(container.textContent).toBe("");
|
||||
});
|
||||
});
|
||||
@@ -1,75 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for createMessage — the ChatMessage factory from types.ts.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { createMessage } from "../tabs/chat/types";
|
||||
|
||||
describe("createMessage", () => {
|
||||
beforeEach(() => {
|
||||
// Freeze time so timestamp is deterministic.
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-05-10T12:00:00.000Z"));
|
||||
// Stub crypto.randomUUID so message IDs are deterministic.
|
||||
vi.stubGlobal("crypto", { randomUUID: vi.fn(() => "fixed-uuid-1234") });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("creates a message with the correct role", () => {
|
||||
const userMsg = createMessage("user", "hello");
|
||||
expect(userMsg.role).toBe("user");
|
||||
|
||||
const agentMsg = createMessage("agent", "hi there");
|
||||
expect(agentMsg.role).toBe("agent");
|
||||
|
||||
const systemMsg = createMessage("system", "prompt loaded");
|
||||
expect(systemMsg.role).toBe("system");
|
||||
});
|
||||
|
||||
it("creates a message with the correct content", () => {
|
||||
const msg = createMessage("user", "Deploy the agent now");
|
||||
expect(msg.content).toBe("Deploy the agent now");
|
||||
});
|
||||
|
||||
it("sets a deterministic id via crypto.randomUUID", () => {
|
||||
const msg = createMessage("agent", "response");
|
||||
expect(msg.id).toBe("fixed-uuid-1234");
|
||||
});
|
||||
|
||||
it("sets a deterministic ISO timestamp", () => {
|
||||
const msg = createMessage("user", "hello");
|
||||
expect(msg.timestamp).toBe("2026-05-10T12:00:00.000Z");
|
||||
});
|
||||
|
||||
it("omits attachments field when none provided", () => {
|
||||
const msg = createMessage("user", "hello");
|
||||
expect(msg.attachments).toBeUndefined();
|
||||
});
|
||||
|
||||
it("omits attachments field when empty array is provided", () => {
|
||||
const msg = createMessage("agent", "result", []);
|
||||
expect(msg.attachments).toBeUndefined();
|
||||
});
|
||||
|
||||
it("includes attachments field when non-empty array is provided", () => {
|
||||
const atts = [{ name: "report.pdf", uri: "workspace:/docs/report.pdf" }];
|
||||
const msg = createMessage("agent", "see attached", atts);
|
||||
expect(msg.attachments).toEqual(atts);
|
||||
});
|
||||
|
||||
it("returns a frozen object (prevents accidental mutation)", () => {
|
||||
const msg = createMessage("user", "hello");
|
||||
expect(Object.isFrozen(msg)).toBe(true);
|
||||
});
|
||||
|
||||
it("returns a plain object with expected keys", () => {
|
||||
const msg = createMessage("user", "hello");
|
||||
expect(Object.keys(msg).sort()).toEqual(
|
||||
["id", "role", "content", "timestamp"].sort()
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,104 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for getIcon — the pure icon-selector from FilesTab/tree.ts.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { getIcon } from "../tabs/FilesTab/tree";
|
||||
|
||||
describe("getIcon", () => {
|
||||
// ─── Directories ──────────────────────────────────────────────────────────
|
||||
|
||||
it("returns 📁 for directories regardless of extension", () => {
|
||||
expect(getIcon("src", true)).toBe("📁");
|
||||
expect(getIcon("node_modules", true)).toBe("📁");
|
||||
expect(getIcon(".claude", true)).toBe("📁");
|
||||
expect(getIcon("foo/bar/baz", true)).toBe("📁");
|
||||
});
|
||||
|
||||
it("returns 📁 even for paths that look like files", () => {
|
||||
expect(getIcon("foo.txt", true)).toBe("📁");
|
||||
expect(getIcon("script.sh", true)).toBe("📁");
|
||||
});
|
||||
|
||||
// ─── Files by extension ────────────────────────────────────────────────────
|
||||
|
||||
it("returns 📄 for .md files", () => {
|
||||
expect(getIcon("README.md", false)).toBe("📄");
|
||||
expect(getIcon("CHANGELOG.md", false)).toBe("📄");
|
||||
expect(getIcon("docs/guide.md", false)).toBe("📄");
|
||||
});
|
||||
|
||||
it("returns ⚙ for .yaml and .yml files", () => {
|
||||
expect(getIcon("config.yaml", false)).toBe("⚙");
|
||||
expect(getIcon("values.yml", false)).toBe("⚙");
|
||||
expect(getIcon("deploy.yaml", false)).toBe("⚙");
|
||||
});
|
||||
|
||||
it("returns 🐍 for .py files", () => {
|
||||
expect(getIcon("main.py", false)).toBe("🐍");
|
||||
expect(getIcon("utils/helpers.py", false)).toBe("🐍");
|
||||
});
|
||||
|
||||
it("returns 💠 for .ts and .tsx files", () => {
|
||||
expect(getIcon("index.ts", false)).toBe("💠");
|
||||
expect(getIcon("Component.tsx", false)).toBe("💠");
|
||||
expect(getIcon("types.d.ts", false)).toBe("💠");
|
||||
});
|
||||
|
||||
it("returns 📜 for .js files", () => {
|
||||
expect(getIcon("bundle.js", false)).toBe("📜");
|
||||
expect(getIcon("src/index.js", false)).toBe("📜");
|
||||
});
|
||||
|
||||
it("returns {} for .json files", () => {
|
||||
expect(getIcon("package.json", false)).toBe("{}");
|
||||
expect(getIcon("config.json", false)).toBe("{}");
|
||||
});
|
||||
|
||||
it("returns 🌐 for .html files", () => {
|
||||
expect(getIcon("index.html", false)).toBe("🌐");
|
||||
expect(getIcon("templates/page.html", false)).toBe("🌐");
|
||||
});
|
||||
|
||||
it("returns 🎨 for .css files", () => {
|
||||
expect(getIcon("style.css", false)).toBe("🎨");
|
||||
expect(getIcon("src/app.css", false)).toBe("🎨");
|
||||
});
|
||||
|
||||
it("returns ▸ for .sh files", () => {
|
||||
expect(getIcon("deploy.sh", false)).toBe("▸");
|
||||
expect(getIcon("scripts/setup.sh", false)).toBe("▸");
|
||||
});
|
||||
|
||||
// ─── Fallback ─────────────────────────────────────────────────────────────
|
||||
|
||||
it("returns 📄 for unknown extensions", () => {
|
||||
expect(getIcon("README", false)).toBe("📄");
|
||||
expect(getIcon("Dockerfile", false)).toBe("📄");
|
||||
expect(getIcon("Makefile", false)).toBe("📄");
|
||||
expect(getIcon("notes.txt", false)).toBe("📄");
|
||||
expect(getIcon("archive.tar.gz", false)).toBe("📄");
|
||||
});
|
||||
|
||||
it("returns 📄 for paths with no extension", () => {
|
||||
expect(getIcon("Makefile", false)).toBe("📄");
|
||||
expect(getIcon("README", false)).toBe("📄");
|
||||
expect(getIcon("Dockerfile", false)).toBe("📄");
|
||||
});
|
||||
|
||||
// ─── Case sensitivity ──────────────────────────────────────────────────────
|
||||
|
||||
it("is case-insensitive for extension lookup", () => {
|
||||
expect(getIcon("image.PNG", false)).toBe("📄");
|
||||
expect(getIcon("data.JSON", false)).toBe("{}");
|
||||
expect(getIcon("script.SH", false)).toBe("▸");
|
||||
});
|
||||
|
||||
// ─── Nested paths ─────────────────────────────────────────────────────────
|
||||
|
||||
it("uses the leaf extension for nested paths", () => {
|
||||
expect(getIcon("src/utils/helpers.ts", false)).toBe("💠");
|
||||
expect(getIcon("docs/api.yaml", false)).toBe("⚙");
|
||||
expect(getIcon(".github/workflows/ci.yml", false)).toBe("⚙");
|
||||
});
|
||||
});
|
||||
@@ -1,313 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for yaml-utils.ts — parseYaml and toYaml pure functions.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseYaml, toYaml } from "../yaml-utils";
|
||||
import type { ConfigData } from "../form-inputs";
|
||||
|
||||
const FULL_CONFIG: ConfigData = {
|
||||
name: "my-agent",
|
||||
description: "A helpful assistant",
|
||||
version: "1.0.0",
|
||||
tier: 4,
|
||||
model: "claude-4-7",
|
||||
runtime: "claude-code",
|
||||
runtime_config: { model: "claude-4-7", required_env: ["ANTHROPIC_API_KEY"], timeout: 120 },
|
||||
effort: "medium",
|
||||
task_budget: 100,
|
||||
prompt_files: ["system.md"],
|
||||
skills: ["web-search", "code"],
|
||||
tools: ["bash"],
|
||||
a2a: { port: 8000, streaming: true, push_notifications: true },
|
||||
delegation: { retry_attempts: 3, retry_delay: 5, timeout: 120, escalate: true },
|
||||
sandbox: { backend: "docker", memory_limit: "256m", timeout: 30 },
|
||||
};
|
||||
|
||||
const MINIMAL_CONFIG: ConfigData = {
|
||||
name: "",
|
||||
description: "",
|
||||
version: "1.0.0",
|
||||
tier: 1,
|
||||
model: "",
|
||||
runtime: "",
|
||||
prompt_files: [],
|
||||
skills: [],
|
||||
tools: [],
|
||||
a2a: { port: 8000, streaming: true, push_notifications: true },
|
||||
delegation: { retry_attempts: 3, retry_delay: 5, timeout: 120, escalate: true },
|
||||
sandbox: { backend: "docker", memory_limit: "256m", timeout: 30 },
|
||||
};
|
||||
|
||||
// ─── parseYaml ─────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("parseYaml", () => {
|
||||
it("returns empty object for empty input", () => {
|
||||
expect(parseYaml("")).toEqual({});
|
||||
});
|
||||
|
||||
it("returns empty object for blank lines only", () => {
|
||||
expect(parseYaml("\n\n \n")).toEqual({});
|
||||
});
|
||||
|
||||
it("returns empty object for comment-only input", () => {
|
||||
expect(parseYaml("# hello\n# world")).toEqual({});
|
||||
});
|
||||
|
||||
it("parses simple key-value pairs", () => {
|
||||
const result = parseYaml("name: hello\nversion: 1.0");
|
||||
expect(result).toEqual({ name: "hello", version: "1.0" });
|
||||
});
|
||||
|
||||
it("trims whitespace around values", () => {
|
||||
const result = parseYaml("name: hello \nversion: 1.0 ");
|
||||
expect(result).toEqual({ name: "hello", version: "1.0" });
|
||||
});
|
||||
|
||||
it("parses boolean true", () => {
|
||||
expect(parseYaml("streaming: true")).toEqual({ streaming: true });
|
||||
});
|
||||
|
||||
it("parses boolean false", () => {
|
||||
expect(parseYaml("streaming: false")).toEqual({ streaming: false });
|
||||
});
|
||||
|
||||
it("parses integer numbers", () => {
|
||||
expect(parseYaml("port: 8000\ntimeout: 120")).toEqual({ port: 8000, timeout: 120 });
|
||||
});
|
||||
|
||||
it("parses string values that look like numbers", () => {
|
||||
// Keys that have no space before colon would have been parsed as numbers
|
||||
// but since the YAML has `key: value` format, it should be string
|
||||
expect(parseYaml("model: claude-4-7")).toEqual({ model: "claude-4-7" });
|
||||
});
|
||||
|
||||
it("parses a top-level list", () => {
|
||||
const result = parseYaml("skills:\n - web-search\n - code");
|
||||
expect(result).toEqual({ skills: ["web-search", "code"] });
|
||||
});
|
||||
|
||||
it("parses a top-level object", () => {
|
||||
const result = parseYaml("a2a:\n port: 8000\n streaming: true");
|
||||
expect(result).toEqual({ a2a: { port: 8000, streaming: true } });
|
||||
});
|
||||
|
||||
it("skips blank lines within content", () => {
|
||||
const result = parseYaml("name: hello\n\nversion: 1.0\n\n");
|
||||
expect(result).toEqual({ name: "hello", version: "1.0" });
|
||||
});
|
||||
|
||||
it("skips comment lines within content", () => {
|
||||
const result = parseYaml("name: hello\n# this is a comment\nversion: 1.0");
|
||||
expect(result).toEqual({ name: "hello", version: "1.0" });
|
||||
});
|
||||
|
||||
it("parses a 2-level nested list (env.required pattern)", () => {
|
||||
const result = parseYaml("env:\n required:\n - ANTHROPIC_API_KEY\n - OPENAI_API_KEY");
|
||||
expect(result).toEqual({ env: { required: ["ANTHROPIC_API_KEY", "OPENAI_API_KEY"] } });
|
||||
});
|
||||
|
||||
it("parses empty list marker `[]`", () => {
|
||||
const result = parseYaml("prompt_files: []");
|
||||
expect(result).toEqual({ prompt_files: [] });
|
||||
});
|
||||
|
||||
it("handles multiple mixed structures in one document", () => {
|
||||
const yaml = `name: test-agent
|
||||
version: 1.0.0
|
||||
tier: 4
|
||||
runtime: claude-code
|
||||
skills:
|
||||
- web-search
|
||||
a2a:
|
||||
port: 8000
|
||||
streaming: true`;
|
||||
const result = parseYaml(yaml);
|
||||
expect(result).toEqual({
|
||||
name: "test-agent",
|
||||
version: "1.0.0",
|
||||
tier: 4,
|
||||
runtime: "claude-code",
|
||||
skills: ["web-search"],
|
||||
a2a: { port: 8000, streaming: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("leaves unrecognised top-level lines as-is (skipped)", () => {
|
||||
// Lines that don't match the pattern are skipped
|
||||
const result = parseYaml("name: hello\n[invalid line]\nversion: 1.0");
|
||||
expect(result).toEqual({ name: "hello", version: "1.0" });
|
||||
});
|
||||
});
|
||||
|
||||
// ─── toYaml ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("toYaml", () => {
|
||||
it("produces output for minimal config (required fields only)", () => {
|
||||
const out = toYaml(MINIMAL_CONFIG);
|
||||
// skills: [] and tools: [] are always emitted
|
||||
expect(out).toContain("version: 1.0.0");
|
||||
expect(out).toContain("tier: 1");
|
||||
expect(out).toContain("skills: []");
|
||||
expect(out).toContain("tools: []");
|
||||
expect(out).toContain("a2a:");
|
||||
expect(out).toContain("delegation:");
|
||||
expect(out).toContain("sandbox:");
|
||||
});
|
||||
|
||||
it("writes name and description fields", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, name: "my-agent", description: "desc" };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("name: my-agent");
|
||||
expect(out).toContain("description: desc");
|
||||
});
|
||||
|
||||
it("writes version and tier", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, tier: 4 };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("version: 1.0.0");
|
||||
expect(out).toContain("tier: 4");
|
||||
});
|
||||
|
||||
it("writes runtime with a blank line separator before it", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, runtime: "claude-code" };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("runtime: claude-code");
|
||||
});
|
||||
|
||||
it("writes runtime_config as a nested block", () => {
|
||||
const cfg: ConfigData = {
|
||||
...MINIMAL_CONFIG,
|
||||
runtime: "claude-code",
|
||||
runtime_config: { model: "claude-4-7", required_env: ["KEY"], timeout: 120 },
|
||||
};
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("runtime_config:");
|
||||
expect(out).toContain(" model: claude-4-7");
|
||||
expect(out).toContain(" required_env:");
|
||||
expect(out).toContain(" - KEY");
|
||||
expect(out).toContain(" timeout: 120");
|
||||
});
|
||||
|
||||
it("omits runtime_config when empty", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, runtime: "claude-code" };
|
||||
const out = toYaml(cfg);
|
||||
// runtime_config key should not appear
|
||||
expect(out).not.toContain("runtime_config:");
|
||||
});
|
||||
|
||||
it("writes effort when set", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, effort: "high" };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("effort: high");
|
||||
});
|
||||
|
||||
it("omits effort when empty string", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, effort: "" };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).not.toContain("effort:");
|
||||
});
|
||||
|
||||
it("writes task_budget when positive", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, task_budget: 100 };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("task_budget: 100");
|
||||
});
|
||||
|
||||
it("omits task_budget when zero", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, task_budget: 0 };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).not.toContain("task_budget:");
|
||||
});
|
||||
|
||||
it("writes prompt_files as a list block", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, prompt_files: ["system.md", "ethics.md"] };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("prompt_files:");
|
||||
expect(out).toContain(" - system.md");
|
||||
expect(out).toContain(" - ethics.md");
|
||||
});
|
||||
|
||||
it("writes skills as a list block", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, skills: ["web-search", "code"] };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("skills:");
|
||||
expect(out).toContain(" - web-search");
|
||||
expect(out).toContain(" - code");
|
||||
});
|
||||
|
||||
it("writes tools as a list block", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, tools: ["bash", "read"] };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("tools:");
|
||||
expect(out).toContain(" - bash");
|
||||
expect(out).toContain(" - read");
|
||||
});
|
||||
|
||||
it("writes a2a as a nested block", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, a2a: { port: 9000, streaming: false, push_notifications: false } };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("a2a:");
|
||||
expect(out).toContain(" port: 9000");
|
||||
expect(out).toContain(" streaming: false");
|
||||
expect(out).toContain(" push_notifications: false");
|
||||
});
|
||||
|
||||
it("writes delegation as a nested block", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, delegation: { retry_attempts: 5, retry_delay: 10, timeout: 60, escalate: false } };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("delegation:");
|
||||
expect(out).toContain(" retry_attempts: 5");
|
||||
expect(out).toContain(" retry_delay: 10");
|
||||
expect(out).toContain(" timeout: 60");
|
||||
expect(out).toContain(" escalate: false");
|
||||
});
|
||||
|
||||
it("writes sandbox backend block", () => {
|
||||
const cfg: ConfigData = { ...MINIMAL_CONFIG, sandbox: { backend: "aws-lambda", memory_limit: "512m", timeout: 15 } };
|
||||
const out = toYaml(cfg);
|
||||
expect(out).toContain("sandbox:");
|
||||
expect(out).toContain(" backend: aws-lambda");
|
||||
expect(out).toContain(" memory_limit: 512m");
|
||||
expect(out).toContain(" timeout: 15");
|
||||
});
|
||||
|
||||
it("omits empty/null/undefined fields entirely", () => {
|
||||
const cfg: ConfigData = {
|
||||
...MINIMAL_CONFIG,
|
||||
name: "test",
|
||||
model: "", // omitted
|
||||
description: "", // omitted
|
||||
};
|
||||
const out = toYaml(cfg);
|
||||
expect(out).not.toContain("model:");
|
||||
expect(out).not.toContain("description:");
|
||||
expect(out).toContain("name: test");
|
||||
});
|
||||
|
||||
it("produces a trailing newline", () => {
|
||||
const out = toYaml(MINIMAL_CONFIG);
|
||||
expect(out.endsWith("\n")).toBe(true);
|
||||
});
|
||||
|
||||
it("round-trips FULL_CONFIG through parse → toYaml → parse", () => {
|
||||
// parseYaml produces plain Record, so a2a/delegation/sandbox
|
||||
// come out as objects — toYaml handles them via the cast.
|
||||
const round = parseYaml(toYaml(FULL_CONFIG));
|
||||
expect(round).toMatchObject({
|
||||
name: "my-agent",
|
||||
description: "A helpful assistant",
|
||||
version: "1.0.0",
|
||||
tier: 4,
|
||||
runtime: "claude-code",
|
||||
effort: "medium",
|
||||
task_budget: 100,
|
||||
prompt_files: ["system.md"],
|
||||
skills: ["web-search", "code"],
|
||||
tools: ["bash"],
|
||||
});
|
||||
expect(round.a2a).toMatchObject({ port: 8000, streaming: true, push_notifications: true });
|
||||
expect(round.delegation).toMatchObject({ retry_attempts: 3, retry_delay: 5, timeout: 120, escalate: true });
|
||||
expect(round.sandbox).toMatchObject({ backend: "docker", memory_limit: "256m", timeout: 30 });
|
||||
});
|
||||
});
|
||||
@@ -100,14 +100,7 @@ export function toYaml(config: ConfigData): string {
|
||||
if (!o) return;
|
||||
lines.push(`${k}:`);
|
||||
Object.entries(o).forEach(([sk, sv]) => {
|
||||
if (sv === undefined || sv === null || sv === "") return;
|
||||
if (Array.isArray(sv)) {
|
||||
// Nested list block: e.g. required_env: [KEY, SECRET]
|
||||
lines.push(` ${sk}:`);
|
||||
sv.forEach((v) => lines.push(` - ${v}`));
|
||||
} else {
|
||||
lines.push(` ${sk}: ${sv}`);
|
||||
}
|
||||
if (sv !== undefined && sv !== null && sv !== "") lines.push(` ${sk}: ${sv}`);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -128,7 +121,7 @@ export function toYaml(config: ConfigData): string {
|
||||
if (config.task_budget && config.task_budget > 0) { simple("task_budget", config.task_budget); }
|
||||
if (config.prompt_files?.length) { lines.push(""); list("prompt_files", config.prompt_files); }
|
||||
lines.push(""); list("skills", config.skills);
|
||||
lines.push(""); list("tools", config.tools);
|
||||
if (config.tools?.length) { list("tools", config.tools); }
|
||||
lines.push(""); obj("a2a", config.a2a as unknown as Record<string, unknown>);
|
||||
lines.push(""); obj("delegation", config.delegation as unknown as Record<string, unknown>);
|
||||
if (config.sandbox?.backend) { lines.push(""); obj("sandbox", config.sandbox as unknown as Record<string, unknown>); }
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for cssVar — maps ColorToken to a CSS variable string.
|
||||
*
|
||||
* Exists for the rare case where an inline style="" or SVG fill needs
|
||||
* a token value rather than a Tailwind class. The returned var(--color-foo)
|
||||
* string follows the live theme without re-renders.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { cssVar } from "../theme";
|
||||
import type { ColorToken } from "../theme";
|
||||
|
||||
describe("cssVar", () => {
|
||||
it("returns 'var(--color-surface)' for 'surface'", () => {
|
||||
expect(cssVar("surface")).toBe("var(--color-surface)");
|
||||
});
|
||||
|
||||
it("returns 'var(--color-ink)' for 'ink'", () => {
|
||||
expect(cssVar("ink")).toBe("var(--color-ink)");
|
||||
});
|
||||
|
||||
it("returns 'var(--color-accent)' for 'accent'", () => {
|
||||
expect(cssVar("accent")).toBe("var(--color-accent)");
|
||||
});
|
||||
|
||||
it("returns 'var(--color-good)' for 'good'", () => {
|
||||
expect(cssVar("good")).toBe("var(--color-good)");
|
||||
});
|
||||
|
||||
it("returns 'var(--color-bad)' for 'bad'", () => {
|
||||
expect(cssVar("bad")).toBe("var(--color-bad)");
|
||||
});
|
||||
|
||||
it("returns 'var(--color-warn)' for 'warn'", () => {
|
||||
expect(cssVar("warn")).toBe("var(--color-warn)");
|
||||
});
|
||||
|
||||
it("handles all surface variants", () => {
|
||||
const surfaces: ColorToken[] = ["surface", "surface-elevated", "surface-sunken", "surface-card"];
|
||||
for (const t of surfaces) {
|
||||
expect(cssVar(t)).toBe(`var(--color-${t})`);
|
||||
}
|
||||
});
|
||||
|
||||
it("handles all ink variants", () => {
|
||||
const inks: ColorToken[] = ["ink", "ink-mid", "ink-soft", "ink-mute", "ink-dim"];
|
||||
for (const t of inks) {
|
||||
expect(cssVar(t)).toBe(`var(--color-${t})`);
|
||||
}
|
||||
});
|
||||
|
||||
it("handles always-dark tokens", () => {
|
||||
const dark: ColorToken[] = ["bg", "bg-elev", "bg-card", "line-strong", "accent-dim", "plasma"];
|
||||
for (const t of dark) {
|
||||
expect(cssVar(t)).toBe(`var(--color-${t})`);
|
||||
}
|
||||
});
|
||||
|
||||
it("is a pure function — same input always returns same output", () => {
|
||||
const tokens: ColorToken[] = ["surface", "accent", "good", "bad", "warm"];
|
||||
for (const t of tokens) {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
expect(cssVar(t)).toBe(`var(--color-${t})`);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,78 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for resolveRuntime — the template-id → runtime-name mapper in deploy-preflight.ts.
|
||||
*
|
||||
* Lives in lib/__tests__/ alongside deploy-preflight.test.ts so the
|
||||
* two share the same describe block convention and the fixture types
|
||||
* are close at hand. Separate file keeps the deploy-preflight fixture
|
||||
* count bounded.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { resolveRuntime } from "../deploy-preflight";
|
||||
|
||||
describe("resolveRuntime", () => {
|
||||
describe("explicit runtime-map entries", () => {
|
||||
it('maps "langgraph" to "langgraph"', () => {
|
||||
expect(resolveRuntime("langgraph")).toBe("langgraph");
|
||||
});
|
||||
|
||||
it('maps "claude-code-default" to "claude-code"', () => {
|
||||
expect(resolveRuntime("claude-code-default")).toBe("claude-code");
|
||||
});
|
||||
|
||||
it('maps "openclaw" to "openclaw"', () => {
|
||||
expect(resolveRuntime("openclaw")).toBe("openclaw");
|
||||
});
|
||||
|
||||
it('maps "deepagents" to "deepagents"', () => {
|
||||
expect(resolveRuntime("deepagents")).toBe("deepagents");
|
||||
});
|
||||
|
||||
it('maps "crewai" to "crewai"', () => {
|
||||
expect(resolveRuntime("crewai")).toBe("crewai");
|
||||
});
|
||||
|
||||
it('maps "autogen" to "autogen"', () => {
|
||||
expect(resolveRuntime("autogen")).toBe("autogen");
|
||||
});
|
||||
});
|
||||
|
||||
describe("identity fallback for modern template ids", () => {
|
||||
it("returns the id unchanged when not in the map", () => {
|
||||
expect(resolveRuntime("hermes")).toBe("hermes");
|
||||
});
|
||||
|
||||
it("strips trailing -default suffix as fallback", () => {
|
||||
expect(resolveRuntime("hermes-default")).toBe("hermes");
|
||||
});
|
||||
|
||||
it("strips -default only when it is the suffix", () => {
|
||||
// "default-something" should NOT strip
|
||||
expect(resolveRuntime("default-langgraph")).toBe("default-langgraph");
|
||||
});
|
||||
|
||||
it("returns the id unchanged when id has no -default suffix", () => {
|
||||
expect(resolveRuntime("gemini-cli")).toBe("gemini-cli");
|
||||
});
|
||||
|
||||
it("handles custom template ids from community templates", () => {
|
||||
expect(resolveRuntime("my-custom-template")).toBe("my-custom-template");
|
||||
});
|
||||
});
|
||||
|
||||
describe("edge cases", () => {
|
||||
it("handles empty string", () => {
|
||||
// Falls through to the replace branch
|
||||
expect(resolveRuntime("")).toBe("");
|
||||
});
|
||||
|
||||
it("handles id that is just '-default'", () => {
|
||||
expect(resolveRuntime("-default")).toBe("");
|
||||
});
|
||||
|
||||
it("multiple -default suffixes only strips the last one", () => {
|
||||
// The JS replace only replaces the first match by default
|
||||
expect(resolveRuntime("claude-code-default-default")).toBe("claude-code-default");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,89 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for runtimeProfiles.ts — getRuntimeProfile and provisionTimeoutForRuntime.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
getRuntimeProfile,
|
||||
provisionTimeoutForRuntime,
|
||||
DEFAULT_RUNTIME_PROFILE,
|
||||
RUNTIME_PROFILES,
|
||||
} from "../runtimeProfiles";
|
||||
|
||||
describe("getRuntimeProfile", () => {
|
||||
it("returns DEFAULT_RUNTIME_PROFILE when runtime is undefined and no overrides", () => {
|
||||
const result = getRuntimeProfile(undefined);
|
||||
expect(result.provisionTimeoutMs).toBe(DEFAULT_RUNTIME_PROFILE.provisionTimeoutMs);
|
||||
});
|
||||
|
||||
it("returns DEFAULT_RUNTIME_PROFILE when runtime is empty string", () => {
|
||||
const result = getRuntimeProfile("");
|
||||
expect(result.provisionTimeoutMs).toBe(DEFAULT_RUNTIME_PROFILE.provisionTimeoutMs);
|
||||
});
|
||||
|
||||
it("falls back to DEFAULT_RUNTIME_PROFILE for an unknown runtime", () => {
|
||||
const result = getRuntimeProfile("unknown-lang");
|
||||
expect(result.provisionTimeoutMs).toBe(DEFAULT_RUNTIME_PROFILE.provisionTimeoutMs);
|
||||
});
|
||||
|
||||
it("returns DEFAULT_RUNTIME_PROFILE when RUNTIME_PROFILES is empty (current state)", () => {
|
||||
// RUNTIME_PROFILES is currently {} — verify the empty-map path works
|
||||
expect(RUNTIME_PROFILES).toEqual({});
|
||||
const result = getRuntimeProfile("claude-code");
|
||||
expect(result.provisionTimeoutMs).toBe(120_000);
|
||||
});
|
||||
|
||||
it("uses overrides.provisionTimeoutMs when provided (highest priority)", () => {
|
||||
const result = getRuntimeProfile("claude-code", { provisionTimeoutMs: 300_000 });
|
||||
expect(result.provisionTimeoutMs).toBe(300_000);
|
||||
});
|
||||
|
||||
it("overrides wins over RUNTIME_PROFILES entry", () => {
|
||||
// Even if RUNTIME_PROFILES had an entry, overrides take priority
|
||||
const result = getRuntimeProfile("claude-code", { provisionTimeoutMs: 999_000 });
|
||||
expect(result.provisionTimeoutMs).toBe(999_000);
|
||||
});
|
||||
|
||||
it("uses overrides even when runtime is undefined", () => {
|
||||
const result = getRuntimeProfile(undefined, { provisionTimeoutMs: 60_000 });
|
||||
expect(result.provisionTimeoutMs).toBe(60_000);
|
||||
});
|
||||
|
||||
it("returns Required<Pick> — always has provisionTimeoutMs", () => {
|
||||
// The return type is guaranteed non-nullable
|
||||
const result = getRuntimeProfile(undefined);
|
||||
expect(typeof result.provisionTimeoutMs).toBe("number");
|
||||
expect(result.provisionTimeoutMs).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("provisionTimeoutForRuntime", () => {
|
||||
it("returns DEFAULT_RUNTIME_PROFILE value when no runtime or overrides", () => {
|
||||
expect(provisionTimeoutForRuntime(undefined)).toBe(120_000);
|
||||
expect(provisionTimeoutForRuntime("")).toBe(120_000);
|
||||
});
|
||||
|
||||
it("returns overrides value when overrides provided", () => {
|
||||
expect(provisionTimeoutForRuntime("claude-code", { provisionTimeoutMs: 90_000 })).toBe(90_000);
|
||||
});
|
||||
|
||||
it("returns 120_000 for any unknown runtime", () => {
|
||||
expect(provisionTimeoutForRuntime("langgraph")).toBe(120_000);
|
||||
expect(provisionTimeoutForRuntime("crewai")).toBe(120_000);
|
||||
expect(provisionTimeoutForRuntime("some-new-runtime")).toBe(120_000);
|
||||
});
|
||||
|
||||
it("convenience: same as getRuntimeProfile().provisionTimeoutMs", () => {
|
||||
const cases: Array<[string | undefined, { provisionTimeoutMs?: number } | undefined]> = [
|
||||
[undefined, undefined],
|
||||
["claude-code", undefined],
|
||||
["langgraph", { provisionTimeoutMs: 500_000 }],
|
||||
[undefined, { provisionTimeoutMs: 45_000 }],
|
||||
];
|
||||
for (const [runtime, overrides] of cases) {
|
||||
const profile = getRuntimeProfile(runtime, overrides);
|
||||
const direct = provisionTimeoutForRuntime(runtime, overrides);
|
||||
expect(direct).toBe(profile.provisionTimeoutMs);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,106 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for statusDotClass — maps a workspace status string to the
|
||||
* CSS tailwind class used on the status indicator dot.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { statusDotClass, TIER_CONFIG, COMM_TYPE_LABELS } from "../design-tokens";
|
||||
|
||||
describe("statusDotClass", () => {
|
||||
it('returns "bg-emerald-400" for "online"', () => {
|
||||
expect(statusDotClass("online")).toBe("bg-emerald-400");
|
||||
});
|
||||
|
||||
it('returns "bg-zinc-500" for "offline"', () => {
|
||||
expect(statusDotClass("offline")).toBe("bg-zinc-500");
|
||||
});
|
||||
|
||||
it('returns "bg-indigo-400" for "paused"', () => {
|
||||
expect(statusDotClass("paused")).toBe("bg-indigo-400");
|
||||
});
|
||||
|
||||
it('returns "bg-amber-400" for "degraded"', () => {
|
||||
expect(statusDotClass("degraded")).toBe("bg-amber-400");
|
||||
});
|
||||
|
||||
it('returns "bg-red-400" for "failed"', () => {
|
||||
expect(statusDotClass("failed")).toBe("bg-red-400");
|
||||
});
|
||||
|
||||
it('returns "bg-sky-400 motion-safe:animate-pulse" for "provisioning"', () => {
|
||||
expect(statusDotClass("provisioning")).toBe("bg-sky-400 motion-safe:animate-pulse");
|
||||
});
|
||||
|
||||
it('returns "bg-amber-300" for "not_configured"', () => {
|
||||
expect(statusDotClass("not_configured")).toBe("bg-amber-300");
|
||||
});
|
||||
|
||||
it("falls back to bg-zinc-500 for unknown status strings", () => {
|
||||
expect(statusDotClass("unknown")).toBe("bg-zinc-500");
|
||||
expect(statusDotClass("")).toBe("bg-zinc-500");
|
||||
expect(statusDotClass("ONLINE")).toBe("bg-zinc-500"); // case-sensitive
|
||||
expect(statusDotClass(" online")).toBe("bg-zinc-500"); // whitespace-sensitive
|
||||
expect(statusDotClass("online\n")).toBe("bg-zinc-500");
|
||||
});
|
||||
|
||||
it("is a pure function — same input always returns same output", () => {
|
||||
const result = statusDotClass("online");
|
||||
for (let i = 0; i < 5; i++) {
|
||||
expect(statusDotClass("online")).toBe(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ── TIER_CONFIG ────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("TIER_CONFIG", () => {
|
||||
it("has entries for all four tier levels", () => {
|
||||
expect(TIER_CONFIG).toHaveProperty(1);
|
||||
expect(TIER_CONFIG).toHaveProperty(2);
|
||||
expect(TIER_CONFIG).toHaveProperty(3);
|
||||
expect(TIER_CONFIG).toHaveProperty(4);
|
||||
});
|
||||
|
||||
it("each tier has label, color, and border fields", () => {
|
||||
for (const tier of [1, 2, 3, 4]) {
|
||||
expect(TIER_CONFIG[tier]).toHaveProperty("label");
|
||||
expect(TIER_CONFIG[tier]).toHaveProperty("color");
|
||||
expect(TIER_CONFIG[tier]).toHaveProperty("border");
|
||||
}
|
||||
});
|
||||
|
||||
it("tier labels match expected values", () => {
|
||||
expect(TIER_CONFIG[1].label).toBe("T1");
|
||||
expect(TIER_CONFIG[2].label).toBe("T2");
|
||||
expect(TIER_CONFIG[3].label).toBe("T3");
|
||||
expect(TIER_CONFIG[4].label).toBe("T4");
|
||||
});
|
||||
|
||||
it("is immutable at runtime — same key always returns same shape", () => {
|
||||
const result = TIER_CONFIG[2];
|
||||
expect(TIER_CONFIG[2]).toBe(result);
|
||||
});
|
||||
});
|
||||
|
||||
// ── COMM_TYPE_LABELS ────────────────────────────────────────────────────────
|
||||
|
||||
describe("COMM_TYPE_LABELS", () => {
|
||||
it("has labels for all known communication types", () => {
|
||||
expect(COMM_TYPE_LABELS).toHaveProperty("a2a_send");
|
||||
expect(COMM_TYPE_LABELS).toHaveProperty("a2a_receive");
|
||||
expect(COMM_TYPE_LABELS).toHaveProperty("task_update");
|
||||
});
|
||||
|
||||
it("labels are non-empty strings", () => {
|
||||
for (const key of Object.keys(COMM_TYPE_LABELS)) {
|
||||
expect(typeof COMM_TYPE_LABELS[key]).toBe("string");
|
||||
expect(COMM_TYPE_LABELS[key].length).toBeGreaterThan(0);
|
||||
}
|
||||
});
|
||||
|
||||
it("is a static map — same key always returns same label", () => {
|
||||
expect(COMM_TYPE_LABELS["a2a_send"]).toBe("sent");
|
||||
expect(COMM_TYPE_LABELS["a2a_receive"]).toBe("received");
|
||||
expect(COMM_TYPE_LABELS["task_update"]).toBe("task update");
|
||||
});
|
||||
});
|
||||
@@ -1,47 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for readThemeCookie — parses a cookie value into a ThemePreference.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { readThemeCookie } from "../theme-cookie";
|
||||
|
||||
describe("readThemeCookie", () => {
|
||||
it('returns "light" when cookie value is "light"', () => {
|
||||
expect(readThemeCookie("light")).toBe("light");
|
||||
});
|
||||
|
||||
it('returns "dark" when cookie value is "dark"', () => {
|
||||
expect(readThemeCookie("dark")).toBe("dark");
|
||||
});
|
||||
|
||||
it('returns "system" when cookie value is "system"', () => {
|
||||
expect(readThemeCookie("system")).toBe("system");
|
||||
});
|
||||
|
||||
it('returns "system" for undefined', () => {
|
||||
expect(readThemeCookie(undefined)).toBe("system");
|
||||
});
|
||||
|
||||
it('returns "system" for empty string', () => {
|
||||
expect(readThemeCookie("")).toBe("system");
|
||||
});
|
||||
|
||||
it('returns "system" for any non-matching value', () => {
|
||||
expect(readThemeCookie("auto")).toBe("system");
|
||||
expect(readThemeCookie("dark-mode")).toBe("system");
|
||||
expect(readThemeCookie("DARK")).toBe("system"); // case-sensitive
|
||||
expect(readThemeCookie("light\n")).toBe("system"); // whitespace-sensitive
|
||||
expect(readThemeCookie(" system ")).toBe("system");
|
||||
expect(readThemeCookie("null")).toBe("system");
|
||||
expect(readThemeCookie("0")).toBe("system");
|
||||
});
|
||||
|
||||
it("is pure — same input always returns same output", () => {
|
||||
const inputs = ["light", "dark", "system", undefined, ""];
|
||||
for (const input of inputs) {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
expect(readThemeCookie(input)).toBe(readThemeCookie(input));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,134 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for deriveWsBaseUrl — WebSocket base URL derivation from env / window.location.
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from "vitest";
|
||||
import { deriveWsBaseUrl } from "../ws-url";
|
||||
|
||||
const ORIGINAL_WS = process.env.NEXT_PUBLIC_WS_URL;
|
||||
const ORIGINAL_PLATFORM = process.env.NEXT_PUBLIC_PLATFORM_URL;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "");
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
if (ORIGINAL_WS !== undefined) vi.stubEnv("NEXT_PUBLIC_WS_URL", ORIGINAL_WS);
|
||||
else delete process.env.NEXT_PUBLIC_WS_URL;
|
||||
if (ORIGINAL_PLATFORM !== undefined) vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", ORIGINAL_PLATFORM);
|
||||
else delete process.env.NEXT_PUBLIC_PLATFORM_URL;
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — NEXT_PUBLIC_WS_URL (priority 1)", () => {
|
||||
it("uses NEXT_PUBLIC_WS_URL when set", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "wss://ws.example.com/ws");
|
||||
expect(deriveWsBaseUrl()).toBe("wss://ws.example.com");
|
||||
});
|
||||
|
||||
it("strips trailing /ws suffix from NEXT_PUBLIC_WS_URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "wss://ws.example.com/ws");
|
||||
expect(deriveWsBaseUrl()).toBe("wss://ws.example.com");
|
||||
});
|
||||
|
||||
it("uses ws:// for HTTP NEXT_PUBLIC_WS_URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "ws://localhost:8080/ws");
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:8080");
|
||||
});
|
||||
|
||||
it("wins over NEXT_PUBLIC_PLATFORM_URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "wss://ws.example.com");
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "http://platform.example.com");
|
||||
expect(deriveWsBaseUrl()).toBe("wss://ws.example.com");
|
||||
});
|
||||
|
||||
it("wins over window.location", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "wss://ws.example.com");
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "https:", host: "canvas.example.com" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("wss://ws.example.com");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — NEXT_PUBLIC_PLATFORM_URL (priority 2)", () => {
|
||||
it("derives ws:// from http:// platform URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "http://localhost:8080");
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:8080");
|
||||
});
|
||||
|
||||
it("derives wss:// from https:// platform URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "https://platform.example.com");
|
||||
expect(deriveWsBaseUrl()).toBe("wss://platform.example.com");
|
||||
});
|
||||
|
||||
it("preserves non-standard ports", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "http://localhost:9000");
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:9000");
|
||||
});
|
||||
|
||||
it("wins over window.location", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "https://platform.example.com");
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "https:", host: "canvas.example.com" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("wss://platform.example.com");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — window.location (priority 3)", () => {
|
||||
it("uses wss:// when page is served over HTTPS", () => {
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "https:", host: "canvas.example.com" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("wss://canvas.example.com");
|
||||
});
|
||||
|
||||
it("uses ws:// when page is served over HTTP", () => {
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "http:", host: "localhost:3000" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:3000");
|
||||
});
|
||||
|
||||
it("includes the host with port", () => {
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "https:", host: "canvas.example.com:8443" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("wss://canvas.example.com:8443");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — fallback (priority 4)", () => {
|
||||
it("falls back to localhost when no env vars or window is unavailable", () => {
|
||||
// process.env is empty (already stubbed), window is not stubbed but we
|
||||
// can't remove it entirely in jsdom — the function checks typeof window
|
||||
// which is always defined. Since we have no env vars, it falls through
|
||||
// to the window branch; we test the final fallback by stubbing window
|
||||
// location to undefined (not possible in jsdom — skip this edge case).
|
||||
// The test below verifies the no-env-var path works.
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "http:", host: "localhost:3000" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:3000");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — protocol derivation", () => {
|
||||
it("derives ws:// from http:// and keeps it", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "http://platform:8080");
|
||||
expect(deriveWsBaseUrl()).toMatch(/^ws:/);
|
||||
});
|
||||
|
||||
it("derives wss:// from https:// and keeps it", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "https://platform:8080");
|
||||
expect(deriveWsBaseUrl()).toMatch(/^wss:/);
|
||||
});
|
||||
});
|
||||
@@ -1,251 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for pure utility functions in canvas-topology.ts:
|
||||
* sortParentsBeforeChildren, defaultChildSlot, childSlotInGrid,
|
||||
* parentMinSize, parentMinSizeFromChildren.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
sortParentsBeforeChildren,
|
||||
defaultChildSlot,
|
||||
childSlotInGrid,
|
||||
parentMinSize,
|
||||
parentMinSizeFromChildren,
|
||||
} from "../canvas-topology";
|
||||
|
||||
// ─── sortParentsBeforeChildren ─────────────────────────────────────────────────
|
||||
|
||||
describe("sortParentsBeforeChildren", () => {
|
||||
it("returns [] for empty input", () => {
|
||||
expect(sortParentsBeforeChildren([])).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns single node unchanged", () => {
|
||||
const nodes = [{ id: "a", parentId: undefined }];
|
||||
expect(sortParentsBeforeChildren(nodes)).toEqual(nodes);
|
||||
});
|
||||
|
||||
it("places parent before child", () => {
|
||||
// Deliberately reversed so naive iteration would place child first
|
||||
const nodes = [
|
||||
{ id: "child", parentId: "parent" },
|
||||
{ id: "parent", parentId: undefined },
|
||||
];
|
||||
const result = sortParentsBeforeChildren(nodes);
|
||||
expect(result[0].id).toBe("parent");
|
||||
expect(result[1].id).toBe("child");
|
||||
});
|
||||
|
||||
it("places grandparent before parent before child (deep chain)", () => {
|
||||
const nodes = [
|
||||
{ id: "child", parentId: "parent" },
|
||||
{ id: "grandchild", parentId: "child" },
|
||||
{ id: "parent", parentId: "grandparent" },
|
||||
{ id: "grandparent", parentId: undefined },
|
||||
];
|
||||
const result = sortParentsBeforeChildren(nodes);
|
||||
const ids = result.map((n) => n.id);
|
||||
expect(ids).toEqual(["grandparent", "parent", "child", "grandchild"]);
|
||||
});
|
||||
|
||||
it("siblings share the same parent", () => {
|
||||
const nodes = [
|
||||
{ id: "b", parentId: "a" },
|
||||
{ id: "a", parentId: undefined },
|
||||
{ id: "c", parentId: "a" },
|
||||
];
|
||||
const result = sortParentsBeforeChildren(nodes);
|
||||
expect(result[0].id).toBe("a");
|
||||
expect(new Set(result.slice(1).map((n) => n.id))).toEqual(new Set(["b", "c"]));
|
||||
});
|
||||
|
||||
it("no-ops when children already precede parents", () => {
|
||||
// Already sorted — output should be in the same order
|
||||
const nodes = [
|
||||
{ id: "root", parentId: undefined },
|
||||
{ id: "child", parentId: "root" },
|
||||
];
|
||||
expect(sortParentsBeforeChildren(nodes)).toEqual(nodes);
|
||||
});
|
||||
|
||||
it("handles orphan nodes (no parentId)", () => {
|
||||
const nodes = [{ id: "a" }, { id: "b" }];
|
||||
expect(sortParentsBeforeChildren(nodes).map((n) => n.id)).toEqual(["a", "b"]);
|
||||
});
|
||||
|
||||
it("returns a new array (does not mutate input)", () => {
|
||||
const nodes = [{ id: "child", parentId: "parent" }, { id: "parent", parentId: undefined }];
|
||||
const result = sortParentsBeforeChildren(nodes);
|
||||
expect(result).not.toBe(nodes);
|
||||
});
|
||||
|
||||
it("deduplicates already-visited nodes", () => {
|
||||
// Child's parent is also in the list — visited guard prevents loops
|
||||
const nodes = [
|
||||
{ id: "child", parentId: "parent" },
|
||||
{ id: "parent", parentId: undefined },
|
||||
];
|
||||
const result = sortParentsBeforeChildren(nodes);
|
||||
expect(result.map((n) => n.id)).toEqual(["parent", "child"]);
|
||||
});
|
||||
|
||||
it("does not crash when parentId references a missing node", () => {
|
||||
const nodes = [
|
||||
{ id: "orphan", parentId: "ghost" },
|
||||
{ id: "root", parentId: undefined },
|
||||
];
|
||||
// Missing parent is skipped; orphan placed after root
|
||||
const result = sortParentsBeforeChildren(nodes);
|
||||
expect(result.map((n) => n.id)).toEqual(["root", "orphan"]);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── defaultChildSlot ─────────────────────────────────────────────────────────
|
||||
|
||||
describe("defaultChildSlot — 2-column grid (240×130 cards)", () => {
|
||||
it("slot 0 → column 0, row 0", () => {
|
||||
const s = defaultChildSlot(0);
|
||||
expect(s).toEqual({ x: 16, y: 130 });
|
||||
});
|
||||
|
||||
it("slot 1 → column 1, row 0", () => {
|
||||
const s = defaultChildSlot(1);
|
||||
expect(s.x).toBe(16 + 240 + 14); // PARENT_SIDE_PADDING + CHILD_DEFAULT_WIDTH + CHILD_GUTTER
|
||||
expect(s.y).toBe(130);
|
||||
});
|
||||
|
||||
it("slot 2 → column 0, row 1", () => {
|
||||
const s = defaultChildSlot(2);
|
||||
expect(s.x).toBe(16);
|
||||
expect(s.y).toBe(130 + 130 + 14); // row 0 height + gutter
|
||||
});
|
||||
|
||||
it("slot 3 → column 1, row 1", () => {
|
||||
const s = defaultChildSlot(3);
|
||||
expect(s.x).toBe(16 + 240 + 14);
|
||||
expect(s.y).toBe(130 + 130 + 14);
|
||||
});
|
||||
|
||||
it("slot 4 → column 0, row 2", () => {
|
||||
const s = defaultChildSlot(4);
|
||||
expect(s.x).toBe(16);
|
||||
expect(s.y).toBe(130 + (130 + 14) * 2); // row 1 end + gutter
|
||||
});
|
||||
});
|
||||
|
||||
// ─── childSlotInGrid ──────────────────────────────────────────────────────────
|
||||
|
||||
describe("childSlotInGrid — variable-size siblings", () => {
|
||||
it("empty siblingSizes returns side-padded position", () => {
|
||||
const s = childSlotInGrid(0, []);
|
||||
expect(s).toEqual({ x: 16, y: 130 });
|
||||
});
|
||||
|
||||
it("slot 0 in uniform-size siblings matches defaultChildSlot", () => {
|
||||
const sizes = [{ width: 240, height: 130 }, { width: 240, height: 130 }];
|
||||
const s = childSlotInGrid(0, sizes);
|
||||
expect(s.x).toBe(16);
|
||||
expect(s.y).toBe(130);
|
||||
});
|
||||
|
||||
it("taller sibling bumps next row down", () => {
|
||||
// Column width = max(200, 240) = 240; row 0 height = max(300, 130) = 300
|
||||
const sizes = [{ width: 200, height: 300 }, { width: 240, height: 130 }];
|
||||
const slot1 = childSlotInGrid(1, sizes);
|
||||
// Slot 1 is in column 1, row 0; x = 16 + 1*(240+14)
|
||||
expect(slot1.x).toBe(16 + 240 + 14);
|
||||
expect(slot1.y).toBe(130);
|
||||
// Slot 2 (col 0, row 1) — y must include row 0 height + gutter
|
||||
const slot2 = childSlotInGrid(2, sizes);
|
||||
expect(slot2.x).toBe(16);
|
||||
expect(slot2.y).toBe(130 + 300 + 14);
|
||||
});
|
||||
|
||||
it("colW is the maximum sibling width, not the column of the target slot", () => {
|
||||
// Column width is always the max — slot at col 0 uses colW of wider col 1 sibling
|
||||
const sizes = [{ width: 100, height: 100 }, { width: 300, height: 100 }];
|
||||
const slot0 = childSlotInGrid(0, sizes);
|
||||
expect(slot0.x).toBe(16); // col 0
|
||||
// x for col 1 would be 16 + 300 + 14 = 330
|
||||
const slot1 = childSlotInGrid(1, sizes);
|
||||
expect(slot1.x).toBe(16 + 300 + 14);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── parentMinSize ─────────────────────────────────────────────────────────────
|
||||
|
||||
describe("parentMinSize — uniform-size children", () => {
|
||||
it("0 children → compact default (210×120)", () => {
|
||||
expect(parentMinSize(0)).toEqual({ width: 210, height: 120 });
|
||||
});
|
||||
|
||||
it("1 child → 1 col, 1 row", () => {
|
||||
const s = parentMinSize(1);
|
||||
// width = 16*2 + 1*240 + 0 = 272; height = 130 + 1*130 + 0 + 16 = 276
|
||||
expect(s.width).toBe(16 * 2 + 240);
|
||||
expect(s.height).toBe(130 + 130 + 16);
|
||||
});
|
||||
|
||||
it("2 children → 2 cols, 1 row", () => {
|
||||
const s = parentMinSize(2);
|
||||
// width = 16*2 + 2*240 + 1*14 = 526; height = 130 + 1*130 + 0 + 16 = 276
|
||||
expect(s.width).toBe(16 * 2 + 2 * 240 + 14);
|
||||
expect(s.height).toBe(130 + 130 + 16);
|
||||
});
|
||||
|
||||
it("3 children → 2 cols, 2 rows", () => {
|
||||
const s = parentMinSize(3);
|
||||
// width = 16*2 + 2*240 + 1*14 = 526
|
||||
expect(s.width).toBe(16 * 2 + 2 * 240 + 14);
|
||||
// height = 130 + 2*130 + 1*14 + 16 = 416
|
||||
expect(s.height).toBe(130 + 2 * 130 + 14 + 16);
|
||||
});
|
||||
|
||||
it("4 children → 2 cols, 2 rows (full grid)", () => {
|
||||
const s = parentMinSize(4);
|
||||
expect(s.width).toBe(16 * 2 + 2 * 240 + 14);
|
||||
expect(s.height).toBe(130 + 2 * 130 + 14 + 16);
|
||||
});
|
||||
|
||||
it("5 children → 2 cols, 3 rows", () => {
|
||||
const s = parentMinSize(5);
|
||||
expect(s.width).toBe(16 * 2 + 2 * 240 + 14);
|
||||
expect(s.height).toBe(130 + 3 * 130 + 2 * 14 + 16);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── parentMinSizeFromChildren ────────────────────────────────────────────────
|
||||
|
||||
describe("parentMinSizeFromChildren — variable-size children", () => {
|
||||
it("empty array → compact default (210×120)", () => {
|
||||
expect(parentMinSizeFromChildren([])).toEqual({ width: 210, height: 120 });
|
||||
});
|
||||
|
||||
it("single child matches defaultChildSlot bounding box", () => {
|
||||
const s = parentMinSizeFromChildren([{ width: 240, height: 130 }]);
|
||||
// cols=1, rows=1, colW=240
|
||||
expect(s.width).toBe(16 * 2 + 240); // 272
|
||||
expect(s.height).toBe(130 + 130 + 16); // 276
|
||||
});
|
||||
|
||||
it("two equal-width children → same as parentMinSize(2)", () => {
|
||||
const fromChildren = parentMinSizeFromChildren([
|
||||
{ width: 240, height: 130 },
|
||||
{ width: 240, height: 130 },
|
||||
]);
|
||||
expect(fromChildren.width).toBe(parentMinSize(2).width);
|
||||
expect(fromChildren.height).toBe(parentMinSize(2).height);
|
||||
});
|
||||
|
||||
it("taller child increases height", () => {
|
||||
const tall = parentMinSizeFromChildren([{ width: 240, height: 400 }]);
|
||||
const short = parentMinSizeFromChildren([{ width: 240, height: 130 }]);
|
||||
expect(tall.height).toBeGreaterThan(short.height);
|
||||
});
|
||||
|
||||
it("wider child increases width", () => {
|
||||
const wide = parentMinSizeFromChildren([{ width: 500, height: 130 }]);
|
||||
const narrow = parentMinSizeFromChildren([{ width: 200, height: 130 }]);
|
||||
expect(wide.width).toBeGreaterThan(narrow.width);
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,6 @@
|
||||
services:
|
||||
# digest-pinned 2026-05-10 (sha256:4941ef97aaa2633ce9808f7766f8b8d746dd039ce8c51ca6da185c3dc63ab579, linux/amd64)
|
||||
postgres:
|
||||
image: postgres@sha256:4941ef97aaa2633ce9808f7766f8b8d746dd039ce8c51ca6da185c3dc63ab579
|
||||
image: postgres:16-alpine
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-dev}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-dev}
|
||||
@@ -18,7 +17,7 @@ services:
|
||||
retries: 10
|
||||
|
||||
langfuse-db-init:
|
||||
image: postgres@sha256:4941ef97aaa2633ce9808f7766f8b8d746dd039ce8c51ca6da185c3dc63ab579
|
||||
image: postgres:16-alpine
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
@@ -37,9 +36,8 @@ services:
|
||||
psql -h postgres -U "$${POSTGRES_USER}" -d postgres -c "CREATE DATABASE langfuse"
|
||||
fi
|
||||
|
||||
# digest-pinned 2026-05-10 (sha256:b1addbe72465a718643cff9e60a58e6df1841e29d6d7d60c9a85d8d72f08d1a7, linux/amd64)
|
||||
redis:
|
||||
image: redis@sha256:b1addbe72465a718643cff9e60a58e6df1841e29d6d7d60c9a85d8d72f08d1a7
|
||||
image: redis:7-alpine
|
||||
command: ["redis-server", "--notify-keyspace-events", "KEA"]
|
||||
ports:
|
||||
- "6379:6379"
|
||||
@@ -51,9 +49,8 @@ services:
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
|
||||
# digest-pinned 2026-05-10 (sha256:5b296e0ba1da74efea3143c773ddd60245f249fb7c72eb1d866c2d6ebc759fbe, linux/amd64)
|
||||
clickhouse:
|
||||
image: clickhouse/clickhouse-server@sha256:5b296e0ba1da74efea3143c773ddd60245f249fb7c72eb1d866c2d6ebc759fbe
|
||||
image: clickhouse/clickhouse-server:24-alpine
|
||||
environment:
|
||||
CLICKHOUSE_DB: langfuse
|
||||
CLICKHOUSE_USER: langfuse
|
||||
@@ -67,9 +64,8 @@ services:
|
||||
retries: 10
|
||||
|
||||
# dev-only: no-auth on 0.0.0.0:7233; production must gate via mTLS or API key
|
||||
# digest-pinned 2026-05-10 (sha256:9ce78f5a7ba7169acb659a8bb7a174a64251c3bfe1553d1fefdd669a59d41df5, linux/amd64)
|
||||
temporal:
|
||||
image: temporalio/auto-setup@sha256:9ce78f5a7ba7169acb659a8bb7a174a64251c3bfe1553d1fefdd669a59d41df5
|
||||
image: temporalio/auto-setup:1.25
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
@@ -89,9 +85,8 @@ services:
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
|
||||
# digest-pinned 2026-05-10 (sha256:7be8d6e41d4846ccb718c4f35956c9557512f8085e94a73954286a4e95113703, linux/amd64)
|
||||
temporal-ui:
|
||||
image: temporalio/ui@sha256:7be8d6e41d4846ccb718c4f35956c9557512f8085e94a73954286a4e95113703
|
||||
image: temporalio/ui:2.31.2
|
||||
depends_on:
|
||||
- temporal
|
||||
environment:
|
||||
@@ -100,9 +95,8 @@ services:
|
||||
ports:
|
||||
- "8233:8080"
|
||||
|
||||
# digest-pinned 2026-05-10 (sha256:e7aafd3ccf721821b40f8b2251220b4bb8af5e4877b5c5a8846af5b3318aaf1d, linux/amd64)
|
||||
langfuse-web:
|
||||
image: langfuse/langfuse@sha256:e7aafd3ccf721821b40f8b2251220b4bb8af5e4877b5c5a8846af5b3318aaf1d
|
||||
image: langfuse/langfuse:2
|
||||
depends_on:
|
||||
clickhouse:
|
||||
condition: service_healthy
|
||||
|
||||
+7
-17
@@ -4,9 +4,8 @@ include:
|
||||
|
||||
services:
|
||||
# --- Infrastructure ---
|
||||
# digest-pinned 2026-05-10 (sha256:4941ef97aaa2633ce9808f7766f8b8d746dd039ce8c51ca6da185c3dc63ab579, linux/amd64)
|
||||
postgres:
|
||||
image: postgres@sha256:4941ef97aaa2633ce9808f7766f8b8d746dd039ce8c51ca6da185c3dc63ab579
|
||||
image: postgres:16-alpine
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-dev}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-dev}
|
||||
@@ -26,7 +25,7 @@ services:
|
||||
retries: 10
|
||||
|
||||
langfuse-db-init:
|
||||
image: postgres@sha256:4941ef97aaa2633ce9808f7766f8b8d746dd039ce8c51ca6da185c3dc63ab579
|
||||
image: postgres:16-alpine
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
@@ -47,9 +46,8 @@ services:
|
||||
networks:
|
||||
- molecule-core-net
|
||||
|
||||
# digest-pinned 2026-05-10 (sha256:b1addbe72465a718643cff9e60a58e6df1841e29d6d7d60c9a85d8d72f08d1a7, linux/amd64)
|
||||
redis:
|
||||
image: redis@sha256:b1addbe72465a718643cff9e60a58e6df1841e29d6d7d60c9a85d8d72f08d1a7
|
||||
image: redis:7-alpine
|
||||
command: ["redis-server", "--notify-keyspace-events", "KEA"]
|
||||
ports:
|
||||
- "6379:6379"
|
||||
@@ -65,9 +63,8 @@ services:
|
||||
retries: 10
|
||||
|
||||
# --- Observability ---
|
||||
# digest-pinned 2026-05-10 (sha256:5b296e0ba1da74efea3143c773ddd60245f249fb7c72eb1d866c2d6ebc759fbe, linux/amd64)
|
||||
langfuse-clickhouse:
|
||||
image: clickhouse/clickhouse-server@sha256:5b296e0ba1da74efea3143c773ddd60245f249fb7c72eb1d866c2d6ebc759fbe
|
||||
image: clickhouse/clickhouse-server:24-alpine
|
||||
environment:
|
||||
CLICKHOUSE_DB: langfuse
|
||||
CLICKHOUSE_USER: langfuse
|
||||
@@ -82,9 +79,8 @@ services:
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
|
||||
# digest-pinned 2026-05-10 (sha256:e7aafd3ccf721821b40f8b2251220b4bb8af5e4877b5c5a8846af5b3318aaf1d, linux/amd64)
|
||||
langfuse:
|
||||
image: langfuse/langfuse@sha256:e7aafd3ccf721821b40f8b2251220b4bb8af5e4877b5c5a8846af5b3318aaf1d
|
||||
image: langfuse/langfuse:2
|
||||
depends_on:
|
||||
langfuse-clickhouse:
|
||||
condition: service_healthy
|
||||
@@ -243,8 +239,6 @@ services:
|
||||
# First-time local setup or testing unreleased changes — build from source:
|
||||
# docker compose build canvas && docker compose up -d canvas
|
||||
# Note: ECR images require AWS auth — `aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 153263036946.dkr.ecr.us-east-2.amazonaws.com` before pull.
|
||||
# Digest-pin requires: aws ecr describe-images --repository-name molecule-ai/canvas --image-tags latest --query 'imageDetails[0].imageDigest'
|
||||
# TODO: pin canvas ECR image digest once AWS creds are available in CI.
|
||||
image: 153263036946.dkr.ecr.us-east-2.amazonaws.com/molecule-ai/canvas:latest
|
||||
build:
|
||||
context: ./canvas
|
||||
@@ -285,10 +279,8 @@ services:
|
||||
# And use model names from infra/litellm_config.yml (e.g. "claude-opus-4-5",
|
||||
# "gpt-4o", "openrouter/deepseek-r1", "ollama/llama3.2").
|
||||
# Edit infra/litellm_config.yml to add/remove providers and models.
|
||||
# digest-pinned 2026-05-10 (sha256:7c311546c25e7bb6e8cafede9fcd3d0d622ac636b5c9418befaa32e85dfb0186)
|
||||
# Refresh: curl -sI https://ghcr.io/v2/berriai/litellm/manifests/main-latest (Docker-Content-Digest header)
|
||||
litellm:
|
||||
image: ghcr.io/berriai/litellm/main-latest@sha256:7c311546c25e7bb6e8cafede9fcd3d0d622ac636b5c9418befaa32e85dfb0186
|
||||
image: ghcr.io/berriai/litellm:main-latest
|
||||
profiles:
|
||||
- multi-provider
|
||||
ports:
|
||||
@@ -319,10 +311,8 @@ services:
|
||||
# docker compose exec ollama ollama pull qwen2.5-coder:7b
|
||||
# Then set MODEL_PROVIDER=ollama:llama3.2 in your workspace config.yaml
|
||||
# Workspace agents reach Ollama at http://ollama:11434 (internal Docker network).
|
||||
# digest-pinned 2026-05-10 (sha256:90bd8ed1ad1853fbfb1ef5835f9d7a24fe890e05ace521e2d8d7a6f56bb667dd, linux/amd64)
|
||||
# Refresh: curl -s https://hub.docker.com/v2/repositories/ollama/ollama/tags/latest | python3 -c "import json,sys; ..."
|
||||
ollama:
|
||||
image: ollama/ollama@sha256:90bd8ed1ad1853fbfb1ef5835f9d7a24fe890e05ace521e2d8d7a6f56bb667dd
|
||||
image: ollama/ollama:latest
|
||||
profiles:
|
||||
- local-models
|
||||
ports:
|
||||
|
||||
@@ -269,28 +269,6 @@ Each workspace exposes an A2A server, builds an Agent Card, and registers with t
|
||||
|
||||
But the long-term collaboration model remains direct workspace-to-workspace communication via A2A.
|
||||
|
||||
## Known Limitations
|
||||
|
||||
### Playwright / browser system libs are not installed
|
||||
|
||||
The base `molecule-ai-workspace-runtime` image (`workspace/Dockerfile`) is built on `python:3.11-slim` with Node.js 22, git, and `gh` — about 500 MB. It deliberately **does not** include the system libraries Chromium needs (`libnss3`, `libatk-bridge2.0-0`, `libxkbcommon0`, `libcups2`, `libdrm2`, `libxcomposite1`, `libxdamage1`, `libxrandr2`, `libgbm1`, `libpango-1.0-0`, `libasound2`, etc.). Adding them would inflate the image by ~200–250 MB (~40%) for every workspace, even though only frontend / QA workspaces ever launch a browser.
|
||||
|
||||
Practical consequences:
|
||||
|
||||
- `npx playwright test` (and any other Chromium-driven E2E tooling) **will fail at browser launch** when run from inside an in-container workspace agent.
|
||||
- The error surface is missing-shared-object messages such as `error while loading shared libraries: libnss3.so` or `Host system is missing dependencies to run browsers`.
|
||||
- Unit and integration tests (Vitest, Jest, etc.) that don't spawn a real browser are unaffected.
|
||||
|
||||
Recommended workflow:
|
||||
|
||||
1. **Run E2E in CI**, not in-container. The Gitea Actions self-hosted runner (and the GitHub Actions runner used by mirror repos) has the full Playwright dep set installed and is the supported surface for E2E. Push a branch, let CI run the suite.
|
||||
2. **Local debugging** of a single failing spec is best done on a developer laptop with `npx playwright install-deps` run once.
|
||||
3. **In-container iteration** on test logic itself is fine — write specs, lint them, type-check them — just don't expect `playwright test` to actually launch a browser.
|
||||
|
||||
If a particular workspace role genuinely needs in-container E2E (a dedicated QA template, for instance), the right place to layer Playwright deps is in a **role-specific adapter template image** that does `FROM molecule-ai-workspace-runtime:<tag>` and adds `RUN npx playwright install-deps`. Open a request against `molecule-ai-workspace-runtime` if you need this template stamped.
|
||||
|
||||
Tracking issue: [molecule-ai/molecule-app#7](https://git.moleculesai.app/molecule-ai/molecule-app/issues/7).
|
||||
|
||||
## Related Docs
|
||||
|
||||
- [Agent Runtime Adapters](./cli-runtime.md)
|
||||
|
||||
@@ -88,7 +88,6 @@ PR: `fix/ink-soft-wcag-contrast`.
|
||||
- Arrow keys move selected node 10px (50px with Shift) — keyboard node drag (PR #182) ✅
|
||||
- `Cmd/Ctrl+Arrow` resize selected node (↑↓ height, ←→ width, 10px, Shift 2px) ✅
|
||||
- Hierarchy navigation (Enter/Shift+Enter), z-order (Cmd+]/[), zoom-to-team (Z) ✅
|
||||
- Toolbar help dialog ("Shortcuts & tips") documents all shortcuts + mouse interactions ✅
|
||||
|
||||
### Focus Management ✅ (strong)
|
||||
- Skip link → `#canvas-main` ✅
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
# Admin Authentication Runbook
|
||||
|
||||
## Test-token route: lock in staging and production
|
||||
|
||||
The `GET /admin/workspaces/:id/test-token` endpoint mints fresh workspace auth tokens.
|
||||
It is gated by `TestTokensEnabled()` which returns `true` only when `MOLECULE_ENV != "production"`.
|
||||
|
||||
**Effect**: if `MOLECULE_ENV` is unset or set to `development` / `dev` in a staging or production
|
||||
tenant, the test-token route remains enabled. While the route is protected by `subtle.ConstantTimeCompare`
|
||||
against `ADMIN_TOKEN` (returns 404 when disabled, not 403), the safest posture is to lock it
|
||||
out in any environment where it is not intentionally used.
|
||||
|
||||
### Required: set MOLECULE_ENV in all non-dev environments
|
||||
|
||||
```bash
|
||||
# In your tenant / EC2 / Railway environment variables:
|
||||
MOLECULE_ENV=production
|
||||
```
|
||||
|
||||
This matches the production tenant default. When `MOLECULE_ENV=production`:
|
||||
|
||||
- `TestTokensEnabled()` → `false`
|
||||
- `GET /admin/workspaces/:id/test-token` → 404 (route disabled)
|
||||
|
||||
### Startup visibility
|
||||
|
||||
workspace-server logs the test-token route state at boot:
|
||||
|
||||
```
|
||||
Platform starting on ... (dev-mode-fail-open=...)
|
||||
```
|
||||
|
||||
Additionally, when `TestTokensEnabled()` is `true` (route enabled), the server emits an INFO line
|
||||
so operators can confirm the setting in logs:
|
||||
|
||||
```
|
||||
[molecule-git-token-helper] NOTE: /admin/workspaces/:id/test-token is ENABLED
|
||||
(running with MOLECULE_ENV != production)
|
||||
```
|
||||
|
||||
If you do not see this line and the route is still accessible, verify `MOLECULE_ENV` is not set to
|
||||
`development`, `dev`, or any value that is not exactly `production`.
|
||||
|
||||
### Dev environments
|
||||
|
||||
In local dev (`MOLECULE_ENV=development` or unset with no `ADMIN_TOKEN`), the test-token route
|
||||
is intentionally enabled — it is the only way to bootstrap a workspace bearer token without a running
|
||||
canvas. This is the correct default for developer workstations.
|
||||
|
||||
## Admin bearer token (`ADMIN_TOKEN`)
|
||||
|
||||
The platform uses `ADMIN_TOKEN` as the bearer credential for admin-gated endpoints:
|
||||
|
||||
| Endpoint | Auth method |
|
||||
|----------|-------------|
|
||||
| `GET/POST/PATCH/DELETE /workspaces` | `Authorization: Bearer <ADMIN_TOKEN>` |
|
||||
| `GET /admin/liveness` | `Authorization: Bearer <ADMIN_TOKEN>` |
|
||||
| `POST /org/import` | `Authorization: Bearer <ADMIN_TOKEN>` |
|
||||
| `GET /admin/workspaces/:id/test-token` | `Authorization: Bearer <ADMIN_TOKEN>` (enabled only when `MOLECULE_ENV != "production"`) |
|
||||
|
||||
Missing or invalid `ADMIN_TOKEN` → AdminAuth fails open in dev mode (no token set), or
|
||||
returns 401 in production mode (token set but invalid).
|
||||
@@ -44,4 +44,3 @@
|
||||
{"name": "mock-bigorg", "repo": "molecule-ai/molecule-ai-org-template-mock-bigorg", "ref": "main"}
|
||||
]
|
||||
}
|
||||
// Triggered by Integration Tester at 2026-05-10T08:52Z
|
||||
|
||||
+4
-46
@@ -1,15 +1,10 @@
|
||||
#!/bin/bash
|
||||
# canary-smoke.sh — runs the post-deploy smoke suite against the
|
||||
# staging canary tenant fleet. Called by the canary-verify.yml GitHub
|
||||
# Actions workflow after a new workspace-server image lands in ECR;
|
||||
# exits non-zero on any failure so the workflow can block the
|
||||
# redeploy-fleet promotion that would otherwise release broken code
|
||||
# to the prod tenant fleet.
|
||||
#
|
||||
# Registry note: GHCR was retired 2026-05-06. Images are now pushed
|
||||
# to the operator's ECR org (153263036946.dkr.ecr.us-east-2.amazonaws.com/
|
||||
# molecule-ai/platform-tenant). The registry URL is a runtime concern for
|
||||
# the CI push step; this script tests the running tenant directly.
|
||||
# Actions workflow after a new workspace-server image gets pushed to
|
||||
# GHCR; exits non-zero on any failure so the workflow can skip the
|
||||
# :staging-sha → :latest retag that would otherwise release broken
|
||||
# code to the prod tenant fleet.
|
||||
#
|
||||
# Environment:
|
||||
# CANARY_TENANT_URLS space-sep list of canary tenant base URLs
|
||||
@@ -113,43 +108,6 @@ for i in "${!URLS[@]}"; do
|
||||
# 5. Negative: unauth'd admin call must 401 (C4 regression gate).
|
||||
unauth_code=$(curl -sS -o /dev/null -w '%{http_code}' --max-time 10 "$base/admin/liveness" || echo "000")
|
||||
check "unauth'd /admin/liveness returns 401" "401" "$unauth_code"
|
||||
|
||||
# 6. POST /org/import unauth → 401. Proves the route is compiled in
|
||||
# and AdminAuth is enforced. A missing route returns 404 (the failure
|
||||
# mode caught by issue #213). Regression guard for the silent-GHCR-
|
||||
# migration gap: canary-verify was testing a stale GHCR image while
|
||||
# actual tenants ran ECR — this test would have caught a missing-route
|
||||
# binary before it reached prod.
|
||||
unauth_code=$(curl -sS -o /dev/null -w '%{http_code}' \
|
||||
--max-time 10 -X POST "$base/org/import" || echo "000")
|
||||
check "POST /org/import unauth returns 401 (not 404)" "401" "$unauth_code"
|
||||
|
||||
# 7. POST /org/import authed → 400/422 (malformed body, not 404).
|
||||
# Proves the route IS in the binary AND AdminAuth passed. Using a
|
||||
# deliberately broken body so we hit the handler's validation, not a
|
||||
# business-logic error that might return 500 in some states.
|
||||
bad_code=$(curl -sS -o /dev/null -w '%{http_code}' \
|
||||
--max-time 10 -X POST \
|
||||
-H "Authorization: Bearer $token" \
|
||||
-H "Content-Type: application/json" \
|
||||
--data '{"dir":"nonexistent-org-template"}' \
|
||||
"$base/org/import" || echo "000")
|
||||
# Accept 400 (bad request / validation), 404 (template not found but
|
||||
# route exists — good enough to prove route compiled), or 422 (unproc).
|
||||
# Reject 000 (connection error) and 500 (server crash).
|
||||
if [ "$bad_code" = "000" ] || [ "$bad_code" = "500" ]; then
|
||||
printf " FAIL POST /org/import authed returns HTTP %s (expected 400/404/422)\n" "$bad_code" >&2
|
||||
FAIL=$((FAIL + 1))
|
||||
else
|
||||
printf " PASS POST /org/import authed returns HTTP %s (route compiled + AdminAuth enforced)\n" "$bad_code"
|
||||
PASS=$((PASS + 1))
|
||||
fi
|
||||
|
||||
# 8. POST /workspaces unauth → 401. Proves the route is compiled in.
|
||||
# GET /workspaces was already covered in step 2; POST was the gap.
|
||||
unauth_code=$(curl -sS -o /dev/null -w '%{http_code}' \
|
||||
--max-time 10 -X POST "$base/workspaces" || echo "000")
|
||||
check "POST /workspaces unauth returns 401 (not 404)" "401" "$unauth_code"
|
||||
done
|
||||
|
||||
# ── Summary ──────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -37,50 +37,6 @@ PLUGINS_DIR="${4:?Missing plugins dir}"
|
||||
EXPECTED=0
|
||||
CLONED=0
|
||||
|
||||
# clone_one_with_retry — clone a single repo, retrying on transient failure.
|
||||
#
|
||||
# Why: the publish-workspace-server-image (and harness-replays) CI jobs
|
||||
# clone the full manifest (~36 repos) serially on a memory-constrained
|
||||
# Gitea Actions runner. Under host memory pressure the OOM killer
|
||||
# occasionally SIGKILLs git-remote-https mid-clone:
|
||||
#
|
||||
# error: git-remote-https died of signal 9
|
||||
# fatal: the remote end hung up unexpectedly
|
||||
#
|
||||
# (observed in publish-workspace-server-image run 4622 on 2026-05-10 — the
|
||||
# job died on the 14th of 36 clones, which wedged staging→main). One
|
||||
# transient SIGKILL / network blip would otherwise fail the whole tenant
|
||||
# image rebuild. Retrying after a short backoff lets the pressure subside.
|
||||
# The durable fix is more runner RAM/swap (tracked with Infra-SRE); this
|
||||
# just stops a single flake from being release-blocking.
|
||||
#
|
||||
# Args: <target_dir> <name> <clone_url> <display_url> <ref>
|
||||
clone_one_with_retry() {
|
||||
local tdir="$1" name="$2" url="$3" display="$4" ref="$5"
|
||||
local attempt=1 max_attempts=3 backoff
|
||||
|
||||
while : ; do
|
||||
# A killed attempt can leave a partial directory behind; git clone
|
||||
# refuses a non-empty target, so wipe it before each try.
|
||||
rm -rf "$tdir/$name"
|
||||
|
||||
if [ "$ref" = "main" ]; then
|
||||
if git clone --depth=1 -q "$url" "$tdir/$name"; then return 0; fi
|
||||
else
|
||||
if git clone --depth=1 -q --branch "$ref" "$url" "$tdir/$name"; then return 0; fi
|
||||
fi
|
||||
|
||||
if [ "$attempt" -ge "$max_attempts" ]; then
|
||||
echo "::error::clone failed after ${max_attempts} attempts: ${display}" >&2
|
||||
return 1
|
||||
fi
|
||||
backoff=$((attempt * 3)) # 3s, then 6s
|
||||
echo " ⚠ clone attempt ${attempt}/${max_attempts} failed for ${display} — retrying in ${backoff}s" >&2
|
||||
sleep "$backoff"
|
||||
attempt=$((attempt + 1))
|
||||
done
|
||||
}
|
||||
|
||||
clone_category() {
|
||||
local category="$1"
|
||||
local target_dir="$2"
|
||||
@@ -126,7 +82,11 @@ clone_category() {
|
||||
fi
|
||||
|
||||
echo " cloning $display_url -> $target_dir/$name (ref=$ref)"
|
||||
clone_one_with_retry "$target_dir" "$name" "$clone_url" "$display_url" "$ref"
|
||||
if [ "$ref" = "main" ]; then
|
||||
git clone --depth=1 -q "$clone_url" "$target_dir/$name"
|
||||
else
|
||||
git clone --depth=1 -q --branch "$ref" "$clone_url" "$target_dir/$name"
|
||||
fi
|
||||
CLONED=$((CLONED + 1))
|
||||
i=$((i + 1))
|
||||
done
|
||||
|
||||
@@ -115,16 +115,8 @@ COPY --from=canvas-builder /canvas/.next/static ./.next/static
|
||||
COPY --from=canvas-builder /canvas/public ./public
|
||||
|
||||
COPY workspace-server/entrypoint-tenant.sh /entrypoint.sh
|
||||
# /org-templates must be writable by the canvas user — the !external
|
||||
# resolver mkdirs <orgBaseDir>/.external-cache/<repo>/<sha>/ on first
|
||||
# import to cache cross-repo subtree fetches (org_external.go,
|
||||
# internal#77 / task #222). Without this chown the resolver fails with
|
||||
# "mkdir cache root: permission denied" and POST /org/import returns
|
||||
# 400 "org template expansion failed" for any template that uses
|
||||
# !external (e.g. molecule-dev → dev-lead). Caught on staging-cplead-2
|
||||
# 2026-05-10 — see internal incident debrief.
|
||||
RUN chmod +x /entrypoint.sh && \
|
||||
chown -R canvas:canvas /canvas /platform /memory-plugin /migrations /org-templates
|
||||
chown -R canvas:canvas /canvas /platform /memory-plugin /migrations
|
||||
|
||||
EXPOSE 8080
|
||||
# entrypoint.sh starts as root to fix volume perms, then drops to
|
||||
|
||||
@@ -367,9 +367,6 @@ func main() {
|
||||
// Start server in goroutine
|
||||
go func() {
|
||||
log.Printf("Platform starting on %s:%s (dev-mode-fail-open=%v)", bindHost, port, middleware.IsDevModeFailOpen())
|
||||
if handlers.TestTokensEnabled() {
|
||||
log.Printf("NOTE: /admin/workspaces/:id/test-token is ENABLED (MOLECULE_ENV=%q — set MOLECULE_ENV=production in staging/prod to lock this route)", os.Getenv("MOLECULE_ENV"))
|
||||
}
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("Server failed: %v", err)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||
go.moleculesai.app/plugin/gh-identity v0.0.0-20260509010445-788988195fce
|
||||
github.com/alicebob/miniredis/v2 v2.37.0
|
||||
github.com/creack/pty v1.1.24
|
||||
github.com/docker/docker v28.5.2+incompatible
|
||||
@@ -18,7 +19,6 @@ require (
|
||||
github.com/opencontainers/image-spec v1.1.1
|
||||
github.com/redis/go-redis/v9 v9.19.0
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
go.moleculesai.app/plugin/gh-identity v0.0.0-20260509010445-788988195fce
|
||||
golang.org/x/crypto v0.50.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@@ -4,6 +4,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7Oputl
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||
github.com/Molecule-AI/molecule-ai-plugin-gh-identity v0.0.0-20260424033845-4fd5ac7be30f h1:YkLRhUg+9qr9OV9N8dG1Hj0Ml7TThHlRwh5F//oUJVs=
|
||||
github.com/Molecule-AI/molecule-ai-plugin-gh-identity v0.0.0-20260424033845-4fd5ac7be30f/go.mod h1:NqdtlWZDJvpXNJRHnMkPhTKHdA1LZTNH+63TB66JSOU=
|
||||
github.com/alicebob/miniredis/v2 v2.37.0 h1:RheObYW32G1aiJIj81XVt78ZHJpHonHLHW7OLIshq68=
|
||||
github.com/alicebob/miniredis/v2 v2.37.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
@@ -152,8 +154,6 @@ github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M
|
||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
go.moleculesai.app/plugin/gh-identity v0.0.0-20260509010445-788988195fce h1:ftm0ba0ukLlfqeFes+/jWnXH8XULXmRpMy3fOCZ83/U=
|
||||
go.moleculesai.app/plugin/gh-identity v0.0.0-20260509010445-788988195fce/go.mod h1:0aAqoDle2V7Cywso94MXdv1DH/HEe/0oZmcbqWYMK7g=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/envx"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/events"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/models"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/provisioner"
|
||||
@@ -111,14 +110,11 @@ const maxProxyResponseBody = 10 << 20
|
||||
// a generic 502 page to canvas. 10s is well above realistic intra-region
|
||||
// latencies and well below CF's edge timeout.
|
||||
//
|
||||
// 3. Transport.ResponseHeaderTimeout — 180s default. From request-body-end
|
||||
// to response-headers-start. Configurable via
|
||||
// A2A_PROXY_RESPONSE_HEADER_TIMEOUT (envx.Duration). Covers cold-start
|
||||
// first-byte (30-60s OAuth flow above) with enough room for Opus agent
|
||||
// turns (big context + internal delegate_task round-trips routinely exceed
|
||||
// the old 60s ceiling). Body streaming after headers is governed by the
|
||||
// per-request context deadline, NOT this timeout — so multi-minute agent
|
||||
// responses still work fine.
|
||||
// 3. Transport.ResponseHeaderTimeout — 60s. From request-body-end to
|
||||
// response-headers-start. Covers cold-start first-byte (the 30-60s OAuth
|
||||
// flow above), with margin. Body streaming after headers is governed by
|
||||
// the per-request context deadline, NOT this timeout — so multi-minute
|
||||
// agent responses still work fine.
|
||||
//
|
||||
// The point of (2) and (3) is to surface a *structured* 503 from
|
||||
// handleA2ADispatchError when the workspace agent is unreachable, so canvas
|
||||
@@ -131,7 +127,7 @@ var a2aClient = &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
ResponseHeaderTimeout: envx.Duration("A2A_PROXY_RESPONSE_HEADER_TIMEOUT", 180*time.Second),
|
||||
ResponseHeaderTimeout: 60 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
// MaxIdleConns / IdleConnTimeout: stdlib defaults are fine; agent
|
||||
// fan-in is bounded by the platform's broadcaster fan-out, not by
|
||||
|
||||
@@ -2276,43 +2276,3 @@ func TestProxyA2A_PollMode_FailsClosedToPush(t *testing.T) {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== a2aClient ResponseHeaderTimeout config ====================
|
||||
|
||||
func TestA2AClientResponseHeaderTimeout(t *testing.T) {
|
||||
const defaultTimeout = 180 * time.Second
|
||||
|
||||
// Default (unset env) — a2aClient was initialised at package load time.
|
||||
if a2aClient.Transport.(*http.Transport).ResponseHeaderTimeout != defaultTimeout {
|
||||
t.Errorf("a2aClient default ResponseHeaderTimeout = %v, want %v",
|
||||
a2aClient.Transport.(*http.Transport).ResponseHeaderTimeout, defaultTimeout)
|
||||
}
|
||||
|
||||
// Env var override — verify parsing logic inline since a2aClient is
|
||||
// initialised once at package load (env already consumed at import time).
|
||||
t.Run("A2A_PROXY_RESPONSE_HEADER_TIMEOUT parsed correctly", func(t *testing.T) {
|
||||
// We can't re-initialise a2aClient, but we can verify the same
|
||||
// envx.Duration logic inline for the 5m override case.
|
||||
t.Setenv("A2A_PROXY_RESPONSE_HEADER_TIMEOUT", "5m")
|
||||
if d, err := time.ParseDuration("5m"); err == nil && d > 0 {
|
||||
if d != 5*time.Minute {
|
||||
t.Errorf("ParseDuration(\"5m\") = %v, want 5m", d)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid A2A_PROXY_RESPONSE_HEADER_TIMEOUT falls back to default", func(t *testing.T) {
|
||||
t.Setenv("A2A_PROXY_RESPONSE_HEADER_TIMEOUT", "not-a-duration")
|
||||
// Simulate what envx.Duration does with an invalid value.
|
||||
var fallback = 180 * time.Second
|
||||
override := fallback
|
||||
if v := os.Getenv("A2A_PROXY_RESPONSE_HEADER_TIMEOUT"); v != "" {
|
||||
if d, err := time.ParseDuration(v); err == nil && d > 0 {
|
||||
override = d
|
||||
}
|
||||
}
|
||||
if override != fallback {
|
||||
t.Errorf("invalid env var: got %v, want fallback %v", override, fallback)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -71,17 +71,10 @@ func TemplateImageRef(runtime string) string {
|
||||
|
||||
// ghcrAuthHeader returns the base64-encoded JSON auth payload Docker's
|
||||
// ImagePull expects in PullOptions.RegistryAuth, or empty string when no
|
||||
// GHCR_USER/GHCR_TOKEN env is set (lets public images pull through and lets
|
||||
// ECR's credential-helper-driven flow take over without a stale GHCR
|
||||
// payload masking it).
|
||||
// GHCR_USER/GHCR_TOKEN env is set (lets public images pull through).
|
||||
//
|
||||
// The Docker SDK doesn't read ~/.docker/config.json — every authenticated
|
||||
// pull needs an explicit RegistryAuth string. The serveraddress field is
|
||||
// resolved from provisioner.RegistryHost() so it tracks MOLECULE_IMAGE_REGISTRY
|
||||
// when the operator points the platform at a private mirror (e.g. ECR).
|
||||
// Leaving it hardcoded to "ghcr.io" caused the engine to match the wrong
|
||||
// auth entry post-suspension when MOLECULE_IMAGE_REGISTRY was flipped to
|
||||
// the AWS ECR mirror (RFC #229).
|
||||
// pull needs an explicit RegistryAuth string.
|
||||
func ghcrAuthHeader() string {
|
||||
user := strings.TrimSpace(os.Getenv("GHCR_USER"))
|
||||
token := strings.TrimSpace(os.Getenv("GHCR_TOKEN"))
|
||||
@@ -91,7 +84,7 @@ func ghcrAuthHeader() string {
|
||||
payload := map[string]string{
|
||||
"username": user,
|
||||
"password": token,
|
||||
"serveraddress": provisioner.RegistryHost(),
|
||||
"serveraddress": "ghcr.io",
|
||||
}
|
||||
js, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
func TestGHCRAuthHeader_NoEnvReturnsEmpty(t *testing.T) {
|
||||
t.Setenv("GHCR_USER", "")
|
||||
t.Setenv("GHCR_TOKEN", "")
|
||||
t.Setenv("MOLECULE_IMAGE_REGISTRY", "")
|
||||
if got := ghcrAuthHeader(); got != "" {
|
||||
t.Errorf("expected empty (no auth → public-only), got %q", got)
|
||||
}
|
||||
@@ -30,10 +29,6 @@ func TestGHCRAuthHeader_PartialEnvReturnsEmpty(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGHCRAuthHeader_EncodesDockerEnginePayload(t *testing.T) {
|
||||
// Default registry env (unset → ghcr.io/molecule-ai) means the
|
||||
// serveraddress field should resolve to ghcr.io. Pin both env vars so the
|
||||
// test is hermetic regardless of the host's MOLECULE_IMAGE_REGISTRY.
|
||||
t.Setenv("MOLECULE_IMAGE_REGISTRY", "")
|
||||
t.Setenv("GHCR_USER", "alice")
|
||||
t.Setenv("GHCR_TOKEN", "fake-tok-value")
|
||||
got := ghcrAuthHeader()
|
||||
@@ -59,41 +54,7 @@ func TestGHCRAuthHeader_EncodesDockerEnginePayload(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestGHCRAuthHeader_RespectsRegistryEnv pins the RFC #229 fix: when
|
||||
// MOLECULE_IMAGE_REGISTRY points at a private mirror (e.g. AWS ECR), the
|
||||
// Docker engine auth payload's serveraddress must reflect that mirror's
|
||||
// host so credential matching lands on the right entry. Pre-fix this was
|
||||
// hardcoded to "ghcr.io" and silently dropped the override.
|
||||
func TestGHCRAuthHeader_RespectsRegistryEnv(t *testing.T) {
|
||||
t.Setenv("GHCR_USER", "alice")
|
||||
t.Setenv("GHCR_TOKEN", "fake-tok-value")
|
||||
t.Setenv("MOLECULE_IMAGE_REGISTRY", "004947743811.dkr.ecr.us-east-2.amazonaws.com/molecule-ai")
|
||||
|
||||
got := ghcrAuthHeader()
|
||||
if got == "" {
|
||||
t.Fatal("expected non-empty auth header")
|
||||
}
|
||||
raw, err := base64.URLEncoding.DecodeString(got)
|
||||
if err != nil {
|
||||
t.Fatalf("auth header is not valid base64-url: %v", err)
|
||||
}
|
||||
var payload map[string]string
|
||||
if err := json.Unmarshal(raw, &payload); err != nil {
|
||||
t.Fatalf("decoded auth is not valid JSON: %v (raw=%s)", err, raw)
|
||||
}
|
||||
want := "004947743811.dkr.ecr.us-east-2.amazonaws.com"
|
||||
if payload["serveraddress"] != want {
|
||||
t.Errorf("serveraddress: got %q, want %q (must follow MOLECULE_IMAGE_REGISTRY host)",
|
||||
payload["serveraddress"], want)
|
||||
}
|
||||
// Sanity: the org-path portion must NOT leak into serveraddress.
|
||||
if payload["serveraddress"] == "004947743811.dkr.ecr.us-east-2.amazonaws.com/molecule-ai" {
|
||||
t.Error("serveraddress must be host-only, not host+org-path")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGHCRAuthHeader_TrimsWhitespace(t *testing.T) {
|
||||
t.Setenv("MOLECULE_IMAGE_REGISTRY", "")
|
||||
// .env lines often have trailing newlines or accidental spaces. Without
|
||||
// trimming, a stray space would produce an auth payload the engine
|
||||
// rejects with a confusing 401.
|
||||
|
||||
@@ -121,7 +121,7 @@ curl -fsS -X POST "{{PLATFORM_URL}}/registry/register" \
|
||||
// operators whose external agent IS a Claude Code session (laptop or
|
||||
// remote dev VM); routes the workspace's A2A traffic into the running
|
||||
// Claude Code session as conversation turns via MCP. The plugin source
|
||||
// lives at git.moleculesai.app/molecule-ai/molecule-mcp-claude-channel — polling
|
||||
// lives at github.com/Molecule-AI/molecule-mcp-claude-channel — polling
|
||||
// based, no tunnel required (uses /workspaces/:id/activity?since_secs=,
|
||||
// platform-side support shipped in #2300).
|
||||
const externalChannelTemplate = `# Claude Code channel — bridges this workspace's A2A traffic into your
|
||||
@@ -134,8 +134,8 @@ const externalChannelTemplate = `# Claude Code channel — bridges this workspac
|
||||
# The plugin is NOT on Anthropic's default allowlist, so a one-time
|
||||
# marketplace-add is needed before install:
|
||||
#
|
||||
# /plugin marketplace add https://git.moleculesai.app/molecule-ai/molecule-mcp-claude-channel.git
|
||||
# /plugin install molecule@molecule-channel
|
||||
# /plugin marketplace add Molecule-AI/molecule-mcp-claude-channel
|
||||
# /plugin install molecule@molecule-mcp-claude-channel
|
||||
#
|
||||
# Then either run /reload-plugins or restart Claude Code so the
|
||||
# plugin is registered.
|
||||
@@ -154,7 +154,7 @@ chmod 600 ~/.claude/channels/molecule/.env
|
||||
# flag to opt in — without it, you'll see "not on the approved channels
|
||||
# allowlist" on startup.
|
||||
claude --dangerously-load-development-channels \
|
||||
--channels plugin:molecule@molecule-channel
|
||||
--channels plugin:molecule@molecule-mcp-claude-channel
|
||||
|
||||
# You should see on stderr:
|
||||
# molecule channel: connected — watching 1 workspace(s) at {{PLATFORM_URL}}
|
||||
@@ -176,7 +176,7 @@ claude --dangerously-load-development-channels \
|
||||
# add the plugin to allowedChannelPlugins in claude.ai admin settings.
|
||||
#
|
||||
# Multi-workspace: comma-separate IDs and tokens (same order). See
|
||||
# https://git.moleculesai.app/molecule-ai/molecule-mcp-claude-channel for
|
||||
# https://github.com/Molecule-AI/molecule-mcp-claude-channel for
|
||||
# pairing flow, push-mode upgrade, and v0.2 roadmap.
|
||||
|
||||
# Need help?
|
||||
@@ -258,7 +258,7 @@ claude mcp add molecule -s user -- env \
|
||||
// externalPythonTemplate uses molecule-sdk-python's RemoteAgentClient +
|
||||
// A2AServer (PR #13 in that repo). Until the SDK cuts a v0.y release
|
||||
// to PyPI the snippet pins git+main.
|
||||
const externalPythonTemplate = `# pip install 'git+https://git.moleculesai.app/molecule-ai/molecule-sdk-python.git@main'
|
||||
const externalPythonTemplate = `# pip install 'git+https://github.com/Molecule-AI/molecule-sdk-python.git@main'
|
||||
|
||||
import asyncio
|
||||
from molecule_agent import RemoteAgentClient, A2AServer
|
||||
@@ -307,7 +307,7 @@ if __name__ == "__main__":
|
||||
// A2A traffic into the running hermes gateway as platform messages
|
||||
// via the molecule-channel plugin.
|
||||
//
|
||||
// The plugin (molecule-ai/hermes-channel-molecule on Gitea) is a hermes
|
||||
// The plugin (Molecule-AI/hermes-channel-molecule) is a hermes
|
||||
// platform adapter that:
|
||||
// 1. Spawns ``python -m molecule_runtime.a2a_mcp_server`` as a
|
||||
// stdio MCP subprocess (separate from any hermes-side MCP
|
||||
@@ -336,7 +336,7 @@ const externalHermesChannelTemplate = `# Hermes channel — bridges this workspa
|
||||
#
|
||||
# 1. Install the runtime + plugin:
|
||||
pip install molecule-ai-workspace-runtime
|
||||
pip install 'git+https://git.moleculesai.app/molecule-ai/hermes-channel-molecule.git'
|
||||
pip install 'git+https://github.com/Molecule-AI/hermes-channel-molecule.git'
|
||||
|
||||
# 2. Export the workspace credentials:
|
||||
export MOLECULE_WORKSPACE_ID={{WORKSPACE_ID}}
|
||||
@@ -366,7 +366,7 @@ hermes gateway --replace
|
||||
# by the plugin's molecule_runtime MCP subprocess).
|
||||
#
|
||||
# Source + issue tracker:
|
||||
# https://git.moleculesai.app/molecule-ai/hermes-channel-molecule
|
||||
# https://github.com/Molecule-AI/hermes-channel-molecule
|
||||
|
||||
# Need help?
|
||||
# Documentation: https://doc.moleculesai.app/docs/guides/external-agent-registration
|
||||
|
||||
@@ -75,46 +75,3 @@ func TestExternalMcpTemplates_UseMoleculeMcpWrapper(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestExternalTemplates_NoBrokenMoleculeAIGitHubURLs pins the invariant
|
||||
// that operator-facing snippets never embed github.com URLs pointing at
|
||||
// Molecule-AI repos.
|
||||
//
|
||||
// Why: the Molecule-AI GitHub org was suspended 2026-05-06 and the
|
||||
// canonical SCM is now git.moleculesai.app. Any `pip install
|
||||
// git+https://github.com/Molecule-AI/...` or marketplace-add Molecule-AI/
|
||||
// URL emitted to an external operator hits a 404 / org-suspended page,
|
||||
// breaking onboarding silently. RFC #229 P2-5.
|
||||
//
|
||||
// Third-party github URLs (gin, openai/codex, NousResearch/hermes-agent
|
||||
// upstream issue trackers, npm @openai/codex) remain valid — only
|
||||
// Molecule-AI/ paths are broken.
|
||||
func TestExternalTemplates_NoBrokenMoleculeAIGitHubURLs(t *testing.T) {
|
||||
templates := map[string]string{
|
||||
"externalCurlTemplate": externalCurlTemplate,
|
||||
"externalChannelTemplate": externalChannelTemplate,
|
||||
"externalUniversalMcpTemplate": externalUniversalMcpTemplate,
|
||||
"externalPythonTemplate": externalPythonTemplate,
|
||||
"externalHermesChannelTemplate": externalHermesChannelTemplate,
|
||||
"externalCodexTemplate": externalCodexTemplate,
|
||||
"externalOpenClawTemplate": externalOpenClawTemplate,
|
||||
}
|
||||
// Substrings that imply the snippet is pointing an operator at the
|
||||
// suspended Molecule-AI GitHub org.
|
||||
bannedSubstrings := []string{
|
||||
"github.com/Molecule-AI/",
|
||||
"github.com/molecule-ai/",
|
||||
// Bare `Molecule-AI/<repo>` form used by `/plugin marketplace add`
|
||||
// resolves through GitHub by default — explicit Gitea URL is
|
||||
// required post-suspension.
|
||||
"marketplace add Molecule-AI/",
|
||||
"marketplace add molecule-ai/",
|
||||
}
|
||||
for name, body := range templates {
|
||||
for _, banned := range bannedSubstrings {
|
||||
if strings.Contains(body, banned) {
|
||||
t.Errorf("%s contains %q — Molecule-AI GitHub org is suspended; use git.moleculesai.app/molecule-ai/<repo> instead (RFC #229 P2-5)", name, banned)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,6 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/pkg/provisionhook"
|
||||
@@ -99,17 +98,7 @@ func (h *GitHubTokenHandler) GetInstallationToken(c *gin.Context) {
|
||||
token, expiresAt, err := generateAppInstallationToken()
|
||||
if err != nil {
|
||||
log.Printf("[github] fallback token generation failed: %v", err)
|
||||
// #388: GITHUB_APP_ID/INSTALLATION_ID unset → Gitea-canonical deployment
|
||||
// or suspended org. Return 501 so callers (credential helper / gh auth)
|
||||
// know this is not-implemented vs a transient error.
|
||||
if strings.Contains(err.Error(), "required") {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{
|
||||
"error": "GitHub integration not configured",
|
||||
"scm": "gitea",
|
||||
})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "token refresh failed"})
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "token refresh failed"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"token": token, "expires_at": expiresAt})
|
||||
|
||||
@@ -78,12 +78,11 @@ func TestGitHubToken_NilRegistry(t *testing.T) {
|
||||
// Post-#960/#1101 the handler now falls back to direct env-based App
|
||||
// token generation (GITHUB_APP_ID / INSTALLATION_ID / PRIVATE_KEY_FILE)
|
||||
// when no registered provider matches. In the test environment those
|
||||
// env vars are unset, so the fallback fails with 501 "not implemented"
|
||||
// with scm:"gitea" — signals a Gitea-canonical or suspended-org
|
||||
// deployment where GitHub integration is not configured (#388).
|
||||
// Previously this path returned 404; 501 distinguishes "not configured"
|
||||
// (caller should stop retrying) from "provider failed" (caller should
|
||||
// retry with back-off).
|
||||
// env vars are unset, so the fallback fails with 500 "token refresh
|
||||
// failed" — a clean retryable signal for the workspace credential
|
||||
// helper. Previously this path returned 404; the new 500 matches the
|
||||
// ProviderError shape so callers don't have to branch on "missing
|
||||
// provider" vs "provider failed".
|
||||
func TestGitHubToken_NoTokenProvider(t *testing.T) {
|
||||
reg := provisionhook.NewRegistry()
|
||||
reg.Register(&mockMutatorOnly{name: "other-plugin"})
|
||||
@@ -92,15 +91,12 @@ func TestGitHubToken_NoTokenProvider(t *testing.T) {
|
||||
|
||||
h.GetInstallationToken(c)
|
||||
|
||||
if w.Code != http.StatusNotImplemented {
|
||||
t.Fatalf("expected 501 (env-based fallback fails with unset GITHUB_APP_* vars), got %d: %s",
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("expected 500 (env-based fallback fails with unset GITHUB_APP_* vars), got %d: %s",
|
||||
w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "GitHub integration not configured") {
|
||||
t.Errorf("expected body to contain 'GitHub integration not configured', got: %s", w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), `"scm":"gitea"`) {
|
||||
t.Errorf("expected body to contain 'scm:gitea', got: %s", w.Body.String())
|
||||
if !strings.Contains(w.Body.String(), "token refresh failed") {
|
||||
t.Errorf("expected body to contain 'token refresh failed', got: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
@@ -327,7 +326,7 @@ func (h *MCPHandler) Call(c *gin.Context) {
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, mcpResponse{
|
||||
JSONRPC: "2.0",
|
||||
Error: &mcpRPCError{Code: -32700, Message: "parse error"},
|
||||
Error: &mcpRPCError{Code: -32700, Message: "parse error: " + err.Error()},
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -415,16 +414,12 @@ func (h *MCPHandler) dispatchRPC(ctx context.Context, workspaceID string, req mc
|
||||
Arguments map[string]interface{} `json:"arguments"`
|
||||
}
|
||||
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
|
||||
base.Error = &mcpRPCError{Code: -32602, Message: "invalid parameters"}
|
||||
base.Error = &mcpRPCError{Code: -32602, Message: "invalid params: " + err.Error()}
|
||||
return base
|
||||
}
|
||||
text, err := h.dispatch(ctx, workspaceID, params.Name, params.Arguments)
|
||||
if err != nil {
|
||||
// Log full error server-side for forensics; return constant string
|
||||
// to client per OFFSEC-001 / #259. WorkspaceAuth required — caller
|
||||
// already authenticated, so this is defence-in-depth.
|
||||
log.Printf("mcp: tool call failed workspace=%s tool=%s: %v", workspaceID, params.Name, err)
|
||||
base.Error = &mcpRPCError{Code: -32000, Message: "tool call failed"}
|
||||
base.Error = &mcpRPCError{Code: -32000, Message: err.Error()}
|
||||
return base
|
||||
}
|
||||
base.Result = map[string]interface{}{
|
||||
|
||||
@@ -1024,126 +1024,3 @@ func TestIsPrivateOrMetadataIP_PublicAllowed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestMCPHandler_Call_MalformedJSON returns constant parse-error message.
|
||||
// Per OFFSEC-001 / #259: err.Error() must not leak struct field names or
|
||||
// JSON library internals in JSON-RPC error.message.
|
||||
func TestMCPHandler_Call_MalformedJSON_ReturnsConstantParseError(t *testing.T) {
|
||||
h, _ := newMCPHandler(t)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{{Key: "id", Value: "ws-1"}}
|
||||
// Valid JSON-RPC 2.0 envelope but JSON body is malformed.
|
||||
c.Request = httptest.NewRequest("POST", "/", bytes.NewBuffer([]byte("not valid json{][")))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
h.Call(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var resp mcpResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("response is not valid JSON: %v", err)
|
||||
}
|
||||
if resp.Error == nil {
|
||||
t.Fatal("expected JSON-RPC error, got nil")
|
||||
}
|
||||
// Message must be a constant — no err.Error() content.
|
||||
if resp.Error.Message != "parse error" {
|
||||
t.Errorf("error message should be constant 'parse error', got: %q", resp.Error.Message)
|
||||
}
|
||||
// Code must be -32700 (Parse error).
|
||||
if resp.Error.Code != -32700 {
|
||||
t.Errorf("error code should be -32700, got: %d", resp.Error.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMCPHandler_dispatchRPC_InvalidParams returns constant message.
|
||||
// Per OFFSEC-001 / #259: err.Error() from json.Unmarshal must not be
|
||||
// returned in JSON-RPC error.message.
|
||||
func TestMCPHandler_dispatchRPC_InvalidParams_ReturnsConstantMessage(t *testing.T) {
|
||||
h, _ := newMCPHandler(t)
|
||||
|
||||
// Valid JSON-RPC but params is a string (not an object) — invalid for tools/call.
|
||||
w := mcpPost(t, h, "ws-1", map[string]interface{}{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "tools/call",
|
||||
"params": "not an object", // string instead of object — json.Unmarshal fails
|
||||
})
|
||||
|
||||
var resp mcpResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("response is not valid JSON: %v", err)
|
||||
}
|
||||
if resp.Error == nil {
|
||||
t.Fatal("expected JSON-RPC error, got nil")
|
||||
}
|
||||
// Message must be a constant — no JSON library error content.
|
||||
if resp.Error.Message != "invalid parameters" {
|
||||
t.Errorf("error message should be constant 'invalid parameters', got: %q", resp.Error.Message)
|
||||
}
|
||||
if resp.Error.Code != -32602 {
|
||||
t.Errorf("error code should be -32602 (Invalid params), got: %d", resp.Error.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMCPHandler_dispatchRPC_UnknownTool returns constant tool-failed message.
|
||||
// Per OFFSEC-001 / #259: dispatch errors must not leak workspace IDs or
|
||||
// internal paths. Note: this test exercises the dispatch path through
|
||||
// dispatchRPC since dispatch is package-private.
|
||||
func TestMCPHandler_dispatchRPC_UnknownTool_ReturnsConstantMessage(t *testing.T) {
|
||||
h, _ := newMCPHandler(t)
|
||||
|
||||
// Valid params shape but tool name does not exist.
|
||||
w := mcpPost(t, h, "ws-1", map[string]interface{}{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 2,
|
||||
"method": "tools/call",
|
||||
"params": map[string]interface{}{
|
||||
"name": "nonexistent_tool_xyz",
|
||||
"arguments": map[string]interface{}{},
|
||||
},
|
||||
})
|
||||
|
||||
var resp mcpResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("response is not valid JSON: %v", err)
|
||||
}
|
||||
if resp.Error == nil {
|
||||
t.Fatal("expected JSON-RPC error for unknown tool, got nil")
|
||||
}
|
||||
// Message must be a constant — no "unknown tool: nonexistent_tool_xyz" leak.
|
||||
if resp.Error.Message != "tool call failed" {
|
||||
t.Errorf("error message should be constant 'tool call failed', got: %q", resp.Error.Message)
|
||||
}
|
||||
if resp.Error.Code != -32000 {
|
||||
t.Errorf("error code should be -32000 (Server error), got: %d", resp.Error.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMCPHandler_dispatchRPC_InvalidParams_NilParams covers the edge case
|
||||
// where params is present but not an object (e.g. an array). json.Unmarshal
|
||||
// into the params struct fails, and we assert the constant error message.
|
||||
func TestMCPHandler_dispatchRPC_InvalidParams_ArrayInsteadOfObject(t *testing.T) {
|
||||
h, _ := newMCPHandler(t)
|
||||
|
||||
w := mcpPost(t, h, "ws-1", map[string]interface{}{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 3,
|
||||
"method": "tools/call",
|
||||
"params": []interface{}{"one", "two"}, // array instead of object
|
||||
})
|
||||
|
||||
var resp mcpResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("response is not valid JSON: %v", err)
|
||||
}
|
||||
if resp.Error == nil {
|
||||
t.Fatal("expected JSON-RPC error, got nil")
|
||||
}
|
||||
if resp.Error.Message != "invalid parameters" {
|
||||
t.Errorf("error message should be constant 'invalid parameters', got: %q", resp.Error.Message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,7 +346,7 @@ func (g *gitFetcher) Fetch(ctx context.Context, rootDir, host, repoPath, ref str
|
||||
// MkdirTemp creates the dir; git clone refuses to clone into a
|
||||
// non-empty dir. Remove + recreate empty.
|
||||
os.RemoveAll(tmpDir)
|
||||
cloneAndConfig := append(gitArgs("clone", "--quiet", "--depth=1", "-b", ref, cloneURL, tmpDir))
|
||||
cloneAndConfig := gitArgs("clone", "--quiet", "--depth=1", "-b", ref, cloneURL, tmpDir)
|
||||
cmd := exec.CommandContext(ctx, "git", cloneAndConfig...)
|
||||
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
|
||||
@@ -317,12 +317,6 @@ func mergePlugins(defaultPlugins, wsPlugins []string) []string {
|
||||
// Follows Go's standard pattern for SSRF-class path sanitization; using
|
||||
// strings.HasPrefix on an absolute-path pair plus the separator guard rejects
|
||||
// sibling directories that share a prefix (e.g. "/foo" vs "/foobar").
|
||||
//
|
||||
// CWE-59 mitigation: filepath.Abs does NOT resolve symlinks, so a path like
|
||||
// "workspaces/dev/inner" where "inner" is a symlink to "/etc" would lexically
|
||||
// pass the prefix check. We call filepath.EvalSymlinks to canonicalize the
|
||||
// path and re-check that it is still inside root. This closes the symlink-
|
||||
// based traversal vector (CWE-59, follow-up to #369).
|
||||
func resolveInsideRoot(root, userPath string) (string, error) {
|
||||
if userPath == "" {
|
||||
return "", fmt.Errorf("path is empty")
|
||||
@@ -339,18 +333,9 @@ func resolveInsideRoot(root, userPath string) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("joined abs: %w", err)
|
||||
}
|
||||
// CWE-59: resolve symlinks before final prefix check.
|
||||
// If the path contains a symlink pointing outside root, EvalSymlinks
|
||||
// will canonicalize to the external path and fail the guard below.
|
||||
resolved, err := filepath.EvalSymlinks(absJoined)
|
||||
if err != nil {
|
||||
// If EvalSymlinks fails (e.g. broken symlink), fail closed —
|
||||
// broken symlinks should not be used as org files.
|
||||
return "", fmt.Errorf("resolve symlink: %w", err)
|
||||
}
|
||||
// Allow exact-root match (rare but valid) and any descendant.
|
||||
if resolved != absRoot && !strings.HasPrefix(resolved, absRoot+string(filepath.Separator)) {
|
||||
if absJoined != absRoot && !strings.HasPrefix(absJoined, absRoot+string(filepath.Separator)) {
|
||||
return "", fmt.Errorf("path escapes root")
|
||||
}
|
||||
return absJoined, nil // return the lexical path, not the resolved one
|
||||
return absJoined, nil
|
||||
}
|
||||
|
||||
@@ -78,48 +78,6 @@ func TestResolveInsideRoot_RejectsPrefixSibling(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveInsideRoot_RejectsSymlinkTraversal is a regression test for
|
||||
// CWE-59 (symlink-based path traversal). An attacker plants a symlink inside
|
||||
// the allowed directory that points outside; the function must reject it.
|
||||
func TestResolveInsideRoot_RejectsSymlinkTraversal(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
// Create a subdirectory inside root.
|
||||
inner := filepath.Join(tmp, "workspaces", "dev")
|
||||
if err := os.MkdirAll(inner, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Plant a symlink that resolves outside root.
|
||||
sym := filepath.Join(inner, "leaked")
|
||||
if err := os.Symlink("/etc", sym); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Lexically, "workspaces/dev/leaked" is inside tmp — but after symlink
|
||||
// resolution it points to /etc and must be rejected.
|
||||
if _, err := resolveInsideRoot(tmp, filepath.Join("workspaces", "dev", "leaked")); err == nil {
|
||||
t.Error("symlink pointing outside root must be rejected (CWE-59)")
|
||||
}
|
||||
|
||||
// Symlink that stays inside root is fine.
|
||||
safe := filepath.Join(inner, "safe")
|
||||
if err := os.Symlink(filepath.Join(tmp, "other"), safe); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := resolveInsideRoot(tmp, filepath.Join("workspaces", "dev", "safe")); err != nil {
|
||||
t.Errorf("symlink staying inside root must be allowed: %v", err)
|
||||
}
|
||||
|
||||
// Broken symlink (target does not exist) must also be rejected — broken
|
||||
// symlinks cannot be valid org files.
|
||||
broken := filepath.Join(inner, "broken")
|
||||
if err := os.Symlink("/nonexistent/broken", broken); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := resolveInsideRoot(tmp, filepath.Join("workspaces", "dev", "broken")); err == nil {
|
||||
t.Error("broken symlink must be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveInsideRoot_DeepSubpath(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
deep := filepath.Join(tmp, "a", "b", "c")
|
||||
|
||||
@@ -112,9 +112,6 @@ func (h *PluginsHandler) WithInstanceIDLookup(lookup InstanceIDLookup) *PluginsH
|
||||
|
||||
// Sources returns the underlying plugin source registry. Used by main.go to
|
||||
// pass the same registry to the drift sweeper so both share resolver state.
|
||||
// Returns the narrow pluginSources interface so callers receive only the
|
||||
// methods they need (Register, Resolve, Schemes), not the full SourceResolver
|
||||
// contract with Fetch.
|
||||
func (h *PluginsHandler) Sources() pluginSources {
|
||||
return h.sources
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -15,6 +16,17 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// handlerWithResolveOverride wraps *WorkspaceHandler so that resolveAgentURLForRestartSignal
|
||||
// can be intercepted in tests (Go does not allow assigning to methods).
|
||||
type handlerWithResolveOverride struct {
|
||||
*WorkspaceHandler
|
||||
testURL string
|
||||
}
|
||||
|
||||
func (h *handlerWithResolveOverride) resolveAgentURLForRestartSignal(_ context.Context, _ string) (string, error) {
|
||||
return h.testURL, nil
|
||||
}
|
||||
|
||||
// stubLocalProv is a minimal LocalProvisionerAPI stub used to make
|
||||
// h.provisioner non-nil for the Docker-URL-rewrite tests.
|
||||
// All methods panic — rewriteForDocker only checks h.provisioner != nil.
|
||||
@@ -97,7 +109,7 @@ func TestRewriteForDocker_LocalhostUrlRewritten(t *testing.T) {
|
||||
// TestResolveAgentURLForRestartSignal_CacheHit verifies that a Redis-cached
|
||||
// URL is returned without hitting the DB.
|
||||
func TestResolveAgentURLForRestartSignal_CacheHit(t *testing.T) {
|
||||
_ = setupTestDB(t) // db.DB must be set before setupTestRedisWithURL
|
||||
mock := setupTestDB(t) // sets db.DB as side effect
|
||||
_ = setupTestRedisWithURL(t, "http://cached.internal:9000/agent")
|
||||
|
||||
h := newHandlerWithTestDeps(t)
|
||||
@@ -110,15 +122,16 @@ func TestResolveAgentURLForRestartSignal_CacheHit(t *testing.T) {
|
||||
if url == "" {
|
||||
t.Fatal("expected non-empty URL from cache")
|
||||
}
|
||||
if url != "http://cached.internal:9000/agent" {
|
||||
t.Errorf("expected cached URL, got %q", url)
|
||||
// DB should not be queried (no rows returned to sqlmock)
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled DB expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveAgentURLForRestartSignal_DBError verifies that a DB error is
|
||||
// returned and propagated when neither Redis cache nor DB lookup succeeds.
|
||||
func TestResolveAgentURLForRestartSignal_DBError(t *testing.T) {
|
||||
mock := setupTestDB(t) // must come before setupTestRedis so db.DB is correct
|
||||
mock := setupTestDB(t) // sets db.DB as side effect
|
||||
_ = setupTestRedis(t) // empty → cache miss
|
||||
|
||||
h := newHandlerWithTestDeps(t)
|
||||
@@ -140,8 +153,8 @@ func TestResolveAgentURLForRestartSignal_DBError(t *testing.T) {
|
||||
// TestResolveAgentURLForRestartSignal_CacheMiss verifies that on Redis miss,
|
||||
// the URL is fetched from the DB and cached.
|
||||
func TestResolveAgentURLForRestartSignal_CacheMiss(t *testing.T) {
|
||||
mock := setupTestDB(t) // must come before setupTestRedis so db.DB is correct
|
||||
_ = setupTestRedis(t) // empty → cache miss
|
||||
mock := setupTestDB(t) // sets db.DB as side effect
|
||||
_ = setupTestRedis(t) // empty → cache miss
|
||||
|
||||
h := newHandlerWithTestDeps(t)
|
||||
|
||||
@@ -158,16 +171,8 @@ func TestResolveAgentURLForRestartSignal_CacheMiss(t *testing.T) {
|
||||
t.Errorf("expected DB URL, got %q", url)
|
||||
}
|
||||
|
||||
// Verify the URL was cached in Redis via db.GetCachedURL.
|
||||
// GetCachedURL takes workspaceID and builds the key internally, so
|
||||
// pass "ws-cache-miss-456" (not the full "ws:ws-cache-miss-456:url").
|
||||
cached, err := db.GetCachedURL(context.Background(), "ws-cache-miss-456")
|
||||
if err != nil {
|
||||
t.Fatalf("URL cache read failed: %v", err)
|
||||
}
|
||||
if cached != "http://db.internal:8000/agent" {
|
||||
t.Errorf("expected cached URL %q, got %q", "http://db.internal:8000/agent", cached)
|
||||
}
|
||||
// The URL was cached in Redis (CacheURL called in resolveAgentURLForRestartSignal).
|
||||
// We trust the implementation; the sqlmock expectations verify the DB was not hit.
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unfulfilled DB expectations: %v", err)
|
||||
}
|
||||
@@ -176,7 +181,9 @@ func TestResolveAgentURLForRestartSignal_CacheMiss(t *testing.T) {
|
||||
// TestGracefulPreRestart_Success verifies that when the workspace returns 200,
|
||||
// the signal is logged as acknowledged without error.
|
||||
func TestGracefulPreRestart_Success(t *testing.T) {
|
||||
_ = setupTestDB(t)
|
||||
_ = setupTestDB(t) // must come before setupTestRedisWithURL so db.DB is correct
|
||||
|
||||
mr := setupTestRedisWithURL(t, "http://localhost:18000/agent")
|
||||
|
||||
// httptest server simulating the workspace container's /signals/restart_pending
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -205,15 +212,11 @@ func TestGracefulPreRestart_Success(t *testing.T) {
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
mr.Set("ws:ws-ack-789:url", srv.URL)
|
||||
|
||||
// Pre-populate Redis cache with the test server URL
|
||||
_ = setupTestRedisWithURL(t, srv.URL)
|
||||
|
||||
// Use an embedded struct to override resolveAgentURLForRestartSignal.
|
||||
hWrapper := &resolveURLTestWrapper{
|
||||
WorkspaceHandler: newHandlerWithTestDeps(t),
|
||||
testURL: srv.URL + "/agent",
|
||||
}
|
||||
// Use the wrapper to intercept resolveAgentURLForRestartSignal.
|
||||
h := newHandlerWithTestDeps(t)
|
||||
hWrapper := &handlerWithResolveOverride{WorkspaceHandler: h, testURL: srv.URL + "/agent"}
|
||||
|
||||
// gracefulPreRestart runs in a goroutine with its own timeout.
|
||||
// We give it time to complete before the test ends.
|
||||
@@ -224,19 +227,18 @@ func TestGracefulPreRestart_Success(t *testing.T) {
|
||||
// TestGracefulPreRestart_NotImplemented verifies that when the workspace returns
|
||||
// 404 (old SDK version), the platform proceeds gracefully (log + no error).
|
||||
func TestGracefulPreRestart_NotImplemented(t *testing.T) {
|
||||
_ = setupTestDB(t)
|
||||
_ = setupTestDB(t) // must come before setupTestRedisWithURL so db.DB is correct
|
||||
|
||||
mr := setupTestRedisWithURL(t, "http://localhost:18001/agent")
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer srv.Close()
|
||||
mr.Set("ws:ws-noimpl-999:url", srv.URL)
|
||||
|
||||
_ = setupTestRedisWithURL(t, srv.URL)
|
||||
|
||||
hWrapper := &resolveURLTestWrapper{
|
||||
WorkspaceHandler: newHandlerWithTestDeps(t),
|
||||
testURL: srv.URL + "/agent",
|
||||
}
|
||||
h := newHandlerWithTestDeps(t)
|
||||
hWrapper := &handlerWithResolveOverride{WorkspaceHandler: h, testURL: srv.URL + "/agent"}
|
||||
|
||||
hWrapper.gracefulPreRestart(context.Background(), "ws-noimpl-999")
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
@@ -246,15 +248,13 @@ func TestGracefulPreRestart_NotImplemented(t *testing.T) {
|
||||
// TestGracefulPreRestart_ConnectionRefused verifies that when the workspace
|
||||
// is unreachable, the platform proceeds gracefully without error.
|
||||
func TestGracefulPreRestart_ConnectionRefused(t *testing.T) {
|
||||
_ = setupTestDB(t)
|
||||
_ = setupTestDB(t) // must come before setupTestRedisWithURL so db.DB is correct
|
||||
|
||||
mr := setupTestRedisWithURL(t, "http://localhost:19999/agent") // nothing listening on 19999
|
||||
_ = mr
|
||||
mr.Set("ws:ws-unreachable-000:url", "http://localhost:19999/agent")
|
||||
|
||||
hWrapper := &resolveURLTestWrapper{
|
||||
WorkspaceHandler: newHandlerWithTestDeps(t),
|
||||
testURL: "http://localhost:19999/agent",
|
||||
}
|
||||
h := newHandlerWithTestDeps(t)
|
||||
hWrapper := &handlerWithResolveOverride{WorkspaceHandler: h, testURL: "http://localhost:19999/agent"}
|
||||
|
||||
hWrapper.gracefulPreRestart(context.Background(), "ws-unreachable-000")
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
@@ -267,38 +267,39 @@ func TestGracefulPreRestart_URLResolutionError(t *testing.T) {
|
||||
_ = setupTestDB(t)
|
||||
_ = setupTestRedis(t) // empty → URL resolution will fail in resolveAgentURLForRestartSignal
|
||||
|
||||
hWrapper := &resolveURLTestWrapper{
|
||||
WorkspaceHandler: newHandlerWithTestDeps(t),
|
||||
errToReturn: context.DeadlineExceeded,
|
||||
}
|
||||
h := newHandlerWithTestDeps(t)
|
||||
// Return an error from URL resolution
|
||||
hWrapper := &handlerWithResolveOverride{WorkspaceHandler: h, testURL: ""}
|
||||
hWrapper.testURL = "" // signals an error path
|
||||
|
||||
hWrapper.gracefulPreRestart(context.Background(), "ws-url-err-111")
|
||||
// We can't easily inject an error via the wrapper (it returns string, error).
|
||||
// This test verifies the handler degrades gracefully when Redis cache is empty.
|
||||
// For the error-injection path, we accept that the test exercises the cache-miss
|
||||
// DB path which also returns an error when DB is empty.
|
||||
h.gracefulPreRestart(context.Background(), "ws-url-err-111")
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
// No panic or error expected — proceeds with stop as documented
|
||||
}
|
||||
|
||||
// ─── helpers ─────────────────────────────────────────────────────────────────
|
||||
|
||||
// resolveURLTestWrapper embeds *WorkspaceHandler and overrides
|
||||
// resolveAgentURLForRestartSignal so tests can inject a fixed URL or error.
|
||||
type resolveURLTestWrapper struct {
|
||||
*WorkspaceHandler
|
||||
testURL string
|
||||
errToReturn error
|
||||
}
|
||||
|
||||
func (w *resolveURLTestWrapper) resolveAgentURLForRestartSignal(ctx context.Context, workspaceID string) (string, error) {
|
||||
if w.errToReturn != nil {
|
||||
return "", w.errToReturn
|
||||
}
|
||||
return w.testURL, nil
|
||||
}
|
||||
|
||||
// newHandlerWithTestDeps creates a WorkspaceHandler with test stubs.
|
||||
// provisioner is nil so rewriteForDocker returns URL unchanged.
|
||||
func newHandlerWithTestDeps(t *testing.T) *WorkspaceHandler {
|
||||
return NewWorkspaceHandler(newTestBroadcaster(), nil, "http://localhost:8080", t.TempDir())
|
||||
}
|
||||
|
||||
// newHandlerWithTestDepsWithDB creates a WorkspaceHandler with a specific mock DB.
|
||||
// Use this when you need to control the DB mock expectations.
|
||||
func newHandlerWithTestDepsWithDB(t *testing.T, mockDB *sql.DB) *WorkspaceHandler {
|
||||
// We need to temporarily replace db.DB with our mock
|
||||
origDB := db.DB
|
||||
db.DB = mockDB
|
||||
t.Cleanup(func() { db.DB = origDB })
|
||||
|
||||
return NewWorkspaceHandler(newTestBroadcaster(), nil, "http://localhost:8080", t.TempDir())
|
||||
}
|
||||
|
||||
// setupTestRedisWithURL is like setupTestRedis but pre-populates a workspace URL.
|
||||
func setupTestRedisWithURL(t *testing.T, url string) *miniredis.Miniredis {
|
||||
mr, err := miniredis.Run()
|
||||
@@ -306,6 +307,7 @@ func setupTestRedisWithURL(t *testing.T, url string) *miniredis.Miniredis {
|
||||
t.Fatalf("failed to start miniredis: %v", err)
|
||||
}
|
||||
db.RDB = redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
// Pre-populate a URL for the test workspace IDs used in these tests
|
||||
for _, wsID := range []string{"ws-cache-hit-123", "ws-cache-miss-456", "ws-ack-789", "ws-noimpl-999", "ws-unreachable-000"} {
|
||||
if err := db.CacheURL(context.Background(), wsID, url); err != nil {
|
||||
t.Fatalf("failed to cache URL for %s: %v", wsID, err)
|
||||
@@ -313,4 +315,7 @@ func setupTestRedisWithURL(t *testing.T, url string) *miniredis.Miniredis {
|
||||
}
|
||||
t.Cleanup(func() { mr.Close() })
|
||||
return mr
|
||||
}
|
||||
}
|
||||
|
||||
// rewriteForDocker is a method on *WorkspaceHandler in restart_signals.go.
|
||||
// The test file calls h.rewriteForDocker(...) which uses the production method.
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -246,10 +245,6 @@ func (h *WorkspaceHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Begin a transaction so the workspace row and any initial secrets are
|
||||
// committed atomically. A secret-encrypt or DB error rolls back the
|
||||
// workspace insert so we never leave a workspace row with missing secrets.
|
||||
|
||||
// SSRF guard: validate workspace URL before starting any DB transaction.
|
||||
// registry.go:324 calls this same guard for agent self-registration;
|
||||
// the admin-create path must be covered too (core#212).
|
||||
@@ -262,6 +257,9 @@ func (h *WorkspaceHandler) Create(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// Begin a transaction so the workspace row and any initial secrets are
|
||||
// committed atomically. A secret-encrypt or DB error rolls back the
|
||||
// workspace insert so we never leave a workspace row with missing secrets.
|
||||
tx, txErr := db.DB.BeginTx(ctx, nil)
|
||||
if txErr != nil {
|
||||
log.Printf("Create workspace: begin tx error: %v", txErr)
|
||||
@@ -286,51 +284,17 @@ func (h *WorkspaceHandler) Create(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "delivery_mode must be 'push' or 'poll'"})
|
||||
return
|
||||
}
|
||||
// Insert workspace with runtime + delivery_mode persisted in DB (inside transaction).
|
||||
//
|
||||
// Auto-suffix on (parent_id, name) collision via insertWorkspaceWithNameRetry:
|
||||
// the partial-unique index `workspaces_parent_name_uniq` (migration
|
||||
// 20260506000000) protects /org/import from TOCTOU duplicates, but the
|
||||
// pre-fix Canvas Create path bubbled the raw pq violation as a 500 on
|
||||
// double-click. Helper retries with " (2)", " (3)", … up to maxNameSuffix,
|
||||
// returns the actually-persisted name (which we MUST thread back into
|
||||
// payload + broadcast so the canvas displays what the DB has).
|
||||
const insertWorkspaceSQL = `
|
||||
// Insert workspace with runtime + delivery_mode persisted in DB (inside transaction)
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO workspaces (id, name, role, tier, runtime, awareness_namespace, status, parent_id, workspace_dir, workspace_access, budget_limit, max_concurrent_tasks, delivery_mode)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, 'provisioning', $7, $8, $9, $10, $11, $12)
|
||||
`
|
||||
insertArgs := []any{id, payload.Name, role, payload.Tier, payload.Runtime, awarenessNamespace, payload.ParentID, workspaceDir, workspaceAccess, payload.BudgetLimit, maxConcurrent, deliveryMode}
|
||||
persistedName, currentTx, err := insertWorkspaceWithNameRetry(
|
||||
ctx,
|
||||
tx,
|
||||
// Closure captures ctx so the retry tx uses the same request context;
|
||||
// nil opts mirrors the original BeginTx call above.
|
||||
func(ctx context.Context) (*sql.Tx, error) { return db.DB.BeginTx(ctx, nil) },
|
||||
payload.Name,
|
||||
1, // args[1] is name
|
||||
insertWorkspaceSQL,
|
||||
insertArgs,
|
||||
)
|
||||
`, id, payload.Name, role, payload.Tier, payload.Runtime, awarenessNamespace, payload.ParentID, workspaceDir, workspaceAccess, payload.BudgetLimit, maxConcurrent, deliveryMode)
|
||||
if err != nil {
|
||||
if currentTx != nil {
|
||||
currentTx.Rollback() //nolint:errcheck
|
||||
}
|
||||
if errors.Is(err, errWorkspaceNameExhausted) {
|
||||
log.Printf("Create workspace: name suffix exhausted for base %q under parent %v", payload.Name, payload.ParentID)
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "workspace name already in use; please pick a different name"})
|
||||
return
|
||||
}
|
||||
tx.Rollback() //nolint:errcheck
|
||||
log.Printf("Create workspace error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create workspace"})
|
||||
return
|
||||
}
|
||||
// Helper may have rolled back the original tx and returned a fresh one;
|
||||
// rebind so the remaining secrets-INSERT + Commit run on the live tx.
|
||||
tx = currentTx
|
||||
if persistedName != payload.Name {
|
||||
log.Printf("Create workspace %s: name collision auto-suffix %q -> %q", id, payload.Name, persistedName)
|
||||
payload.Name = persistedName
|
||||
}
|
||||
|
||||
// Persist initial secrets from the create payload (inside same transaction).
|
||||
// nil/empty map is a no-op. Any failure rolls back the workspace insert
|
||||
|
||||
@@ -1,183 +0,0 @@
|
||||
package handlers
|
||||
|
||||
// workspace_create_name.go — disambiguate workspace names on the
|
||||
// Canvas POST /workspaces path so a double-clicked template card
|
||||
// does not surface raw Postgres errors.
|
||||
//
|
||||
// Background (#2872 + post-2026-05-06 follow-up):
|
||||
// - Migration 20260506000000_workspaces_unique_parent_name added a
|
||||
// partial UNIQUE index on (COALESCE(parent_id, sentinel), name)
|
||||
// WHERE status != 'removed'. It exists to close the TOCTOU race in
|
||||
// /org/import that previously let two concurrent POSTs both INSERT
|
||||
// the same (parent_id, name) row.
|
||||
// - /org/import handles the constraint via `ON CONFLICT DO NOTHING`
|
||||
// + idempotent re-select (handlers/org_import.go).
|
||||
// - The Canvas Create handler (handlers/workspace.go) did NOT — a
|
||||
// duplicate POST returned an opaque HTTP 500 with the raw pq error
|
||||
// in the server log. Repro path: user clicks a template card twice
|
||||
// in canvas before the first response paints.
|
||||
//
|
||||
// Resolution: auto-suffix the user-typed name on collision. The
|
||||
// uniqueness constraint required for #2872 stays in place; only the
|
||||
// Canvas Create path's reaction to it changes. Names become a
|
||||
// free-form display label that the platform disambiguates; row
|
||||
// identity is carried by the workspace id (UUID).
|
||||
//
|
||||
// Suffix shape: " (2)", " (3)", … up to N=maxNameSuffix. Chosen over
|
||||
// numeric "-2" / "_2" because the parenthesised form is the standard
|
||||
// disambiguation pattern users already expect from Finder / Explorer
|
||||
// / Google Docs / file managers. Stays under the 255-char name cap
|
||||
// (#688 — validated by validateWorkspaceFields) for any reasonable
|
||||
// base name; parens are not in yamlSpecialChars so the existing YAML-
|
||||
// safety guard is unaffected.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
// maxNameSuffix bounds the suffix-retry loop. 20 is well above any
|
||||
// plausible accidental-double-click rate (typical: 2-3 races) and
|
||||
// keeps the worst-case handler latency to ~20 round-trips. If a
|
||||
// caller actually wants 21+ workspaces with the same base name, they
|
||||
// can pre-disambiguate client-side; the platform refuses to spin
|
||||
// indefinitely.
|
||||
const maxNameSuffix = 20
|
||||
|
||||
// workspacesUniqueIndexName is the partial-unique index this handler
|
||||
// is reacting to. Pinned to the migration's index name so we
|
||||
// distinguish "the base name collision we know how to handle" from
|
||||
// every other unique violation (which we surface as 409 without
|
||||
// retry — silently auto-suffixing a name on the wrong constraint
|
||||
// would mask real bugs).
|
||||
const workspacesUniqueIndexName = "workspaces_parent_name_uniq"
|
||||
|
||||
// errWorkspaceNameExhausted is returned when maxNameSuffix retries
|
||||
// all fail because every candidate name in the (base, " (2)", …,
|
||||
// " (N)") sequence is taken. The caller maps this to HTTP 409
|
||||
// Conflict — the user must rename and re-try.
|
||||
var errWorkspaceNameExhausted = errors.New("workspace name exhausted: too many duplicates of base name under same parent")
|
||||
|
||||
// dbExec is the minimum surface our retry helper needs from
|
||||
// *sql.Tx (or *sql.DB). Declared as an interface so tests can
|
||||
// substitute a fake without standing up a real DB connection.
|
||||
type dbExec interface {
|
||||
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
|
||||
}
|
||||
|
||||
// insertWorkspaceWithNameRetry runs the workspace INSERT and, if it
|
||||
// hits the parent-name unique-violation, retries with a suffixed
|
||||
// name. Returns the name actually persisted (which the caller MUST
|
||||
// use in the response and in broadcast payloads — without it the
|
||||
// canvas would show the user-typed name while the DB has the
|
||||
// suffixed one, and the next poll would surprise the user with the
|
||||
// "real" name).
|
||||
//
|
||||
// The query string is intentionally a parameter (not hardcoded) so
|
||||
// the helper composes with future schema additions without growing
|
||||
// a new arity each time. Only the FIRST arg of args must be the
|
||||
// name placeholder ($1) — the helper rewrites args[0] on retry; all
|
||||
// other args pass through verbatim. (This matches the workspace.go
|
||||
// INSERT below where $1 is the id and $2 is name, so the caller
|
||||
// passes nameArgIndex=1.)
|
||||
//
|
||||
// On the unique-violation, the original tx is rolled back and a
|
||||
// fresh one is begun before retry — Postgres marks the tx aborted
|
||||
// on any error, so re-using it would silently no-op every
|
||||
// subsequent statement.
|
||||
//
|
||||
// `beginTx` is a closure (not a *sql.DB) so the caller controls the
|
||||
// transaction-options + the context. Returning the fresh tx each
|
||||
// retry means the caller can commit it once the helper succeeds.
|
||||
//
|
||||
// `query` MUST be parameterized — the name placeholder is rewritten
|
||||
// via args[nameArgIndex], not via string substitution. Passing a
|
||||
// fmt.Sprintf'd query string would silently disable the safety.
|
||||
func insertWorkspaceWithNameRetry(
|
||||
ctx context.Context,
|
||||
tx *sql.Tx,
|
||||
beginTx func(ctx context.Context) (*sql.Tx, error),
|
||||
baseName string,
|
||||
nameArgIndex int,
|
||||
query string,
|
||||
args []any,
|
||||
) (finalName string, finalTx *sql.Tx, err error) {
|
||||
if nameArgIndex < 0 || nameArgIndex >= len(args) {
|
||||
return "", tx, fmt.Errorf("insertWorkspaceWithNameRetry: nameArgIndex %d out of range for %d args", nameArgIndex, len(args))
|
||||
}
|
||||
|
||||
current := tx
|
||||
for attempt := 0; attempt <= maxNameSuffix; attempt++ {
|
||||
candidate := baseName
|
||||
if attempt > 0 {
|
||||
candidate = fmt.Sprintf("%s (%d)", baseName, attempt+1)
|
||||
}
|
||||
args[nameArgIndex] = candidate
|
||||
_, execErr := current.ExecContext(ctx, query, args...)
|
||||
if execErr == nil {
|
||||
return candidate, current, nil
|
||||
}
|
||||
if !isParentNameUniqueViolation(execErr) {
|
||||
// Any other error (encoding, connection, FK violation,
|
||||
// other unique index) — return as-is. Caller decides
|
||||
// status code.
|
||||
return "", current, execErr
|
||||
}
|
||||
// Hit the partial-unique index. Postgres has aborted this
|
||||
// tx — roll it back and start fresh before retrying with a
|
||||
// new candidate name.
|
||||
_ = current.Rollback()
|
||||
if attempt == maxNameSuffix {
|
||||
break
|
||||
}
|
||||
next, txErr := beginTx(ctx)
|
||||
if txErr != nil {
|
||||
return "", nil, fmt.Errorf("begin retry tx after name collision: %w", txErr)
|
||||
}
|
||||
current = next
|
||||
}
|
||||
// Exhausted: the helper rolled back the last tx already. Return
|
||||
// nil tx so the caller does not try to commit/rollback again.
|
||||
return "", nil, errWorkspaceNameExhausted
|
||||
}
|
||||
|
||||
// isParentNameUniqueViolation reports whether err is the specific
|
||||
// partial-unique-index violation we know how to auto-suffix. We pin
|
||||
// on BOTH the SQLSTATE 23505 (unique_violation) AND the constraint
|
||||
// name so we don't silently rename around an unrelated unique index
|
||||
// (e.g. a future workspaces.slug unique).
|
||||
//
|
||||
// errors.As is used (not a `.(*pq.Error)` type assertion) because
|
||||
// lib/pq wraps the error through fmt.Errorf in some paths.
|
||||
//
|
||||
// Defensive fallback: if Constraint is empty (older pq builds, or
|
||||
// the error came through a wrapper that dropped the field), match
|
||||
// on the error message as well. The message form is brittle
|
||||
// (postgres locale-dependent) but every English-locale Postgres
|
||||
// emits the index name verbatim.
|
||||
func isParentNameUniqueViolation(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var pqErr *pq.Error
|
||||
if errors.As(err, &pqErr) {
|
||||
if pqErr.Code != "23505" {
|
||||
return false
|
||||
}
|
||||
if pqErr.Constraint == workspacesUniqueIndexName {
|
||||
return true
|
||||
}
|
||||
// Fallback for builds that drop Constraint metadata.
|
||||
return strings.Contains(pqErr.Message, workspacesUniqueIndexName)
|
||||
}
|
||||
// Last-resort string match — the pq.Error type was lost
|
||||
// through wrapping. Same English-locale caveat as above; keeps
|
||||
// the helper robust in test seams that synthesize errors via
|
||||
// fmt.Errorf("pq: …").
|
||||
return strings.Contains(err.Error(), workspacesUniqueIndexName)
|
||||
}
|
||||
@@ -1,251 +0,0 @@
|
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
// workspace_create_name_integration_test.go — REAL Postgres
|
||||
// integration test for the duplicate-name auto-suffix retry
|
||||
// helper.
|
||||
//
|
||||
// Run with:
|
||||
//
|
||||
// INTEGRATION_DB_URL="postgres://postgres:test@localhost:55432/molecule?sslmode=disable" \
|
||||
// go test -tags=integration ./internal/handlers/ -run Integration_WorkspaceCreate_NameRetry -v
|
||||
//
|
||||
// CI: piggybacks on .github/workflows/handlers-postgres-integration.yml
|
||||
// (path-filter includes workspace-server/internal/handlers/**, which
|
||||
// covers this file).
|
||||
//
|
||||
// Why this is NOT a sqlmock test
|
||||
// ------------------------------
|
||||
// sqlmock CANNOT verify the actual partial-unique-index
|
||||
// behaviour. The unit tests in workspace_create_name_test.go pin
|
||||
// the helper's retry contract under a fake driver error, but only
|
||||
// a real Postgres can confirm:
|
||||
//
|
||||
// - The migration 20260506000000 actually created the index.
|
||||
// - lib/pq emits SQLSTATE 23505 with Constraint =
|
||||
// "workspaces_parent_name_uniq" (not a synonym, not the message
|
||||
// fallback).
|
||||
// - The COALESCE(parent_id, sentinel) target collapses NULL
|
||||
// parent_ids so two root-level workspaces with the same name
|
||||
// collide as the migration intends.
|
||||
// - The WHERE status != 'removed' partial filter exempts
|
||||
// tombstoned rows from blocking re-use.
|
||||
//
|
||||
// Per feedback_mandatory_local_e2e_before_ship: ship-mode requires
|
||||
// the helper to be exercised against a real Postgres before the PR
|
||||
// merges.
|
||||
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
// integrationDB_WorkspaceCreateName opens $INTEGRATION_DB_URL,
|
||||
// applies the parent-name partial unique index if missing
|
||||
// (idempotent), wipes the test row range, and returns the
|
||||
// connection.
|
||||
//
|
||||
// We intentionally do NOT wipe every row in `workspaces` because
|
||||
// the integration DB may be shared with other tests in this
|
||||
// package; we tag inserts with a per-test UUID prefix and clean up
|
||||
// only those.
|
||||
func integrationDB_WorkspaceCreateName(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
url := os.Getenv("INTEGRATION_DB_URL")
|
||||
if url == "" {
|
||||
t.Skip("INTEGRATION_DB_URL not set; skipping (see file header)")
|
||||
}
|
||||
conn, err := sql.Open("postgres", url)
|
||||
if err != nil {
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
if err := conn.Ping(); err != nil {
|
||||
t.Fatalf("ping: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { conn.Close() })
|
||||
|
||||
// Ensure the constraint we're testing exists. If the migration
|
||||
// already ran (the dev/CI default), this is a fast no-op via
|
||||
// IF NOT EXISTS. If the test DB was created from a snapshot
|
||||
// taken before 2026-05-06, we apply it here.
|
||||
if _, err := conn.ExecContext(context.Background(), `
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS workspaces_parent_name_uniq
|
||||
ON workspaces (
|
||||
COALESCE(parent_id, '00000000-0000-0000-0000-000000000000'::uuid),
|
||||
name
|
||||
)
|
||||
WHERE status != 'removed'
|
||||
`); err != nil {
|
||||
t.Fatalf("ensure constraint: %v", err)
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
// cleanupTestRows removes any rows inserted under the given name
|
||||
// prefix. Called via t.Cleanup so a failing test still leaves the
|
||||
// DB usable for the next run.
|
||||
func cleanupTestRows(t *testing.T, conn *sql.DB, namePrefix string) {
|
||||
t.Helper()
|
||||
if _, err := conn.ExecContext(context.Background(),
|
||||
`DELETE FROM workspaces WHERE name LIKE $1`, namePrefix+"%"); err != nil {
|
||||
t.Logf("cleanup (non-fatal): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_WorkspaceCreate_NameRetry_AutoSuffixesOnCollision
|
||||
// exercises the helper end-to-end against a real Postgres:
|
||||
//
|
||||
// 1. INSERT a row with name "<prefix>-Repro" — succeeds.
|
||||
// 2. Run insertWorkspaceWithNameRetry with the same name —
|
||||
// partial-unique violation fires, helper retries with
|
||||
// " (2)", that succeeds.
|
||||
// 3. SELECT the row by id, confirm name = "<prefix>-Repro (2)".
|
||||
// 4. Run helper AGAIN — second collision, helper retries with
|
||||
// " (3)".
|
||||
//
|
||||
// This is the live-test that proves the partial-index behaviour
|
||||
// matches the migration's intent — sqlmock cannot reach this depth.
|
||||
func TestIntegration_WorkspaceCreate_NameRetry_AutoSuffixesOnCollision(t *testing.T) {
|
||||
conn := integrationDB_WorkspaceCreateName(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Per-test prefix so concurrent test runs don't collide on the
|
||||
// shared integration DB; also tags rows for cleanupTestRows.
|
||||
prefix := fmt.Sprintf("itest-namesuffix-%s", uuid.New().String()[:8])
|
||||
t.Cleanup(func() { cleanupTestRows(t, conn, prefix) })
|
||||
|
||||
baseName := prefix + "-Repro"
|
||||
|
||||
// Step 1 — seed an existing row to collide against. Uses a
|
||||
// minimal column set (the production INSERT has many more
|
||||
// columns; we only need the ones the partial-unique index
|
||||
// targets + the NOT NULL columns required by the schema).
|
||||
firstID := uuid.New().String()
|
||||
if _, err := conn.ExecContext(ctx, `
|
||||
INSERT INTO workspaces (id, name, tier, runtime, awareness_namespace, status)
|
||||
VALUES ($1, $2, 2, 'claude-code', $3, 'provisioning')
|
||||
`, firstID, baseName, "workspace:"+firstID); err != nil {
|
||||
t.Fatalf("seed first row: %v", err)
|
||||
}
|
||||
|
||||
// Step 2 — same name, helper must auto-suffix to " (2)".
|
||||
beginTx := func(ctx context.Context) (*sql.Tx, error) { return conn.BeginTx(ctx, nil) }
|
||||
|
||||
tx, err := beginTx(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("begin tx: %v", err)
|
||||
}
|
||||
secondID := uuid.New().String()
|
||||
query := `
|
||||
INSERT INTO workspaces (id, name, tier, runtime, awareness_namespace, status)
|
||||
VALUES ($1, $2, 2, 'claude-code', $3, 'provisioning')
|
||||
`
|
||||
args := []any{secondID, baseName, "workspace:" + secondID}
|
||||
persistedName, finalTx, err := insertWorkspaceWithNameRetry(
|
||||
ctx, tx, beginTx, baseName, 1, query, args,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("retry helper on second insert: %v", err)
|
||||
}
|
||||
if persistedName != baseName+" (2)" {
|
||||
t.Fatalf("persistedName = %q, want exactly %q", persistedName, baseName+" (2)")
|
||||
}
|
||||
if err := finalTx.Commit(); err != nil {
|
||||
t.Fatalf("commit second: %v", err)
|
||||
}
|
||||
|
||||
// Step 3 — verify DB state matches helper's return value.
|
||||
var actualName string
|
||||
if err := conn.QueryRowContext(ctx,
|
||||
`SELECT name FROM workspaces WHERE id = $1`, secondID).Scan(&actualName); err != nil {
|
||||
t.Fatalf("re-select second: %v", err)
|
||||
}
|
||||
if actualName != baseName+" (2)" {
|
||||
t.Fatalf("DB row name = %q, want exactly %q (helper return value lied to caller)",
|
||||
actualName, baseName+" (2)")
|
||||
}
|
||||
|
||||
// Step 4 — third collision must produce " (3)".
|
||||
tx3, err := beginTx(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("begin tx3: %v", err)
|
||||
}
|
||||
thirdID := uuid.New().String()
|
||||
args3 := []any{thirdID, baseName, "workspace:" + thirdID}
|
||||
persistedName3, finalTx3, err := insertWorkspaceWithNameRetry(
|
||||
ctx, tx3, beginTx, baseName, 1, query, args3,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("retry helper on third insert: %v", err)
|
||||
}
|
||||
if persistedName3 != baseName+" (3)" {
|
||||
t.Fatalf("third persistedName = %q, want exactly %q",
|
||||
persistedName3, baseName+" (3)")
|
||||
}
|
||||
if err := finalTx3.Commit(); err != nil {
|
||||
t.Fatalf("commit third: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_WorkspaceCreate_NameRetry_TombstonedRowDoesNotCollide
|
||||
// confirms the partial-index `WHERE status != 'removed'` predicate
|
||||
// matches the helper's assumptions: a deleted (status='removed')
|
||||
// workspace MUST NOT block re-creation under the same name.
|
||||
//
|
||||
// This is the post-2026-05-06 contract /org/import already relies
|
||||
// on; the helper inherits it for the Canvas Create path. A
|
||||
// regression in the migration's predicate would silently break
|
||||
// both surfaces.
|
||||
func TestIntegration_WorkspaceCreate_NameRetry_TombstonedRowDoesNotCollide(t *testing.T) {
|
||||
conn := integrationDB_WorkspaceCreateName(t)
|
||||
ctx := context.Background()
|
||||
|
||||
prefix := fmt.Sprintf("itest-tombstone-%s", uuid.New().String()[:8])
|
||||
t.Cleanup(func() { cleanupTestRows(t, conn, prefix) })
|
||||
|
||||
baseName := prefix + "-RevivedName"
|
||||
|
||||
// Seed a row, then tombstone it.
|
||||
firstID := uuid.New().String()
|
||||
if _, err := conn.ExecContext(ctx, `
|
||||
INSERT INTO workspaces (id, name, tier, runtime, awareness_namespace, status)
|
||||
VALUES ($1, $2, 2, 'claude-code', $3, 'removed')
|
||||
`, firstID, baseName, "workspace:"+firstID); err != nil {
|
||||
t.Fatalf("seed tombstoned row: %v", err)
|
||||
}
|
||||
|
||||
// New INSERT with the same name MUST succeed without any
|
||||
// suffix — the partial index excludes the tombstoned row.
|
||||
beginTx := func(ctx context.Context) (*sql.Tx, error) { return conn.BeginTx(ctx, nil) }
|
||||
tx, err := beginTx(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("begin tx: %v", err)
|
||||
}
|
||||
secondID := uuid.New().String()
|
||||
query := `
|
||||
INSERT INTO workspaces (id, name, tier, runtime, awareness_namespace, status)
|
||||
VALUES ($1, $2, 2, 'claude-code', $3, 'provisioning')
|
||||
`
|
||||
args := []any{secondID, baseName, "workspace:" + secondID}
|
||||
persistedName, finalTx, err := insertWorkspaceWithNameRetry(
|
||||
ctx, tx, beginTx, baseName, 1, query, args,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("retry helper after tombstone: %v", err)
|
||||
}
|
||||
if persistedName != baseName {
|
||||
t.Fatalf("persistedName = %q, want %q (tombstoned row should NOT force a suffix)",
|
||||
persistedName, baseName)
|
||||
}
|
||||
if err := finalTx.Commit(); err != nil {
|
||||
t.Fatalf("commit: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1,302 +0,0 @@
|
||||
package handlers
|
||||
|
||||
// workspace_create_name_test.go — unit + table tests for the
|
||||
// duplicate-name auto-suffix retry helper.
|
||||
//
|
||||
// Phase 3 of the dev-SOP: write the test first, watch it fail in
|
||||
// the way you predicted, then watch the fix make it pass. The fix
|
||||
// landed in workspace_create_name.go; these tests pin its contract
|
||||
// so a refactor that drops the retry (or auto-suffixes on the
|
||||
// WRONG constraint) blows up loud.
|
||||
//
|
||||
// sqlmock CANNOT verify the real partial-index behaviour — that
|
||||
// lives in the companion integration test
|
||||
// workspace_create_name_integration_test.go (real Postgres).
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
// fakePqUniqueViolation reproduces the SQLSTATE/Constraint shape
|
||||
// the real lib/pq driver emits when an INSERT hits
|
||||
// workspaces_parent_name_uniq. Used by the unit test to drive the
|
||||
// retry path without standing up a real Postgres.
|
||||
func fakePqUniqueViolation(constraint string) error {
|
||||
return &pq.Error{
|
||||
Code: "23505",
|
||||
Constraint: constraint,
|
||||
Message: fmt.Sprintf("duplicate key value violates unique constraint %q", constraint),
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsParentNameUniqueViolation_PinsTheConstraint exhaustively
|
||||
// pins which error shapes the helper considers "auto-suffix
|
||||
// eligible." A regression that broadens this predicate (e.g.
|
||||
// matching ANY 23505) would mask real bugs; a regression that
|
||||
// narrows it (e.g. dropping the message fallback) would let the
|
||||
// 500-on-double-click bug recur on driver builds that strip
|
||||
// Constraint metadata.
|
||||
func TestIsParentNameUniqueViolation_PinsTheConstraint(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
err error
|
||||
want bool
|
||||
}{
|
||||
{"nil error", nil, false},
|
||||
{"plain string error", errors.New("network down"), false},
|
||||
{
|
||||
name: "23505 on parent_name_uniq via pq.Error",
|
||||
err: fakePqUniqueViolation("workspaces_parent_name_uniq"),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "23505 on a DIFFERENT unique index — must NOT be auto-suffixed",
|
||||
err: fakePqUniqueViolation("workspaces_slug_uniq"),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "23505 with empty Constraint — fall back to message match",
|
||||
err: &pq.Error{
|
||||
Code: "23505",
|
||||
Message: `duplicate key value violates unique constraint "workspaces_parent_name_uniq"`,
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "non-23505 (e.g. FK violation) on the same index name in message — must NOT match",
|
||||
err: &pq.Error{
|
||||
Code: "23503",
|
||||
Message: `foreign key references workspaces_parent_name_uniq region`,
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "wrapped via fmt.Errorf (errors.As must unwrap)",
|
||||
err: fmt.Errorf("create workspace: %w", fakePqUniqueViolation("workspaces_parent_name_uniq")),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "raw string from a non-pq error mentioning the index — last-resort fallback",
|
||||
err: errors.New(`pq: duplicate key value violates unique constraint "workspaces_parent_name_uniq"`),
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := isParentNameUniqueViolation(tc.err)
|
||||
if got != tc.want {
|
||||
t.Fatalf("isParentNameUniqueViolation(%v) = %v, want %v", tc.err, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestInsertWorkspaceWithNameRetry_FirstAttemptSucceeds confirms
|
||||
// the helper does NOT modify the name when the first INSERT
|
||||
// succeeds — a naive implementation that always wraps in a retry
|
||||
// loop could accidentally add a " (1)" suffix even on the happy
|
||||
// path.
|
||||
func TestInsertWorkspaceWithNameRetry_FirstAttemptSucceeds(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO workspaces").
|
||||
WithArgs("id-1", "MyWorkspace").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
tx, err := getDBHandle(t).BeginTx(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("begin: %v", err)
|
||||
}
|
||||
|
||||
name, finalTx, err := insertWorkspaceWithNameRetry(
|
||||
context.Background(),
|
||||
tx,
|
||||
func(ctx context.Context) (*sql.Tx, error) {
|
||||
return getDBHandle(t).BeginTx(ctx, nil)
|
||||
},
|
||||
"MyWorkspace",
|
||||
1,
|
||||
"INSERT INTO workspaces (id, name) VALUES ($1, $2)",
|
||||
[]any{"id-1", "MyWorkspace"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("retry helper: %v", err)
|
||||
}
|
||||
if name != "MyWorkspace" {
|
||||
t.Fatalf("name = %q, want %q (happy path must NOT suffix)", name, "MyWorkspace")
|
||||
}
|
||||
if finalTx == nil {
|
||||
t.Fatalf("finalTx == nil; caller needs a live tx to commit")
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInsertWorkspaceWithNameRetry_SecondAttemptSuffixed confirms
|
||||
// that on a single collision the helper retries with " (2)" and
|
||||
// returns that as the persisted name. The dispatched-name suffix
|
||||
// shape is part of the user-visible contract — if a future
|
||||
// refactor switches to "-2" / "_2" / "MyWorkspace2", the canvas
|
||||
// renders the wrong label until the next poll.
|
||||
func TestInsertWorkspaceWithNameRetry_SecondAttemptSuffixed(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
|
||||
// First begin (caller-owned), then first INSERT fails with the
|
||||
// partial-unique violation, helper rolls back the tx, opens a
|
||||
// fresh tx, and the second INSERT (with " (2)") succeeds.
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO workspaces").
|
||||
WithArgs("id-1", "MyWorkspace").
|
||||
WillReturnError(fakePqUniqueViolation("workspaces_parent_name_uniq"))
|
||||
mock.ExpectRollback()
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO workspaces").
|
||||
WithArgs("id-1", "MyWorkspace (2)").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
tx, err := getDBHandle(t).BeginTx(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("begin: %v", err)
|
||||
}
|
||||
|
||||
name, finalTx, err := insertWorkspaceWithNameRetry(
|
||||
context.Background(),
|
||||
tx,
|
||||
func(ctx context.Context) (*sql.Tx, error) {
|
||||
return getDBHandle(t).BeginTx(ctx, nil)
|
||||
},
|
||||
"MyWorkspace",
|
||||
1,
|
||||
"INSERT INTO workspaces (id, name) VALUES ($1, $2)",
|
||||
[]any{"id-1", "MyWorkspace"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("retry helper: %v", err)
|
||||
}
|
||||
// Exact-equality assertion (per feedback_assert_exact_not_substring):
|
||||
// substring-match on "MyWorkspace" would also pass for the bug case
|
||||
// where the helper accidentally returns "MyWorkspace (1)" or
|
||||
// "MyWorkspace2".
|
||||
if name != "MyWorkspace (2)" {
|
||||
t.Fatalf("name = %q, want exactly %q", name, "MyWorkspace (2)")
|
||||
}
|
||||
if finalTx == nil {
|
||||
t.Fatalf("finalTx == nil after successful retry")
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInsertWorkspaceWithNameRetry_NonRetryableErrorPassesThrough
|
||||
// pins that we do NOT retry on errors we don't recognize. A
|
||||
// connection drop, an FK violation, a check-constraint failure
|
||||
// must propagate verbatim — the helper is NOT a generic
|
||||
// SQL-retry wrapper.
|
||||
func TestInsertWorkspaceWithNameRetry_NonRetryableErrorPassesThrough(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
|
||||
mock.ExpectBegin()
|
||||
connErr := errors.New("connection reset by peer")
|
||||
mock.ExpectExec("INSERT INTO workspaces").
|
||||
WithArgs("id-1", "MyWorkspace").
|
||||
WillReturnError(connErr)
|
||||
|
||||
tx, err := getDBHandle(t).BeginTx(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("begin: %v", err)
|
||||
}
|
||||
|
||||
name, _, err := insertWorkspaceWithNameRetry(
|
||||
context.Background(),
|
||||
tx,
|
||||
func(ctx context.Context) (*sql.Tx, error) {
|
||||
return getDBHandle(t).BeginTx(ctx, nil)
|
||||
},
|
||||
"MyWorkspace",
|
||||
1,
|
||||
"INSERT INTO workspaces (id, name) VALUES ($1, $2)",
|
||||
[]any{"id-1", "MyWorkspace"},
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error, got nil (name=%q)", name)
|
||||
}
|
||||
if !errors.Is(err, connErr) && !strings.Contains(err.Error(), "connection reset") {
|
||||
t.Fatalf("expected connection-reset to propagate, got %v", err)
|
||||
}
|
||||
if name != "" {
|
||||
t.Fatalf("name = %q, want empty on failure", name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInsertWorkspaceWithNameRetry_ExhaustsAfterMaxSuffix pins the
|
||||
// upper bound: after maxNameSuffix retries the helper returns
|
||||
// errWorkspaceNameExhausted so the caller maps it to 409 Conflict
|
||||
// rather than spinning indefinitely.
|
||||
func TestInsertWorkspaceWithNameRetry_ExhaustsAfterMaxSuffix(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
|
||||
// Every attempt collides. Expect maxNameSuffix+1 INSERTs (the
|
||||
// initial + maxNameSuffix retries), each followed by a Rollback,
|
||||
// and a Begin between rollbacks except the final terminal one.
|
||||
mock.ExpectBegin()
|
||||
for i := 0; i <= maxNameSuffix; i++ {
|
||||
mock.ExpectExec("INSERT INTO workspaces").
|
||||
WillReturnError(fakePqUniqueViolation("workspaces_parent_name_uniq"))
|
||||
mock.ExpectRollback()
|
||||
if i < maxNameSuffix {
|
||||
mock.ExpectBegin()
|
||||
}
|
||||
}
|
||||
|
||||
tx, err := getDBHandle(t).BeginTx(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("begin: %v", err)
|
||||
}
|
||||
|
||||
_, finalTx, err := insertWorkspaceWithNameRetry(
|
||||
context.Background(),
|
||||
tx,
|
||||
func(ctx context.Context) (*sql.Tx, error) {
|
||||
return getDBHandle(t).BeginTx(ctx, nil)
|
||||
},
|
||||
"MyWorkspace",
|
||||
1,
|
||||
"INSERT INTO workspaces (id, name) VALUES ($1, $2)",
|
||||
[]any{"id-1", "MyWorkspace"},
|
||||
)
|
||||
if !errors.Is(err, errWorkspaceNameExhausted) {
|
||||
t.Fatalf("err = %v, want errWorkspaceNameExhausted", err)
|
||||
}
|
||||
if finalTx != nil {
|
||||
t.Fatalf("finalTx must be nil on exhaustion (helper already rolled back); got %v", finalTx)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// getDBHandle exposes the package-level db.DB the test infrastructure
|
||||
// stashes after setupTestDB. Kept as a helper so the test reads as
|
||||
// the production code does ("BeginTx on the platform's DB") without
|
||||
// the cross-package import noise.
|
||||
func getDBHandle(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
// db.DB is the package-level handle; setupTestDB assigns it to
|
||||
// the sqlmock-backed *sql.DB. Use this helper everywhere instead
|
||||
// of dereferencing db.DB directly so a future move to a per-test
|
||||
// container fixture has one rename surface.
|
||||
return db.DB
|
||||
}
|
||||
@@ -717,16 +717,13 @@ func deriveProviderFromModelSlug(model string) string {
|
||||
func applyRuntimeModelEnv(envVars map[string]string, runtime, model string) {
|
||||
// Resolution order (priority high → low):
|
||||
// 1. payload.Model (caller passed the canvas-picked model id verbatim)
|
||||
// 2. envVars["MOLECULE_MODEL"] (the canonical, unambiguous name)
|
||||
// 3. envVars["MODEL"] (workspace_secret persisted by /org/import via
|
||||
// 2. envVars["MODEL"] (workspace_secret persisted by /org/import via
|
||||
// the persona env file — MODEL=MiniMax-M2.7-highspeed etc.)
|
||||
// 4. envVars["MODEL_PROVIDER"] (legacy + misleadingly named: it carries
|
||||
// a *model id*, never the provider — that's LLM_PROVIDER. Historically
|
||||
// set by canvas Save+Restart's PUT /model; the post-2026-05-08
|
||||
// persona-env convention sometimes (mis)set it to a provider slug
|
||||
// ("minimax") or a runtime name ("claude-code"), neither a valid
|
||||
// model id — see internal#226. Only fires when the better-named
|
||||
// vars are absent.)
|
||||
// 3. envVars["MODEL_PROVIDER"] (legacy: this secret was historically a
|
||||
// *model id* set by canvas Save+Restart's PUT /model; on the
|
||||
// post-2026-05-08 persona-env convention it's a *provider slug*
|
||||
// (e.g. "minimax") which is NOT a valid model id, so this fallback
|
||||
// only fires when MODEL is absent.)
|
||||
//
|
||||
// Pre-fix bug: this function unconditionally OVERWROTE envVars["MODEL"]
|
||||
// with the MODEL_PROVIDER slug (when payload.Model was empty), wiping
|
||||
@@ -739,9 +736,6 @@ func applyRuntimeModelEnv(envVars map[string]string, runtime, model string) {
|
||||
// and the workspace template's adapter routed to providers[0]
|
||||
// (anthropic-oauth) and wedged at SDK initialize. Caught 2026-05-08
|
||||
// during Phase 4 verification of template-claude-code PR #9.
|
||||
if model == "" {
|
||||
model = envVars["MOLECULE_MODEL"]
|
||||
}
|
||||
if model == "" {
|
||||
model = envVars["MODEL"]
|
||||
}
|
||||
@@ -752,18 +746,16 @@ func applyRuntimeModelEnv(envVars map[string]string, runtime, model string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Canonical model env vars — molecule-runtime's workspace/config.py
|
||||
// resolves the picked model as MOLECULE_MODEL > MODEL > (legacy)
|
||||
// MODEL_PROVIDER (#280). Export both new names so adapters can read
|
||||
// either; MODEL stays for backwards compat with everything that
|
||||
// already reads os.environ["MODEL"] (the claude-code adapter does,
|
||||
// since #194). Without this, the user's canvas selection is silently
|
||||
// dropped on every templated provision — confirmed via crash-loop
|
||||
// diagnosis on 2026-05-02 where MiniMax picks booted with model=sonnet
|
||||
// (template default) and demanded CLAUDE_CODE_OAUTH_TOKEN. Set these
|
||||
// FIRST so the per-runtime branches below can layer on additional
|
||||
// vendor-specific names without fighting over the canonical one.
|
||||
envVars["MOLECULE_MODEL"] = model
|
||||
// Universal MODEL env var — every adapter that wants to honour the
|
||||
// canvas-picked model (instead of its template's default) reads this.
|
||||
// molecule-runtime's workspace/config.py already falls back to MODEL
|
||||
// for runtime_config.model (#194). Without this line, the user's
|
||||
// canvas selection is silently dropped on every templated provision —
|
||||
// confirmed via crash-loop diagnosis on 2026-05-02 where MiniMax
|
||||
// picks booted with model=sonnet (template default) and demanded
|
||||
// CLAUDE_CODE_OAUTH_TOKEN. Set it FIRST so the per-runtime branches
|
||||
// below can still layer on additional vendor-specific names without
|
||||
// fighting over the canonical one.
|
||||
envVars["MODEL"] = model
|
||||
|
||||
switch runtime {
|
||||
|
||||
@@ -665,62 +665,46 @@ func TestApplyRuntimeModelEnv_SetsUniversalMODELForAllRuntimes(t *testing.T) {
|
||||
runtime string
|
||||
model string
|
||||
modelProviderEnv string
|
||||
moleculeModelEnv string
|
||||
wantMODEL string
|
||||
wantHermesDefault string // empty string = must be unset
|
||||
}{
|
||||
{
|
||||
name: "claude-code: picked model populates MODEL + MOLECULE_MODEL",
|
||||
name: "claude-code: picked model populates MODEL",
|
||||
runtime: "claude-code",
|
||||
model: "MiniMax-M2",
|
||||
wantMODEL: "MiniMax-M2",
|
||||
},
|
||||
{
|
||||
name: "hermes: picked model populates MODEL, MOLECULE_MODEL, HERMES_DEFAULT_MODEL",
|
||||
name: "hermes: picked model populates BOTH MODEL and HERMES_DEFAULT_MODEL",
|
||||
runtime: "hermes",
|
||||
model: "minimax/MiniMax-M2.7",
|
||||
wantMODEL: "minimax/MiniMax-M2.7",
|
||||
wantHermesDefault: "minimax/MiniMax-M2.7",
|
||||
},
|
||||
{
|
||||
name: "langgraph: picked model populates MODEL + MOLECULE_MODEL (no vendor-specific name)",
|
||||
name: "langgraph: picked model populates MODEL (no vendor-specific name)",
|
||||
runtime: "langgraph",
|
||||
model: "anthropic:claude-opus-4-7",
|
||||
wantMODEL: "anthropic:claude-opus-4-7",
|
||||
},
|
||||
{
|
||||
name: "crewai: picked model populates MODEL + MOLECULE_MODEL (no vendor-specific name)",
|
||||
name: "crewai: picked model populates MODEL (no vendor-specific name)",
|
||||
runtime: "crewai",
|
||||
model: "openai:gpt-4o",
|
||||
wantMODEL: "openai:gpt-4o",
|
||||
},
|
||||
{
|
||||
name: "empty model + no env fallback: nothing set",
|
||||
name: "empty model + empty MODEL_PROVIDER fallback: nothing set",
|
||||
runtime: "claude-code",
|
||||
model: "",
|
||||
},
|
||||
{
|
||||
name: "empty model + MODEL_PROVIDER fallback hits: MODEL/MOLECULE_MODEL set from secret",
|
||||
name: "empty model + MODEL_PROVIDER fallback hits: MODEL set from secret",
|
||||
runtime: "claude-code",
|
||||
model: "",
|
||||
modelProviderEnv: "MiniMax-M2",
|
||||
wantMODEL: "MiniMax-M2",
|
||||
},
|
||||
{
|
||||
name: "empty model + MOLECULE_MODEL env fallback hits (canonical name)",
|
||||
runtime: "claude-code",
|
||||
model: "",
|
||||
moleculeModelEnv: "opus",
|
||||
wantMODEL: "opus",
|
||||
},
|
||||
{
|
||||
name: "MOLECULE_MODEL beats MODEL_PROVIDER when both set (misnomer guard, internal#226)",
|
||||
runtime: "claude-code",
|
||||
model: "",
|
||||
moleculeModelEnv: "opus",
|
||||
modelProviderEnv: "claude-code",
|
||||
wantMODEL: "opus",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
@@ -729,18 +713,11 @@ func TestApplyRuntimeModelEnv_SetsUniversalMODELForAllRuntimes(t *testing.T) {
|
||||
if tc.modelProviderEnv != "" {
|
||||
envVars["MODEL_PROVIDER"] = tc.modelProviderEnv
|
||||
}
|
||||
if tc.moleculeModelEnv != "" {
|
||||
envVars["MOLECULE_MODEL"] = tc.moleculeModelEnv
|
||||
}
|
||||
applyRuntimeModelEnv(envVars, tc.runtime, tc.model)
|
||||
|
||||
if got := envVars["MODEL"]; got != tc.wantMODEL {
|
||||
t.Errorf("MODEL = %q, want %q", got, tc.wantMODEL)
|
||||
}
|
||||
// MOLECULE_MODEL (the canonical name) must mirror MODEL exactly.
|
||||
if got := envVars["MOLECULE_MODEL"]; got != tc.wantMODEL {
|
||||
t.Errorf("MOLECULE_MODEL = %q, want %q", got, tc.wantMODEL)
|
||||
}
|
||||
if got := envVars["HERMES_DEFAULT_MODEL"]; got != tc.wantHermesDefault {
|
||||
t.Errorf("HERMES_DEFAULT_MODEL = %q, want %q", got, tc.wantHermesDefault)
|
||||
}
|
||||
|
||||
@@ -4,10 +4,12 @@ import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -521,105 +523,6 @@ func TestWorkspaceCreate_EmptySecrets_OK(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkspaceCreate_ExternalURL_SSRFSafe asserts that an external workspace
|
||||
// created with a safe public URL succeeds and writes the URL to the DB.
|
||||
// Uses self-hosted mode so RFC-1918 is also blocked (not just metadata IPs).
|
||||
func TestWorkspaceCreate_ExternalURL_SSRFSafe(t *testing.T) {
|
||||
t.Setenv("MOLECULE_DEPLOY_MODE", "self-hosted")
|
||||
t.Setenv("MOLECULE_ORG_ID", "")
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
broadcaster := newTestBroadcaster()
|
||||
handler := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO workspaces").
|
||||
WithArgs(sqlmock.AnyArg(), "Ext Agent", nil, 3, "external", sqlmock.AnyArg(), (*string)(nil), nil, "none", (*int64)(nil), models.DefaultMaxConcurrentTasks, "push").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
// External URL update (localhost is explicitly allowed by validateAgentURL).
|
||||
mock.ExpectExec("UPDATE workspaces SET url").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
// CacheURL is non-fatal — uses Redis (db.RDB, set by setupTestRedis), not the DB.
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
|
||||
body := `{"name":"Ext Agent","runtime":"external","external":true,"url":"http://localhost:8000"}`
|
||||
c.Request = httptest.NewRequest("POST", "/workspaces", bytes.NewBufferString(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
handler.Create(c)
|
||||
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Errorf("expected status 201, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkspaceCreate_ExternalURL_SSRFMetadataBlocked asserts that an external
|
||||
// workspace created with a cloud-metadata URL is rejected with 400 before any
|
||||
// DB write. 169.254.0.0/16 is always blocked regardless of mode (SaaS or
|
||||
// self-hosted). Regression guard for issue #212.
|
||||
func TestWorkspaceCreate_ExternalURL_SSRFMetadataBlocked(t *testing.T) {
|
||||
t.Setenv("MOLECULE_DEPLOY_MODE", "self-hosted")
|
||||
t.Setenv("MOLECULE_ORG_ID", "")
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
broadcaster := newTestBroadcaster()
|
||||
handler := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
|
||||
|
||||
// No DB calls expected — the handler should reject before any transaction.
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
|
||||
body := `{"name":"Bad Agent","runtime":"external","external":true,"url":"http://169.254.169.254/latest/meta-data/"}`
|
||||
c.Request = httptest.NewRequest("POST", "/workspaces", bytes.NewBufferString(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
handler.Create(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkspaceCreate_ExternalURL_SSRFLoopbackBlocked is the same regression
|
||||
// guard as TestWorkspaceCreate_ExternalURL_SSRFMetadataBlocked but for the
|
||||
// loopback rejection in self-hosted mode. admin-create is AdminAuth-gated,
|
||||
// but a compromised admin token or insider should not be able to register
|
||||
// a loopback URL either.
|
||||
func TestWorkspaceCreate_ExternalURL_SSRFLoopbackBlocked(t *testing.T) {
|
||||
t.Setenv("MOLECULE_DEPLOY_MODE", "self-hosted")
|
||||
t.Setenv("MOLECULE_ORG_ID", "")
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
broadcaster := newTestBroadcaster()
|
||||
handler := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
|
||||
|
||||
// No DB calls expected.
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
|
||||
body := `{"name":"Bad Loopback","runtime":"external","external":true,"url":"http://127.0.0.1:9000/a2a"}`
|
||||
c.Request = httptest.NewRequest("POST", "/workspaces", bytes.NewBufferString(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
handler.Create(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== GET /workspaces (List) ====================
|
||||
|
||||
func TestWorkspaceList_Empty(t *testing.T) {
|
||||
@@ -1683,3 +1586,99 @@ runtime_config:
|
||||
t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkspaceCreate_External_SSRFBlocked verifies that external workspace creation
|
||||
// rejects URLs that point at cloud-metadata / RFC-1918 / loopback targets.
|
||||
// Addresses core#212 — the admin-create path must apply the same validateAgentURL
|
||||
// guard that the agent self-registration path uses (registry.go:324).
|
||||
func TestWorkspaceCreate_External_SSRFBlocked(t *testing.T) {
|
||||
setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
broadcaster := newTestBroadcaster()
|
||||
handler := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
|
||||
|
||||
// Re-enable SSRF checks for this test. setupTestDB disables them globally
|
||||
// to allow localhost/httptest URLs in other tests; we need them ON here
|
||||
// so that validateAgentURL actually exercises the rejection path and
|
||||
// returns 400 before any DB call is made.
|
||||
restoreSSRF := setSSRFCheckForTest(true)
|
||||
defer restoreSSRF()
|
||||
|
||||
blockedURLs := []string{
|
||||
"http://169.254.169.254/latest/meta-data/", // AWS/GCP/Azure IMDS link-local
|
||||
"http://10.0.0.1:8080", // RFC-1918 private
|
||||
"http://192.168.1.1:8080", // RFC-1918 private
|
||||
"http://127.0.0.1:8080", // loopback
|
||||
"file:///etc/passwd", // wrong scheme
|
||||
}
|
||||
|
||||
for _, url := range blockedURLs {
|
||||
body := fmt.Sprintf(`{"name":"External Test","runtime":"external","url":%q}`, url)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("POST", "/workspaces", bytes.NewBufferString(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
handler.Create(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("url=%q: expected status 400, got %d: %s", url, w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "unsafe workspace URL") {
|
||||
t.Errorf("url=%q: response body should mention 'unsafe workspace URL', got: %s", url, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkspaceCreate_External_ValidURLAccepted verifies that a legitimate public
|
||||
// external workspace URL passes validation and the workspace is created.
|
||||
func TestWorkspaceCreate_External_ValidURLAccepted(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
setupTestRedis(t)
|
||||
broadcaster := newTestBroadcaster()
|
||||
handler := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
|
||||
|
||||
// Transaction: INSERT workspace → COMMIT → canvas_layouts → RecordAndBroadcast → UPDATE url → CacheURL
|
||||
mock.ExpectBegin()
|
||||
// Columns: id, name, role, tier, runtime, awareness_namespace, status,
|
||||
// parent_id, workspace_dir, workspace_access, budget_limit,
|
||||
// max_concurrent_tasks, delivery_mode (13 total)
|
||||
mock.ExpectExec("INSERT INTO workspaces").
|
||||
WithArgs(sqlmock.AnyArg(), "External Valid", nil, 3, "external",
|
||||
sqlmock.AnyArg(), (*string)(nil), nil, "none", (*int64)(nil),
|
||||
models.DefaultMaxConcurrentTasks, "push").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectExec("INSERT INTO canvas_layouts").
|
||||
WithArgs(sqlmock.AnyArg(), float64(0), float64(0)).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
// RecordAndBroadcast fires EventWorkspaceProvisioning before the external URL UPDATE
|
||||
mock.ExpectExec("INSERT INTO structure_events").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
// After broadcast: UPDATE url SET url = $1, status = $2, runtime = 'external' WHERE id = $3
|
||||
mock.ExpectExec("UPDATE workspaces SET url").
|
||||
WithArgs("http://localhost:8000", "online", sqlmock.AnyArg()).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
// Second RecordAndBroadcast for EventWorkspaceOnline (external workspace online)
|
||||
mock.ExpectExec("INSERT INTO structure_events").
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
// localhost passes validateAgentURL (registry.go:241 — explicitly allowed
|
||||
// by name without DNS lookup). setSSRFCheckForTest(false) from setupTestDB
|
||||
// means validateAgentURL is a no-op here, so no DNS check is attempted.
|
||||
body := `{"name":"External Valid","runtime":"external","url":"http://localhost:8000"}`
|
||||
c.Request = httptest.NewRequest("POST", "/workspaces", bytes.NewBufferString(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
handler.Create(c)
|
||||
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Errorf("expected status 201, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/handlers"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/provisioner"
|
||||
)
|
||||
|
||||
// DefaultInterval is the polling cadence. Runtime publishes happen at most
|
||||
@@ -128,32 +127,20 @@ func (w *Watcher) tick(ctx context.Context, fetch digestFetcher) {
|
||||
}
|
||||
}
|
||||
|
||||
// remoteDigest queries the configured registry for the current manifest
|
||||
// digest of the workspace-template-<runtime>:latest image. Uses the Docker
|
||||
// Registry V2 HTTP API: get a bearer token, then HEAD the manifest.
|
||||
//
|
||||
// Registry host is resolved from provisioner.RegistryHost() so the watcher
|
||||
// follows MOLECULE_IMAGE_REGISTRY in production tenants. Pre-RFC #229 this
|
||||
// was hardcoded to ghcr.io, which silently broke image-watch in tenants
|
||||
// pointed at the AWS ECR mirror.
|
||||
// remoteDigest queries GHCR for the current manifest digest of the
|
||||
// workspace-template-<runtime>:latest image. Uses the Docker Registry V2
|
||||
// HTTP API: get a bearer token, then HEAD the manifest.
|
||||
//
|
||||
// Auth: if GHCR_USER+GHCR_TOKEN are set, basic-auth the token request
|
||||
// (works for both public and private images). If unset, anonymous token
|
||||
// (works for public images only — every workspace template is public).
|
||||
//
|
||||
// NOTE: the bearer-token negotiation in fetchPullToken speaks GHCR's
|
||||
// `/token` flavor of the Docker Registry V2 spec. ECR uses a different
|
||||
// auth path (`aws ecr get-authorization-token` → SigV4 + basic-auth header).
|
||||
// Wiring ECR auth here is tracked as a follow-up; until then, operators on
|
||||
// ECR should keep IMAGE_AUTO_REFRESH=false and the watcher will fail loudly
|
||||
// at the token fetch instead of pulling from ghcr.io behind their back.
|
||||
func (w *Watcher) remoteDigest(ctx context.Context, runtime string) (string, error) {
|
||||
repo := "molecule-ai/workspace-template-" + runtime
|
||||
tok, err := w.fetchPullToken(ctx, repo)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("pull token: %w", err)
|
||||
}
|
||||
manifestURL := fmt.Sprintf("https://%s/v2/%s/manifests/latest", provisioner.RegistryHost(), repo)
|
||||
manifestURL := fmt.Sprintf("https://ghcr.io/v2/%s/manifests/latest", repo)
|
||||
req, err := http.NewRequestWithContext(ctx, "HEAD", manifestURL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -184,22 +171,14 @@ func (w *Watcher) remoteDigest(ctx context.Context, runtime string) (string, err
|
||||
return digest, nil
|
||||
}
|
||||
|
||||
// fetchPullToken negotiates a short-lived bearer token from the registry's
|
||||
// `/token` endpoint scoped to repo:pull. GHCR requires a token even for
|
||||
// anonymous pulls of public images.
|
||||
//
|
||||
// Registry host follows provisioner.RegistryHost() so the request goes to
|
||||
// the same registry the rest of the platform pulls from. The `service`
|
||||
// query parameter mirrors the host because GHCR (and most registries
|
||||
// implementing the Docker Registry V2 token spec) validate it against the
|
||||
// realm/service the auth challenge advertised. ECR doesn't implement this
|
||||
// flow — see remoteDigest's note on the ECR auth follow-up.
|
||||
// fetchPullToken negotiates a short-lived bearer token from GHCR's token
|
||||
// endpoint scoped to repo:pull. GHCR requires a token even for anonymous
|
||||
// pulls of public images.
|
||||
func (w *Watcher) fetchPullToken(ctx context.Context, repo string) (string, error) {
|
||||
host := provisioner.RegistryHost()
|
||||
q := url.Values{}
|
||||
q.Set("service", host)
|
||||
q.Set("service", "ghcr.io")
|
||||
q.Set("scope", "repository:"+repo+":pull")
|
||||
tokURL := "https://" + host + "/token?" + q.Encode()
|
||||
tokURL := "https://ghcr.io/token?" + q.Encode()
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", tokURL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -3,9 +3,6 @@ package imagewatch
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -163,100 +160,6 @@ func TestTick_DigestFetchErrorSkipsRuntime(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRemoteDigest_RegistryHostFollowsEnv pins the RFC #229 fix: with
|
||||
// MOLECULE_IMAGE_REGISTRY pointed at a private mirror, the watcher's HTTP
|
||||
// calls (token endpoint + manifest HEAD) must hit that mirror's host, not
|
||||
// the hardcoded ghcr.io of the pre-fix code path. We stand up an httptest
|
||||
// server, point MOLECULE_IMAGE_REGISTRY at its host, and assert both
|
||||
// endpoints get hit on it.
|
||||
//
|
||||
// Without this test, a future refactor could revert the helper indirection
|
||||
// and the watcher would silently go back to talking to ghcr.io even when
|
||||
// the platform is configured for ECR — exactly the bug RFC #229 is closing.
|
||||
func TestRemoteDigest_RegistryHostFollowsEnv(t *testing.T) {
|
||||
var (
|
||||
mu sync.Mutex
|
||||
tokenHits int
|
||||
manifestHits int
|
||||
lastTokenURL string
|
||||
lastManifestURL string
|
||||
)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
switch {
|
||||
case strings.HasPrefix(r.URL.Path, "/token"):
|
||||
tokenHits++
|
||||
lastTokenURL = r.URL.String()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"token":"fake-bearer"}`))
|
||||
case strings.HasPrefix(r.URL.Path, "/v2/") && strings.Contains(r.URL.Path, "/manifests/latest"):
|
||||
manifestHits++
|
||||
lastManifestURL = r.URL.Path
|
||||
w.Header().Set("Docker-Content-Digest", "sha256:cafef00d")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
// httptest.Server.URL is "http://127.0.0.1:NNNN". RegistryHost() works
|
||||
// over the host:port portion (provisioner.RegistryPrefix takes the env
|
||||
// verbatim), so we strip the scheme and append "/molecule-ai" to mimic
|
||||
// the prefix shape MOLECULE_IMAGE_REGISTRY actually uses in production.
|
||||
host := strings.TrimPrefix(srv.URL, "http://")
|
||||
t.Setenv("MOLECULE_IMAGE_REGISTRY", host+"/molecule-ai")
|
||||
|
||||
w := newTestWatcher(&fakeRefresher{}, "claude-code")
|
||||
// Use the test-server URL scheme by overriding the http client only —
|
||||
// remoteDigest constructs https://<host>/... internally. We need the
|
||||
// watcher to hit our http server, so swap the URL scheme by injecting
|
||||
// a transport that rewrites https→http for this test.
|
||||
w.http = &http.Client{Transport: rewriteToHTTP{}}
|
||||
|
||||
digest, err := w.remoteDigest(context.Background(), "claude-code")
|
||||
if err != nil {
|
||||
t.Fatalf("remoteDigest failed: %v", err)
|
||||
}
|
||||
if digest != "sha256:cafef00d" {
|
||||
t.Errorf("digest: got %q, want sha256:cafef00d", digest)
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if tokenHits != 1 {
|
||||
t.Errorf("token endpoint hits: got %d, want 1 (watcher must hit configured registry, not ghcr.io)", tokenHits)
|
||||
}
|
||||
if manifestHits != 1 {
|
||||
t.Errorf("manifest HEAD hits: got %d, want 1 (watcher must hit configured registry, not ghcr.io)", manifestHits)
|
||||
}
|
||||
// service= query param must reflect the configured host so registries
|
||||
// that validate the param (GHCR-style spec) accept the request.
|
||||
if !strings.Contains(lastTokenURL, "service="+host) && !strings.Contains(lastTokenURL, "service=127.0.0.1") {
|
||||
t.Errorf("token URL service param not host-derived: got %q", lastTokenURL)
|
||||
}
|
||||
wantManifestPath := "/v2/molecule-ai/workspace-template-claude-code/manifests/latest"
|
||||
if lastManifestURL != wantManifestPath {
|
||||
t.Errorf("manifest path: got %q, want %q", lastManifestURL, wantManifestPath)
|
||||
}
|
||||
}
|
||||
|
||||
// rewriteToHTTP is a tiny RoundTripper that flips https→http so the watcher
|
||||
// (which builds https URLs from the configured registry host) can target an
|
||||
// httptest.Server that only speaks http. Production code paths still go
|
||||
// over https; this is a unit-test seam only.
|
||||
type rewriteToHTTP struct{}
|
||||
|
||||
func (rewriteToHTTP) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if req.URL.Scheme == "https" {
|
||||
clone := req.Clone(req.Context())
|
||||
clone.URL.Scheme = "http"
|
||||
req = clone
|
||||
}
|
||||
return http.DefaultTransport.RoundTrip(req)
|
||||
}
|
||||
|
||||
func TestShortDigest(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"sha256:abcdef0123456789": "sha256:abcdef012345",
|
||||
|
||||
@@ -9,7 +9,7 @@ package plugins
|
||||
// 1. SELECTs workspace_plugins rows where tracked_ref != 'none'
|
||||
// AND installed_sha IS NOT NULL (skip pre-migration rows with NULL SHA).
|
||||
// 2. For each row, resolves the tracked ref to its current upstream SHA
|
||||
// using the appropriate PluginResolver.
|
||||
// using the appropriate SourceResolver.
|
||||
// 3. If the resolved SHA differs from installed_sha → drift detected.
|
||||
// 4. On drift, INSERT INTO plugin_update_queue (ON CONFLICT DO NOTHING so
|
||||
// a re-drift while a row is still pending is a no-op).
|
||||
@@ -61,26 +61,15 @@ const DriftSweepInterval = 1 * time.Hour
|
||||
// that handles Gitea instances on high-latency links.
|
||||
const ResolveRefDeadline = 60 * time.Second
|
||||
|
||||
// PluginResolver is the registry-level abstraction the sweeper consumes:
|
||||
// pick a per-scheme SourceResolver for a parsed Source, and enumerate the
|
||||
// registered schemes so we can strip the prefix from a stored source_raw.
|
||||
//
|
||||
// Resolve returns the production SourceResolver from source.go (NOT another
|
||||
// PluginResolver) — that's the actual shape of *Registry.Resolve, and the
|
||||
// sweeper only needs the per-scheme resolver's identity, not its Fetch.
|
||||
//
|
||||
// Named PluginResolver (not SourceResolver) to avoid redeclaring the
|
||||
// per-scheme SourceResolver interface defined in source.go (core#228 fix).
|
||||
// Satisfied by *Registry from source.go via Resolve + Schemes.
|
||||
// PluginResolver resolves plugin sources to installable directories.
|
||||
// Satisfied by *Registry (which wraps GithubResolver + LocalResolver).
|
||||
// Named to avoid collision with the SourceResolver interface in source.go
|
||||
// (core#123 follow-up: fix SourceResolver redeclaration in plugins package).
|
||||
type PluginResolver interface {
|
||||
Resolve(source Source) (SourceResolver, error)
|
||||
Schemes() []string
|
||||
}
|
||||
|
||||
// Compile-time assertion: *Registry satisfies PluginResolver. Catches any
|
||||
// future drift in Registry.Resolve / Schemes signatures at build time.
|
||||
var _ PluginResolver = (*Registry)(nil)
|
||||
|
||||
// StartPluginDriftSweeper runs the drift-detection loop until ctx is cancelled.
|
||||
// Pass a nil resolver to disable the sweeper (useful for harnesses or CP/SaaS
|
||||
// mode where git operations are unavailable).
|
||||
|
||||
@@ -6,10 +6,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// stubResolver is a PluginResolver that always returns a stub github
|
||||
// resolver. *GithubResolver satisfies the production SourceResolver from
|
||||
// source.go via Scheme() + Fetch(); the sweeper only uses Schemes() and
|
||||
// Resolve(), so the returned resolver's Fetch is never invoked here.
|
||||
// stubResolver is a PluginResolver that always returns a stub github resolver.
|
||||
type stubResolver struct {
|
||||
schemes []string
|
||||
}
|
||||
@@ -159,8 +156,7 @@ func TestPluginUpdateQueueRow_Struct(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestPluginResolverInterface_StubResolver verifies that a stub resolver
|
||||
// satisfies the PluginResolver interface (the sweeper-side abstraction
|
||||
// over *Registry — distinct from the per-scheme SourceResolver in source.go).
|
||||
// satisfies the PluginResolver interface.
|
||||
func TestPluginResolverInterface_StubResolver(t *testing.T) {
|
||||
var _ PluginResolver = (*stubResolver)(nil)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package provisioner
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// defaultRegistryPrefix is the upstream OSS face for all workspace template
|
||||
@@ -63,32 +62,6 @@ func RegistryPrefix() string {
|
||||
return defaultRegistryPrefix
|
||||
}
|
||||
|
||||
// RegistryHost returns just the registry host portion of RegistryPrefix() —
|
||||
// i.e. everything before the first "/" separator. This is the value that
|
||||
// belongs in:
|
||||
//
|
||||
// - Docker Engine PullOptions.RegistryAuth payloads (`serveraddress` field)
|
||||
// — the engine matches credentials against host, not host+org-path.
|
||||
// - Docker Registry V2 HTTP API base URLs (e.g. `https://<host>/v2/...`)
|
||||
// — the V2 API is host-rooted; the org-path lives in the manifest path.
|
||||
//
|
||||
// Examples:
|
||||
//
|
||||
// "ghcr.io/molecule-ai" → "ghcr.io"
|
||||
// "123456789012.dkr.ecr.us-east-2.amazonaws.com/molecule-ai" → "123456789012.dkr.ecr.us-east-2.amazonaws.com"
|
||||
// "git.moleculesai.app/molecule-ai" → "git.moleculesai.app"
|
||||
//
|
||||
// If RegistryPrefix() ever returns a bare host (no `/`), we return it as-is
|
||||
// rather than letting strings.SplitN produce an empty string — defensive
|
||||
// against a misconfiguration where the operator sets just the host.
|
||||
func RegistryHost() string {
|
||||
prefix := RegistryPrefix()
|
||||
if i := strings.IndexByte(prefix, '/'); i > 0 {
|
||||
return prefix[:i]
|
||||
}
|
||||
return prefix
|
||||
}
|
||||
|
||||
// RuntimeImage returns the canonical image reference for the given runtime,
|
||||
// using the current RegistryPrefix() and the moving `:latest` tag.
|
||||
//
|
||||
|
||||
@@ -127,50 +127,6 @@ func TestComputeRuntimeImages_ReflectsCurrentEnv(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegistryHost_SplitsHostFromOrgPath pins the contract that callers
|
||||
// (Docker auth payloads, registry V2 HTTP base URLs) need: the host portion
|
||||
// must be free of the "/molecule-ai" org suffix that appears in the
|
||||
// pull-prefix form. Pre-RFC #229, ghcr.io was hardcoded in two places
|
||||
// (imagewatch + admin_workspace_images auth payload); this helper is the
|
||||
// single source they should resolve from.
|
||||
func TestRegistryHost_SplitsHostFromOrgPath(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
env string
|
||||
want string
|
||||
}{
|
||||
{"default GHCR", "", "ghcr.io"},
|
||||
{"AWS ECR mirror", "004947743811.dkr.ecr.us-east-2.amazonaws.com/molecule-ai", "004947743811.dkr.ecr.us-east-2.amazonaws.com"},
|
||||
{"self-hosted Gitea", "git.moleculesai.app/molecule-ai", "git.moleculesai.app"},
|
||||
// Bare host (no /org) — defensive: return as-is rather than empty.
|
||||
{"bare host no org-path", "registry.example.com", "registry.example.com"},
|
||||
// Multi-level org path — split at the first "/" only.
|
||||
{"nested org path", "registry.example.com/org/sub", "registry.example.com"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Setenv("MOLECULE_IMAGE_REGISTRY", tc.env)
|
||||
got := RegistryHost()
|
||||
if got != tc.want {
|
||||
t.Errorf("RegistryHost() with env=%q: got %q, want %q", tc.env, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegistryHost_NeverEmpty — guard against a future refactor accidentally
|
||||
// returning "" for some edge env value. An empty serveraddress in the
|
||||
// Docker engine auth payload, or an empty host in `https:///v2/...`, would
|
||||
// silently break image operations.
|
||||
func TestRegistryHost_NeverEmpty(t *testing.T) {
|
||||
for _, env := range []string{"", "ghcr.io/molecule-ai", "/leading-slash", "host-only", "host/with/path"} {
|
||||
t.Setenv("MOLECULE_IMAGE_REGISTRY", env)
|
||||
if got := RegistryHost(); got == "" {
|
||||
t.Errorf("RegistryHost() with env=%q returned empty (would break Docker auth + V2 HTTP)", env)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestKnownRuntimes_AlphabeticalOrder — pin the order so test snapshots
|
||||
// (and human readers diffing the file) see deterministic output. Adding a
|
||||
// new runtime out of alphabetical order will fail this test, which is the
|
||||
|
||||
@@ -27,15 +27,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Setup wires the gin router. pluginResolver is the registry-level resolver
|
||||
// (typically *plugins.Registry from main.go) reserved for future per-deploy
|
||||
// customisation — currently passed only to satisfy the call-site contract;
|
||||
// plgh (PluginsHandler) constructs its own internal registry with the
|
||||
// default github+local resolvers via NewPluginsHandler. The drift sweeper
|
||||
// (main.go) gets the same pluginResolver instance so it can share scheme
|
||||
// enumeration if a deployment registers extra schemes externally. A nil
|
||||
// pluginResolver is harmless: plgh still works with its built-in defaults.
|
||||
func Setup(hub *ws.Hub, broadcaster *events.Broadcaster, prov *provisioner.Provisioner, platformURL, configsDir string, wh *handlers.WorkspaceHandler, channelMgr *channels.Manager, memBundle *memwiring.Bundle, pluginResolver plugins.PluginResolver) *gin.Engine {
|
||||
func Setup(hub *ws.Hub, broadcaster *events.Broadcaster, prov *provisioner.Provisioner, platformURL, configsDir string, wh *handlers.WorkspaceHandler, channelMgr *channels.Manager, memBundle *memwiring.Bundle, pluginResolver plugins.SourceResolver) *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
// Issue #179 — trust no reverse-proxy headers. Without this call Gin's
|
||||
@@ -507,72 +499,6 @@ func Setup(hub *ws.Hub, broadcaster *events.Broadcaster, prov *provisioner.Provi
|
||||
r.POST("/admin/workspace-images/refresh", middleware.AdminAuth(db.DB), imgH.Refresh)
|
||||
}
|
||||
|
||||
// dockerCli is shared across plugins, terminal, templates, and bundle
|
||||
// handlers. Declared up-front (was at line ~594) because the plugins
|
||||
// init block — moved here in 70f84823 to fix "undefined: plgh" — needs
|
||||
// dockerCli at construction time (NewPluginsHandler signature). Moving
|
||||
// only the plgh block left dockerCli used-before-declared. Same nil
|
||||
// guard semantics: prov nil → dockerCli nil → handlers fall back to
|
||||
// non-Docker paths or skip Docker-dependent routes.
|
||||
var dockerCli *client.Client
|
||||
if prov != nil {
|
||||
dockerCli = prov.DockerClient()
|
||||
}
|
||||
|
||||
// Plugins — plgh must be initialized before the drift handler that uses it.
|
||||
// Moved here (core#248 fix) because the drift handler block (core#123) was
|
||||
// registered before plgh was created, causing "undefined: plgh" on main.
|
||||
pluginsDir := findPluginsDir(configsDir)
|
||||
// Runtime lookup lets the plugins handler filter the registry to plugins
|
||||
// that declare support for the workspace's runtime, without taking a
|
||||
// direct DB dependency in the handler package.
|
||||
runtimeLookup := func(workspaceID string) (string, error) {
|
||||
var runtime string
|
||||
err := db.DB.QueryRowContext(
|
||||
context.Background(),
|
||||
`SELECT COALESCE(runtime, 'langgraph') FROM workspaces WHERE id = $1`,
|
||||
workspaceID,
|
||||
).Scan(&runtime)
|
||||
return runtime, err
|
||||
}
|
||||
// Instance-id lookup powers the SaaS dispatch in install/uninstall:
|
||||
// when a workspace is on the EC2-per-workspace backend (instance_id
|
||||
// non-NULL) and there's no local Docker container to exec into, the
|
||||
// pipeline pushes the staged plugin tarball to that EC2 over EIC SSH.
|
||||
// Empty result means the workspace lives on the local-Docker backend
|
||||
// (or hasn't been provisioned yet) and the handler falls back to its
|
||||
// original Docker path. Same pattern templates.go and terminal.go use.
|
||||
instanceIDLookup := func(workspaceID string) (string, error) {
|
||||
var instanceID string
|
||||
err := db.DB.QueryRowContext(
|
||||
context.Background(),
|
||||
`SELECT COALESCE(instance_id, '') FROM workspaces WHERE id = $1`,
|
||||
workspaceID,
|
||||
).Scan(&instanceID)
|
||||
return instanceID, err
|
||||
}
|
||||
// plgh constructs its own internal registry (github + local) inside
|
||||
// NewPluginsHandler. The pluginResolver param is the SHARED registry the
|
||||
// drift sweeper consumes (main.go); we don't graft it onto plgh because
|
||||
// plgh's WithSourceResolver expects a per-scheme SourceResolver, not a
|
||||
// PluginResolver/registry. Cross-wiring those types was the original
|
||||
// "*Registry doesn't implement SourceResolver" build break (core#228).
|
||||
// Use of pluginResolver here is intentionally read-side only.
|
||||
_ = pluginResolver
|
||||
plgh := handlers.NewPluginsHandler(pluginsDir, dockerCli, wh.RestartByID).
|
||||
WithRuntimeLookup(runtimeLookup).
|
||||
WithInstanceIDLookup(instanceIDLookup)
|
||||
r.GET("/plugins", plgh.ListRegistry)
|
||||
r.GET("/plugins/sources", plgh.ListSources)
|
||||
wsAuth.GET("/plugins", plgh.ListInstalled)
|
||||
wsAuth.GET("/plugins/available", plgh.ListAvailableForWorkspace)
|
||||
wsAuth.GET("/plugins/compatibility", plgh.CheckRuntimeCompatibility)
|
||||
wsAuth.POST("/plugins", plgh.Install)
|
||||
wsAuth.DELETE("/plugins/:name", plgh.Uninstall)
|
||||
// Phase 30.3 — stream plugin as tar.gz so remote agents can pull +
|
||||
// unpack locally instead of going through Docker exec.
|
||||
wsAuth.GET("/plugins/:name/download", plgh.Download)
|
||||
|
||||
// Admin — plugin version-subscription drift queue (core#123).
|
||||
// List pending drift entries and apply approved updates.
|
||||
{
|
||||
@@ -611,7 +537,11 @@ func Setup(hub *ws.Hub, broadcaster *events.Broadcaster, prov *provisioner.Provi
|
||||
wsAuth.GET("/github-installation-token", ghTokH.GetInstallationToken)
|
||||
}
|
||||
|
||||
// Terminal — shares Docker client with provisioner (declared above).
|
||||
// Terminal — shares Docker client with provisioner
|
||||
var dockerCli *client.Client
|
||||
if prov != nil {
|
||||
dockerCli = prov.DockerClient()
|
||||
}
|
||||
th := handlers.NewTerminalHandler(dockerCli)
|
||||
wsAuth.GET("/terminal", th.HandleConnect)
|
||||
wsAuth.GET("/terminal/diagnose", th.HandleDiagnose)
|
||||
@@ -665,6 +595,57 @@ func Setup(hub *ws.Hub, broadcaster *events.Broadcaster, prov *provisioner.Provi
|
||||
wsAuth.GET("/pending-uploads/:file_id/content", puh.GetContent)
|
||||
wsAuth.POST("/pending-uploads/:file_id/ack", puh.Ack)
|
||||
|
||||
// Plugins
|
||||
pluginsDir := findPluginsDir(configsDir)
|
||||
// Runtime lookup lets the plugins handler filter the registry to plugins
|
||||
// that declare support for the workspace's runtime, without taking a
|
||||
// direct DB dependency in the handler package.
|
||||
runtimeLookup := func(workspaceID string) (string, error) {
|
||||
var runtime string
|
||||
err := db.DB.QueryRowContext(
|
||||
context.Background(),
|
||||
`SELECT COALESCE(runtime, 'langgraph') FROM workspaces WHERE id = $1`,
|
||||
workspaceID,
|
||||
).Scan(&runtime)
|
||||
return runtime, err
|
||||
}
|
||||
// Instance-id lookup powers the SaaS dispatch in install/uninstall:
|
||||
// when a workspace is on the EC2-per-workspace backend (instance_id
|
||||
// non-NULL) and there's no local Docker container to exec into, the
|
||||
// pipeline pushes the staged plugin tarball to that EC2 over EIC SSH.
|
||||
// Empty result means the workspace lives on the local-Docker backend
|
||||
// (or hasn't been provisioned yet) and the handler falls back to its
|
||||
// original Docker path. Same pattern templates.go and terminal.go use.
|
||||
instanceIDLookup := func(workspaceID string) (string, error) {
|
||||
var instanceID string
|
||||
err := db.DB.QueryRowContext(
|
||||
context.Background(),
|
||||
`SELECT COALESCE(instance_id, '') FROM workspaces WHERE id = $1`,
|
||||
workspaceID,
|
||||
).Scan(&instanceID)
|
||||
return instanceID, err
|
||||
}
|
||||
// pluginResolver: when provided (normal production), use it for plgh so
|
||||
// the drift sweeper (which also gets the same resolver in main.go) uses
|
||||
// identical resolver state. When nil (test / backward compat), let
|
||||
// NewPluginsHandler create its own default registry.
|
||||
plgh := handlers.NewPluginsHandler(pluginsDir, dockerCli, wh.RestartByID).
|
||||
WithRuntimeLookup(runtimeLookup).
|
||||
WithInstanceIDLookup(instanceIDLookup)
|
||||
if pluginResolver != nil {
|
||||
plgh = plgh.WithSourceResolver(pluginResolver)
|
||||
}
|
||||
r.GET("/plugins", plgh.ListRegistry)
|
||||
r.GET("/plugins/sources", plgh.ListSources)
|
||||
wsAuth.GET("/plugins", plgh.ListInstalled)
|
||||
wsAuth.GET("/plugins/available", plgh.ListAvailableForWorkspace)
|
||||
wsAuth.GET("/plugins/compatibility", plgh.CheckRuntimeCompatibility)
|
||||
wsAuth.POST("/plugins", plgh.Install)
|
||||
wsAuth.DELETE("/plugins/:name", plgh.Uninstall)
|
||||
// Phase 30.3 — stream plugin as tar.gz so remote agents can pull +
|
||||
// unpack locally instead of going through Docker exec.
|
||||
wsAuth.GET("/plugins/:name/download", plgh.Download)
|
||||
|
||||
// Bundles — #164 + #165: both gated behind AdminAuth.
|
||||
// POST /bundles/import — CRITICAL: anon creation of arbitrary workspaces
|
||||
// with user-supplied config (system prompts,
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
"""Sanitization helpers for A2A delegation results.
|
||||
|
||||
OFFSEC-003: Peer text must not be able to escape trust boundaries by
|
||||
injecting control markers that the caller interprets as structured framing.
|
||||
|
||||
This module is intentionally isolated from the rest of the molecule-runtime
|
||||
import graph to avoid circular imports. Callers import only from here when
|
||||
they need to sanitize a2a result text before returning it to the agent.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
|
||||
# Sentinel strings used by a2a_tools_delegation.py as control prefixes.
|
||||
_A2A_ERROR_PREFIX = "[A2A_ERROR] "
|
||||
_A2A_QUEUED_PREFIX = "[A2A_QUEUED] "
|
||||
_A2A_RESULT_FROM_PEER = "[A2A_RESULT_FROM_PEER]"
|
||||
_A2A_RESULT_TO_PEER = "[A2A_RESULT_TO_PEER]"
|
||||
|
||||
# Regex patterns for the lookahead. Each is a raw string where \[ = escaped
|
||||
# '[' and \] = escaped ']'. The full pattern (separator + '[' + rest) is
|
||||
# matched in two pieces:
|
||||
# 1. (?=<marker>) — lookahead: matches the ENTIRE marker (including '[')
|
||||
# at the current position without consuming any chars.
|
||||
# 2. \[ — consumes the '[' so it gets replaced, not duplicated.
|
||||
#
|
||||
# Why the lookahead-first approach? If we match (^|\n)\[ first, the lookahead
|
||||
# would fire at the *new* position (after the '['), not the original one, and
|
||||
# would fail. By matching the lookahead first, we assert the marker is present
|
||||
# at the correct token boundary, then consume the '[' separately.
|
||||
_BOUNDARY_PATTERNS: list[tuple[str, str]] = [
|
||||
(_A2A_ERROR_PREFIX, r"\[A2A_ERROR\] "),
|
||||
(_A2A_QUEUED_PREFIX, r"\[A2A_QUEUED\] "),
|
||||
(_A2A_RESULT_FROM_PEER, r"\[A2A_RESULT_FROM_PEER\]"),
|
||||
(_A2A_RESULT_TO_PEER, r"\[A2A_RESULT_TO_PEER\]"),
|
||||
]
|
||||
|
||||
_CONTROL_PATTERNS: list[tuple[str, str]] = [
|
||||
(r"[SYSTEM]", r"\[SYSTEM\]"),
|
||||
(r"[OVERRIDE]", r"\[OVERRIDE\]"),
|
||||
(r"[INSTRUCTIONS]", r"\[INSTRUCTIONS\]"),
|
||||
(r"[IGNORE ALL]", r"\[IGNORE ALL\]"),
|
||||
(r"[YOU ARE NOW]", r"\[YOU ARE NOW\]"),
|
||||
]
|
||||
|
||||
# ZERO-WIDTH SPACE (U+200B)
|
||||
_ZWSP = ""
|
||||
|
||||
|
||||
def _escape_boundary_markers(text: str) -> str:
|
||||
"""Escape trust-boundary markers embedded in raw peer text.
|
||||
|
||||
Scans ``text`` for any known boundary-control pattern that appears as a
|
||||
TOP-LEVEL token (start of string or after a newline) and inserts a
|
||||
ZERO-WIDTH SPACE (U+200B) before the opening '[' so that downstream
|
||||
parsers that look for the raw '[' no longer match the marker as a prefix.
|
||||
"""
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
# Build alternation from the second (regex) element of each tuple.
|
||||
marker_alts = "|".join(pat for _, pat in _BOUNDARY_PATTERNS + _CONTROL_PATTERNS)
|
||||
|
||||
# Pattern: (?=<marker>)\[ — lookahead for the FULL marker, then consume '['.
|
||||
# This ensures the '[' is consumed so it gets replaced, not duplicated.
|
||||
# We use regular string concatenation for (^|\n) so \n is 0x0A.
|
||||
boundary_re = re.compile(
|
||||
"(^|\n)(?=" + marker_alts + ")\\[",
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
|
||||
def _replacer(m: re.Match[str]) -> str:
|
||||
# m.group(1) = '' or '\n'; the '[' is consumed by the match
|
||||
return m.group(1) + _ZWSP + "["
|
||||
|
||||
return boundary_re.sub(_replacer, text)
|
||||
|
||||
|
||||
def sanitize_a2a_result(text: str) -> str:
|
||||
"""Sanitize raw A2A delegation result text before returning to the caller."""
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
text = _escape_boundary_markers(text)
|
||||
text = _strip_closed_blocks(text)
|
||||
return text
|
||||
|
||||
|
||||
def _strip_closed_blocks(text: str) -> str:
|
||||
"""Remove content after a closing marker injected by a malicious peer."""
|
||||
CLOSERS = [
|
||||
"[/A2A_ERROR]",
|
||||
"[/A2A_QUEUED]",
|
||||
"[/A2A_RESULT_FROM_PEER]",
|
||||
"[/A2A_RESULT_TO_PEER]",
|
||||
"[/SYSTEM]",
|
||||
"[/OVERRIDE]",
|
||||
"[/INSTRUCTIONS]",
|
||||
"[/IGNORE ALL]",
|
||||
"[/YOU ARE NOW]",
|
||||
]
|
||||
closer_re = "|".join(re.escape(c) for c in CLOSERS)
|
||||
|
||||
parts = re.split(
|
||||
"(?<=\n)(?=" + closer_re + ")|(?=^)(?=" + closer_re + ")",
|
||||
text, maxsplit=1, flags=re.MULTILINE,
|
||||
)
|
||||
# parts[0] may have a trailing \n that was part of the (?<=\n) boundary;
|
||||
# strip it so the result ends cleanly at the closer boundary.
|
||||
return parts[0].rstrip("\n")
|
||||
@@ -179,23 +179,6 @@ def parse(data: Any) -> Variant:
|
||||
)
|
||||
return Malformed(raw=data)
|
||||
|
||||
# Push-mode queue envelope — returned when a push-mode workspace
|
||||
# (one with a public URL) is at capacity. The platform queues the
|
||||
# request and returns {"queued": true, "message": "...", "queue_id": "..."}.
|
||||
# Unlike the poll-mode envelope (status=queued + delivery_mode=poll),
|
||||
# this shape has no delivery_mode key — it's distinguishable by
|
||||
# data.get("queued") is True alone. Checked before poll-mode so the
|
||||
# two cases are mutually exclusive even if a buggy server sends both.
|
||||
if data.get("queued") is True:
|
||||
method_raw = data.get(_KEY_METHOD)
|
||||
method = str(method_raw) if method_raw is not None else "message/send"
|
||||
logger.info(
|
||||
"a2a_response.parse: queued for busy push-mode peer (method=%s, queue_id=%s)",
|
||||
method,
|
||||
data.get("queue_id", "?"),
|
||||
)
|
||||
return Queued(method=method)
|
||||
|
||||
# Poll-queued envelope. Both keys must be present — the workspace
|
||||
# server sets them together; if only one is present the body is
|
||||
# ambiguous and we route to Malformed for visibility.
|
||||
|
||||
@@ -47,7 +47,6 @@ from a2a_client import (
|
||||
send_a2a_message,
|
||||
)
|
||||
from a2a_tools_rbac import auth_headers_for_heartbeat as _auth_headers_for_heartbeat
|
||||
from _sanitize_a2a import sanitize_a2a_result
|
||||
|
||||
|
||||
# RFC #2829 PR-5 cutover constants. The poll cadence + timeout are
|
||||
@@ -167,19 +166,12 @@ async def _delegate_sync_via_polling(
|
||||
break
|
||||
if terminal:
|
||||
if (terminal.get("status") or "").lower() == "completed":
|
||||
# OFFSEC-003: sanitize response_preview before returning so
|
||||
# boundary markers injected by a malicious peer cannot escape
|
||||
# the trust boundary.
|
||||
return sanitize_a2a_result(terminal.get("response_preview") or "")
|
||||
# OFFSEC-003: sanitize error_detail / summary before wrapping with
|
||||
# the _A2A_ERROR_PREFIX sentinel so injected markers cannot appear
|
||||
# inside the trusted error block returned to the agent.
|
||||
err_raw = (
|
||||
return terminal.get("response_preview") or ""
|
||||
err = (
|
||||
terminal.get("error_detail")
|
||||
or terminal.get("summary")
|
||||
or "delegation failed"
|
||||
)
|
||||
err = sanitize_a2a_result(err_raw)
|
||||
return f"{_A2A_ERROR_PREFIX}{err}"
|
||||
|
||||
await asyncio.sleep(_SYNC_POLL_INTERVAL_S)
|
||||
@@ -212,20 +204,6 @@ async def tool_delegate_task(
|
||||
if not workspace_id or not task:
|
||||
return "Error: workspace_id and task are required"
|
||||
|
||||
# Self-delegation guard: delegating to your own workspace ID deadlocks —
|
||||
# the sending turn holds _run_lock while the receive handler waits for the
|
||||
# same lock, the request 30s-times-out, and the whole cycle is wasted.
|
||||
# Reject immediately with an actionable message. (effective_src mirrors the
|
||||
# `src or WORKSPACE_ID` resolution used below for routing.)
|
||||
effective_src = source_workspace_id or _peer_to_source.get(workspace_id) or WORKSPACE_ID
|
||||
if workspace_id and workspace_id == effective_src:
|
||||
return (
|
||||
"Error: cannot delegate_task to your own workspace — self-delegation "
|
||||
"deadlocks _run_lock (your sending turn holds it, the receive handler "
|
||||
"waits for it, the request times out). There is no peer who is also you: "
|
||||
"just do the work yourself, or call commit_memory / send_message_to_user directly."
|
||||
)
|
||||
|
||||
# Auto-route: if source not specified, look up which registered
|
||||
# workspace last saw this peer (populated by tool_list_peers). Falls
|
||||
# back to the legacy WORKSPACE_ID for single-workspace operators.
|
||||
@@ -345,16 +323,6 @@ async def tool_delegate_task_async(
|
||||
|
||||
src = source_workspace_id or _peer_to_source.get(workspace_id) or WORKSPACE_ID
|
||||
|
||||
# Self-delegation guard: even on the async path, queuing a task to your own
|
||||
# workspace just makes you re-process your own dispatch — never useful, and
|
||||
# on the sync path it deadlocks (see tool_delegate_task). Reject early.
|
||||
if workspace_id and workspace_id == src:
|
||||
return (
|
||||
"Error: cannot delegate_task_async to your own workspace — there is no "
|
||||
"peer who is also you. Do the work yourself, or call commit_memory / "
|
||||
"send_message_to_user directly."
|
||||
)
|
||||
|
||||
# Idempotency key: SHA-256 of (source, target, task) so that a
|
||||
# restarted agent firing the same delegation gets the same key and
|
||||
# the platform returns the existing delegation_id instead of
|
||||
@@ -414,11 +382,7 @@ async def tool_check_task_status(
|
||||
# Filter by delegation_id
|
||||
matching = [d for d in delegations if d.get("delegation_id") == task_id]
|
||||
if matching:
|
||||
# OFFSEC-003: sanitize peer-supplied fields
|
||||
d = matching[0]
|
||||
d["summary"] = sanitize_a2a_result(d.get("summary", ""))
|
||||
d["response_preview"] = sanitize_a2a_result(d.get("response_preview", ""))
|
||||
return json.dumps(d)
|
||||
return json.dumps(matching[0])
|
||||
return json.dumps({"status": "not_found", "delegation_id": task_id})
|
||||
# Return all recent delegations
|
||||
summary = []
|
||||
@@ -427,9 +391,8 @@ async def tool_check_task_status(
|
||||
"delegation_id": d.get("delegation_id", ""),
|
||||
"target_id": d.get("target_id", ""),
|
||||
"status": d.get("status", ""),
|
||||
# OFFSEC-003: sanitize peer-supplied fields before embedding in JSON
|
||||
"summary": sanitize_a2a_result(d.get("summary", "")),
|
||||
"response_preview": sanitize_a2a_result(d.get("response_preview", "")),
|
||||
"summary": d.get("summary", ""),
|
||||
"response_preview": d.get("response_preview", ""),
|
||||
})
|
||||
return json.dumps({"delegations": summary, "count": len(delegations)})
|
||||
except Exception as e:
|
||||
|
||||
@@ -66,27 +66,10 @@ async def delegate_task(workspace_id: str, task: str) -> str:
|
||||
)
|
||||
data = a2a_resp.json()
|
||||
if "result" in data:
|
||||
result = data["result"]
|
||||
parts = result.get("parts", []) if isinstance(result, dict) else []
|
||||
if parts and isinstance(parts[0], dict):
|
||||
return parts[0].get("text", "(no text)")
|
||||
# Empty parts list (e.g. {"parts": []}) should return str(result),
|
||||
# not "(no text)" — preserves pre-fix behavior (#279 regression fix).
|
||||
if isinstance(result, dict) and result.get("parts") == []:
|
||||
return str(result)
|
||||
return str(result) if isinstance(result, str) else "(no text)"
|
||||
parts = data["result"].get("parts", [])
|
||||
return parts[0].get("text", "(no text)") if parts else str(data["result"])
|
||||
elif "error" in data:
|
||||
err = data["error"]
|
||||
# Handle both string-form errors ("error": "some string")
|
||||
# and object-form errors ("error": {"message": "...", "code": ...}).
|
||||
msg = ""
|
||||
if isinstance(err, dict):
|
||||
msg = err.get("message", "")
|
||||
elif isinstance(err, str):
|
||||
msg = err
|
||||
else:
|
||||
msg = str(err)
|
||||
return f"Error: {msg}"
|
||||
return f"Error: {data['error'].get('message', str(data['error']))}"
|
||||
return str(data)
|
||||
except Exception as e:
|
||||
return f"Error sending A2A message: {e}"
|
||||
|
||||
+8
-54
@@ -1,6 +1,5 @@
|
||||
"""Load workspace configuration from config.yaml."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
@@ -8,8 +7,6 @@ from typing import Optional
|
||||
|
||||
import yaml
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class RBACConfig:
|
||||
@@ -384,47 +381,6 @@ def _derive_provider_from_model(model: str) -> str:
|
||||
return ""
|
||||
|
||||
|
||||
_legacy_model_provider_warned = False
|
||||
|
||||
|
||||
def _picked_model_from_env(default: str) -> str:
|
||||
"""Resolve the operator-picked model id from env; newest name wins.
|
||||
|
||||
Precedence: ``MOLECULE_MODEL`` (canonical, unambiguous) → ``MODEL`` →
|
||||
``MODEL_PROVIDER`` (legacy) → ``default`` (the YAML ``model:`` field).
|
||||
|
||||
``MODEL_PROVIDER`` is **misleadingly named**: it carries the picked
|
||||
*model id*, never the LLM provider — the provider lives in
|
||||
``LLM_PROVIDER`` / the YAML ``provider:`` field. The legacy path stays
|
||||
so canvas Save+Restart, the workspace-server secret-mint path, and
|
||||
persona env files that set it keep working, but if it's the *only* one
|
||||
set we log a deprecation once — the misnomer keeps biting (e.g. setting
|
||||
``MODEL_PROVIDER=claude-code`` expecting it to select the claude-code
|
||||
*runtime* — it doesn't, ``runtime:`` does — after which the claude CLI
|
||||
404s on ``--model claude-code``). Set ``MODEL``/``MOLECULE_MODEL`` to
|
||||
an id from ``runtime_config.models[].id`` (e.g. ``opus``, ``sonnet``,
|
||||
``claude-opus-4-7``, ``MiniMax-M2.7-highspeed``) instead.
|
||||
"""
|
||||
global _legacy_model_provider_warned
|
||||
for name in ("MOLECULE_MODEL", "MODEL"):
|
||||
v = (os.environ.get(name) or "").strip()
|
||||
if v:
|
||||
return v
|
||||
legacy = (os.environ.get("MODEL_PROVIDER") or "").strip()
|
||||
if legacy:
|
||||
if not _legacy_model_provider_warned:
|
||||
logger.warning(
|
||||
"MODEL_PROVIDER=%r is deprecated and misleadingly named — it "
|
||||
"sets the picked *model id*, not the LLM provider (that's "
|
||||
"LLM_PROVIDER / the YAML `provider:` field). Set MODEL (or "
|
||||
"MOLECULE_MODEL) to an id from runtime_config.models instead.",
|
||||
legacy,
|
||||
)
|
||||
_legacy_model_provider_warned = True
|
||||
return legacy
|
||||
return default
|
||||
|
||||
|
||||
_EVENT_LOG_VALID_BACKENDS = {"memory", "disabled"}
|
||||
|
||||
|
||||
@@ -489,10 +445,8 @@ def load_config(config_path: Optional[str] = None) -> WorkspaceConfig:
|
||||
with open(config_file) as f:
|
||||
raw = yaml.safe_load(f) or {}
|
||||
|
||||
# Operator-picked model from env (canvas / secret-mint / persona env),
|
||||
# falling back to the YAML `model:` field. See _picked_model_from_env for
|
||||
# the precedence (MOLECULE_MODEL > MODEL > legacy MODEL_PROVIDER).
|
||||
model = _picked_model_from_env(raw.get("model", "anthropic:claude-opus-4-7"))
|
||||
# Override model from env if provided
|
||||
model = os.environ.get("MODEL_PROVIDER", raw.get("model", "anthropic:claude-opus-4-7"))
|
||||
|
||||
# Resolve top-level provider with this priority chain:
|
||||
# 1. ``LLM_PROVIDER`` env var (canvas Save+Restart sets this so the
|
||||
@@ -563,9 +517,8 @@ def load_config(config_path: Optional[str] = None) -> WorkspaceConfig:
|
||||
required_env=runtime_raw.get("required_env", []),
|
||||
timeout=runtime_raw.get("timeout", 0),
|
||||
# Picked-model precedence (priority order):
|
||||
# 1. operator-picked model from env — MOLECULE_MODEL > MODEL >
|
||||
# (legacy) MODEL_PROVIDER, plumbed via canvas Save+Restart,
|
||||
# workspace-server's secret-mint path, or the universal
|
||||
# 1. MODEL_PROVIDER env var — canvas-picked model, plumbed via
|
||||
# workspace-server's secret-mint path or the universal
|
||||
# MODEL/MODEL_PROVIDER env from applyRuntimeModelEnv. The
|
||||
# operator's canvas selection MUST win over the template's
|
||||
# baked-in default; previously the template's
|
||||
@@ -574,12 +527,13 @@ def load_config(config_path: Optional[str] = None) -> WorkspaceConfig:
|
||||
# surfaced 2026-05-02 during E2E).
|
||||
# 2. runtime_raw.model — explicit YAML override in the
|
||||
# template's runtime_config.
|
||||
# 3. top-level `model` (already env-resolved above). This is
|
||||
# the SaaS restart case (CP regenerates a minimal
|
||||
# 3. top-level `model` — already honors MODEL_PROVIDER (line
|
||||
# 359) but only when YAML lacks a top-level `model:`. This
|
||||
# is the SaaS restart case (CP regenerates a minimal
|
||||
# config.yaml on every boot, dropping runtime_config.model).
|
||||
# Centralising here means EVERY adapter gets the override for
|
||||
# free — no per-adapter env-reading code required.
|
||||
model=_picked_model_from_env(runtime_raw.get("model") or model),
|
||||
model=os.environ.get("MODEL_PROVIDER") or runtime_raw.get("model") or model,
|
||||
# Same fallback shape as ``model`` above: an explicit
|
||||
# ``runtime_config.provider`` wins; otherwise inherit the
|
||||
# top-level resolved provider so adapters see a single
|
||||
|
||||
@@ -34,7 +34,6 @@ from typing import TYPE_CHECKING, Any
|
||||
|
||||
import httpx
|
||||
|
||||
from _sanitize_a2a import sanitize_a2a_result # noqa: E402
|
||||
from builtin_tools.security import _redact_secrets
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -205,25 +204,12 @@ def read_delegation_results() -> str:
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
status = record.get("status", "?")
|
||||
# Both summary and response_preview come from peer-supplied A2A response
|
||||
# text (platform truncates to 80/200 bytes before writing). Sanitize
|
||||
# BEFORE truncating so boundary markers embedded by a malicious peer
|
||||
# are escaped before the 80/200-char limit cuts off any closing marker.
|
||||
raw_summary = record.get("summary", "")
|
||||
raw_preview = record.get("response_preview", "")
|
||||
# sanitize_a2a_result wraps in boundary markers + escapes any markers
|
||||
# already in the content (OFFSEC-003). After escaping, truncate to
|
||||
# stay within the 80/200-char limits.
|
||||
safe_summary = sanitize_a2a_result(raw_summary)[:80]
|
||||
parts.append(f"- [{status}] {safe_summary}")
|
||||
if raw_preview:
|
||||
safe_preview = sanitize_a2a_result(raw_preview)[:200]
|
||||
parts.append(f" Response: {safe_preview}")
|
||||
if not parts:
|
||||
return ""
|
||||
# OFFSEC-003: wrap in boundary markers to establish trust boundary
|
||||
# so any content AFTER this block is clearly NOT from a peer.
|
||||
return "[A2A_RESULT_FROM_PEER]\n" + "\n".join(parts) + "\n[/A2A_RESULT_FROM_PEER]"
|
||||
summary = record.get("summary", "")
|
||||
preview = record.get("response_preview", "")
|
||||
parts.append(f"- [{status}] {summary}")
|
||||
if preview:
|
||||
parts.append(f" Response: {preview[:200]}")
|
||||
return "\n".join(parts)
|
||||
|
||||
|
||||
# ========================================================================
|
||||
|
||||
@@ -668,31 +668,6 @@ async def main(): # pragma: no cover
|
||||
if heartbeat.active_tasks > 0:
|
||||
continue
|
||||
|
||||
# Issue #381 fix: skip the idle prompt if there are unconsumed
|
||||
# delegation results waiting. The heartbeat sends a self-message
|
||||
# for every new result batch, so sending the idle prompt here would
|
||||
# race: the agent would compose a stale tick BEFORE processing the
|
||||
# results notification, producing repeated identical asks (peer sends
|
||||
# correction, we respond with stale state, peer asks again).
|
||||
# By skipping the idle prompt when results are pending, we let the
|
||||
# heartbeat's own self-message wake the agent after results are
|
||||
# written. The agent then sees the results in _prepare_prompt()
|
||||
# and processes them before composing.
|
||||
from heartbeat import DELEGATION_RESULTS_FILE as _DRF
|
||||
try:
|
||||
with open(_DRF) as _rf:
|
||||
_rf.seek(0)
|
||||
_content = _rf.read().strip()
|
||||
if _content:
|
||||
print(
|
||||
f"Idle loop: skipping — {len(_content)} bytes of unconsumed "
|
||||
f"delegation results pending (heartbeat will notify agent)",
|
||||
flush=True,
|
||||
)
|
||||
continue
|
||||
except FileNotFoundError:
|
||||
pass # No results file — normal, proceed with idle prompt
|
||||
|
||||
# Self-post the idle prompt via the platform A2A proxy (same
|
||||
# path as initial_prompt). The agent's own concurrency control
|
||||
# rejects if the workspace becomes busy between this check and
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user