import { describe, it, expect } from "vitest";
import { getNodeSdkConfig, setNodeSdkConfig } from "../lib/config-manager";
import { NodeSDKConfig } from "../lib";

describe("Config Manager", () => {
  it("throws if getConfig is called before initialization", () => {
    expect(() => getNodeSdkConfig()).toThrow();
  });

  it("sdk defined config is not pollutable", () => {
    const config: NodeSDKConfig = {
      collectorUrl: "http://example.com",
      serviceName: "MyService",
      spanAttributes: {
        "my.attribute": "value",
      },
    };

    setNodeSdkConfig(config);

    const cfg = getNodeSdkConfig();

    // Top level
    cfg.collectorUrl = "http://example.com/changed";
    // Subfield
    cfg.spanAttributes["my.attribute"] = "another-attribute";

    // Ensure config values remain unchanged
    expect(getNodeSdkConfig().collectorUrl).toStrictEqual(config.collectorUrl);
    expect(getNodeSdkConfig().spanAttributes["my.attribute"]).toStrictEqual(
      config.spanAttributes["my.attribute"],
    );
  });
});
