integration.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. import { describe, expect } from "bun:test"
  2. import { Cause, Clock, Duration, Effect, Exit, Fiber, Layer, Scope, Stream } from "effect"
  3. import * as TestClock from "effect/testing/TestClock"
  4. import { Credential } from "@opencode-ai/core/credential"
  5. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  6. import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
  7. import { LayerNode } from "@opencode-ai/util/effect/layer-node"
  8. import { Bus } from "@opencode-ai/core/bus"
  9. import { Integration } from "@opencode-ai/core/integration"
  10. import { testEffect } from "./lib/effect"
  11. const it = testEffect(AppNodeBuilder.build(LayerNode.group([Integration.node, Credential.node, Bus.node])))
  12. const failingCredentialNode = makeGlobalNode({
  13. service: Credential.Service,
  14. layer: Layer.succeed(
  15. Credential.Service,
  16. Credential.Service.of({
  17. all: () => Effect.succeed([]),
  18. list: () => Effect.succeed([]),
  19. get: () => Effect.succeed(undefined),
  20. create: () => Effect.die(new Error("credential persistence failed")),
  21. update: () => Effect.void,
  22. remove: () => Effect.void,
  23. }),
  24. ),
  25. deps: [],
  26. })
  27. const failingIt = testEffect(
  28. AppNodeBuilder.build(LayerNode.group([Integration.node, Bus.node]), [[Credential.node, failingCredentialNode]]),
  29. )
  30. function eventually<A, E, R>(
  31. effect: Effect.Effect<A, E, R>,
  32. predicate: (value: A) => boolean,
  33. remaining = 1000,
  34. ): Effect.Effect<A, E | Error, R> {
  35. return Effect.gen(function* () {
  36. const value = yield* effect
  37. if (predicate(value)) return value
  38. if (remaining === 0) return yield* Effect.fail(new Error("Timed out waiting for value"))
  39. yield* Effect.promise(() => Bun.sleep(1))
  40. return yield* eventually(effect, predicate, remaining - 1)
  41. })
  42. }
  43. describe("Integration", () => {
  44. it.effect("registers integrations through the editor", () =>
  45. Effect.gen(function* () {
  46. const integrations = yield* Integration.Service
  47. const scope = yield* Scope.fork(yield* Scope.Scope)
  48. const openai = Integration.ID.make("openai")
  49. yield* integrations
  50. .transform((editor) => editor.update(openai, (integration) => (integration.name = "OpenAI")))
  51. .pipe(Scope.provide(scope))
  52. expect(yield* integrations.get(openai)).toEqual(
  53. Integration.Info.make({ id: openai, name: "OpenAI", methods: [], connections: [] }),
  54. )
  55. yield* Scope.close(scope, Exit.void)
  56. expect(yield* integrations.get(openai)).toBeUndefined()
  57. }),
  58. )
  59. it.effect("reveals the previous registration when an override closes", () =>
  60. Effect.gen(function* () {
  61. const integrations = yield* Integration.Service
  62. const id = Integration.ID.make("openai")
  63. const first = yield* Scope.fork(yield* Scope.Scope)
  64. const second = yield* Scope.fork(yield* Scope.Scope)
  65. yield* integrations
  66. .transform((editor) => editor.update(id, (integration) => (integration.name = "OpenAI")))
  67. .pipe(Scope.provide(first))
  68. yield* integrations
  69. .transform((editor) => editor.update(id, (integration) => (integration.name = "OpenAI Override")))
  70. .pipe(Scope.provide(second))
  71. expect((yield* integrations.get(id))?.name).toBe("OpenAI Override")
  72. yield* Scope.close(second, Exit.void)
  73. expect((yield* integrations.get(id))?.name).toBe("OpenAI")
  74. expect((yield* integrations.list()).map((integration) => integration.id)).toEqual([id])
  75. }),
  76. )
  77. it.effect("registers and overrides methods independently", () =>
  78. Effect.gen(function* () {
  79. const integrations = yield* Integration.Service
  80. const integrationID = Integration.ID.make("openai")
  81. const methodID = Integration.MethodID.make("chatgpt")
  82. const first = yield* Scope.fork(yield* Scope.Scope)
  83. const second = yield* Scope.fork(yield* Scope.Scope)
  84. const authorize = () =>
  85. Effect.succeed({
  86. mode: "auto" as const,
  87. url: "https://example.com/authorize",
  88. instructions: "Sign in",
  89. callback: Effect.never,
  90. })
  91. yield* integrations
  92. .transform((editor) =>
  93. editor.method.update({
  94. integrationID,
  95. method: { id: methodID, type: "oauth", label: "ChatGPT" },
  96. authorize,
  97. }),
  98. )
  99. .pipe(Scope.provide(first))
  100. yield* integrations
  101. .transform((editor) => {
  102. expect(editor.get(integrationID)).toEqual({ id: integrationID, name: "openai" })
  103. expect(editor.list()).toEqual([{ id: integrationID, name: "openai" }])
  104. expect(editor.method.list(integrationID)).toEqual([
  105. expect.objectContaining({ id: methodID, label: "ChatGPT" }),
  106. ])
  107. editor.method.update({
  108. integrationID,
  109. method: { id: methodID, type: "oauth", label: "ChatGPT Override" },
  110. authorize,
  111. })
  112. })
  113. .pipe(Scope.provide(second))
  114. expect((yield* integrations.get(integrationID))?.name).toBe("openai")
  115. expect((yield* integrations.get(integrationID))?.methods[0]).toMatchObject({ label: "ChatGPT Override" })
  116. yield* Scope.close(second, Exit.void)
  117. expect((yield* integrations.get(integrationID))?.methods[0]).toMatchObject({ label: "ChatGPT" })
  118. expect((yield* integrations.get(integrationID))?.methods).toEqual([expect.objectContaining({ id: methodID })])
  119. }),
  120. )
  121. it.effect("connects with a key and stores the credential", () =>
  122. Effect.gen(function* () {
  123. const integrations = yield* Integration.Service
  124. const credentials = yield* Credential.Service
  125. const bus = yield* Bus.Service
  126. const integrationID = Integration.ID.make("openai")
  127. yield* integrations.transform((editor) =>
  128. editor.method.update({
  129. integrationID,
  130. method: { type: "key", label: "API key" },
  131. }),
  132. )
  133. const updated = yield* bus
  134. .subscribe(Integration.Event.Updated)
  135. .pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
  136. yield* Effect.yieldNow
  137. yield* integrations.connection.key({
  138. integrationID,
  139. key: "secret",
  140. label: "Work",
  141. })
  142. expect(yield* credentials.list(integrationID)).toEqual([
  143. expect.objectContaining({
  144. integrationID,
  145. label: "Work",
  146. value: Credential.Key.make({ type: "key", key: "secret" }),
  147. }),
  148. ])
  149. expect((yield* Fiber.join(updated)).length).toBe(1)
  150. }),
  151. )
  152. it.live("runs command authentication and stores the final output line", () =>
  153. Effect.gen(function* () {
  154. const integrations = yield* Integration.Service
  155. const credentials = yield* Credential.Service
  156. const integrationID = Integration.ID.make("company")
  157. const methodID = Integration.MethodID.make("login")
  158. yield* integrations.transform((editor) =>
  159. editor.method.update({
  160. integrationID,
  161. method: {
  162. id: methodID,
  163. type: "command",
  164. label: "Log in",
  165. command: [
  166. process.execPath,
  167. "-e",
  168. 'console.error("https://example.com/login"); await Bun.sleep(50); console.log("secret")',
  169. ],
  170. },
  171. }),
  172. )
  173. const attempt = yield* integrations.command.connect({ integrationID, methodID, label: "Work" })
  174. const pending = yield* eventually(
  175. integrations.command.status({ integrationID, attemptID: attempt.attemptID }),
  176. (status) => status.status === "pending" && status.message?.includes("https://example.com/login") === true,
  177. )
  178. expect(pending).toMatchObject({ status: "pending", message: "https://example.com/login\n" })
  179. expect(
  180. yield* eventually(
  181. integrations.command.status({ integrationID, attemptID: attempt.attemptID }),
  182. (status) => status.status === "complete",
  183. ),
  184. ).toEqual({ status: "complete", time: attempt.time })
  185. expect(yield* credentials.list(integrationID)).toEqual([
  186. expect.objectContaining({
  187. integrationID,
  188. label: "Work",
  189. value: Credential.Key.make({ type: "key", key: "secret" }),
  190. }),
  191. ])
  192. }),
  193. )
  194. it.effect("completes code OAuth once and stores the credential", () =>
  195. Effect.gen(function* () {
  196. const integrations = yield* Integration.Service
  197. const credentials = yield* Credential.Service
  198. const integrationID = Integration.ID.make("openai")
  199. const methodID = Integration.MethodID.make("chatgpt")
  200. yield* integrations.transform((editor) =>
  201. editor.method.update({
  202. integrationID,
  203. method: { id: methodID, type: "oauth", label: "ChatGPT" },
  204. authorize: () =>
  205. Effect.succeed({
  206. mode: "code" as const,
  207. url: "https://example.com/authorize",
  208. instructions: "Paste the code",
  209. callback: (code: string) =>
  210. Effect.succeed(
  211. Credential.OAuth.make({
  212. type: "oauth",
  213. methodID,
  214. access: "access",
  215. refresh: "refresh",
  216. expires: 1,
  217. metadata: { code },
  218. }),
  219. ),
  220. }),
  221. }),
  222. )
  223. const attempt = yield* integrations.oauth.connect({
  224. integrationID,
  225. methodID,
  226. inputs: {},
  227. label: "Personal",
  228. })
  229. expect(attempt.mode).toBe("code")
  230. yield* integrations.oauth.complete({ integrationID, attemptID: attempt.attemptID, code: "1234" })
  231. expect((yield* credentials.list(integrationID))[0]).toEqual(
  232. expect.objectContaining({
  233. integrationID,
  234. label: "Personal",
  235. value: Credential.OAuth.make({
  236. type: "oauth",
  237. methodID,
  238. access: "access",
  239. refresh: "refresh",
  240. expires: 1,
  241. metadata: { code: "1234" },
  242. }),
  243. }),
  244. )
  245. }),
  246. )
  247. it.effect("keeps code attempts open when the code is missing and closes them on cancel", () =>
  248. Effect.gen(function* () {
  249. const integrations = yield* Integration.Service
  250. const credentials = yield* Credential.Service
  251. const integrationID = Integration.ID.make("openai")
  252. const methodID = Integration.MethodID.make("chatgpt")
  253. let closed = false
  254. yield* integrations.transform((editor) =>
  255. editor.method.update({
  256. integrationID,
  257. method: { id: methodID, type: "oauth", label: "ChatGPT" },
  258. authorize: () =>
  259. Effect.addFinalizer(() => Effect.sync(() => (closed = true))).pipe(
  260. Effect.as({
  261. mode: "code" as const,
  262. url: "https://example.com/authorize",
  263. instructions: "Paste the code",
  264. callback: () => Effect.die("unexpected callback"),
  265. }),
  266. ),
  267. }),
  268. )
  269. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  270. expect(
  271. yield* integrations.oauth.complete({ integrationID, attemptID: attempt.attemptID }).pipe(Effect.flip),
  272. ).toBeInstanceOf(Integration.CodeRequiredError)
  273. expect(closed).toBe(false)
  274. yield* integrations.oauth.cancel({
  275. integrationID: Integration.ID.make("other"),
  276. attemptID: attempt.attemptID,
  277. })
  278. expect(closed).toBe(false)
  279. yield* integrations.oauth.cancel({ integrationID, attemptID: attempt.attemptID })
  280. expect(closed).toBe(true)
  281. expect(yield* credentials.list(integrationID)).toEqual([])
  282. }),
  283. )
  284. it.effect("completes auto OAuth in the background", () =>
  285. Effect.gen(function* () {
  286. const integrations = yield* Integration.Service
  287. const credentials = yield* Credential.Service
  288. const integrationID = Integration.ID.make("openai")
  289. const methodID = Integration.MethodID.make("browser")
  290. yield* integrations.transform((editor) =>
  291. editor.method.update({
  292. integrationID,
  293. method: { id: methodID, type: "oauth", label: "Browser" },
  294. authorize: () =>
  295. Effect.succeed({
  296. mode: "auto" as const,
  297. url: "https://example.com/authorize",
  298. instructions: "Sign in",
  299. callback: Effect.succeed(
  300. Credential.OAuth.make({ type: "oauth", methodID, access: "access", refresh: "refresh", expires: 1 }),
  301. ),
  302. }),
  303. }),
  304. )
  305. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  306. yield* Effect.yieldNow
  307. expect(yield* integrations.oauth.status({ integrationID, attemptID: attempt.attemptID })).toEqual({
  308. status: "complete",
  309. time: attempt.time,
  310. })
  311. expect(yield* credentials.list(integrationID)).toHaveLength(1)
  312. }),
  313. )
  314. failingIt.effect("fails the attempt when credential persistence fails", () =>
  315. Effect.gen(function* () {
  316. const integrations = yield* Integration.Service
  317. const integrationID = Integration.ID.make("openai")
  318. const methodID = Integration.MethodID.make("chatgpt")
  319. yield* integrations.transform((editor) =>
  320. editor.method.update({
  321. integrationID,
  322. method: { id: methodID, type: "oauth", label: "ChatGPT" },
  323. authorize: () =>
  324. Effect.succeed({
  325. mode: "code" as const,
  326. url: "https://example.com/authorize",
  327. instructions: "Paste the code",
  328. callback: () =>
  329. Effect.succeed(
  330. Credential.OAuth.make({
  331. type: "oauth",
  332. methodID,
  333. access: "access",
  334. refresh: "refresh",
  335. expires: 1,
  336. }),
  337. ),
  338. }),
  339. }),
  340. )
  341. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  342. const exit = yield* integrations.oauth
  343. .complete({ integrationID, attemptID: attempt.attemptID, code: "1234" })
  344. .pipe(Effect.exit)
  345. expect(Exit.isFailure(exit) && Cause.hasDies(exit.cause)).toBe(true)
  346. expect(yield* integrations.oauth.status({ integrationID, attemptID: attempt.attemptID })).toEqual({
  347. status: "failed",
  348. message: "credential persistence failed",
  349. time: attempt.time,
  350. })
  351. }),
  352. )
  353. it.effect("expires abandoned OAuth attempts", () =>
  354. Effect.gen(function* () {
  355. const integrations = yield* Integration.Service
  356. const credentials = yield* Credential.Service
  357. const integrationID = Integration.ID.make("openai")
  358. const methodID = Integration.MethodID.make("browser")
  359. let closed = false
  360. yield* integrations.transform((editor) =>
  361. editor.method.update({
  362. integrationID,
  363. method: { id: methodID, type: "oauth", label: "Browser" },
  364. authorize: () =>
  365. Effect.addFinalizer(() => Effect.sync(() => (closed = true))).pipe(
  366. Effect.as({
  367. mode: "auto" as const,
  368. url: "https://example.com/authorize",
  369. instructions: "Sign in",
  370. callback: Effect.never,
  371. }),
  372. ),
  373. }),
  374. )
  375. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  376. expect(attempt.time.expires - attempt.time.created).toBe(Duration.toMillis(Duration.minutes(10)))
  377. yield* TestClock.adjust(Duration.minutes(10))
  378. yield* Effect.yieldNow
  379. expect(yield* integrations.oauth.status({ integrationID, attemptID: attempt.attemptID })).toEqual({
  380. status: "expired",
  381. time: attempt.time,
  382. })
  383. expect(closed).toBe(true)
  384. expect(yield* credentials.list(integrationID)).toEqual([])
  385. }),
  386. )
  387. it.effect("uses provider-defined OAuth attempt expirations", () =>
  388. Effect.gen(function* () {
  389. const integrations = yield* Integration.Service
  390. const integrationID = Integration.ID.make("openai")
  391. const created = yield* Clock.currentTimeMillis
  392. const expirations = [
  393. created + Duration.toMillis(Duration.minutes(5)),
  394. created + Duration.toMillis(Duration.minutes(20)),
  395. ]
  396. yield* Effect.forEach(expirations, (expiresAt, index) => {
  397. const methodID = Integration.MethodID.make(`browser-${index}`)
  398. return Effect.gen(function* () {
  399. yield* integrations.transform((editor) =>
  400. editor.method.update({
  401. integrationID,
  402. method: { id: methodID, type: "oauth", label: "Browser" },
  403. authorize: () =>
  404. Effect.succeed({
  405. mode: "auto" as const,
  406. url: "https://example.com/authorize",
  407. instructions: "Sign in",
  408. expiresAt,
  409. callback: Effect.never,
  410. }),
  411. }),
  412. )
  413. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  414. expect(attempt.time).toEqual({ created, expires: expiresAt })
  415. })
  416. })
  417. }),
  418. )
  419. it.effect("projects credential and env connections", () => {
  420. const integrationID = Integration.ID.make("acme")
  421. return Effect.acquireUseRelease(
  422. Effect.sync(() => {
  423. const previous = process.env.INTEGRATION_TEST_ACME_KEY
  424. process.env.INTEGRATION_TEST_ACME_KEY = "secret"
  425. delete process.env.INTEGRATION_TEST_ACME_MISSING
  426. return previous
  427. }),
  428. () =>
  429. Effect.gen(function* () {
  430. const integrations = yield* Integration.Service
  431. const credentials = yield* Credential.Service
  432. yield* integrations.transform((editor) =>
  433. editor.method.update({
  434. integrationID,
  435. method: {
  436. type: "env",
  437. names: ["INTEGRATION_TEST_ACME_KEY", "INTEGRATION_TEST_ACME_MISSING"],
  438. },
  439. }),
  440. )
  441. const work = yield* credentials.create({
  442. integrationID,
  443. label: "Work",
  444. value: Credential.Key.make({ type: "key", key: "a" }),
  445. })
  446. const personal = yield* credentials.create({
  447. integrationID,
  448. label: "Personal",
  449. value: Credential.Key.make({ type: "key", key: "b" }),
  450. })
  451. // Stored credentials and detected env vars appear as connections.
  452. expect((yield* integrations.get(integrationID))?.connections).toEqual([
  453. {
  454. type: "credential",
  455. id: personal.id,
  456. label: "Personal",
  457. },
  458. { type: "env", name: "INTEGRATION_TEST_ACME_KEY" },
  459. ])
  460. expect(yield* integrations.connection.active(integrationID)).toEqual({
  461. type: "credential",
  462. id: personal.id,
  463. label: "Personal",
  464. })
  465. expect(work.id).not.toBe(personal.id)
  466. }),
  467. (previous) =>
  468. Effect.sync(() => {
  469. if (previous === undefined) delete process.env.INTEGRATION_TEST_ACME_KEY
  470. else process.env.INTEGRATION_TEST_ACME_KEY = previous
  471. }),
  472. )
  473. })
  474. })