Open source alternative to Semrush and Ahrefs
TypeScript backlink-analysisgoogle-search-console-mcpkeyword-researchmcpseoseo-agentseo-auditseo-automationseo-skillsseo-toolssite-audit
import { sort } from "remeda";
import { describe, expect, it } from "vitest";
import {
LOCATION_OPTIONS,
formatLocationLabel,
getIsoCountryCode,
getKeywordDataProvider,
getLanguageCode,
isLabsLocationCode,
isSupportedLanguageCode,
isSupportedLocationCode,
resolveKeywordDataLanguage,
resolveLabsMarket,
resolveMarket,
} from "./keyword-locations";
describe("keyword locations", () => {
it("routes Google-Ads-only countries to google_ads and unknown codes to labs", () => {
expect(getKeywordDataProvider(2352)).toBe("google_ads"); // Iceland
expect(isSupportedLocationCode(2352)).toBe(true);
expect(isLabsLocationCode(2352)).toBe(false);
expect(getLanguageCode(2352)).toBe("is");
// Labs rejects unknown codes upstream.
expect(getKeywordDataProvider(999999)).toBe("labs");
expect(isSupportedLocationCode(999999)).toBe(false);
});
it("accepts every supported language code and rejects unknown ones", () => {
// Every per-country default we send is, by construction, a supported code.
for (const option of LOCATION_OPTIONS) {
expect(isSupportedLanguageCode(option.languageCode)).toBe(true);
}
expect(isSupportedLanguageCode("en")).toBe(true);
expect(isSupportedLanguageCode("zh-TW")).toBe(true);
// Non-default codes from the master picker list are valid too (e.g. Hindi).
expect(isSupportedLanguageCode("hi")).toBe(true);
// Malformed/unsupported codes DataForSEO would reject as a charged failure.
expect(isSupportedLanguageCode("english")).toBe(false);
expect(isSupportedLanguageCode("en-US")).toBe(false);
expect(isSupportedLanguageCode("zh-tw")).toBe(false);
});
it("keeps the picker sorted alphabetically with unique codes", () => {
const labels = LOCATION_OPTIONS.map((option) => option.label);
expect(labels).toEqual(sort(labels, (a, b) => a.localeCompare(b)));
const codes = LOCATION_OPTIONS.map((option) => option.code);
expect(new Set(codes).size).toBe(codes.length);
});
});
describe("getIsoCountryCode", () => {
it("maps the UK display label to its ISO code gb", () => {
expect(getIsoCountryCode(2826)).toBe("gb");
});
});
describe("formatLocationLabel", () => {
it("trims uneven spacing around canonical name segments and truncates to maxSegments", () => {
expect(formatLocationLabel("Portland-Auburn, ME,United States")).toBe(
"Portland-Auburn, ME, United States",
);
expect(formatLocationLabel("Springfield,Illinois,United States", 2)).toBe(
"Springfield, Illinois",
);
});
});
describe("resolveMarket", () => {
const vietnamProject = { locationCode: 2704, languageCode: "vi" };
it("snaps the language to a location override instead of borrowing the project's", () => {
// A Vietnam project querying Germany must not send Vietnamese.
expect(resolveMarket({ locationCode: 2276 }, vietnamProject)).toEqual({
locationCode: 2276,
languageCode: "de",
});
});
it("keeps the project language when the override matches the project location", () => {
const spanishUs = { locationCode: 2840, languageCode: "es" };
expect(resolveMarket({ locationCode: 2840 }, spanishUs)).toEqual({
locationCode: 2840,
languageCode: "es",
});
});
});
describe("resolveLabsMarket", () => {
it("inherits a Labs-served project market like resolveMarket does", () => {
expect(
resolveLabsMarket({}, { locationCode: 2704, languageCode: "vi" }),
).toEqual({ locationCode: 2704, languageCode: "vi" });
});
it("falls back to the US when the project market is Google-Ads-served", () => {
// Iceland has no Labs data. The caller never picked it, so a Labs-only
// tool must not fail on it.
expect(
resolveLabsMarket({}, { locationCode: 2352, languageCode: "en" }),
).toEqual({ locationCode: 2840, languageCode: "en" });
});
it("falls back to the US when the project pair is not served", () => {
// Concurrent half-updates can leave a location/language pair Labs rejects;
// sending it would spend credits on a task that always fails.
expect(
resolveLabsMarket({}, { locationCode: 2276, languageCode: "vi" }),
).toEqual({ locationCode: 2840, languageCode: "en" });
});
it("leaves an explicit location alone so the caller's assert can reject it", () => {
expect(
resolveLabsMarket(
{ locationCode: 2352 },
{ locationCode: 2704, languageCode: "vi" },
),
).toMatchObject({ locationCode: 2352 });
});
});
describe("resolveKeywordDataLanguage", () => {
it("falls back to the country default for a SERP-only pair", () => {
expect(resolveKeywordDataLanguage(2840, "es")).toBe("es");
// Rank tracking can track English in Czechia; Labs would charge and fail.
expect(resolveKeywordDataLanguage(2203, "en")).toBe("cs");
// Google-Ads countries keep their single default too.
expect(resolveKeywordDataLanguage(2352, "en")).toBe("is");
});
});