···11+import { describe, it, expect, vi, beforeEach } from "vitest";
22+import { getLanguage } from "./getLanguage.js";
33+44+// Mock the logger
55+vi.mock("../logger.js", () => ({
66+ logger: {
77+ warn: vi.fn(),
88+ },
99+}));
1010+1111+describe("getLanguage", () => {
1212+ beforeEach(() => {
1313+ vi.clearAllMocks();
1414+ });
1515+1616+ describe("language detection", () => {
1717+ it("should detect English text", async () => {
1818+ const text = "Hello world, this is a test of the English language.";
1919+ const result = await getLanguage(text);
2020+ expect(result).toBe("eng");
2121+ });
2222+2323+ it("should detect Spanish text", async () => {
2424+ const text =
2525+ "Hola mundo, esta es una prueba del idioma español con suficiente texto para detectar.";
2626+ const result = await getLanguage(text);
2727+ expect(result).toBe("spa");
2828+ });
2929+3030+ it("should detect French text", async () => {
3131+ const text =
3232+ "Bonjour le monde, ceci est un test de la langue française avec suffisamment de texte.";
3333+ const result = await getLanguage(text);
3434+ expect(result).toBe("fra");
3535+ });
3636+3737+ it("should detect German text", async () => {
3838+ const text =
3939+ "Hallo Welt, dies ist ein Test der deutschen Sprache mit genügend Text.";
4040+ const result = await getLanguage(text);
4141+ expect(result).toBe("deu");
4242+ });
4343+4444+ it("should detect Portuguese text", async () => {
4545+ const text =
4646+ "Olá mundo, este é um teste da língua portuguesa com texto suficiente para detecção.";
4747+ const result = await getLanguage(text);
4848+ expect(result).toBe("por");
4949+ });
5050+5151+ it("should detect Italian text", async () => {
5252+ const text =
5353+ "Ciao mondo, questo è un test della lingua italiana con abbastanza testo.";
5454+ const result = await getLanguage(text);
5555+ expect(result).toBe("ita");
5656+ });
5757+5858+ it("should detect Japanese text", async () => {
5959+ const text = "これは日本語のテストです。十分なテキストで言語を検出します。";
6060+ const result = await getLanguage(text);
6161+ expect(result).toBe("jpn");
6262+ });
6363+ });
6464+6565+ describe("edge cases", () => {
6666+ it("should default to eng for empty strings", async () => {
6767+ const result = await getLanguage("");
6868+ expect(result).toBe("eng");
6969+ });
7070+7171+ it("should default to eng for whitespace-only strings", async () => {
7272+ const result = await getLanguage(" ");
7373+ expect(result).toBe("eng");
7474+ });
7575+7676+ it("should default to eng for very short text", async () => {
7777+ const result = await getLanguage("hi");
7878+ expect(result).toBe("eng");
7979+ });
8080+8181+ it("should default to eng for undetermined language", async () => {
8282+ const result = await getLanguage("123 456 789");
8383+ expect(result).toBe("eng");
8484+ });
8585+8686+ it("should default to eng for symbols only", async () => {
8787+ const result = await getLanguage("!@#$%^&*()");
8888+ expect(result).toBe("eng");
8989+ });
9090+ });
9191+9292+ describe("invalid input handling", () => {
9393+ it("should handle non-string input gracefully", async () => {
9494+ const result = await getLanguage(123 as any);
9595+ expect(result).toBe("eng");
9696+ });
9797+9898+ it("should handle null input gracefully", async () => {
9999+ const result = await getLanguage(null as any);
100100+ expect(result).toBe("eng");
101101+ });
102102+103103+ it("should handle undefined input gracefully", async () => {
104104+ const result = await getLanguage(undefined as any);
105105+ expect(result).toBe("eng");
106106+ });
107107+108108+ it("should handle object input gracefully", async () => {
109109+ const result = await getLanguage({} as any);
110110+ expect(result).toBe("eng");
111111+ });
112112+113113+ it("should handle array input gracefully", async () => {
114114+ const result = await getLanguage([] as any);
115115+ expect(result).toBe("eng");
116116+ });
117117+ });
118118+119119+ describe("trimming behavior", () => {
120120+ it("should trim leading whitespace", async () => {
121121+ const text =
122122+ " Hello world, this is a test of the English language.";
123123+ const result = await getLanguage(text);
124124+ expect(result).toBe("eng");
125125+ });
126126+127127+ it("should trim trailing whitespace", async () => {
128128+ const text =
129129+ "Hello world, this is a test of the English language. ";
130130+ const result = await getLanguage(text);
131131+ expect(result).toBe("eng");
132132+ });
133133+134134+ it("should trim both leading and trailing whitespace", async () => {
135135+ const text =
136136+ " Hello world, this is a test of the English language. ";
137137+ const result = await getLanguage(text);
138138+ expect(result).toBe("eng");
139139+ });
140140+ });
141141+142142+ describe("mixed language text", () => {
143143+ it("should detect primary language in mixed content", async () => {
144144+ const text =
145145+ "This is primarily English text with some español words mixed in.";
146146+ const result = await getLanguage(text);
147147+ expect(result).toBe("eng");
148148+ });
149149+150150+ it("should handle code mixed with text", async () => {
151151+ const text =
152152+ "Here is some English text with const x = 123; code in it.";
153153+ const result = await getLanguage(text);
154154+ expect(result).toBe("eng");
155155+ });
156156+ });
157157+});