File size: 5,747 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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]);
  });
});