Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/embedded-copy-prompt-clipboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inkeep/open-knowledge": patch
---

Fix the empty-state "copy prompt" rows not working when OK runs inside an embedded host iframe (e.g. Claude). The copy buttons called the async Clipboard API directly, which the host frame's Permissions-Policy refuses inside an iframe — the rejection was swallowed silently, so nothing copied and the row never showed "Copied". The copy now routes through the shared clipboard adapter, whose `execCommand` fallback succeeds under the click's user activation where the policy-gated async write is blocked.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test';
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import type { ReactNode } from 'react';
import { renderLinguiTemplate } from '@/test-utils/lingui-mock';

mock.module('@lingui/react/macro', () => ({
Trans: ({ children }: { children?: ReactNode }) => <>{children}</>,
useLingui: () => ({ t: renderLinguiTemplate }),
}));

const { CopyablePromptList } = await import('./CopyablePromptList');

describe('CopyablePromptList', () => {
beforeEach(() => {
Reflect.deleteProperty(globalThis, 'okDesktop');
Object.defineProperty(globalThis.navigator, 'clipboard', {
configurable: true,
value: undefined,
});
});
afterEach(() => {
cleanup();
Reflect.deleteProperty(globalThis.document, 'execCommand');
});

test('flips a row to "Copied" when the clipboard write resolves', async () => {
const writeText = mock(() => Promise.resolve());
Object.defineProperty(globalThis.navigator, 'clipboard', {
configurable: true,
value: { writeText },
});

render(<CopyablePromptList scenario="new-project" />);

const button = screen.getByTestId('copy-prompt-button-competitor-research');
fireEvent.click(button);

await waitFor(() => {
expect(screen.getByTestId('copy-prompt-button-competitor-research').textContent).toContain(
'Copied',
);
});
expect(writeText).toHaveBeenCalledTimes(1);
});

test('falls back to execCommand and still copies when embedded-iframe policy refuses the async write', async () => {
const writeText = mock(() =>
Promise.reject(
Object.assign(new Error('blocked because of a permissions policy'), {
name: 'NotAllowedError',
}),
),
);
Object.defineProperty(globalThis.navigator, 'clipboard', {
configurable: true,
value: { writeText },
});
const execCommand = mock(() => true);
Object.defineProperty(globalThis.document, 'execCommand', {
configurable: true,
value: execCommand,
});

render(<CopyablePromptList scenario="new-project" />);

fireEvent.click(screen.getByTestId('copy-prompt-button-competitor-research'));

await waitFor(() => {
expect(screen.getByTestId('copy-prompt-button-competitor-research').textContent).toContain(
'Copied',
);
});
expect(writeText).toHaveBeenCalledTimes(1);
expect(execCommand).toHaveBeenCalledWith('copy');
});

test('does not flip to "Copied" when every clipboard path is refused', async () => {
const writeText = mock(() =>
Promise.reject(Object.assign(new Error('blocked'), { name: 'NotAllowedError' })),
);
Object.defineProperty(globalThis.navigator, 'clipboard', {
configurable: true,
value: { writeText },
});
const execCommand = mock(() => false);
Object.defineProperty(globalThis.document, 'execCommand', {
configurable: true,
value: execCommand,
});

render(<CopyablePromptList scenario="new-project" />);
fireEvent.click(screen.getByTestId('copy-prompt-button-competitor-research'));

await waitFor(() => expect(execCommand).toHaveBeenCalledWith('copy'));
expect(writeText).toHaveBeenCalledTimes(1);
expect(screen.getByTestId('copy-prompt-button-competitor-research').textContent).not.toContain(
'Copied',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useCreateSuggestions,
} from '@/components/empty-state/use-create-suggestions';
import { Button } from '@/components/ui/button';
import { scheduleClipboardWrite } from '@/lib/share/clipboard-adapter';
import { cn } from '@/lib/utils';

interface CopyablePromptListProps {
Expand All @@ -22,9 +23,7 @@ export function CopyablePromptList({ scenario, className }: CopyablePromptListPr
useEffect(() => () => clearTimeout(resetTimerRef.current), []);

function handleCopy(id: string, prompt: string) {
if (!navigator.clipboard) return;
void navigator.clipboard
.writeText(prompt)
void scheduleClipboardWrite(prompt)
.then(() => {
setCopiedId(id);
clearTimeout(resetTimerRef.current);
Expand Down