返回 oh-my-ppt
pptx-chart-rewrite-agent.test.ts
根目录 / tests / unit / pptx-chart-rewrite-agent.test.ts
1 import { describe, expect, it, vi } from 'vitest'
2 import type { PptxChartRewriteRequest } from '../../src/main/io/pptx-import'
3
4 vi.mock('../../src/main/agent-runtime/model', () => ({
5 resolveModel: vi.fn(),
6 extractJsonBlock: (raw: string) => {
7 const match = raw.match(/```(?:json)?\s*([\s\S]*?)```/i)
8 return match?.[1]?.trim() || raw
9 },
10 extractModelText: (value: unknown) =>
11 typeof value === 'string'
12 ? value
13 : value && typeof value === 'object' && 'content' in value
14 ? String((value as { content?: unknown }).content || '')
15 : ''
16 }))
17
18 vi.mock('../../src/main/product-skills/paths', () => ({
19 resolveBuiltinSkillsSourcePath: vi.fn(() => '/tmp/resources/skills')
20 }))
21
22 vi.mock('@shared/model-timeout', () => ({
23 resolveModelTimeoutMs: vi.fn((value: number) => value)
24 }))
25
26 vi.mock('electron-log/main.js', () => ({
27 default: {
28 info: vi.fn(),
29 warn: vi.fn()
30 }
31 }))
32
33 import {
34 buildPptxChartRewriteSystemPrompt,
35 buildPptxChartRewriteUserPrompt,
36 parsePptxChartRewriteAgentResponse
37 } from '../../src/main/io/pptx-import/chart-rewrite-agent'
38
39 describe('pptx chart rewrite agent', () => {
40 it('tells the agent to preserve importer absolute positioning', () => {
41 const prompt = buildPptxChartRewriteSystemPrompt({
42 skill: 'Use PPT.createChart(canvasElement, config).',
43 reference: 'Chart.js examples'
44 })
45
46 expect(prompt).toContain('oh-my-ppt-chart/SKILL.md')
47 expect(prompt).toContain('style="position:absolute; left:...; top:...; width:...; height:...; ..."')
48 expect(prompt).toContain('Do not output HTML')
49 expect(prompt).toContain('Return only a Chart.js config object')
50 })
51
52 it('includes the importer frame style as immutable context', () => {
53 const request = {
54 element: {
55 type: 'chart',
56 chartType: 'stockChart',
57 data: [[1, 2]],
58 colors: ['#123456']
59 },
60 blockId: 'chart-1',
61 pageId: 'page-1',
62 chartIndex: 1,
63 canvasId: 'chart-page-1-1',
64 frameStyle: 'position:absolute;left:10px;top:20px;width:300px;height:180px;',
65 animationAttrs: 'data-anim="fade"',
66 pageNumber: 2
67 } as PptxChartRewriteRequest
68
69 const prompt = buildPptxChartRewriteUserPrompt(request)
70
71 expect(prompt).toContain('"blockId": "chart-1"')
72 expect(prompt).toContain('"canvasId": "chart-page-1-1"')
73 expect(prompt).toContain('"frameStyle": "position:absolute;left:10px;top:20px;width:300px;height:180px;"')
74 expect(prompt).toContain('"chartType": "stockChart"')
75 })
76
77 it('parses config JSON and forces responsive chart options', () => {
78 const result = parsePptxChartRewriteAgentResponse({
79 content: `\`\`\`json
80 {
81 "config": {
82 "type": "line",
83 "data": { "labels": ["A"], "datasets": [{ "data": [1] }] },
84 "options": { "maintainAspectRatio": true }
85 },
86 "warnings": ["simplified"]
87 }
88 \`\`\``
89 })
90
91 expect(result?.config.type).toBe('line')
92 expect(result?.config.options).toMatchObject({
93 responsive: true,
94 maintainAspectRatio: false
95 })
96 expect(result?.warnings).toEqual(['simplified'])
97 })
98 })
99
99 lines TYPESCRIPT