test(server): migrate httpapi ui tests to effect runner (#27236)
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { createHash } from "node:crypto"
|
import { createHash } from "node:crypto"
|
||||||
import { afterEach, describe, expect, test } from "bun:test"
|
import { describe, expect } from "bun:test"
|
||||||
import { Flag } from "@opencode-ai/core/flag/flag"
|
import { Flag } from "@opencode-ai/core/flag/flag"
|
||||||
import * as Log from "@opencode-ai/core/util/log"
|
import * as Log from "@opencode-ai/core/util/log"
|
||||||
import { ConfigProvider, Effect, Layer } from "effect"
|
import { ConfigProvider, Effect, Layer } from "effect"
|
||||||
@@ -18,24 +18,33 @@ import { authorizationRouterMiddleware } from "../../src/server/routes/instance/
|
|||||||
import { ExperimentalHttpApiServer } from "../../src/server/routes/instance/httpapi/server"
|
import { ExperimentalHttpApiServer } from "../../src/server/routes/instance/httpapi/server"
|
||||||
import { serveEmbeddedUIEffect, serveUIEffect } from "../../src/server/shared/ui"
|
import { serveEmbeddedUIEffect, serveUIEffect } from "../../src/server/shared/ui"
|
||||||
import { Server } from "../../src/server/server"
|
import { Server } from "../../src/server/server"
|
||||||
|
import { testEffect } from "../lib/effect"
|
||||||
|
|
||||||
void Log.init({ print: false })
|
void Log.init({ print: false })
|
||||||
|
|
||||||
const original = {
|
const testStateLayer = Layer.effectDiscard(
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const original = {
|
||||||
OPENCODE_DISABLE_EMBEDDED_WEB_UI: Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI,
|
OPENCODE_DISABLE_EMBEDDED_WEB_UI: Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI,
|
||||||
OPENCODE_SERVER_PASSWORD: Flag.OPENCODE_SERVER_PASSWORD,
|
OPENCODE_SERVER_PASSWORD: Flag.OPENCODE_SERVER_PASSWORD,
|
||||||
OPENCODE_SERVER_USERNAME: Flag.OPENCODE_SERVER_USERNAME,
|
OPENCODE_SERVER_USERNAME: Flag.OPENCODE_SERVER_USERNAME,
|
||||||
envPassword: process.env.OPENCODE_SERVER_PASSWORD,
|
envPassword: process.env.OPENCODE_SERVER_PASSWORD,
|
||||||
envUsername: process.env.OPENCODE_SERVER_USERNAME,
|
envUsername: process.env.OPENCODE_SERVER_USERNAME,
|
||||||
}
|
}
|
||||||
|
|
||||||
afterEach(() => {
|
yield* Effect.addFinalizer(() =>
|
||||||
|
Effect.sync(() => {
|
||||||
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = original.OPENCODE_DISABLE_EMBEDDED_WEB_UI
|
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = original.OPENCODE_DISABLE_EMBEDDED_WEB_UI
|
||||||
Flag.OPENCODE_SERVER_PASSWORD = original.OPENCODE_SERVER_PASSWORD
|
Flag.OPENCODE_SERVER_PASSWORD = original.OPENCODE_SERVER_PASSWORD
|
||||||
Flag.OPENCODE_SERVER_USERNAME = original.OPENCODE_SERVER_USERNAME
|
Flag.OPENCODE_SERVER_USERNAME = original.OPENCODE_SERVER_USERNAME
|
||||||
restoreEnv("OPENCODE_SERVER_PASSWORD", original.envPassword)
|
restoreEnv("OPENCODE_SERVER_PASSWORD", original.envPassword)
|
||||||
restoreEnv("OPENCODE_SERVER_USERNAME", original.envUsername)
|
restoreEnv("OPENCODE_SERVER_USERNAME", original.envUsername)
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
const it = testEffect(Layer.mergeAll(testStateLayer, AppFileSystem.defaultLayer))
|
||||||
|
|
||||||
function restoreEnv(key: string, value: string | undefined) {
|
function restoreEnv(key: string, value: string | undefined) {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
@@ -61,9 +70,13 @@ function app(input?: { password?: string; username?: string }) {
|
|||||||
).handler
|
).handler
|
||||||
return {
|
return {
|
||||||
request(input: string | URL | Request, init?: RequestInit) {
|
request(input: string | URL | Request, init?: RequestInit) {
|
||||||
return handler(
|
return Effect.promise(() =>
|
||||||
|
Promise.resolve(
|
||||||
|
handler(
|
||||||
input instanceof Request ? input : new Request(new URL(input, "http://localhost"), init),
|
input instanceof Request ? input : new Request(new URL(input, "http://localhost"), init),
|
||||||
ExperimentalHttpApiServer.context,
|
ExperimentalHttpApiServer.context,
|
||||||
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -95,9 +108,13 @@ function uiApp(input?: { password?: string; username?: string; client?: Layer.La
|
|||||||
).handler
|
).handler
|
||||||
return {
|
return {
|
||||||
request(input: string | URL | Request, init?: RequestInit) {
|
request(input: string | URL | Request, init?: RequestInit) {
|
||||||
return handler(
|
return Effect.promise(() =>
|
||||||
|
Promise.resolve(
|
||||||
|
handler(
|
||||||
input instanceof Request ? input : new Request(new URL(input, "http://localhost"), init),
|
input instanceof Request ? input : new Request(new URL(input, "http://localhost"), init),
|
||||||
ExperimentalHttpApiServer.context,
|
ExperimentalHttpApiServer.context,
|
||||||
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -113,12 +130,17 @@ function httpClient(response: Response, onRequest?: (request: HttpClientRequest.
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function responseText(response: Response) {
|
||||||
|
return Effect.promise(() => response.text())
|
||||||
|
}
|
||||||
|
|
||||||
describe("HttpApi UI fallback", () => {
|
describe("HttpApi UI fallback", () => {
|
||||||
test("serves the web UI through the experimental backend", async () => {
|
it.live("serves the web UI through the experimental backend", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
||||||
let proxiedUrl: string | undefined
|
let proxiedUrl: string | undefined
|
||||||
|
|
||||||
const response = await uiApp({
|
const response = yield* uiApp({
|
||||||
client: httpClient(
|
client: httpClient(
|
||||||
new Response("<html>opencode</html>", { headers: { "content-type": "text/html" } }),
|
new Response("<html>opencode</html>", { headers: { "content-type": "text/html" } }),
|
||||||
(request) => {
|
(request) => {
|
||||||
@@ -129,16 +151,17 @@ describe("HttpApi UI fallback", () => {
|
|||||||
|
|
||||||
expect(response.status).toBe(200)
|
expect(response.status).toBe(200)
|
||||||
expect(response.headers.get("content-type")).toContain("text/html")
|
expect(response.headers.get("content-type")).toContain("text/html")
|
||||||
expect(await response.text()).toBe("<html>opencode</html>")
|
expect(yield* responseText(response)).toBe("<html>opencode</html>")
|
||||||
expect(proxiedUrl).toBe("https://app.opencode.ai/")
|
expect(proxiedUrl).toBe("https://app.opencode.ai/")
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("strips upstream transfer encoding headers from proxied assets", async () => {
|
it.live("strips upstream transfer encoding headers from proxied assets", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
||||||
let proxiedUrl: string | undefined
|
let proxiedUrl: string | undefined
|
||||||
|
|
||||||
const response = await Effect.runPromise(
|
const response = yield* Effect.gen(function* () {
|
||||||
Effect.gen(function* () {
|
|
||||||
const fs = yield* AppFileSystem.Service
|
const fs = yield* AppFileSystem.Service
|
||||||
const client = yield* HttpClient.HttpClient
|
const client = yield* HttpClient.HttpClient
|
||||||
return yield* serveUIEffect(HttpServerRequest.fromWeb(new Request("http://localhost/assets/app.js")), {
|
return yield* serveUIEffect(HttpServerRequest.fromWeb(new Request("http://localhost/assets/app.js")), {
|
||||||
@@ -147,8 +170,6 @@ describe("HttpApi UI fallback", () => {
|
|||||||
})
|
})
|
||||||
}).pipe(
|
}).pipe(
|
||||||
Effect.provide(
|
Effect.provide(
|
||||||
Layer.mergeAll(
|
|
||||||
AppFileSystem.defaultLayer,
|
|
||||||
Layer.succeed(
|
Layer.succeed(
|
||||||
HttpClient.HttpClient,
|
HttpClient.HttpClient,
|
||||||
HttpClient.make((request) => {
|
HttpClient.make((request) => {
|
||||||
@@ -168,9 +189,7 @@ describe("HttpApi UI fallback", () => {
|
|||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
Effect.map(HttpServerResponse.toWeb),
|
Effect.map(HttpServerResponse.toWeb),
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toBe(200)
|
expect(response.status).toBe(200)
|
||||||
@@ -178,17 +197,18 @@ describe("HttpApi UI fallback", () => {
|
|||||||
expect(response.headers.get("content-encoding")).toBeNull()
|
expect(response.headers.get("content-encoding")).toBeNull()
|
||||||
expect(response.headers.get("content-length")).not.toBe("999")
|
expect(response.headers.get("content-length")).not.toBe("999")
|
||||||
expect(response.headers.get("content-type")).toContain("text/javascript")
|
expect(response.headers.get("content-type")).toContain("text/javascript")
|
||||||
expect(await response.text()).toBe("console.log('ok')")
|
expect(yield* responseText(response)).toBe("console.log('ok')")
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
// Regression for #25698 (Ope): upstream `transfer-encoding: chunked` was
|
// Regression for #25698 (Ope): upstream `transfer-encoding: chunked` was
|
||||||
// forwarded through the proxy while the proxy itself re-frames the body,
|
// forwarded through the proxy while the proxy itself re-frames the body,
|
||||||
// causing browsers to fail with `ERR_INVALID_CHUNKED_ENCODING`.
|
// causing browsers to fail with `ERR_INVALID_CHUNKED_ENCODING`.
|
||||||
test("strips upstream transfer-encoding header from proxied assets", async () => {
|
it.live("strips upstream transfer-encoding header from proxied assets", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
||||||
|
|
||||||
const response = await Effect.runPromise(
|
const response = yield* Effect.gen(function* () {
|
||||||
Effect.gen(function* () {
|
|
||||||
const fs = yield* AppFileSystem.Service
|
const fs = yield* AppFileSystem.Service
|
||||||
const client = yield* HttpClient.HttpClient
|
const client = yield* HttpClient.HttpClient
|
||||||
return yield* serveUIEffect(HttpServerRequest.fromWeb(new Request("http://localhost/")), {
|
return yield* serveUIEffect(HttpServerRequest.fromWeb(new Request("http://localhost/")), {
|
||||||
@@ -197,8 +217,6 @@ describe("HttpApi UI fallback", () => {
|
|||||||
})
|
})
|
||||||
}).pipe(
|
}).pipe(
|
||||||
Effect.provide(
|
Effect.provide(
|
||||||
Layer.mergeAll(
|
|
||||||
AppFileSystem.defaultLayer,
|
|
||||||
Layer.succeed(
|
Layer.succeed(
|
||||||
HttpClient.HttpClient,
|
HttpClient.HttpClient,
|
||||||
HttpClient.make((request) =>
|
HttpClient.make((request) =>
|
||||||
@@ -216,23 +234,21 @@ describe("HttpApi UI fallback", () => {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
Effect.map(HttpServerResponse.toWeb),
|
Effect.map(HttpServerResponse.toWeb),
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(response.status).toBe(200)
|
expect(response.status).toBe(200)
|
||||||
expect(response.headers.get("transfer-encoding")).toBeNull()
|
expect(response.headers.get("transfer-encoding")).toBeNull()
|
||||||
expect(await response.text()).toBe("<html>opencode</html>")
|
expect(yield* responseText(response)).toBe("<html>opencode</html>")
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("serves embedded UI assets when Bun can read them but access reports missing", async () => {
|
it.live("serves embedded UI assets when Bun can read them but access reports missing", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
let readPath: string | undefined
|
let readPath: string | undefined
|
||||||
|
|
||||||
const response = await Effect.runPromise(
|
|
||||||
Effect.gen(function* () {
|
|
||||||
const fs = yield* AppFileSystem.Service
|
const fs = yield* AppFileSystem.Service
|
||||||
return yield* serveEmbeddedUIEffect(
|
const response = yield* serveEmbeddedUIEffect(
|
||||||
"/assets/app.js",
|
"/assets/app.js",
|
||||||
{
|
{
|
||||||
...fs,
|
...fs,
|
||||||
@@ -245,23 +261,21 @@ describe("HttpApi UI fallback", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ "assets/app.js": "/$bunfs/root/assets/app.js" },
|
{ "assets/app.js": "/$bunfs/root/assets/app.js" },
|
||||||
)
|
).pipe(Effect.map(HttpServerResponse.toWeb))
|
||||||
}).pipe(Effect.provide(AppFileSystem.defaultLayer), Effect.map(HttpServerResponse.toWeb)),
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(response.status).toBe(200)
|
expect(response.status).toBe(200)
|
||||||
expect(readPath).toBe("/$bunfs/root/assets/app.js")
|
expect(readPath).toBe("/$bunfs/root/assets/app.js")
|
||||||
expect(response.headers.get("content-type")).toContain("text/javascript")
|
expect(response.headers.get("content-type")).toContain("text/javascript")
|
||||||
expect(await response.text()).toBe("console.log('embedded')")
|
expect(yield* responseText(response)).toBe("console.log('embedded')")
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("allows embedded UI terminal wasm and theme preload CSP", async () => {
|
it.live("allows embedded UI terminal wasm and theme preload CSP", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
const script = 'document.documentElement.dataset.theme = "dark"'
|
const script = 'document.documentElement.dataset.theme = "dark"'
|
||||||
|
|
||||||
const response = await Effect.runPromise(
|
|
||||||
Effect.gen(function* () {
|
|
||||||
const fs = yield* AppFileSystem.Service
|
const fs = yield* AppFileSystem.Service
|
||||||
return yield* serveEmbeddedUIEffect(
|
const response = yield* serveEmbeddedUIEffect(
|
||||||
"/",
|
"/",
|
||||||
{
|
{
|
||||||
...fs,
|
...fs,
|
||||||
@@ -276,74 +290,84 @@ describe("HttpApi UI fallback", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ "index.html": "/$bunfs/root/index.html" },
|
{ "index.html": "/$bunfs/root/index.html" },
|
||||||
)
|
).pipe(Effect.map(HttpServerResponse.toWeb))
|
||||||
}).pipe(Effect.provide(AppFileSystem.defaultLayer), Effect.map(HttpServerResponse.toWeb)),
|
|
||||||
)
|
|
||||||
|
|
||||||
const csp = response.headers.get("content-security-policy") ?? ""
|
const csp = response.headers.get("content-security-policy") ?? ""
|
||||||
expect(csp).toContain("script-src 'self' 'wasm-unsafe-eval'")
|
expect(csp).toContain("script-src 'self' 'wasm-unsafe-eval'")
|
||||||
expect(csp).toContain(`'sha256-${createHash("sha256").update(script).digest("base64")}'`)
|
expect(csp).toContain(`'sha256-${createHash("sha256").update(script).digest("base64")}'`)
|
||||||
expect(csp).toContain("connect-src * data:")
|
expect(csp).toContain("connect-src * data:")
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("keeps matched API routes ahead of the UI fallback", async () => {
|
it.live("keeps matched API routes ahead of the UI fallback", () =>
|
||||||
const response = await Server.Default().app.request("/session/ses_nope")
|
Effect.gen(function* () {
|
||||||
|
const response = yield* Effect.promise(() => Promise.resolve(Server.Default().app.request("/session/ses_nope")))
|
||||||
|
|
||||||
expect(response.status).toBe(404)
|
expect(response.status).toBe(404)
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("requires server password for the web UI", async () => {
|
it.live("requires server password for the web UI", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
||||||
|
|
||||||
const response = await uiApp({ password: "secret", username: "opencode" }).request("/")
|
const response = yield* uiApp({ password: "secret", username: "opencode" }).request("/")
|
||||||
|
|
||||||
expect(response.status).toBe(401)
|
expect(response.status).toBe(401)
|
||||||
expect(response.headers.get("www-authenticate")).toBe('Basic realm="Secure Area"')
|
expect(response.headers.get("www-authenticate")).toBe('Basic realm="Secure Area"')
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("accepts auth token for the web UI", async () => {
|
it.live("accepts auth token for the web UI", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
||||||
|
|
||||||
const response = await uiApp({
|
const response = yield* uiApp({
|
||||||
password: "secret",
|
password: "secret",
|
||||||
username: "opencode",
|
username: "opencode",
|
||||||
client: httpClient(new Response("<html>opencode</html>", { headers: { "content-type": "text/html" } })),
|
client: httpClient(new Response("<html>opencode</html>", { headers: { "content-type": "text/html" } })),
|
||||||
}).request(`/?auth_token=${btoa("opencode:secret")}`)
|
}).request(`/?auth_token=${btoa("opencode:secret")}`)
|
||||||
|
|
||||||
expect(response.status).toBe(200)
|
expect(response.status).toBe(200)
|
||||||
expect(await response.text()).toBe("<html>opencode</html>")
|
expect(yield* responseText(response)).toBe("<html>opencode</html>")
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("accepts basic auth for the web UI", async () => {
|
it.live("accepts basic auth for the web UI", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
||||||
|
|
||||||
const response = await uiApp({ password: "secret", username: "opencode" }).request("/", {
|
const response = yield* uiApp({ password: "secret", username: "opencode" }).request("/", {
|
||||||
headers: { authorization: `Basic ${btoa("opencode:secret")}` },
|
headers: { authorization: `Basic ${btoa("opencode:secret")}` },
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(response.status).toBe(200)
|
expect(response.status).toBe(200)
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
// Regression for #25698 (Ope): the browser fetches the PWA manifest and
|
// Regression for #25698 (Ope): the browser fetches the PWA manifest and
|
||||||
// its icons via flows that don't carry app-managed credentials (the
|
// its icons via flows that don't carry app-managed credentials (the
|
||||||
// `<link rel="manifest">` request is not under page-auth control), so the
|
// `<link rel="manifest">` request is not under page-auth control), so the
|
||||||
// server returning 401 breaks PWA install. These specific public assets
|
// server returning 401 breaks PWA install. These specific public assets
|
||||||
// should bypass auth.
|
// should bypass auth.
|
||||||
test("serves the PWA manifest without auth even when a server password is set", async () => {
|
it.live("serves the PWA manifest without auth even when a server password is set", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
Flag.OPENCODE_DISABLE_EMBEDDED_WEB_UI = true
|
||||||
|
|
||||||
for (const path of ["/site.webmanifest", "/web-app-manifest-192x192.png", "/web-app-manifest-512x512.png"]) {
|
for (const path of ["/site.webmanifest", "/web-app-manifest-192x192.png", "/web-app-manifest-512x512.png"]) {
|
||||||
const response = await uiApp({
|
const response = yield* uiApp({
|
||||||
password: "secret",
|
password: "secret",
|
||||||
username: "opencode",
|
username: "opencode",
|
||||||
client: httpClient(new Response("ok")),
|
client: httpClient(new Response("ok")),
|
||||||
}).request(path)
|
}).request(path)
|
||||||
expect(response.status).not.toBe(401)
|
expect(response.status).not.toBe(401)
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("allows web UI preflight without auth", async () => {
|
it.live("allows web UI preflight without auth", () =>
|
||||||
const response = await app({ password: "secret", username: "opencode" }).request("/", {
|
Effect.gen(function* () {
|
||||||
|
const response = yield* app({ password: "secret", username: "opencode" }).request("/", {
|
||||||
method: "OPTIONS",
|
method: "OPTIONS",
|
||||||
headers: {
|
headers: {
|
||||||
origin: "http://localhost:3000",
|
origin: "http://localhost:3000",
|
||||||
@@ -353,5 +377,6 @@ describe("HttpApi UI fallback", () => {
|
|||||||
|
|
||||||
expect(response.status).toBe(204)
|
expect(response.status).toBe(204)
|
||||||
expect(response.headers.get("access-control-allow-origin")).toBe("http://localhost:3000")
|
expect(response.headers.get("access-control-allow-origin")).toBe("http://localhost:3000")
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user