import { describe, test, expect, spyOn, beforeEach, afterEach } from "bun:test";
import { SandboxJiraClient } from "@/jira/client.js";
import { mockLogger } from "../_helpers/mock-logger.js";

const logger = mockLogger();

function makeClient(): SandboxJiraClient {
  return new SandboxJiraClient("mydomain", "test@example.com", "token123", logger);
}

describe("SandboxJiraClient – no-op writes", () => {
  let fetchSpy: ReturnType<typeof spyOn>;

  beforeEach(() => {
    // Sandbox writes must never hit the network; stub fetch so an accidental call
    // resolves harmlessly (the tests assert it is never invoked).
    fetchSpy = spyOn(globalThis, "fetch").mockResolvedValue(new Response(null));
  });

  afterEach(() => {
    fetchSpy.mockRestore();
  });

  test("transitionIssue does not call fetch", async () => {
    const client = makeClient();
    await client.transitionIssue("PROJ-1", "In Progress");
    expect(fetchSpy).not.toHaveBeenCalled();
  });

  test("addComment does not call fetch and returns stub { id }", async () => {
    const client = makeClient();
    const result = await client.addComment("PROJ-1", "some comment");
    expect(fetchSpy).not.toHaveBeenCalled();
    expect(result).toEqual({ id: "sandbox-stub" });
  });

  test("createSubtask does not call fetch and returns { id, key: parentKey }", async () => {
    const client = makeClient();
    const result = await client.createSubtask("PARENT-1", "Fix bug", "account123");
    expect(fetchSpy).not.toHaveBeenCalled();
    expect(result).toEqual({ id: "sandbox-stub", key: "PARENT-1" });
  });

  test("createSubtask returns parent key regardless of componentIds", async () => {
    const client = makeClient();
    const result = await client.createSubtask("BUG-42", "Do thing", "account456", [{ id: "c1" }]);
    expect(result.key).toBe("BUG-42");
  });
});
