|
| 1 | +/// <reference types="bun-types/test-globals" /> |
| 2 | + |
| 3 | +import { mkdtempSync, rmSync, writeFileSync } from "fs"; |
| 4 | +import { join } from "path"; |
| 5 | +import { tmpdir } from "os"; |
| 6 | +import { |
| 7 | + isCursorPluginEnabledInConfig, |
| 8 | + resolveOpenCodeConfigPath, |
| 9 | + shouldEnableCursorPlugin, |
| 10 | +} from "../../src/plugin-toggle"; |
| 11 | + |
| 12 | +describe("plugin toggle", () => { |
| 13 | + it("enables plugin when plugin array includes cursor-acp", () => { |
| 14 | + expect(isCursorPluginEnabledInConfig({ plugin: ["cursor-acp"] })).toBe(true); |
| 15 | + }); |
| 16 | + |
| 17 | + it("disables plugin when plugin array excludes cursor-acp", () => { |
| 18 | + expect(isCursorPluginEnabledInConfig({ plugin: ["other-plugin"] })).toBe(false); |
| 19 | + }); |
| 20 | + |
| 21 | + it("keeps legacy behavior when plugin array is missing", () => { |
| 22 | + expect(isCursorPluginEnabledInConfig({ provider: { "cursor-acp": {} } })).toBe(true); |
| 23 | + expect(isCursorPluginEnabledInConfig({ provider: {} })).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it("resolves config from OPENCODE_CONFIG first", () => { |
| 27 | + const customConfig = join(tmpdir(), "custom-opencode.json"); |
| 28 | + const xdgHome = join(tmpdir(), "xdg"); |
| 29 | + const path = resolveOpenCodeConfigPath({ |
| 30 | + OPENCODE_CONFIG: customConfig, |
| 31 | + XDG_CONFIG_HOME: xdgHome, |
| 32 | + }); |
| 33 | + expect(path).toBe(customConfig); |
| 34 | + }); |
| 35 | + |
| 36 | + it("disables when config file exists and plugin array excludes cursor-acp", () => { |
| 37 | + const dir = mkdtempSync(join(tmpdir(), "cursor-toggle-")); |
| 38 | + const configPath = join(dir, "opencode.json"); |
| 39 | + |
| 40 | + try { |
| 41 | + writeFileSync(configPath, JSON.stringify({ plugin: ["other-plugin"] })); |
| 42 | + const state = shouldEnableCursorPlugin({ OPENCODE_CONFIG: configPath }); |
| 43 | + expect(state.enabled).toBe(false); |
| 44 | + expect(state.reason).toBe("disabled_in_plugin_array"); |
| 45 | + } finally { |
| 46 | + rmSync(dir, { recursive: true, force: true }); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + it("stays enabled when config is invalid JSON", () => { |
| 51 | + const dir = mkdtempSync(join(tmpdir(), "cursor-toggle-")); |
| 52 | + const configPath = join(dir, "opencode.json"); |
| 53 | + |
| 54 | + try { |
| 55 | + writeFileSync(configPath, "{not-json"); |
| 56 | + const state = shouldEnableCursorPlugin({ OPENCODE_CONFIG: configPath }); |
| 57 | + expect(state.enabled).toBe(true); |
| 58 | + expect(state.reason).toBe("config_unreadable_or_invalid"); |
| 59 | + } finally { |
| 60 | + rmSync(dir, { recursive: true, force: true }); |
| 61 | + } |
| 62 | + }); |
| 63 | +}); |
0 commit comments