File size: 7,108 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 | import { describe, it, expect } from "vitest";
import {
ALL_OPPONENTS,
getOpponentsByDifficulty,
getRandomOpponent,
getOpponentById,
getOpponentSampler,
type OpponentDifficulty,
} from "@/lib/debate/opponents";
// ---------------------------------------------------------------------------
// Data integrity
// ---------------------------------------------------------------------------
describe("ALL_OPPONENTS data integrity", () => {
it("has at least 10 opponents", () => {
expect(ALL_OPPONENTS.length).toBeGreaterThanOrEqual(10);
});
it("has unique IDs", () => {
const ids = ALL_OPPONENTS.map((o) => o.id);
expect(new Set(ids).size).toBe(ids.length);
});
it("all IDs follow opp-<difficulty>-<name> format", () => {
for (const opp of ALL_OPPONENTS) {
expect(opp.id).toMatch(/^opp-(easy|med|hard)-[a-z]+$/);
}
});
it("every opponent has a non-empty name", () => {
for (const opp of ALL_OPPONENTS) {
expect(opp.name.trim().length).toBeGreaterThan(0);
}
});
it("every opponent has a non-empty description", () => {
for (const opp of ALL_OPPONENTS) {
expect(opp.description.trim().length).toBeGreaterThan(0);
}
});
it("every opponent has a non-empty systemPrompt", () => {
for (const opp of ALL_OPPONENTS) {
expect(opp.systemPrompt.trim().length).toBeGreaterThan(50);
}
});
it("every opponent has a non-empty icon", () => {
for (const opp of ALL_OPPONENTS) {
expect(opp.icon.trim().length).toBeGreaterThan(0);
}
});
it("every opponent has a valid difficulty", () => {
const validDifficulties: OpponentDifficulty[] = ["easy", "medium", "hard"];
for (const opp of ALL_OPPONENTS) {
expect(validDifficulties).toContain(opp.difficulty);
}
});
it("has opponents at all three difficulty levels", () => {
const difficulties = new Set(ALL_OPPONENTS.map((o) => o.difficulty));
expect(difficulties.has("easy")).toBe(true);
expect(difficulties.has("medium")).toBe(true);
expect(difficulties.has("hard")).toBe(true);
});
it("has at least 2 easy opponents", () => {
expect(ALL_OPPONENTS.filter((o) => o.difficulty === "easy").length).toBeGreaterThanOrEqual(2);
});
it("has at least 3 medium opponents", () => {
expect(ALL_OPPONENTS.filter((o) => o.difficulty === "medium").length).toBeGreaterThanOrEqual(3);
});
it("has at least 5 hard opponents (historical figures)", () => {
expect(ALL_OPPONENTS.filter((o) => o.difficulty === "hard").length).toBeGreaterThanOrEqual(5);
});
it("all hard opponents have an era field", () => {
for (const opp of ALL_OPPONENTS.filter((o) => o.difficulty === "hard")) {
expect(opp.era).toBeDefined();
expect(opp.era!.trim().length).toBeGreaterThan(0);
}
});
it("easy and medium opponents do NOT have an era field", () => {
for (const opp of ALL_OPPONENTS.filter((o) => o.difficulty !== "hard")) {
expect(opp.era).toBeUndefined();
}
});
it("unique names across all opponents", () => {
const names = ALL_OPPONENTS.map((o) => o.name);
expect(new Set(names).size).toBe(names.length);
});
});
// ---------------------------------------------------------------------------
// getOpponentsByDifficulty
// ---------------------------------------------------------------------------
describe("getOpponentsByDifficulty", () => {
it("returns only easy opponents for 'easy'", () => {
const opponents = getOpponentsByDifficulty("easy");
expect(opponents.length).toBeGreaterThan(0);
for (const opp of opponents) {
expect(opp.difficulty).toBe("easy");
}
});
it("returns only medium opponents for 'medium'", () => {
const opponents = getOpponentsByDifficulty("medium");
expect(opponents.length).toBeGreaterThan(0);
for (const opp of opponents) {
expect(opp.difficulty).toBe("medium");
}
});
it("returns only hard opponents for 'hard'", () => {
const opponents = getOpponentsByDifficulty("hard");
expect(opponents.length).toBeGreaterThan(0);
for (const opp of opponents) {
expect(opp.difficulty).toBe("hard");
}
});
it("returns complete set — sum equals ALL_OPPONENTS length", () => {
const easy = getOpponentsByDifficulty("easy");
const medium = getOpponentsByDifficulty("medium");
const hard = getOpponentsByDifficulty("hard");
expect(easy.length + medium.length + hard.length).toBe(ALL_OPPONENTS.length);
});
});
// ---------------------------------------------------------------------------
// getRandomOpponent
// ---------------------------------------------------------------------------
describe("getRandomOpponent", () => {
it("returns an easy opponent", () => {
const opp = getRandomOpponent("easy");
expect(opp.difficulty).toBe("easy");
expect(ALL_OPPONENTS).toContain(opp);
});
it("returns a medium opponent", () => {
const opp = getRandomOpponent("medium");
expect(opp.difficulty).toBe("medium");
});
it("returns a hard opponent", () => {
const opp = getRandomOpponent("hard");
expect(opp.difficulty).toBe("hard");
});
it("returns different hard opponents over multiple calls", () => {
const names = new Set<string>();
for (let i = 0; i < 50; i++) {
names.add(getRandomOpponent("hard").name);
}
expect(names.size).toBeGreaterThan(1);
});
});
// ---------------------------------------------------------------------------
// getOpponentById
// ---------------------------------------------------------------------------
describe("getOpponentById", () => {
it("finds an opponent by valid ID", () => {
const opp = getOpponentById("opp-hard-socrates");
expect(opp).toBeDefined();
expect(opp!.name).toBe("Socrates");
expect(opp!.difficulty).toBe("hard");
});
it("returns undefined for invalid ID", () => {
expect(getOpponentById("nonexistent")).toBeUndefined();
});
it("returns undefined for empty string", () => {
expect(getOpponentById("")).toBeUndefined();
});
it("finds opponents at each difficulty", () => {
expect(getOpponentById("opp-easy-balanced")?.difficulty).toBe("easy");
expect(getOpponentById("opp-med-analyst")?.difficulty).toBe("medium");
expect(getOpponentById("opp-hard-voltaire")?.difficulty).toBe("hard");
});
});
// ---------------------------------------------------------------------------
// getOpponentSampler
// ---------------------------------------------------------------------------
describe("getOpponentSampler", () => {
it("returns exactly 3 opponents", () => {
const sample = getOpponentSampler();
expect(sample).toHaveLength(3);
});
it("returns [easy, medium, hard] in order", () => {
const [easy, medium, hard] = getOpponentSampler();
expect(easy.difficulty).toBe("easy");
expect(medium.difficulty).toBe("medium");
expect(hard.difficulty).toBe("hard");
});
it("all three are from ALL_OPPONENTS", () => {
const sample = getOpponentSampler();
for (const opp of sample) {
expect(ALL_OPPONENTS).toContain(opp);
}
});
});
|