diff --git a/src/context-types.ts b/src/context-types.ts index 19dd6aa0..42af56ec 100644 --- a/src/context-types.ts +++ b/src/context-types.ts @@ -670,7 +670,9 @@ export interface SidebarSessionInput { * Replace the whole draft text. **Not an insert API**: it removes inline * reference chips (the draft comes back as plain text), so both insertion * paths emit scoped composer events instead — see - * `client/conversation-draft.ts`. + * `client/conversation-draft.ts`. If a future host changes that semantic + * (chip-preserving replacement, or a public insert/`captureInsertion` face), + * revisit this note together with both paths in that module. */ setDraft(text: string): void } diff --git a/tests/e2e/mount.e2e.ts b/tests/e2e/mount.e2e.ts index 3b04f49c..b0153761 100644 --- a/tests/e2e/mount.e2e.ts +++ b/tests/e2e/mount.e2e.ts @@ -906,3 +906,88 @@ test('the custom-CSS escape hatch injects the user stylesheet live', async ({ re await request.post(sidebarApi('settings.update'), { data: { patch: { customCss: '' } } }) } }) + +/** + * The explorer's @-reference button, twice in a row, against a real composer. + * + * The shipped regression this pins: the plugin's insert used to rewrite the + * whole draft through DSH's `setDraft`, whose contract is "replace the whole + * draft with plain text, removing inline reference chips" — so the second + * click flattened the first chip into `@dir/file` text (which the host then + * decorates as `@dir/` plus an undecorated file name, i.e. the "two-piece" + * mention). A chip-free second insert is what the host's own picker does; the + * composer must therefore hold exactly two chips and no plain-text reference. + */ +test('the explorer @-reference button keeps earlier chips (no whole-draft rewrite)', async ({ page }) => { + const pageErrors: string[] = [] + page.on('pageerror', (error) => pageErrors.push(String(error))) + + // One workspace directory for the folder-reference half of this test; it is + // created before the page loads so the tree's first listing already has it. + const refDir = 'refs' + mkdirSync(join(WORKSPACE_PATH, refDir), { recursive: true }) + + await gotoPage(page) + await dismissOnboarding(page) + // A session with content: the native sidebar's entry point and the composer + // both need one. + await sendFirstMessage(page) + + // Chips exist only in the composer, so the marker itself is the anchor + // (locale-proof, unlike the composer's aria-label). + const chips = page.locator('[data-composer-chip]') + + // The plugin's explorer, through the host's own right sidebar. + const pane = page.locator('[data-sidebar-right-panel]') + await page.locator('[data-sidebar-right-expand]').first().click() + await expect(pane).toBeVisible({ timeout: 30_000 }) + if (await page.locator('[data-sidebar-right-guide]').count() === 0) { + await page.locator('[data-dockkit-add-tab]').first().click() + } + await page.locator('[data-sidebar-right-guide-entry="files"]').click() + + /** Reveal and click one row's @-reference action (hover-revealed at the row end). */ + const referenceRow = async (file: string): Promise => { + const row = pane.locator(`[role="button"][title$="${file}"]:visible`) + await expect(row, `the seeded "${file}" file must appear in the plugin's explorer`).toHaveCount(1, { timeout: 30_000 }) + await row.hover() + // "@file" (en) / "@文件" (zh) — match the leading trigger instead of a locale. + await row.getByRole('button', { name: /^@/ }).click({ timeout: 10_000 }) + } + + await referenceRow(SEEDED_FILE) + await expect( + chips, + 'the explorer @-reference must reach the composer as one chip', + ).toHaveCount(1, { timeout: 15_000 }) + + await referenceRow(SEEDED_MD_FILE) + await expect( + chips, + 'the second @-reference must leave the first chip intact (a whole-draft rewrite would flatten it)', + ).toHaveCount(2, { timeout: 15_000 }) + await expect( + chips.first(), + 'the first chip must still carry its own file, not the flattened mention text', + ).toContainText(SEEDED_FILE, { timeout: 10_000 }) + await expect( + page.locator('[data-composer-text-ref]'), + 'no mention may degrade into plain decorated text', + ).toHaveCount(0) + + // The folder branch stays on purpose the host's plain-text insert (its own + // folder-drill path): one folder reference must land as DSH's decorated + // `@refs/` token — and it must not disturb the chips. + const dirRow = pane.locator(`[class*="explorerDir"]:has([class*="explorerName"]:text-is("${refDir}"))`) + await expect(dirRow, `the seeded "${refDir}" directory must be listed`).toHaveCount(1, { timeout: 30_000 }) + await dirRow.hover() + await dirRow.getByRole('button', { name: /^@/ }).click({ timeout: 10_000 }) + await expect( + page.locator('[data-composer-text-ref]'), + 'the folder insert must reach the composer as the host-decorated plain-text token', + ).toHaveCount(1, { timeout: 15_000 }) + await expect(chips, 'the folder insert must leave both chips intact').toHaveCount(2) + + expect(pageErrors, 'no page error while inserting references').toEqual([]) +}) +