UNPKG

i18n-ai-translate

Version:

AI-powered localization CLI, Node library, and GitHub Action. Translate i18next JSON, Gettext PO, Java .properties, and iOS .strings with ChatGPT, Claude, Gemini, or local Ollama models.

68 lines 3.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const cache_1 = require("../cache"); const fs_1 = __importDefault(require("fs")); const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const tmpFile = () => path_1.default.join(fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "i18n-cache-")), "cache.json"); describe("cacheKey", () => { it("is deterministic for identical inputs", () => { expect((0, cache_1.cacheKey)("en", "fr", "", "Hello")).toBe((0, cache_1.cacheKey)("en", "fr", "", "Hello")); }); it("differs by source text, languages, and context", () => { const base = (0, cache_1.cacheKey)("en", "fr", "", "Hello"); expect((0, cache_1.cacheKey)("en", "fr", "", "Goodbye")).not.toBe(base); expect((0, cache_1.cacheKey)("en", "es", "", "Hello")).not.toBe(base); expect((0, cache_1.cacheKey)("de", "fr", "", "Hello")).not.toBe(base); expect((0, cache_1.cacheKey)("en", "fr", "a SaaS app", "Hello")).not.toBe(base); }); }); describe("get/set cached translation", () => { it("round-trips a stored translation", () => { const cache = (0, cache_1.createCache)(); expect((0, cache_1.getCachedTranslation)(cache, "en", "fr", "", "Hello")).toBeUndefined(); (0, cache_1.setCachedTranslation)(cache, "en", "fr", "", "Hello", "Bonjour"); expect((0, cache_1.getCachedTranslation)(cache, "en", "fr", "", "Hello")).toBe("Bonjour"); }); it("keeps entries for different languages and contexts separate", () => { const cache = (0, cache_1.createCache)(); (0, cache_1.setCachedTranslation)(cache, "en", "fr", "", "Hello", "Bonjour"); (0, cache_1.setCachedTranslation)(cache, "en", "es", "", "Hello", "Hola"); (0, cache_1.setCachedTranslation)(cache, "en", "fr", "formal", "Hello", "Bonjour M."); expect((0, cache_1.getCachedTranslation)(cache, "en", "fr", "", "Hello")).toBe("Bonjour"); expect((0, cache_1.getCachedTranslation)(cache, "en", "es", "", "Hello")).toBe("Hola"); expect((0, cache_1.getCachedTranslation)(cache, "en", "fr", "formal", "Hello")).toBe("Bonjour M."); }); }); describe("loadCache / saveCache", () => { it("returns a fresh cache when the file is missing", () => { const cache = (0, cache_1.loadCache)(tmpFile()); expect(cache.version).toBe(cache_1.CACHE_VERSION); expect(cache.entries).toEqual({}); }); it("round-trips a saved cache through disk", () => { const file = tmpFile(); const cache = (0, cache_1.createCache)(); (0, cache_1.setCachedTranslation)(cache, "en", "fr", "", "Hello", "Bonjour"); (0, cache_1.saveCache)(file, cache); const reloaded = (0, cache_1.loadCache)(file); expect((0, cache_1.getCachedTranslation)(reloaded, "en", "fr", "", "Hello")).toBe("Bonjour"); }); it("ignores a cache written at an incompatible version", () => { const file = tmpFile(); fs_1.default.writeFileSync(file, JSON.stringify({ entries: { abc: "x" }, version: 999 })); const cache = (0, cache_1.loadCache)(file); expect(cache.version).toBe(cache_1.CACHE_VERSION); expect(cache.entries).toEqual({}); }); it("ignores a malformed cache file", () => { const file = tmpFile(); fs_1.default.writeFileSync(file, "not json at all {"); const cache = (0, cache_1.loadCache)(file); expect(cache.entries).toEqual({}); }); }); //# sourceMappingURL=cache.spec.js.map