Files
makelore/tests/unit/teacher-guidance.test.ts
鲨鱼辣椒 dd96a7b7b4
Some checks failed
Electron E2E / Electron E2E (macos-latest) (push) Has been cancelled
Electron E2E / Electron E2E (ubuntu-latest) (push) Has been cancelled
Electron E2E / Electron E2E (windows-latest) (push) Has been cancelled
Merge Yuxi teacher support with classroom discussions
2026-09-24 10:27:36 +08:00

183 lines
12 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it } from 'vitest';
import examples from '../fixtures/teacher-guidance-examples.json';
import { TEACHER_BEHAVIOR_PROMPT } from '../../electron/coding-teacher/behavior-prompt';
import { compileTeacherContext, estimateTeacherTokens, teacherHistoryMessages } from '../../electron/coding-teacher/context';
import { consultationDefinition } from '../../electron/coding-teacher/consultation-role';
import { applyDiscussionReply, discussionInstructions, editDiscussion, validateDiscussionContext } from '../../electron/coding-teacher/discussion';
import { parseTeacherDiscussionReply } from '../../shared/teacher-discussion';
import type { TeacherDefinition, TeacherDiscussionAction, TeacherDiscussionContext, TeacherRequest, TeacherSourceContext, TeacherTopic } from '../../shared/coding-teacher';
const definition: TeacherDefinition = {
schema_version: 1, teacher_id: 'coding-teacher', name: '老师', description: '', avatar_id: 'avatar-01',
welcome_message: '一起想一想', suggested_questions: [], system_prompt: '运营发布的老师说明',
skills: [{ id: 'extra', name: '补充', description: '', instructions_markdown: '启用的教学补充', enabled: true },
{ id: 'off', name: '停用', description: '', instructions_markdown: '不应加载的资料', enabled: false }],
model: { model_id: 'configured-model', reasoning_choice: { mode: 'default' } },
limits: { max_input_tokens: 16000, max_output_tokens: 2000 },
};
const source: TeacherSourceContext = {
messages: [{ id: 'source', role: 'user', text: '我想做自己的作品' }],
cursor: { workerGeneration: 1, seq: 1 }, capturedAt: 'now',
};
function topic(): TeacherTopic {
return { id: 'topic', schemaVersion: 1, revision: 1, accountId: 'account', projectId: 'project',
sourceConversationId: 'source', version: 1, definition, createdAt: 'now', updatedAt: 'now', requests: [] };
}
function action(owner: TeacherTopic, value: TeacherDiscussionAction['action']) {
editDiscussion(owner, { toolId: owner.discussion!.id, revision: owner.discussion!.revision, action: value });
}
function turn(owner: TeacherTopic, text: string, response: unknown, extra: Partial<TeacherDiscussionContext> = {}) {
const context = owner.discussion?.status === 'active'
? validateDiscussionContext(owner, { toolId: owner.discussion.id, revision: owner.discussion.revision, ...extra }) : undefined;
const compiled = compileTeacherContext(definition, source, owner.requests, text, [], undefined, 'question', discussionInstructions(owner, context));
expect(compiled.messages[0].content).toContain(TEACHER_BEHAVIOR_PROMPT);
expect(compiled.includedSourceMessageIds).toEqual(['source']);
expect(estimateTeacherTokens(compiled.messages)).toBeLessThanOrEqual(definition.limits.max_input_tokens);
const request: TeacherRequest = { id: `turn-${owner.requests.length}`, text, references: [], createdAt: 'now',
sourceCursor: source.cursor, sourceCapturedAt: 'now', includedSourceMessageIds: compiled.includedSourceMessageIds,
omittedMessages: compiled.omittedMessages, status: 'running', response: '', presentation: 'discussion-v1', discussionContext: context };
applyDiscussionReply(owner, request, JSON.stringify(response));
expect(request.discussionError).toBeUndefined();
request.status = 'completed';
owner.requests.push(request);
return request;
}
describe('teacher behavior wiring and per-request formats', () => {
it('does not duplicate the same teaching baseline when operations publishes it', () => {
const configured = { ...definition, system_prompt: `\n${TEACHER_BEHAVIOR_PROMPT}\n` };
const compiled = compileTeacherContext(configured, source, [], '继续聊', []);
expect(compiled.messages[0].content.split(TEACHER_BEHAVIOR_PROMPT)).toHaveLength(2);
expect(compiled.messages[0].content).toContain('启用的教学补充');
expect(configured.system_prompt).toBe(`\n${TEACHER_BEHAVIOR_PROMPT}\n`);
});
it.each(['question', 'suggestions', 'guided-help', 'check-in'] as const)('applies teacher guidance to %s without mutating the cloud definition', intent => {
const before = structuredClone(definition);
const compiled = compileTeacherContext(definition, source, [], '帮我想一想', [], undefined, intent);
const system = compiled.messages[0].content;
expect(system.split(TEACHER_BEHAVIOR_PROMPT)).toHaveLength(2);
expect(system).toContain(definition.system_prompt);
expect(system).toContain('启用的教学补充');
expect(system).not.toContain('不应加载的资料');
expect(definition).toEqual(before);
expect(compiled.messages.at(-1)?.role).toBe(intent === 'check-in' ? 'system' : 'user');
if (intent === 'suggestions') expect(compiled.messages.at(-1)?.content).toContain('{"intro":string,"questions":string[]}');
expect(compiled.messages.some(message => message.content.includes('本轮界面协议'))).toBe(false);
});
it('leaves the friend persona and enabled teaching material isolated', () => {
const friend = consultationDefinition(definition, 'friend');
const compiled = compileTeacherContext(friend, source, [], '你觉得呢', [], undefined, 'question', undefined, true);
expect(compiled.messages[0].content).toContain(friend.system_prompt);
expect(compiled.messages[0].content).not.toContain(TEACHER_BEHAVIOR_PROMPT);
expect(compiled.messages[0].content).not.toContain('启用的教学补充');
expect(compiled.messages[0].content).not.toContain('你可以通过只读工具');
});
it('keeps the structured guided-help format alongside project read access and teaching guidance', () => {
const instructions = discussionInstructions(topic());
const compiled = compileTeacherContext(definition, source, [], '我说不清', [], undefined, 'guided-help', instructions, true);
expect(compiled.messages[0].content).toContain(TEACHER_BEHAVIOR_PROMPT);
expect(compiled.messages[0].content).toContain('你可以通过只读工具');
expect(compiled.messages[0].content).not.toContain('没有项目读取工具');
expect(compiled.messages.at(-2)).toEqual({ role: 'system', content: instructions });
expect(compiled.messages.at(-1)?.content).toContain('按本轮界面协议返回');
expect(compiled.messages.at(-1)?.content).not.toContain('不返回 JSON');
});
it('budgets readable history without fabricating a student turn for check-ins or losing suggested questions', () => {
const saved: TeacherRequest = {
id: 'check-in', intent: 'check-in', text: '内部主动触发', response: '刚才的作品有新进展。',
references: [], createdAt: 'now', sourceCursor: source.cursor, sourceCapturedAt: 'now',
includedSourceMessageIds: [], omittedMessages: 0, status: 'completed',
};
const history: TeacherRequest[] = [saved, {
...saved, id: 'latest', intent: 'suggestions', text: '一起聊什么?',
response: '先理解玩家想做什么。' + '需要考虑的作品细节。'.repeat(1200),
suggestedQuestions: ['怎样判断这个体验是否有趣?'],
}];
const original = structuredClone(history);
const readable = teacherHistoryMessages(history);
expect(readable.map(message => message.id)).toEqual([
'teacher:check-in:assistant', 'teacher:latest:user', 'teacher:latest:assistant',
]);
expect(readable.at(-1)?.text).toContain('怎样判断这个体验是否有趣?');
expect(readable.some(message => message.text.includes('内部主动触发'))).toBe(false);
const instructions = discussionInstructions(topic());
const fixed = compileTeacherContext(definition, source, [], '接着刚才的问题聊', [], undefined, 'question', instructions, true);
const budget = estimateTeacherTokens(fixed.messages) + 1200;
const compiled = compileTeacherContext(definition, source, history, '接着刚才的问题聊', [], budget, 'question', instructions, true);
const latest = compiled.messages.find(message => message.content.startsWith('[teacher:latest:assistant]'));
expect(latest?.content).toContain('先理解玩家想做什么');
expect(latest?.content).toContain('中间内容已省略');
expect(latest?.content).toContain('怎样判断这个体验是否有趣?');
expect(compiled.omittedMessages).toBe(1);
expect(compiled.truncatedMessages).toBeGreaterThan(0);
expect(compiled.messages.at(-2)).toEqual({ role: 'system', content: instructions });
expect(estimateTeacherTokens(compiled.messages)).toBeLessThanOrEqual(budget);
expect(history).toEqual(original);
expect(teacherHistoryMessages(history)).toEqual(readable);
});
});
describe('representative teaching examples against the real discussion contract', () => {
const responses = [examples.pet, examples.pet.followup, examples.pet.structure, examples.pet.paused,
examples.pet.finished, examples.garden, examples.garden.followup, examples.shooter, examples.cake];
it.each(responses)('parses the example: $response.reply', ({ response }) => {
const parsed = parseTeacherDiscussionReply(JSON.stringify(response));
expect(parsed.toolError).toBeUndefined();
expect(parsed.reply).toBe(response.reply);
expect(parsed.tool ?? null).toEqual(response.tool);
expect(parsed.quickReplies).toEqual(response.quickReplies);
});
it('continues the pet structure across discussion, pause, finish and return without accepting a teacher candidate', () => {
const owner = topic();
turn(owner, examples.pet.student, examples.pet.response);
expect(owner.discussion?.status).toBe('offered');
const identity = owner.discussion!.id;
action(owner, 'enter');
turn(owner, examples.pet.followup.student, examples.pet.followup.response, { focusId: 'dog' });
expect(owner.discussion?.content).toEqual(examples.pet.followup.response.tool);
turn(owner, examples.pet.structure.studentAction, examples.pet.structure.response, { transition: 'structure' });
expect(owner.discussion?.previousIdeas).toEqual(examples.pet.followup.response.tool);
expect(owner.discussion?.content).toEqual(examples.pet.structure.response.tool);
const structure = structuredClone(owner.discussion!.content);
action(owner, 'pause');
turn(owner, examples.pet.paused.student, examples.pet.paused.response);
expect(owner.discussion?.content).toEqual(structure);
action(owner, 'resume');
action(owner, 'finish');
turn(owner, examples.pet.finished.student, examples.pet.finished.response);
expect(owner.discussion).toMatchObject({ id: identity, status: 'finished', content: structure });
action(owner, 'resume');
action(owner, 'back-ideas');
expect(owner.discussion).toMatchObject({ id: identity, status: 'active', content: examples.pet.followup.response.tool });
expect(owner.discussion!.content).not.toHaveProperty('confirmed');
});
it('changes only the discussed garden outcome while retaining the condition, other outcome and edges', () => {
const owner = topic();
turn(owner, examples.garden.student, examples.garden.response);
action(owner, 'enter');
const before = structuredClone(owner.discussion!);
turn(owner, examples.garden.followup.student, examples.garden.followup.response, { focusId: 'wait' });
expect(owner.discussion?.id).toBe(before.id);
expect(owner.discussion?.content).toEqual(examples.garden.followup.response.tool);
expect(owner.requests[0].discussionSnapshot).toEqual(examples.garden.response.tool);
});
it('keeps a comparison as aligned discussion data and allows a creative project to stay in plain conversation', () => {
const compared = topic();
turn(compared, examples.shooter.student, examples.shooter.response);
expect(compared.discussion?.content).toEqual(examples.shooter.response.tool);
const plain = topic();
const request = turn(plain, examples.cake.student, examples.cake.response);
expect(plain.discussion).toBeUndefined();
expect(request.suggestedQuestions).toEqual(examples.cake.response.quickReplies);
});
});