"""Delivery mode pull: nothing is sent; the Fusion workflow (a While Loop with a Wait) picks its events up with its own OAuth client and confirms each. No OCI IAM app is needed. An event not confirmed is handed out again; one client never sees another's events.""" from __future__ import annotations import unittest from coworker.platform import agent_studio as A from coworker.platform.agent_studio import AgentStudio from coworker.tests.test_ai_scenarios import fusion_app class TestPullDelivery(unittest.TestCase): @classmethod def setUpClass(cls): cls.app, cls.c, cls.svc, cls.h, _ = fusion_app() cls.studio = AgentStudio(cls.svc) cls.clients = {} for name in ("pull", "other"): r = cls.c.post("/api/agent-studio/clients", json={"name": name}, headers=cls.h["admin"]) assert r.status_code == 201, r.text cls.clients[name] = r.json() for name in ("pull", "other"): r = cls.c.put(f"/api/agent-studio/clients/{cls.clients[name]['client_id']}/delivery", json={"mode": "pull"}, headers=cls.h["admin"]) assert r.status_code == 200 and r.json()["delivery"] == "pull", r.text def auth(self, name="pull"): c = self.clients[name] t = self.c.post("/oauth/token", data={"grant_type": "client_credentials", "client_id": c["client_id"], "client_secret": c["client_secret"], "scope": "patchwise.invoke"}) assert t.status_code == 200, t.text return {"Authorization": "Bearer " + t.json()["access_token"]} def claim(self, name="pull", **body): r = self.c.post("/api/agent-studio/v1/events/claim", json=body, headers=self.auth(name)) self.assertEqual(r.status_code, 200, r.text) return r.json() def ack(self, ids, name="pull"): return self.c.post("/api/agent-studio/v1/events/ack", json={"delivery_ids": ids}, headers=self.auth(name)).json() def test_a_events_wait_are_picked_up_once_and_confirmed(self): cid = self.clients["pull"]["client_id"] d1 = self.studio.queue(cid, None, "scripts_ready", {"count": 3}, "cyc_pull_a") d2 = self.studio.queue(cid, None, "results", {"passed": 3}, "cyc_pull_b") self.assertEqual(self.studio.deliver_due(), 0) # nothing is pushed got = self.claim(cycle_id="cyc_pull_a") self.assertEqual([(e["delivery_id"], e["event"], e["count"]) for e in got["events"]], [(d1, "scripts_ready", 3)]) self.assertEqual(self.claim(cycle_id="cyc_pull_a")["count"], 0) # held while Fusion handles it self.assertEqual(self.ack([d1, "dlv_unknown"])["confirmed"], [d1]) self.assertEqual(self.ack([d1])["count"], 0) # confirming twice is harmless self.assertEqual([e["delivery_id"] for e in self.claim()["events"]], [d2]) self.ack([d2]) def test_b_an_unconfirmed_event_comes_back(self): cid = self.clients["pull"]["client_id"] did = self.studio.queue(cid, None, "stopped", {"status": "failed"}, "cyc_pull_c") self.assertEqual(self.claim(cycle_id="cyc_pull_c")["events"][0]["pickup"], 1) self.svc.db.q("UPDATE agent_callbacks SET next_at=? WHERE delivery_id=?", ("2000-01-01T00:00:00+00:00", did)) again = self.claim(cycle_id="cyc_pull_c")["events"] self.assertEqual((again[0]["delivery_id"], again[0]["pickup"]), (did, 2)) self.ack([did]) def test_c_a_client_sees_and_confirms_only_its_own_events(self): did = self.studio.queue(self.clients["pull"]["client_id"], None, "results", {}, "cyc_pull_d") self.assertEqual(self.claim("other")["count"], 0) self.assertEqual(self.ack([did], "other")["count"], 0) self.assertEqual(self.claim(cycle_id="cyc_pull_d")["count"], 1) self.ack([did]) def test_d_claim_needs_the_client_token_and_limits_are_kept(self): self.assertEqual(self.c.post("/api/agent-studio/v1/events/claim", json={}).status_code, 401) self.assertEqual(self.c.post("/api/agent-studio/v1/events/claim", json={"max": 50}, headers=self.auth()).status_code, 422) cid = self.clients["pull"]["client_id"] ids = [self.studio.queue(cid, None, "results", {"n": i}, "cyc_pull_e") for i in range(3)] got = self.claim(cycle_id="cyc_pull_e", max=2) self.assertEqual((got["count"], got["more"], got["confirm_within_s"]), (2, True, A.LEASE_S)) self.ack(ids) def test_f_the_check_function_also_picks_up_and_confirms(self): cid = self.clients["pull"]["client_id"] did = self.studio.queue(cid, None, "scripts_ready", {"count": 2}, "cyc_pull_f") h = self.auth() got = self.c.post("/api/agent-studio/v1/test-data/check", json={"pickup": {"max": 1, "cycle_id": "cyc_pull_f"}}, headers=h).json() self.assertEqual([(e["delivery_id"], e["event"]) for e in got["events"]], [(did, "scripts_ready")]) self.assertEqual(self.c.post("/api/agent-studio/v1/test-data/check", json={"confirm": [did]}, headers=h).json()["confirmed"], [did]) self.assertEqual(self.claim(cycle_id="cyc_pull_f")["count"], 0) bad = self.c.post("/api/agent-studio/v1/test-data/check", json={"pickup": {"max": "lots"}}, headers=h) self.assertEqual(bad.status_code, 200) # a bad max reads as 1, never an error plain = self.c.post("/api/agent-studio/v1/test-data/check", json={"test_data": {"AP": {"supplier": "TEST_SUPPLIER"}}}, headers=h).json() self.assertTrue(plain["ok"]) # the plain check is unchanged self.assertEqual(self.c.post("/api/agent-studio/v1/test-data/check", json={"pickup": {}}).status_code, 401) def test_e_bootstrap_can_set_pull(self): import json import os from unittest import mock cid, secret = "pwc_00112233445566cc", "pws_" + "p" * 40 with mock.patch.dict(os.environ, {"FPT_SECRET_AGENT_STUDIO_BOOTSTRAP_CLIENTS": json.dumps([{"client_id": cid, "client_secret": secret, "delivery": "pull"}])}): self.assertEqual(self.studio.ensure_from_env(), [cid]) self.assertEqual(self.studio.client(cid)["delivery"], "pull") if __name__ == "__main__": unittest.main()