//  @ts-ignore
import reportErrorToRollbar from "./rollbar.mjs";
import provideConfig from "./provideConfig";
import { ResolvedEmbeddableConfig } from "./defineConfig";
import Rollbar from "rollbar";
import * as path from "node:path";
import * as fs from "node:fs/promises";
import { jwtDecode } from "jwt-decode";

const config = {
  applicationEnvironment: "test",
  client: {
    rootDir: "rootDir",
  },
};

vi.mock("node:fs/promises", () => ({
  writeFile: vi.fn(),
  readFile: vi.fn(),
}));

vi.mock("./provideConfig", () => ({
  default: vi.fn(),
}));

vi.mock("jwt-decode", () => ({
  jwtDecode: vi.fn(),
}));

vi.mock("node:path", async () => {
  const actual = await vi.importActual("path");
  return {
    ...actual,
    resolve: vi.fn(),
  };
});

// mock rollback constructor
vi.mock("rollbar", () => ({
  default: vi.fn(),
}));

vi.mock("resolvedPath", () => ({
  default: vi.fn(),
  devDependencies: {
    "@embeddable.com/sdk-utils": "0.1.2",
  },
  dependencies: {
    "@embeddable.com/core": "1.0.0",
    "@embeddable.com/sdk-core": "1.2.3",
  },
}));

const rollbarInstanceMock = {
  error: vi.fn(),
  configure: vi.fn(),
};

describe("rollbar", () => {
  beforeEach(() => {
    vi.mocked(provideConfig).mockResolvedValue(
      config as ResolvedEmbeddableConfig,
    );
    vi.mocked(Rollbar).mockReturnValue(rollbarInstanceMock as any);
    vi.mocked(path.resolve).mockReturnValue("resolvedPath");

    vi.mocked(fs.readFile).mockResolvedValue("{}");
    vi.mocked(jwtDecode).mockReturnValue({ sub: "test", iss: "iss" });
  });

  it("should call Rollbar.error with the error", async () => {
    const error = new Error("test error");
    await reportErrorToRollbar(error);

    expect(rollbarInstanceMock.configure).toHaveBeenCalledWith({
      payload: {
        person: {
          id: "iss@test",
        },
      },
    });

    expect(rollbarInstanceMock.error).toHaveBeenCalledWith(error, {
      custom: {
        code_version: '{"core":"1.0.0","sdk_core":"1.2.3"}',
      },
    });
  });
});
