import { createAppSettings, createJob } from "@shared/testing/factories.js";
import { describe, expect, it } from "vitest";
import { getEnabledSources, getJobCounts } from "./utils";

describe("orchestrator utils", () => {
  it("enables adzuna only when both app id and key are configured", () => {
    const withCreds = createAppSettings({
      adzunaAppId: "app-id",
      adzunaAppKeyHint: "key-",
    });
    const withoutKey = createAppSettings({
      adzunaAppId: "app-id",
      adzunaAppKeyHint: null,
    });

    expect(getEnabledSources(withCreds)).toContain("adzuna");
    expect(getEnabledSources(withoutKey)).not.toContain("adzuna");
  });

  it("enables startupjobs without credentials", () => {
    expect(getEnabledSources(createAppSettings())).toContain("startupjobs");
  });

  it("enables jobicy and themuse without credentials", () => {
    const enabled = getEnabledSources(createAppSettings());
    expect(enabled).toContain("jobicy");
    expect(enabled).toContain("themuse");
  });

  it("enables new public job boards without credentials", () => {
    const enabled = getEnabledSources(createAppSettings());
    for (const source of [
      "workingnomads",
      "testdevjobs",
      "wellfound",
      "builtin",
      "google-jobs",
    ] as const) {
      expect(enabled).toContain(source);
    }
  });

  it("enables ATS sources when company lists are configured", () => {
    const enabled = getEnabledSources(
      createAppSettings({
        teamtailorCompanies: {
          value: ["testgorilla"],
          default: [],
          override: null,
        },
        careersPageUrls: {
          value: ["https://sentry.io/careers/"],
          default: [],
          override: null,
        },
      }),
    );
    expect(enabled).toContain("teamtailor");
    expect(enabled).toContain("careerspages");
  });

  it("counts processing jobs in ready and discovered tabs", () => {
    const jobs = [
      createJob({ id: "ready", status: "ready", closedAt: null }),
      createJob({ id: "processing", status: "processing", closedAt: null }),
      createJob({ id: "discovered", status: "discovered", closedAt: null }),
      createJob({ id: "applied", status: "applied", closedAt: null }),
    ];

    expect(getJobCounts(jobs)).toEqual({
      ready: 2,
      discovered: 2,
      applied: 1,
      all: 4,
    });
  });
});
