itpdp/api/src/party/__tests__/question-generation.test.ts
2026-05-16 13:28:03 +02:00

233 lines
4.9 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import type { Question, QuizRound } from "../../party-types";
import { buildNumericQuestion } from "../numeric-question-generator";
import {
buildMemberOptions,
type PartyAnalytics,
type PartyQuestionMember,
pickQuestionCandidate,
} from "../question-utils";
import { buildSocialQuestion } from "../social-question-generator";
type Db = typeof import("../../db").db;
function makeChoiceQuestion(
text: string,
key: string,
subjectKey: string,
): Question {
return {
type: "choice",
text,
correct: 0,
startTimestamp: 1,
endTimestamp: 2,
points: 10,
options: ["A", "B"],
questionKey: key,
subjectKey,
};
}
function createFakeDb(trackReleaseDate: Date | null) {
const trackRecord = {
id: "track-1",
name: "Shared Track",
album: {
name: "Shared Album",
release_date: trackReleaseDate,
},
artists: [{ id: "artist-1", name: "Shared Artist" }],
};
return {
query: {
track: {
findFirst: vi.fn(async () => trackRecord),
findMany: vi.fn(async () => []),
},
artist: {
findFirst: vi.fn(async () => ({
id: "artist-1",
name: "Shared Artist",
})),
},
},
select: vi.fn(() => ({
from: () => ({
where: async () => [],
}),
})),
} as unknown as Db;
}
describe("question generation", () => {
it("skips repeated question keys, subjects, and text", () => {
const history: QuizRound[] = [
{
questionIndex: 0,
question: {
...makeChoiceQuestion(
"Which genre appears most in the party analytics?",
"audio:genre:pop",
"genre:pop",
),
},
responses: [],
},
];
const question = pickQuestionCandidate(
[
{
key: "audio:genre:pop",
subjectKey: "genre:pop",
question: makeChoiceQuestion(
"Which genre appears most in the party analytics?",
"audio:genre:pop",
"genre:pop",
),
},
{
key: "audio:artist:abba",
subjectKey: "artist:abba",
question: makeChoiceQuestion(
"Which artist shows up most often in the shared audio data?",
"audio:artist:abba",
"artist:abba",
),
},
],
history,
0,
);
expect(question?.text).toBe(
"Which artist shows up most often in the shared audio data?",
);
});
it("preserves candidate metadata on the generated question", () => {
const question = pickQuestionCandidate(
[
{
key: "social:leader",
subjectKey: "member:a",
question: {
type: "choice",
text: "Who is leading the quiz right now?",
correct: 0,
startTimestamp: 1,
endTimestamp: 2,
points: 10,
options: ["A", "B"],
} as Question,
},
],
[],
0,
);
expect(question?.questionKey).toBe("social:leader");
expect(question?.subjectKey).toBe("member:a");
});
it("returns null when member options would require fake placeholders", () => {
const members: PartyQuestionMember[] = [
{ userId: "a", name: "Sam" },
{ userId: "b", name: "Sam" },
];
const correctMember = members[0];
expect(correctMember).toBeDefined();
if (correctMember) {
expect(buildMemberOptions(correctMember, members)).toBeNull();
}
});
it("skips numeric questions with missing release dates and zero counts", async () => {
const db = createFakeDb(null);
const analytics = {
storyClusters: [
{
tracks: [
{
name: "Shared Track",
artists: [{ name: "Shared Artist" }],
albumName: "Shared Album",
},
],
artists: [{ name: "Shared Artist" }],
genres: [],
},
],
groupSummary: {
mostSharedGenres: [],
},
} as PartyAnalytics;
const members: PartyQuestionMember[] = [
{ userId: "a", name: "A" },
{ userId: "b", name: "B" },
];
const history: QuizRound[] = [
{
questionIndex: 0,
question: {
type: "numeric",
text: "What's the release year of Shared Album?",
correct: 2010,
startTimestamp: 1,
endTimestamp: 2,
points: 10,
range: { min: 2000, max: 2010 },
questionKey: "numeric:album-year:Shared Album",
subjectKey: "album:Shared Album",
},
responses: [],
},
];
const question = await buildNumericQuestion({
db,
analytics,
index: 0,
members,
history,
});
expect(question).toBeNull();
});
it("skips social fallback names for duplicate members", async () => {
const db = createFakeDb(null);
const members: PartyQuestionMember[] = [
{ userId: "a", name: "Sam" },
{ userId: "b", name: "Sam" },
];
const quizState = {
status: "running" as const,
workflowId: null,
questionIndex: 0,
currentQuestion: null,
answers: {},
scores: { a: 3, b: 1 },
history: [],
};
const question = await buildSocialQuestion(
db,
quizState,
{
groupSummary: {
mostDiverseMember: { userId: "a", genreEntropy: 1 },
mostSharedGenres: [],
mostAlignedPair: null,
},
},
members,
0,
);
expect(question).toBeNull();
});
});