Download src/lib/sources/__tests__/wikipedia.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 8.38 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/sources/__tests__/wikipedia.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/sources/__tests__/wikipedia.test.ts
-
curl -L -o wikipedia.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/sources/__tests__/wikipedia.test.ts
8.38 kB
| import { describe, it, expect, vi, beforeEach } from "vitest"; | |
| // Mock fetch globally | |
| const mockFetch = vi.fn(); | |
| vi.stubGlobal("fetch", mockFetch); | |
| // Import after mocking | |
| import { | |
| searchWikipedia, | |
| getWikipediaExtract, | |
| fetchWikipediaSources, | |
| verifyWikipediaUrl, | |
| } from "../wikipedia"; | |
| beforeEach(() => { | |
| mockFetch.mockReset(); | |
| }); | |
| describe("searchWikipedia", () => { | |
| it("returns parsed search results", async () => { | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| search: [ | |
| { pageid: 123, title: "Hash table", snippet: "A <b>hash table</b> is a data structure" }, | |
| { pageid: 456, title: "Hash function", snippet: "A <b>hash function</b> maps data" }, | |
| ], | |
| }, | |
| }), | |
| }); | |
| const results = await searchWikipedia("hash table", 2); | |
| expect(results).toHaveLength(2); | |
| expect(results[0].pageId).toBe(123); | |
| expect(results[0].title).toBe("Hash table"); | |
| expect(results[0].snippet).toBe("A hash table is a data structure"); // HTML stripped | |
| expect(results[1].pageId).toBe(456); | |
| }); | |
| it("returns empty array when no results", async () => { | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ query: { search: [] } }), | |
| }); | |
| const results = await searchWikipedia("xyznonexistent12345"); | |
| expect(results).toHaveLength(0); | |
| }); | |
| it("throws on non-OK response", async () => { | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: false, | |
| status: 500, | |
| statusText: "Internal Server Error", | |
| }); | |
| await expect(searchWikipedia("test")).rejects.toThrow("Wikipedia search failed: 500"); | |
| }); | |
| it("passes correct query parameters", async () => { | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ query: { search: [] } }), | |
| }); | |
| await searchWikipedia("recursion", 5); | |
| const calledUrl = mockFetch.mock.calls[0][0] as string; | |
| expect(calledUrl).toContain("srsearch=recursion"); | |
| expect(calledUrl).toContain("srlimit=5"); | |
| expect(calledUrl).toContain("format=json"); | |
| }); | |
| }); | |
| describe("getWikipediaExtract", () => { | |
| it("returns extract for a valid page", async () => { | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| pages: { | |
| "123": { | |
| pageid: 123, | |
| title: "Hash table", | |
| extract: "In computing, a hash table is a data structure that implements an associative array, also called a dictionary.", | |
| fullurl: "https://en.wikipedia.org/wiki/Hash_table", | |
| }, | |
| }, | |
| }, | |
| }), | |
| }); | |
| const result = await getWikipediaExtract("Hash table"); | |
| expect(result).not.toBeNull(); | |
| expect(result!.pageId).toBe(123); | |
| expect(result!.title).toBe("Hash table"); | |
| expect(result!.url).toBe("https://en.wikipedia.org/wiki/Hash_table"); | |
| expect(result!.extract).toContain("associative array"); | |
| }); | |
| it("returns null for missing page", async () => { | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| pages: { | |
| "-1": { pageid: -1, title: "Nonexistent", missing: "" }, | |
| }, | |
| }, | |
| }), | |
| }); | |
| const result = await getWikipediaExtract("Nonexistent"); | |
| expect(result).toBeNull(); | |
| }); | |
| it("returns null for stub pages with very short extracts", async () => { | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| pages: { | |
| "789": { | |
| pageid: 789, | |
| title: "Stub page", | |
| extract: "Short.", // less than 50 chars | |
| fullurl: "https://en.wikipedia.org/wiki/Stub_page", | |
| }, | |
| }, | |
| }, | |
| }), | |
| }); | |
| const result = await getWikipediaExtract("Stub page"); | |
| expect(result).toBeNull(); | |
| }); | |
| it("constructs fallback URL when fullurl missing", async () => { | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| pages: { | |
| "100": { | |
| pageid: 100, | |
| title: "Test Article", | |
| extract: "A".repeat(60), // enough to pass the 50 char minimum | |
| }, | |
| }, | |
| }, | |
| }), | |
| }); | |
| const result = await getWikipediaExtract("Test Article"); | |
| expect(result).not.toBeNull(); | |
| expect(result!.url).toBe("https://en.wikipedia.org/wiki/Test_Article"); | |
| }); | |
| }); | |
| describe("fetchWikipediaSources", () => { | |
| it("combines search + extract into source list", async () => { | |
| // First call: search | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| search: [ | |
| { pageid: 1, title: "Recursion", snippet: "Recursion in CS" }, | |
| { pageid: 2, title: "Recursive function", snippet: "A recursive function" }, | |
| ], | |
| }, | |
| }), | |
| }); | |
| // Second call: extract for "Recursion" | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| pages: { | |
| "1": { | |
| pageid: 1, | |
| title: "Recursion", | |
| extract: "Recursion is the process of defining something in terms of itself. " + "A".repeat(50), | |
| fullurl: "https://en.wikipedia.org/wiki/Recursion", | |
| }, | |
| }, | |
| }, | |
| }), | |
| }); | |
| // Third call: extract for "Recursive function" | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| pages: { | |
| "2": { | |
| pageid: 2, | |
| title: "Recursive function", | |
| extract: "In mathematical logic, a recursive function is a function that calls itself. " + "B".repeat(50), | |
| fullurl: "https://en.wikipedia.org/wiki/Recursive_function", | |
| }, | |
| }, | |
| }, | |
| }), | |
| }); | |
| const sources = await fetchWikipediaSources("recursion", 2); | |
| expect(sources).toHaveLength(2); | |
| expect(sources[0].title).toBe("Recursion"); | |
| expect(sources[1].title).toBe("Recursive function"); | |
| }); | |
| it("skips stub pages and continues to next result", async () => { | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| search: [ | |
| { pageid: 1, title: "Stub Topic", snippet: "..." }, | |
| { pageid: 2, title: "Real Topic", snippet: "..." }, | |
| ], | |
| }, | |
| }), | |
| }); | |
| // Stub — returns null | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| pages: { "1": { pageid: 1, title: "Stub Topic", extract: "Short." } }, | |
| }, | |
| }), | |
| }); | |
| // Real article | |
| mockFetch.mockResolvedValueOnce({ | |
| ok: true, | |
| json: async () => ({ | |
| query: { | |
| pages: { | |
| "2": { | |
| pageid: 2, | |
| title: "Real Topic", | |
| extract: "This is a real, substantive article about the topic. " + "C".repeat(50), | |
| fullurl: "https://en.wikipedia.org/wiki/Real_Topic", | |
| }, | |
| }, | |
| }, | |
| }), | |
| }); | |
| const sources = await fetchWikipediaSources("stub test", 2); | |
| expect(sources).toHaveLength(1); | |
| expect(sources[0].title).toBe("Real Topic"); | |
| }); | |
| }); | |
| describe("verifyWikipediaUrl", () => { | |
| it("returns true for OK HEAD response", async () => { | |
| mockFetch.mockResolvedValueOnce({ ok: true }); | |
| const result = await verifyWikipediaUrl("https://en.wikipedia.org/wiki/Test"); | |
| expect(result).toBe(true); | |
| }); | |
| it("returns false for non-OK response", async () => { | |
| mockFetch.mockResolvedValueOnce({ ok: false }); | |
| const result = await verifyWikipediaUrl("https://en.wikipedia.org/wiki/Missing"); | |
| expect(result).toBe(false); | |
| }); | |
| it("returns false on network error", async () => { | |
| mockFetch.mockRejectedValueOnce(new Error("Network error")); | |
| const result = await verifyWikipediaUrl("https://en.wikipedia.org/wiki/Error"); | |
| expect(result).toBe(false); | |
| }); | |
| it("uses HEAD method", async () => { | |
| mockFetch.mockResolvedValueOnce({ ok: true }); | |
| await verifyWikipediaUrl("https://en.wikipedia.org/wiki/Test"); | |
| const options = mockFetch.mock.calls[0][1] as RequestInit; | |
| expect(options.method).toBe("HEAD"); | |
| }); | |
| }); | |