import { describe, test, assert } from "vitest";
import { parseLog } from "../utils/alloy-log-parser";
import { readFile } from "node:fs/promises";
import { join } from "node:path";

describe("pii integration test", () => {
  test("should check redacted attributes and detect zero emails in logs", async () => {
    const data = await readFile(join(__dirname, "logs.txt"), "utf-8");

    let health_traces_counter = 0;
    let dummy_traces_counter = 0;
    let email_not_redacted = 0;

    for (const line of data.split(/\nts=/)) {
      const parsedLine: Record<string, object | string | number> =
        parseLog(line);

      if (
        parsedLine["attributes"] &&
        parsedLine["attributes"]["span_kind"] &&
        parsedLine["attributes"]["span_kind"] === "log"
      ) {
        const matches = (parsedLine["log_body"] as string).match(
          /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g,
        );
        if (matches) {
          email_not_redacted++;
        }
      }

      if (
        parsedLine["attributes"] &&
        parsedLine["attributes"]["span_kind"] &&
        parsedLine["attributes"]["span_kind"] === "trace"
      ) {
        if (parsedLine["attributes"]["http.target"] === "/api/dummy") {
          dummy_traces_counter++;

          // verify global sdk span resource
          assert.equal(
            parsedLine["resource_attributes"]["email"],
            "[REDACTED EMAIL]",
          );

          // verify global sdk span attributes
          assert.equal(parsedLine["attributes"]["email"], "[REDACTED EMAIL]");
          // verify custom runtime span attributes
          assert.equal(
            parsedLine["attributes"]["multiple.email"],
            "[REDACTED EMAIL]",
          );

          // verify global sdk span attributes
          assert.equal(parsedLine["events"]["email"], "[REDACTED EMAIL]");

          continue;
        }
        if (parsedLine["attributes"]["http.target"] === "/api/health") {
          health_traces_counter++;
        }
      }
    }

    assert.equal(email_not_redacted, 0);
    assert.equal(health_traces_counter, 0);
    assert.equal(dummy_traces_counter, 2);
  });
});
