// The entry-detail provenance badge must name the software the history
// ACTUALLY came from. Balanced Sum restores from any accounting software,
// so a Sage BusinessWorks user drilling into her own migrated entry must
// never be told it was "Imported from QuickBooks" — and a QuickBooks user
// must still see QuickBooks. Both directions are asserted here, plus the
// honest fallback for a source that identifies no specific product.
//
// The source strings under test are exactly what the pipelines write:
// 'quickbooks_journal' (tools/lib/journal-load.ts's default) and
// 'universal:<slug>' (server/src/universalImport.ts, slug = the preset key
// the user picked in web/src/setup/UniversalWizard.tsx).

import { describe, it, expect } from "vitest";
import {
  importSourceName,
  importedFromLabel,
  UNKNOWN_SOURCE_NAME,
} from "../../web/src/lib/importSource.js";
import { UNIVERSAL_PRESETS } from "../../tools/universal-presets.js";

describe("importSourceName", () => {
  it("names QuickBooks for the QuickBooks Journal restore", () => {
    expect(importSourceName("quickbooks_journal")).toBe("QuickBooks");
  });

  it("names the product a universal preset identifies", () => {
    expect(importSourceName("universal:sage-businessworks")).toBe("Sage BusinessWorks");
    expect(importSourceName("universal:xero")).toBe("Xero");
    expect(importSourceName("universal:wave")).toBe("Wave");
    expect(importSourceName("universal:freshbooks")).toBe("FreshBooks");
    expect(importSourceName("universal:sage-50")).toBe("Sage 50");
  });

  it("never claims QuickBooks for a non-QuickBooks source", () => {
    for (const source of [
      "universal:sage-businessworks",
      "universal:generic-debit-credit",
      "universal:generic-signed",
      "universal:import",
      "universal:something-we-have-never-seen",
      "adp_gl",
      null,
      undefined,
      "",
    ]) {
      expect(importSourceName(source)).not.toContain("QuickBooks");
    }
  });

  it("falls back to honest generic wording rather than guessing a product", () => {
    // The two generic presets mean "something else" by definition, and the
    // 'import' slug is universalImport.ts's DEFAULT_SLUG.
    expect(importSourceName("universal:generic-debit-credit")).toBe(UNKNOWN_SOURCE_NAME);
    expect(importSourceName("universal:generic-signed")).toBe(UNKNOWN_SOURCE_NAME);
    expect(importSourceName("universal:import")).toBe(UNKNOWN_SOURCE_NAME);
    expect(importSourceName("universal:whatever-comes-next")).toBe(UNKNOWN_SOURCE_NAME);
    expect(importSourceName(null)).toBe(UNKNOWN_SOURCE_NAME);
    expect(importSourceName(undefined)).toBe(UNKNOWN_SOURCE_NAME);
  });

  it("covers every shipped non-generic preset key", () => {
    // Guards against a new product preset landing in tools/universal-presets.ts
    // while its entries keep saying "your old software".
    for (const preset of UNIVERSAL_PRESETS) {
      if (preset.key.startsWith("generic-")) continue;
      expect(importSourceName(`universal:${preset.key}`)).not.toBe(UNKNOWN_SOURCE_NAME);
    }
  });
});

describe("importedFromLabel", () => {
  it("reads as a sentence a person would write", () => {
    expect(importedFromLabel("quickbooks_journal", "journal-2019.xlsx")).toBe(
      "Imported from QuickBooks — journal-2019.xlsx",
    );
    expect(importedFromLabel("universal:sage-businessworks", "gl-2019.csv")).toBe(
      "Imported from Sage BusinessWorks — gl-2019.csv",
    );
    expect(importedFromLabel("universal:generic-signed", "ledger.csv")).toBe(
      "Imported from your old software — ledger.csv",
    );
  });
});
