|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { IPage } from '../../types.js'; |
| 3 | +import { AuthRequiredError, CommandExecutionError, TimeoutError } from '../../errors.js'; |
| 4 | +import { __test__ } from './ask.js'; |
| 5 | +import { askCommand } from './ask.js'; |
| 6 | + |
| 7 | +describe('yuanbao ask helpers', () => { |
| 8 | + describe('isOnYuanbao', () => { |
| 9 | + const fakePage = (url: string | Error): IPage => |
| 10 | + ({ evaluate: () => url instanceof Error ? Promise.reject(url) : Promise.resolve(url) }) as unknown as IPage; |
| 11 | + |
| 12 | + it('returns true for yuanbao.tencent.com URLs', async () => { |
| 13 | + expect(await __test__.isOnYuanbao(fakePage('https://yuanbao.tencent.com/'))).toBe(true); |
| 14 | + expect(await __test__.isOnYuanbao(fakePage('https://yuanbao.tencent.com/chat/abc'))).toBe(true); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns false for non-yuanbao domains', async () => { |
| 18 | + expect(await __test__.isOnYuanbao(fakePage('https://example.com/?next=yuanbao.tencent.com'))).toBe(false); |
| 19 | + expect(await __test__.isOnYuanbao(fakePage('about:blank'))).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns false when evaluate throws', async () => { |
| 23 | + expect(await __test__.isOnYuanbao(fakePage(new Error('detached')))).toBe(false); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('removes echoed prompt prefixes from transcript additions', () => { |
| 28 | + expect(__test__.sanitizeYuanbaoResponseText('你好\n你好,我是元宝。', '你好')).toBe('你好,我是元宝。'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('filters transient in-progress assistant placeholders', () => { |
| 32 | + expect(__test__.sanitizeYuanbaoResponseText('正在搜索资料', '张雪机车相关的股票有哪些?')).toBe(''); |
| 33 | + }); |
| 34 | + |
| 35 | + it('normalizes boolean flags with explicit defaults', () => { |
| 36 | + expect(__test__.normalizeBooleanFlag(undefined, true)).toBe(true); |
| 37 | + expect(__test__.normalizeBooleanFlag(undefined, false)).toBe(false); |
| 38 | + expect(__test__.normalizeBooleanFlag('true', false)).toBe(true); |
| 39 | + expect(__test__.normalizeBooleanFlag('1', false)).toBe(true); |
| 40 | + expect(__test__.normalizeBooleanFlag('yes', false)).toBe(true); |
| 41 | + expect(__test__.normalizeBooleanFlag('false', true)).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it('ignores baseline lines and echoed prompts when collecting additions', () => { |
| 45 | + const response = __test__.collectYuanbaoTranscriptAdditions( |
| 46 | + ['旧消息'], |
| 47 | + ['旧消息', '你好', '你好\n你好,我是元宝。'], |
| 48 | + '你好', |
| 49 | + ); |
| 50 | + |
| 51 | + expect(response).toBe('你好,我是元宝。'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('prefers fresh assistant messages over echoed prompts and older messages', () => { |
| 55 | + const response = __test__.pickLatestYuanbaoAssistantCandidate( |
| 56 | + ['旧回复', '你好', '你好!我是元宝,由腾讯推出的AI助手。'], |
| 57 | + 1, |
| 58 | + '你好', |
| 59 | + ); |
| 60 | + |
| 61 | + expect(response).toBe('你好!我是元宝,由腾讯推出的AI助手。'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('converts assistant html tables to markdown tables via turndown', () => { |
| 65 | + const markdown = __test__.convertYuanbaoHtmlToMarkdown(` |
| 66 | + <h3>核心产业链概念股一览</h3> |
| 67 | + <table> |
| 68 | + <thead> |
| 69 | + <tr><th>细分赛道</th><th>核心标的</th></tr> |
| 70 | + </thead> |
| 71 | + <tbody> |
| 72 | + <tr><td>光模块</td><td>中际旭创</td></tr> |
| 73 | + </tbody> |
| 74 | + </table> |
| 75 | + `); |
| 76 | + |
| 77 | + expect(markdown).toContain('### 核心产业链概念股一览'); |
| 78 | + expect(markdown).toContain('| 细分赛道 | 核心标的 |'); |
| 79 | + expect(markdown).toContain('| --- | --- |'); |
| 80 | + expect(markdown).toContain('| 光模块 | 中际旭创 |'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('tracks stabilization by incrementing repeats and resetting on changes', () => { |
| 84 | + expect(__test__.updateStableState('', 0, '第一段')).toEqual({ |
| 85 | + previousText: '第一段', |
| 86 | + stableCount: 0, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(__test__.updateStableState('第一段', 0, '第一段')).toEqual({ |
| 90 | + previousText: '第一段', |
| 91 | + stableCount: 1, |
| 92 | + }); |
| 93 | + |
| 94 | + expect(__test__.updateStableState('第一段', 1, '第二段')).toEqual({ |
| 95 | + previousText: '第二段', |
| 96 | + stableCount: 0, |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +function createAskPageMock(overrides: { |
| 102 | + currentUrl?: string; |
| 103 | + hasLoginGate?: boolean; |
| 104 | + sendResult?: { ok?: boolean; reason?: string; detail?: string; action?: string }; |
| 105 | +} = {}): IPage { |
| 106 | + const currentUrl = overrides.currentUrl ?? 'https://yuanbao.tencent.com/'; |
| 107 | + const hasLoginGate = overrides.hasLoginGate ?? false; |
| 108 | + const sendResult = overrides.sendResult; |
| 109 | + |
| 110 | + return { |
| 111 | + goto: vi.fn().mockResolvedValue(undefined), |
| 112 | + wait: vi.fn().mockResolvedValue(undefined), |
| 113 | + evaluate: vi.fn().mockImplementation(async (script: string) => { |
| 114 | + if (script === 'window.location.href') return currentUrl; |
| 115 | + if (script.includes('微信扫码登录')) return hasLoginGate; |
| 116 | + if (script.includes('[dt-button-id="internet_search"]')) return { found: false, enabled: false }; |
| 117 | + if (script.includes('[dt-button-id="deep_think"]')) return { found: false, enabled: false }; |
| 118 | + if (script.includes('.agent-chat__list__item--ai')) return []; |
| 119 | + if (script.includes('const stopLines = new Set([')) return []; |
| 120 | + if (script.includes('Failed to insert the prompt into the Yuanbao composer.')) { |
| 121 | + return sendResult ?? { ok: true, action: 'click' }; |
| 122 | + } |
| 123 | + throw new Error(`Unexpected evaluate script in test: ${script.slice(0, 80)}`); |
| 124 | + }), |
| 125 | + } as unknown as IPage; |
| 126 | +} |
| 127 | + |
| 128 | +describe('yuanbao ask command', () => { |
| 129 | + it('throws AuthRequiredError when Yuanbao shows a login gate before sending', async () => { |
| 130 | + const page = createAskPageMock({ hasLoginGate: true }); |
| 131 | + |
| 132 | + await expect(askCommand.func!(page, { prompt: '你好', timeout: '60', search: true, think: false })) |
| 133 | + .rejects.toBeInstanceOf(AuthRequiredError); |
| 134 | + }); |
| 135 | + |
| 136 | + it('throws CommandExecutionError when the prompt cannot be sent', async () => { |
| 137 | + const page = createAskPageMock({ |
| 138 | + sendResult: { |
| 139 | + ok: false, |
| 140 | + reason: 'Yuanbao composer was not found.', |
| 141 | + }, |
| 142 | + }); |
| 143 | + |
| 144 | + await expect(askCommand.func!(page, { prompt: '你好', timeout: '60', search: true, think: false })) |
| 145 | + .rejects.toBeInstanceOf(CommandExecutionError); |
| 146 | + }); |
| 147 | + |
| 148 | + it('throws TimeoutError when no response arrives before timeout', async () => { |
| 149 | + const page = createAskPageMock({ |
| 150 | + sendResult: { ok: true, action: 'click' }, |
| 151 | + }); |
| 152 | + |
| 153 | + await expect(askCommand.func!(page, { prompt: '你好', timeout: '-1', search: true, think: false })) |
| 154 | + .rejects.toBeInstanceOf(TimeoutError); |
| 155 | + }); |
| 156 | +}); |
0 commit comments