733b54865f
OpenCode supports plugin entries as [string, object] tuples for passing options, but loadOpencodePlugins assumed all entries were strings. When a tuple entry hit matchesKnownPlugin, it called .toLowerCase() on an array, crashing the plugin on startup. Extract the string name from tuple entries and skip non-string values. Add regression test covering the tuple plugin format.
492 lines
17 KiB
TypeScript
492 lines
17 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"
|
|
import { detectExternalNotificationPlugin, getNotificationConflictWarning, detectExternalSkillPlugin, getSkillPluginConflictWarning } from "./external-plugin-detector"
|
|
import * as fs from "node:fs"
|
|
import * as path from "node:path"
|
|
import * as os from "node:os"
|
|
|
|
async function importFreshExternalPluginDetectorModule(): Promise<typeof import("./external-plugin-detector")> {
|
|
return import(`./external-plugin-detector?test=${Date.now()}-${Math.random()}`)
|
|
}
|
|
|
|
describe("external-plugin-detector", () => {
|
|
let tempDir: string
|
|
let tempHomeDir: string
|
|
|
|
beforeEach(() => {
|
|
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "omo-test-"))
|
|
tempHomeDir = fs.mkdtempSync(path.join(os.tmpdir(), "omo-home-"))
|
|
})
|
|
|
|
afterEach(() => {
|
|
mock.restore()
|
|
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
fs.rmSync(tempHomeDir, { recursive: true, force: true })
|
|
})
|
|
|
|
describe("detectExternalNotificationPlugin", () => {
|
|
test("should return detected=false when no plugins configured", () => {
|
|
// given - empty directory
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
// then
|
|
expect(result.detected).toBe(false)
|
|
expect(result.pluginName).toBeNull()
|
|
})
|
|
|
|
test("should return detected=false when only oh-my-opencode is configured", () => {
|
|
// given - opencode.json with only oh-my-opencode
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["oh-my-opencode"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(false)
|
|
expect(result.pluginName).toBeNull()
|
|
expect(result.allPlugins).toContain("oh-my-opencode")
|
|
})
|
|
|
|
test("should detect opencode-notifier plugin", () => {
|
|
// given - opencode.json with opencode-notifier
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["oh-my-opencode", "opencode-notifier"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-notifier")
|
|
})
|
|
|
|
test("should detect opencode-notifier with version suffix", () => {
|
|
// given - opencode.json with versioned opencode-notifier
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["oh-my-opencode", "opencode-notifier@1.2.3"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-notifier")
|
|
})
|
|
|
|
test("should detect @mohak34/opencode-notifier", () => {
|
|
// given - opencode.json with scoped package name
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["oh-my-opencode", "@mohak34/opencode-notifier"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then - returns the matched known plugin pattern, not the full entry
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toContain("opencode-notifier")
|
|
})
|
|
|
|
test("should safely handle tuple-format plugin entries without crashing (fixes #3122)", () => {
|
|
// given - opencode.json with array/tuple plugin entries
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({
|
|
plugin: [
|
|
"oh-my-opencode",
|
|
["advanced-tuple-plugin", { debug: true }],
|
|
"opencode-notifier"
|
|
]
|
|
})
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then - should detect opencode-notifier without crashing on the tuple entry
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-notifier")
|
|
expect(result.allPlugins).toContain("oh-my-opencode")
|
|
expect(result.allPlugins).toContain("advanced-tuple-plugin")
|
|
expect(result.allPlugins).not.toContain(["advanced-tuple-plugin", { debug: true }])
|
|
})
|
|
|
|
test("should handle JSONC format with comments", () => {
|
|
// given - opencode.jsonc with comments
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.jsonc"),
|
|
`{
|
|
// This is a comment
|
|
"plugin": [
|
|
"oh-my-opencode",
|
|
"opencode-notifier" // Another comment
|
|
]
|
|
}`
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-notifier")
|
|
})
|
|
})
|
|
|
|
describe("false positive prevention", () => {
|
|
test("should NOT match my-opencode-notifier-fork (suffix variation)", () => {
|
|
// given - plugin with similar name but different suffix
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["my-opencode-notifier-fork"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(false)
|
|
expect(result.pluginName).toBeNull()
|
|
})
|
|
|
|
test("should NOT match some-other-plugin/opencode-notifier-like (path with similar name)", () => {
|
|
// given - plugin path containing similar substring
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["some-other-plugin/opencode-notifier-like"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(false)
|
|
expect(result.pluginName).toBeNull()
|
|
})
|
|
|
|
test("should NOT match opencode-notifier-extended (prefix match but different package)", () => {
|
|
// given - plugin with prefix match but extended name
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["opencode-notifier-extended"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(false)
|
|
expect(result.pluginName).toBeNull()
|
|
})
|
|
|
|
test("should match opencode-notifier exactly", () => {
|
|
// given - exact match
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["opencode-notifier"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-notifier")
|
|
})
|
|
|
|
test("should match opencode-notifier@1.2.3 (version suffix)", () => {
|
|
// given - version suffix
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["opencode-notifier@1.2.3"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-notifier")
|
|
})
|
|
|
|
test("should match @mohak34/opencode-notifier (scoped package)", () => {
|
|
// given - scoped package
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["@mohak34/opencode-notifier"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toContain("opencode-notifier")
|
|
})
|
|
|
|
test("should match npm:opencode-notifier (npm prefix)", () => {
|
|
// given - npm prefix
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["npm:opencode-notifier"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-notifier")
|
|
})
|
|
|
|
test("should match npm:opencode-notifier@2.0.0 (npm prefix with version)", () => {
|
|
// given - npm prefix with version
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["npm:opencode-notifier@2.0.0"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-notifier")
|
|
})
|
|
|
|
test("should match file:///path/to/opencode-notifier (file path)", () => {
|
|
// given - file path
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["file:///home/user/plugins/opencode-notifier"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalNotificationPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-notifier")
|
|
})
|
|
})
|
|
|
|
describe("getNotificationConflictWarning", () => {
|
|
test("should generate warning message with plugin name", () => {
|
|
// when
|
|
const warning = getNotificationConflictWarning("opencode-notifier")
|
|
|
|
// then
|
|
expect(warning).toContain("opencode-notifier")
|
|
expect(warning).toContain("session.idle")
|
|
expect(warning).toContain("auto-disabled")
|
|
expect(warning).toContain("force_enable")
|
|
})
|
|
})
|
|
|
|
describe("detectExternalSkillPlugin", () => {
|
|
test("should return detected=false when no plugins configured", () => {
|
|
// given - empty directory
|
|
// when
|
|
const result = detectExternalSkillPlugin(tempDir)
|
|
// then
|
|
expect(result.detected).toBe(false)
|
|
expect(result.pluginName).toBeNull()
|
|
})
|
|
|
|
test("should return detected=false when only oh-my-opencode is configured", () => {
|
|
// given - opencode.json with only oh-my-opencode
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["oh-my-opencode"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalSkillPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(false)
|
|
expect(result.pluginName).toBeNull()
|
|
expect(result.allPlugins).toContain("oh-my-opencode")
|
|
})
|
|
|
|
test("should detect opencode-skills plugin", () => {
|
|
// given - opencode.json with opencode-skills
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["oh-my-opencode", "opencode-skills"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalSkillPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-skills")
|
|
})
|
|
|
|
test("should detect opencode-skills with version suffix", () => {
|
|
// given - opencode.json with versioned opencode-skills
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["oh-my-opencode", "opencode-skills@1.2.3"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalSkillPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-skills")
|
|
})
|
|
|
|
test("should detect @opencode/skills scoped package", () => {
|
|
// given - opencode.json with scoped package name
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["oh-my-opencode", "@opencode/skills"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalSkillPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("@opencode/skills")
|
|
})
|
|
|
|
test("should detect npm:opencode-skills", () => {
|
|
// given - npm prefix
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["npm:opencode-skills"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalSkillPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-skills")
|
|
})
|
|
|
|
test("should detect file:///path/to/opencode-skills", () => {
|
|
// given - file path
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["file:///home/user/plugins/opencode-skills"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalSkillPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-skills")
|
|
})
|
|
|
|
test("should detect user-level opencode-skills when project config exists without plugins", async () => {
|
|
// given
|
|
const projectConfigDir = path.join(tempDir, ".opencode")
|
|
const userConfigDir = path.join(tempHomeDir, ".config", "opencode")
|
|
fs.mkdirSync(projectConfigDir, { recursive: true })
|
|
fs.mkdirSync(userConfigDir, { recursive: true })
|
|
fs.writeFileSync(path.join(projectConfigDir, "opencode.json"), JSON.stringify({}))
|
|
fs.writeFileSync(path.join(userConfigDir, "opencode.json"), JSON.stringify({ plugin: ["opencode-skills"] }))
|
|
|
|
const nodeOs = await import("node:os")
|
|
mock.module("node:os", () => ({
|
|
...nodeOs,
|
|
homedir: () => tempHomeDir,
|
|
}))
|
|
const { detectExternalSkillPlugin: detectExternalSkillPluginFresh } = await importFreshExternalPluginDetectorModule()
|
|
|
|
// when
|
|
const result = detectExternalSkillPluginFresh(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(true)
|
|
expect(result.pluginName).toBe("opencode-skills")
|
|
expect(result.allPlugins).toEqual(["opencode-skills"])
|
|
})
|
|
|
|
test("should NOT match opencode-skills-extra (suffix variation)", () => {
|
|
// given - plugin with similar name but different suffix
|
|
const opencodeDir = path.join(tempDir, ".opencode")
|
|
fs.mkdirSync(opencodeDir, { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(opencodeDir, "opencode.json"),
|
|
JSON.stringify({ plugin: ["opencode-skills-extra"] })
|
|
)
|
|
|
|
// when
|
|
const result = detectExternalSkillPlugin(tempDir)
|
|
|
|
// then
|
|
expect(result.detected).toBe(false)
|
|
expect(result.pluginName).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe("getSkillPluginConflictWarning", () => {
|
|
test("should generate warning message with plugin name", () => {
|
|
// when
|
|
const warning = getSkillPluginConflictWarning("opencode-skills")
|
|
|
|
// then
|
|
expect(warning).toContain("opencode-skills")
|
|
expect(warning).toContain("Duplicate tool names detected")
|
|
expect(warning).toContain("claude_code")
|
|
expect(warning).toContain("skills")
|
|
})
|
|
})
|
|
})
|