// src/lib/backlink/domain-profile-import.test.ts
import { describe, expect, it } from "bun:test";
import { parseDomainBacklinks } from "./domain-profile-import";

const CSV = `Page ascore,Source title,Source url,Target url,Anchor,External links,Internal links,Nofollow,Sponsored,Ugc,Text,Frame,Form,Image,Sitewide,First seen,Last seen,New link,Lost link
53,Art,https://www.trm24.fr/x/,https://aciebenergie.fr/,Joël,1,1,false,false,false,true,false,false,false,false,2025-01-15,2025-06-01,true,false
53,Art,https://www.trm24.fr/x/,https://aciebenergie.fr/,Joël,1,1,false,false,false,true,false,false,false,false,2025-01-15,2025-06-01,true,false`;

describe("parseDomainBacklinks", () => {
  it("maps rows, builds a stable id, dedupes, coerces dates", () => {
    const at = new Date("2026-06-08T00:00:00.000Z");
    const rows = parseDomainBacklinks(CSV, "site-x", at);
    expect(rows).toHaveLength(1); // duplicate (same source+target) collapsed
    const r = rows[0];
    expect(r.id.startsWith("site-x:")).toBe(true);
    expect(r.siteId).toBe("site-x");
    expect(r.sourceDomain).toBe("trm24.fr");
    expect(r.authority).toBe(53);
    expect(r.nofollow).toBe(false);
    expect(r.firstSeen instanceof Date).toBe(true);
    expect(r.importedAt).toBe(at);
  });
});
