fix(ultrawork): lazy-load bun:sqlite to support Node/Electron runtime
Top-level `import { Database } from 'bun:sqlite'` caused Node/Electron's
ESM loader to reject the plugin at module-graph resolution time because
the `bun:` protocol is not in Node's allowed scheme list. This prevented
the OpenCode desktop app from loading the plugin at all.
Fix:
- Remove top-level static import of `bun:sqlite`
- Use a lazy importer (`_bunSqliteImporter`) that calls
`import('bun:sqlite').catch(() => null)` at runtime
- If the import returns null (non-Bun environment), log a warning and
return early — no DB override attempted, plugin loads normally
- Expose `__setBunSqliteImporterForTesting` / `__resetBunSqliteImporterForTesting`
test seams to verify the Node/Electron fallback path
Closes #3795
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test"
|
||||
import * as sharedModule from "../shared"
|
||||
import {
|
||||
scheduleDeferredModelOverride,
|
||||
__setBunSqliteImporterForTesting,
|
||||
__resetBunSqliteImporterForTesting,
|
||||
} from "./ultrawork-db-model-override"
|
||||
|
||||
function flushMicrotasks(depth: number): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
let remaining = depth
|
||||
function step() {
|
||||
if (remaining <= 0) {
|
||||
resolve()
|
||||
return
|
||||
}
|
||||
remaining--
|
||||
queueMicrotask(step)
|
||||
}
|
||||
queueMicrotask(step)
|
||||
})
|
||||
}
|
||||
|
||||
describe("scheduleDeferredModelOverride bun:sqlite unavailable", () => {
|
||||
let logCalls: Array<[string, Record<string, unknown>?]> = []
|
||||
|
||||
beforeEach(() => {
|
||||
// Simulate non-Bun runtime (Node/Electron): bun:sqlite import returns null
|
||||
__setBunSqliteImporterForTesting(async () => null)
|
||||
|
||||
spyOn(sharedModule, "log").mockImplementation((message: string, metadata?: Record<string, unknown>) => {
|
||||
logCalls.push([message, metadata])
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
__resetBunSqliteImporterForTesting()
|
||||
mock.restore()
|
||||
logCalls = []
|
||||
})
|
||||
|
||||
test("#given non-Bun runtime #when scheduleDeferredModelOverride is called #then it returns without throwing", async () => {
|
||||
//#given
|
||||
//#when
|
||||
expect(() => {
|
||||
scheduleDeferredModelOverride("msg_unavailable", {
|
||||
providerID: "anthropic",
|
||||
modelID: "claude-opus-4-7",
|
||||
})
|
||||
}).not.toThrow()
|
||||
|
||||
await flushMicrotasks(5)
|
||||
|
||||
//#then
|
||||
const logMessages = logCalls.map(([msg]) => msg)
|
||||
expect(logMessages).toContain(
|
||||
"[ultrawork-db-override] bun:sqlite unavailable (non-Bun runtime), skipping deferred override",
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user