|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; |
| 2 | +import { |
| 3 | + clearComponentGuideIntentCache, |
| 4 | + detectComponentGuideIntent, |
| 5 | + extractLatestUserText, |
| 6 | +} from "./component-guide-intent"; |
| 7 | +import { clearLlmsPropsCache } from "./llms-props"; |
| 8 | + |
| 9 | +const SAMPLE_REACT_LLMS_INDEX = `# SEED Design React - LLM Reference |
| 10 | +
|
| 11 | +### components |
| 12 | +
|
| 13 | +- [Box](https://seed-design.io/llms/react/components/layout/box.txt) |
| 14 | +- [Alert Dialog](https://seed-design.io/llms/react/components/alert-dialog.txt) |
| 15 | +`; |
| 16 | + |
| 17 | +describe("detectComponentGuideIntent", () => { |
| 18 | + const originalFetch = globalThis.fetch; |
| 19 | + const componentIds = ["alert-dialog", "action-button", "checkbox"]; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + clearComponentGuideIntentCache(); |
| 23 | + clearLlmsPropsCache(); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + clearComponentGuideIntentCache(); |
| 28 | + clearLlmsPropsCache(); |
| 29 | + globalThis.fetch = originalFetch; |
| 30 | + }); |
| 31 | + |
| 32 | + it("detects Korean usage query with spaced component name", async () => { |
| 33 | + const result = await detectComponentGuideIntent("alert dialog 어떻게 사용해?", { componentIds }); |
| 34 | + |
| 35 | + expect(result?.type).toBe("component-guide"); |
| 36 | + expect(result?.component.id).toBe("alert-dialog"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("detects PascalCase component query", async () => { |
| 40 | + const result = await detectComponentGuideIntent("AlertDialog 설치 방법 알려줘", { componentIds }); |
| 41 | + |
| 42 | + expect(result?.component.id).toBe("alert-dialog"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("detects kebab-case component props query", async () => { |
| 46 | + const result = await detectComponentGuideIntent("alert-dialog props 알려줘", { componentIds }); |
| 47 | + |
| 48 | + expect(result?.component.id).toBe("alert-dialog"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns null when query is not a guide question", async () => { |
| 52 | + const result = await detectComponentGuideIntent("최근 배포 이슈 요약해줘", { componentIds }); |
| 53 | + |
| 54 | + expect(result).toBeNull(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("loads component ids from react/llms.txt using baseUrl", async () => { |
| 58 | + globalThis.fetch = (async (input) => { |
| 59 | + const url = typeof input === "string" ? input : input.url; |
| 60 | + if (url === "https://seed-design.io/react/llms.txt") { |
| 61 | + return new Response(SAMPLE_REACT_LLMS_INDEX, { status: 200 }); |
| 62 | + } |
| 63 | + |
| 64 | + return new Response("not found", { status: 404 }); |
| 65 | + }) as typeof fetch; |
| 66 | + |
| 67 | + const result = await detectComponentGuideIntent("box props는 어떤게 있어?", { |
| 68 | + baseUrl: "https://seed-design.io", |
| 69 | + }); |
| 70 | + |
| 71 | + expect(result?.type).toBe("component-guide"); |
| 72 | + expect(result?.component.id).toBe("box"); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe("extractLatestUserText", () => { |
| 77 | + it("extracts latest user text from message parts", () => { |
| 78 | + const messages = [ |
| 79 | + { |
| 80 | + role: "user", |
| 81 | + parts: [{ type: "text", text: "이전 질문" }], |
| 82 | + }, |
| 83 | + { |
| 84 | + role: "assistant", |
| 85 | + parts: [{ type: "text", text: "이전 답변" }], |
| 86 | + }, |
| 87 | + { |
| 88 | + role: "user", |
| 89 | + parts: [{ type: "text", text: "alert dialog 어떻게 사용해?" }], |
| 90 | + }, |
| 91 | + ]; |
| 92 | + |
| 93 | + expect(extractLatestUserText(messages)).toBe("alert dialog 어떻게 사용해?"); |
| 94 | + }); |
| 95 | +}); |
0 commit comments