import * as cut from "./llm-response-adapter";

describe("LLMResponseAdapter", () => {
  it("should be implemented", () => {
    expect(cut.LlmResponseAdapter).toBeDefined();
    expect(cut.LlmResponseAdapter.prototype.extractContent).toBeDefined();
    expect(typeof cut.LlmResponseAdapter.prototype.extractContent).toBe(
      "function"
    );
  });

  it("should adapt response as string", () => {
    const rawResponse = {
      message: { role: "assistant", content: "Hello, world!" },
      usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 },
      raw: {},
    };

    const adapter = new cut.LlmResponseAdapter(rawResponse);

    const adaptedResponse = adapter.extractContent();

    expect(adaptedResponse).toBe("Hello, world!");
    expect(typeof adaptedResponse).toBe("string");
    expect(adapter).toBeInstanceOf(cut.LlmResponseAdapter);
  });

  it("should adapt response as json object", () => {
    const rawResponse = {
      message: {
        role: "assistant",
        content: "```json " + JSON.stringify({ foo: "Hello, world!" }) + "```",
      },
      usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 },
      raw: {},
    };

    const adapter = new cut.LlmResponseAdapter(rawResponse);

    const adaptedResponse = adapter.extractContent();

    expect(adaptedResponse).toStrictEqual({ foo: "Hello, world!" });
    expect(typeof adaptedResponse).toBe("object");
    expect(adapter).toBeInstanceOf(cut.LlmResponseAdapter);
  });

  it("should adapt response as markdown", () => {
    const rawResponse = {
      message: { role: "assistant", content: "```markdown ###Hello, world!```" },
      usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 },
      raw: {},
    };

    const adapter = new cut.LlmResponseAdapter(rawResponse);

    const adaptedResponse = adapter.extractContent();

    expect(adaptedResponse).toBe("###Hello, world!");
    expect(typeof adaptedResponse).toBe("string");
    expect(adapter).toBeInstanceOf(cut.LlmResponseAdapter);
  });
});
