connector.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import { describe, expect } from "bun:test"
  2. import { Duration, Effect, Exit, Layer, Scope } from "effect"
  3. import * as TestClock from "effect/testing/TestClock"
  4. import { Connector } from "@opencode-ai/core/connector"
  5. import { Credential } from "@opencode-ai/core/credential"
  6. import { EventV2 } from "@opencode-ai/core/event"
  7. import { it } from "./lib/effect"
  8. const layer = Connector.locationLayer.pipe(
  9. Layer.provide(EventV2.defaultLayer),
  10. Layer.provide(
  11. Layer.mock(Credential.Service)({
  12. create: () => Effect.die("unexpected credential creation"),
  13. }),
  14. ),
  15. )
  16. function connectionLayer(
  17. created: Array<{
  18. connectorID: Connector.ID
  19. methodID: Connector.MethodID
  20. label?: string
  21. value: Credential.Value
  22. }>,
  23. ) {
  24. return Connector.locationLayer.pipe(
  25. Layer.provide(EventV2.defaultLayer),
  26. Layer.provide(
  27. Layer.mock(Credential.Service)({
  28. create: (input) =>
  29. Effect.sync(() => {
  30. created.push(input)
  31. return new Credential.Info({ id: Credential.ID.create(), ...input, label: input.label ?? "default" })
  32. }),
  33. }),
  34. ),
  35. )
  36. }
  37. describe("Connector", () => {
  38. it.effect("registers connectors through the editor", () =>
  39. Effect.gen(function* () {
  40. const connectors = yield* Connector.Service
  41. const scope = yield* Scope.fork(yield* Scope.Scope)
  42. const openai = Connector.ID.make("openai")
  43. yield* connectors
  44. .update((editor) => editor.update(openai, (connector) => (connector.name = "OpenAI")))
  45. .pipe(Scope.provide(scope))
  46. expect(yield* connectors.get(openai)).toEqual(new Connector.Info({ id: openai, name: "OpenAI", methods: [] }))
  47. yield* Scope.close(scope, Exit.void)
  48. expect(yield* connectors.get(openai)).toBeUndefined()
  49. }).pipe(Effect.provide(layer)),
  50. )
  51. it.effect("reveals the previous registration when an override closes", () =>
  52. Effect.gen(function* () {
  53. const connectors = yield* Connector.Service
  54. const id = Connector.ID.make("openai")
  55. const first = yield* Scope.fork(yield* Scope.Scope)
  56. const second = yield* Scope.fork(yield* Scope.Scope)
  57. yield* connectors
  58. .update((editor) => editor.update(id, (connector) => (connector.name = "OpenAI")))
  59. .pipe(Scope.provide(first))
  60. yield* connectors
  61. .update((editor) => editor.update(id, (connector) => (connector.name = "OpenAI Override")))
  62. .pipe(Scope.provide(second))
  63. expect((yield* connectors.get(id))?.name).toBe("OpenAI Override")
  64. yield* Scope.close(second, Exit.void)
  65. expect((yield* connectors.get(id))?.name).toBe("OpenAI")
  66. expect((yield* connectors.list()).map((connector) => connector.id)).toEqual([id])
  67. }).pipe(Effect.provide(layer)),
  68. )
  69. it.effect("registers and overrides methods independently", () =>
  70. Effect.gen(function* () {
  71. const connectors = yield* Connector.Service
  72. const connectorID = Connector.ID.make("openai")
  73. const methodID = Connector.MethodID.make("chatgpt")
  74. const first = yield* Scope.fork(yield* Scope.Scope)
  75. const second = yield* Scope.fork(yield* Scope.Scope)
  76. const authorize = () =>
  77. Effect.succeed({
  78. mode: "auto" as const,
  79. url: "https://example.com/authorize",
  80. instructions: "Sign in",
  81. callback: Effect.never,
  82. })
  83. yield* connectors
  84. .update((editor) =>
  85. editor.method.update({
  86. connectorID,
  87. method: new Connector.OAuthMethod({ id: methodID, type: "oauth", label: "ChatGPT" }),
  88. authorize,
  89. }),
  90. )
  91. .pipe(Scope.provide(first))
  92. yield* connectors
  93. .update((editor) =>
  94. editor.method.update({
  95. connectorID,
  96. method: new Connector.OAuthMethod({ id: methodID, type: "oauth", label: "ChatGPT Override" }),
  97. authorize,
  98. }),
  99. )
  100. .pipe(Scope.provide(second))
  101. expect((yield* connectors.get(connectorID))?.name).toBe("openai")
  102. expect((yield* connectors.get(connectorID))?.methods[0]?.label).toBe("ChatGPT Override")
  103. yield* Scope.close(second, Exit.void)
  104. expect((yield* connectors.get(connectorID))?.methods[0]?.label).toBe("ChatGPT")
  105. expect((yield* connectors.get(connectorID))?.methods.map((method) => method.id)).toEqual([methodID])
  106. }).pipe(Effect.provide(layer)),
  107. )
  108. it.effect("connects with a key and stores the credential", () => {
  109. const created: Array<{
  110. connectorID: Connector.ID
  111. methodID: Connector.MethodID
  112. label?: string
  113. value: Credential.Value
  114. }> = []
  115. return Effect.gen(function* () {
  116. const connectors = yield* Connector.Service
  117. const connectorID = Connector.ID.make("openai")
  118. const methodID = Connector.MethodID.make("api-key")
  119. yield* connectors.update((editor) =>
  120. editor.method.update({
  121. connectorID,
  122. method: new Connector.KeyMethod({ id: methodID, type: "key", label: "API key" }),
  123. authorize: (key, inputs) =>
  124. Effect.succeed(
  125. new Credential.Key({ type: "key", key, metadata: { organization: inputs.organization ?? "" } }),
  126. ),
  127. }),
  128. )
  129. yield* connectors.connect.key({
  130. connectorID,
  131. methodID,
  132. key: "secret",
  133. inputs: { organization: "acme" },
  134. label: "Work",
  135. })
  136. expect(created).toEqual([
  137. {
  138. connectorID,
  139. methodID,
  140. label: "Work",
  141. value: new Credential.Key({ type: "key", key: "secret", metadata: { organization: "acme" } }),
  142. },
  143. ])
  144. }).pipe(Effect.provide(connectionLayer(created)))
  145. })
  146. it.effect("refreshes OAuth with the originating method", () => {
  147. const connectorID = Connector.ID.make("openai")
  148. const methodID = Connector.MethodID.make("chatgpt")
  149. const credentialID = Credential.ID.create()
  150. const current = new Credential.OAuth({
  151. type: "oauth",
  152. access: "old-access",
  153. refresh: "old-refresh",
  154. expires: 1,
  155. metadata: { accountID: "account" },
  156. })
  157. const updated: Array<{ id: Credential.ID; value: Credential.Value }> = []
  158. const refreshLayer = Connector.locationLayer.pipe(
  159. Layer.provide(EventV2.defaultLayer),
  160. Layer.provide(
  161. Layer.mock(Credential.Service)({
  162. get: () =>
  163. Effect.succeed(
  164. new Credential.Info({
  165. id: credentialID,
  166. connectorID,
  167. methodID,
  168. label: "Personal",
  169. value: current,
  170. }),
  171. ),
  172. update: (id, input) =>
  173. Effect.sync(() => {
  174. if (input.value) updated.push({ id, value: input.value })
  175. }),
  176. }),
  177. ),
  178. )
  179. return Effect.gen(function* () {
  180. const connectors = yield* Connector.Service
  181. yield* connectors.update((editor) =>
  182. editor.method.update({
  183. connectorID,
  184. method: new Connector.OAuthMethod({ id: methodID, type: "oauth", label: "ChatGPT" }),
  185. authorize: () => Effect.die("unexpected authorization"),
  186. refresh: (value) =>
  187. Effect.succeed(
  188. new Credential.OAuth({
  189. type: "oauth",
  190. access: "new-access",
  191. refresh: "new-refresh",
  192. expires: 2,
  193. metadata: value.metadata,
  194. }),
  195. ),
  196. }),
  197. )
  198. yield* connectors.refresh(credentialID)
  199. expect(updated).toEqual([
  200. {
  201. id: credentialID,
  202. value: new Credential.OAuth({
  203. type: "oauth",
  204. access: "new-access",
  205. refresh: "new-refresh",
  206. expires: 2,
  207. metadata: { accountID: "account" },
  208. }),
  209. },
  210. ])
  211. }).pipe(Effect.provide(refreshLayer))
  212. })
  213. it.effect("completes code OAuth once and stores the credential", () => {
  214. const created: Array<{
  215. connectorID: Connector.ID
  216. methodID: Connector.MethodID
  217. label?: string
  218. value: Credential.Value
  219. }> = []
  220. return Effect.gen(function* () {
  221. const connectors = yield* Connector.Service
  222. const connectorID = Connector.ID.make("openai")
  223. const methodID = Connector.MethodID.make("chatgpt")
  224. yield* connectors.update((editor) =>
  225. editor.method.update({
  226. connectorID,
  227. method: new Connector.OAuthMethod({ id: methodID, type: "oauth", label: "ChatGPT" }),
  228. authorize: () =>
  229. Effect.succeed({
  230. mode: "code" as const,
  231. url: "https://example.com/authorize",
  232. instructions: "Paste the code",
  233. callback: (code: string) =>
  234. Effect.succeed(
  235. new Credential.OAuth({
  236. type: "oauth",
  237. access: "access",
  238. refresh: "refresh",
  239. expires: 1,
  240. metadata: { code },
  241. }),
  242. ),
  243. }),
  244. }),
  245. )
  246. const attempt = yield* connectors.connect.oauth.begin({ connectorID, methodID, inputs: {}, label: "Personal" })
  247. expect(attempt.mode).toBe("code")
  248. yield* connectors.connect.oauth.complete({ attemptID: attempt.attemptID, code: "1234" })
  249. expect(created[0]).toEqual({
  250. connectorID,
  251. methodID,
  252. label: "Personal",
  253. value: new Credential.OAuth({
  254. type: "oauth",
  255. access: "access",
  256. refresh: "refresh",
  257. expires: 1,
  258. metadata: { code: "1234" },
  259. }),
  260. })
  261. }).pipe(Effect.provide(connectionLayer(created)))
  262. })
  263. it.effect("keeps code attempts open when the code is missing and closes them on cancel", () => {
  264. const created: Array<{
  265. connectorID: Connector.ID
  266. methodID: Connector.MethodID
  267. label?: string
  268. value: Credential.Value
  269. }> = []
  270. return Effect.gen(function* () {
  271. const connectors = yield* Connector.Service
  272. const connectorID = Connector.ID.make("openai")
  273. const methodID = Connector.MethodID.make("chatgpt")
  274. let closed = false
  275. yield* connectors.update((editor) =>
  276. editor.method.update({
  277. connectorID,
  278. method: new Connector.OAuthMethod({ id: methodID, type: "oauth", label: "ChatGPT" }),
  279. authorize: () =>
  280. Effect.addFinalizer(() => Effect.sync(() => (closed = true))).pipe(
  281. Effect.as({
  282. mode: "code" as const,
  283. url: "https://example.com/authorize",
  284. instructions: "Paste the code",
  285. callback: () => Effect.die("unexpected callback"),
  286. }),
  287. ),
  288. }),
  289. )
  290. const attempt = yield* connectors.connect.oauth.begin({ connectorID, methodID, inputs: {} })
  291. expect(
  292. yield* connectors.connect.oauth.complete({ attemptID: attempt.attemptID }).pipe(Effect.flip),
  293. ).toBeInstanceOf(Connector.CodeRequiredError)
  294. expect(closed).toBe(false)
  295. yield* connectors.connect.oauth.cancel(attempt.attemptID)
  296. expect(closed).toBe(true)
  297. expect(created).toEqual([])
  298. }).pipe(Effect.provide(connectionLayer(created)))
  299. })
  300. it.effect("completes auto OAuth in the background", () => {
  301. const created: Array<{
  302. connectorID: Connector.ID
  303. methodID: Connector.MethodID
  304. label?: string
  305. value: Credential.Value
  306. }> = []
  307. return Effect.gen(function* () {
  308. const connectors = yield* Connector.Service
  309. const connectorID = Connector.ID.make("openai")
  310. const methodID = Connector.MethodID.make("browser")
  311. yield* connectors.update((editor) =>
  312. editor.method.update({
  313. connectorID,
  314. method: new Connector.OAuthMethod({ id: methodID, type: "oauth", label: "Browser" }),
  315. authorize: () =>
  316. Effect.succeed({
  317. mode: "auto" as const,
  318. url: "https://example.com/authorize",
  319. instructions: "Sign in",
  320. callback: Effect.succeed(
  321. new Credential.OAuth({ type: "oauth", access: "access", refresh: "refresh", expires: 1 }),
  322. ),
  323. }),
  324. }),
  325. )
  326. const attempt = yield* connectors.connect.oauth.begin({ connectorID, methodID, inputs: {} })
  327. yield* Effect.yieldNow
  328. expect(yield* connectors.connect.oauth.status(attempt.attemptID)).toEqual({
  329. status: "complete",
  330. time: attempt.time,
  331. })
  332. expect(created).toHaveLength(1)
  333. }).pipe(Effect.provide(connectionLayer(created)))
  334. })
  335. it.effect("expires abandoned OAuth attempts", () => {
  336. const created: Array<{
  337. connectorID: Connector.ID
  338. methodID: Connector.MethodID
  339. label?: string
  340. value: Credential.Value
  341. }> = []
  342. return Effect.gen(function* () {
  343. const connectors = yield* Connector.Service
  344. const connectorID = Connector.ID.make("openai")
  345. const methodID = Connector.MethodID.make("browser")
  346. let closed = false
  347. yield* connectors.update((editor) =>
  348. editor.method.update({
  349. connectorID,
  350. method: new Connector.OAuthMethod({ id: methodID, type: "oauth", label: "Browser" }),
  351. authorize: () =>
  352. Effect.addFinalizer(() => Effect.sync(() => (closed = true))).pipe(
  353. Effect.as({
  354. mode: "auto" as const,
  355. url: "https://example.com/authorize",
  356. instructions: "Sign in",
  357. callback: Effect.never,
  358. }),
  359. ),
  360. }),
  361. )
  362. const attempt = yield* connectors.connect.oauth.begin({ connectorID, methodID, inputs: {} })
  363. expect(attempt.time.expires - attempt.time.created).toBe(Duration.toMillis(Duration.minutes(10)))
  364. yield* TestClock.adjust(Duration.minutes(10))
  365. yield* Effect.yieldNow
  366. expect(yield* connectors.connect.oauth.status(attempt.attemptID)).toEqual({
  367. status: "expired",
  368. time: attempt.time,
  369. })
  370. expect(closed).toBe(true)
  371. expect(created).toEqual([])
  372. }).pipe(Effect.provide(connectionLayer(created)))
  373. })
  374. })