59 lines
2.1 KiB
JavaScript
59 lines
2.1 KiB
JavaScript
import assert from "node:assert/strict"
|
|
import test from "node:test"
|
|
import { addPlugin, removePlugin, replacePlugin } from "../dist/installer/config.js"
|
|
|
|
const pluginName = "@sargatxet/opencode-toolkit"
|
|
|
|
test("adds a package plugin while preserving unrelated JSONC and comments", () => {
|
|
const original = `{
|
|
// Keep the user's settings intact.
|
|
"model": "provider/model",
|
|
"plugins": ["existing-plugin"]
|
|
}`
|
|
|
|
const result = addPlugin(original, pluginName)
|
|
const parsed = JSON.parse(result.content.replace("// Keep the user's settings intact.\n ", ""))
|
|
|
|
assert.equal(result.added, true)
|
|
assert.equal(parsed.model, "provider/model")
|
|
assert.deepEqual(parsed.plugins, ["existing-plugin", pluginName])
|
|
assert.match(result.content, /Keep the user's settings intact/)
|
|
})
|
|
|
|
test("does not duplicate an already configured package plugin", () => {
|
|
const original = JSON.stringify({ plugins: [pluginName] }, null, 2)
|
|
const result = addPlugin(original, pluginName)
|
|
|
|
assert.equal(result.added, false)
|
|
assert.equal(result.content, original)
|
|
})
|
|
|
|
test("removes only the requested package plugin", () => {
|
|
const original = JSON.stringify({ plugins: ["other-plugin", pluginName] }, null, 2)
|
|
const result = removePlugin(original, pluginName)
|
|
|
|
assert.equal(result.removed, true)
|
|
assert.deepEqual(JSON.parse(result.content).plugins, ["other-plugin"])
|
|
})
|
|
|
|
test("replaces a managed plugin entry while preserving JSONC comments", () => {
|
|
const original = `{
|
|
"plugins": [
|
|
// Added by the previous installer version.
|
|
"${pluginName}"
|
|
]
|
|
}`
|
|
const replacement = "../node_modules/@sargatxet/opencode-toolkit"
|
|
|
|
const result = replacePlugin(original, pluginName, replacement)
|
|
|
|
assert.equal(result.replaced, true)
|
|
assert.deepEqual(JSON.parse(result.content.replace(" // Added by the previous installer version.\n", "")).plugins, [replacement])
|
|
assert.match(result.content, /Added by the previous installer version/)
|
|
})
|
|
|
|
test("rejects malformed or incompatible plugin configuration", () => {
|
|
assert.throws(() => addPlugin('{ "plugins": "not-an-array" }', pluginName), /plugins.*array/i)
|
|
assert.throws(() => addPlugin('{ broken', pluginName), /invalid JSONC/i)
|
|
})
|