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

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