6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
263 lines
8.4 KiB
TypeScript
263 lines
8.4 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import type { BillingAttributionSnapshot } from '@/lib/billing/core/billing-attribution'
|
|
import { ADMISSION_ERROR_CODE } from '@/lib/core/admission/transient-failure'
|
|
|
|
const { checkAttributedUsageLimitsMock, reserveExecutionSlotMock, resolveBillingAttributionMock } =
|
|
vi.hoisted(() => ({
|
|
checkAttributedUsageLimitsMock: vi.fn(),
|
|
reserveExecutionSlotMock: vi.fn(),
|
|
resolveBillingAttributionMock: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/billing/core/billing-attribution', () => ({
|
|
checkAttributedUsageLimits: checkAttributedUsageLimitsMock,
|
|
resolveBillingAttribution: resolveBillingAttributionMock,
|
|
}))
|
|
|
|
vi.mock('@/lib/billing/calculations/usage-reservation', () => ({
|
|
reserveExecutionSlot: reserveExecutionSlotMock,
|
|
UsageReservationUnavailableError: class UsageReservationUnavailableError extends Error {
|
|
readonly code = ADMISSION_ERROR_CODE.RESERVATION_INFRASTRUCTURE
|
|
readonly statusCode = 503
|
|
readonly retryable = true
|
|
},
|
|
}))
|
|
|
|
import {
|
|
applyCreateWorkflowOutputToContext,
|
|
prepareWorkflowExecutionAdmission,
|
|
resolveWorkflowExecutionBillingAttribution,
|
|
WorkflowExecutionAdmissionError,
|
|
} from '@/lib/copilot/request/tools/workflow-context'
|
|
import type { ExecutionContext } from '@/lib/copilot/request/types'
|
|
|
|
const billingAttribution: BillingAttributionSnapshot = {
|
|
actorUserId: 'user-1',
|
|
workspaceId: 'workspace-1',
|
|
organizationId: null,
|
|
billedAccountUserId: 'owner-1',
|
|
billingEntity: { type: 'user', id: 'owner-1' },
|
|
billingPeriod: {
|
|
start: '2026-07-01T00:00:00.000Z',
|
|
end: '2026-08-01T00:00:00.000Z',
|
|
},
|
|
payerSubscription: null,
|
|
}
|
|
|
|
const childBillingAttribution: BillingAttributionSnapshot = Object.freeze({
|
|
actorUserId: 'user-1',
|
|
workspaceId: 'workspace-2',
|
|
organizationId: 'organization-2',
|
|
billedAccountUserId: 'owner-2',
|
|
billingEntity: { type: 'organization', id: 'organization-2' },
|
|
billingPeriod: {
|
|
start: '2026-07-01T00:00:00.000Z',
|
|
end: '2026-08-01T00:00:00.000Z',
|
|
},
|
|
payerSubscription: {
|
|
id: 'subscription-2',
|
|
plan: 'enterprise',
|
|
referenceId: 'organization-2',
|
|
status: 'active',
|
|
periodStart: '2026-07-01T00:00:00.000Z',
|
|
periodEnd: '2026-08-01T00:00:00.000Z',
|
|
seats: 25,
|
|
enterpriseConcurrencyLimit: 1250,
|
|
},
|
|
})
|
|
|
|
function createContext(): ExecutionContext {
|
|
return {
|
|
userId: 'user-1',
|
|
workflowId: '',
|
|
workspaceId: 'workspace-1',
|
|
billingAttribution,
|
|
}
|
|
}
|
|
|
|
describe('create_workflow execution context', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
checkAttributedUsageLimitsMock.mockResolvedValue({
|
|
isExceeded: false,
|
|
payerUsage: { currentUsage: 1, limit: 10 },
|
|
})
|
|
reserveExecutionSlotMock.mockResolvedValue({ reserved: true, created: true })
|
|
})
|
|
|
|
it('adopts a same-workspace workflow without replacing its billing snapshot', () => {
|
|
const context = createContext()
|
|
|
|
applyCreateWorkflowOutputToContext(
|
|
{ workflowId: 'workflow-2', workspaceId: 'workspace-1' },
|
|
context
|
|
)
|
|
|
|
expect(context).toMatchObject({
|
|
userId: 'user-1',
|
|
workflowId: 'workflow-2',
|
|
workspaceId: 'workspace-1',
|
|
billingAttribution,
|
|
})
|
|
expect(context.billingAttribution).toBe(billingAttribution)
|
|
})
|
|
|
|
it('does not adopt a cross-workspace workflow', () => {
|
|
const context = createContext()
|
|
|
|
applyCreateWorkflowOutputToContext(
|
|
{ workflowId: 'workflow-2', workspaceId: 'workspace-2' },
|
|
context
|
|
)
|
|
|
|
expect(context).toMatchObject({
|
|
userId: 'user-1',
|
|
workflowId: '',
|
|
workspaceId: 'workspace-1',
|
|
billingAttribution,
|
|
})
|
|
expect(context.billingAttribution).toBe(billingAttribution)
|
|
})
|
|
|
|
it.each([
|
|
['missing workspace scope', { workflowId: 'workflow-2' }],
|
|
['non-string workspace scope', { workflowId: 'workflow-2', workspaceId: 2 }],
|
|
['missing workflow id', { workspaceId: 'workspace-1' }],
|
|
['non-string workflow id', { workflowId: 2, workspaceId: 'workspace-1' }],
|
|
['non-object output', 'workflow-2'],
|
|
])('does not adopt output with %s', (_case, output) => {
|
|
const context = createContext()
|
|
|
|
applyCreateWorkflowOutputToContext(output, context)
|
|
|
|
expect(context).toMatchObject({
|
|
workflowId: '',
|
|
workspaceId: 'workspace-1',
|
|
billingAttribution,
|
|
})
|
|
})
|
|
|
|
it('does not replace an existing rooted workflow context', () => {
|
|
const context = { ...createContext(), workflowId: 'workflow-1' }
|
|
|
|
applyCreateWorkflowOutputToContext(
|
|
{ workflowId: 'workflow-2', workspaceId: 'workspace-2' },
|
|
context
|
|
)
|
|
|
|
expect(context).toMatchObject({
|
|
workflowId: 'workflow-1',
|
|
workspaceId: 'workspace-1',
|
|
billingAttribution,
|
|
})
|
|
})
|
|
|
|
it('reuses the root snapshot for same-workspace workflow execution', async () => {
|
|
const context = createContext()
|
|
|
|
const attribution = await resolveWorkflowExecutionBillingAttribution(context, 'workspace-1')
|
|
|
|
expect(attribution).toBe(billingAttribution)
|
|
expect(resolveBillingAttributionMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does not implicitly resolve billing without a root lifecycle snapshot', async () => {
|
|
const context = { ...createContext(), billingAttribution: undefined }
|
|
|
|
const attribution = await resolveWorkflowExecutionBillingAttribution(context, 'workspace-2')
|
|
|
|
expect(attribution).toBeUndefined()
|
|
expect(resolveBillingAttributionMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('resolves one child snapshot for cross-workspace workflow execution', async () => {
|
|
const context = createContext()
|
|
resolveBillingAttributionMock.mockResolvedValue(childBillingAttribution)
|
|
|
|
const attribution = await resolveWorkflowExecutionBillingAttribution(context, 'workspace-2')
|
|
|
|
expect(resolveBillingAttributionMock).toHaveBeenCalledOnce()
|
|
expect(resolveBillingAttributionMock).toHaveBeenCalledWith({
|
|
actorUserId: 'user-1',
|
|
workspaceId: 'workspace-2',
|
|
})
|
|
expect(attribution).toBe(childBillingAttribution)
|
|
expect(attribution).not.toBe(billingAttribution)
|
|
expect(Object.isFrozen(attribution)).toBe(true)
|
|
expect(context.billingAttribution).toBe(billingAttribution)
|
|
})
|
|
})
|
|
|
|
describe('prepareWorkflowExecutionAdmission', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resolveBillingAttributionMock.mockResolvedValue(childBillingAttribution)
|
|
checkAttributedUsageLimitsMock.mockResolvedValue({
|
|
isExceeded: false,
|
|
payerUsage: { currentUsage: 1, limit: 10 },
|
|
})
|
|
reserveExecutionSlotMock.mockResolvedValue({ reserved: true, created: true })
|
|
})
|
|
|
|
it('forwards the target Enterprise concurrency override to admission', async () => {
|
|
const result = await prepareWorkflowExecutionAdmission(
|
|
createContext(),
|
|
'workspace-2',
|
|
'child-execution-1'
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
billingAttribution: childBillingAttribution,
|
|
targetReservation: true,
|
|
})
|
|
expect(reserveExecutionSlotMock).toHaveBeenCalledWith({
|
|
billingEntity: { type: 'organization', id: 'organization-2' },
|
|
executionId: 'child-execution-1',
|
|
plan: 'enterprise',
|
|
enterpriseConcurrencyLimit: 1250,
|
|
currentUsage: 1,
|
|
limit: 10,
|
|
})
|
|
})
|
|
|
|
it.each([
|
|
{
|
|
reason: 'payer_concurrency' as const,
|
|
code: ADMISSION_ERROR_CODE.RESERVATION_CONCURRENCY,
|
|
statusCode: 429,
|
|
retryable: true,
|
|
message: 'Target workspace execution concurrency is currently exhausted',
|
|
},
|
|
{
|
|
reason: 'payer_headroom' as const,
|
|
code: ADMISSION_ERROR_CODE.RESERVATION_PAYER_HEADROOM,
|
|
statusCode: 402,
|
|
retryable: false,
|
|
message: 'Target workspace payer usage headroom is currently exhausted',
|
|
},
|
|
{
|
|
reason: 'member_headroom' as const,
|
|
code: ADMISSION_ERROR_CODE.RESERVATION_MEMBER_HEADROOM,
|
|
statusCode: 402,
|
|
retryable: false,
|
|
message: 'Target workspace member usage headroom is currently exhausted',
|
|
},
|
|
])(
|
|
'maps $reason with target-specific wording',
|
|
async ({ reason, code, statusCode, retryable, message }) => {
|
|
reserveExecutionSlotMock.mockResolvedValueOnce({ reserved: false, reason })
|
|
|
|
try {
|
|
await prepareWorkflowExecutionAdmission(createContext(), 'workspace-2', 'child-execution-1')
|
|
throw new Error('Expected target admission to reject the reservation')
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(WorkflowExecutionAdmissionError)
|
|
expect(error).toMatchObject({ code, statusCode, retryable, message })
|
|
}
|
|
}
|
|
)
|
|
})
|