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
232 lines
6.5 KiB
TypeScript
232 lines
6.5 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { dbChainMockFns, resetDbChainMock } from '@sim/testing'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const {
|
|
mockCheckActorUsageLimits,
|
|
mockGenerateEmbeddings,
|
|
mockGetBoundWorkspaceFileSecretProvenanceByMetadata,
|
|
mockGetFileMetadataByKeys,
|
|
mockProcessDocument,
|
|
} = vi.hoisted(() => ({
|
|
mockCheckActorUsageLimits: vi.fn(),
|
|
mockGenerateEmbeddings: vi.fn(),
|
|
mockGetBoundWorkspaceFileSecretProvenanceByMetadata: vi.fn(),
|
|
mockGetFileMetadataByKeys: vi.fn(),
|
|
mockProcessDocument: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/billing/calculations/usage-monitor', () => ({
|
|
checkActorUsageLimits: mockCheckActorUsageLimits,
|
|
}))
|
|
|
|
vi.mock('@/lib/knowledge/documents/document-processor', () => ({
|
|
processDocument: mockProcessDocument,
|
|
}))
|
|
|
|
vi.mock('@/lib/knowledge/embedding-models', () => ({
|
|
getEmbeddingModelInfo: vi.fn(() => ({ tokenizerProvider: 'openai' })),
|
|
}))
|
|
|
|
vi.mock('@/lib/knowledge/embeddings', () => ({
|
|
generateEmbeddings: mockGenerateEmbeddings,
|
|
}))
|
|
|
|
vi.mock('@/lib/uploads/contexts/workspace/workspace-file-secret-provenance', () => ({
|
|
getBoundWorkspaceFileSecretProvenanceByMetadata:
|
|
mockGetBoundWorkspaceFileSecretProvenanceByMetadata,
|
|
}))
|
|
|
|
vi.mock('@/lib/uploads/core/storage-service', () => ({
|
|
deleteFile: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/uploads/server/metadata', () => ({
|
|
deleteFileMetadataByIdentity: vi.fn(),
|
|
getFileMetadataByKeys: mockGetFileMetadataByKeys,
|
|
}))
|
|
|
|
import { processDocumentAsync } from '@/lib/knowledge/documents/service'
|
|
|
|
const PERSISTED_KEY = 'workspace/workspace-1/persisted.pdf'
|
|
const PERSISTED_URL = `/api/files/serve/${encodeURIComponent(PERSISTED_KEY)}?context=workspace`
|
|
const CONTENT_UPDATED_AT = new Date('2026-08-05T12:00:00.000Z')
|
|
|
|
const PERSISTED_CONTEXT = {
|
|
workspaceId: null,
|
|
knowledgeBaseUserId: 'knowledge-owner',
|
|
chunkingConfig: null,
|
|
embeddingModel: 'text-embedding-3-small',
|
|
billedAccountUserId: null,
|
|
uploadedBy: 'uploader-1',
|
|
filename: 'persisted.pdf',
|
|
fileUrl: PERSISTED_URL,
|
|
fileSize: 512,
|
|
mimeType: 'application/pdf',
|
|
tag1: null,
|
|
tag2: null,
|
|
tag3: null,
|
|
tag4: null,
|
|
tag5: null,
|
|
tag6: null,
|
|
tag7: null,
|
|
number1: null,
|
|
number2: null,
|
|
number3: null,
|
|
number4: null,
|
|
number5: null,
|
|
date1: null,
|
|
date2: null,
|
|
boolean1: null,
|
|
boolean2: null,
|
|
boolean3: null,
|
|
}
|
|
|
|
const PERSISTED_PROVENANCE_ROW = {
|
|
id: 'document-1',
|
|
secretProvenanceVersion: null,
|
|
filename: PERSISTED_CONTEXT.filename,
|
|
fileUrl: PERSISTED_CONTEXT.fileUrl,
|
|
contentHash: null,
|
|
sourceUrl: null,
|
|
tag1: null,
|
|
tag2: null,
|
|
tag3: null,
|
|
tag4: null,
|
|
tag5: null,
|
|
tag6: null,
|
|
tag7: null,
|
|
number1: null,
|
|
number2: null,
|
|
number3: null,
|
|
number4: null,
|
|
number5: null,
|
|
date1: null,
|
|
date2: null,
|
|
boolean1: null,
|
|
boolean2: null,
|
|
boolean3: null,
|
|
provenanceSourceHash: null,
|
|
status: null,
|
|
entries: null,
|
|
}
|
|
|
|
const SOURCE_BINDING = {
|
|
id: 'source-file-1',
|
|
key: PERSISTED_KEY,
|
|
userId: 'uploader-1',
|
|
workspaceId: 'workspace-1',
|
|
context: 'workspace',
|
|
originalName: PERSISTED_CONTEXT.filename,
|
|
displayName: PERSISTED_CONTEXT.filename,
|
|
contentType: PERSISTED_CONTEXT.mimeType,
|
|
size: PERSISTED_CONTEXT.fileSize,
|
|
folderId: null,
|
|
uploadedAt: CONTENT_UPDATED_AT,
|
|
contentUpdatedAt: CONTENT_UPDATED_AT,
|
|
deletedAt: null,
|
|
secretProvenanceVersion: null,
|
|
}
|
|
|
|
describe('knowledge document processing source', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetDbChainMock()
|
|
dbChainMockFns.limit
|
|
.mockResolvedValueOnce([PERSISTED_CONTEXT])
|
|
.mockResolvedValueOnce([PERSISTED_PROVENANCE_ROW])
|
|
.mockResolvedValueOnce([{ id: 'document-1' }])
|
|
mockCheckActorUsageLimits.mockResolvedValue({ isExceeded: false })
|
|
mockGetFileMetadataByKeys.mockImplementation(async (_keys: string[], context: string) =>
|
|
context === 'workspace' ? [SOURCE_BINDING] : []
|
|
)
|
|
mockGetBoundWorkspaceFileSecretProvenanceByMetadata.mockResolvedValue(
|
|
new Map([[SOURCE_BINDING.id, { status: 'exact', entries: [] }]])
|
|
)
|
|
mockProcessDocument.mockResolvedValue({
|
|
chunks: [],
|
|
metadata: { chunkCount: 0, tokenCount: 0, characterCount: 0 },
|
|
})
|
|
})
|
|
|
|
it('uses the persisted document source instead of stale queued source fields', async () => {
|
|
await processDocumentAsync('knowledge-base-1', 'document-1', {
|
|
filename: 'stale.pdf',
|
|
fileUrl: 'https://example.com/stale.pdf',
|
|
fileSize: 1,
|
|
mimeType: 'text/plain',
|
|
})
|
|
|
|
expect(mockGetFileMetadataByKeys).toHaveBeenCalledWith(
|
|
[PERSISTED_KEY],
|
|
'workspace',
|
|
expect.anything()
|
|
)
|
|
expect(mockGetBoundWorkspaceFileSecretProvenanceByMetadata).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
[SOURCE_BINDING]
|
|
)
|
|
expect(mockProcessDocument).toHaveBeenCalledWith(
|
|
PERSISTED_CONTEXT.fileUrl,
|
|
PERSISTED_CONTEXT.filename,
|
|
PERSISTED_CONTEXT.mimeType,
|
|
1024,
|
|
200,
|
|
100,
|
|
PERSISTED_CONTEXT.uploadedBy,
|
|
null,
|
|
undefined,
|
|
undefined
|
|
)
|
|
expect(mockGenerateEmbeddings).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('processes a legacy document when its workspace metadata row no longer exists', async () => {
|
|
mockGetFileMetadataByKeys.mockResolvedValue([])
|
|
mockGetBoundWorkspaceFileSecretProvenanceByMetadata.mockResolvedValue(new Map())
|
|
|
|
await processDocumentAsync('knowledge-base-1', 'document-1', {
|
|
filename: 'stale.pdf',
|
|
fileUrl: 'https://example.com/stale.pdf',
|
|
fileSize: 1,
|
|
mimeType: 'text/plain',
|
|
})
|
|
|
|
expect(mockProcessDocument).toHaveBeenCalledWith(
|
|
PERSISTED_CONTEXT.fileUrl,
|
|
PERSISTED_CONTEXT.filename,
|
|
PERSISTED_CONTEXT.mimeType,
|
|
1024,
|
|
200,
|
|
100,
|
|
PERSISTED_CONTEXT.uploadedBy,
|
|
null,
|
|
undefined,
|
|
undefined
|
|
)
|
|
})
|
|
|
|
it('fails before parsing an existing document when its current source is tracked unknown', async () => {
|
|
mockGetFileMetadataByKeys.mockImplementation(async (_keys: string[], context: string) =>
|
|
context === 'workspace' ? [{ ...SOURCE_BINDING, secretProvenanceVersion: 1 }] : []
|
|
)
|
|
mockGetBoundWorkspaceFileSecretProvenanceByMetadata.mockResolvedValue(
|
|
new Map([[SOURCE_BINDING.id, { status: 'unknown' }]])
|
|
)
|
|
|
|
await expect(
|
|
processDocumentAsync('knowledge-base-1', 'document-1', {
|
|
filename: 'stale.pdf',
|
|
fileUrl: 'https://example.com/stale.pdf',
|
|
fileSize: 1,
|
|
mimeType: 'text/plain',
|
|
})
|
|
).rejects.toThrow('Knowledge document secret provenance is unavailable')
|
|
|
|
expect(mockProcessDocument).not.toHaveBeenCalled()
|
|
expect(mockGenerateEmbeddings).not.toHaveBeenCalled()
|
|
})
|
|
})
|