"""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 its credential from the whole token response (access, rotating refresh, org). With the flow in `albedo_device_flow`, this is the test that keeps that shape working for the provider that uses it. The sign-in writes the daemon's own creds, so the class is exclusive. """ import atexit import json import time import unittest from harness import Albedo, Provider, exclusive_class, operation_id, text from 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_class class 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")