File size: 10,636 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { describe, it, expect } from "vitest";
import {
  getTopicById,
  getDirectPrerequisites,
  getPrerequisiteChain,
  checkPrerequisites,
  filterReadyTopics,
  partitionByReadiness,
  getSuggestedPrerequisites,
} from "../prerequisites";
import { FEYNMAN_TOPICS } from "../topics";

// ---------------------------------------------------------------------------
// getTopicById
// ---------------------------------------------------------------------------

describe("getTopicById", () => {
  it("returns topic for valid ID", () => {
    const t = getTopicById("cs-01");
    expect(t).toBeDefined();
    expect(t!.id).toBe("cs-01");
    expect(t!.topic).toBe("How a hash table works");
  });

  it("returns undefined for unknown ID", () => {
    expect(getTopicById("xx-99")).toBeUndefined();
  });
});

// ---------------------------------------------------------------------------
// getDirectPrerequisites
// ---------------------------------------------------------------------------

describe("getDirectPrerequisites", () => {
  it("returns empty array for beginner topic with no prerequisites", () => {
    const prereqs = getDirectPrerequisites("cs-01");
    expect(prereqs).toHaveLength(0);
  });

  it("returns direct prerequisites for topic with dependencies", () => {
    // cs-09 (B-tree) requires cs-01 (hash table) and cs-02 (Big O)
    const prereqs = getDirectPrerequisites("cs-09");
    expect(prereqs).toHaveLength(2);
    const ids = prereqs.map((p) => p.id);
    expect(ids).toContain("cs-01");
    expect(ids).toContain("cs-02");
  });

  it("returns single prerequisite", () => {
    // ma-02 (fund. theorem of calculus) requires ma-01 (derivative)
    const prereqs = getDirectPrerequisites("ma-02");
    expect(prereqs).toHaveLength(1);
    expect(prereqs[0].id).toBe("ma-01");
  });

  it("returns empty array for unknown topic", () => {
    expect(getDirectPrerequisites("xx-99")).toHaveLength(0);
  });
});

// ---------------------------------------------------------------------------
// getPrerequisiteChain
// ---------------------------------------------------------------------------

describe("getPrerequisiteChain", () => {
  it("returns empty chain for topic without prerequisites", () => {
    const chain = getPrerequisiteChain("ph-01");
    expect(chain).toHaveLength(0);
  });

  it("returns single-level chain for topic with direct prereqs only", () => {
    // cs-05 requires cs-02 (Big O), which has no prereqs
    const chain = getPrerequisiteChain("cs-05");
    expect(chain).toHaveLength(1);
    expect(chain[0].id).toBe("cs-02");
  });

  it("returns multi-level chain for deep dependencies", () => {
    // ph-03 (photoelectric) requires ph-02 (EM waves), which requires ph-01 (Newton)
    const chain = getPrerequisiteChain("ph-03");
    const ids = chain.map((t) => t.id);
    expect(ids).toContain("ph-01");
    expect(ids).toContain("ph-02");
    // ph-01 should come before ph-02 (deepest first)
    expect(ids.indexOf("ph-01")).toBeLessThan(ids.indexOf("ph-02"));
  });

  it("returns transitive chain for expert topic", () => {
    // ma-04 (Euler's identity) requires ma-02 + ma-03
    // ma-02 requires ma-01
    const chain = getPrerequisiteChain("ma-04");
    const ids = chain.map((t) => t.id);
    expect(ids).toContain("ma-01");
    expect(ids).toContain("ma-02");
    expect(ids).toContain("ma-03");
  });

  it("does not include the topic itself", () => {
    const chain = getPrerequisiteChain("cs-09");
    expect(chain.map((t) => t.id)).not.toContain("cs-09");
  });

  it("handles topic with no registered prerequisites gracefully", () => {
    const chain = getPrerequisiteChain("xx-99");
    expect(chain).toHaveLength(0);
  });
});

// ---------------------------------------------------------------------------
// checkPrerequisites
// ---------------------------------------------------------------------------

describe("checkPrerequisites", () => {
  it("returns ready=true for topic with no prerequisites", () => {
    const info = checkPrerequisites("cs-01", new Set());
    expect(info.ready).toBe(true);
    expect(info.met).toHaveLength(0);
    expect(info.unmet).toHaveLength(0);
  });

  it("returns ready=true when all prerequisites are mastered", () => {
    // cs-09 requires cs-01 + cs-02
    const mastered = new Set(["cs-01", "cs-02"]);
    const info = checkPrerequisites("cs-09", mastered);
    expect(info.ready).toBe(true);
    expect(info.met).toHaveLength(2);
    expect(info.unmet).toHaveLength(0);
  });

  it("returns ready=false when some prerequisites are unmet", () => {
    // cs-09 requires cs-01 + cs-02, only cs-01 mastered
    const mastered = new Set(["cs-01"]);
    const info = checkPrerequisites("cs-09", mastered);
    expect(info.ready).toBe(false);
    expect(info.met).toHaveLength(1);
    expect(info.unmet).toHaveLength(1);
    expect(info.unmet[0].id).toBe("cs-02");
  });

  it("returns ready=false when no prerequisites are mastered", () => {
    const info = checkPrerequisites("cs-09", new Set());
    expect(info.ready).toBe(false);
    expect(info.unmet).toHaveLength(2);
  });

  it("includes full transitive chain", () => {
    // ph-03 has chain: ph-01 -> ph-02 -> ph-03
    const info = checkPrerequisites("ph-03", new Set());
    expect(info.chain.length).toBeGreaterThanOrEqual(2);
    const chainIds = info.chain.map((t) => t.id);
    expect(chainIds).toContain("ph-01");
    expect(chainIds).toContain("ph-02");
  });
});

// ---------------------------------------------------------------------------
// filterReadyTopics
// ---------------------------------------------------------------------------

describe("filterReadyTopics", () => {
  it("includes all beginner topics with no prerequisites", () => {
    const beginners = FEYNMAN_TOPICS.filter(
      (t) => t.difficulty === "beginner" && !t.prerequisites?.length,
    );
    const ready = filterReadyTopics(beginners, new Set());
    expect(ready.length).toBe(beginners.length);
  });

  it("excludes topics with unmet prerequisites", () => {
    const subset = FEYNMAN_TOPICS.filter((t) => ["cs-01", "cs-09"].includes(t.id));
    const ready = filterReadyTopics(subset, new Set());
    expect(ready).toHaveLength(1);
    expect(ready[0].id).toBe("cs-01");
  });

  it("includes topics when prerequisites are mastered", () => {
    const subset = FEYNMAN_TOPICS.filter((t) => ["cs-01", "cs-09"].includes(t.id));
    const ready = filterReadyTopics(subset, new Set(["cs-01", "cs-02"]));
    expect(ready).toHaveLength(2);
  });
});

// ---------------------------------------------------------------------------
// partitionByReadiness
// ---------------------------------------------------------------------------

describe("partitionByReadiness", () => {
  it("puts all topics in ready when no prerequisites exist", () => {
    const noPrereqs = FEYNMAN_TOPICS.filter((t) => !t.prerequisites?.length);
    const { ready, locked } = partitionByReadiness(noPrereqs, new Set());
    expect(ready.length).toBe(noPrereqs.length);
    expect(locked).toHaveLength(0);
  });

  it("correctly partitions mixed topics", () => {
    const subset = FEYNMAN_TOPICS.filter((t) =>
      ["cs-01", "cs-02", "cs-05", "cs-09"].includes(t.id),
    );
    // cs-05 needs cs-02, cs-09 needs cs-01+cs-02
    const mastered = new Set(["cs-02"]);
    const { ready, locked } = partitionByReadiness(subset, mastered);
    // cs-01 (no prereqs) + cs-02 (no prereqs) + cs-05 (cs-02 mastered) = 3 ready
    const readyIds = ready.map((t) => t.id);
    const lockedIds = locked.map((t) => t.id);
    expect(readyIds).toContain("cs-01");
    expect(readyIds).toContain("cs-02");
    expect(readyIds).toContain("cs-05");
    expect(lockedIds).toContain("cs-09"); // needs cs-01 which is not mastered
  });
});

// ---------------------------------------------------------------------------
// getSuggestedPrerequisites
// ---------------------------------------------------------------------------

describe("getSuggestedPrerequisites", () => {
  it("returns topics that would unlock the most other topics", () => {
    const suggestions = getSuggestedPrerequisites(new Set(), 5);
    expect(suggestions.length).toBeGreaterThan(0);
    expect(suggestions.length).toBeLessThanOrEqual(5);
    // All suggestions should be valid topics
    for (const s of suggestions) {
      expect(s.id).toBeDefined();
      expect(s.topic).toBeDefined();
    }
  });

  it("returns fewer suggestions when many topics are mastered", () => {
    // Master all beginner topics
    const allIds = new Set(FEYNMAN_TOPICS.map((t) => t.id));
    const suggestions = getSuggestedPrerequisites(allIds, 3);
    expect(suggestions).toHaveLength(0);
  });

  it("respects limit parameter", () => {
    const suggestions = getSuggestedPrerequisites(new Set(), 1);
    expect(suggestions.length).toBeLessThanOrEqual(1);
  });
});

// ---------------------------------------------------------------------------
// Data integrity
// ---------------------------------------------------------------------------

describe("prerequisite data integrity", () => {
  it("all prerequisite IDs reference existing topics", () => {
    const allIds = new Set(FEYNMAN_TOPICS.map((t) => t.id));
    for (const topic of FEYNMAN_TOPICS) {
      if (topic.prerequisites) {
        for (const pid of topic.prerequisites) {
          expect(allIds.has(pid)).toBe(true);
        }
      }
    }
  });

  it("no topic lists itself as a prerequisite", () => {
    for (const topic of FEYNMAN_TOPICS) {
      if (topic.prerequisites) {
        expect(topic.prerequisites).not.toContain(topic.id);
      }
    }
  });

  it("prerequisites are same-domain or cross-domain (no broken chains)", () => {
    for (const topic of FEYNMAN_TOPICS) {
      if (topic.prerequisites) {
        for (const pid of topic.prerequisites) {
          const prereq = getTopicById(pid);
          expect(prereq).toBeDefined();
        }
      }
    }
  });

  it("beginner topics have no prerequisites", () => {
    const beginners = FEYNMAN_TOPICS.filter((t) => t.difficulty === "beginner");
    for (const t of beginners) {
      expect(t.prerequisites ?? []).toHaveLength(0);
    }
  });

  it("no circular dependencies exist (chain terminates)", () => {
    for (const topic of FEYNMAN_TOPICS) {
      if (topic.prerequisites?.length) {
        const chain = getPrerequisiteChain(topic.id);
        // Chain should not contain the topic itself
        expect(chain.map((t) => t.id)).not.toContain(topic.id);
        // Chain length should be bounded
        expect(chain.length).toBeLessThan(FEYNMAN_TOPICS.length);
      }
    }
  });
});