Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899"""OpenCode Go's sign-in runs on the device flow every subscription provider shares.
It speaks JSON to its console, adds an organization to the grant, and builds itscredential from the whole token response (access, rotating refresh, org). Withthe flow in `albedo_device_flow`, this is the test that keeps that shape workingfor the provider that uses it.
The sign-in writes the daemon's own creds, so the class is exclusive."""
import atexitimport jsonimport timeimport unittest
from harness import Albedo, Provider, exclusive_class, operation_id, textfrom opencode_go_support import ACCESS_TOKEN, ORG, REFRESH_TOKEN, Console
_console = None
def console(): global _console if _console is None: _console = Console().start() atexit.register(_console.stop) return _console
@exclusive_classclass OpencodeGoLoginTests(unittest.TestCase): @property def console(self): return console()
def prepare(self, app): app.env["OPENCODE_CONSOLE_SERVER"] = self.console.url
def test_the_device_sign_in_stores_an_org_scoped_account(self): provider = Provider(lambda _request: text("ok")) self.addCleanup(provider.close) app = Albedo( provider, providers={ "go": { "extension": "opencode-go", "baseUrl": provider.url, "model": "fixture-model", "protocol": "chat_completions", } }, prepare=self.prepare, ) app.__enter__() self.addCleanup(app.__exit__, None, None, None)
login = operation_id() with app.api( f"/auth/logins/{login}", {"provider": "opencode-go", "flow": "browser", "values": {}}, method="PUT", ) as response: json.load(response)
deadline = time.monotonic() + 30 while True: with app.api(f"/auth/logins/{login}") as response: state = json.load(response) if state.get("url") or state["state"] not in ("waiting", "exchanging"): break self.assertLess(time.monotonic(), deadline, state) time.sleep(0.05) self.assertTrue(state["url"].startswith(self.console.url), state)
self.console.authorize() while state["state"] in ("waiting", "exchanging"): with app.api(f"/auth/logins/{login}") as response: state = json.load(response) self.assertLess(time.monotonic(), deadline, state) time.sleep(0.05) self.assertEqual(state["state"], "complete", state)
stored = json.loads((app.home / "creds.json").read_text()) account = stored["accounts"]["opencode-go"] self.assertEqual(account["type"], "oauth", account) self.assertEqual(account["access"], ACCESS_TOKEN, account) self.assertEqual(account["refresh"], REFRESH_TOKEN, account) self.assertEqual(account["orgId"], ORG["id"], account) self.assertEqual(account["email"], "ana@example.com", account)
# The code request carried the console's own client identity and asked # for an organization scope; the polls named the device code. code_request = self.console.requests("/auth/device/code")[0]["body"] self.assertEqual(code_request["client_id"], "opencode-cli") self.assertTrue(code_request["supports_org_scope"]) polls = self.console.requests("/auth/device/token") self.assertTrue(polls, self.console.seen) self.assertEqual(polls[0]["body"]["device_code"], "opencode-device-code")