Download src/lib/reading/__tests__/articles.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 5.75 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/reading/__tests__/articles.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/reading/__tests__/articles.test.ts
-
curl -L -o articles.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/reading/__tests__/articles.test.ts
5.75 kB
| import { describe, it, expect } from "vitest"; | |
| import { | |
| ARTICLES, | |
| getArticleBySlug, | |
| getArticlesByDifficulty, | |
| getRandomArticle, | |
| type ArticleDifficulty, | |
| } from "../articles"; | |
| // --------------------------------------------------------------------------- | |
| // Data integrity | |
| // --------------------------------------------------------------------------- | |
| describe("ARTICLES data integrity", () => { | |
| it("has at least one article", () => { | |
| expect(ARTICLES.length).toBeGreaterThanOrEqual(1); | |
| }); | |
| it("has unique slugs", () => { | |
| const slugs = ARTICLES.map((a) => a.slug); | |
| expect(new Set(slugs).size).toBe(slugs.length); | |
| }); | |
| it("slugs are URL-safe (lowercase, hyphens, no spaces)", () => { | |
| for (const a of ARTICLES) { | |
| expect(a.slug).toMatch(/^[a-z0-9]+(-[a-z0-9]+)*$/); | |
| } | |
| }); | |
| it("every article has non-empty required fields", () => { | |
| for (const a of ARTICLES) { | |
| expect(a.slug).toBeTruthy(); | |
| expect(a.title).toBeTruthy(); | |
| expect(a.domain).toBeTruthy(); | |
| expect(a.body.length).toBeGreaterThan(100); | |
| expect(["beginner", "intermediate", "advanced"]).toContain(a.difficulty); | |
| } | |
| }); | |
| it("every article has a positive word count", () => { | |
| for (const a of ARTICLES) { | |
| expect(a.wordCount).toBeGreaterThan(0); | |
| } | |
| }); | |
| it("every article has a positive estimated reading time", () => { | |
| for (const a of ARTICLES) { | |
| expect(a.estimatedMinutes).toBeGreaterThan(0); | |
| } | |
| }); | |
| it("word count is roughly consistent with actual body length", () => { | |
| for (const a of ARTICLES) { | |
| // Count words in body (split on whitespace, filter empties) | |
| const actualWords = a.body | |
| .split(/\s+/) | |
| .filter((w) => w.length > 0).length; | |
| // Allow ±40% tolerance — markdown headers, formatting symbols, and | |
| // special characters can shift the naive split count vs stated wordCount | |
| expect(actualWords).toBeGreaterThan(a.wordCount * 0.6); | |
| expect(actualWords).toBeLessThan(a.wordCount * 1.4); | |
| } | |
| }); | |
| it("estimated reading time is reasonable for word count (~200 wpm)", () => { | |
| for (const a of ARTICLES) { | |
| const expectedMinutes = a.wordCount / 200; | |
| // Allow generous range (1-3x the naive estimate) | |
| expect(a.estimatedMinutes).toBeGreaterThanOrEqual( | |
| Math.max(1, Math.floor(expectedMinutes * 0.5)), | |
| ); | |
| expect(a.estimatedMinutes).toBeLessThanOrEqual( | |
| Math.ceil(expectedMinutes * 3), | |
| ); | |
| } | |
| }); | |
| it("has articles at multiple difficulty levels", () => { | |
| const difficulties = new Set(ARTICLES.map((a) => a.difficulty)); | |
| expect(difficulties.size).toBeGreaterThanOrEqual(2); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // getArticleBySlug | |
| // --------------------------------------------------------------------------- | |
| describe("getArticleBySlug", () => { | |
| it("returns the correct article for a known slug", () => { | |
| const first = ARTICLES[0]; | |
| const result = getArticleBySlug(first.slug); | |
| expect(result).toBeDefined(); | |
| expect(result!.slug).toBe(first.slug); | |
| expect(result!.title).toBe(first.title); | |
| }); | |
| it("returns undefined for an unknown slug", () => { | |
| expect(getArticleBySlug("nonexistent-article-xyz")).toBeUndefined(); | |
| }); | |
| it("returns undefined for empty string", () => { | |
| expect(getArticleBySlug("")).toBeUndefined(); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // getArticlesByDifficulty | |
| // --------------------------------------------------------------------------- | |
| describe("getArticlesByDifficulty", () => { | |
| it("returns only articles of the requested difficulty", () => { | |
| const difficulties: ArticleDifficulty[] = [ | |
| "beginner", | |
| "intermediate", | |
| "advanced", | |
| ]; | |
| for (const d of difficulties) { | |
| const results = getArticlesByDifficulty(d); | |
| for (const r of results) { | |
| expect(r.difficulty).toBe(d); | |
| } | |
| } | |
| }); | |
| it("returns an empty array when no articles match", () => { | |
| // This tests the filter — if all articles happen to cover a level, verify it returns the right count | |
| const beginner = getArticlesByDifficulty("beginner"); | |
| const intermediate = getArticlesByDifficulty("intermediate"); | |
| const advanced = getArticlesByDifficulty("advanced"); | |
| expect(beginner.length + intermediate.length + advanced.length).toBe( | |
| ARTICLES.length, | |
| ); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // getRandomArticle | |
| // --------------------------------------------------------------------------- | |
| describe("getRandomArticle", () => { | |
| it("returns an article from the collection", () => { | |
| const a = getRandomArticle(); | |
| expect(ARTICLES).toContainEqual(a); | |
| }); | |
| it("respects difficulty filter", () => { | |
| const a = getRandomArticle("beginner"); | |
| expect(a.difficulty).toBe("beginner"); | |
| }); | |
| it("excludes specified slugs", () => { | |
| const allSlugsButLast = ARTICLES.slice(0, -1).map((a) => a.slug); | |
| const a = getRandomArticle(undefined, allSlugsButLast); | |
| // Should return either the last article or fall back to ARTICLES[0] | |
| expect(a).toBeDefined(); | |
| }); | |
| it("falls back to ARTICLES[0] when all are excluded", () => { | |
| const allSlugs = ARTICLES.map((a) => a.slug); | |
| const a = getRandomArticle(undefined, allSlugs); | |
| expect(a).toBe(ARTICLES[0]); | |
| }); | |
| it("falls back to ARTICLES[0] when difficulty + exclude leaves empty pool", () => { | |
| const beginnerSlugs = ARTICLES.filter((a) => a.difficulty === "beginner").map( | |
| (a) => a.slug, | |
| ); | |
| const a = getRandomArticle("beginner", beginnerSlugs); | |
| expect(a).toBe(ARTICLES[0]); | |
| }); | |
| }); | |