import { describe, test, expect } from "bun:test";
import { createApp } from "@/server/app.js";
import { createDebugDeps } from "../_helpers/hono-app.js";

// ── Helpers ───────────────────────────────────────────────────────────────────

function makeApp() {
  const deps = createDebugDeps();
  return createApp({
    queue: deps.queue,
    jiraClient: deps.jiraClient,
    cursorCloudClient: deps.cursorCloudClient,
    pipeline: deps.pipeline,
    repoRegistry: deps.repoRegistry,
    repoSelector: deps.repoSelector,
    getGitHubToken: deps.getGitHubToken,
    jiraDomain: deps.jiraDomain,
    workdirPath: deps.workdirPath,
    githubBotUser: deps.githubBotUser,
    logger: deps.logger,
    datadogLogsClient: null,
    slackNotifier: undefined,
    slackBotToken: undefined,
    slackDailyReportChannelId: undefined,
    squadScope: [],
    botEmail: deps.botEmail,
    jiraPoller: deps.jiraPoller,
    ticketReviewPoller: deps.ticketReviewPoller,
    prCommentPoller: deps.prCommentPoller,
    prMentionPoller: deps.prMentionPoller,
    slackMentionPoller: deps.slackMentionPoller,
    dailyReportScheduler: deps.dailyReportScheduler,
  });
}

// ── Tests ─────────────────────────────────────────────────────────────────────

describe("createApp()", () => {
  test("GET /hu-agent/health returns 200 with status ok", async () => {
    const app = makeApp();
    const res = await app.request("/hu-agent/health");
    expect(res.status).toBe(200);
    const body = (await res.json()) as { status: string };
    expect(body.status).toBe("ok");
  });

  test("GET /health (without /hu-agent prefix) returns 404", async () => {
    const app = makeApp();
    const res = await app.request("/health");
    expect(res.status).toBe(404);
  });

  test("debug routes are mounted under /hu-agent/api/", async () => {
    const app = makeApp();
    const res = await app.request("/hu-agent/api/repos");
    expect(res.status).toBe(200);
    const body = (await res.json()) as { count: number };
    expect(body.count).toBe(0);
  });
});
