Files
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

156 lines
4.6 KiB
TypeScript

/**
* @vitest-environment node
*/
import {
dbChainMockFns,
defaultMockEnv,
resetDbChainMock,
resetEnvFlagsMock,
setEnvFlags,
} from '@sim/testing'
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import type { BillingAttributionSnapshot } from '@/lib/billing/core/billing-attribution'
import { env } from '@/lib/core/config/env'
const { mockBatchTrigger } = vi.hoisted(() => ({
mockBatchTrigger: vi.fn(),
}))
vi.mock('@trigger.dev/sdk', () => ({
tasks: {
batchTrigger: mockBatchTrigger,
},
}))
vi.mock('@/lib/core/async-jobs/region', () => ({
resolveTriggerRegion: vi.fn().mockResolvedValue('us-east-1'),
}))
/**
* Under `isolate: false` the shared `@/lib/knowledge/embeddings` /
* `documents/service` modules may be cached bound to the REAL env module, so
* mutate the real `env` object per test (restored afterAll) instead of
* vi.mock'ing a file-local replacement a cached consumer would never see.
*/
const envSnapshot = { ...env }
afterAll(() => {
for (const key of Object.keys(env)) {
delete (env as Record<string, unknown>)[key]
}
Object.assign(env, envSnapshot)
})
import { processDocumentsWithQueue } from '@/lib/knowledge/documents/service'
const BILLING_ATTRIBUTION = {
actorUserId: 'external-admin',
workspaceId: 'workspace-1',
organizationId: null,
billedAccountUserId: 'workspace-owner',
billingEntity: { type: 'user', id: 'workspace-owner' },
billingPeriod: {
start: '2026-07-01T00:00:00.000Z',
end: '2026-08-01T00:00:00.000Z',
},
payerSubscription: null,
} satisfies BillingAttributionSnapshot
const DOCUMENT = {
documentId: 'document-1',
filename: 'document.txt',
fileUrl: 'https://example.com/document.txt',
fileSize: 128,
mimeType: 'text/plain',
}
beforeAll(() => {
setEnvFlags({ isTriggerDevEnabled: true })
})
afterAll(resetEnvFlagsMock)
describe('processDocumentsWithQueue billing attribution', () => {
beforeEach(() => {
vi.clearAllMocks()
resetDbChainMock()
mockBatchTrigger.mockResolvedValue({ batchId: 'batch-1' })
for (const key of Object.keys(env)) {
delete (env as Record<string, unknown>)[key]
}
Object.assign(env, { ...defaultMockEnv, TRIGGER_SECRET_KEY: 'trigger-secret' })
})
it('validates and preserves workspace attribution before enqueue', async () => {
dbChainMockFns.limit.mockResolvedValueOnce([
{ userId: 'knowledge-owner', workspaceId: 'workspace-1' },
])
await processDocumentsWithQueue(
[DOCUMENT],
'knowledge-base-1',
{},
'request-1',
BILLING_ATTRIBUTION
)
const jobs = mockBatchTrigger.mock.calls[0][1]
expect(structuredClone(jobs[0].payload)).toEqual({
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',
billingScope: 'workspace',
actorUserId: 'external-admin',
workspaceId: 'workspace-1',
billingAttribution: BILLING_ATTRIBUTION,
})
})
it('rejects missing workspace attribution without enqueueing', async () => {
dbChainMockFns.limit.mockResolvedValueOnce([
{ userId: 'knowledge-owner', workspaceId: 'workspace-1' },
])
await expect(
processDocumentsWithQueue([DOCUMENT], 'knowledge-base-1', {}, 'request-1', undefined)
).rejects.toThrow('Workspace document processing requires a billing attribution snapshot')
expect(mockBatchTrigger).not.toHaveBeenCalled()
})
it('rejects mismatched workspace attribution without enqueueing', async () => {
dbChainMockFns.limit.mockResolvedValueOnce([
{ userId: 'knowledge-owner', workspaceId: 'workspace-2' },
])
await expect(
processDocumentsWithQueue(
[DOCUMENT],
'knowledge-base-1',
{},
'request-1',
BILLING_ATTRIBUTION
)
).rejects.toThrow('Document processing workspace does not match billing attribution')
expect(mockBatchTrigger).not.toHaveBeenCalled()
})
it('enqueues workspace-less knowledge bases with an explicit non-workspace payload', async () => {
dbChainMockFns.limit.mockResolvedValueOnce([{ userId: 'legacy-owner', workspaceId: null }])
await processDocumentsWithQueue([DOCUMENT], 'knowledge-base-1', {}, 'request-1', undefined)
const jobs = mockBatchTrigger.mock.calls[0][1]
expect(jobs[0].payload).toMatchObject({
billingScope: 'non-workspace',
actorUserId: 'legacy-owner',
workspaceId: null,
})
expect(jobs[0].payload).not.toHaveProperty('billingAttribution')
})
})