Download src/lib/feynman/__tests__/prerequisites.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/feynman/__tests__/prerequisites.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/feynman/__tests__/prerequisites.test.ts
-
curl -L -o prerequisites.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/feynman/__tests__/prerequisites.test.ts
10.6 kB
| 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); | |
| } | |
| } | |
| }); | |
| }); | |