|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { LOCALE_COOKIE_NAME } from "../core"; |
| 3 | +import { loadDictionary_internal } from "./loader"; |
| 4 | + |
| 5 | +describe("loadDictionary_internal", () => { |
| 6 | + function createMockRequest(cookieHeader?: string): Request { |
| 7 | + const headers = new Headers(); |
| 8 | + if (cookieHeader) { |
| 9 | + headers.set("Cookie", cookieHeader); |
| 10 | + } |
| 11 | + return new Request("http://localhost", { headers }); |
| 12 | + } |
| 13 | + |
| 14 | + const mockDictionaryLoader = { |
| 15 | + en: vi.fn().mockResolvedValue({ default: { hello: "Hello" } }), |
| 16 | + es: vi.fn().mockResolvedValue({ default: { hello: "Hola" } }), |
| 17 | + fr: vi.fn().mockResolvedValue({ default: { hello: "Bonjour" } }), |
| 18 | + }; |
| 19 | + |
| 20 | + it("should return null when no Cookie header is present", async () => { |
| 21 | + const request = createMockRequest(); |
| 22 | + const result = await loadDictionary_internal(request, mockDictionaryLoader); |
| 23 | + |
| 24 | + expect(mockDictionaryLoader.en).toHaveBeenCalled(); |
| 25 | + expect(result).toEqual({ hello: "Hello" }); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should return null when Cookie header exists but no lingo-locale cookie", async () => { |
| 29 | + const request = createMockRequest("session=abc123; other-cookie=value"); |
| 30 | + const result = await loadDictionary_internal(request, mockDictionaryLoader); |
| 31 | + |
| 32 | + expect(mockDictionaryLoader.en).toHaveBeenCalled(); |
| 33 | + expect(result).toEqual({ hello: "Hello" }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should parse locale from lingo-locale cookie", async () => { |
| 37 | + const request = createMockRequest(`${LOCALE_COOKIE_NAME}=es`); |
| 38 | + const result = await loadDictionary_internal(request, mockDictionaryLoader); |
| 39 | + |
| 40 | + expect(mockDictionaryLoader.es).toHaveBeenCalled(); |
| 41 | + expect(result).toEqual({ hello: "Hola" }); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should handle lingo-locale cookie with other cookies", async () => { |
| 45 | + const request = createMockRequest( |
| 46 | + `session=abc; ${LOCALE_COOKIE_NAME}=fr; other=value`, |
| 47 | + ); |
| 48 | + const result = await loadDictionary_internal(request, mockDictionaryLoader); |
| 49 | + |
| 50 | + expect(mockDictionaryLoader.fr).toHaveBeenCalled(); |
| 51 | + expect(result).toEqual({ hello: "Bonjour" }); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should handle lingo-locale cookie with spaces", async () => { |
| 55 | + const request = createMockRequest( |
| 56 | + `session=abc; ${LOCALE_COOKIE_NAME}=es ; other=value`, |
| 57 | + ); |
| 58 | + const result = await loadDictionary_internal(request, mockDictionaryLoader); |
| 59 | + |
| 60 | + expect(mockDictionaryLoader.es).toHaveBeenCalled(); |
| 61 | + expect(result).toEqual({ hello: "Hola" }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should use explicit locale string when provided", async () => { |
| 65 | + const result = await loadDictionary_internal("fr", mockDictionaryLoader); |
| 66 | + |
| 67 | + expect(mockDictionaryLoader.fr).toHaveBeenCalled(); |
| 68 | + expect(result).toEqual({ hello: "Bonjour" }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should return null when locale is not available in dictionary", async () => { |
| 72 | + const request = createMockRequest(`${LOCALE_COOKIE_NAME}=de`); |
| 73 | + const result = await loadDictionary_internal(request, mockDictionaryLoader); |
| 74 | + |
| 75 | + expect(result).toBeNull(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return null when explicit locale is not available", async () => { |
| 79 | + const result = await loadDictionary_internal("de", mockDictionaryLoader); |
| 80 | + |
| 81 | + expect(result).toBeNull(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should handle malformed cookie values gracefully", async () => { |
| 85 | + const request = createMockRequest(`${LOCALE_COOKIE_NAME}=`); |
| 86 | + const result = await loadDictionary_internal(request, mockDictionaryLoader); |
| 87 | + |
| 88 | + expect(result).toBeNull(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should handle cookie with equals sign in value", async () => { |
| 92 | + const request = createMockRequest(`${LOCALE_COOKIE_NAME}=en=US`); |
| 93 | + const mockLoader = { |
| 94 | + "en=US": vi.fn().mockResolvedValue({ default: { hello: "Hello US" } }), |
| 95 | + }; |
| 96 | + const result = await loadDictionary_internal(request, mockLoader); |
| 97 | + |
| 98 | + expect(mockLoader["en=US"]).toHaveBeenCalled(); |
| 99 | + expect(result).toEqual({ hello: "Hello US" }); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should handle empty string locale", async () => { |
| 103 | + const result = await loadDictionary_internal("", mockDictionaryLoader); |
| 104 | + |
| 105 | + expect(result).toBeNull(); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should extract default export from loader result", async () => { |
| 109 | + const customLoader = { |
| 110 | + custom: vi.fn().mockResolvedValue({ |
| 111 | + default: { test: "value" }, |
| 112 | + other: { ignored: "data" }, |
| 113 | + }), |
| 114 | + }; |
| 115 | + const result = await loadDictionary_internal("custom", customLoader); |
| 116 | + |
| 117 | + expect(result).toEqual({ test: "value" }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments