integration.test.ts 19 KB

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