File size: 8,846 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 | 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");
}
});
});
|