RotNot / src /lib /debate /__tests__ /topics.test.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
8.85 kB
import { describe, it, expect } from "vitest";
import {
DEBATE_TOPICS,
DEBATE_CATEGORIES,
getRandomDebateTopic,
getDebateTopicChoices,
getDebateTopicById,
type DebateCategory,
} from "@/lib/debate/topics";
// ---------------------------------------------------------------------------
// Data integrity
// ---------------------------------------------------------------------------
describe("DEBATE_TOPICS data integrity", () => {
it("has at least 48 topics", () => {
expect(DEBATE_TOPICS.length).toBeGreaterThanOrEqual(48);
});
it("has unique IDs", () => {
const ids = DEBATE_TOPICS.map((t) => t.id);
expect(new Set(ids).size).toBe(ids.length);
});
it("all IDs follow dt-<prefix>-NN format", () => {
for (const topic of DEBATE_TOPICS) {
expect(topic.id).toMatch(/^dt-[a-z]+-\d{2}$/);
}
});
it("every topic has a non-empty topic string", () => {
for (const topic of DEBATE_TOPICS) {
expect(topic.topic.trim().length).toBeGreaterThan(0);
}
});
it("every topic has a non-empty description", () => {
for (const topic of DEBATE_TOPICS) {
expect(topic.description.trim().length).toBeGreaterThan(0);
}
});
it("every topic has a valid category", () => {
for (const topic of DEBATE_TOPICS) {
expect(DEBATE_CATEGORIES).toContain(topic.category);
}
});
it("every topic has a valid framing", () => {
for (const topic of DEBATE_TOPICS) {
expect(["pro-con", "open"]).toContain(topic.framing);
}
});
it("covers all 8 categories", () => {
const categories = new Set(DEBATE_TOPICS.map((t) => t.category));
expect(categories.size).toBe(DEBATE_CATEGORIES.length);
for (const cat of DEBATE_CATEGORIES) {
expect(categories.has(cat)).toBe(true);
}
});
it("has at least 6 topics per category", () => {
for (const cat of DEBATE_CATEGORIES) {
const count = DEBATE_TOPICS.filter((t) => t.category === cat).length;
expect(count).toBeGreaterThanOrEqual(6);
}
});
it("topic strings are unique", () => {
const topics = DEBATE_TOPICS.map((t) => t.topic.toLowerCase());
expect(new Set(topics).size).toBe(topics.length);
});
});
// ---------------------------------------------------------------------------
// DEBATE_CATEGORIES
// ---------------------------------------------------------------------------
describe("DEBATE_CATEGORIES", () => {
it("has 8 categories", () => {
expect(DEBATE_CATEGORIES).toHaveLength(8);
});
it("contains no duplicates", () => {
expect(new Set(DEBATE_CATEGORIES).size).toBe(DEBATE_CATEGORIES.length);
});
});
// ---------------------------------------------------------------------------
// getRandomDebateTopic
// ---------------------------------------------------------------------------
describe("getRandomDebateTopic", () => {
it("returns a topic from the pool", () => {
const topic = getRandomDebateTopic();
expect(topic).toBeDefined();
expect(topic.id).toBeDefined();
expect(topic.topic).toBeDefined();
expect(DEBATE_TOPICS).toContain(topic);
});
it("filters by category when specified", () => {
const topic = getRandomDebateTopic("Ethics");
expect(topic.category).toBe("Ethics");
});
it("returns a topic even for nonexistent category (falls back to full pool)", () => {
const topic = getRandomDebateTopic("NonexistentCategory" as DebateCategory);
expect(topic).toBeDefined();
expect(DEBATE_TOPICS).toContain(topic);
});
it("returns topics from multiple categories over repeated calls", () => {
const categories = new Set<string>();
for (let i = 0; i < 100; i++) {
categories.add(getRandomDebateTopic().category);
}
expect(categories.size).toBeGreaterThan(1);
});
});
// ---------------------------------------------------------------------------
// getDebateTopicChoices
// ---------------------------------------------------------------------------
describe("getDebateTopicChoices", () => {
it("returns 3 topics by default", () => {
const choices = getDebateTopicChoices();
expect(choices).toHaveLength(3);
});
it("returns topics with unique IDs", () => {
const choices = getDebateTopicChoices();
const ids = choices.map((c) => c.id);
expect(new Set(ids).size).toBe(ids.length);
});
it("tries to include topics from different categories", () => {
let sawMultipleCategories = false;
for (let i = 0; i < 30; i++) {
const choices = getDebateTopicChoices();
const categories = new Set(choices.map((c) => c.category));
if (categories.size >= 2) {
sawMultipleCategories = true;
break;
}
}
expect(sawMultipleCategories).toBe(true);
});
it("respects custom count parameter", () => {
const choices = getDebateTopicChoices(undefined, 5);
expect(choices).toHaveLength(5);
});
it("returns all topics if count exceeds pool size for a category", () => {
const philTopics = DEBATE_TOPICS.filter((t) => t.category === "Philosophy");
const choices = getDebateTopicChoices("Philosophy", 100);
expect(choices).toHaveLength(philTopics.length);
});
it("filters by category when specified", () => {
const choices = getDebateTopicChoices("Science");
for (const c of choices) {
expect(c.category).toBe("Science");
}
});
it("returns empty array for nonexistent category", () => {
const choices = getDebateTopicChoices("Nonexistent" as DebateCategory);
expect(choices).toHaveLength(0);
});
it("returns 1 topic when count is 1", () => {
const choices = getDebateTopicChoices(undefined, 1);
expect(choices).toHaveLength(1);
});
it("all returned topics are from the DEBATE_TOPICS pool", () => {
const choices = getDebateTopicChoices(undefined, 5);
for (const c of choices) {
expect(DEBATE_TOPICS).toContain(c);
}
});
});
// ---------------------------------------------------------------------------
// getDebateTopicById
// ---------------------------------------------------------------------------
describe("getDebateTopicById", () => {
it("finds a topic by valid ID", () => {
const topic = getDebateTopicById("dt-phil-01");
expect(topic).toBeDefined();
expect(topic!.id).toBe("dt-phil-01");
expect(topic!.category).toBe("Philosophy");
});
it("returns undefined for invalid ID", () => {
expect(getDebateTopicById("nonexistent")).toBeUndefined();
});
it("returns undefined for empty string", () => {
expect(getDebateTopicById("")).toBeUndefined();
});
it("finds topics from different categories", () => {
expect(getDebateTopicById("dt-eth-01")?.category).toBe("Ethics");
expect(getDebateTopicById("dt-sci-01")?.category).toBe("Science");
expect(getDebateTopicById("dt-tech-01")?.category).toBe("Technology");
expect(getDebateTopicById("dt-econ-01")?.category).toBe("Economics");
});
it("finds newly added expansion topics", () => {
expect(getDebateTopicById("dt-phil-05")?.category).toBe("Philosophy");
expect(getDebateTopicById("dt-phil-06")?.category).toBe("Philosophy");
expect(getDebateTopicById("dt-eth-05")?.category).toBe("Ethics");
expect(getDebateTopicById("dt-eth-06")?.category).toBe("Ethics");
expect(getDebateTopicById("dt-sci-05")?.category).toBe("Science");
expect(getDebateTopicById("dt-sci-06")?.category).toBe("Science");
expect(getDebateTopicById("dt-pol-05")?.category).toBe("Politics");
expect(getDebateTopicById("dt-pol-06")?.category).toBe("Politics");
expect(getDebateTopicById("dt-econ-05")?.category).toBe("Economics");
expect(getDebateTopicById("dt-econ-06")?.category).toBe("Economics");
expect(getDebateTopicById("dt-tech-05")?.category).toBe("Technology");
expect(getDebateTopicById("dt-tech-06")?.category).toBe("Technology");
expect(getDebateTopicById("dt-edu-05")?.category).toBe("Education");
expect(getDebateTopicById("dt-edu-06")?.category).toBe("Education");
expect(getDebateTopicById("dt-soc-05")?.category).toBe("Society");
expect(getDebateTopicById("dt-soc-06")?.category).toBe("Society");
});
it("all expansion topics have non-empty topic strings and descriptions", () => {
const expansionIds = [
"dt-phil-05", "dt-phil-06",
"dt-eth-05", "dt-eth-06",
"dt-sci-05", "dt-sci-06",
"dt-pol-05", "dt-pol-06",
"dt-econ-05", "dt-econ-06",
"dt-tech-05", "dt-tech-06",
"dt-edu-05", "dt-edu-06",
"dt-soc-05", "dt-soc-06",
];
for (const id of expansionIds) {
const topic = getDebateTopicById(id);
expect(topic, `missing topic ${id}`).toBeDefined();
expect(topic!.topic.trim().length).toBeGreaterThan(0);
expect(topic!.description.trim().length).toBeGreaterThan(0);
expect(topic!.framing).toBe("pro-con");
}
});
});