/**
 * @file Pins the URL and method `api.lanes.git` hits. The card renders whatever
 * this returns, so a wrong path would surface as a permanently missing git row
 * rather than as a visible failure — worth a test even though it is one line.
 * @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
 */

import { describe, it, expect, vi, afterEach } from "vitest";
import { api } from "../api";

afterEach(() => {
  vi.unstubAllGlobals();
});

function stubFetch(body: unknown) {
  const spy = vi.fn().mockResolvedValue({
    ok: true,
    status: 200,
    headers: { get: () => "application/json" },
    json: async () => body,
    text: async () => JSON.stringify(body),
  });
  vi.stubGlobal("fetch", spy);
  return spy;
}

describe("api.lanes.git", () => {
  it("requests /api/lanes/<id>/git and returns the parsed facts", async () => {
    const facts = {
      available: true,
      branch: "feat/x",
      head: "abc1234",
      subject: "do a thing",
      dirty: 2,
      untracked: 1,
    };
    const spy = stubFetch(facts);

    const result = await api.lanes.git(3);

    expect(spy).toHaveBeenCalledTimes(1);
    const call = spy.mock.calls[0] as [string, RequestInit | undefined];
    // endsWith, not toContain: `/gitt` contains `/git` and would slip through.
    expect(String(call[0]).endsWith("/api/lanes/3/git")).toBe(true);
    expect(call[1]?.method ?? "GET").toBe("GET");
    expect(result).toEqual(facts);
  });

  it("passes an available:false body straight through", async () => {
    stubFetch({ available: false });
    expect(await api.lanes.git(9)).toEqual({ available: false });
  });
});
