Files
simstudioai--sim/apps/sim/app/workspace/providers/socket-join-controller.test.ts
T
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

247 lines
7.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
SOCKET_JOIN_RETRY_BASE_DELAY_MS,
SOCKET_JOIN_RETRY_MAX_DELAY_MS,
SocketJoinController,
} from '@/app/workspace/providers/socket-join-controller'
describe('SocketJoinController', () => {
it('blocks rejoining a deleted workflow until the desired workflow changes', () => {
const controller = new SocketJoinController()
expect(controller.setConnected(true)).toEqual([])
expect(controller.requestWorkflow('workflow-a')).toEqual([
{ type: 'join', workflowId: 'workflow-a' },
])
expect(controller.handleJoinSuccess('workflow-a')).toMatchObject({
apply: true,
ignored: false,
commands: [],
workflowId: 'workflow-a',
})
expect(controller.handleWorkflowDeleted('workflow-a')).toEqual({
shouldClearCurrent: true,
commands: [],
})
expect(controller.requestWorkflow('workflow-a')).toEqual([])
expect(controller.requestWorkflow('workflow-b')).toEqual([
{ type: 'join', workflowId: 'workflow-b' },
])
})
it('joins only the latest desired workflow after rapid A to B to C switching', () => {
const controller = new SocketJoinController()
controller.setConnected(true)
controller.requestWorkflow('workflow-a')
controller.handleJoinSuccess('workflow-a')
expect(controller.requestWorkflow('workflow-b')).toEqual([
{ type: 'join', workflowId: 'workflow-b' },
])
expect(controller.requestWorkflow('workflow-c')).toEqual([])
expect(controller.handleJoinSuccess('workflow-b')).toMatchObject({
apply: false,
ignored: true,
workflowId: 'workflow-b',
commands: [{ type: 'join', workflowId: 'workflow-c' }],
})
expect(controller.handleJoinSuccess('workflow-c')).toMatchObject({
apply: true,
ignored: false,
workflowId: 'workflow-c',
commands: [],
})
})
it('rejoins the original workflow when a stale success lands after switching back', () => {
const controller = new SocketJoinController()
controller.setConnected(true)
controller.requestWorkflow('workflow-a')
controller.handleJoinSuccess('workflow-a')
expect(controller.requestWorkflow('workflow-b')).toEqual([
{ type: 'join', workflowId: 'workflow-b' },
])
expect(controller.requestWorkflow('workflow-a')).toEqual([])
expect(controller.handleJoinSuccess('workflow-b')).toMatchObject({
apply: false,
ignored: true,
workflowId: 'workflow-b',
commands: [{ type: 'join', workflowId: 'workflow-a' }],
})
expect(controller.handleJoinSuccess('workflow-a')).toMatchObject({
apply: true,
ignored: false,
workflowId: 'workflow-a',
commands: [],
})
})
it('leaves the room when a late join succeeds after navigating away', () => {
const controller = new SocketJoinController()
controller.setConnected(true)
controller.requestWorkflow('workflow-a')
controller.handleJoinSuccess('workflow-a')
expect(controller.requestWorkflow('workflow-b')).toEqual([
{ type: 'join', workflowId: 'workflow-b' },
])
expect(controller.requestWorkflow(null)).toEqual([])
expect(controller.handleJoinSuccess('workflow-b')).toMatchObject({
apply: false,
ignored: true,
workflowId: 'workflow-b',
commands: [{ type: 'leave' }],
})
})
it('preserves the last joined workflow during retryable switch failures', () => {
const controller = new SocketJoinController()
controller.setConnected(true)
expect(controller.requestWorkflow('workflow-a')).toEqual([
{ type: 'join', workflowId: 'workflow-a' },
])
controller.handleJoinSuccess('workflow-a')
expect(controller.requestWorkflow('workflow-b')).toEqual([
{ type: 'join', workflowId: 'workflow-b' },
])
const errorResult = controller.handleJoinError({
workflowId: 'workflow-b',
retryable: true,
})
expect(errorResult.apply).toBe(false)
expect(errorResult.retryScheduled).toBe(true)
expect(errorResult.commands).toEqual([
{
type: 'schedule-retry',
workflowId: 'workflow-b',
attempt: 1,
delayMs: SOCKET_JOIN_RETRY_BASE_DELAY_MS,
},
])
expect(controller.getJoinedWorkflowId()).toBe('workflow-a')
expect(controller.retryJoin('workflow-b')).toEqual([{ type: 'join', workflowId: 'workflow-b' }])
})
it('uses capped exponential backoff for retryable join failures', () => {
const controller = new SocketJoinController()
controller.setConnected(true)
controller.requestWorkflow('workflow-a')
const first = controller.handleJoinError({ workflowId: 'workflow-a', retryable: true })
expect(first.commands).toEqual([
{
type: 'schedule-retry',
workflowId: 'workflow-a',
attempt: 1,
delayMs: SOCKET_JOIN_RETRY_BASE_DELAY_MS,
},
])
controller.retryJoin('workflow-a')
const second = controller.handleJoinError({ workflowId: 'workflow-a', retryable: true })
expect(second.commands).toEqual([
{
type: 'schedule-retry',
workflowId: 'workflow-a',
attempt: 2,
delayMs: SOCKET_JOIN_RETRY_BASE_DELAY_MS * 2,
},
])
controller.retryJoin('workflow-a')
controller.handleJoinError({ workflowId: 'workflow-a', retryable: true })
controller.retryJoin('workflow-a')
const fourth = controller.handleJoinError({ workflowId: 'workflow-a', retryable: true })
expect(fourth.commands).toEqual([
{
type: 'schedule-retry',
workflowId: 'workflow-a',
attempt: 4,
delayMs: SOCKET_JOIN_RETRY_BASE_DELAY_MS * 8,
},
])
controller.retryJoin('workflow-a')
const fifth = controller.handleJoinError({ workflowId: 'workflow-a', retryable: true })
expect(fifth.commands).toEqual([
{
type: 'schedule-retry',
workflowId: 'workflow-a',
attempt: 5,
delayMs: SOCKET_JOIN_RETRY_MAX_DELAY_MS,
},
])
})
it('blocks a permanently failed workflow and leaves the fallback room cleanly', () => {
const controller = new SocketJoinController()
controller.setConnected(true)
controller.requestWorkflow('workflow-a')
controller.handleJoinSuccess('workflow-a')
expect(controller.requestWorkflow('workflow-b')).toEqual([
{ type: 'join', workflowId: 'workflow-b' },
])
const errorResult = controller.handleJoinError({
workflowId: 'workflow-b',
retryable: false,
})
expect(errorResult.apply).toBe(true)
expect(errorResult.commands).toEqual([{ type: 'leave' }])
expect(controller.getJoinedWorkflowId()).toBeNull()
expect(controller.requestWorkflow('workflow-b')).toEqual([])
expect(controller.requestWorkflow('workflow-c')).toEqual([
{ type: 'join', workflowId: 'workflow-c' },
])
})
it('rejoins the desired workflow when the server session is lost', () => {
const controller = new SocketJoinController()
controller.setConnected(true)
controller.requestWorkflow('workflow-a')
controller.handleJoinSuccess('workflow-a')
expect(controller.forceRejoinWorkflow('workflow-a')).toEqual([
{ type: 'join', workflowId: 'workflow-a' },
])
expect(controller.getJoinedWorkflowId()).toBeNull()
})
it('resolves retryable errors without workflowId against the pending join', () => {
const controller = new SocketJoinController()
controller.setConnected(true)
controller.requestWorkflow('workflow-a')
const errorResult = controller.handleJoinError({ retryable: true })
expect(errorResult.workflowId).toBe('workflow-a')
expect(errorResult.retryScheduled).toBe(true)
expect(errorResult.commands).toEqual([
{
type: 'schedule-retry',
workflowId: 'workflow-a',
attempt: 1,
delayMs: SOCKET_JOIN_RETRY_BASE_DELAY_MS,
},
])
})
})