Files
alzeo/dashboard/tests/test_cockpit_opencode_config.py
T
Hasan BasbunarandClaude Opus 5.5 d10b3273f0 opencode: a provider block in opencode.jsonc, which wins over opencode.json, keeps the lane's limits and default too
opencode merges the user's global files and takes the .jsonc's: run on a home of its own (2.3.2), a model's limit in both
files is the .jsonc's. This repo wrote opencode.json alone, and the cockpit read it alone: a block pasted in the .jsonc
kept the limits and the default of the day it was pasted. The editors that write a model's limits, the compaction and the
default edit the .jsonc beside the user's opencode.json when it names a provider of this box, comments kept, the fit reads
it as a file of its own, and the cockpit shows what wins. A .jsonc with no provider of ours is left alone, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 08:59:12 +01:00

92 lines
4.2 KiB
Python

"""The Settings tab reads opencode's config the way opencode does.
collect_opencode() read it with json.loads: a config with a comment or a trailing comma,
which opencode reads (jsonc-parser), came out unreadable, and an unreadable one showed as
empty ("none", "not declared") on the page (found in review, 2026-09-24). This loads the
real cockpit with COCKPIT_DRY_RUN=1, a throwaway config dir and a throwaway HOME."""
import importlib.util
import os
import shutil
import sys
import tempfile
import unittest
from pathlib import Path
HERE = Path(__file__).resolve()
DASH = HERE.parents[1]
REPO = HERE.parents[2]
COMMENTED = """{
// the lane the installer points at
"model": "qwen38/qwen3.8-27b",
"provider": {
"qwen38": {"models": {"qwen3.8-27b": {"limit": {"context": 559000, "output": 186000},},},},
},
}
"""
class TheConfigAsOpencodeReadsIt(unittest.TestCase):
@classmethod
def setUpClass(cls):
env, path = dict(os.environ), list(sys.path)
cls.addClassCleanup(lambda: (os.environ.clear(), os.environ.update(env), sys.path.__setitem__(slice(None), path)))
cls.tmp = Path(tempfile.mkdtemp(prefix="cockpit-oc-"))
cls.addClassCleanup(shutil.rmtree, cls.tmp, True)
(cls.tmp / "cfg").mkdir()
(cls.tmp / "cfg" / "api-key").write_text("k\n")
os.environ.update(HOME=str(cls.tmp), COCKPIT_DRY_RUN="1", COCKPIT_CONFIG_DIR=str(cls.tmp / "cfg"),
COCKPIT_REPO_DIR=str(REPO), COCKPIT_PORT="0", COCKPIT_AGENT_PORT="0", COCKPIT_AUTOHEAL="0")
sys.path.insert(0, str(DASH))
spec = importlib.util.spec_from_file_location("cockpit_oc_config_under_test", DASH / "cockpit.py")
cls.cp = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cls.cp)
cls.real = cls.tmp / ".config" / "opencode" / "opencode.json"
cls.real.parent.mkdir(parents=True)
def collect(self, text):
self.real.write_text(text)
out = self.cp.collect_opencode()
return out.get("data", out)["real"]
def test_the_launcher_is_read_as_the_opencode_it_starts(self):
# OpenCode 2's launcher, the default since v1.56.0, lifts no cap: the page read "output cap ?" (found 2026-11-30)
oc = self.tmp / ".local" / "bin" / "oc"
oc.parent.mkdir(parents=True, exist_ok=True)
for text, want in (("#!/bin/bash\n# oc launcher installed by dgx-spark-qwen38: ...\n# This one starts OpenCode 2 (...)\n", (True, None, 2)),
("#!/bin/bash\n# oc launcher installed by dgx-spark-qwen38\nexport OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX=200000\n", (True, 200000, 1))):
oc.write_text(text)
out = self.cp.collect_opencode()
got = out.get("data", out)["launcher"]
self.assertEqual((got["ours"], got["cap"], got["major"]), want)
oc.unlink()
def test_comments_and_trailing_commas_are_read(self):
real = self.collect(COMMENTED)
self.assertNotIn("error", real)
self.assertEqual(real["default"], "qwen38/qwen3.8-27b")
self.assertEqual(real["limits"]["qwen38/qwen3.8-27b"], {"context": 559000, "output": 186000})
def test_the_jsonc_beside_it_wins_as_opencode_merges_them(self):
# opencode 2.3.2 on a home of its own (2026-11-30): the limit and the default of the .jsonc, the rest of the .json
beside = self.real.with_suffix(".jsonc")
self.addCleanup(beside.unlink, True)
beside.write_text('// mine\n{"provider": {"qwen38": {"models": {"qwen3.8-27b": {"limit": {"context": 111000, "output": 1111}}}}}}')
real = self.collect(COMMENTED)
self.assertEqual((real["limits"]["qwen38/qwen3.8-27b"], real["default"]), ({"context": 111000, "output": 1111}, "qwen38/qwen3.8-27b"))
beside.write_text('{"model": "openai/gpt-5"}')
self.assertEqual(self.collect(COMMENTED)["default"], "openai/gpt-5")
def test_a_config_that_does_not_parse_says_so(self):
real = self.collect('{"model": ')
self.assertIn("error", real)
self.assertIsNone(real["default"])
def test_a_config_that_is_not_an_object_says_so(self):
real = self.collect("[1, 2]")
self.assertIn("not a JSON object", real["error"])
if __name__ == "__main__":
unittest.main(verbosity=2)