2024-05-13 23:42:58 +02:00
|
|
|
import { container } from "tsyringe";
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
2024-05-21 19:59:04 +02:00
|
|
|
import type { LocaleService } from "@spt/services/LocaleService";
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
describe("LocaleService", () =>
|
|
|
|
{
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
|
|
let localeService: any; // Using "any" to access private/protected methods without type errors.
|
|
|
|
|
|
|
|
beforeEach(() =>
|
|
|
|
{
|
|
|
|
localeService = container.resolve<LocaleService>("LocaleService");
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() =>
|
|
|
|
{
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
2024-05-14 11:26:07 +02:00
|
|
|
describe("getLocaleDb", () =>
|
|
|
|
{
|
|
|
|
it("should return 'en' globals data when no desired locale found and display warning'", () =>
|
|
|
|
{
|
|
|
|
vi.spyOn(localeService, "getDesiredGameLocale").mockReturnValue({
|
2024-05-20 10:54:40 +02:00
|
|
|
undefined,
|
2024-05-14 11:26:07 +02:00
|
|
|
});
|
|
|
|
|
2024-05-20 10:54:40 +02:00
|
|
|
const warningLogSpy = vi.spyOn(localeService.logger, "warning");
|
2024-05-14 11:26:07 +02:00
|
|
|
|
|
|
|
const result = localeService.getLocaleDb();
|
2024-05-20 10:54:40 +02:00
|
|
|
expect(result["54cb50c76803fa8b248b4571 FirstName"]).equals("Pavel Yegorovich");
|
2024-05-14 11:26:07 +02:00
|
|
|
|
|
|
|
expect(warningLogSpy).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getDesiredGameLocale", () =>
|
|
|
|
{
|
|
|
|
it("should return gameLocale property from config when value is not 'system''", () =>
|
|
|
|
{
|
|
|
|
localeService.localeConfig.gameLocale = "test";
|
|
|
|
|
|
|
|
expect(localeService.getDesiredGameLocale()).toBe("test");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return desired value when gameLocale property from config is 'system'", () =>
|
2024-05-20 10:54:40 +02:00
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
|
|
|
vi.spyOn(localeService, "getPlatformForClientLocale").mockReturnValue(
|
|
|
|
"desiredLocale",
|
|
|
|
);
|
|
|
|
|
|
|
|
localeService.localeConfig.gameLocale = "system";
|
|
|
|
|
|
|
|
expect(localeService.getDesiredGameLocale()).toBe("desiredLocale");
|
|
|
|
});
|
|
|
|
});
|
2024-05-14 11:26:07 +02:00
|
|
|
|
|
|
|
describe("getDesiredServerLocale", () =>
|
2024-05-20 10:54:40 +02:00
|
|
|
{
|
|
|
|
it("should return serverLocale property from config when value is not 'system''", () =>
|
2024-05-14 11:26:07 +02:00
|
|
|
{
|
2024-05-20 10:54:40 +02:00
|
|
|
localeService.localeConfig.serverLocale = "test";
|
|
|
|
|
|
|
|
expect(localeService.getDesiredServerLocale()).toBe("test");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return desired value when serverLocale property from config is 'system'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
|
|
|
vi.spyOn(localeService, "getPlatformForServerLocale").mockReturnValue(
|
|
|
|
"desiredLocale",
|
|
|
|
);
|
|
|
|
|
|
|
|
localeService.localeConfig.serverLocale = "system";
|
|
|
|
|
|
|
|
expect(localeService.getDesiredServerLocale()).toBe("desiredLocale");
|
|
|
|
});
|
|
|
|
});
|
2024-05-14 11:26:07 +02:00
|
|
|
|
|
|
|
describe("getPlatformForServerLocale", () =>
|
|
|
|
{
|
|
|
|
it("should return 'en' when no system locale found and display warning'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
undefined,
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
|
|
|
|
2024-05-14 16:45:56 +02:00
|
|
|
const warningLogSpy = vi.spyOn(localeService.logger, "warning");
|
2024-05-14 11:26:07 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("en");
|
|
|
|
expect(warningLogSpy).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 'en' when unsupported system local encountered and display warning'", () =>
|
2024-05-20 10:54:40 +02:00
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
|
|
|
{ baseName: "test_locale_that_doesnt_exist", language: "test_language" },
|
|
|
|
);
|
|
|
|
|
|
|
|
const warningLogSpy = vi.spyOn(localeService.logger, "warning");
|
2024-05-14 11:26:07 +02:00
|
|
|
|
2024-05-20 10:54:40 +02:00
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("en");
|
|
|
|
expect(warningLogSpy).toHaveBeenCalledTimes(1);
|
2024-05-14 11:26:07 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getPlatformForClientLocale", () =>
|
|
|
|
{
|
|
|
|
it("should return 'en' when no platform locale found and display warning'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
undefined,
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-20 10:54:40 +02:00
|
|
|
const warningLogSpy = vi.spyOn(localeService.logger, "warning");
|
2024-05-14 11:26:07 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("en");
|
|
|
|
expect(warningLogSpy).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 'en' when unsupported platform local encountered and display warning'", () =>
|
2024-05-20 10:54:40 +02:00
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
|
|
|
localeService.getPlatformLocale = () =>
|
2024-05-14 11:26:07 +02:00
|
|
|
{
|
2024-05-20 10:54:40 +02:00
|
|
|
return { baseName: "test_locale_that_doesnt_exist", language: "test_language" };
|
|
|
|
};
|
2024-05-14 11:26:07 +02:00
|
|
|
|
2024-05-20 10:54:40 +02:00
|
|
|
const warningLogSpy = vi.spyOn(localeService.logger, "warning");
|
|
|
|
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("en");
|
|
|
|
expect(warningLogSpy).toHaveBeenCalledTimes(1);
|
2024-05-14 11:26:07 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-05-13 23:42:58 +02:00
|
|
|
describe("ValidateServerLocale", () =>
|
|
|
|
{
|
|
|
|
it("should return 'en' for 'en-US'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
2024-05-14 11:26:07 +02:00
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
new Intl.Locale("en-US"),
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("en");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 'ko' for 'ko-KR'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
2024-05-14 11:26:07 +02:00
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
new Intl.Locale("ko-KR"),
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("ko");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 'pt-pt' for 'pt-PT'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
2024-05-14 11:26:07 +02:00
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
new Intl.Locale("pt-PT"),
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("pt-pt");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 'pt-br' for 'pt-BR'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
2024-05-14 11:26:07 +02:00
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
new Intl.Locale("pt-BR"),
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("pt-br");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("ValidateClientLocale", () =>
|
|
|
|
{
|
|
|
|
it("should return 'en' for 'en-US'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
2024-05-14 11:26:07 +02:00
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
new Intl.Locale("en-US"),
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("en");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 'kr' for 'ko-KR'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
2024-05-14 11:26:07 +02:00
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
new Intl.Locale("ko-KR"),
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("kr");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 'es-mx' for 'es-MX'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
2024-05-14 11:26:07 +02:00
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
new Intl.Locale("es-MX"),
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("es-mx");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 'cz' for 'cs-CZ'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
2024-05-14 11:26:07 +02:00
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
new Intl.Locale("cs-CZ"),
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("cz");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 'ge' for 'de-DE'", () =>
|
|
|
|
{
|
|
|
|
// Override the get locale so we control what the input is
|
2024-05-14 11:26:07 +02:00
|
|
|
vi.spyOn(localeService, "getPlatformLocale").mockReturnValue(
|
2024-05-20 10:54:40 +02:00
|
|
|
new Intl.Locale("de-DE"),
|
2024-05-14 11:26:07 +02:00
|
|
|
);
|
2024-05-13 23:42:58 +02:00
|
|
|
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("ge");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|