import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";

const content = readFileSync(new URL("./content-linkedin.js", import.meta.url), "utf8");
const background = readFileSync(new URL("./background.js", import.meta.url), "utf8");
const service = readFileSync(
  new URL("../social-autoposter/src/service-entry.js", import.meta.url),
  "utf8",
);

test("View Profiles uses rendered navigation instead of the retired API write", () => {
  const dashboardStart = content.indexOf('case "li:run-view-profiles"');
  const dashboardEnd = content.indexOf('case "li:run-add-connections"', dashboardStart);
  const dashboardFlow = content.slice(dashboardStart, dashboardEnd);
  const panelStart = content.indexOf('"view-profiles":');
  const panelEnd = content.indexOf('"connect-commenters":', panelStart);
  const panelFlow = content.slice(panelStart, panelEnd);

  assert.match(dashboardFlow, /viewLinkedInProfileViaDom\(slug\)/);
  assert.match(panelFlow, /viewLinkedInProfileViaDom\(slug\)/);
  assert.doesNotMatch(dashboardFlow, /linkedin:http-view-profile/);
  assert.doesNotMatch(panelFlow, /linkedin:http-view-profile/);
});

test("background profile views stay inside the minimized worker window and require confirmation", () => {
  const start = background.indexOf('msg.action === "li-view-profile-dom-bg"');
  const end = background.indexOf("// r50", start);
  const flow = background.slice(start, end);

  assert.match(flow, /getOrCreateWorkerTab\(\)/);
  assert.match(flow, /windowId: coordinator\.windowId/);
  assert.match(flow, /"li:confirm-profile-view"/);
  assert.match(flow, /await chrome\.tabs\.remove\(viewTab\.id\)/);
});

test("queued profile-view probes navigate and use the same DOM confirmation", () => {
  const start = background.indexOf('if (action === "li:view-profile")');
  const end = background.indexOf('if (action === "li:ping")', start);
  const flow = background.slice(start, end);

  assert.match(flow, /navigateLinkedInTab/);
  assert.match(flow, /"li:confirm-profile-view"/);
  assert.doesNotMatch(flow, /linkedin:http-view-profile/);
});

test("content confirmation verifies the exact profile slug and rendered controls", () => {
  const start = content.indexOf("async function handleConfirmProfileView");
  const end = content.indexOf("/** Message connections", start);
  const flow = content.slice(start, end);

  assert.match(flow, /landedSlug !== expectedSlug/);
  assert.match(flow, /waitForProfileControls\(null, 10_000\)/);
  assert.match(flow, /reason: "profile_not_rendered"/);
  assert.match(flow, /confirmed: true/);
});

test("metadata lookup no longer calls LinkedIn's retired profileView URL", () => {
  const start = service.indexOf('case "linkedin:http-view-profile"');
  const end = service.indexOf('case "linkedin:http-company-posts"', start);
  const flow = service.slice(start, end);

  assert.match(flow, /lookupLinkedInProfile\(session, slug\)/);
  assert.doesNotMatch(flow, /fetch\([^\n]*profileView/);
});
