import { describe, expect, it } from "vitest";
import {
  extractJobLocationFromText,
  parseQaJobDetailPage,
} from "../src/detail-page.js";

const SAMPLE_JSON_LD = `<!DOCTYPE html><html><body>
<script type="application/ld+json">
{
  "@context": "https://schema.org/",
  "@type": "JobPosting",
  "title": "Sr. QA Automation Engineer",
  "description": "<p><strong>Job Location: Mumbai/Nagpur</strong></p><p>India | UK | USA</p>",
  "jobLocationType": "TELECOMMUTE"
}
</script>
</body></html>`;

describe("parseQaJobDetailPage", () => {
  it("extracts job location from JSON-LD description on the detail page", () => {
    const result = parseQaJobDetailPage(SAMPLE_JSON_LD);
    expect(result?.location).toBe("Mumbai/Nagpur");
    expect(result?.jobDescription).toContain("Mumbai/Nagpur");
  });

  it("marks posting expired when validThrough is in the past", () => {
    const html = `<script type="application/ld+json">
{"@type":"JobPosting","validThrough":"2020-01-01","title":"Old role"}
</script>`;
    expect(parseQaJobDetailPage(html)).toEqual({ expired: true });
  });

  it("extracts job location lines from plain text", () => {
    expect(
      extractJobLocationFromText(
        "Job Location: Vancouver, British Columbia, Canada",
      ),
    ).toBe("Vancouver, British Columbia, Canada");
  });
});
