Files
simstudioai--sim/apps/sim/lib/copilot/persistence/tool-confirm/tool-confirm.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

167 lines
4.1 KiB
TypeScript

/**
* @vitest-environment node
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { getAsyncToolCalls } = vi.hoisted(() => ({
getAsyncToolCalls: vi.fn(),
}))
const channelHandlers = new Set<(event: any) => void>()
vi.mock('@/lib/copilot/async-runs/repository', () => ({
getAsyncToolCalls,
}))
vi.mock('@/lib/events/pubsub', () => ({
createPubSubChannel: () => ({
publish(event: any) {
for (const handler of channelHandlers) handler(event)
},
subscribe(handler: (event: any) => void) {
channelHandlers.add(handler)
return () => {
channelHandlers.delete(handler)
}
},
dispose() {},
}),
}))
import {
getToolConfirmation,
publishToolConfirmation,
waitForToolConfirmation,
} from '@/lib/copilot/persistence/tool-confirm'
describe('copilot orchestrator persistence', () => {
let row: {
status: string
error?: string | null
result?: unknown
updatedAt: Date
} | null
beforeEach(() => {
vi.clearAllMocks()
channelHandlers.clear()
row = null
getAsyncToolCalls.mockImplementation(async () => (row ? [row] : []))
})
it('reads the durable DB row as the source of truth', async () => {
row = {
status: 'completed',
result: { ok: true },
error: null,
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
}
await expect(getToolConfirmation('tool-1')).resolves.toEqual({
status: 'success',
message: undefined,
data: { ok: true },
timestamp: '2026-01-01T00:00:00.000Z',
})
})
it('preserves primitive durable results in confirmations', async () => {
row = {
status: 'completed',
result: 'done',
error: null,
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
}
await expect(getToolConfirmation('tool-1')).resolves.toEqual({
status: 'success',
message: undefined,
data: 'done',
timestamp: '2026-01-01T00:00:00.000Z',
})
})
it('ignores delivered rows in request confirmation flow', async () => {
row = {
status: 'delivered',
result: { ok: true },
error: null,
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
}
await expect(getToolConfirmation('tool-1')).resolves.toBeNull()
})
it('ignores background when waiting for a foreground terminal status', async () => {
row = {
status: 'pending',
error: null,
result: null,
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
}
const waitPromise = waitForToolConfirmation('tool-1', 5_000, undefined, {
acceptStatus: (status) =>
status === 'success' || status === 'error' || status === 'cancelled',
})
publishToolConfirmation({
toolCallId: 'tool-1',
status: 'background',
message: 'Client disconnected, execution continuing server-side',
timestamp: '2026-01-01T00:00:01.000Z',
})
await Promise.resolve()
row = {
status: 'completed',
error: null,
result: { ok: true },
updatedAt: new Date('2026-01-01T00:00:02.000Z'),
}
publishToolConfirmation({
toolCallId: 'tool-1',
status: 'success',
timestamp: '2026-01-01T00:00:02.000Z',
})
await expect(waitPromise).resolves.toEqual({
status: 'success',
message: undefined,
data: { ok: true },
timestamp: '2026-01-01T00:00:02.000Z',
})
})
it('resolves background from the pubsub event when the durable row stays pending', async () => {
row = {
status: 'pending',
error: null,
result: null,
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
}
const waitPromise = waitForToolConfirmation('tool-1', 5_000, undefined, {
acceptStatus: (status) => status === 'background',
})
await Promise.resolve()
publishToolConfirmation({
toolCallId: 'tool-1',
status: 'background',
message: 'Client disconnected, execution continuing server-side',
timestamp: '2026-01-01T00:00:01.000Z',
})
await expect(waitPromise).resolves.toEqual({
status: 'background',
message: 'Client disconnected, execution continuing server-side',
timestamp: '2026-01-01T00:00:01.000Z',
})
})
})