|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "bun:test" |
| 2 | +import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import { join } from "node:path" |
| 5 | + |
| 6 | +import { resetConfigContext } from "./config-context" |
| 7 | +import { detectCurrentConfig } from "./detect-current-config" |
| 8 | +import { addPluginToOpenCodeConfig } from "./add-plugin-to-opencode-config" |
| 9 | + |
| 10 | +describe("detectCurrentConfig - dual name detection", () => { |
| 11 | + let testConfigDir = "" |
| 12 | + let testConfigPath = "" |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + testConfigDir = join(tmpdir(), `omo-detect-config-${Date.now()}-${Math.random().toString(36).slice(2)}`) |
| 16 | + testConfigPath = join(testConfigDir, "opencode.json") |
| 17 | + |
| 18 | + mkdirSync(testConfigDir, { recursive: true }) |
| 19 | + process.env.OPENCODE_CONFIG_DIR = testConfigDir |
| 20 | + resetConfigContext() |
| 21 | + }) |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + rmSync(testConfigDir, { recursive: true, force: true }) |
| 25 | + resetConfigContext() |
| 26 | + delete process.env.OPENCODE_CONFIG_DIR |
| 27 | + }) |
| 28 | + |
| 29 | + it("detects oh-my-opencode in plugin array", () => { |
| 30 | + // given |
| 31 | + const config = { plugin: ["oh-my-opencode"] } |
| 32 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 33 | + |
| 34 | + // when |
| 35 | + const result = detectCurrentConfig() |
| 36 | + |
| 37 | + // then |
| 38 | + expect(result.isInstalled).toBe(true) |
| 39 | + }) |
| 40 | + |
| 41 | + it("detects oh-my-openagent in plugin array", () => { |
| 42 | + // given |
| 43 | + const config = { plugin: ["oh-my-openagent"] } |
| 44 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 45 | + |
| 46 | + // when |
| 47 | + const result = detectCurrentConfig() |
| 48 | + |
| 49 | + // then |
| 50 | + expect(result.isInstalled).toBe(true) |
| 51 | + }) |
| 52 | + |
| 53 | + it("detects oh-my-opencode with version pin", () => { |
| 54 | + // given |
| 55 | + const config = { plugin: ["[email protected]"] } |
| 56 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 57 | + |
| 58 | + // when |
| 59 | + const result = detectCurrentConfig() |
| 60 | + |
| 61 | + // then |
| 62 | + expect(result.isInstalled).toBe(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it("detects oh-my-openagent with version pin", () => { |
| 66 | + // given |
| 67 | + const config = { plugin: ["[email protected]"] } |
| 68 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 69 | + |
| 70 | + // when |
| 71 | + const result = detectCurrentConfig() |
| 72 | + |
| 73 | + // then |
| 74 | + expect(result.isInstalled).toBe(true) |
| 75 | + }) |
| 76 | + |
| 77 | + it("returns false when plugin not present", () => { |
| 78 | + // given |
| 79 | + const config = { plugin: ["some-other-plugin"] } |
| 80 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 81 | + |
| 82 | + // when |
| 83 | + const result = detectCurrentConfig() |
| 84 | + |
| 85 | + // then |
| 86 | + expect(result.isInstalled).toBe(false) |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +describe("addPluginToOpenCodeConfig - dual name detection", () => { |
| 91 | + let testConfigDir = "" |
| 92 | + let testConfigPath = "" |
| 93 | + |
| 94 | + beforeEach(() => { |
| 95 | + testConfigDir = join(tmpdir(), `omo-add-plugin-${Date.now()}-${Math.random().toString(36).slice(2)}`) |
| 96 | + testConfigPath = join(testConfigDir, "opencode.json") |
| 97 | + |
| 98 | + mkdirSync(testConfigDir, { recursive: true }) |
| 99 | + process.env.OPENCODE_CONFIG_DIR = testConfigDir |
| 100 | + resetConfigContext() |
| 101 | + }) |
| 102 | + |
| 103 | + afterEach(() => { |
| 104 | + rmSync(testConfigDir, { recursive: true, force: true }) |
| 105 | + resetConfigContext() |
| 106 | + delete process.env.OPENCODE_CONFIG_DIR |
| 107 | + }) |
| 108 | + |
| 109 | + it("finds and replaces old oh-my-opencode with new name", async () => { |
| 110 | + // given |
| 111 | + const config = { plugin: ["oh-my-opencode"] } |
| 112 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 113 | + |
| 114 | + // when |
| 115 | + const result = await addPluginToOpenCodeConfig("3.11.0") |
| 116 | + |
| 117 | + // then |
| 118 | + expect(result.success).toBe(true) |
| 119 | + const savedConfig = JSON.parse(readFileSync(testConfigPath, "utf-8")) |
| 120 | + expect(savedConfig.plugin).toContain("oh-my-openagent") |
| 121 | + expect(savedConfig.plugin).not.toContain("oh-my-opencode") |
| 122 | + }) |
| 123 | + |
| 124 | + it("finds and replaces oh-my-openagent with new name", async () => { |
| 125 | + // given |
| 126 | + const config = { plugin: ["oh-my-openagent"] } |
| 127 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 128 | + |
| 129 | + // when |
| 130 | + const result = await addPluginToOpenCodeConfig("3.11.0") |
| 131 | + |
| 132 | + // then |
| 133 | + expect(result.success).toBe(true) |
| 134 | + const savedConfig = JSON.parse(readFileSync(testConfigPath, "utf-8")) |
| 135 | + expect(savedConfig.plugin).toContain("oh-my-openagent") |
| 136 | + expect(savedConfig.plugin).not.toContain("oh-my-opencode") |
| 137 | + }) |
| 138 | + |
| 139 | + it("finds and replaces version-pinned [email protected]", async () => { |
| 140 | + // given |
| 141 | + const config = { plugin: ["[email protected]"] } |
| 142 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 143 | + |
| 144 | + // when |
| 145 | + const result = await addPluginToOpenCodeConfig("3.11.0") |
| 146 | + |
| 147 | + // then |
| 148 | + expect(result.success).toBe(true) |
| 149 | + const savedConfig = JSON.parse(readFileSync(testConfigPath, "utf-8")) |
| 150 | + expect(savedConfig.plugin).toContain("oh-my-openagent") |
| 151 | + expect(savedConfig.plugin).not.toContain("[email protected]") |
| 152 | + }) |
| 153 | + |
| 154 | + it("finds and replaces version-pinned [email protected]", async () => { |
| 155 | + // given |
| 156 | + const config = { plugin: ["[email protected]"] } |
| 157 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 158 | + |
| 159 | + // when |
| 160 | + const result = await addPluginToOpenCodeConfig("3.11.0") |
| 161 | + |
| 162 | + // then |
| 163 | + expect(result.success).toBe(true) |
| 164 | + const savedConfig = JSON.parse(readFileSync(testConfigPath, "utf-8")) |
| 165 | + expect(savedConfig.plugin).toContain("oh-my-openagent") |
| 166 | + expect(savedConfig.plugin).not.toContain("[email protected]") |
| 167 | + }) |
| 168 | + |
| 169 | + it("adds new plugin when none exists", async () => { |
| 170 | + // given - no plugin array |
| 171 | + const config = {} |
| 172 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 173 | + |
| 174 | + // when |
| 175 | + const result = await addPluginToOpenCodeConfig("3.11.0") |
| 176 | + |
| 177 | + // then |
| 178 | + expect(result.success).toBe(true) |
| 179 | + const savedConfig = JSON.parse(readFileSync(testConfigPath, "utf-8")) |
| 180 | + expect(savedConfig.plugin).toContain("oh-my-openagent") |
| 181 | + }) |
| 182 | + |
| 183 | + it("adds plugin when plugin array is empty", async () => { |
| 184 | + // given - empty plugin array |
| 185 | + const config = { plugin: [] } |
| 186 | + writeFileSync(testConfigPath, JSON.stringify(config, null, 2) + "\n", "utf-8") |
| 187 | + |
| 188 | + // when |
| 189 | + const result = await addPluginToOpenCodeConfig("3.11.0") |
| 190 | + |
| 191 | + // then |
| 192 | + expect(result.success).toBe(true) |
| 193 | + const savedConfig = JSON.parse(readFileSync(testConfigPath, "utf-8")) |
| 194 | + expect(savedConfig.plugin).toContain("oh-my-openagent") |
| 195 | + }) |
| 196 | +}) |
0 commit comments