httpapi-provider.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import { describe, expect } from "bun:test"
  2. import { FSUtil } from "@opencode-ai/core/fs-util"
  3. import { Effect, Layer } from "effect"
  4. import path from "path"
  5. import * as Log from "@opencode-ai/core/util/log"
  6. import { resetDatabase } from "../fixture/db"
  7. import { TestInstance } from "../fixture/fixture"
  8. import { markPluginDependenciesReady } from "../fixture/plugin"
  9. import { testEffect } from "../lib/effect"
  10. import { httpApiLayer, request } from "./httpapi-layer"
  11. void Log.init({ print: false })
  12. const testStateLayer = Layer.effectDiscard(
  13. Effect.acquireRelease(
  14. Effect.promise(() => resetDatabase()),
  15. () => Effect.promise(() => resetDatabase()),
  16. ),
  17. )
  18. const it = testEffect(Layer.mergeAll(testStateLayer, FSUtil.defaultLayer, httpApiLayer))
  19. const projectOptions = { config: { formatter: false, lsp: false } }
  20. const providerID = "test-oauth-parity"
  21. const oauthURL = "https://example.com/oauth"
  22. const oauthInstructions = "Finish OAuth"
  23. function providerListHasFetch(list: unknown) {
  24. if (!Array.isArray(list)) return false
  25. return list.some((item: unknown) => {
  26. if (typeof item !== "object" || item === null || !("id" in item) || !("options" in item)) return false
  27. if (item.id !== "google") return false
  28. if (typeof item.options !== "object" || item.options === null) return false
  29. return "fetch" in item.options
  30. })
  31. }
  32. function hasProviderWithFetch(input: unknown, key: "all" | "providers") {
  33. if (typeof input !== "object" || input === null) return false
  34. if (key === "all") return "all" in input && providerListHasFetch(input.all)
  35. return "providers" in input && providerListHasFetch(input.providers)
  36. }
  37. function isRecord(value: unknown): value is Record<string, unknown> {
  38. return typeof value === "object" && value !== null && !Array.isArray(value)
  39. }
  40. function providerList(input: unknown, key: "all" | "providers") {
  41. if (!isRecord(input)) return []
  42. if (!Array.isArray(input[key])) return []
  43. return input[key]
  44. }
  45. function providerByID(input: unknown, key: "all" | "providers", id: string) {
  46. return providerList(input, key).find((provider) => isRecord(provider) && provider.id === id)
  47. }
  48. function hasNonZeroModelCost(input: unknown, key: "all" | "providers", id: string) {
  49. const provider = providerByID(input, key, id)
  50. if (!isRecord(provider) || !isRecord(provider.models)) return false
  51. return Object.values(provider.models).some((model) => {
  52. if (!isRecord(model) || !isRecord(model.cost) || !isRecord(model.cost.cache)) return false
  53. return [model.cost.input, model.cost.output, model.cost.cache.read, model.cost.cache.write].some(
  54. (cost) => typeof cost === "number" && cost > 0,
  55. )
  56. })
  57. }
  58. function hasProviderMutationMarker(input: unknown, key: "all" | "providers", id: string) {
  59. const provider = providerByID(input, key, id)
  60. if (!isRecord(provider)) return false
  61. if (provider.name === "mutated-provider") return true
  62. return isRecord(provider.options) && provider.options.mutatedByPlugin === true
  63. }
  64. function requestAuthorize(input: {
  65. providerID: string
  66. method: number
  67. headers: HeadersInit
  68. inputs?: Record<string, string>
  69. }) {
  70. return Effect.gen(function* () {
  71. const response = yield* request(`/provider/${input.providerID}/oauth/authorize`, {
  72. method: "POST",
  73. headers: input.headers,
  74. body: JSON.stringify({ method: input.method, ...(input.inputs ? { inputs: input.inputs } : {}) }),
  75. })
  76. return {
  77. status: response.status,
  78. body: yield* response.text,
  79. }
  80. })
  81. }
  82. function requestCallback(input: { providerID: string; method: number; headers: HeadersInit; code?: string }) {
  83. return Effect.gen(function* () {
  84. const response = yield* request(`/provider/${input.providerID}/oauth/callback`, {
  85. method: "POST",
  86. headers: input.headers,
  87. body: JSON.stringify({ method: input.method, ...(input.code ? { code: input.code } : {}) }),
  88. })
  89. return {
  90. status: response.status,
  91. body: yield* response.text,
  92. }
  93. })
  94. }
  95. function writeProviderAuthPlugin(dir: string) {
  96. return Effect.gen(function* () {
  97. const fs = yield* FSUtil.Service
  98. yield* Effect.promise(() => markPluginDependenciesReady(path.join(dir, ".opencode")))
  99. yield* fs.writeWithDirs(
  100. path.join(dir, ".opencode", "plugin", "provider-oauth-parity.ts"),
  101. [
  102. "export default {",
  103. ' id: "test.provider-oauth-parity",',
  104. " server: async () => ({",
  105. " auth: {",
  106. ` provider: "${providerID}",`,
  107. " methods: [",
  108. ' { type: "api", label: "API key" },',
  109. " {",
  110. ' type: "oauth",',
  111. ' label: "OAuth",',
  112. " authorize: async () => ({",
  113. ` url: "${oauthURL}",`,
  114. ' method: "code",',
  115. ` instructions: "${oauthInstructions}",`,
  116. " callback: async () => ({ type: 'success', key: 'token' }),",
  117. " }),",
  118. " },",
  119. " ],",
  120. " },",
  121. " }),",
  122. "}",
  123. "",
  124. ].join("\n"),
  125. )
  126. })
  127. }
  128. function writeProviderAuthValidationPlugin(dir: string) {
  129. return Effect.gen(function* () {
  130. const fs = yield* FSUtil.Service
  131. yield* Effect.promise(() => markPluginDependenciesReady(path.join(dir, ".opencode")))
  132. yield* fs.writeWithDirs(
  133. path.join(dir, ".opencode", "plugin", "provider-oauth-validation.ts"),
  134. [
  135. "export default {",
  136. ' id: "test.provider-oauth-validation",',
  137. " server: async () => ({",
  138. " auth: {",
  139. ' provider: "test-oauth-validation",',
  140. " methods: [",
  141. " {",
  142. ' type: "oauth",',
  143. ' label: "OAuth",',
  144. " prompts: [",
  145. " {",
  146. ' type: "text",',
  147. ' key: "token",',
  148. ' message: "Token",',
  149. " validate: (value) => value === 'ok' ? undefined : 'Token must be ok',",
  150. " },",
  151. " ],",
  152. " authorize: async () => ({",
  153. ` url: "${oauthURL}",`,
  154. ' method: "code",',
  155. ` instructions: "${oauthInstructions}",`,
  156. " callback: async () => ({ type: 'success', key: 'token' }),",
  157. " }),",
  158. " },",
  159. " ],",
  160. " },",
  161. " }),",
  162. "}",
  163. "",
  164. ].join("\n"),
  165. )
  166. })
  167. }
  168. function writeFunctionOptionsPlugin(dir: string) {
  169. return Effect.gen(function* () {
  170. const fs = yield* FSUtil.Service
  171. yield* Effect.promise(() => markPluginDependenciesReady(path.join(dir, ".opencode")))
  172. yield* fs.writeWithDirs(
  173. path.join(dir, ".opencode", "plugin", "provider-function-options.ts"),
  174. [
  175. "export default {",
  176. ' id: "test.provider-function-options",',
  177. " server: async () => ({",
  178. " auth: {",
  179. ' provider: "google",',
  180. " loader: async (_getAuth, provider) => {",
  181. " for (const model of Object.values(provider.models ?? {})) {",
  182. " model.cost = { input: 0, output: 0 }",
  183. " }",
  184. " return {",
  185. ' apiKey: "",',
  186. " fetch: async (input, init) => fetch(input, init),",
  187. " }",
  188. " },",
  189. " methods: [{ type: 'api', label: 'API key' }],",
  190. " },",
  191. " }),",
  192. "}",
  193. "",
  194. ].join("\n"),
  195. )
  196. })
  197. }
  198. function writeProviderModelsMutationPlugin(dir: string) {
  199. return Effect.gen(function* () {
  200. const fs = yield* FSUtil.Service
  201. yield* Effect.promise(() => markPluginDependenciesReady(path.join(dir, ".opencode")))
  202. yield* fs.writeWithDirs(
  203. path.join(dir, ".opencode", "plugin", "provider-models-mutation.ts"),
  204. [
  205. "export default {",
  206. ' id: "test.provider-models-mutation",',
  207. " server: async () => ({",
  208. " provider: {",
  209. ' id: "google",',
  210. " models: async (provider) => {",
  211. " const models = Object.fromEntries(",
  212. " Object.entries(provider.models ?? {}).map(([id, model]) => [id, { ...model }]),",
  213. " )",
  214. ' provider.name = "mutated-provider"',
  215. " provider.options = { ...provider.options, mutatedByPlugin: true }",
  216. " for (const model of Object.values(provider.models ?? {})) {",
  217. " model.cost = { input: 0, output: 0 }",
  218. " }",
  219. " return models",
  220. " },",
  221. " },",
  222. " }),",
  223. "}",
  224. "",
  225. ].join("\n"),
  226. )
  227. })
  228. }
  229. function setEnvScoped(key: string, value: string) {
  230. return Effect.acquireRelease(
  231. Effect.sync(() => {
  232. const previous = process.env[key]
  233. process.env[key] = value
  234. return previous
  235. }),
  236. (previous) =>
  237. Effect.sync(() => {
  238. if (previous === undefined) delete process.env[key]
  239. else process.env[key] = previous
  240. }),
  241. )
  242. }
  243. describe("provider HttpApi", () => {
  244. it.instance.skip(
  245. "returns public v2 provider not found errors",
  246. Effect.gen(function* () {
  247. const directory = (yield* TestInstance).directory
  248. const response = yield* request("/api/provider/missing", {
  249. headers: { "x-opencode-directory": directory },
  250. })
  251. expect(response.status).toBe(404)
  252. expect(yield* response.json).toEqual({
  253. _tag: "ProviderNotFoundError",
  254. providerID: "missing",
  255. message: "Provider not found: missing",
  256. })
  257. }),
  258. projectOptions,
  259. )
  260. it.instance(
  261. "serves OAuth authorize response shapes",
  262. Effect.gen(function* () {
  263. const directory = (yield* TestInstance).directory
  264. const headers = { "x-opencode-directory": directory, "content-type": "application/json" }
  265. const api = yield* requestAuthorize({
  266. providerID,
  267. method: 0,
  268. headers,
  269. })
  270. // method 0 (api-key style) — authorize() resolves with no further
  271. // redirect; #26474 changed the wire format to JSON `null` so clients
  272. // can `.json()` parse uniformly instead of getting an empty body
  273. // that throws.
  274. expect(api).toEqual({ status: 200, body: "null" })
  275. const oauth = yield* requestAuthorize({
  276. providerID,
  277. method: 1,
  278. headers,
  279. })
  280. expect(JSON.parse(oauth.body)).toEqual({
  281. url: oauthURL,
  282. method: "code",
  283. instructions: oauthInstructions,
  284. })
  285. }),
  286. { ...projectOptions, init: writeProviderAuthPlugin },
  287. 30000,
  288. )
  289. it.instance(
  290. "returns declared provider auth validation errors",
  291. Effect.gen(function* () {
  292. const directory = (yield* TestInstance).directory
  293. const response = yield* requestAuthorize({
  294. providerID: "test-oauth-validation",
  295. method: 0,
  296. inputs: { token: "nope" },
  297. headers: { "x-opencode-directory": directory, "content-type": "application/json" },
  298. })
  299. expect(response.status).toBe(400)
  300. expect(JSON.parse(response.body)).toEqual({
  301. name: "ProviderAuthValidationFailed",
  302. data: { field: "token", message: "Token must be ok" },
  303. })
  304. }),
  305. { ...projectOptions, init: writeProviderAuthValidationPlugin },
  306. 30000,
  307. )
  308. it.instance(
  309. "returns declared provider auth callback errors",
  310. Effect.gen(function* () {
  311. const directory = (yield* TestInstance).directory
  312. const response = yield* requestCallback({
  313. providerID,
  314. method: 0,
  315. headers: { "x-opencode-directory": directory, "content-type": "application/json" },
  316. })
  317. expect(response.status).toBe(400)
  318. expect(JSON.parse(response.body)).toEqual({
  319. name: "ProviderAuthOauthMissing",
  320. data: { providerID },
  321. })
  322. }),
  323. projectOptions,
  324. 30000,
  325. )
  326. it.instance(
  327. "serves provider lists when auth loaders add runtime fetch options",
  328. Effect.gen(function* () {
  329. const directory = (yield* TestInstance).directory
  330. yield* setEnvScoped(
  331. "OPENCODE_AUTH_CONTENT",
  332. JSON.stringify({
  333. google: { type: "oauth", refresh: "dummy", access: "dummy", expires: 9999999999999 },
  334. }),
  335. )
  336. const headers = { "x-opencode-directory": directory }
  337. const providerResponse = yield* request("/provider", { headers })
  338. const configResponse = yield* request("/config/providers", { headers })
  339. expect(providerResponse.status).toBe(200)
  340. expect(configResponse.status).toBe(200)
  341. const providerBody = yield* providerResponse.json
  342. const configBody = yield* configResponse.json
  343. expect(hasProviderWithFetch(providerBody, "all")).toBe(false)
  344. expect(hasProviderWithFetch(configBody, "providers")).toBe(false)
  345. expect(hasNonZeroModelCost(providerBody, "all", "google")).toBe(true)
  346. expect(hasNonZeroModelCost(configBody, "providers", "google")).toBe(true)
  347. }),
  348. { ...projectOptions, init: writeFunctionOptionsPlugin },
  349. )
  350. it.instance(
  351. "keeps provider.models hook input mutations out of provider state",
  352. Effect.gen(function* () {
  353. const directory = (yield* TestInstance).directory
  354. const headers = { "x-opencode-directory": directory }
  355. const providerResponse = yield* request("/provider", { headers })
  356. const configResponse = yield* request("/config/providers", { headers })
  357. expect(providerResponse.status).toBe(200)
  358. expect(configResponse.status).toBe(200)
  359. const providerBody = yield* providerResponse.json
  360. const configBody = yield* configResponse.json
  361. expect(hasProviderMutationMarker(providerBody, "all", "google")).toBe(false)
  362. expect(hasProviderMutationMarker(configBody, "providers", "google")).toBe(false)
  363. expect(hasNonZeroModelCost(providerBody, "all", "google")).toBe(true)
  364. }),
  365. { ...projectOptions, init: writeProviderModelsMutationPlugin },
  366. )
  367. })