agent-color.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { test, expect } from "bun:test"
  2. import path from "path"
  3. import { tmpdir } from "../fixture/fixture"
  4. import { Instance } from "../../src/project/instance"
  5. import { Config } from "../../src/config/config"
  6. import { Agent as AgentSvc } from "../../src/agent/agent"
  7. import { Color } from "../../src/util/color"
  8. test("agent color parsed from project config", async () => {
  9. await using tmp = await tmpdir({
  10. init: async (dir) => {
  11. await Bun.write(
  12. path.join(dir, "opencode.json"),
  13. JSON.stringify({
  14. $schema: "https://opencode.ai/config.json",
  15. agent: {
  16. build: { color: "#FFA500" },
  17. plan: { color: "primary" },
  18. },
  19. }),
  20. )
  21. },
  22. })
  23. await Instance.provide({
  24. directory: tmp.path,
  25. fn: async () => {
  26. const cfg = await Config.get()
  27. expect(cfg.agent?.["build"]?.color).toBe("#FFA500")
  28. expect(cfg.agent?.["plan"]?.color).toBe("primary")
  29. },
  30. })
  31. })
  32. test("Agent.get includes color from config", async () => {
  33. await using tmp = await tmpdir({
  34. init: async (dir) => {
  35. await Bun.write(
  36. path.join(dir, "opencode.json"),
  37. JSON.stringify({
  38. $schema: "https://opencode.ai/config.json",
  39. agent: {
  40. plan: { color: "#A855F7" },
  41. build: { color: "accent" },
  42. },
  43. }),
  44. )
  45. },
  46. })
  47. await Instance.provide({
  48. directory: tmp.path,
  49. fn: async () => {
  50. const plan = await AgentSvc.get("plan")
  51. expect(plan?.color).toBe("#A855F7")
  52. const build = await AgentSvc.get("build")
  53. expect(build?.color).toBe("accent")
  54. },
  55. })
  56. })
  57. test("Color.hexToAnsiBold converts valid hex to ANSI", () => {
  58. const result = Color.hexToAnsiBold("#FFA500")
  59. expect(result).toBe("\x1b[38;2;255;165;0m\x1b[1m")
  60. })
  61. test("Color.hexToAnsiBold returns undefined for invalid hex", () => {
  62. expect(Color.hexToAnsiBold(undefined)).toBeUndefined()
  63. expect(Color.hexToAnsiBold("")).toBeUndefined()
  64. expect(Color.hexToAnsiBold("#FFF")).toBeUndefined()
  65. expect(Color.hexToAnsiBold("FFA500")).toBeUndefined()
  66. expect(Color.hexToAnsiBold("#GGGGGG")).toBeUndefined()
  67. })