Files
simstudioai--sim/apps/sim/lib/execution/cancellation.test.ts
T
wehub-resource-sync d25d482dc2
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
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

126 lines
3.8 KiB
TypeScript

import { redisConfigMock, redisConfigMockFns } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockRedisSet, mockPublish, mockSubscribe } = vi.hoisted(() => ({
mockRedisSet: vi.fn(),
mockPublish: vi.fn(),
mockSubscribe: vi.fn(),
}))
const mockGetRedisClient = redisConfigMockFns.mockGetRedisClient
vi.mock('@/lib/core/config/redis', () => redisConfigMock)
vi.mock('@/lib/events/pubsub', () => ({
createPubSubChannel: () => ({
publish: mockPublish,
subscribe: mockSubscribe,
dispose: vi.fn(),
}),
}))
import { getCancellationChannel, markExecutionCancelled } from './cancellation'
import {
abortManualExecution,
registerManualExecutionAborter,
unregisterManualExecutionAborter,
} from './manual-cancellation'
describe('markExecutionCancelled', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('returns redis_unavailable when no Redis client exists', async () => {
mockGetRedisClient.mockReturnValue(null)
await expect(markExecutionCancelled('execution-1')).resolves.toEqual({
durablyRecorded: false,
reason: 'redis_unavailable',
})
})
it('returns recorded when Redis write succeeds', async () => {
mockRedisSet.mockResolvedValue('OK')
mockGetRedisClient.mockReturnValue({ set: mockRedisSet })
await expect(markExecutionCancelled('execution-1')).resolves.toEqual({
durablyRecorded: true,
reason: 'recorded',
})
})
it('returns redis_write_failed when Redis write throws', async () => {
mockRedisSet.mockRejectedValue(new Error('set failed'))
mockGetRedisClient.mockReturnValue({ set: mockRedisSet })
await expect(markExecutionCancelled('execution-1')).resolves.toEqual({
durablyRecorded: false,
reason: 'redis_write_failed',
})
})
it('publishes even when the Redis write fails so local subscribers wake up', async () => {
mockRedisSet.mockRejectedValue(new Error('set failed'))
mockGetRedisClient.mockReturnValue({ set: mockRedisSet })
await markExecutionCancelled('execution-write-failed')
expect(mockPublish).toHaveBeenCalledWith({ executionId: 'execution-write-failed' })
})
it('publishes a cancellation event after a successful Redis write', async () => {
mockRedisSet.mockResolvedValue('OK')
mockGetRedisClient.mockReturnValue({ set: mockRedisSet })
await markExecutionCancelled('execution-2')
expect(mockPublish).toHaveBeenCalledWith({ executionId: 'execution-2' })
expect(mockRedisSet.mock.invocationCallOrder[0]).toBeLessThan(
mockPublish.mock.invocationCallOrder[0]
)
})
it('publishes even when Redis is unavailable so local subscribers wake up', async () => {
mockGetRedisClient.mockReturnValue(null)
await markExecutionCancelled('execution-3')
expect(mockPublish).toHaveBeenCalledWith({ executionId: 'execution-3' })
})
})
describe('getCancellationChannel', () => {
it('returns the same channel instance across calls', () => {
expect(getCancellationChannel()).toBe(getCancellationChannel())
})
})
describe('manual execution cancellation registry', () => {
beforeEach(() => {
unregisterManualExecutionAborter('execution-1')
})
it('aborts registered executions', () => {
const abort = vi.fn()
registerManualExecutionAborter('execution-1', abort)
expect(abortManualExecution('execution-1')).toBe(true)
expect(abort).toHaveBeenCalledTimes(1)
})
it('returns false when no execution is registered', () => {
expect(abortManualExecution('execution-missing')).toBe(false)
})
it('unregisters executions', () => {
const abort = vi.fn()
registerManualExecutionAborter('execution-1', abort)
unregisterManualExecutionAborter('execution-1')
expect(abortManualExecution('execution-1')).toBe(false)
expect(abort).not.toHaveBeenCalled()
})
})