Files
optimclaw/tests/gateway_workflow_integration.rs
T
f05896fe6a Migrate GitHub webhook normalization into github tool (#758)
* Add event-triggered routines and workflow skill templates

* Add generic host-verified webhook ingress for tools

* Migrate GitHub webhook normalization into github tool

* Bump github tool registry version

* Stabilize trace E2E test rig and approval behavior

* Add reusable gateway workflow harness with mock LLM server (#762)

* Add reusable gateway workflow test harness with mock LLM server

* Fix clippy issues in workflow harness

* Stabilize trace E2E test rig and approval behavior

* Address PR review feedback on gateway workflow harness

- Extract shared TestChannelHandle into test_channel.rs with name override
  support, eliminating ~55 lines of duplication between test_rig.rs and
  gateway_workflow_harness.rs
- Remove redundant RoutineEngine creation that was immediately overwritten
  by Agent::run()
- Replace flaky sleep(500ms) with polling loop for routine run count check
- Use components.context_manager instead of creating a fresh ContextManager
  for job tools, ensuring agent and tools share the same instance

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Fix import ordering in gateway_workflow_harness

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>

* Address PR #758 review feedback

- Fix header_value to use fully case-insensitive lookup (iterate with
  to_ascii_lowercase) instead of checking only exact/lower/upper variants
- Change comment_id from u32 to u64 to handle GitHub's billion-range IDs
- Remove handle_webhook from LLM-facing JSON schema to prevent direct
  invocation bypassing HMAC verification
- Rename enrichment keys from repository/sender to repository_name/
  sender_login to preserve original JSON objects in webhook payloads
- Remove put_string_normalized helper (no longer needed)
- Replace no-op tests (test_validate_event_in_create_pr_review,
  test_validate_merge_method) with test_header_value_case_insensitive
- Add README docs for 6 undocumented actions (list_issue_comments,
  create_issue_comment, list_pull_request_comments,
  reply_pull_request_comment, get_pull_request_reviews,
  get_combined_status)
- Add comment explaining max_tool_calls <= 8 bound in e2e test
- Fix gateway workflow harness: add webhook_capability with secret auth
  to MockGithubWebhookTool, matching staging's hardened webhook security
- Fix merge artifacts: remove duplicate test function, orphaned code
  fragment in e2e_routine_heartbeat

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Fix formatting in gateway workflow harness

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Address Copilot review: filter keys, pr_number fallback, feature gate, version alignment

- Update SKILL.md and workflow-routines.md templates to use `repository_name`
  and `sender_login` (matching enriched payload field names)
- Mark webhook HMAC secret as required in SKILL.md prerequisites
- Fall back to `/issue/number` for `pr_number` on issue_comment PR webhooks
- Gate `gateway_workflow_harness` module behind `#[cfg(feature = "libsql")]`
- Align tool version to 0.2.1 in Cargo.toml and capabilities.json

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
2026-03-12 01:52:47 +00:00

151 lines
5.5 KiB
Rust

//! Live-ish gateway workflow integration using an in-process mock OpenAI server.
//! This exercises the same path as manual validation:
//! - chat send through gateway
//! - routine creation via tool call
//! - system-event emission via tool call
//! - webhook ingestion via generic tools webhook server
//! - status/runs checks via routines API
#[cfg(feature = "libsql")]
mod support;
#[cfg(feature = "libsql")]
mod tests {
use std::time::Duration;
use crate::support::gateway_workflow_harness::GatewayWorkflowHarness;
use crate::support::mock_openai_server::{
MockOpenAiResponse, MockOpenAiRule, MockOpenAiServerBuilder, MockToolCall,
};
#[tokio::test]
async fn gateway_workflow_harness_chat_and_webhook() {
let mock = MockOpenAiServerBuilder::new()
.with_rule(MockOpenAiRule::on_user_contains(
"create workflow routine",
MockOpenAiResponse::ToolCalls(vec![MockToolCall::new(
"call_create_1",
"routine_create",
serde_json::json!({
"name": "wf-ci-webhook-demo",
"description": "CI webhook workflow demo",
"trigger_type": "system_event",
"event_source": "github",
"event_type": "issue.opened",
"event_filters": {"repository": "nearai/ironclaw"},
"action_type": "lightweight",
"prompt": "Summarize webhook and report issue number"
}),
)]),
))
.with_rule(MockOpenAiRule::on_user_contains(
"emit webhook event",
MockOpenAiResponse::ToolCalls(vec![MockToolCall::new(
"call_emit_1",
"event_emit",
serde_json::json!({
"source": "github",
"event_type": "issue.opened",
"payload": {
"repository": "nearai/ironclaw",
"issue": {"number": 777, "title": "Infra test"}
}
}),
)]),
))
.with_default_response(MockOpenAiResponse::Text("ack".to_string()))
.start()
.await;
let harness =
GatewayWorkflowHarness::start_openai_compatible(&mock.openai_base_url(), "mock-model")
.await;
let thread_id = harness.create_thread().await;
harness
.send_chat(&thread_id, "create workflow routine")
.await;
harness
.wait_for_turns(&thread_id, 1, Duration::from_secs(10))
.await;
let mut routine = None;
for _ in 0..30 {
routine = harness.routine_by_name("wf-ci-webhook-demo").await;
if routine.is_some() {
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
let routine = if let Some(r) = routine {
r
} else {
let history_dbg = harness.history(&thread_id).await;
let started_dbg = harness.test_channel.tool_calls_started();
let requests_dbg = mock.requests().await;
panic!(
"routine not created; tool_calls_started={started_dbg:?}; history={history_dbg}; mock_requests={requests_dbg:?}"
);
};
let routine_id = routine["id"].as_str().expect("routine id missing");
harness.send_chat(&thread_id, "emit webhook event").await;
let history = harness
.wait_for_turns(&thread_id, 2, Duration::from_secs(10))
.await;
let turns = history["turns"].as_array().expect("turns array missing");
assert!(turns.len() >= 2, "expected at least 2 turns");
let runs_before = harness.routine_runs(routine_id).await;
let before_count = runs_before["runs"]
.as_array()
.map(|a| a.len())
.unwrap_or_default();
let hook = harness
.github_webhook(
"issues",
serde_json::json!({
"action": "opened",
"repository": {"full_name": "nearai/ironclaw"},
"issue": {"number": 778, "title": "Webhook endpoint test"}
}),
)
.await;
assert_eq!(hook["status"], "accepted");
assert_eq!(hook["emitted_events"], 1);
assert!(
hook["fired_routines"].as_u64().unwrap_or(0) >= 1,
"expected webhook to fire at least one routine"
);
let mut after_count = before_count;
for _ in 0..50 {
let runs_after = harness.routine_runs(routine_id).await;
after_count = runs_after["runs"]
.as_array()
.map(|a| a.len())
.unwrap_or_default();
if after_count > before_count {
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
assert!(
after_count > before_count,
"expected routine runs to increase after webhook; before={before_count}, after={after_count}"
);
let requests = mock.requests().await;
assert!(
requests.len() >= 2,
"expected mock LLM server to receive requests"
);
harness.shutdown().await;
mock.shutdown().await;
}
}