"""Prompts the model schedules for itself must show in the chat sidebar, so the user sees what will wake the session without opening /schedule.""" import json import sqlite3 import unittest import urllib.error from harness import Albedo, Provider, exclusive, python, text SCHEDULE = ( "await commands.schedule('add', 'in:5400 rebuild the index')\n" "await commands.schedule('heartbeat', 'every:600 check the ci')" ) def reply(request): if request["messages"][-1].get("role") == "user": return python(SCHEDULE) return text("scheduled") class ScheduleTests(unittest.TestCase): def test_http_refusals_preserve_status_code_and_detail(self): provider = Provider(lambda _: text("unused")) self.addCleanup(provider.close) with Albedo(provider) as app: session = app.session() candidate = { "session_id": session, "kind": "once", "prompt": "later", "delay_seconds": 3600, } for path, body, expected in [ ( "/extensions/schedule/jobs/9223372036854775807", None, (404, "not_found", "schedule not found"), ), ( "/extensions/schedule/jobs", {**candidate, "session_id": "absent-session"}, (404, "not_found", "session not found"), ), ( "/extensions/schedule/jobs", {**candidate, "prompt": ""}, (400, "invalid_request", "invalid schedule candidate"), ), ]: with self.subTest(path=path, body=body): with self.assertRaises(urllib.error.HTTPError) as caught: app.api(path, body) problem = json.load(caught.exception) self.assertEqual( (caught.exception.code, problem["code"], problem["detail"]), expected, ) with app.api("/extensions/schedule/jobs", candidate) as response: resource = json.load(response)["resource"] with app.api( resource["url"], {"prompt": "changed"}, method="PATCH", headers={"If-Match": resource["etag"]}, ) as response: response.read() with self.assertRaises(urllib.error.HTTPError) as caught: app.api( resource["url"], method="DELETE", headers={"If-Match": resource["etag"]}, ) problem = json.load(caught.exception) self.assertEqual( (caught.exception.code, problem["code"], problem["detail"]), (412, "precondition_failed", "the observed resource has changed"), ) # exclusive: installs a global schedule write trigger @exclusive def test_storage_refusal_is_not_reclassified_as_a_missing_schedule(self): provider = Provider(lambda _: text("unused")) self.addCleanup(provider.close) with Albedo(provider) as app: session = app.session() with sqlite3.connect(app.home / "albedo.sqlite") as db: db.execute( "CREATE TRIGGER reject_schedule BEFORE INSERT ON schedules " "BEGIN SELECT RAISE(FAIL, 'schedule not found'); END" ) with self.assertRaises(urllib.error.HTTPError) as caught: app.api( "/extensions/schedule/jobs", { "session_id": session, "kind": "once", "prompt": "later", "delay_seconds": 3600, }, ) problem = json.load(caught.exception) self.assertEqual(caught.exception.code, 503) self.assertEqual(problem["code"], "storage_failed") with app.api(f"/extensions/schedule/jobs?session_id={session}") as response: self.assertEqual(json.load(response)["items"], []) def test_model_schedules_show_in_the_sidebar(self): provider = Provider(reply) self.addCleanup(provider.close) with Albedo(provider) as app: session = app.session() app.prompt(session, "remind yourself").close() app.idle(session) with app.api(f"/sessions/{session}?tail=0") as response: glances = json.load(response)["glances"] glance = next(g for g in glances if g["extension"] == "schedule") self.assertEqual(glance["title"], "scheduled") self.assertEqual( glance["url"], f"/extensions/schedule/jobs?session_id={session}" ) # soonest first, each led by when it fires self.assertEqual( [(row["badge"], row["text"]) for row in glance["rows"]], [ ("heartbeat", "heartbeat 10m · check the ci"), ("once", "in 1h · rebuild the index"), ], ) if __name__ == "__main__": unittest.main()