diff --git a/ts/packages/azure-ai-foundry/src/wikipedia.ts b/ts/packages/azure-ai-foundry/src/wikipedia.ts index f1356c4166..184cf63135 100644 --- a/ts/packages/azure-ai-foundry/src/wikipedia.ts +++ b/ts/packages/azure-ai-foundry/src/wikipedia.ts @@ -93,15 +93,16 @@ export function apiSettingsFromEnv( * Get the page object of the item with the supplied title. * @param title - The title of the page to retrieve * @param config - The wikipedia API configuration + * @param locale - The Wikipedia language edition (e.g. "en", "de", "fr"). Defaults to "en". * @returns The page object. */ export async function getPageObject( title: string, config: wikipedia.WikipediaApiSettings, + locale: string = "en", ) { - // TODO: localization (e.g. en, de, fr, etc.) const response = await fetch( - `${config.endpoint}core/v1/wikipedia/en/page/${title}/bare`, + `${config.endpoint}core/v1/wikipedia/${locale}/page/${encodeWikipediaTitle(title)}/bare`, { method: "GET", headers: await config.getAPIHeaders() }, ); @@ -116,14 +117,15 @@ export async function getPageObject( * * @param title - The title of the page whose content to get. * @param config - The wikipedia API configuration - * @returns - The content of the requetsed page or undefined if there was a problem + * @param locale - The Wikipedia language edition (e.g. "en", "de", "fr"). Defaults to "en". + * @returns - The content of the requested page or undefined if there was a problem */ export async function getPageMarkdown( title: string, config: wikipedia.WikipediaApiSettings, + locale: string = "en", ): Promise { - // TODO: localization (e.g. en, de, fr, etc.) - const url: string = `${config.endpoint}en/page/${encodeWikipediaTitle(title)}`; + const url: string = `${config.endpoint}${locale}/page/${encodeWikipediaTitle(title)}`; const response = await fetch(url, { method: "GET", headers: await config.getAPIHeaders(), diff --git a/ts/packages/azure-ai-foundry/test/wikipedia.spec.ts b/ts/packages/azure-ai-foundry/test/wikipedia.spec.ts new file mode 100644 index 0000000000..a8016e8d3a --- /dev/null +++ b/ts/packages/azure-ai-foundry/test/wikipedia.spec.ts @@ -0,0 +1,106 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { + encodeWikipediaTitle, + getPageObject, + getPageMarkdown, +} from "../src/wikipedia.js"; +import { WikipediaApiSettings } from "../src/wikipedia.js"; + +// Minimal stub — only getAPIHeaders is needed for the fetch calls under test +function makeConfig(endpoint: string): WikipediaApiSettings { + return { + endpoint, + getToken: async () => "token", + getAPIHeaders: async () => ({ Authorization: "Bearer token" }), + }; +} + +describe("encodeWikipediaTitle", () => { + test("replaces spaces with underscores", () => { + expect(encodeWikipediaTitle("Johann Sebastian Bach")).toBe( + "Johann_Sebastian_Bach", + ); + }); + + test("percent-encodes special characters", () => { + expect(encodeWikipediaTitle("Ångström")).toBe("%C3%85ngstr%C3%B6m"); + }); + + test("leaves plain ASCII titles unchanged", () => { + expect(encodeWikipediaTitle("Berlin")).toBe("Berlin"); + }); +}); + +describe("getPageObject locale", () => { + let capturedUrl: string; + const originalFetch = globalThis.fetch; + + beforeEach(() => { + globalThis.fetch = async (input: string | URL | Request) => { + capturedUrl = input.toString(); + return { ok: false } as Response; // return undefined from the function under test + }; + }); + + afterEach(() => { + globalThis.fetch = originalFetch; + }); + + test("defaults to 'en' locale", async () => { + const config = makeConfig("https://api.example.com/"); + await getPageObject("Berlin", config); + expect(capturedUrl).toContain("/en/"); + }); + + test("uses supplied locale in URL", async () => { + const config = makeConfig("https://api.example.com/"); + await getPageObject("Berlin", config, "de"); + expect(capturedUrl).toContain("/de/"); + expect(capturedUrl).not.toContain("/en/"); + }); + + test("constructs correct URL shape for 'fr'", async () => { + const config = makeConfig("https://api.example.com/"); + await getPageObject("Paris", config, "fr"); + expect(capturedUrl).toBe( + "https://api.example.com/core/v1/wikipedia/fr/page/Paris/bare", + ); + }); +}); + +describe("getPageMarkdown locale", () => { + let capturedUrl: string; + const originalFetch = globalThis.fetch; + + beforeEach(() => { + globalThis.fetch = async (input: string | URL | Request) => { + capturedUrl = input.toString(); + return { ok: false } as Response; + }; + }); + + afterEach(() => { + globalThis.fetch = originalFetch; + }); + + test("defaults to 'en' locale", async () => { + const config = makeConfig("https://api.example.com/"); + await getPageMarkdown("Berlin", config); + expect(capturedUrl).toContain("/en/"); + }); + + test("uses supplied locale in URL", async () => { + const config = makeConfig("https://api.example.com/"); + await getPageMarkdown("Berlin", config, "de"); + expect(capturedUrl).toContain("/de/"); + expect(capturedUrl).not.toContain("/en/"); + }); + + test("constructs correct URL shape for 'ja'", async () => { + const config = makeConfig("https://api.example.com/"); + await getPageMarkdown("Tokyo", config, "ja"); + expect(capturedUrl).toBe("https://api.example.com/ja/page/Tokyo"); + }); +});