d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
160 lines
5.1 KiB
TypeScript
160 lines
5.1 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { authMockFns, dbChainMock, dbChainMockFns, resetDbChainMock } from '@sim/testing'
|
|
import { NextRequest } from 'next/server'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
vi.mock('@sim/db', () => dbChainMock)
|
|
|
|
const { mockAppendCopilotChatMessages, mockPublishStatusChanged } = vi.hoisted(() => ({
|
|
mockAppendCopilotChatMessages: vi.fn(),
|
|
mockPublishStatusChanged: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/copilot/chat/messages-store', () => ({
|
|
appendCopilotChatMessages: mockAppendCopilotChatMessages,
|
|
}))
|
|
|
|
vi.mock('@/lib/copilot/chat-status', () => ({
|
|
chatPubSub: {
|
|
publishStatusChanged: mockPublishStatusChanged,
|
|
},
|
|
}))
|
|
|
|
import { POST } from '@/app/api/copilot/chat/stop/route'
|
|
|
|
function createRequest(body: Record<string, unknown>) {
|
|
return new NextRequest('http://localhost:3000/api/copilot/chat/stop', {
|
|
method: 'POST',
|
|
body: JSON.stringify(body),
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Sequence the two in-tx reads `finalizeAssistantTurn` issues: the chat row
|
|
* (`FOR UPDATE ... LIMIT 1`) and the last-message lookup that drives dedup
|
|
* (both terminate on `.limit(1)`).
|
|
*/
|
|
function mockReads(opts: {
|
|
chat: Record<string, unknown> | null
|
|
last?: { messageId: string; role: string }
|
|
}) {
|
|
dbChainMockFns.limit.mockResolvedValueOnce(opts.chat ? [opts.chat] : [])
|
|
dbChainMockFns.limit.mockResolvedValueOnce(opts.last ? [opts.last] : [])
|
|
}
|
|
|
|
describe('copilot chat stop route', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
// Drain the once-queue (clearAllMocks/resetDbChainMock don't), then restore defaults.
|
|
dbChainMockFns.limit.mockReset()
|
|
resetDbChainMock()
|
|
authMockFns.mockGetSession.mockResolvedValue({ user: { id: 'user-1' } })
|
|
})
|
|
|
|
it('returns 401 when unauthenticated', async () => {
|
|
authMockFns.mockGetSession.mockResolvedValueOnce(null)
|
|
|
|
const response = await POST(
|
|
createRequest({ chatId: 'chat-1', streamId: 'stream-1', content: '' })
|
|
)
|
|
|
|
expect(response.status).toBe(401)
|
|
expect(await response.json()).toEqual({ error: 'Unauthorized' })
|
|
})
|
|
|
|
it('is a no-op when the chat is missing', async () => {
|
|
mockReads({ chat: null })
|
|
|
|
const response = await POST(
|
|
createRequest({ chatId: 'missing-chat', streamId: 'stream-1', content: '' })
|
|
)
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual({ success: true })
|
|
expect(mockAppendCopilotChatMessages).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('appends a stopped assistant message even with no content', async () => {
|
|
mockReads({
|
|
chat: { workspaceId: 'ws-1', conversationId: 'stream-1', model: null },
|
|
last: { messageId: 'stream-1', role: 'user' },
|
|
})
|
|
|
|
const response = await POST(
|
|
createRequest({ chatId: 'chat-1', streamId: 'stream-1', content: '' })
|
|
)
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual({ success: true })
|
|
|
|
const setArg = dbChainMockFns.set.mock.calls[0]?.[0] as Record<string, unknown>
|
|
expect(setArg.conversationId).toBeNull()
|
|
expect(Object.hasOwn(setArg, 'messages')).toBe(false)
|
|
|
|
expect(mockAppendCopilotChatMessages).toHaveBeenCalledTimes(1)
|
|
const [, appended] = mockAppendCopilotChatMessages.mock.calls[0]
|
|
expect(appended[0]).toMatchObject({
|
|
role: 'assistant',
|
|
content: '',
|
|
contentBlocks: [{ type: 'complete', status: 'cancelled' }],
|
|
})
|
|
|
|
expect(mockPublishStatusChanged).toHaveBeenCalledWith({
|
|
workspaceId: 'ws-1',
|
|
chatId: 'chat-1',
|
|
type: 'completed',
|
|
streamId: 'stream-1',
|
|
})
|
|
})
|
|
|
|
it('appends a stopped assistant message if the stream marker was already cleared', async () => {
|
|
mockReads({
|
|
chat: { workspaceId: 'ws-1', conversationId: null, model: null },
|
|
last: { messageId: 'stream-1', role: 'user' },
|
|
})
|
|
|
|
const response = await POST(
|
|
createRequest({ chatId: 'chat-1', streamId: 'stream-1', content: 'partial' })
|
|
)
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual({ success: true })
|
|
|
|
expect(mockAppendCopilotChatMessages).toHaveBeenCalledTimes(1)
|
|
const [, appended] = mockAppendCopilotChatMessages.mock.calls[0]
|
|
expect(appended[0]).toMatchObject({ role: 'assistant', content: 'partial' })
|
|
|
|
expect(mockPublishStatusChanged).toHaveBeenCalledWith({
|
|
workspaceId: 'ws-1',
|
|
chatId: 'chat-1',
|
|
type: 'completed',
|
|
streamId: 'stream-1',
|
|
})
|
|
})
|
|
|
|
it('republishes completed status when the assistant was already persisted', async () => {
|
|
mockReads({
|
|
chat: { workspaceId: 'ws-1', conversationId: null, model: null },
|
|
last: { messageId: 'assistant-1', role: 'assistant' },
|
|
})
|
|
|
|
const response = await POST(
|
|
createRequest({ chatId: 'chat-1', streamId: 'stream-1', content: 'partial' })
|
|
)
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual({ success: true })
|
|
expect(mockAppendCopilotChatMessages).not.toHaveBeenCalled()
|
|
expect(dbChainMockFns.set).not.toHaveBeenCalled()
|
|
expect(mockPublishStatusChanged).toHaveBeenCalledWith({
|
|
workspaceId: 'ws-1',
|
|
chatId: 'chat-1',
|
|
type: 'completed',
|
|
streamId: 'stream-1',
|
|
})
|
|
})
|
|
})
|