Share-link generation and med math on request
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 34s
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 34s
- Visitors of class and exam links can generate NCLEX-style quizzes (default and maximum 50 questions, 25% select-all, Python-checked, optional 5 med math questions) and flashcard sets from chosen class materials, once the owner turns it on for the link. - 4 quizzes and 2 flashcard sets per link per day; failed attempts do not count; "Reset limits" in the share dialog gives a fresh allowance. - Sets are drafts tied to the link (migration 7): listed for every visitor, explainable, kept off the owner's drafts list, and reviewable or savable from the share dialog. - Generate gains "Include med math" with a count (default 5): exactly that many dosage calculation questions at the chosen difficulty, spread through the quiz, even when the material has none. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
25bc03ea75
commit
e945f589ce
16 changed files with 807 additions and 44 deletions
|
|
@ -139,3 +139,124 @@ describe('OpenRouter replaces the Ollama type', () => {
|
|||
expect(rows.map((r) => r.base_url)).toEqual(['http://localhost:11434/v1', 'http://box:11434/v1', 'http://box:11434/v1', 'http://box:11434/v1']);
|
||||
});
|
||||
});
|
||||
|
||||
/* ------------------------------ med math and share-link generation ------------------------------ */
|
||||
|
||||
async function waitDraft(id: string): Promise<any> {
|
||||
for (let i = 0; i < 400; i += 1) {
|
||||
const draft = (await t.api('GET', `/api/drafts/${id}`)).body.draft;
|
||||
if (draft.status !== 'generating') return draft;
|
||||
await new Promise((r) => setTimeout(r, 25));
|
||||
}
|
||||
throw new Error('generation did not finish');
|
||||
}
|
||||
|
||||
async function classWithMaterial() {
|
||||
const cls = (await t.api('POST', '/api/classes', { name: 'Pharm' })).body.class;
|
||||
const doc = (await t.api('POST', '/api/documents/text', { classId: cls.id, title: 'Heart failure', text: '# Heart failure\n\nFurosemide is a loop diuretic. Monitor potassium and daily weights.' })).body.document;
|
||||
await queueIdle(t.ctx);
|
||||
return { cls, doc };
|
||||
}
|
||||
|
||||
describe('med math on request', () => {
|
||||
it('writes exactly that many calculations, spread through the quiz, even without calculations in the material', async () => {
|
||||
await connectAi();
|
||||
const { cls } = await classWithMaterial();
|
||||
const started = await t.api('POST', '/api/ai/generate', { kind: 'quiz', classId: cls.id, source: { allClassDocuments: true }, count: 8, mathQuestions: 2, verify: false });
|
||||
expect(started.status).toBe(202);
|
||||
const draft = await waitDraft(started.body.draftId);
|
||||
expect(draft.status).toBe('ready');
|
||||
const types = draft.payload.questions.map((q: any) => q.type);
|
||||
expect(types).toHaveLength(8);
|
||||
expect(types.filter((x: string) => x === 'numeric')).toHaveLength(2);
|
||||
// Not bunched at the end.
|
||||
expect(types.slice(-2)).not.toEqual(['numeric', 'numeric']);
|
||||
const sent = JSON.stringify(chatCalls().map((r) => r.body.messages));
|
||||
expect(sent).toContain('medication math');
|
||||
});
|
||||
|
||||
it('spreads items evenly', async () => {
|
||||
const { spreadEvenly } = await import('../server/ai/generate.ts');
|
||||
const list = [...Array.from({ length: 8 }, (_, i) => `r${i}`), 'm0', 'm1'];
|
||||
expect(spreadEvenly(list, (x) => x.startsWith('m'))).toEqual(['r0', 'r1', 'm0', 'r2', 'r3', 'r4', 'r5', 'm1', 'r6', 'r7']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('share-link visitors can generate', () => {
|
||||
beforeEach(async () => {
|
||||
const { resetShareAiLimits } = await import('../server/routes/shares.ts');
|
||||
resetShareAiLimits();
|
||||
});
|
||||
|
||||
async function waitSets(token: string): Promise<any[]> {
|
||||
for (let i = 0; i < 400; i += 1) {
|
||||
const res = await t.api('GET', `/api/public/${token}/generated`);
|
||||
if (!res.body.sets.some((s: any) => s.status === 'generating')) return res.body.sets;
|
||||
await new Promise((r) => setTimeout(r, 25));
|
||||
}
|
||||
throw new Error('generation did not finish');
|
||||
}
|
||||
|
||||
it('is off until the owner allows it, then makes quizzes and flashcards within daily limits', async () => {
|
||||
await connectAi();
|
||||
const { cls, doc } = await classWithMaterial();
|
||||
const share = (await t.api('POST', '/api/shares', { kind: 'class', targetId: cls.id })).body.share;
|
||||
const token = share.token;
|
||||
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 4 })).status).toBe(403);
|
||||
await t.api('PATCH', `/api/shares/${share.id}`, { ai: { generate: true, explain: true } });
|
||||
expect((await t.api('GET', `/api/public/${token}`)).body.ai.generate).toBe(true);
|
||||
|
||||
// At most 50 questions, materials from the link only.
|
||||
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 51 })).status).toBe(422);
|
||||
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', documentIds: ['not-shared'] })).status).toBe(404);
|
||||
|
||||
mock.requests.length = 0;
|
||||
const first = await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 8, difficulty: 'hard', documentIds: [doc.id], math: true });
|
||||
expect(first.status).toBe(202);
|
||||
expect(first.body.usage.quiz).toEqual({ used: 1, limit: 4 });
|
||||
const [set] = await waitSets(token);
|
||||
expect(set).toMatchObject({ kind: 'quiz', status: 'ready', count: 8 });
|
||||
const quiz = (await t.api('GET', `/api/public/${token}/items/ai/${set.id}`)).body;
|
||||
expect(quiz.kind).toBe('quiz');
|
||||
expect(quiz.questions.filter((q: any) => q.type === 'numeric')).toHaveLength(5);
|
||||
expect(quiz.questions[0].id).toBe('q1');
|
||||
const sent = JSON.stringify(chatCalls().map((r) => r.body.messages));
|
||||
expect(sent).toContain('Difficulty: hard');
|
||||
expect(sent).toMatch(/Exactly 1 of them must be select-all-that-apply/);
|
||||
|
||||
// Explain works on a generated question.
|
||||
const explained = await t.raw('POST', `/api/public/${token}/explain`, { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ kind: 'question', quizId: set.id, questionId: 'q1' }) });
|
||||
expect(explained.status).toBe(200);
|
||||
|
||||
// The owner's drafts list does not show visitors' sets; the share settings do.
|
||||
expect((await t.api('GET', '/api/drafts')).body.drafts.some((d: any) => d.id === set.id)).toBe(false);
|
||||
const listed = (await t.api('GET', '/api/shares')).body.shares.find((s: any) => s.id === share.id);
|
||||
expect(listed.generated.sets.map((s: any) => s.id)).toEqual([set.id]);
|
||||
|
||||
// Four quizzes and two flashcard sets a day.
|
||||
for (let i = 0; i < 3; i += 1) expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 2 })).status).toBe(202);
|
||||
const over = await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 2 });
|
||||
expect(over.status).toBe(429);
|
||||
expect(over.body.error.message).toContain('4 quizzes');
|
||||
for (let i = 0; i < 2; i += 1) expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'deck', count: 3 })).status).toBe(202);
|
||||
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'deck', count: 3 })).status).toBe(429);
|
||||
await waitSets(token);
|
||||
|
||||
// The owner resets the allowance.
|
||||
const reset = await t.api('POST', `/api/shares/${share.id}/generate/reset`);
|
||||
expect(reset.body.generated.usage.quiz.used).toBe(0);
|
||||
expect((await t.api('POST', `/api/public/${token}/generate`, { kind: 'quiz', count: 2 })).status).toBe(202);
|
||||
await waitSets(token);
|
||||
});
|
||||
|
||||
it('defaults to a 50-question quiz', async () => {
|
||||
await connectAi();
|
||||
const { cls } = await classWithMaterial();
|
||||
const share = (await t.api('POST', '/api/shares', { kind: 'class', targetId: cls.id })).body.share;
|
||||
await t.api('PATCH', `/api/shares/${share.id}`, { ai: { generate: true } });
|
||||
const res = await t.api('POST', `/api/public/${share.token}/generate`, { kind: 'quiz' });
|
||||
expect(res.status).toBe(202);
|
||||
const draft = await waitDraft(res.body.set.id);
|
||||
expect(draft.meta.request).toMatchObject({ count: 50, sataShare: 0.25, format: 'exam' });
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue