skill-invocation-policy.e2e.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Web e2e scenario: the real host serves every user-invocable skill to the
  2. // browser slash source — user-only (disable-model-invocation) entries appear
  3. // with their marker while user-disabled quadrants stay hidden. A real
  4. // chromium connects a fresh workspace seeded with all four policy quadrants;
  5. // no model call is issued, so a stray stream fails loud on the open LLM seam.
  6. import { mkdir, writeFile } from 'node:fs/promises'
  7. import { fileURLToPath } from 'node:url'
  8. import { join } from 'node:path'
  9. import type { Browser, Page } from 'playwright'
  10. import { chromium } from 'playwright'
  11. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  12. import {
  13. assertFixtureInventory,
  14. captureStableAria,
  15. compareOrRefreshGolden,
  16. launchWebScaffold,
  17. watchConsole,
  18. webSnapshotMode,
  19. type WebScaffold,
  20. } from './scaffold.ts'
  21. import { connectFreshWorkspace, newEnglishPage, saveFailureShot, writeComposerDraft } from './support.ts'
  22. const SNAPSHOT_DIR = fileURLToPath(new URL('./expected/skill-invocation-policy', import.meta.url))
  23. const MENU_EXPECTED = join(SNAPSHOT_DIR, 'menu.expected.md')
  24. const FUZZY_MENU_EXPECTED = join(SNAPSHOT_DIR, 'menu-fuzzy.expected.md')
  25. const MODE = webSnapshotMode()
  26. interface SeedSkill {
  27. name: string
  28. description: string
  29. frontmatter: string
  30. }
  31. const SKILLS: readonly SeedSkill[] = [
  32. {
  33. name: 'policy-shared',
  34. description: 'Available to both model and user invocation',
  35. frontmatter: '',
  36. },
  37. {
  38. name: 'policy-model-only',
  39. description: 'Available only to model invocation',
  40. frontmatter: 'user-invocable: false\n',
  41. },
  42. {
  43. name: 'policy-user-only',
  44. description: 'Available only to user invocation',
  45. frontmatter: 'disable-model-invocation: true\n',
  46. },
  47. {
  48. name: 'policy-trusted-only',
  49. description: 'Available only to trusted internal callers',
  50. frontmatter: 'disable-model-invocation: true\nuser-invocable: false\n',
  51. },
  52. ]
  53. async function seedSkills(workspaceCwd: string): Promise<void> {
  54. for (const skill of SKILLS) {
  55. const directory = join(workspaceCwd, 'workspace', '.agents', 'skills', skill.name)
  56. await mkdir(directory, { recursive: true })
  57. const policyLines = skill.frontmatter === '' ? [] : skill.frontmatter.trimEnd().split('\n')
  58. await writeFile(join(directory, 'SKILL.md'), [
  59. '---',
  60. `name: ${skill.name}`,
  61. `description: ${skill.description}`,
  62. ...policyLines,
  63. '---',
  64. '',
  65. `# ${skill.name}`,
  66. '',
  67. ].join('\n'))
  68. }
  69. }
  70. describe('web e2e: skill invocation policy through the real host', () => {
  71. let scaffold: WebScaffold
  72. let browser: Browser
  73. let page: Page
  74. let tripwire: ReturnType<typeof watchConsole>
  75. beforeAll(async () => {
  76. scaffold = await launchWebScaffold({})
  77. await seedSkills(scaffold.workspaceCwd)
  78. browser = await chromium.launch()
  79. page = await newEnglishPage(browser)
  80. tripwire = watchConsole(page)
  81. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  82. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  83. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  84. }, 120_000)
  85. afterAll(async () => {
  86. await browser?.close()
  87. await scaffold?.close()
  88. })
  89. it('renders every user-invocable skill and marks the user-only entry', async () => {
  90. onTestFailed(() => saveFailureShot(page, 'web-e2e-skill-invocation-policy'))
  91. const input = page.locator('[data-composer-input]').first()
  92. await input.fill('/policy')
  93. const menu = page.getByRole('listbox', { name: 'Trigger suggestions' })
  94. await expect.poll(
  95. () => menu.getByRole('option', { name: /policy-shared/ }).count(),
  96. { timeout: 10_000 },
  97. ).toBe(1)
  98. // The user-only quadrant is invocable here — its only entry point — and
  99. // wears the user-only marker; both user-disabled quadrants stay hidden.
  100. expect(await menu.getByRole('option', { name: /policy-user-only user-only · / }).count()).toBe(1)
  101. expect(await menu.getByRole('option', { name: /policy-model-only/ }).count()).toBe(0)
  102. expect(await menu.getByRole('option', { name: /policy-trusted-only/ }).count()).toBe(0)
  103. const snapshot = await captureStableAria(page, '[role="listbox"]', scaffold.workspaceCwd)
  104. await compareOrRefreshGolden(MENU_EXPECTED, snapshot, MODE)
  105. // Discovery needs no prefix: an in-order subsequence of one skill name
  106. // ranks that skill alone, through the ranker the command group uses.
  107. await writeComposerDraft(page, input, '/plcyusr')
  108. await expect.poll(() => menu.getByRole('option').count(), { timeout: 10_000 }).toBe(1)
  109. expect(await menu.getByRole('option', { name: /policy-user-only/ }).count()).toBe(1)
  110. const fuzzySnapshot = await captureStableAria(page, '[role="listbox"]', scaffold.workspaceCwd)
  111. await compareOrRefreshGolden(FUZZY_MENU_EXPECTED, fuzzySnapshot, MODE)
  112. expect(tripwire.pageErrors).toEqual([])
  113. expect(tripwire.warnings).toEqual([])
  114. await assertFixtureInventory(SNAPSHOT_DIR, ['menu-fuzzy.expected.md', 'menu.expected.md'])
  115. })
  116. })