File size: 8,975 Bytes
a0ebf39 | 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | /**
* Regression tests for GitHub issue #472:
* `languageDirective` is dropped or hardcoded across the scene generation pipeline,
* silently breaking prompt-level language control.
*
* The bug caused `{{languageDirective}}` to leak as a literal placeholder into
* LLM user messages. These tests thread a sentinel directive through every affected
* code path and assert it both reaches the rendered prompt AND the literal
* placeholder is gone.
*/
import { describe, expect, it, vi, afterEach } from 'vitest';
import { generateSceneContent, generateSceneActions } from '@/lib/generation/scene-generator';
import { buildSceneFromOutline } from '@/lib/generation/scene-builder';
import type { AICallFn } from '@/lib/generation/pipeline-types';
import type {
SceneOutline,
GeneratedSlideContent,
GeneratedQuizContent,
GeneratedInteractiveContent,
GeneratedPBLContent,
} from '@/lib/types/generation';
const DIRECTIVE = '<<LANG-DIRECTIVE-SENTINEL>>';
function makeCapturingAiCall(response: string): {
aiCall: AICallFn;
lastUser: () => string;
lastSystem: () => string;
} {
let lastUser = '';
let lastSystem = '';
const aiCall: AICallFn = async (system, user) => {
lastSystem = system;
lastUser = user;
return response;
};
return {
aiCall,
lastUser: () => lastUser,
lastSystem: () => lastSystem,
};
}
function baseOutline(overrides: Partial<SceneOutline> = {}): SceneOutline {
return {
id: 'scene-1',
type: 'slide',
title: 'Test Scene',
description: 'A scene for testing language directive threading.',
keyPoints: ['point a', 'point b'],
order: 0,
...overrides,
};
}
describe('scene-generator language directive threading (issue #472)', () => {
describe('content generation', () => {
it('threads languageDirective into slide content prompt', async () => {
const { aiCall, lastUser } = makeCapturingAiCall(
JSON.stringify({ elements: [], background: null, remark: '' }),
);
await generateSceneContent(baseOutline({ type: 'slide' }), aiCall, {
languageDirective: DIRECTIVE,
});
expect(lastUser()).toContain(DIRECTIVE);
expect(lastUser()).not.toContain('{{languageDirective}}');
});
it('threads languageDirective into quiz content prompt', async () => {
const { aiCall, lastUser } = makeCapturingAiCall(JSON.stringify([]));
await generateSceneContent(
baseOutline({
type: 'quiz',
quizConfig: {
questionCount: 1,
difficulty: 'easy',
questionTypes: ['single'],
},
}),
aiCall,
{ languageDirective: DIRECTIVE },
);
expect(lastUser()).toContain(DIRECTIVE);
expect(lastUser()).not.toContain('{{languageDirective}}');
});
});
describe('actions generation', () => {
it('threads languageDirective into slide actions prompt', async () => {
const { aiCall, lastUser } = makeCapturingAiCall('[]');
const content: GeneratedSlideContent = {
elements: [
{
id: 'text_1',
type: 'text',
left: 0,
top: 0,
width: 100,
height: 40,
content: '<p>hi</p>',
defaultFontName: '',
defaultColor: '#000',
rotate: 0,
},
],
background: undefined,
remark: '',
};
await generateSceneActions(baseOutline({ type: 'slide' }), content, aiCall, {
languageDirective: DIRECTIVE,
});
expect(lastUser()).toContain(DIRECTIVE);
expect(lastUser()).not.toContain('{{languageDirective}}');
});
it('threads languageDirective into quiz actions prompt', async () => {
const { aiCall, lastUser } = makeCapturingAiCall('[]');
const content: GeneratedQuizContent = {
questions: [
{
id: 'q1',
type: 'single',
question: 'x?',
options: [{ value: 'A', label: 'yes' }],
answer: ['A'],
hasAnswer: true,
},
],
};
await generateSceneActions(baseOutline({ type: 'quiz' }), content, aiCall, {
languageDirective: DIRECTIVE,
});
expect(lastUser()).toContain(DIRECTIVE);
expect(lastUser()).not.toContain('{{languageDirective}}');
});
it('threads languageDirective into interactive actions prompt', async () => {
const { aiCall, lastUser } = makeCapturingAiCall('[]');
const content: GeneratedInteractiveContent = {
html: '<div />',
// No widgetType/teacherActions so we hit the normal actions path
};
await generateSceneActions(baseOutline({ type: 'interactive' }), content, aiCall, {
languageDirective: DIRECTIVE,
});
expect(lastUser()).toContain(DIRECTIVE);
expect(lastUser()).not.toContain('{{languageDirective}}');
});
it('threads languageDirective into pbl actions prompt', async () => {
const { aiCall, lastUser } = makeCapturingAiCall('[]');
const content: GeneratedPBLContent = {
projectConfig: {
projectInfo: { title: 't', description: 'd' },
agents: [],
issueboard: { agent_ids: [], issues: [], current_issue_id: null },
chat: { messages: [] },
},
};
await generateSceneActions(
baseOutline({
type: 'pbl',
pblConfig: {
projectTopic: 't',
projectDescription: 'd',
targetSkills: [],
},
}),
content,
aiCall,
{ languageDirective: DIRECTIVE },
);
expect(lastUser()).toContain(DIRECTIVE);
expect(lastUser()).not.toContain('{{languageDirective}}');
});
});
describe('widget generation (interactive scenes)', () => {
it('threads languageDirective into widget content AND widget-teacher-actions prompts', async () => {
const captured: string[] = [];
// 1st call: widget HTML content; 2nd call: widget-teacher-actions JSON
const aiCall: AICallFn = async (_system, user) => {
captured.push(user);
return captured.length === 1
? '<!DOCTYPE html><html><body>widget</body></html>'
: JSON.stringify({ actions: [] });
};
await generateSceneContent(
baseOutline({
type: 'interactive',
widgetType: 'simulation',
widgetOutline: { concept: 'Projectile', keyVariables: ['angle'] },
}),
aiCall,
{ languageDirective: DIRECTIVE },
);
expect(captured).toHaveLength(2);
for (const user of captured) {
expect(user).toContain(DIRECTIVE);
expect(user).not.toContain('{{languageDirective}}');
expect(user).not.toContain('{{language}}');
}
});
});
describe('buildSceneFromOutline (high-level pipeline)', () => {
it('threads languageDirective through content AND actions for a slide', async () => {
const captured: string[] = [];
const aiCall: AICallFn = async (_system, user) => {
captured.push(user);
// First call is content (expects JSON); second is actions (expects array)
return captured.length === 1
? JSON.stringify({ elements: [], background: null, remark: '' })
: '[]';
};
await buildSceneFromOutline(
baseOutline({ type: 'slide' }),
aiCall,
'stage-1',
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
DIRECTIVE,
);
expect(captured).toHaveLength(2);
for (const user of captured) {
expect(user).toContain(DIRECTIVE);
expect(user).not.toContain('{{languageDirective}}');
}
});
});
describe('pbl content honors caller-provided directive', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('forwards options.languageDirective to generatePBLContent', async () => {
const pblModule = await import('@/lib/pbl/generate-pbl');
const spy = vi.spyOn(pblModule, 'generatePBLContent').mockResolvedValue({
projectInfo: { title: '', description: '' },
agents: [],
issueboard: { agent_ids: [], issues: [], current_issue_id: null },
chat: { messages: [] },
});
const aiCall: AICallFn = async () => '';
await generateSceneContent(
baseOutline({
type: 'pbl',
pblConfig: {
projectTopic: 't',
projectDescription: 'd',
targetSkills: [],
},
}),
aiCall,
{
languageDirective: DIRECTIVE,
languageModel: {} as unknown as import('ai').LanguageModel,
},
);
expect(spy).toHaveBeenCalledTimes(1);
const config = spy.mock.calls[0][0];
expect(config.languageDirective).toBe(DIRECTIVE);
});
});
});
|