|
| 1 | +import { expect } from "bun:test" |
| 2 | +import { Context, Deferred, Effect, Fiber, Layer } from "effect" |
| 3 | +import { TestClock } from "effect/testing" |
| 4 | +import { Node } from "@opencode/util/effect/app-node" |
| 5 | +import { LayerNode } from "@opencode/util/effect/layer-node" |
| 6 | +import { Credential } from "@opencode/core/credential" |
| 7 | +import { Integration } from "@opencode/core/integration" |
| 8 | +import { testEffect } from "./lib/effect" |
| 9 | + |
| 10 | +const it = testEffect(Layer.empty) |
| 11 | + |
| 12 | +const locations = Effect.gen(function* () { |
| 13 | + const layer = LayerNode.compile(LayerNode.group([Integration.node, Credential.node]), { |
| 14 | + shared: Node.tags.values.global, |
| 15 | + }) |
| 16 | + const memo = yield* Layer.makeMemoMap |
| 17 | + const scope = yield* Effect.scope |
| 18 | + const first = yield* Layer.buildWithMemoMap(layer, memo, scope) |
| 19 | + const second = yield* Layer.buildWithMemoMap(layer, memo, scope) |
| 20 | + const a = Context.get(first, Integration.Service) |
| 21 | + const b = Context.get(second, Integration.Service) |
| 22 | + const credentials = Context.get(first, Credential.Service) |
| 23 | + expect(a).not.toBe(b) |
| 24 | + expect(credentials).toBe(Context.get(second, Credential.Service)) |
| 25 | + return { a, b, credentials } |
| 26 | +}) |
| 27 | + |
| 28 | +for (const cancel of [false, true]) { |
| 29 | + it.effect(`shares refresh across Locations${cancel ? " after the first caller is interrupted" : ""}`, () => |
| 30 | + Effect.gen(function* () { |
| 31 | + const services = yield* locations |
| 32 | + const entered = yield* Deferred.make<void>() |
| 33 | + const release = yield* Deferred.make<void>() |
| 34 | + const integrationID = Integration.ID.make("openai") |
| 35 | + const methodID = Integration.MethodID.make("oauth") |
| 36 | + let calls = 0 |
| 37 | + const registration = { |
| 38 | + integrationID, |
| 39 | + method: { id: methodID, type: "oauth" as const, label: "OAuth" }, |
| 40 | + authorize: () => Effect.die("unused sign-in"), |
| 41 | + refresh: (value: Credential.OAuth) => |
| 42 | + Effect.gen(function* () { |
| 43 | + calls++ |
| 44 | + expect(value.refresh).toBe("old-refresh") |
| 45 | + yield* Deferred.succeed(entered, undefined) |
| 46 | + yield* Deferred.await(release) |
| 47 | + return Credential.OAuth.make({ |
| 48 | + ...value, |
| 49 | + access: "new-access", |
| 50 | + refresh: "new-refresh", |
| 51 | + expires: Number.MAX_SAFE_INTEGER, |
| 52 | + }) |
| 53 | + }), |
| 54 | + } |
| 55 | + yield* services.a.transform((editor) => editor.method.update(registration)) |
| 56 | + yield* services.b.transform((editor) => editor.method.update(registration)) |
| 57 | + const stored = yield* services.credentials.create({ |
| 58 | + integrationID, |
| 59 | + value: Credential.OAuth.make({ |
| 60 | + type: "oauth", |
| 61 | + methodID, |
| 62 | + access: "old-access", |
| 63 | + refresh: "old-refresh", |
| 64 | + expires: 1, |
| 65 | + }), |
| 66 | + }) |
| 67 | + const connection = { type: "credential" as const, id: stored.id, label: stored.label, method: "oauth" as const } |
| 68 | + const first = yield* services.a.connection.resolve(connection).pipe(Effect.forkScoped) |
| 69 | + yield* Deferred.await(entered) |
| 70 | + if (cancel) yield* Fiber.interrupt(first) |
| 71 | + const second = yield* services.b.connection.resolve(connection).pipe(Effect.forkScoped) |
| 72 | + yield* Effect.yieldNow |
| 73 | + expect(calls).toBe(1) |
| 74 | + yield* Deferred.succeed(release, undefined) |
| 75 | + const value = yield* Fiber.join(second) |
| 76 | + if (!cancel) expect(yield* Fiber.join(first)).toEqual(value) |
| 77 | + expect((yield* services.credentials.get(stored.id))?.value).toEqual(value) |
| 78 | + expect(yield* services.b.connection.resolve(connection)).toEqual(value) |
| 79 | + expect(calls).toBe(1) |
| 80 | + }), |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +it.effect("bounds a stuck integration refresh and allows a later attempt", () => |
| 85 | + Effect.gen(function* () { |
| 86 | + const services = yield* locations |
| 87 | + const entered = yield* Deferred.make<void>() |
| 88 | + const integrationID = Integration.ID.make("openai") |
| 89 | + const methodID = Integration.MethodID.make("oauth") |
| 90 | + const stored = yield* services.credentials.create({ |
| 91 | + integrationID, |
| 92 | + value: Credential.OAuth.make({ type: "oauth", methodID, access: "old", refresh: "refresh", expires: 1 }), |
| 93 | + }) |
| 94 | + yield* services.a.transform((editor) => |
| 95 | + editor.method.update({ |
| 96 | + integrationID, |
| 97 | + method: { id: methodID, type: "oauth", label: "OAuth" }, |
| 98 | + authorize: () => Effect.die("unused sign-in"), |
| 99 | + refresh: () => Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)), |
| 100 | + }), |
| 101 | + ) |
| 102 | + const connection = { type: "credential" as const, id: stored.id, label: stored.label, method: "oauth" as const } |
| 103 | + const first = yield* services.a.connection.resolve(connection).pipe(Effect.flip, Effect.forkScoped) |
| 104 | + yield* Deferred.await(entered) |
| 105 | + yield* TestClock.adjust("30 seconds") |
| 106 | + expect(yield* Fiber.join(first)).toBeInstanceOf(Integration.AuthorizationError) |
| 107 | + yield* services.a.transform((editor) => |
| 108 | + editor.method.update({ |
| 109 | + integrationID, |
| 110 | + method: { id: methodID, type: "oauth", label: "OAuth" }, |
| 111 | + authorize: () => Effect.die("unused sign-in"), |
| 112 | + refresh: (value) => |
| 113 | + Effect.succeed(Credential.OAuth.make({ ...value, access: "new", expires: Number.MAX_SAFE_INTEGER })), |
| 114 | + }), |
| 115 | + ) |
| 116 | + expect(yield* services.a.connection.resolve(connection)).toMatchObject({ access: "new" }) |
| 117 | + }), |
| 118 | +) |
0 commit comments