Files
simstudioai--sim/apps/sim/background/knowledge-processing.test.ts
WeHub Mirror 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
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

140 lines
4.0 KiB
TypeScript

/**
* @vitest-environment node
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockAssertBillingAttributionSnapshot, mockProcessDocumentAsync, mockTask } = vi.hoisted(
() => ({
mockAssertBillingAttributionSnapshot: vi.fn(),
mockProcessDocumentAsync: vi.fn(),
mockTask: vi.fn((config) => config),
})
)
vi.mock('@trigger.dev/sdk', () => ({ task: mockTask }))
vi.mock('@/lib/billing/core/billing-attribution', () => ({
assertBillingAttributionSnapshot: mockAssertBillingAttributionSnapshot,
}))
vi.mock('@/lib/knowledge/documents/service', () => ({
processDocumentAsync: mockProcessDocumentAsync,
}))
import { runDocumentProcessing } from '@/background/knowledge-processing'
const BILLING_ATTRIBUTION = {
actorUserId: 'external-admin',
workspaceId: 'workspace-1',
organizationId: null,
billedAccountUserId: 'workspace-owner',
billingEntity: { type: 'user' as const, id: 'workspace-owner' },
billingPeriod: {
start: '2026-07-01T00:00:00.000Z',
end: '2026-08-01T00:00:00.000Z',
},
payerSubscription: null,
}
const BASE_PAYLOAD = {
knowledgeBaseId: 'knowledge-base-1',
documentId: 'document-1',
docData: {
filename: 'document.txt',
fileUrl: 'https://example.com/document.txt',
fileSize: 128,
mimeType: 'text/plain',
},
processingOptions: {},
requestId: 'request-1',
}
const WORKSPACE_PAYLOAD = {
...BASE_PAYLOAD,
billingScope: 'workspace' as const,
actorUserId: 'external-admin',
workspaceId: 'workspace-1',
billingAttribution: BILLING_ATTRIBUTION,
}
describe('knowledge processing worker', () => {
beforeEach(() => {
vi.clearAllMocks()
mockAssertBillingAttributionSnapshot.mockImplementation((value) => {
if (!value) {
throw new Error('Billing attribution snapshot must be an object')
}
return value
})
mockProcessDocumentAsync.mockResolvedValue(undefined)
})
it('rejects workspace work without attribution before document processing starts', async () => {
await expect(
runDocumentProcessing({
...BASE_PAYLOAD,
billingScope: 'workspace',
actorUserId: 'external-admin',
workspaceId: 'workspace-1',
})
).rejects.toThrow('Workspace document processing requires a billing attribution snapshot')
expect(mockProcessDocumentAsync).not.toHaveBeenCalled()
})
it('preserves the validated actor and payer snapshot through serialization', async () => {
await runDocumentProcessing(structuredClone(WORKSPACE_PAYLOAD))
expect(mockProcessDocumentAsync).toHaveBeenCalledWith(
'knowledge-base-1',
'document-1',
BASE_PAYLOAD.docData,
{},
{
billingScope: 'workspace',
actorUserId: 'external-admin',
workspaceId: 'workspace-1',
billingAttribution: BILLING_ATTRIBUTION,
}
)
})
it('rejects an actor mismatch before document processing starts', async () => {
await expect(
runDocumentProcessing({
...WORKSPACE_PAYLOAD,
actorUserId: 'different-actor',
})
).rejects.toThrow('Document processing actor does not match billing attribution')
expect(mockProcessDocumentAsync).not.toHaveBeenCalled()
})
it('rejects a workspace mismatch before document processing starts', async () => {
await expect(
runDocumentProcessing({
...WORKSPACE_PAYLOAD,
workspaceId: 'workspace-2',
})
).rejects.toThrow('Document processing workspace does not match billing attribution')
expect(mockProcessDocumentAsync).not.toHaveBeenCalled()
})
it('preserves explicit non-workspace processing without workspace attribution', async () => {
await runDocumentProcessing({
...BASE_PAYLOAD,
billingScope: 'non-workspace',
actorUserId: 'legacy-owner',
workspaceId: null,
})
expect(mockProcessDocumentAsync).toHaveBeenCalledWith(
'knowledge-base-1',
'document-1',
BASE_PAYLOAD.docData,
{},
{
billingScope: 'non-workspace',
actorUserId: 'legacy-owner',
workspaceId: null,
}
)
})
})