integration.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. inputs: { accountId: "account" },
  141. label: "Work",
  142. })
  143. expect(yield* credentials.list(integrationID)).toEqual([
  144. expect.objectContaining({
  145. integrationID,
  146. label: "Work",
  147. value: Credential.Key.make({ type: "key", key: "secret", metadata: { accountId: "account" } }),
  148. }),
  149. ])
  150. expect((yield* Fiber.join(updated)).length).toBe(1)
  151. }),
  152. )
  153. it.live("runs command authentication and stores the final output line", () =>
  154. Effect.gen(function* () {
  155. const integrations = yield* Integration.Service
  156. const credentials = yield* Credential.Service
  157. const integrationID = Integration.ID.make("company")
  158. const methodID = Integration.MethodID.make("login")
  159. yield* integrations.transform((editor) =>
  160. editor.method.update({
  161. integrationID,
  162. method: {
  163. id: methodID,
  164. type: "command",
  165. label: "Log in",
  166. command: [
  167. process.execPath,
  168. "-e",
  169. 'console.error("https://example.com/login"); await Bun.sleep(50); console.log("secret")',
  170. ],
  171. },
  172. }),
  173. )
  174. const attempt = yield* integrations.command.connect({ integrationID, methodID, label: "Work" })
  175. const pending = yield* eventually(
  176. integrations.command.status({ integrationID, attemptID: attempt.attemptID }),
  177. (status) => status.status === "pending" && status.message?.includes("https://example.com/login") === true,
  178. )
  179. expect(pending).toMatchObject({ status: "pending", message: "https://example.com/login\n" })
  180. expect(
  181. yield* eventually(
  182. integrations.command.status({ integrationID, attemptID: attempt.attemptID }),
  183. (status) => status.status === "complete",
  184. ),
  185. ).toEqual({ status: "complete", time: attempt.time })
  186. expect(yield* credentials.list(integrationID)).toEqual([
  187. expect.objectContaining({
  188. integrationID,
  189. label: "Work",
  190. value: Credential.Key.make({ type: "key", key: "secret" }),
  191. }),
  192. ])
  193. }),
  194. )
  195. it.effect("completes code OAuth once and stores the credential", () =>
  196. Effect.gen(function* () {
  197. const integrations = yield* Integration.Service
  198. const credentials = yield* Credential.Service
  199. const integrationID = Integration.ID.make("openai")
  200. const methodID = Integration.MethodID.make("chatgpt")
  201. yield* integrations.transform((editor) =>
  202. editor.method.update({
  203. integrationID,
  204. method: { id: methodID, type: "oauth", label: "ChatGPT" },
  205. authorize: () =>
  206. Effect.succeed({
  207. mode: "code" as const,
  208. url: "https://example.com/authorize",
  209. instructions: "Paste the code",
  210. callback: (code: string) =>
  211. Effect.succeed(
  212. Credential.OAuth.make({
  213. type: "oauth",
  214. methodID,
  215. access: "access",
  216. refresh: "refresh",
  217. expires: 1,
  218. metadata: { code },
  219. }),
  220. ),
  221. }),
  222. }),
  223. )
  224. const attempt = yield* integrations.oauth.connect({
  225. integrationID,
  226. methodID,
  227. inputs: {},
  228. label: "Personal",
  229. })
  230. expect(attempt.mode).toBe("code")
  231. yield* integrations.oauth.complete({ integrationID, attemptID: attempt.attemptID, code: "1234" })
  232. expect((yield* credentials.list(integrationID))[0]).toEqual(
  233. expect.objectContaining({
  234. integrationID,
  235. label: "Personal",
  236. value: Credential.OAuth.make({
  237. type: "oauth",
  238. methodID,
  239. access: "access",
  240. refresh: "refresh",
  241. expires: 1,
  242. metadata: { code: "1234" },
  243. }),
  244. }),
  245. )
  246. }),
  247. )
  248. it.effect("keeps code attempts open when the code is missing and closes them on cancel", () =>
  249. Effect.gen(function* () {
  250. const integrations = yield* Integration.Service
  251. const credentials = yield* Credential.Service
  252. const integrationID = Integration.ID.make("openai")
  253. const methodID = Integration.MethodID.make("chatgpt")
  254. let closed = false
  255. yield* integrations.transform((editor) =>
  256. editor.method.update({
  257. integrationID,
  258. method: { id: methodID, type: "oauth", label: "ChatGPT" },
  259. authorize: () =>
  260. Effect.addFinalizer(() => Effect.sync(() => (closed = true))).pipe(
  261. Effect.as({
  262. mode: "code" as const,
  263. url: "https://example.com/authorize",
  264. instructions: "Paste the code",
  265. callback: () => Effect.die("unexpected callback"),
  266. }),
  267. ),
  268. }),
  269. )
  270. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  271. expect(
  272. yield* integrations.oauth.complete({ integrationID, attemptID: attempt.attemptID }).pipe(Effect.flip),
  273. ).toBeInstanceOf(Integration.CodeRequiredError)
  274. expect(closed).toBe(false)
  275. yield* integrations.oauth.cancel({
  276. integrationID: Integration.ID.make("other"),
  277. attemptID: attempt.attemptID,
  278. })
  279. expect(closed).toBe(false)
  280. yield* integrations.oauth.cancel({ integrationID, attemptID: attempt.attemptID })
  281. expect(closed).toBe(true)
  282. expect(yield* credentials.list(integrationID)).toEqual([])
  283. }),
  284. )
  285. it.effect("completes auto OAuth in the background", () =>
  286. Effect.gen(function* () {
  287. const integrations = yield* Integration.Service
  288. const credentials = yield* Credential.Service
  289. const integrationID = Integration.ID.make("openai")
  290. const methodID = Integration.MethodID.make("browser")
  291. yield* integrations.transform((editor) =>
  292. editor.method.update({
  293. integrationID,
  294. method: { id: methodID, type: "oauth", label: "Browser" },
  295. authorize: () =>
  296. Effect.succeed({
  297. mode: "auto" as const,
  298. url: "https://example.com/authorize",
  299. instructions: "Sign in",
  300. callback: Effect.succeed(
  301. Credential.OAuth.make({ type: "oauth", methodID, access: "access", refresh: "refresh", expires: 1 }),
  302. ),
  303. }),
  304. }),
  305. )
  306. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  307. yield* Effect.yieldNow
  308. expect(yield* integrations.oauth.status({ integrationID, attemptID: attempt.attemptID })).toEqual({
  309. status: "complete",
  310. time: attempt.time,
  311. })
  312. expect(yield* credentials.list(integrationID)).toHaveLength(1)
  313. }),
  314. )
  315. failingIt.effect("fails the attempt when credential persistence fails", () =>
  316. Effect.gen(function* () {
  317. const integrations = yield* Integration.Service
  318. const integrationID = Integration.ID.make("openai")
  319. const methodID = Integration.MethodID.make("chatgpt")
  320. yield* integrations.transform((editor) =>
  321. editor.method.update({
  322. integrationID,
  323. method: { id: methodID, type: "oauth", label: "ChatGPT" },
  324. authorize: () =>
  325. Effect.succeed({
  326. mode: "code" as const,
  327. url: "https://example.com/authorize",
  328. instructions: "Paste the code",
  329. callback: () =>
  330. Effect.succeed(
  331. Credential.OAuth.make({
  332. type: "oauth",
  333. methodID,
  334. access: "access",
  335. refresh: "refresh",
  336. expires: 1,
  337. }),
  338. ),
  339. }),
  340. }),
  341. )
  342. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  343. const exit = yield* integrations.oauth
  344. .complete({ integrationID, attemptID: attempt.attemptID, code: "1234" })
  345. .pipe(Effect.exit)
  346. expect(Exit.isFailure(exit) && Cause.hasDies(exit.cause)).toBe(true)
  347. expect(yield* integrations.oauth.status({ integrationID, attemptID: attempt.attemptID })).toEqual({
  348. status: "failed",
  349. message: "credential persistence failed",
  350. time: attempt.time,
  351. })
  352. }),
  353. )
  354. it.effect("expires abandoned OAuth attempts", () =>
  355. Effect.gen(function* () {
  356. const integrations = yield* Integration.Service
  357. const credentials = yield* Credential.Service
  358. const integrationID = Integration.ID.make("openai")
  359. const methodID = Integration.MethodID.make("browser")
  360. let closed = false
  361. yield* integrations.transform((editor) =>
  362. editor.method.update({
  363. integrationID,
  364. method: { id: methodID, type: "oauth", label: "Browser" },
  365. authorize: () =>
  366. Effect.addFinalizer(() => Effect.sync(() => (closed = true))).pipe(
  367. Effect.as({
  368. mode: "auto" as const,
  369. url: "https://example.com/authorize",
  370. instructions: "Sign in",
  371. callback: Effect.never,
  372. }),
  373. ),
  374. }),
  375. )
  376. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  377. expect(attempt.time.expires - attempt.time.created).toBe(Duration.toMillis(Duration.minutes(10)))
  378. yield* TestClock.adjust(Duration.minutes(10))
  379. yield* Effect.yieldNow
  380. expect(yield* integrations.oauth.status({ integrationID, attemptID: attempt.attemptID })).toEqual({
  381. status: "expired",
  382. time: attempt.time,
  383. })
  384. expect(closed).toBe(true)
  385. expect(yield* credentials.list(integrationID)).toEqual([])
  386. }),
  387. )
  388. it.effect("uses provider-defined OAuth attempt expirations", () =>
  389. Effect.gen(function* () {
  390. const integrations = yield* Integration.Service
  391. const integrationID = Integration.ID.make("openai")
  392. const created = yield* Clock.currentTimeMillis
  393. const expirations = [
  394. created + Duration.toMillis(Duration.minutes(5)),
  395. created + Duration.toMillis(Duration.minutes(20)),
  396. ]
  397. yield* Effect.forEach(expirations, (expiresAt, index) => {
  398. const methodID = Integration.MethodID.make(`browser-${index}`)
  399. return Effect.gen(function* () {
  400. yield* integrations.transform((editor) =>
  401. editor.method.update({
  402. integrationID,
  403. method: { id: methodID, type: "oauth", label: "Browser" },
  404. authorize: () =>
  405. Effect.succeed({
  406. mode: "auto" as const,
  407. url: "https://example.com/authorize",
  408. instructions: "Sign in",
  409. expiresAt,
  410. callback: Effect.never,
  411. }),
  412. }),
  413. )
  414. const attempt = yield* integrations.oauth.connect({ integrationID, methodID, inputs: {} })
  415. expect(attempt.time).toEqual({ created, expires: expiresAt })
  416. })
  417. })
  418. }),
  419. )
  420. it.effect("projects credential and env connections", () => {
  421. const integrationID = Integration.ID.make("acme")
  422. return Effect.acquireUseRelease(
  423. Effect.sync(() => {
  424. const previous = process.env.INTEGRATION_TEST_ACME_KEY
  425. process.env.INTEGRATION_TEST_ACME_KEY = "secret"
  426. delete process.env.INTEGRATION_TEST_ACME_MISSING
  427. return previous
  428. }),
  429. () =>
  430. Effect.gen(function* () {
  431. const integrations = yield* Integration.Service
  432. const credentials = yield* Credential.Service
  433. yield* integrations.transform((editor) =>
  434. editor.method.update({
  435. integrationID,
  436. method: {
  437. type: "env",
  438. names: ["INTEGRATION_TEST_ACME_KEY", "INTEGRATION_TEST_ACME_MISSING"],
  439. },
  440. }),
  441. )
  442. const work = yield* credentials.create({
  443. integrationID,
  444. label: "Work",
  445. value: Credential.Key.make({ type: "key", key: "a" }),
  446. })
  447. const personal = yield* credentials.create({
  448. integrationID,
  449. label: "Personal",
  450. value: Credential.Key.make({ type: "key", key: "b" }),
  451. })
  452. // Stored credentials and detected env vars appear as connections.
  453. expect((yield* integrations.get(integrationID))?.connections).toEqual([
  454. {
  455. type: "credential",
  456. id: personal.id,
  457. label: "Personal",
  458. },
  459. { type: "env", name: "INTEGRATION_TEST_ACME_KEY" },
  460. ])
  461. expect(yield* integrations.connection.active(integrationID)).toEqual({
  462. type: "credential",
  463. id: personal.id,
  464. label: "Personal",
  465. })
  466. expect(work.id).not.toBe(personal.id)
  467. }),
  468. (previous) =>
  469. Effect.sync(() => {
  470. if (previous === undefined) delete process.env.INTEGRATION_TEST_ACME_KEY
  471. else process.env.INTEGRATION_TEST_ACME_KEY = previous
  472. }),
  473. )
  474. })
  475. })