|
| 1 | +import { afterEach, describe, expect } from "bun:test" |
| 2 | +import type { UpgradeWebSocket } from "hono/ws" |
| 3 | +import { Effect, FileSystem, Layer, Path } from "effect" |
| 4 | +import { NodeFileSystem, NodePath } from "@effect/platform-node" |
| 5 | +import { Flag } from "@opencode-ai/core/flag/flag" |
| 6 | +import { Instance } from "../../src/project/instance" |
| 7 | +import { InstanceRoutes } from "../../src/server/routes/instance" |
| 8 | +import * as Log from "@opencode-ai/core/util/log" |
| 9 | +import { resetDatabase } from "../fixture/db" |
| 10 | +import { provideInstance } from "../fixture/fixture" |
| 11 | +import { testEffect } from "../lib/effect" |
| 12 | + |
| 13 | +void Log.init({ print: false }) |
| 14 | + |
| 15 | +const original = Flag.OPENCODE_EXPERIMENTAL_HTTPAPI |
| 16 | +const websocket = (() => () => new Response(null, { status: 501 })) as unknown as UpgradeWebSocket |
| 17 | +const it = testEffect(Layer.mergeAll(NodeFileSystem.layer, NodePath.layer)) |
| 18 | +const providerID = "test-oauth-parity" |
| 19 | +const oauthURL = "https://example.com/oauth" |
| 20 | +const oauthInstructions = "Finish OAuth" |
| 21 | + |
| 22 | +function app(experimental: boolean) { |
| 23 | + Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = experimental |
| 24 | + return InstanceRoutes(websocket) |
| 25 | +} |
| 26 | + |
| 27 | +function requestAuthorize(input: { |
| 28 | + app: ReturnType<typeof InstanceRoutes> |
| 29 | + providerID: string |
| 30 | + method: number |
| 31 | + headers: HeadersInit |
| 32 | +}) { |
| 33 | + return Effect.promise(async () => { |
| 34 | + const response = await input.app.request(`/provider/${input.providerID}/oauth/authorize`, { |
| 35 | + method: "POST", |
| 36 | + headers: input.headers, |
| 37 | + body: JSON.stringify({ method: input.method }), |
| 38 | + }) |
| 39 | + return { |
| 40 | + status: response.status, |
| 41 | + body: await response.text(), |
| 42 | + } |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +function writeProviderAuthPlugin(dir: string) { |
| 47 | + return Effect.gen(function* () { |
| 48 | + const fs = yield* FileSystem.FileSystem |
| 49 | + const path = yield* Path.Path |
| 50 | + |
| 51 | + yield* fs.makeDirectory(path.join(dir, ".opencode", "plugin"), { recursive: true }) |
| 52 | + yield* fs.writeFileString( |
| 53 | + path.join(dir, ".opencode", "plugin", "provider-oauth-parity.ts"), |
| 54 | + [ |
| 55 | + "export default {", |
| 56 | + ' id: "test.provider-oauth-parity",', |
| 57 | + " server: async () => ({", |
| 58 | + " auth: {", |
| 59 | + ` provider: "${providerID}",`, |
| 60 | + " methods: [", |
| 61 | + ' { type: "api", label: "API key" },', |
| 62 | + " {", |
| 63 | + ' type: "oauth",', |
| 64 | + ' label: "OAuth",', |
| 65 | + " authorize: async () => ({", |
| 66 | + ` url: "${oauthURL}",`, |
| 67 | + ' method: "code",', |
| 68 | + ` instructions: "${oauthInstructions}",`, |
| 69 | + " callback: async () => ({ type: 'success', key: 'token' }),", |
| 70 | + " }),", |
| 71 | + " },", |
| 72 | + " ],", |
| 73 | + " },", |
| 74 | + " }),", |
| 75 | + "}", |
| 76 | + "", |
| 77 | + ].join("\n"), |
| 78 | + ) |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +function withProviderProject<A, E, R>(self: (dir: string) => Effect.Effect<A, E, R>) { |
| 83 | + return Effect.gen(function* () { |
| 84 | + const fs = yield* FileSystem.FileSystem |
| 85 | + const path = yield* Path.Path |
| 86 | + const dir = yield* fs.makeTempDirectoryScoped({ prefix: "opencode-test-" }) |
| 87 | + |
| 88 | + yield* fs.writeFileString( |
| 89 | + path.join(dir, "opencode.json"), |
| 90 | + JSON.stringify({ $schema: "https://opencode.ai/config.json", formatter: false, lsp: false }), |
| 91 | + ) |
| 92 | + yield* writeProviderAuthPlugin(dir) |
| 93 | + yield* Effect.addFinalizer(() => |
| 94 | + Effect.promise(() => Instance.provide({ directory: dir, fn: () => Instance.dispose() })).pipe(Effect.ignore), |
| 95 | + ) |
| 96 | + |
| 97 | + return yield* self(dir).pipe(provideInstance(dir)) |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +afterEach(async () => { |
| 102 | + Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = original |
| 103 | + await Instance.disposeAll() |
| 104 | + await resetDatabase() |
| 105 | +}) |
| 106 | + |
| 107 | +describe("provider HttpApi", () => { |
| 108 | + it.live( |
| 109 | + "matches legacy OAuth authorize response shapes", |
| 110 | + withProviderProject((dir) => |
| 111 | + Effect.gen(function* () { |
| 112 | + const headers = { "x-opencode-directory": dir, "content-type": "application/json" } |
| 113 | + const legacy = app(false) |
| 114 | + const httpapi = app(true) |
| 115 | + |
| 116 | + const apiLegacy = yield* requestAuthorize({ |
| 117 | + app: legacy, |
| 118 | + providerID, |
| 119 | + method: 0, |
| 120 | + headers, |
| 121 | + }) |
| 122 | + const apiHttpApi = yield* requestAuthorize({ |
| 123 | + app: httpapi, |
| 124 | + providerID, |
| 125 | + method: 0, |
| 126 | + headers, |
| 127 | + }) |
| 128 | + expect(apiLegacy).toEqual({ status: 200, body: "" }) |
| 129 | + expect(apiHttpApi).toEqual(apiLegacy) |
| 130 | + |
| 131 | + const oauthLegacy = yield* requestAuthorize({ |
| 132 | + app: legacy, |
| 133 | + providerID, |
| 134 | + method: 1, |
| 135 | + headers, |
| 136 | + }) |
| 137 | + const oauthHttpApi = yield* requestAuthorize({ |
| 138 | + app: httpapi, |
| 139 | + providerID, |
| 140 | + method: 1, |
| 141 | + headers, |
| 142 | + }) |
| 143 | + expect(oauthHttpApi).toEqual(oauthLegacy) |
| 144 | + expect(JSON.parse(oauthHttpApi.body)).toEqual({ |
| 145 | + url: oauthURL, |
| 146 | + method: "code", |
| 147 | + instructions: oauthInstructions, |
| 148 | + }) |
| 149 | + }), |
| 150 | + ), |
| 151 | + ) |
| 152 | +}) |
0 commit comments