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
155 lines
4.6 KiB
TypeScript
155 lines
4.6 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const { mockAnd, mockEq, mockGetChatStreamLockOwners, mockSet, mockUpdate, mockWhere } = vi.hoisted(
|
|
() => ({
|
|
mockAnd: vi.fn((...conditions: unknown[]) => ({ type: 'and', conditions })),
|
|
mockEq: vi.fn((field: unknown, value: unknown) => ({ type: 'eq', field, value })),
|
|
mockGetChatStreamLockOwners: vi.fn(),
|
|
mockSet: vi.fn(),
|
|
mockUpdate: vi.fn(),
|
|
mockWhere: vi.fn(),
|
|
})
|
|
)
|
|
|
|
vi.mock('@sim/db', () => ({
|
|
db: { update: mockUpdate },
|
|
}))
|
|
|
|
vi.mock('@sim/db/schema', () => ({
|
|
copilotChats: {
|
|
id: 'copilotChats.id',
|
|
conversationId: 'copilotChats.conversationId',
|
|
},
|
|
}))
|
|
|
|
vi.mock('drizzle-orm', () => ({
|
|
and: mockAnd,
|
|
eq: mockEq,
|
|
}))
|
|
|
|
vi.mock('@/lib/copilot/request/session', () => ({
|
|
getChatStreamLockOwners: mockGetChatStreamLockOwners,
|
|
}))
|
|
|
|
import { reconcileChatStreamMarkers } from '@/lib/copilot/chat/stream-liveness'
|
|
|
|
describe('reconcileChatStreamMarkers', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockSet.mockReturnValue({ where: mockWhere })
|
|
mockUpdate.mockReturnValue({ set: mockSet })
|
|
mockWhere.mockResolvedValue(undefined)
|
|
mockGetChatStreamLockOwners.mockResolvedValue({
|
|
status: 'verified',
|
|
ownersByChatId: new Map<string, string>(),
|
|
})
|
|
})
|
|
|
|
it('clears a persisted stream marker when Redis verifies no lock owner exists', async () => {
|
|
const markers = await reconcileChatStreamMarkers([
|
|
{ chatId: 'chat-stuck', streamId: 'stream-orphaned' },
|
|
])
|
|
|
|
expect(mockGetChatStreamLockOwners).toHaveBeenCalledWith(['chat-stuck'])
|
|
expect(markers.get('chat-stuck')).toEqual({
|
|
chatId: 'chat-stuck',
|
|
streamId: null,
|
|
status: 'inactive',
|
|
})
|
|
})
|
|
|
|
it('repairs a verified stale persisted stream marker when requested', async () => {
|
|
await reconcileChatStreamMarkers([{ chatId: 'chat-stuck', streamId: 'stream-orphaned' }], {
|
|
repairVerifiedStaleMarkers: true,
|
|
})
|
|
|
|
expect(mockUpdate).toHaveBeenCalled()
|
|
expect(mockSet).toHaveBeenCalledWith({ conversationId: null })
|
|
expect(mockWhere).toHaveBeenCalledWith(
|
|
mockAnd(
|
|
mockEq('copilotChats.id', 'chat-stuck'),
|
|
mockEq('copilotChats.conversationId', 'stream-orphaned')
|
|
)
|
|
)
|
|
})
|
|
|
|
it('uses the canonical Redis owner when the persisted stream marker is stale', async () => {
|
|
mockGetChatStreamLockOwners.mockResolvedValueOnce({
|
|
status: 'verified',
|
|
ownersByChatId: new Map([['chat-mismatch', 'stream-live']]),
|
|
})
|
|
|
|
const markers = await reconcileChatStreamMarkers([
|
|
{ chatId: 'chat-mismatch', streamId: 'stream-stale' },
|
|
])
|
|
|
|
expect(markers.get('chat-mismatch')).toEqual({
|
|
chatId: 'chat-mismatch',
|
|
streamId: 'stream-live',
|
|
status: 'active',
|
|
})
|
|
})
|
|
|
|
it('preserves persisted stream markers when Redis state is unknown', async () => {
|
|
mockGetChatStreamLockOwners.mockResolvedValueOnce({
|
|
status: 'unknown',
|
|
ownersByChatId: new Map<string, string>(),
|
|
})
|
|
|
|
const markers = await reconcileChatStreamMarkers([
|
|
{ chatId: 'chat-remote', streamId: 'stream-remote' },
|
|
])
|
|
|
|
expect(markers.get('chat-remote')).toEqual({
|
|
chatId: 'chat-remote',
|
|
streamId: 'stream-remote',
|
|
status: 'unknown',
|
|
})
|
|
})
|
|
|
|
it('preserves a persisted marker when unknown local owner differs', async () => {
|
|
mockGetChatStreamLockOwners.mockResolvedValueOnce({
|
|
status: 'unknown',
|
|
ownersByChatId: new Map([['chat-mismatch', 'stream-local']]),
|
|
})
|
|
|
|
const markers = await reconcileChatStreamMarkers([
|
|
{ chatId: 'chat-mismatch', streamId: 'stream-persisted' },
|
|
])
|
|
|
|
expect(markers.get('chat-mismatch')).toEqual({
|
|
chatId: 'chat-mismatch',
|
|
streamId: 'stream-persisted',
|
|
status: 'unknown',
|
|
})
|
|
})
|
|
|
|
it('treats a null persisted marker as inactive even when Redis still holds a lock (post-completion teardown window)', async () => {
|
|
mockGetChatStreamLockOwners.mockResolvedValueOnce({
|
|
status: 'verified',
|
|
ownersByChatId: new Map([['chat-starting', 'stream-starting']]),
|
|
})
|
|
|
|
const markers = await reconcileChatStreamMarkers([{ chatId: 'chat-starting', streamId: null }])
|
|
|
|
expect(markers.get('chat-starting')).toEqual({
|
|
chatId: 'chat-starting',
|
|
streamId: null,
|
|
status: 'inactive',
|
|
})
|
|
})
|
|
|
|
it('does not query locks when no chats have persisted stream markers', async () => {
|
|
const markers = await reconcileChatStreamMarkers([{ chatId: 'chat-idle', streamId: null }])
|
|
|
|
expect(markers.get('chat-idle')).toEqual({
|
|
chatId: 'chat-idle',
|
|
streamId: null,
|
|
status: 'inactive',
|
|
})
|
|
})
|
|
})
|