|
| 1 | +/** |
| 2 | + * A chat view learns from the stream it reads whether the desktop app's background executor runs |
| 3 | + * the turn's desktop tools, so it only shows those calls. Exercised against real PostgreSQL and |
| 4 | + * Redis through the production reconnect route, for the live tail and the replay batch a view |
| 5 | + * attaches with after a reload. |
| 6 | + */ |
| 7 | +import { authMock, authMockFns } from '@sim/testing/mocks/auth.mock' |
| 8 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +const { redisUrl } = await vi.hoisted(async () => { |
| 11 | + const { readTestRedisUrl } = await import('@sim/db/testing/test-infrastructure') |
| 12 | + const url = readTestRedisUrl() |
| 13 | + if (url) process.env.REDIS_URL = url |
| 14 | + return { redisUrl: url } |
| 15 | +}) |
| 16 | + |
| 17 | +vi.mock('@/lib/auth', () => authMock) |
| 18 | + |
| 19 | +import { db } from '@sim/db' |
| 20 | +import { |
| 21 | + copilotChats, |
| 22 | + copilotRuns, |
| 23 | + desktopDevices, |
| 24 | + permissions, |
| 25 | + user, |
| 26 | + workspace, |
| 27 | +} from '@sim/db/schema' |
| 28 | +import { generateId } from '@sim/utils/id' |
| 29 | +import { eq } from 'drizzle-orm' |
| 30 | +import { NextRequest } from 'next/server' |
| 31 | +import { closeRedisConnection } from '@/lib/core/config/redis' |
| 32 | +import { MOTHERSHIP_DESKTOP_EXECUTOR_HEADER } from '@/lib/mothership/constants' |
| 33 | +import { GET as streamGET } from '@/app/api/copilot/chat/stream/route' |
| 34 | + |
| 35 | +const userId = generateId() |
| 36 | +const workspaceId = generateId() |
| 37 | +const chatId = generateId() |
| 38 | +const deviceId = generateId() |
| 39 | + |
| 40 | +async function finishedRun(desktopDeviceId: string | null): Promise<string> { |
| 41 | + const streamId = generateId() |
| 42 | + await db.insert(copilotRuns).values({ |
| 43 | + id: generateId(), |
| 44 | + executionId: generateId(), |
| 45 | + chatId, |
| 46 | + userId, |
| 47 | + workspaceId, |
| 48 | + streamId, |
| 49 | + status: 'complete', |
| 50 | + desktopDeviceId, |
| 51 | + }) |
| 52 | + return streamId |
| 53 | +} |
| 54 | + |
| 55 | +function reconnect(streamId: string, batch: boolean) { |
| 56 | + return streamGET( |
| 57 | + new NextRequest( |
| 58 | + `http://localhost:3000/api/copilot/chat/stream?streamId=${streamId}&after=0${batch ? '&batch=true' : ''}` |
| 59 | + ), |
| 60 | + { params: Promise.resolve({}) } |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +describe.runIf(Boolean(redisUrl))('desktop executor binding on a chat stream', () => { |
| 65 | + beforeAll(async () => { |
| 66 | + const now = new Date() |
| 67 | + await db.insert(user).values({ |
| 68 | + id: userId, |
| 69 | + name: 'Desktop stream fixture', |
| 70 | + email: `${userId}@desktop-stream.test`, |
| 71 | + emailVerified: true, |
| 72 | + createdAt: now, |
| 73 | + updatedAt: now, |
| 74 | + }) |
| 75 | + await db.insert(workspace).values({ |
| 76 | + id: workspaceId, |
| 77 | + name: 'Desktop stream fixture', |
| 78 | + ownerId: userId, |
| 79 | + billedAccountUserId: userId, |
| 80 | + }) |
| 81 | + await db.insert(permissions).values({ |
| 82 | + id: generateId(), |
| 83 | + userId, |
| 84 | + entityType: 'workspace', |
| 85 | + entityId: workspaceId, |
| 86 | + permissionType: 'admin', |
| 87 | + }) |
| 88 | + await db.insert(copilotChats).values({ id: chatId, userId, workspaceId, type: 'mothership' }) |
| 89 | + await db.insert(desktopDevices).values({ |
| 90 | + id: deviceId, |
| 91 | + userId, |
| 92 | + name: 'Studio Mac', |
| 93 | + appVersion: '0.9.0', |
| 94 | + platform: 'darwin-arm64', |
| 95 | + capabilities: { executor: 1, browser: true, terminal: true, localFiles: true }, |
| 96 | + }) |
| 97 | + authMockFns.mockGetSession.mockResolvedValue({ |
| 98 | + user: { id: userId }, |
| 99 | + session: { id: generateId() }, |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + afterAll(async () => { |
| 104 | + await db.delete(copilotRuns).where(eq(copilotRuns.chatId, chatId)) |
| 105 | + await db.delete(copilotChats).where(eq(copilotChats.id, chatId)) |
| 106 | + await db.delete(desktopDevices).where(eq(desktopDevices.id, deviceId)) |
| 107 | + await db.delete(permissions).where(eq(permissions.userId, userId)) |
| 108 | + await db.delete(workspace).where(eq(workspace.id, workspaceId)) |
| 109 | + await db.delete(user).where(eq(user.id, userId)) |
| 110 | + await closeRedisConnection() |
| 111 | + }) |
| 112 | + |
| 113 | + it('marks the live tail and the replay batch of a turn a desktop runs', async () => { |
| 114 | + const streamId = await finishedRun(deviceId) |
| 115 | + |
| 116 | + const tail = await reconnect(streamId, false) |
| 117 | + await tail.text() |
| 118 | + const batch = await (await reconnect(streamId, true)).json() |
| 119 | + |
| 120 | + expect(tail.headers.get(MOTHERSHIP_DESKTOP_EXECUTOR_HEADER)).toBe('device') |
| 121 | + expect(batch.desktopToolsOnDevice).toBe(true) |
| 122 | + }) |
| 123 | + |
| 124 | + it('leaves a turn the chat view runs unmarked', async () => { |
| 125 | + const streamId = await finishedRun(null) |
| 126 | + |
| 127 | + const tail = await reconnect(streamId, false) |
| 128 | + await tail.text() |
| 129 | + const batch = await (await reconnect(streamId, true)).json() |
| 130 | + |
| 131 | + expect(tail.headers.get(MOTHERSHIP_DESKTOP_EXECUTOR_HEADER)).toBeNull() |
| 132 | + expect(batch.desktopToolsOnDevice).toBeUndefined() |
| 133 | + }) |
| 134 | +}) |
0 commit comments