Compare commits

..

2 Commits

Author SHA1 Message Date
fullstack-engineer b2fa3bc937 test(canvas): fix test infrastructure — cleanup isolation, accessibility queries, role= textbox
audit-force-merge / audit (pull_request) Successful in 22s
Scope:
- form-inputs.test.tsx (new): 35 cases covering TextInput, NumberInput,
  Toggle, TagList, Section. Section coverage includes aria-expanded,
  aria-controls, content id, and aria-hidden indicator span.
- form-inputs.tsx (Section): add aria-expanded + aria-controls to the
  toggle button and a matching id on the collapsible content region;
  aria-hidden on the ▾/▸ indicator so screen readers skip it.

Test isolation fixes (afterEach(cleanup) missing → DOM element accumulation):
- ApprovalBanner.test.tsx
- StatusDot.test.tsx        — also adds { hidden: true } to getByRole("img")
                               since @testing-library/dom v10+ excludes
                               aria-hidden elements from accessible queries
- ValidationHint.test.tsx  — also fixes checkmark test that assumed
                               ✓ + "Valid format" were one text node
- TopBar.test.tsx
- RevealToggle.test.tsx
- StatusBadge.test.tsx

Tooltip.test.tsx:
- Adds vi.useFakeTimers() beforeEach / vi.useRealTimers() afterEach
  (tests called vi.advanceTimersByTime without fake timers)
- Fixes aria-describedby test to check the wrapper div, not the button

KeyValueField.tsx:
- Adds role="textbox" to the <input> element so getByRole("textbox")
  finds it in @testing-library/dom v10 (password inputs lack implicit
  textbox role in jsdom).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-11 23:00:46 +00:00
release-manager 9ce20958a5 fix(a2a): restore OFFSEC-003 trust-boundary wrap on tool_delegate_task return (closes #491) (#492)
Secret scan / Scan diff for credential-shaped strings (push) Successful in 3s
Co-authored-by: Molecule AI Release Manager <release-manager@agents.moleculesai.app>
Co-committed-by: Molecule AI Release Manager <release-manager@agents.moleculesai.app>
2026-05-11 15:01:18 +00:00
13 changed files with 319 additions and 452 deletions
@@ -16,6 +16,8 @@ vi.mock("@/components/Toaster", () => ({
showToast: vi.fn(),
}));
afterEach(cleanup);
// ─── Helpers ──────────────────────────────────────────────────────────────────
const pendingApproval = (id = "a1", workspaceId = "ws-1"): {
@@ -6,11 +6,12 @@
* 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 { render, screen, fireEvent, cleanup } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { RevealToggle } from "../ui/RevealToggle";
describe("RevealToggle — render", () => {
afterEach(cleanup);
it("renders a button element", () => {
render(<RevealToggle revealed={false} onToggle={vi.fn()} />);
expect(screen.getByRole("button")).toBeTruthy();
@@ -6,11 +6,12 @@
* 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 { render, screen, cleanup } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { StatusBadge } from "../ui/StatusBadge";
describe("StatusBadge — render", () => {
afterEach(cleanup);
it("renders verified status with ✓ icon", () => {
render(<StatusBadge status="verified" />);
const badge = screen.getByRole("status");
@@ -11,16 +11,18 @@
* - provisioning status carries motion-safe:animate-pulse for the pulsing effect
* - glow class applied when STATUS_CONFIG declares one
*/
import { describe, expect, it } from "vitest";
import { render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { render, screen, cleanup } from "@testing-library/react";
import React from "react";
import { StatusDot } from "../StatusDot";
afterEach(cleanup);
describe("StatusDot — snapshot", () => {
it("renders with online status", () => {
render(<StatusDot status="online" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("bg-emerald-400");
expect(dot.className).toContain("shadow-emerald-400/50");
expect(dot.getAttribute("aria-hidden")).toBe("true");
@@ -28,7 +30,7 @@ describe("StatusDot — snapshot", () => {
it("renders with offline status", () => {
render(<StatusDot status="offline" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("bg-zinc-500");
// offline has no glow
expect(dot.className).not.toContain("shadow-");
@@ -36,34 +38,34 @@ describe("StatusDot — snapshot", () => {
it("renders with degraded status", () => {
render(<StatusDot status="degraded" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("bg-amber-400");
expect(dot.className).toContain("shadow-amber-400/50");
});
it("renders with failed status", () => {
render(<StatusDot status="failed" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("bg-red-400");
expect(dot.className).toContain("shadow-red-400/50");
});
it("renders with paused status", () => {
render(<StatusDot status="paused" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("bg-indigo-400");
});
it("renders with not_configured status", () => {
render(<StatusDot status="not_configured" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("bg-amber-300");
expect(dot.className).toContain("shadow-amber-300/50");
});
it("renders with provisioning status and pulsing animation", () => {
render(<StatusDot status="provisioning" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("bg-sky-400");
expect(dot.className).toContain("motion-safe:animate-pulse");
expect(dot.className).toContain("shadow-sky-400/50");
@@ -71,7 +73,7 @@ describe("StatusDot — snapshot", () => {
it("falls back to bg-zinc-500 for unknown status", () => {
render(<StatusDot status="alien_artifact" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("bg-zinc-500");
});
});
@@ -79,14 +81,14 @@ describe("StatusDot — snapshot", () => {
describe("StatusDot — size prop", () => {
it("applies w-2 h-2 (sm, default)", () => {
render(<StatusDot status="online" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("w-2");
expect(dot.className).toContain("h-2");
});
it("applies w-2.5 h-2.5 (md)", () => {
render(<StatusDot status="online" size="md" />);
const dot = screen.getByRole("img");
const dot = screen.getByRole("img", { hidden: true });
expect(dot.className).toContain("w-2.5");
expect(dot.className).toContain("h-2.5");
});
@@ -95,6 +97,6 @@ describe("StatusDot — size prop", () => {
describe("StatusDot — accessibility", () => {
it("is aria-hidden so it doesn't pollute the accessibility tree", () => {
render(<StatusDot status="online" />);
expect(screen.getByRole("img").getAttribute("aria-hidden")).toBe("true");
expect(screen.getByRole("img", { hidden: true }).getAttribute("aria-hidden")).toBe("true");
});
});
@@ -10,9 +10,15 @@ import { render, screen, fireEvent, cleanup, act } from "@testing-library/react"
import { afterEach, describe, expect, it, vi, beforeEach } from "vitest";
import { Tooltip } from "../Tooltip";
afterEach(cleanup);
afterEach(() => {
cleanup();
vi.useRealTimers();
});
describe("Tooltip — render", () => {
beforeEach(() => {
vi.useFakeTimers();
});
it("renders children without showing tooltip on mount", () => {
render(
<Tooltip text="Hello world">
@@ -225,11 +231,12 @@ describe("Tooltip — aria-describedby", () => {
<button type="button">Hover me</button>
</Tooltip>
);
// The aria-describedby is on the wrapper div, not the button child
const btn = screen.getByRole("button");
const describedBy = btn.getAttribute("aria-describedby");
const wrapper = btn.parentElement as HTMLElement;
const describedBy = wrapper.getAttribute("aria-describedby");
expect(describedBy).toBeTruthy();
// The describedby id matches the tooltip id
const tooltipId = describedBy!.replace(/.*?:\s*/, "");
expect(document.getElementById(tooltipId)).toBeTruthy();
expect(document.getElementById(describedBy!)).toBeTruthy();
});
});
@@ -6,10 +6,12 @@
* SettingsButton integration, custom canvasName prop.
*/
import React from "react";
import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { render, screen, cleanup } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { TopBar } from "../canvas/TopBar";
afterEach(cleanup);
// ─── Mock SettingsButton ───────────────────────────────────────────────────────
vi.mock("../settings/SettingsButton", () => ({
@@ -6,10 +6,12 @@
* aria-live for error, icon rendering.
*/
import React from "react";
import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { render, screen, cleanup } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { ValidationHint } from "../ui/ValidationHint";
afterEach(cleanup);
describe("ValidationHint — error state", () => {
it("renders error message when error is a non-null string", () => {
render(<ValidationHint error="Invalid email address" />);
@@ -43,7 +45,9 @@ describe("ValidationHint — valid state", () => {
it("includes the checkmark icon in valid state", () => {
render(<ValidationHint error={null} showValid={true} />);
expect(screen.getByText(/✓ Valid format/)).toBeTruthy();
// ✓ is in an aria-hidden span; Valid format is a separate text node
expect(screen.getByText(/✓/)).toBeTruthy();
expect(screen.getByText("Valid format")).toBeTruthy();
});
it("uses the valid class on the paragraph element", () => {
@@ -0,0 +1,261 @@
// @vitest-environment jsdom
"use client";
/**
* Tests for form-inputs.tsx — 35 cases:
* TextInput (7), NumberInput (8), Toggle (5), TagList (9), Section (6).
*/
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { render, screen, fireEvent, cleanup } from "@testing-library/react";
import React from "react";
import {
TextInput,
NumberInput,
Toggle,
TagList,
Section,
} from "../form-inputs";
afterEach(cleanup);
// ─── TextInput ───────────────────────────────────────────────────────────────
describe("TextInput", () => {
describe("renders", () => {
it("renders the label", () => {
render(<TextInput label="API Key" value="" onChange={vi.fn()} />);
expect(screen.getByLabelText("API Key")).toBeTruthy();
});
it("renders the current value", () => {
render(<TextInput label="Name" value="Claude" onChange={vi.fn()} />);
expect((screen.getByRole("textbox") as HTMLInputElement).value).toBe("Claude");
});
it("calls onChange when value changes", () => {
const onChange = vi.fn();
render(<TextInput label="Name" value="" onChange={onChange} />);
fireEvent.change(screen.getByRole("textbox"), { target: { value: "Sonnet" } });
expect(onChange).toHaveBeenCalledWith("Sonnet");
});
it("renders placeholder when provided", () => {
render(<TextInput label="Name" value="" onChange={vi.fn()} placeholder="Enter your name" />);
expect((screen.getByRole("textbox") as HTMLInputElement).placeholder).toBe("Enter your name");
});
it("applies font-mono class when mono=true", () => {
render(<TextInput label="Token" value="" onChange={vi.fn()} mono />);
const input = screen.getByRole("textbox");
expect(input.className).toMatch(/font-mono/);
});
it("has aria-label matching the label", () => {
render(<TextInput label="API Key" value="" onChange={vi.fn()} />);
expect(screen.getByRole("textbox").getAttribute("aria-label")).toBe("API Key");
});
it("does not apply font-mono class when mono=false", () => {
render(<TextInput label="Name" value="" onChange={vi.fn()} mono={false} />);
expect(screen.getByRole("textbox").className).not.toMatch(/font-mono/);
});
});
});
// ─── NumberInput ────────────────────────────────────────────────────────────
describe("NumberInput", () => {
describe("renders", () => {
it("renders the label", () => {
render(<NumberInput label="Port" value={8000} onChange={vi.fn()} />);
expect(screen.getByLabelText("Port")).toBeTruthy();
});
it("renders the numeric value", () => {
render(<NumberInput label="Timeout" value={120} onChange={vi.fn()} />);
expect((screen.getByRole("spinbutton") as HTMLInputElement).value).toBe("120");
});
it("calls onChange with parsed integer", () => {
const onChange = vi.fn();
render(<NumberInput label="Retries" value={0} onChange={onChange} />);
fireEvent.change(screen.getByRole("spinbutton"), { target: { value: "3" } });
expect(onChange).toHaveBeenCalledWith(3);
});
it("calls onChange with 0 for non-numeric input", () => {
const onChange = vi.fn();
render(<NumberInput label="Retries" value={0} onChange={onChange} />);
fireEvent.change(screen.getByRole("spinbutton"), { target: { value: "abc" } });
expect(onChange).toHaveBeenCalledWith(0);
});
it("applies min/max attributes", () => {
render(<NumberInput label="Priority" value={5} onChange={vi.fn()} min={1} max={10} />);
const input = screen.getByRole("spinbutton") as HTMLInputElement;
expect(input.min).toBe("1");
expect(input.max).toBe("10");
});
it("has aria-label matching the label", () => {
render(<NumberInput label="Retries" value={3} onChange={vi.fn()} />);
expect(screen.getByRole("spinbutton").getAttribute("aria-label")).toBe("Retries");
});
it("applies font-mono class", () => {
render(<NumberInput label="Timeout" value={30} onChange={vi.fn()} />);
expect(screen.getByRole("spinbutton").className).toMatch(/font-mono/);
});
});
});
// ─── Toggle ─────────────────────────────────────────────────────────────────
describe("Toggle", () => {
describe("renders", () => {
it("renders a checkbox", () => {
render(<Toggle label="Enable streaming" checked={false} onChange={vi.fn()} />);
expect(screen.getByRole("checkbox")).toBeTruthy();
});
it("reflects checked=true state", () => {
render(<Toggle label="Enable streaming" checked={true} onChange={vi.fn()} />);
expect((screen.getByRole("checkbox") as HTMLInputElement).checked).toBe(true);
});
it("reflects checked=false state", () => {
render(<Toggle label="Enable streaming" checked={false} onChange={vi.fn()} />);
expect((screen.getByRole("checkbox") as HTMLInputElement).checked).toBe(false);
});
it("calls onChange with new boolean value", () => {
const onChange = vi.fn();
render(<Toggle label="Enable streaming" checked={false} onChange={onChange} />);
fireEvent.click(screen.getByRole("checkbox"));
expect(onChange).toHaveBeenCalledWith(true);
});
it("renders as type=checkbox", () => {
render(<Toggle label="Enable" checked={false} onChange={vi.fn()} />);
expect(screen.getByRole("checkbox").getAttribute("type")).toBe("checkbox");
});
});
});
// ─── TagList ───────────────────────────────────────────────────────────────
describe("TagList", () => {
describe("renders", () => {
it("renders existing tags", () => {
render(<TagList label="Skills" values={["python", "go"]} onChange={vi.fn()} />);
expect(screen.getByText("python")).toBeTruthy();
expect(screen.getByText("go")).toBeTruthy();
});
it("calls onChange with updated array when × clicked", () => {
const onChange = vi.fn();
render(<TagList label="Skills" values={["python", "go"]} onChange={onChange} />);
fireEvent.click(screen.getByRole("button", { name: /remove tag python/i }));
expect(onChange).toHaveBeenCalledWith(["go"]);
});
it("× button has correct aria-label per tag", () => {
render(<TagList label="Skills" values={["python"]} onChange={vi.fn()} />);
expect(screen.getByRole("button", { name: /remove tag python/i })).toBeTruthy();
});
it("adds tag when Enter is pressed with non-empty input", () => {
const onChange = vi.fn();
render(<TagList label="Skills" values={[]} onChange={onChange} />);
const input = screen.getByRole("textbox");
fireEvent.change(input, { target: { value: "rust" } });
fireEvent.keyDown(input, { key: "Enter" });
expect(onChange).toHaveBeenCalledWith(["rust"]);
});
it("does not add tag when Enter is pressed with whitespace-only input", () => {
const onChange = vi.fn();
render(<TagList label="Skills" values={[]} onChange={onChange} />);
const input = screen.getByRole("textbox");
fireEvent.change(input, { target: { value: " " } });
fireEvent.keyDown(input, { key: "Enter" });
expect(onChange).not.toHaveBeenCalled();
});
it("clears input after adding a tag", () => {
const onChange = vi.fn();
render(<TagList label="Skills" values={[]} onChange={onChange} />);
const input = screen.getByRole("textbox");
fireEvent.change(input, { target: { value: "typescript" } });
fireEvent.keyDown(input, { key: "Enter" });
expect((input as HTMLInputElement).value).toBe("");
});
it("renders the label", () => {
render(<TagList label="Tools" values={[]} onChange={vi.fn()} />);
expect(screen.getByLabelText("Tools")).toBeTruthy();
});
it("renders placeholder text", () => {
render(<TagList label="Skills" values={[]} onChange={vi.fn()} placeholder="Add a skill" />);
expect((screen.getByRole("textbox") as HTMLInputElement).placeholder).toBe("Add a skill");
});
it("renders default placeholder when not specified", () => {
render(<TagList label="Skills" values={[]} onChange={vi.fn()} />);
expect((screen.getByRole("textbox") as HTMLInputElement).placeholder).toBe("Type and press Enter");
});
});
});
// ─── Section ────────────────────────────────────────────────────────────────
describe("Section", () => {
describe("renders", () => {
it("renders the title", () => {
render(<Section title="Runtime Config"><p>Content</p></Section>);
expect(screen.getByText("Runtime Config")).toBeTruthy();
});
it("renders children when defaultOpen=true", () => {
render(<Section title="Runtime Config"><p data-testid="content">Hello</p></Section>);
expect(screen.getByTestId("content")).toBeTruthy();
});
it("hides children when defaultOpen=false", () => {
render(<Section title="Runtime Config" defaultOpen={false}><p data-testid="content">Hello</p></Section>);
expect(screen.queryByTestId("content")).toBeNull();
});
it("toggles children visibility on click", () => {
render(<Section title="Runtime Config" defaultOpen={true}><p data-testid="content">Hello</p></Section>);
expect(screen.getByTestId("content")).toBeTruthy();
fireEvent.click(screen.getByRole("button", { name: /runtime config/i }));
expect(screen.queryByTestId("content")).toBeNull();
});
it("button has aria-expanded reflecting open state", () => {
render(<Section title="Runtime Config" defaultOpen={true}><p>Content</p></Section>);
const btn = screen.getByRole("button", { name: /runtime config/i });
expect(btn.getAttribute("aria-expanded")).toBe("true");
fireEvent.click(btn);
expect(btn.getAttribute("aria-expanded")).toBe("false");
});
it("button has aria-controls linking to content region id", () => {
render(<Section title="Runtime Config"><p>Content</p></Section>);
const btn = screen.getByRole("button", { name: /runtime config/i });
const contentId = btn.getAttribute("aria-controls");
expect(contentId).not.toBeNull();
// Content div has the matching id
expect(document.getElementById(String(contentId))).not.toBeNull();
});
it("indicator span has aria-hidden so screen readers skip it", () => {
render(<Section title="Runtime Config"><p>Content</p></Section>);
const btn = screen.getByRole("button", { name: /runtime config/i });
const indicator = btn.querySelector("[aria-hidden='true']");
expect(indicator).not.toBeNull();
});
});
});
@@ -127,13 +127,20 @@ export function TagList({ label, values, onChange, placeholder }: { label: strin
export function Section({ title, children, defaultOpen = true }: { title: string; children: React.ReactNode; defaultOpen?: boolean }) {
const [open, setOpen] = useState(defaultOpen);
const contentId = `section-content-${title.toLowerCase().replace(/\s+/g, "-")}`;
return (
<div className="border border-line rounded mb-2">
<button type="button" onClick={() => setOpen(!open)} className="w-full flex items-center justify-between px-3 py-1.5 text-[10px] text-ink-mid hover:text-ink bg-surface-sunken/50">
<button
type="button"
onClick={() => setOpen(!open)}
aria-expanded={open}
aria-controls={contentId}
className="w-full flex items-center justify-between px-3 py-1.5 text-[10px] text-ink-mid hover:text-ink bg-surface-sunken/50"
>
<span className="font-medium uppercase tracking-wider">{title}</span>
<span>{open ? "▾" : "▸"}</span>
<span aria-hidden="true">{open ? "▾" : "▸"}</span>
</button>
{open && <div className="p-3 space-y-3">{children}</div>}
{open && <div id={contentId} className="p-3 space-y-3">{children}</div>}
</div>
);
}
@@ -70,6 +70,7 @@ export function KeyValueField({
aria-label={ariaLabel}
autoComplete="off"
spellCheck={false}
role="textbox"
/>
<RevealToggle
revealed={revealed}
+2 -1
View File
@@ -322,7 +322,8 @@ async def tool_delegate_task(
f"You should either: (1) try a different peer, (2) handle this task yourself, "
f"or (3) inform the user that {peer_name} is unavailable and provide your best answer."
)
return result
# OFFSEC-003: wrap peer result in trust boundary before returning to agent context
return sanitize_a2a_result(result)
async def tool_delegate_task_async(
-2
View File
@@ -27,8 +27,6 @@ async def list_peers() -> list[dict]:
async def delegate_task(workspace_id: str, task: str) -> str:
"""Send a task to a peer workspace via A2A and return the response text."""
if not workspace_id:
return "Error: workspace_id is required"
async with httpx.AsyncClient(timeout=120.0) as client:
# Discover target URL
try:
-420
View File
@@ -1,420 +0,0 @@
"""Test coverage for ``builtin_tools.a2a_tools`` and ``send_message_wrapper``.
Issue #367: 21 new test cases targeting previously-uncovered branches.
Uses ``respx`` for HTTP mocking — httpx.AsyncClient instantiates the client
before the mock can intervene (it resolves the host during __init__), so
patching at the class level is unreliable. respx intercepts at the transport
layer, which is safe regardless of how httpx initializes.
"""
from __future__ import annotations
import asyncio
import html
import os
import sys
from types import ModuleType
import pytest
import respx
# ---------------------------------------------------------------------------
# Session-scoped fixture — reload httpx once at test-session start
# ---------------------------------------------------------------------------
_httpx_reloaded = False
def _reload_httpx_and_real_module():
"""Force-reload httpx so builtin_tools.a2a_tools imports the real client.
conftest.py mocks builtin_tools.a2a_tools, which prevents Python from
importing the real module from disk (sys.modules takes precedence). This
helper removes both sys.modules entries and triggers a fresh import of the
real httpx + builtin_tools.a2a_tools chain.
"""
global _httpx_reloaded
if _httpx_reloaded:
return
_httpx_reloaded = True
# conftest.py set builtin_tools.__path__ = [] — restore so Python can
# find builtin_tools/a2a_tools.py on disk.
real_builtin = sys.modules.get("builtin_tools")
if real_builtin is not None:
builtin_dir = os.path.dirname(
os.path.dirname(os.path.abspath(__file__))
)
real_builtin.__path__ = [os.path.join(builtin_dir, "builtin_tools")]
# Remove the conftest.py mock so the real module loads
sys.modules.pop("builtin_tools.a2a_tools", None)
# Session-scoped: reload httpx once, not per-test. Per-test fixture only
# sets env vars (env vars can be set per-test without disturbing httpx).
@pytest.fixture(scope="session", autouse=True)
def _reload_httpx_session():
_reload_httpx_and_real_module()
yield
@pytest.fixture(autouse=True)
def _require_env(monkeypatch):
"""Per-test: set required env vars. httpx is already reloaded at session start."""
monkeypatch.setenv("WORKSPACE_ID", "00000000-0000-0000-0000-000000000001")
monkeypatch.setenv("PLATFORM_URL", "http://test.invalid")
yield
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _run(coro):
return asyncio.get_event_loop().run_until_complete(coro)
# =============================================================================
# builtin_tools/a2a_tools — list_peers
# =============================================================================
class TestListPeers:
"""Coverage for builtin_tools/a2a_tools.list_peers()."""
@respx.mock
def test_returns_peers_on_200(self):
"""Successful GET returns the peer list."""
from builtin_tools.a2a_tools import list_peers
peers = [
{"id": "ws-1", "name": "Alpha", "role": "sre", "status": "online"},
{"id": "ws-2", "name": "Beta", "role": "dev", "status": "busy"},
]
route = respx.get(
"http://test.invalid/registry/00000000-0000-0000-0000-000000000001/peers"
).respond(200, json=peers)
result = _run(list_peers())
assert result == peers
assert route.called
@respx.mock
def test_returns_empty_list_on_non_200(self):
"""list_peers swallows all non-200 responses gracefully."""
from builtin_tools.a2a_tools import list_peers
respx.get(
"http://test.invalid/registry/00000000-0000-0000-0000-000000000001/peers"
).respond(500)
result = _run(list_peers())
assert result == []
@respx.mock
def test_returns_empty_list_on_exception(self):
"""Network errors must not propagate — list_peers returns []. """
from builtin_tools.a2a_tools import list_peers
# Route that raises so httpx propagates an exception
respx.get(
"http://test.invalid/registry/00000000-0000-0000-0000-000000000001/peers"
).mock(side_effect=RuntimeError("dns failure"))
result = _run(list_peers())
assert result == []
# =============================================================================
# builtin_tools/a2a_tools — delegate_task
# =============================================================================
_DISCOVER_ROUTE = "http://test.invalid/registry/discover/ws-target"
class TestDelegateTask:
"""Coverage for builtin_tools/a2a_tools.delegate_task(workspace_id, task)."""
def test_empty_workspace_id_returns_error(self):
"""Empty workspace_id is validated before any network call."""
from builtin_tools.a2a_tools import delegate_task
out = _run(delegate_task("", "do it"))
assert "Error" in out
assert "workspace_id" in out.lower()
@respx.mock
def test_discover_returns_non_200(self):
"""Discovery 4xx/5xx → error message with status code."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(404)
out = _run(delegate_task("ws-target", "do it"))
assert "Error" in out
assert "404" in out
@respx.mock
def test_discover_returns_200_with_empty_url(self):
"""Discovery 200 but no url field → actionable error."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(200, json={"name": "orphan"})
out = _run(delegate_task("ws-target", "do it"))
assert "Error" in out
assert "no URL" in out
@respx.mock
def test_a2a_post_returns_500(self):
"""A2A send 5xx → Error: sending A2A message."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(
200, json={"url": "http://peer.invalid/a2a"}
)
respx.post("http://peer.invalid/a2a").respond(500)
out = _run(delegate_task("ws-target", "do it"))
assert "Error" in out
assert "sending A2A message" in out
@respx.mock
def test_result_parts_empty_dict(self):
"""Regression #279: {"parts": []} → str(result), not "(no text)"."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(
200, json={"url": "http://peer.invalid/a2a"}
)
respx.post("http://peer.invalid/a2a").respond(
200, json={"result": {"parts": []}}
)
out = _run(delegate_task("ws-target", "do it"))
# Must return str(result), not "(no text)"
assert "parts" in out
assert "(no text)" not in out
@respx.mock
def test_result_is_plain_string(self):
"""A bare string result returns as-is."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(
200, json={"url": "http://peer.invalid/a2a"}
)
respx.post("http://peer.invalid/a2a").respond(
200, json={"result": "just a plain string"}
)
out = _run(delegate_task("ws-target", "do it"))
assert out == "just a plain string"
@respx.mock
def test_result_is_number(self):
"""Non-dict, non-string result → falls through to "(no text)"."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(
200, json={"url": "http://peer.invalid/a2a"}
)
respx.post("http://peer.invalid/a2a").respond(
200, json={"result": 12345}
)
out = _run(delegate_task("ws-target", "do it"))
assert out == "(no text)"
@respx.mock
def test_result_parts_non_dict_element(self):
"""parts[0] is not a dict → falls through to "(no text)".
The code checks if parts[0] is a dict; since 123 is an int, it hits
the else-branch and returns "(no text)".
"""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(
200, json={"url": "http://peer.invalid/a2a"}
)
respx.post("http://peer.invalid/a2a").respond(
200, json={"result": {"parts": [123, "also a string"]}}
)
out = _run(delegate_task("ws-target", "do it"))
assert out == "(no text)"
@respx.mock
def test_error_dict_form(self):
"""{"error": {"message": "..."}} → "Error: ..."."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(
200, json={"url": "http://peer.invalid/a2a"}
)
respx.post("http://peer.invalid/a2a").respond(
200, json={"error": {"message": "peer overloaded", "code": 429}}
)
out = _run(delegate_task("ws-target", "do it"))
assert out == "Error: peer overloaded"
@respx.mock
def test_error_string_form(self):
"""{"error": "string error"} → "Error: string error"."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(
200, json={"url": "http://peer.invalid/a2a"}
)
respx.post("http://peer.invalid/a2a").respond(
200, json={"error": "workspace offline"}
)
out = _run(delegate_task("ws-target", "do it"))
assert out == "Error: workspace offline"
@respx.mock
def test_error_null(self):
"""{"error": null} → "Error: None" (edge case — str(null) in message)."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(
200, json={"url": "http://peer.invalid/a2a"}
)
respx.post("http://peer.invalid/a2a").respond(
200, json={"error": None}
)
out = _run(delegate_task("ws-target", "do it"))
assert "Error" in out
@respx.mock
def test_a2a_post_raises_exception(self):
"""Network error during A2A POST → Error: sending A2A message: ..."""
from builtin_tools.a2a_tools import delegate_task
respx.get(_DISCOVER_ROUTE).respond(
200, json={"url": "http://peer.invalid/a2a"}
)
respx.post("http://peer.invalid/a2a").mock(
side_effect=ConnectionError("connection refused")
)
out = _run(delegate_task("ws-target", "do it"))
assert "Error" in out
assert "connection refused" in out
# =============================================================================
# builtin_tools/a2a_tools — get_peers_summary
# =============================================================================
_PEERS_ROUTE = (
"http://test.invalid/registry/00000000-0000-0000-0000-000000000001/peers"
)
class TestGetPeersSummary:
"""Coverage for builtin_tools/a2a_tools.get_peers_summary()."""
@respx.mock
def test_empty_peers_returns_no_peers_available(self):
from builtin_tools.a2a_tools import get_peers_summary
respx.get(_PEERS_ROUTE).respond(200, json=[])
out = _run(get_peers_summary())
assert "No peers" in out
@respx.mock
def test_peer_missing_fields(self):
"""Peers with missing name/id/role/status must not KeyError/TypeError."""
from builtin_tools.a2a_tools import get_peers_summary
# Peer has only 'id'; name, role, status are absent
respx.get(_PEERS_ROUTE).respond(200, json=[{"id": "ws-x"}])
out = _run(get_peers_summary())
assert "ws-x" in out
assert isinstance(out, str)
@respx.mock
def test_healthy_peer_roundtrip(self):
"""Sanity: normal peer dicts produce a formatted list."""
from builtin_tools.a2a_tools import get_peers_summary
peers = [
{"id": "ws-alpha", "name": "Alpha", "role": "sre", "status": "online"},
]
respx.get(_PEERS_ROUTE).respond(200, json=peers)
out = _run(get_peers_summary())
assert "Alpha" in out
assert "ws-alpha" in out
assert "sre" in out
assert "online" in out
# =============================================================================
# send_message_wrapper — safe_send_message
# =============================================================================
from unittest.mock import patch
from adapters.smolagents.send_message_wrapper import safe_send_message
class TestSafeSendMessage:
"""Coverage for adapters.smolagents.send_message_wrapper.safe_send_message()."""
def test_non_string_input_converted(self):
"""Non-str text is str()-converted before escaping."""
delivered = []
safe_send_message(42, send_fn=lambda s: delivered.append(s))
assert delivered == ["[smolagents] 42"]
assert isinstance(delivered[0], str)
def test_html_entities_escaped(self):
"""< > ' are escaped so rendered UIs cannot be injected.
The payload <script>alert('xss')</script> has no literal '&', so &amp;
does not appear. The escape output is: &lt;script&gt;alert(&#x27;xss&#x27;)&lt;/script&gt;
"""
delivered = []
safe_send_message(
"<script>alert('xss')</script>",
send_fn=lambda s: delivered.append(s),
)
assert "&lt;" in delivered[0]
assert "&gt;" in delivered[0]
assert "&#x27;" in delivered[0]
assert "&lt;script&gt;" in delivered[0]
# The angle brackets and quotes must NOT appear unescaped
assert "<script>" not in delivered[0]
assert "alert('" not in delivered[0]
def test_truncation_at_max_len(self):
"""Text > 2000 chars is truncated; caller is warned."""
delivered = []
with patch(
"adapters.smolagents.send_message_wrapper.logger"
) as mock_logger:
long_text = "A" * 2500
safe_send_message(long_text, send_fn=lambda s: delivered.append(s))
assert len(delivered[0]) < len(long_text)
mock_logger.warning.assert_called_once()
assert "truncating" in mock_logger.warning.call_args[0][0]
def test_no_truncation_under_max_len(self):
"""Text ≤ 2000 chars is passed through intact with no warning."""
delivered = []
with patch(
"adapters.smolagents.send_message_wrapper.logger"
) as mock_logger:
text = "A" * 1500
safe_send_message(text, send_fn=lambda s: delivered.append(s))
expected = f"[smolagents] {text}"
assert delivered[0] == expected
mock_logger.warning.assert_not_called()
def test_debug_log_emitted(self):
"""Every delivery logs at DEBUG with final payload length."""
delivered = []
with patch(
"adapters.smolagents.send_message_wrapper.logger"
) as mock_logger:
safe_send_message("hello", send_fn=lambda s: delivered.append(s))
mock_logger.debug.assert_called_once()
assert "delivering" in mock_logger.debug.call_args[0][0]
def test_label_prefix_always_present(self):
"""Every delivered payload starts with '[smolagents]'."""
delivered = []
safe_send_message("x", send_fn=lambda s: delivered.append(s))
assert delivered[0].startswith("[smolagents]")