|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | 2 | import { ArgumentError } from '@jackwener/opencli/errors'; |
3 | | -import { parseQianwenSessionId } from './utils.js'; |
| 3 | +import { parseQianwenSessionId, waitForAnswer } from './utils.js'; |
| 4 | + |
| 5 | +describe('qwen waitForAnswer baseline anchoring', () => { |
| 6 | + // page.evaluate serves both getMessageBubbles (data-chat-question-wrap) and |
| 7 | + // hasLoginGate (alert-biz-modal); route by inspecting the injected JS. |
| 8 | + const fakePage = (bubbles) => ({ |
| 9 | + wait: () => new Promise((r) => setTimeout(r, 5)), |
| 10 | + evaluate: (js) => Promise.resolve( |
| 11 | + String(js).includes('alert-biz-modal') ? false : bubbles, |
| 12 | + ), |
| 13 | + }); |
| 14 | + |
| 15 | + it('does not return the pre-send assistant turn (it is skipped via baseline id)', async () => { |
| 16 | + // Only the previous, already-complete answer is present. With the |
| 17 | + // baseline anchored to its id, waitForAnswer must skip it and time out |
| 18 | + // rather than return the stale answer as the new reply. |
| 19 | + const bubbles = [{ id: 'a1', role: 'Assistant', text: 'old answer', html: '' }]; |
| 20 | + const result = await waitForAnswer(fakePage(bubbles), 'new question', 0.05, 'a1'); |
| 21 | + expect(result.status).toBe('timeout'); |
| 22 | + expect(result.assistant).toBeUndefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns only an assistant turn after the newly sent prompt', async () => { |
| 26 | + const bubbles = [ |
| 27 | + { id: 'u1', role: 'User', text: 'old question', html: '' }, |
| 28 | + { id: 'a1', role: 'Assistant', text: 'old answer', html: '' }, |
| 29 | + { id: 'u2', role: 'User', text: 'new question', html: '' }, |
| 30 | + { id: 'a2', role: 'Assistant', text: 'new answer', html: '<p>new answer</p>' }, |
| 31 | + ]; |
| 32 | + const result = await waitForAnswer( |
| 33 | + fakePage(bubbles), |
| 34 | + 'new question', |
| 35 | + 0.2, |
| 36 | + { lastBubbleId: 'a1', lastAssistantId: 'a1' }, |
| 37 | + ); |
| 38 | + expect(result.status).toBe('partial'); |
| 39 | + expect(result.assistant).toEqual(bubbles[3]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('does not match a repeated prompt that existed before the pre-send anchor', async () => { |
| 43 | + const bubbles = [ |
| 44 | + { id: 'u1', role: 'User', text: 'same prompt', html: '' }, |
| 45 | + { id: 'a1', role: 'Assistant', text: 'old answer', html: '' }, |
| 46 | + ]; |
| 47 | + const result = await waitForAnswer( |
| 48 | + fakePage(bubbles), |
| 49 | + 'same prompt', |
| 50 | + 0.05, |
| 51 | + { lastBubbleId: 'a1', lastAssistantId: 'a1' }, |
| 52 | + ); |
| 53 | + expect(result.status).toBe('timeout'); |
| 54 | + expect(result.assistant).toBeUndefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('does not scan the visible transcript when a non-empty baseline anchor is missing', async () => { |
| 58 | + const bubbles = [ |
| 59 | + { id: 'u1-new-visible-id', role: 'User', text: 'same prompt', html: '' }, |
| 60 | + { id: 'a1-new-visible-id', role: 'Assistant', text: 'old answer', html: '' }, |
| 61 | + ]; |
| 62 | + const result = await waitForAnswer( |
| 63 | + fakePage(bubbles), |
| 64 | + 'same prompt', |
| 65 | + 0.05, |
| 66 | + { lastBubbleId: 'a-anchor-missing', lastAssistantId: 'a-anchor-missing' }, |
| 67 | + ); |
| 68 | + expect(result.status).toBe('timeout'); |
| 69 | + expect(result.assistant).toBeUndefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('supports a fresh chat with no baseline anchor', async () => { |
| 73 | + const bubbles = [ |
| 74 | + { id: 'u1', role: 'User', text: 'fresh question', html: '' }, |
| 75 | + { id: 'a1', role: 'Assistant', text: 'fresh answer', html: '' }, |
| 76 | + ]; |
| 77 | + const result = await waitForAnswer( |
| 78 | + fakePage(bubbles), |
| 79 | + 'fresh question', |
| 80 | + 0.2, |
| 81 | + { lastBubbleId: '', lastAssistantId: '' }, |
| 82 | + ); |
| 83 | + expect(result.status).toBe('partial'); |
| 84 | + expect(result.assistant).toEqual(bubbles[1]); |
| 85 | + }); |
| 86 | +}); |
4 | 87 |
|
5 | 88 | describe('qwen parseQianwenSessionId', () => { |
6 | 89 | const id = 'abcd1234ef567890abcd1234ef567890'; |
|
0 commit comments