sailfork / t3code-github / packages/ provider-opencode/ src/ server/ probeResponses.fixture.ts

import * as Effect from "effect/Effect";
import * as HttpClient from "effect/http/HttpClient";
import * as HttpClientResponse from "effect/http/HttpClientResponse";

interface RecordedResponse {
  readonly status: number;
  readonly contentType?: string;
  readonly body: string;
}

// Unknown paths serve SPA HTML; content type alone rejects these truncated bodies.
const SPA_HTML =
  '<!doctype html>\n<html lang="en" style="background-color: var(--v2-background-bg-deep, #fafafa)">\n';

export const OPENCODE_2_RESPONSES = {
  "/api/info": {
    status: 200,
    contentType: "application/json",
    body: '{"version":"2.0.18","pid":2393556,"urls":["http://127.0.0.1:48771"],"paths":{"tmp":"/tmp/opencode"}}',
  },
  "/global/health": { status: 200, contentType: "text/html", body: SPA_HTML },
  unauthorized: {
    status: 401,
    contentType: "application/json",
    body: '{"_tag":"UnauthorizedError","message":"Authentication required"}',
  },
} satisfies Record<string, RecordedResponse>;

export const OPENCODE_2_WORKSPACE_RESPONSES = {
  "/api/command": {
    status: 200,
    contentType: "application/json",
    body: '{"location":{"directory":"/work"},"data":[{"name":"init","description":"guided AGENTS.md setup"},{"name":"review","description":"review changes [commit|branch|pr], defaults to uncommitted"},{"name":"bee","description":"B command"}]}',
  },
  "/api/skill": {
    status: 200,
    contentType: "application/json",
    body: '{"location":{"directory":"/work"},"data":[{"id":"opencode","name":"OpenCode","description":"Use this skill for any question about OpenCode itself.","path":"/builtin/opencode.md","content":"# OpenCode"},{"id":"report","name":"Report","description":"Use when the user wants to report an opencode issue or bug.","path":"/builtin/report.md","content":"<!-- Built-in skill. -->"},{"id":"plum","name":"plum","description":"plum skill","path":"/work/.opencode/skills/plum/SKILL.md","content":"Say PLUM."}]}',
  },
} satisfies Record<string, RecordedResponse>;

export const OPENCODE_1_RESPONSES = {
  "/api/info": { status: 200, contentType: "text/html", body: SPA_HTML },
  "/global/health": {
    status: 200,
    contentType: "application/json",
    body: '{"healthy":true,"version":"1.18.32"}',
  },
  // 1.x also answers a wrong password with an empty 401 on every path.
  unauthorized: { status: 401, body: "" },
} satisfies Record<string, RecordedResponse>;

export function replayOpenCodeServer(
  responses:
    | typeof OPENCODE_1_RESPONSES
    | typeof OPENCODE_2_RESPONSES
    | (typeof OPENCODE_2_RESPONSES & typeof OPENCODE_2_WORKSPACE_RESPONSES),
  password: string,
  requestedPaths: Array<string> = [],
) {
  const answers: Record<string, RecordedResponse | undefined> = responses;
  // Both versions decode Basic credentials as UTF-8.
  const expected = password
    ? `Basic ${Buffer.from(`opencode:${password}`, "utf8").toString("base64")}`
    : undefined;
  return HttpClient.make((request) => {
    const path = new URL(request.url).pathname;
    requestedPaths.push(path);
    const recorded: RecordedResponse =
      request.headers.authorization === expected
        ? (answers[path] ?? responses.unauthorized)
        : responses.unauthorized;
    return Effect.succeed(
      HttpClientResponse.fromWeb(
        request,
        new Response(recorded.body, {
          status: recorded.status,
          headers: recorded.contentType ? { "content-type": recorded.contentType } : {},
        }),
      ),
    );
  });
}