agent-color.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. },
  18. }),
  19. )
  20. },
  21. })
  22. await Instance.provide({
  23. directory: tmp.path,
  24. fn: async () => {
  25. const cfg = await Config.get()
  26. expect(cfg.agent?.["build"]?.color).toBe("#FFA500")
  27. },
  28. })
  29. })
  30. test("Agent.get includes color from config", async () => {
  31. await using tmp = await tmpdir({
  32. init: async (dir) => {
  33. await Bun.write(
  34. path.join(dir, "opencode.json"),
  35. JSON.stringify({
  36. $schema: "https://opencode.ai/config.json",
  37. agent: {
  38. plan: { color: "#A855F7" },
  39. },
  40. }),
  41. )
  42. },
  43. })
  44. await Instance.provide({
  45. directory: tmp.path,
  46. fn: async () => {
  47. const plan = await AgentSvc.get("plan")
  48. expect(plan?.color).toBe("#A855F7")
  49. },
  50. })
  51. })
  52. test("Color.hexToAnsiBold converts valid hex to ANSI", () => {
  53. const result = Color.hexToAnsiBold("#FFA500")
  54. expect(result).toBe("\x1b[38;2;255;165;0m\x1b[1m")
  55. })
  56. test("Color.hexToAnsiBold returns undefined for invalid hex", () => {
  57. expect(Color.hexToAnsiBold(undefined)).toBeUndefined()
  58. expect(Color.hexToAnsiBold("")).toBeUndefined()
  59. expect(Color.hexToAnsiBold("#FFF")).toBeUndefined()
  60. expect(Color.hexToAnsiBold("FFA500")).toBeUndefined()
  61. expect(Color.hexToAnsiBold("#GGGGGG")).toBeUndefined()
  62. })