import { fileDiffsToMap, isDiffContentsWellFormed } from "./diff_utils.js"
import { type FileDiffContent } from "@codeflash-ai/code-suggester/build/src/types.js"

describe("fileDiffsToMap", () => {
  it("should correctly convert an object to a Map", () => {
    const obj: Record<string, FileDiffContent> = {
      file1: {
        oldContent: "old content",
        newContent: "new content",
      },
      file2: {
        oldContent: "old content 2",
        newContent: "new content 2",
      },
    }

    const map = fileDiffsToMap(obj)

    expect(map.size).toBe(2)
    expect(map.get("file1")).toEqual({
      oldContent: "old content",
      newContent: "new content",
    })
    expect(map.get("file2")).toEqual({
      oldContent: "old content 2",
      newContent: "new content 2",
    })
  })

  it("should ignore non-object values", () => {
    const obj: Record<string, any> = {
      file1: "not an object",
      file2: {
        oldContent: "old content 2",
        newContent: "new content 2",
      },
    }

    const map = fileDiffsToMap(obj)

    expect(map.size).toBe(1)
    expect(map.get("file2")).toEqual({
      oldContent: "old content 2",
      newContent: "new content 2",
    })
  })
})
it("should return an empty Map when the input object is empty", () => {
  const obj: Record<string, FileDiffContent> = {}

  const map = fileDiffsToMap(obj)

  expect(map.size).toBe(0)
})

it("should ignore nested objects", () => {
  const obj: Record<string, any> = {
    file1: {
      oldContent: "old content",
      newContent: "new content",
      extra: { key: "value" },
    },
  }

  const map = fileDiffsToMap(obj)

  expect(map.size).toBe(1)
  expect(map.get("file1")).toEqual({
    oldContent: "old content",
    newContent: "new content",
  })
})

it("should ignore non-string values for oldContent and newContent", () => {
  const obj: Record<string, any> = {
    file1: {
      oldContent: 123,
      newContent: true,
    },
  }

  const map = fileDiffsToMap(obj)

  expect(map.size).toBe(0)
})

// Add tests for isDiffContentsWellFormed function
describe("isDiffContentsWellFormed", () => {
  it("should return false for non-object input", () => {
    const diffContents = "not an object"
    expect(isDiffContentsWellFormed(diffContents)).toBe(false)
  })

  it("should return false for non-object values in the input object", () => {
    const diffContents = { file1: "not an object" }
    expect(isDiffContentsWellFormed(diffContents)).toBe(false)
  })

  it("should return false for objects that lack the oldContent or newContent properties", () => {
    const diffContents = { file1: { notOldContent: "old content", notNewContent: "new content" } }
    expect(isDiffContentsWellFormed(diffContents)).toBe(false)
  })

  it("should return false for oldContent or newContent properties that are not strings", () => {
    const diffContents = { file1: { oldContent: 123, newContent: true } }
    expect(isDiffContentsWellFormed(diffContents)).toBe(false)
  })
})
