fix(auth): add graceful fallback for server auth injection
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
/// <reference types="bun-types" />
|
||||
|
||||
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
|
||||
import { getServerBasicAuthHeader, injectServerAuthIntoClient } from "./opencode-server-auth"
|
||||
|
||||
describe("opencode-server-auth", () => {
|
||||
@@ -68,22 +71,18 @@ describe("opencode-server-auth", () => {
|
||||
})
|
||||
})
|
||||
|
||||
test("#given server password #when client has no _client #then throws error", () => {
|
||||
test("#given server password #when client has no _client #then does not throw", () => {
|
||||
process.env.OPENCODE_SERVER_PASSWORD = "secret"
|
||||
const client = {}
|
||||
|
||||
expect(() => injectServerAuthIntoClient(client)).toThrow(
|
||||
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client structure is incompatible"
|
||||
)
|
||||
expect(() => injectServerAuthIntoClient(client)).not.toThrow()
|
||||
})
|
||||
|
||||
test("#given server password #when client._client has no setConfig #then throws error", () => {
|
||||
test("#given server password #when client._client has no setConfig #then does not throw", () => {
|
||||
process.env.OPENCODE_SERVER_PASSWORD = "secret"
|
||||
const client = { _client: {} }
|
||||
|
||||
expect(() => injectServerAuthIntoClient(client)).toThrow(
|
||||
"[opencode-server-auth] OPENCODE_SERVER_PASSWORD is set but SDK client._client.setConfig is not a function"
|
||||
)
|
||||
expect(() => injectServerAuthIntoClient(client)).not.toThrow()
|
||||
})
|
||||
|
||||
test("#given no server password #when client is invalid #then does not throw", () => {
|
||||
|
||||
@@ -33,7 +33,7 @@ export function injectServerAuthIntoClient(client: unknown): void {
|
||||
return
|
||||
}
|
||||
|
||||
// Runtime type guard for SDK client structure
|
||||
try {
|
||||
if (
|
||||
typeof client !== "object" ||
|
||||
client === null ||
|
||||
@@ -62,4 +62,8 @@ export function injectServerAuthIntoClient(client: unknown): void {
|
||||
Authorization: auth,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
console.warn(`[opencode-server-auth] Failed to inject server auth: ${message}`)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user