markdown-inline-code-links.e2e.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { fileURLToPath } from 'node:url'
  2. import type { Browser, Page } from 'playwright'
  3. import { chromium } from 'playwright'
  4. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  5. import { createMessage, createUserMessage } from '@deepseek-ai/dsh-llm'
  6. import { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session'
  7. import type {} from '@deepseek-ai/dsh-session-title'
  8. import {
  9. assertFixtureInventory,
  10. captureStableAria,
  11. compareOrRefreshGolden,
  12. launchWebScaffold,
  13. seedSession,
  14. watchConsole,
  15. webSnapshotMode,
  16. type WebScaffold,
  17. } from './scaffold.ts'
  18. import { newEnglishPage, saveFailureShot } from './support.ts'
  19. const SNAPSHOT_DIR = fileURLToPath(new URL('./expected/markdown-inline-code-links', import.meta.url))
  20. const UI_EXPECTED = fileURLToPath(new URL('./expected/markdown-inline-code-links/ui.expected.md', import.meta.url))
  21. const MODE = webSnapshotMode()
  22. const SEED_ID = 'markdown-inline-code-links-web-e2e'
  23. const DONE = 'INLINE_CODE_LINK_DONE'
  24. /** Build a settled assistant reply with linkable URL code and inert code controls. */
  25. function markdownFixture(linkUrl: string): string {
  26. const session = Session.create(SessionId('markdown-inline-code-links-source'))
  27. const eventTimeOrigin = new Date().setHours(12, 0, 0, 0)
  28. session.append('turn/start', { turn: 1 })
  29. const user = session.append('user/message', createUserMessage({
  30. content: [{ type: 'text', text: 'Show the local preview URL.' }],
  31. source: { kind: 'user' },
  32. }), { surfaceOp: 'append' })
  33. session.append('session/title', {
  34. title: 'Inline code links',
  35. messageSeqs: [user.seq],
  36. source: { kind: 'fallback' },
  37. })
  38. session.append('step/start', { turn: 1, step: 1 })
  39. session.append('assistant/message', {
  40. stream: [],
  41. turn: 1,
  42. step: 1,
  43. message: createMessage({
  44. role: 'assistant',
  45. content: [{
  46. type: 'text',
  47. text: [
  48. '## Inline code links',
  49. '',
  50. `Preview: \`${linkUrl}\``,
  51. '',
  52. `Standard: [Open preview](${linkUrl})`,
  53. '',
  54. `Command: \`curl ${linkUrl}\``,
  55. '',
  56. 'Unsafe: `javascript:alert(1)`',
  57. '',
  58. DONE,
  59. ].join('\n'),
  60. }],
  61. source: { kind: 'model', provider: 'fixture', model: 'fixture' },
  62. }),
  63. }, { surfaceOp: 'append' })
  64. session.append('step/end', { turn: 1, step: 1 })
  65. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  66. return [
  67. JSON.stringify({
  68. type: 'session',
  69. version: SESSION_FORMAT_VERSION,
  70. id: '{{sessionId}}',
  71. createdAt: 0,
  72. cwd: '{{cwd}}',
  73. isSeeded: false,
  74. delegationDepth: 0,
  75. }),
  76. ...session.snapshotEvents().map(event => JSON.stringify({
  77. ...event,
  78. time: eventTimeOrigin + event.seq * 1_000,
  79. })),
  80. '',
  81. ].join('\n')
  82. }
  83. describe('web e2e: Markdown inline-code links', () => {
  84. let scaffold: WebScaffold
  85. let browser: Browser
  86. let page: Page
  87. let linkUrl: string
  88. let tripwire: ReturnType<typeof watchConsole>
  89. beforeAll(async () => {
  90. scaffold = await launchWebScaffold({})
  91. linkUrl = new URL('/?demo=1', scaffold.baseUrl).toString()
  92. await seedSession(scaffold, markdownFixture(linkUrl), SEED_ID)
  93. browser = await chromium.launch()
  94. page = await newEnglishPage(browser)
  95. tripwire = watchConsole(page)
  96. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  97. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  98. }, 120_000)
  99. afterAll(async () => {
  100. await browser?.close()
  101. await scaffold?.close()
  102. })
  103. it.skipIf(MODE === 'record')('opens a complete HTTP URL from inline code and leaves other code inert', async () => {
  104. onTestFailed(() => saveFailureShot(page, 'web-e2e-markdown-inline-code-links'))
  105. const groupRow = page.locator('[role="treeitem"]').first()
  106. await groupRow.waitFor({ timeout: 15_000 })
  107. await groupRow.click()
  108. const sessionRow = page.locator('[role="treeitem"]').nth(1)
  109. await sessionRow.waitFor({ timeout: 10_000 })
  110. await sessionRow.click()
  111. await expect.poll(() => page.getByText(DONE, { exact: true }).count(), { timeout: 15_000 }).toBe(1)
  112. const inlineCodeLink = page.locator('[class*="markdown"] code a')
  113. await expect.poll(() => inlineCodeLink.count(), { timeout: 10_000 }).toBe(1)
  114. expect(await inlineCodeLink.getAttribute('href')).toBe(linkUrl)
  115. expect(await inlineCodeLink.getAttribute('target')).toBe('_blank')
  116. expect(await inlineCodeLink.getAttribute('rel')).toBe('noopener noreferrer')
  117. await inlineCodeLink.focus()
  118. expect(await inlineCodeLink.evaluate(element => document.activeElement === element)).toBe(true)
  119. const popupPromise = page.waitForEvent('popup')
  120. await inlineCodeLink.click()
  121. const popup = await popupPromise
  122. await popup.waitForURL(linkUrl, { timeout: 15_000 })
  123. expect(popup.url()).toBe(linkUrl)
  124. await popup.close()
  125. expect(await page.getByText(`curl ${linkUrl}`, { exact: true }).locator('a').count()).toBe(0)
  126. expect(await page.getByText('javascript:alert(1)', { exact: true }).locator('a').count()).toBe(0)
  127. const snapshot = (await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd))
  128. .split(SEED_ID).join('{{seededId}}')
  129. .split(linkUrl).join('{{linkUrl}}')
  130. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  131. expect(tripwire.pageErrors).toEqual([])
  132. expect(tripwire.warnings).toEqual([])
  133. await assertFixtureInventory(SNAPSHOT_DIR, ['ui.expected.md'])
  134. }, 60_000)
  135. })