import { beforeEach, describe, expect, it, jest } from "@jest/globals"
import fs from "fs"
import path from "path"
import { fileURLToPath } from "url"
import { dirname } from "path"

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const min_version = fs
  .readFileSync(path.join(__dirname, "../min_supported_version.txt"), "utf8")
  .trim()

describe("getUser (unit test)", () => {
  beforeEach(() => {
    jest.resetModules()
  })

  it("should respond with userId and min_version when cli_version is provided", async () => {
    const { getUser } = await import("../cli-get-user")

    const req = {
      headers: { cli_version: "1.0.0" },
      userId: "test-user-id",
    } as any

    const res = {
      status: jest.fn().mockReturnThis(),
      send: jest.fn(),
    } as any

    getUser(req, res)

    expect(res.status).toHaveBeenCalledWith(200)
    expect(res.send).toHaveBeenCalledWith({
      userId: "test-user-id",
      min_version: min_version,
    })
  })

  it("should respond with userId when cli_version is unknown", async () => {
    const { getUser } = await import("../cli-get-user")

    const req = {
      headers: {},
      userId: "test-user-id",
    } as any

    const res = {
      status: jest.fn().mockReturnThis(),
      send: jest.fn(),
    } as any

    getUser(req, res)

    expect(res.status).toHaveBeenCalledWith(200)
    expect(res.send).toHaveBeenCalledWith("test-user-id")
  })
})
