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/wire-watcher-shares-chat-send-input.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {DraftRowSchema, MarkerRowSchema, SessionMetaSchema} from './rows.js'
const StreamChunkSchema = z.custom<StreamChunk>((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(),
Expand Down
50 changes: 50 additions & 0 deletions packages/embed/tests/e2e/chat-send-parts.it.test.ts
Original file line number Diff line number Diff line change
@@ -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 = '<h1>Payroll Deposit</h1> at src/routes/index.tsx:12:9'
const MESSAGE_TEXT = 'what is wrong with this element'

const GRAB: Grab = {
text: GRAB_TEXT,
snippet: '<h1>Payroll Deposit</h1>',
preview: {kind: 'dom', html: '<p>Payroll Deposit clone</p>', 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})
})
})
46 changes: 46 additions & 0 deletions packages/embed/tests/e2e/panel-session-switch.it.test.ts
Original file line number Diff line number Diff line change
@@ -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})
})
})
9 changes: 4 additions & 5 deletions packages/extension-testkit/src/rpc-wire.ts
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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<typeof ChatSendInput>

export type ChatReconnectFrames = {stop: RpcTransport; send: RpcTransport; subscribe: RpcTransport}

Expand All @@ -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()
Expand Down
Loading