Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

226 lines
7.1 KiB
TypeScript

/**
* @vitest-environment node
*/
import { copilotHttpMock, copilotHttpMockFns, permissionsMock } from '@sim/testing'
import { NextRequest } from 'next/server'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockSelect, mockFrom, mockWhere, mockOrderBy, mockReconcileChatStreamMarkers } = vi.hoisted(
() => ({
mockSelect: vi.fn(),
mockFrom: vi.fn(),
mockWhere: vi.fn(),
mockOrderBy: vi.fn(),
mockReconcileChatStreamMarkers: vi.fn(),
})
)
vi.mock('@sim/db', () => ({
db: {
select: mockSelect,
},
}))
vi.mock('@sim/db/schema', () => ({
copilotChats: {
id: 'copilotChats.id',
title: 'copilotChats.title',
userId: 'copilotChats.userId',
workspaceId: 'copilotChats.workspaceId',
type: 'copilotChats.type',
updatedAt: 'copilotChats.updatedAt',
conversationId: 'copilotChats.conversationId',
lastSeenAt: 'copilotChats.lastSeenAt',
},
}))
vi.mock('drizzle-orm', () => ({
and: vi.fn((...conditions: unknown[]) => ({ type: 'and', conditions })),
desc: vi.fn((field: unknown) => ({ type: 'desc', field })),
eq: vi.fn((field: unknown, value: unknown) => ({ type: 'eq', field, value })),
}))
vi.mock('@/lib/copilot/request/http', () => copilotHttpMock)
vi.mock('@/lib/workspaces/permissions/utils', () => permissionsMock)
vi.mock('@/lib/copilot/chat/stream-liveness', () => ({
reconcileChatStreamMarkers: mockReconcileChatStreamMarkers,
}))
vi.mock('@/lib/copilot/chat-status', () => ({
chatPubSub: { publishStatusChanged: vi.fn() },
}))
vi.mock('@/lib/posthog/server', () => ({
captureServerEvent: vi.fn(),
}))
import { GET } from '@/app/api/mothership/chats/route'
function createRequest(workspaceId: string) {
return new NextRequest(`http://localhost:3000/api/mothership/chats?workspaceId=${workspaceId}`, {
method: 'GET',
})
}
describe('GET /api/mothership/chats', () => {
beforeEach(() => {
vi.clearAllMocks()
copilotHttpMockFns.mockAuthenticateCopilotRequestSessionOnly.mockResolvedValue({
userId: 'user-1',
isAuthenticated: true,
})
mockOrderBy.mockResolvedValue([])
mockWhere.mockReturnValue({ orderBy: mockOrderBy })
mockFrom.mockReturnValue({ where: mockWhere })
mockSelect.mockReturnValue({ from: mockFrom })
mockReconcileChatStreamMarkers.mockImplementation(
async (candidates: Array<{ chatId: string; streamId: string | null }>) =>
new Map(
candidates.map((candidate) => [
candidate.chatId,
{
chatId: candidate.chatId,
streamId: candidate.streamId,
status: candidate.streamId ? 'active' : 'inactive',
},
])
)
)
})
it('clears activeStreamId on chats whose redis lock has expired (stuck-yellow bug)', async () => {
const now = new Date('2026-05-11T12:00:00Z')
mockOrderBy.mockResolvedValueOnce([
{
id: 'chat-stuck',
title: 'Stuck chat',
updatedAt: now,
activeStreamId: 'stream-orphaned',
lastSeenAt: null,
},
{
id: 'chat-live',
title: 'Live chat',
updatedAt: now,
activeStreamId: 'stream-live',
lastSeenAt: null,
},
{
id: 'chat-idle',
title: 'Idle chat',
updatedAt: now,
activeStreamId: null,
lastSeenAt: null,
},
])
mockReconcileChatStreamMarkers.mockResolvedValueOnce(
new Map([
['chat-stuck', { chatId: 'chat-stuck', streamId: null, status: 'inactive' }],
['chat-live', { chatId: 'chat-live', streamId: 'stream-live', status: 'active' }],
['chat-idle', { chatId: 'chat-idle', streamId: null, status: 'inactive' }],
])
)
const response = await GET(createRequest('ws-1'))
expect(response.status).toBe(200)
const body = await response.json()
expect(mockReconcileChatStreamMarkers).toHaveBeenCalledWith(
[
{ chatId: 'chat-stuck', streamId: 'stream-orphaned' },
{ chatId: 'chat-live', streamId: 'stream-live' },
{ chatId: 'chat-idle', streamId: null },
],
{ repairVerifiedStaleMarkers: true }
)
expect(body.success).toBe(true)
expect(body.data).toEqual([
expect.objectContaining({ id: 'chat-stuck', activeStreamId: null }),
expect.objectContaining({ id: 'chat-live', activeStreamId: 'stream-live' }),
expect.objectContaining({ id: 'chat-idle', activeStreamId: null }),
])
})
it('preserves chats when no chat has a stream marker set', async () => {
const now = new Date('2026-05-11T12:00:00Z')
mockOrderBy.mockResolvedValueOnce([
{ id: 'chat-1', title: null, updatedAt: now, activeStreamId: null, lastSeenAt: null },
{ id: 'chat-2', title: null, updatedAt: now, activeStreamId: null, lastSeenAt: null },
])
const response = await GET(createRequest('ws-1'))
expect(response.status).toBe(200)
expect(mockReconcileChatStreamMarkers).toHaveBeenCalledWith(
[
{ chatId: 'chat-1', streamId: null },
{ chatId: 'chat-2', streamId: null },
],
{ repairVerifiedStaleMarkers: true }
)
const body = await response.json()
expect(body.data).toEqual([
expect.objectContaining({ id: 'chat-1', activeStreamId: null }),
expect.objectContaining({ id: 'chat-2', activeStreamId: null }),
])
})
it('leaves activeStreamId untouched when redis confirms every lock is live', async () => {
const now = new Date('2026-05-11T12:00:00Z')
mockOrderBy.mockResolvedValueOnce([
{ id: 'chat-a', title: null, updatedAt: now, activeStreamId: 'stream-a', lastSeenAt: null },
{ id: 'chat-b', title: null, updatedAt: now, activeStreamId: 'stream-b', lastSeenAt: null },
])
const response = await GET(createRequest('ws-1'))
const body = await response.json()
expect(body.data).toEqual([
expect.objectContaining({ id: 'chat-a', activeStreamId: 'stream-a' }),
expect.objectContaining({ id: 'chat-b', activeStreamId: 'stream-b' }),
])
})
it('uses Redis lock owner when it differs from a stale activeStreamId', async () => {
const now = new Date('2026-05-11T12:00:00Z')
mockOrderBy.mockResolvedValueOnce([
{
id: 'chat-mismatch',
title: null,
updatedAt: now,
activeStreamId: 'stream-stale',
lastSeenAt: null,
},
])
mockReconcileChatStreamMarkers.mockResolvedValueOnce(
new Map([
['chat-mismatch', { chatId: 'chat-mismatch', streamId: 'stream-live', status: 'active' }],
])
)
const response = await GET(createRequest('ws-1'))
expect(response.status).toBe(200)
const body = await response.json()
expect(body.data).toEqual([
expect.objectContaining({ id: 'chat-mismatch', activeStreamId: 'stream-live' }),
])
})
it('returns 401 when unauthenticated', async () => {
copilotHttpMockFns.mockAuthenticateCopilotRequestSessionOnly.mockResolvedValueOnce({
userId: null,
isAuthenticated: false,
})
const response = await GET(createRequest('ws-1'))
expect(response.status).toBe(401)
expect(mockSelect).not.toHaveBeenCalled()
expect(mockReconcileChatStreamMarkers).not.toHaveBeenCalled()
})
})