RotNot / src /lib /feynman /__tests__ /topics.test.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
11.1 kB
import { describe, it, expect } from "vitest";
import { getRandomTopic, getTopicChoices, FEYNMAN_TOPICS } from "@/lib/feynman/topics";
describe("getRandomTopic", () => {
it("returns a topic from the pool", () => {
const topic = getRandomTopic();
expect(topic).toBeDefined();
expect(topic.id).toBeDefined();
expect(topic.topic).toBeDefined();
});
it("filters by domain when specified", () => {
const topic = getRandomTopic("Physics");
expect(topic.domain).toBe("Physics");
});
});
describe("getTopicChoices", () => {
it("returns 3 topics by default", () => {
const choices = getTopicChoices();
expect(choices).toHaveLength(3);
});
it("returns topics with unique ids", () => {
const choices = getTopicChoices();
const ids = choices.map((c) => c.id);
expect(new Set(ids).size).toBe(ids.length);
});
it("includes mixed difficulty levels when available", () => {
// Run multiple times to account for randomness
let sawMixed = false;
for (let i = 0; i < 20; i++) {
const choices = getTopicChoices();
const difficulties = new Set(choices.map((c) => c.difficulty));
if (difficulties.size >= 2) {
sawMixed = true;
break;
}
}
expect(sawMixed).toBe(true);
});
it("filters by domain", () => {
const choices = getTopicChoices("CS");
expect(choices.length).toBeGreaterThan(0);
expect(choices.length).toBeLessThanOrEqual(3);
for (const c of choices) {
expect(c.domain).toBe("CS");
}
});
it("returns fewer topics if domain pool is small", () => {
// Create a scenario with a small domain β€” all our domains have > 3 topics
// so we test with an unknown domain
const choices = getTopicChoices("NonexistentDomain");
expect(choices).toHaveLength(0);
});
it("respects custom count parameter", () => {
const choices = getTopicChoices(undefined, 2);
expect(choices).toHaveLength(2);
});
it("caps at pool size if count exceeds pool", () => {
// All domains have 16 topics; request 100 β€” should return the full pool
const choices = getTopicChoices("Philosophy", 100);
const philTopics = FEYNMAN_TOPICS.filter((t) => t.domain === "Philosophy");
expect(choices).toHaveLength(philTopics.length);
});
});
describe("FEYNMAN_TOPICS library integrity", () => {
const DOMAINS = ["CS", "Physics", "Biology", "History", "Economics", "Philosophy", "Psychology", "Mathematics"];
it("has exactly 128 topics", () => {
expect(FEYNMAN_TOPICS).toHaveLength(128);
});
it("has exactly 8 domains", () => {
const domains = new Set(FEYNMAN_TOPICS.map((t) => t.domain));
expect(domains.size).toBe(8);
});
it("has exactly 16 topics per domain", () => {
for (const domain of DOMAINS) {
const count = FEYNMAN_TOPICS.filter((t) => t.domain === domain).length;
expect(count).toBe(16);
}
});
it("all topic IDs are unique", () => {
const ids = FEYNMAN_TOPICS.map((t) => t.id);
expect(new Set(ids).size).toBe(ids.length);
});
it("finds newly added Biology expansion topics (bi-13 to bi-16)", () => {
const newIds = ["bi-13", "bi-14", "bi-15", "bi-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("Biology");
}
});
it("finds newly added History expansion topics (hi-13 to hi-16)", () => {
const newIds = ["hi-13", "hi-14", "hi-15", "hi-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("History");
}
});
it("finds newly added Philosophy expansion topics (pl-13 to pl-16)", () => {
const newIds = ["pl-13", "pl-14", "pl-15", "pl-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("Philosophy");
}
});
it("finds newly added Mathematics expansion topics (ma-13 to ma-16)", () => {
const newIds = ["ma-13", "ma-14", "ma-15", "ma-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("Mathematics");
}
});
// ── Phase 1 expansion topics (cs/ph/ec/ps 9β†’16) ──────────────────────────
it("finds newly added CS expansion topics (cs-13 to cs-16)", () => {
const newIds = ["cs-13", "cs-14", "cs-15", "cs-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("CS");
}
});
it("finds newly added Physics expansion topics (ph-13 to ph-16)", () => {
const newIds = ["ph-13", "ph-14", "ph-15", "ph-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("Physics");
}
});
it("finds newly added Economics expansion topics (ec-13 to ec-16)", () => {
const newIds = ["ec-13", "ec-14", "ec-15", "ec-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("Economics");
}
});
it("finds newly added Psychology expansion topics (ps-13 to ps-16)", () => {
const newIds = ["ps-13", "ps-14", "ps-15", "ps-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("Psychology");
}
});
it("CS expansion topics cs-14 and cs-15 are beginner (no prerequisites)", () => {
const beginnerIds = ["cs-14", "cs-15", "ph-15", "ec-13", "ps-13"];
for (const id of beginnerIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t?.prerequisites ?? []).toHaveLength(0);
}
});
it("CS/Physics/Economics/Psychology expansion topics have non-empty topic strings", () => {
const newIds = [
"cs-13", "cs-14", "cs-15", "cs-16",
"ph-13", "ph-14", "ph-15", "ph-16",
"ec-13", "ec-14", "ec-15", "ec-16",
"ps-13", "ps-14", "ps-15", "ps-16",
];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t?.topic.length).toBeGreaterThan(0);
}
});
it("prerequisite IDs in CS/Physics/Economics/Psychology expansion topics reference existing topics", () => {
const allIds = new Set(FEYNMAN_TOPICS.map((t) => t.id));
const newIds = [
"cs-13", "cs-16",
"ph-13", "ph-14", "ph-16",
"ec-14", "ec-15", "ec-16",
"ps-14", "ps-15", "ps-16",
];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
for (const prereq of t?.prerequisites ?? []) {
expect(allIds.has(prereq), `Topic ${id} has unknown prereq: ${prereq}`).toBe(true);
}
}
});
// ── Phase 2 expansion topics (bi/hi/pl/ma 12β†’16) ─────────────────────────
it("finds newly added Biology expansion topics (bi-13 to bi-16)", () => {
const newIds = ["bi-13", "bi-14", "bi-15", "bi-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("Biology");
}
});
it("finds newly added History expansion topics (hi-13 to hi-16)", () => {
const newIds = ["hi-13", "hi-14", "hi-15", "hi-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("History");
}
});
it("finds newly added Philosophy expansion topics (pl-13 to pl-16)", () => {
const newIds = ["pl-13", "pl-14", "pl-15", "pl-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("Philosophy");
}
});
it("finds newly added Mathematics expansion topics (ma-13 to ma-16)", () => {
const newIds = ["ma-13", "ma-14", "ma-15", "ma-16"];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t).toBeDefined();
expect(t?.domain).toBe("Mathematics");
}
});
it("all new topics have non-empty topic strings", () => {
const newIds = [
"bi-13", "bi-14", "bi-15", "bi-16",
"hi-13", "hi-14", "hi-15", "hi-16",
"pl-13", "pl-14", "pl-15", "pl-16",
"ma-13", "ma-14", "ma-15", "ma-16",
];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t?.topic.length).toBeGreaterThan(0);
}
});
it("beginner topics among new additions have no prerequisites", () => {
const newBeginners = ["bi-14", "hi-13", "pl-13", "ma-13"];
for (const id of newBeginners) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
expect(t?.prerequisites ?? []).toHaveLength(0);
}
});
it("all prerequisite IDs in new topics reference existing topics", () => {
const allIds = new Set(FEYNMAN_TOPICS.map((t) => t.id));
const newIds = [
"bi-13", "bi-15", "bi-16",
"hi-14", "hi-15", "hi-16",
"pl-14", "pl-15", "pl-16",
"ma-14", "ma-15", "ma-16",
];
for (const id of newIds) {
const t = FEYNMAN_TOPICS.find((t) => t.id === id);
for (const prereq of t?.prerequisites ?? []) {
expect(allIds.has(prereq)).toBe(true);
}
}
});
// ── Global invariants (all 128 topics) ───────────────────────────────────
it("every topic has a valid difficulty level", () => {
const VALID_DIFFICULTIES = new Set(["beginner", "intermediate", "expert"]);
for (const t of FEYNMAN_TOPICS) {
expect(VALID_DIFFICULTIES.has(t.difficulty), `Topic ${t.id} has invalid difficulty: ${t.difficulty}`).toBe(true);
}
});
it("all topic IDs follow domain-prefix format (e.g. cs-01)", () => {
const ID_RE = /^[a-z]{2}-\d{2}$/;
for (const t of FEYNMAN_TOPICS) {
expect(ID_RE.test(t.id), `Topic ${t.id} does not match expected format`).toBe(true);
}
});
it("no topic has a self-referential prerequisite", () => {
for (const t of FEYNMAN_TOPICS) {
expect(t.prerequisites ?? []).not.toContain(t.id);
}
});
it("all prerequisite IDs in the entire library reference existing topics", () => {
const allIds = new Set(FEYNMAN_TOPICS.map((t) => t.id));
for (const t of FEYNMAN_TOPICS) {
for (const prereq of t.prerequisites ?? []) {
expect(allIds.has(prereq), `Topic ${t.id} references unknown prereq: ${prereq}`).toBe(true);
}
}
});
it("beginner topics never have prerequisites", () => {
const beginners = FEYNMAN_TOPICS.filter((t) => t.difficulty === "beginner");
for (const t of beginners) {
expect(t.prerequisites ?? [], `Beginner topic ${t.id} should not have prerequisites`).toHaveLength(0);
}
});
});