142 lines
4.3 KiB
TypeScript
142 lines
4.3 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import { PromptBuilder } from "./builder.js";
|
|
import type { DerivedState } from "../state/onboarding-state.js";
|
|
import type { OnboardingSession, PromptContext } from "../types.js";
|
|
|
|
function makeDerived(
|
|
overrides?: Partial<DerivedState>,
|
|
): DerivedState {
|
|
return {
|
|
phase: { kind: "photo-pending" },
|
|
intent: { kind: "photo-question" },
|
|
shouldRespond: true,
|
|
responseMode: "llm",
|
|
suggestedMaxTokens: 256,
|
|
allowShortResponse: false,
|
|
tier: 0,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function makeSession(
|
|
overrides?: Partial<OnboardingSession>,
|
|
): OnboardingSession {
|
|
return {
|
|
chatroomId: "room-1",
|
|
deadlineAt: "2026-04-16T12:00:00.000Z",
|
|
joinedAt: "2026-04-16T10:00:00.000Z",
|
|
kakaotalkNickname: "프리리/95/여/D",
|
|
kakaotalkNicknameAliases: [],
|
|
kakaotalkNicknameVerified: true,
|
|
lastEventAt: "2026-04-16T10:30:00.000Z",
|
|
nicknameVerified: true,
|
|
photoVerified: false,
|
|
status: "pending",
|
|
userId: "user-1",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function makeContext(
|
|
overrides?: Partial<PromptContext>,
|
|
): PromptContext {
|
|
return {
|
|
blackboard: {
|
|
flaggedIssues: [],
|
|
openQuestions: [],
|
|
recentNicknameAttempts: [],
|
|
toneMemory: {},
|
|
},
|
|
conversationHistory: Array.from({ length: 7 }, (_, index) => ({
|
|
role: index % 2 === 0 ? "user" : "assistant",
|
|
content: `message-${index + 1}`,
|
|
timestamp: `2026-04-16T10:0${index}:00.000Z`,
|
|
})),
|
|
summary: "이전 대화 요약",
|
|
userMessage: "하트 인증은 어떻게 해요?",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("PromptBuilder", () => {
|
|
afterEach(() => {
|
|
delete process.env["AGENT_SKILL_MD_MODE"];
|
|
delete process.env["AGENT_USER_MEMORY_ENABLED"];
|
|
});
|
|
|
|
it("includes intent-matched examples in system prompt", () => {
|
|
const messages = PromptBuilder.forDerived(
|
|
makeDerived(),
|
|
makeSession(),
|
|
makeContext(),
|
|
).build();
|
|
|
|
expect(messages[0]?.role).toBe("system");
|
|
expect(messages[0]?.content).toContain("하트 인증이 뭐예요?");
|
|
expect(messages[0]?.content).not.toContain("나이 제한 있나요?");
|
|
expect(messages[0]?.content).toContain("카카오톡 닉네임");
|
|
});
|
|
|
|
it("limits prompt history to the latest five turns when summary exists", () => {
|
|
const messages = PromptBuilder.forDerived(
|
|
makeDerived(),
|
|
makeSession(),
|
|
makeContext(),
|
|
).build();
|
|
|
|
expect(messages).toHaveLength(7);
|
|
expect(messages[1]?.content).toBe("message-3");
|
|
expect(messages[5]?.content).toBe("message-7");
|
|
expect(messages[0]?.content).toContain("이전 대화 요약");
|
|
});
|
|
|
|
it("uses SKILL.md L1/L2 prompt layers when enabled", () => {
|
|
const basePrompt = PromptBuilder.forDerived(
|
|
makeDerived(),
|
|
makeSession(),
|
|
makeContext(),
|
|
).buildSystemPrompt();
|
|
process.env["AGENT_SKILL_MD_MODE"] = "true";
|
|
|
|
const messages = PromptBuilder.forDerived(
|
|
makeDerived(),
|
|
makeSession(),
|
|
makeContext(),
|
|
).build();
|
|
|
|
expect(messages[0]?.content).toContain("[사용 가능한 SKILL.md]");
|
|
expect(messages[0]?.content).toContain("[ACTIVE_SKILL:onboarding]");
|
|
expect(messages[0]?.content).toContain("카카오톡 오픈채팅 새내기 등록");
|
|
expect(messages[0]?.content).toContain("[현재 유저 상태]");
|
|
expect(messages[0]?.content.length ?? 0).toBeLessThan(basePrompt.length * 0.65);
|
|
});
|
|
|
|
it("injects user memory and adaptive examples when enabled", () => {
|
|
process.env["AGENT_USER_MEMORY_ENABLED"] = "true";
|
|
|
|
const messages = PromptBuilder.forDerived(
|
|
makeDerived({
|
|
intent: { kind: "nickname-question" },
|
|
phase: { kind: "nickname-rejected", current: "bad", issues: [] },
|
|
}),
|
|
makeSession(),
|
|
makeContext({
|
|
userProfile: {
|
|
firstSeenAt: "2026-05-01T00:00:00.000Z",
|
|
flaggedIssues: [],
|
|
lastSeenAt: "2026-05-02T00:00:00.000Z",
|
|
pastIntents: [{ count: 3, kind: "nickname-question" }],
|
|
preferredStyle: "concise",
|
|
totalTurns: 3,
|
|
userId: "user_12345678",
|
|
},
|
|
}),
|
|
).build();
|
|
|
|
expect(messages[0]?.content).toContain("[유저 메모리]");
|
|
expect(messages[0]?.content).toContain("preferredStyle: concise");
|
|
expect(messages[0]?.content).toContain("닉네임이 왜 안 돼요?");
|
|
});
|
|
});
|