diff --git a/.changeset/wire-watcher-shares-chat-send-input.md b/.changeset/wire-watcher-shares-chat-send-input.md new file mode 100644 index 0000000000..0766676082 --- /dev/null +++ b/.changeset/wire-watcher-shares-chat-send-input.md @@ -0,0 +1,5 @@ +--- +'@conciv/contract': patch +--- + +Export `ChatSendInput` so the rpc wire watcher asserts against the real `chat.send` schema instead of a looser copy. diff --git a/packages/contract/src/contract.ts b/packages/contract/src/contract.ts index 1562cc6f2b..f5c0720318 100644 --- a/packages/contract/src/contract.ts +++ b/packages/contract/src/contract.ts @@ -27,7 +27,7 @@ import {DraftRowSchema, MarkerRowSchema, SessionMetaSchema} from './rows.js' const StreamChunkSchema = z.custom((value) => typeof value === 'object' && value !== null) const SessionIdInput = z.object({sessionId: z.string()}) -const ChatSendInput = SessionIdInput.extend({ +export const ChatSendInput = SessionIdInput.extend({ runId: z.string().min(1), text: z.string().min(1).optional(), content: z.union([z.string().min(1), z.array(ChatContentPartSchema).min(1).max(16)]).optional(), diff --git a/packages/embed/tests/e2e/chat-send-parts.it.test.ts b/packages/embed/tests/e2e/chat-send-parts.it.test.ts new file mode 100644 index 0000000000..823784b42c --- /dev/null +++ b/packages/embed/tests/e2e/chat-send-parts.it.test.ts @@ -0,0 +1,50 @@ +import {expect, test} from '@playwright/test' +import type {Grab} from '@conciv/grab' +import {GRAB_FILE_NAME, GRAB_MIME, grabToPayload} from '@conciv/grab/grab-attachment' +import {watchRpcWire} from '@conciv/extension-testkit/rpc-wire' +import {setupWidgetSuite} from './helpers/suite.js' +import {openPanel} from './helpers/panel.js' + +const ASSISTANT_TEXT = 'Parts received' +const GRAB_TEXT = '

Payroll Deposit

at src/routes/index.tsx:12:9' +const MESSAGE_TEXT = 'what is wrong with this element' + +const GRAB: Grab = { + text: GRAB_TEXT, + snippet: '

Payroll Deposit

', + preview: {kind: 'dom', html: '

Payroll Deposit clone

', width: 200, height: 40}, + source: {componentName: 'Hero', filePath: 'src/routes/index.tsx', lineNumber: 12}, + rect: {x: 0, y: 0, width: 200, height: 40}, +} + +const GRAB_JSON = JSON.stringify(grabToPayload(GRAB)) + +const suite = setupWidgetSuite({text: ASSISTANT_TEXT}) + +test.describe('a send that carries an attachment', () => { + test('reaches the wire as content parts the wire watcher reports without throwing', async ({page}) => { + test.setTimeout(120_000) + const wire = watchRpcWire(page) + await page.goto(suite.host().base, {waitUntil: 'domcontentloaded'}) + await openPanel(page) + + const addAttachment = page.getByRole('button', {name: 'Add attachment'}) + await expect(addAttachment).toBeEnabled({timeout: 30_000}) + const opened = page.waitForEvent('filechooser', {timeout: 30_000}) + await addAttachment.click() + const chooser = await opened + await chooser.setFiles({name: GRAB_FILE_NAME, mimeType: GRAB_MIME, buffer: Buffer.from(GRAB_JSON)}) + await expect(page.getByRole('button', {name: 'Open grabbed element'})).toBeVisible({timeout: 30_000}) + + await page.getByRole('textbox', {name: 'Message the conciv agent'}).fill(MESSAGE_TEXT) + const sent = wire.nextChatSend() + await page.getByRole('button', {name: 'Send message'}).click() + + const frame = await sent + expect(frame.content).toEqual([ + {type: 'text', content: MESSAGE_TEXT}, + {type: 'document', source: {type: 'data', mimeType: GRAB_MIME, value: Buffer.from(GRAB_JSON).toString('base64')}}, + ]) + await expect(page.getByText(ASSISTANT_TEXT).first()).toBeVisible({timeout: 30_000}) + }) +}) diff --git a/packages/embed/tests/e2e/panel-session-switch.it.test.ts b/packages/embed/tests/e2e/panel-session-switch.it.test.ts new file mode 100644 index 0000000000..94cc875aea --- /dev/null +++ b/packages/embed/tests/e2e/panel-session-switch.it.test.ts @@ -0,0 +1,46 @@ +import {expect, test} from '@playwright/test' +import {setupWidgetSuite} from './helpers/suite.js' +import {openPanel} from './helpers/panel.js' +import {untilPanelDraft} from './helpers/drafts.js' + +const RECENT_ID = '43548fd1-0000-4220-acf0-014b10b5815f' +const OLDER_ID = '43548fd1-0000-4220-acf0-014b10b5815e' +const RECENT_TITLE = 'the session opened at boot' +const OLDER_TITLE = 'the session switched to' +const FIRST_DRAFT = 'draft of the session opened first' +const SECOND_DRAFT = 'draft of the session switched to' + +const suite = setupWidgetSuite({ + history: [ + {id: RECENT_ID, derivedTitle: RECENT_TITLE, updatedAt: Date.now(), messageCount: 3}, + {id: OLDER_ID, derivedTitle: OLDER_TITLE, updatedAt: Date.now() - 60_000, messageCount: 2}, + ], +}) + +test.describe('composer drafts after a session switch', () => { + test('persists each draft against the session the panel is on when it is typed', async ({page}) => { + test.setTimeout(180_000) + await page.goto(suite.host().base, {waitUntil: 'domcontentloaded'}) + await openPanel(page) + + const opened = await suite.kit().rpc.sessions.resolve({id: RECENT_ID}) + const input = page.getByRole('textbox', {name: 'Message the conciv agent'}) + await input.click() + await input.pressSequentially(FIRST_DRAFT) + await untilPanelDraft(suite.kit(), opened.sessionId, (draft) => draft.text === FIRST_DRAFT) + + await page.getByRole('button', {name: /^Session: /}).click() + await page.getByRole('option', {name: new RegExp(OLDER_TITLE)}).click() + await expect(page.getByRole('button', {name: `Session: ${OLDER_TITLE}`})).toBeVisible({timeout: 30_000}) + + const switched = await suite.kit().rpc.sessions.resolve({id: OLDER_ID}) + expect(switched.sessionId).not.toBe(opened.sessionId) + + await input.click() + await input.pressSequentially(SECOND_DRAFT) + await untilPanelDraft(suite.kit(), switched.sessionId, (draft) => draft.text === SECOND_DRAFT) + + expect(await suite.kit().rpc.drafts.get({sessionId: opened.sessionId})).toMatchObject({text: FIRST_DRAFT}) + expect(await suite.kit().rpc.drafts.get({sessionId: switched.sessionId})).toMatchObject({text: SECOND_DRAFT}) + }) +}) diff --git a/packages/extension-testkit/src/rpc-wire.ts b/packages/extension-testkit/src/rpc-wire.ts index 993777717d..dfcb7c6f85 100644 --- a/packages/extension-testkit/src/rpc-wire.ts +++ b/packages/extension-testkit/src/rpc-wire.ts @@ -1,5 +1,6 @@ import type {Page} from 'playwright' -import {z} from 'zod' +import type {z} from 'zod' +import {ChatSendInput} from '@conciv/contract' import {rpcObserverFor, type RpcTransport} from './rpc-observer.js' export type {RpcTransport} @@ -14,9 +15,7 @@ const CHAT_SUBSCRIBE: readonly string[] = ['chat', 'subscribe'] const SESSIONS_LIST: readonly string[] = ['sessions', 'list'] const SESSIONS_RESOLVE: readonly string[] = ['sessions', 'resolve'] -const ChatSendInputSchema = z.object({content: z.string()}) - -export type ChatSendFrame = {transport: RpcTransport; content: string} +export type ChatSendFrame = {transport: RpcTransport} & z.infer export type ChatReconnectFrames = {stop: RpcTransport; send: RpcTransport; subscribe: RpcTransport} @@ -38,7 +37,7 @@ export function watchRpcWire(page: Page): RpcWireWatch { const since = observer.mark() return observer .completed({path: CHAT_SEND, since, timeout: WIRE_TIMEOUT_MS}) - .then((call) => ({transport: call.transport, content: ChatSendInputSchema.parse(call.input).content})) + .then((call) => ({transport: call.transport, ...ChatSendInput.parse(call.input)})) }, chatReconnect: async () => { const since = observer.mark()