Merge branch 'fix/ub7-regression' into dev

This commit is contained in:
YeonGyu-Kim
2026-03-31 17:08:45 -07:00
7 changed files with 187 additions and 54 deletions
+24 -1
View File
@@ -1,18 +1,41 @@
declare module "bun:test" {
interface MockMetadata<TArgs extends unknown[]> {
calls: TArgs[]
}
interface MockFunction<TArgs extends unknown[] = unknown[], TReturn = unknown> {
(...args: TArgs): TReturn
mock: MockMetadata<TArgs>
mockReset(): void
mockReturnValue(value: TReturn): void
mockResolvedValue(value: Awaited<TReturn>): void
}
export function describe(name: string, fn: () => void): void
export function it(name: string, fn: () => void | Promise<void>): void
export function beforeEach(fn: () => void | Promise<void>): void
export function afterEach(fn: () => void | Promise<void>): void
export function beforeAll(fn: () => void | Promise<void>): void
export function afterAll(fn: () => void | Promise<void>): void
export function mock<T extends (...args: never[]) => unknown>(fn: T): T
export function mock<TArgs extends unknown[], TReturn>(
fn: (...args: TArgs) => TReturn,
): MockFunction<TArgs, TReturn>
export namespace mock {
function module(modulePath: string, factory: () => Record<string, unknown>): void
function restore(): void
}
interface Matchers {
toBe(expected: unknown): void
toBeNull(): void
toEqual(expected: unknown): void
toContain(expected: unknown): void
toMatch(expected: RegExp | string): void
toHaveLength(expected: number): void
toHaveBeenCalled(): void
toHaveBeenCalledTimes(expected: number): void
toHaveBeenCalledWith(...expected: unknown[]): void
toBeGreaterThan(expected: number): void
toThrow(expected?: RegExp | string): void
toStartWith(expected: string): void
@@ -1,3 +1,5 @@
/// <reference path="../../../bun-test.d.ts" />
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
@@ -118,6 +120,37 @@ describe("autoMigrateLegacyPluginEntry", () => {
})
})
describe("#given opencode.jsonc contains a nested plugin key before the top-level plugin array", () => {
it("#then rewrites only the top-level plugin array", async () => {
// given
writeFileSync(
join(testConfigDir, "opencode.jsonc"),
`{
"nested": {
"plugin": ["oh-my-opencode"]
},
"plugin": ["oh-my-opencode@latest"]
}
`,
)
const { autoMigrateLegacyPluginEntry } = await importFreshAutoMigrateModule()
// when
const result = autoMigrateLegacyPluginEntry(testConfigDir)
// then
expect(result.migrated).toBe(true)
const content = readFileSync(join(testConfigDir, "opencode.jsonc"), "utf-8")
expect(content).toContain(`"nested": {
"plugin": ["oh-my-opencode"]
}`)
expect(content).toContain(`"plugin": [
"oh-my-openagent@latest"
]`)
})
})
describe("#given only canonical entry exists", () => {
it("#then returns migrated false and leaves file untouched", async () => {
// given
+4 -23
View File
@@ -1,7 +1,8 @@
import { existsSync, readFileSync, writeFileSync } from "node:fs"
import { existsSync, readFileSync } from "node:fs"
import { join } from "node:path"
import { parseJsoncSafe } from "../../shared/jsonc-parser"
import { migrateLegacyPluginEntry } from "../../shared/migrate-legacy-plugin-entry"
import { getOpenCodeConfigPaths } from "../../shared/opencode-config-dir"
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "../../shared/plugin-identity"
@@ -20,10 +21,6 @@ function isLegacyEntry(entry: string): boolean {
return entry === LEGACY_PLUGIN_NAME || entry.startsWith(`${LEGACY_PLUGIN_NAME}@`)
}
function isCanonicalEntry(entry: string): boolean {
return entry === PLUGIN_NAME || entry.startsWith(`${PLUGIN_NAME}@`)
}
function toLegacyCanonical(entry: string): string {
if (entry === LEGACY_PLUGIN_NAME) return PLUGIN_NAME
if (entry.startsWith(`${LEGACY_PLUGIN_NAME}@`)) {
@@ -60,29 +57,13 @@ export function autoMigrateLegacyPluginEntry(overrideConfigDir?: string): Migrat
const legacyEntries = plugins.filter(isLegacyEntry)
if (legacyEntries.length === 0) return { migrated: false, from: null, to: null, configPath }
const hasCanonical = plugins.some(isCanonicalEntry)
const from = legacyEntries[0]
const to = toLegacyCanonical(from)
const normalized = hasCanonical
? plugins.filter((p) => !isLegacyEntry(p))
: plugins.map((p) => (isLegacyEntry(p) ? toLegacyCanonical(p) : p))
const isJsonc = configPath.endsWith(".jsonc")
if (isJsonc) {
const pluginArrayRegex = /((?:"plugin"|plugin)\s*:\s*)\[([\s\S]*?)\]/
const match = content.match(pluginArrayRegex)
if (match) {
const formattedPlugins = normalized.map((p) => `"${p}"`).join(",\n ")
const newContent = content.replace(pluginArrayRegex, `$1[\n ${formattedPlugins}\n ]`)
writeFileSync(configPath, newContent)
return { migrated: true, from, to, configPath }
}
if (!migrateLegacyPluginEntry(configPath)) {
return { migrated: false, from: null, to: null, configPath }
}
const parsed = JSON.parse(content) as Record<string, unknown>
parsed.plugin = normalized
writeFileSync(configPath, JSON.stringify(parsed, null, 2) + "\n")
return { migrated: true, from, to, configPath }
} catch {
return { migrated: false, from: null, to: null, configPath }
+33 -1
View File
@@ -1,10 +1,15 @@
/// <reference path="../../../bun-test.d.ts" />
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test"
import type { LegacyPluginCheckResult } from "../../shared/legacy-plugin-warning"
import type { MigrationResult } from "./auto-migrate"
const mockCheckForLegacyPluginEntry = mock(() => ({
const mockCheckForLegacyPluginEntry = mock((): LegacyPluginCheckResult => ({
hasLegacyEntry: false,
hasCanonicalEntry: false,
legacyEntries: [] as string[],
configPath: null,
}))
const mockAutoMigrate = mock((): MigrationResult => ({
@@ -67,6 +72,7 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: false,
hasCanonicalEntry: true,
legacyEntries: [],
configPath: null,
})
mockAutoMigrate.mockReturnValue({ migrated: false, from: null, to: null, configPath: null })
mockShowToast.mockResolvedValue(undefined)
@@ -93,6 +99,7 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
mockAutoMigrate.mockReturnValue({
migrated: true,
@@ -120,6 +127,7 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
mockAutoMigrate.mockReturnValue({
migrated: false,
@@ -147,6 +155,7 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
mockAutoMigrate.mockReturnValue({
migrated: true,
@@ -173,6 +182,7 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
const { createLegacyPluginToastHook } = await importFreshModule()
const hook = createLegacyPluginToastHook(createMockCtx())
@@ -192,6 +202,7 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
const { createLegacyPluginToastHook } = await importFreshModule()
const hook = createLegacyPluginToastHook(createMockCtx())
@@ -203,4 +214,25 @@ describe("createLegacyPluginToastHook", () => {
expect(mockCheckForLegacyPluginEntry).not.toHaveBeenCalled()
})
})
describe("#given a project directory is available", () => {
it("#then passes the project directory into legacy config detection", async () => {
// given
mockCheckForLegacyPluginEntry.mockReturnValue({
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/test/.opencode/opencode.json",
})
const { createLegacyPluginToastHook } = await importFreshModule()
const hook = createLegacyPluginToastHook(createMockCtx())
// when
await hook.event(createEvent("session.created"))
// then
expect(mockCheckForLegacyPluginEntry).toHaveBeenCalledWith(undefined, "/tmp/test")
expect(mockAutoMigrate).toHaveBeenCalledWith("/tmp/test/.opencode")
})
})
})
+4 -2
View File
@@ -1,3 +1,5 @@
import { dirname } from "node:path"
import type { PluginInput } from "@opencode-ai/plugin"
import { checkForLegacyPluginEntry } from "../../shared/legacy-plugin-warning"
@@ -17,10 +19,10 @@ export function createLegacyPluginToastHook(ctx: PluginInput) {
fired = true
const result = checkForLegacyPluginEntry()
const result = checkForLegacyPluginEntry(undefined, ctx.directory)
if (!result.hasLegacyEntry) return
const migration = autoMigrateLegacyPluginEntry()
const migration = autoMigrateLegacyPluginEntry(result.configPath ? dirname(result.configPath) : undefined)
if (migration.migrated) {
log("[legacy-plugin-toast] Auto-migrated opencode.json plugin entry", {
+24
View File
@@ -1,3 +1,5 @@
/// <reference path="../../bun-test.d.ts" />
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
@@ -80,4 +82,26 @@ describe("checkForLegacyPluginEntry", () => {
expect(result.legacyEntries).toEqual([])
expect(result.configPath).toBeNull()
})
describe("#given a project-local .opencode config contains a legacy plugin entry", () => {
it("#then detects the project-local config path", () => {
// given
const projectDir = join(testConfigDir, "project")
const projectConfigDir = join(projectDir, ".opencode")
mkdirSync(projectConfigDir, { recursive: true })
writeFileSync(
join(projectConfigDir, "opencode.json"),
JSON.stringify({ plugin: ["oh-my-opencode"] }, null, 2),
)
// when
const result = checkForLegacyPluginEntry(undefined, projectDir)
// then
expect(result.hasLegacyEntry).toBe(true)
expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual(["oh-my-opencode"])
expect(result.configPath).toBe(join(projectConfigDir, "opencode.json"))
})
})
})
+65 -27
View File
@@ -16,20 +16,36 @@ export interface LegacyPluginCheckResult {
configPath: string | null
}
function getOpenCodeConfigPath(overrideConfigDir?: string): string | null {
function getConfigPathFromDirectory(configDir: string): string | null {
const jsonPath = join(configDir, "opencode.json")
const jsoncPath = join(configDir, "opencode.jsonc")
if (existsSync(jsoncPath)) return jsoncPath
if (existsSync(jsonPath)) return jsonPath
return null
}
function getOpenCodeConfigPathsToCheck(overrideConfigDir?: string, projectDir?: string): string[] {
if (overrideConfigDir) {
const jsonPath = join(overrideConfigDir, "opencode.json")
const jsoncPath = join(overrideConfigDir, "opencode.jsonc")
if (existsSync(jsoncPath)) return jsoncPath
if (existsSync(jsonPath)) return jsonPath
return null
const overridePath = getConfigPathFromDirectory(overrideConfigDir)
return overridePath ? [overridePath] : []
}
const configPaths: string[] = []
if (projectDir) {
const projectConfigPath = getConfigPathFromDirectory(join(projectDir, ".opencode"))
if (projectConfigPath) {
configPaths.push(projectConfigPath)
}
}
const { configJsonc, configJson } = getOpenCodeConfigPaths({ binary: "opencode", version: null })
if (existsSync(configJsonc)) return configJsonc
if (existsSync(configJson)) return configJson
return null
if (existsSync(configJsonc)) configPaths.push(configJsonc)
else if (existsSync(configJson)) configPaths.push(configJson)
return configPaths
}
function isLegacyPluginEntry(entry: string): boolean {
@@ -40,29 +56,51 @@ function isCanonicalPluginEntry(entry: string): boolean {
return entry === PLUGIN_NAME || entry.startsWith(`${PLUGIN_NAME}@`)
}
export function checkForLegacyPluginEntry(overrideConfigDir?: string): LegacyPluginCheckResult {
const configPath = getOpenCodeConfigPath(overrideConfigDir)
if (!configPath) {
export function checkForLegacyPluginEntry(
overrideConfigDir?: string,
projectDir?: string,
): LegacyPluginCheckResult {
const configPaths = getOpenCodeConfigPathsToCheck(overrideConfigDir, projectDir)
if (configPaths.length === 0) {
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [], configPath: null }
}
try {
const content = readFileSync(configPath, "utf-8")
const parseResult = parseJsoncSafe<OpenCodeConfig>(content)
if (!parseResult.data) {
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [], configPath }
}
let hasCanonicalEntry = false
let detectedConfigPath: string | null = null
const legacyEntries = (parseResult.data.plugin ?? []).filter(isLegacyPluginEntry)
const hasCanonicalEntry = (parseResult.data.plugin ?? []).some(isCanonicalPluginEntry)
for (const configPath of configPaths) {
detectedConfigPath ??= configPath
return {
hasLegacyEntry: legacyEntries.length > 0,
hasCanonicalEntry,
legacyEntries,
configPath,
try {
const content = readFileSync(configPath, "utf-8")
const parseResult = parseJsoncSafe<OpenCodeConfig>(content)
if (!parseResult.data) {
continue
}
const pluginEntries = parseResult.data.plugin ?? []
const legacyEntries = pluginEntries.filter(isLegacyPluginEntry)
const fileHasCanonicalEntry = pluginEntries.some(isCanonicalPluginEntry)
if (legacyEntries.length > 0) {
return {
hasLegacyEntry: true,
hasCanonicalEntry: fileHasCanonicalEntry,
legacyEntries,
configPath,
}
}
hasCanonicalEntry ||= fileHasCanonicalEntry
} catch {
continue
}
} catch {
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [], configPath: null }
}
return {
hasLegacyEntry: false,
hasCanonicalEntry,
legacyEntries: [],
configPath: detectedConfigPath,
}
}