skill-user-invoke.e2e.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Web e2e scenario: a user invokes a disable-model-invocation skill through
  2. // the composer (issue #1470). The entered `/name args` line claims into
  3. // skill.invoke: the real host forwards the gesture as an ordinary user
  4. // prompt, injects the rendered body as instructions context named after the
  5. // skill, and starts a turn answered by the replay adapter. The transcript shows
  6. // the gesture bubble, the collapsed context-injection row, and the reply.
  7. import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
  8. import { tmpdir } from 'node:os'
  9. import { fileURLToPath } from 'node:url'
  10. import { join } from 'node:path'
  11. import type { Browser, Page } from 'playwright'
  12. import { chromium } from 'playwright'
  13. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  14. import type { ReplayOverrideDoc } from '@deepseek-ai/dsh-llm-replay'
  15. import {
  16. assertFixtureInventory,
  17. captureStableAria,
  18. compareOrRefreshGolden,
  19. launchWebScaffold,
  20. watchConsole,
  21. webSnapshotMode,
  22. type WebScaffold,
  23. } from './scaffold.ts'
  24. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  25. const SNAPSHOT_DIR = fileURLToPath(new URL('./expected/skill-user-invoke', import.meta.url))
  26. const UI_EXPECTED = join(SNAPSHOT_DIR, 'ui.expected.md')
  27. const MODE = webSnapshotMode()
  28. const SKILL_NAME = 'user-invoke-demo'
  29. const ARGS_TEXT = 'and confirm the fixture wiring'
  30. const REPLY = 'USER_INVOKE_REPLY acknowledged; following the injected skill.'
  31. async function seedUserOnlySkill(workspaceCwd: string): Promise<void> {
  32. const directory = join(workspaceCwd, 'workspace', '.agents', 'skills', SKILL_NAME)
  33. await mkdir(directory, { recursive: true })
  34. await writeFile(join(directory, 'SKILL.md'), [
  35. '---',
  36. `name: ${SKILL_NAME}`,
  37. 'description: Prove user-explicit invocation of a model-hidden skill',
  38. 'disable-model-invocation: true',
  39. '---',
  40. '',
  41. 'Reply with the fixture acknowledgement line.',
  42. '',
  43. ].join('\n'))
  44. }
  45. const REPLAY: ReplayOverrideDoc = [{
  46. kind: 'chunks',
  47. chunks: [
  48. { type: 'block-start', index: 0, blockType: 'text' },
  49. { type: 'text-delta', index: 0, text: REPLY },
  50. { type: 'block-end', index: 0, block: { type: 'text', text: REPLY } },
  51. { type: 'usage', usage: { inputTokens: 256, outputTokens: 16 } },
  52. { type: 'finish', reason: { kind: 'stop' } },
  53. ],
  54. }]
  55. describe.skipIf(MODE === 'record')('web e2e: user-explicit skill invocation through the composer', () => {
  56. let scaffold: WebScaffold
  57. let browser: Browser
  58. let page: Page
  59. let replayDir: string
  60. let tripwire: ReturnType<typeof watchConsole>
  61. beforeAll(async () => {
  62. replayDir = await mkdtemp(join(tmpdir(), 'dsh-skill-user-invoke-replay-'))
  63. const replayOverride = join(replayDir, 'replay.override.json')
  64. await writeFile(replayOverride, JSON.stringify(REPLAY))
  65. scaffold = await launchWebScaffold({
  66. replayFixture: join(replayDir, 'override-only.jsonl'),
  67. replayOverride,
  68. // Paced replay keeps the timing-derived chrome (TTFT / tok/s) present
  69. // deterministically; instant playback races it in and out of the golden.
  70. paceMs: 10,
  71. })
  72. await seedUserOnlySkill(scaffold.workspaceCwd)
  73. browser = await chromium.launch()
  74. page = await newEnglishPage(browser)
  75. tripwire = watchConsole(page)
  76. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  77. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  78. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  79. }, 120_000)
  80. afterAll(async () => {
  81. const failures: unknown[] = []
  82. await browser?.close().catch((error: unknown) => failures.push(error))
  83. await scaffold?.close().catch((error: unknown) => failures.push(error))
  84. if (replayDir !== undefined) {
  85. await rm(replayDir, { recursive: true, force: true })
  86. .catch((error: unknown) => failures.push(error))
  87. }
  88. if (failures.length === 1) throw failures[0]
  89. if (failures.length > 1) throw new AggregateError(failures, 'skill-user-invoke e2e cleanup failed')
  90. })
  91. it('claims /name args into a gesture bubble, an injection row, and a replayed answer', async () => {
  92. onTestFailed(() => saveFailureShot(page, 'web-e2e-skill-user-invoke'))
  93. const composer = page.locator('textarea:enabled').last()
  94. await composer.waitFor({ timeout: 15_000 })
  95. // The menu lists the user-only skill (its only entry point) before enter.
  96. await composer.fill(`/${SKILL_NAME}`)
  97. const menu = page.getByRole('listbox', { name: 'Trigger suggestions' })
  98. await expect.poll(
  99. () => menu.getByRole('option', { name: new RegExp(SKILL_NAME) }).count(),
  100. { timeout: 10_000 },
  101. ).toBe(1)
  102. const settled = scaffold.whenTurnSettled()
  103. await composer.fill(`/${SKILL_NAME} ${ARGS_TEXT}`)
  104. await composer.press('Enter')
  105. // The gesture stays an ordinary user bubble (decorated /name token plus
  106. // the trailing text), ahead of the injected context.
  107. const bubble = page.locator('[data-ref-chip="skill"]').first()
  108. await bubble.waitFor({ timeout: 15_000 })
  109. expect(await bubble.textContent()).toBe(`/${SKILL_NAME}`)
  110. // The rendered body arrives as a context-injection row named after the
  111. // skill; expanding it reveals the canonical <skill_content> block, and
  112. // the user's text is NOT folded into it.
  113. const injectionRow = page.getByRole('button', { name: `Context injection ${SKILL_NAME}` })
  114. await injectionRow.waitFor({ timeout: 15_000 })
  115. await injectionRow.click()
  116. const injectionBody = page
  117. .locator('[data-context-injection-body]')
  118. .filter({ hasText: `<skill_content name="${SKILL_NAME}">` })
  119. await injectionBody.waitFor({ timeout: 10_000 })
  120. const injected = await injectionBody.textContent()
  121. expect(injected).toContain('Reply with the fixture acknowledgement line.')
  122. expect(injected).not.toContain(ARGS_TEXT)
  123. await injectionRow.click()
  124. // The injection started a turn; the replay adapter answers it.
  125. await page.getByText('USER_INVOKE_REPLY', { exact: false }).first().waitFor({ timeout: 20_000 })
  126. await settled
  127. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  128. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  129. expect(tripwire.pageErrors).toEqual([])
  130. expect(tripwire.warnings).toEqual([])
  131. }, 60_000)
  132. it('keeps its snapshot inventory closed', async () => {
  133. await assertFixtureInventory(SNAPSHOT_DIR, ['ui.expected.md'])
  134. })
  135. })