/**
 * Sanity tests for the web file editor route.
 *
 * Visual correctness (CodeMirror 6 syntax highlighting, CDN loading) is
 * manually verified. These tests cover:
 *
 *   1. Module shape — the default export is a Hono router.
 *   2. Auth guard  — unauthenticated GET to the edit form redirects to /login
 *      or returns an auth-related error status.
 *   3. The AI commit-message endpoint is still reachable (auth guard only —
 *      full coverage in editor-ai-commit.test.ts).
 */

import { describe, it, expect } from "bun:test";
import type { Hono } from "hono";

describe("editor module", () => {
  it("exports a Hono router as the default export", async () => {
    // Dynamic import so test isolation is clean (no side-effects at top level)
    const mod = await import("../routes/editor");
    const router = mod.default;
    // Hono routers expose .routes and .fetch
    expect(typeof router).toBe("object");
    expect(router).not.toBeNull();
    expect(typeof (router as unknown as Hono).fetch).toBe("function");
    expect(Array.isArray((router as unknown as Hono).routes)).toBe(true);
  });

  it("registers at least one route for /:owner/:repo/edit/:ref", async () => {
    const mod = await import("../routes/editor");
    const router = mod.default as unknown as Hono;
    const editRoutes = router.routes.filter(
      (r) => r.path.includes("/edit/") || r.path.includes("edit")
    );
    expect(editRoutes.length).toBeGreaterThan(0);
  });
});

describe("GET /:owner/:repo/edit/:ref — auth guard", () => {
  it("redirects or returns 401/403/404 for unauthenticated users", async () => {
    // We import the full app so the editor is mounted properly
    const { default: app } = await import("../app");
    const res = await app.request(
      "/alice/myrepo/edit/README.md",
      { method: "GET", redirect: "manual" }
    );
    // Any of these are acceptable: redirect to login, or a deny status
    const acceptable = [301, 302, 303, 307, 308, 401, 403, 404, 503];
    expect(acceptable).toContain(res.status);
    if ([302, 303, 307].includes(res.status)) {
      const loc = res.headers.get("location") || "";
      expect(loc).toContain("/login");
    }
  });
});
