import path from "node:path";

import { describe, expect, it } from "vitest";

import {
  inferSearchTaskProfileDirFromArtifactPath,
  resolve1688AccountProfileDir,
  resolve1688AccountProfileRoot,
  resolve1688ProfileDir,
} from "./profile-dir.js";

describe("1688 profile dir resolution", () => {
  it("uses an external long-lived default account profile root", () => {
    expect(resolve1688AccountProfileRoot({
      env: {},
      homeDir: "/Users/searchbot",
    })).toBe(path.join("/Users/searchbot", ".1688-autoprocurement", "1688-profiles"));

    expect(resolve1688AccountProfileDir({
      env: {},
      homeDir: "/Users/searchbot",
      accountId: "account-001",
    })).toBe(path.join("/Users/searchbot", ".1688-autoprocurement", "1688-profiles", "account-001"));
  });

  it("lets each Mac server override the long-lived profile root", () => {
    expect(resolve1688AccountProfileDir({
      env: {
        ALPHAPULSE_1688_PROFILE_ROOT: "~/server-a/1688-profiles",
      },
      homeDir: "/Users/searchbot",
      accountId: "account-002",
    })).toBe(path.join("/Users/searchbot", "server-a", "1688-profiles", "account-002"));
  });

  it("keeps explicit single-profile overrides ahead of account-root resolution", () => {
    expect(resolve1688AccountProfileDir({
      explicitProfileDir: "/tmp/manual-profile",
      env: {
        ALPHAPULSE_1688_PROFILE_ROOT: "/tmp/root",
        SEARCH_1688_PROFILE_DIR: "/tmp/search-profile",
      },
      accountId: "account-003",
    })).toBe("/tmp/manual-profile");

    expect(resolve1688AccountProfileDir({
      env: {
        ALPHAPULSE_1688_PROFILE_ROOT: "/tmp/root",
        SEARCH_1688_PROFILE_DIR: "/tmp/search-profile",
      },
      accountId: "account-003",
    })).toBe("/tmp/search-profile");
  });

  it("sanitizes account ids before using them as profile path segments", () => {
    expect(resolve1688AccountProfileDir({
      env: {
        ALPHAPULSE_1688_PROFILE_ROOT: "/tmp/root",
      },
      accountId: "../account:004 中文",
    })).toBe(path.join("/tmp/root", "account_004"));
  });

  it("infers the task runtime profile from an llm-match-results artifact path", () => {
    const llmJson = path.join(
      "/repo",
      "automation",
      ".runtime",
      "search-tasks",
      "CG-20260429-001-ss-011",
      "output",
      "search-plan",
      "run-2026-05-05T14-22-08-752Z",
      "llm-match-results.json",
    );

    expect(inferSearchTaskProfileDirFromArtifactPath(llmJson)).toBe(path.join(
      "/repo",
      "automation",
      ".runtime",
      "search-tasks",
      "CG-20260429-001-ss-011",
      "profile",
    ));
  });

  it("uses explicit profile before env and inferred task profile", () => {
    expect(resolve1688ProfileDir({
      explicitProfileDir: "/tmp/explicit-profile",
      envProfileDir: "/tmp/env-profile",
      artifactPath: path.join("/repo", "automation", ".runtime", "search-tasks", "task-1", "output", "llm-match-results.json"),
      allowLegacyFallback: false,
    })).toBe("/tmp/explicit-profile");
  });

  it("uses env profile before inferred task profile", () => {
    expect(resolve1688ProfileDir({
      envProfileDir: "/tmp/env-profile",
      artifactPath: path.join("/repo", "automation", ".runtime", "search-tasks", "task-1", "output", "llm-match-results.json"),
      allowLegacyFallback: false,
    })).toBe("/tmp/env-profile");
  });

  it("returns an empty string instead of legacy profile when fallback is disabled", () => {
    expect(resolve1688ProfileDir({
      allowLegacyFallback: false,
      envProfileDir: "",
    })).toBe("");
  });
});
