import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { parseResponse } from "./api.js";

describe("api parseResponse", () => {
  it("treats no-content JSON responses as empty success instead of parsing an empty body", async () => {
    const response = new Response(null, {
      status: 204,
      headers: {
        "content-type": "application/json"
      }
    });

    assert.equal(await parseResponse(response), null);
  });

  it("parses normal JSON response bodies", async () => {
    const response = new Response(JSON.stringify({ ok: true }), {
      status: 200,
      headers: {
        "content-type": "application/json"
      }
    });

    assert.deepEqual(await parseResponse(response), { ok: true });
  });

  it("keeps backend error details when JSON error responses include detail", async () => {
    const response = new Response(JSON.stringify({ detail: "组织节点不存在" }), {
      status: 404,
      headers: {
        "content-type": "application/json"
      }
    });

    await assert.rejects(() => parseResponse(response), /组织节点不存在/);
  });
});
