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

203 lines
7.7 KiB
TypeScript

/**
* @vitest-environment node
*/
import { dbChainMockFns, permissionsMock, permissionsMockFns, resetDbChainMock } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const {
mockApplyStorageUsageDeltasInTx,
mockEnsureUserStatsExists,
mockGetHighestPrioritySubscription,
mockFindActiveFolder,
mockMaybeNotifyStorageLimitForBillingContext,
mockResolveStorageBillingContext,
} = vi.hoisted(() => ({
mockApplyStorageUsageDeltasInTx: vi.fn(),
mockEnsureUserStatsExists: vi.fn(),
mockGetHighestPrioritySubscription: vi.fn(),
mockFindActiveFolder: vi.fn(),
mockMaybeNotifyStorageLimitForBillingContext: vi.fn(),
mockResolveStorageBillingContext: vi.fn(),
}))
vi.mock('@/lib/workspaces/permissions/utils', () => permissionsMock)
vi.mock('@/lib/folders/queries', () => ({
findActiveFolder: mockFindActiveFolder,
}))
vi.mock('@/lib/billing/storage', () => ({
applyStorageUsageDeltasInTx: mockApplyStorageUsageDeltasInTx,
maybeNotifyStorageLimitForBillingContext: mockMaybeNotifyStorageLimitForBillingContext,
resolveStorageBillingContext: mockResolveStorageBillingContext,
}))
vi.mock('@/lib/billing/core/subscription', () => ({
getHighestPrioritySubscription: mockGetHighestPrioritySubscription,
}))
vi.mock('@/lib/billing/core/usage', () => ({
ensureUserStatsExists: mockEnsureUserStatsExists,
}))
import {
createKnowledgeBase,
KnowledgeBaseFolderError,
updateKnowledgeBase,
} from '@/lib/knowledge/service'
const CREATE_INPUT = {
name: 'Base',
workspaceId: 'ws-1',
userId: 'u-1',
embeddingModel: 'text-embedding-3-small',
embeddingDimension: 1536 as const,
chunkingConfig: { maxSize: 1024, minSize: 100, overlap: 200 },
}
/**
* `knowledge_base.folder_id` has a plain FK to `folder.id`, which proves nothing about the
* workspace the folder belongs to or which resource tree it serves. These tests pin the
* application-level admission that stands in for the missing constraint.
*/
describe('createKnowledgeBase — folder assignment', () => {
beforeEach(() => {
vi.clearAllMocks()
dbChainMockFns.limit.mockReset()
resetDbChainMock()
dbChainMockFns.limit.mockResolvedValue([])
permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValue('admin')
mockFindActiveFolder.mockResolvedValue({ id: 'f-1' })
})
it('files the base under the requested folder', async () => {
const created = await createKnowledgeBase({ ...CREATE_INPUT, folderId: 'f-1' }, 'req-1')
expect(mockFindActiveFolder).toHaveBeenCalledWith('f-1', 'ws-1', 'knowledge_base')
expect(dbChainMockFns.values).toHaveBeenCalledWith(
expect.objectContaining({ folderId: 'f-1', workspaceId: 'ws-1' })
)
expect(created.folderId).toBe('f-1')
})
it('creates at the workspace root when no folder is given, without a folder lookup', async () => {
const created = await createKnowledgeBase(CREATE_INPUT, 'req-1')
expect(mockFindActiveFolder).not.toHaveBeenCalled()
expect(dbChainMockFns.values).toHaveBeenCalledWith(expect.objectContaining({ folderId: null }))
expect(created.folderId).toBeNull()
})
it('normalizes an explicit null folder to the workspace root', async () => {
const created = await createKnowledgeBase({ ...CREATE_INPUT, folderId: null }, 'req-1')
expect(mockFindActiveFolder).not.toHaveBeenCalled()
expect(created.folderId).toBeNull()
})
it('rejects a folder that is not an active knowledge_base folder in the workspace', async () => {
mockFindActiveFolder.mockResolvedValue(null)
await expect(
createKnowledgeBase({ ...CREATE_INPUT, folderId: 'f-other-workspace' }, 'req-1')
).rejects.toBeInstanceOf(KnowledgeBaseFolderError)
expect(dbChainMockFns.insert).not.toHaveBeenCalled()
})
it('checks permission before touching the folder', async () => {
permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValue('read')
await expect(
createKnowledgeBase({ ...CREATE_INPUT, folderId: 'f-1' }, 'req-1')
).rejects.toMatchObject({ code: 'KNOWLEDGE_BASE_FORBIDDEN' })
expect(mockFindActiveFolder).not.toHaveBeenCalled()
})
})
describe('updateKnowledgeBase — folder moves', () => {
/**
* The mocked `@sim/db` cannot satisfy the post-transaction read-back select, so a
* successful update still rejects after the transaction body commits.
*/
const runIgnoringReadBack = (promise: Promise<unknown>) => promise.catch(() => undefined)
beforeEach(() => {
vi.clearAllMocks()
dbChainMockFns.limit.mockReset()
resetDbChainMock()
dbChainMockFns.limit.mockResolvedValue([
{ workspaceId: 'ws-1', userId: 'u-1', folderId: 'f-old' },
])
permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValue('admin')
mockFindActiveFolder.mockResolvedValue({ id: 'f-1' })
mockResolveStorageBillingContext.mockImplementation(async (workspaceId: string) => ({
workspaceId,
billedAccountUserId: `${workspaceId}-owner`,
billingEntity: { type: 'user', id: `${workspaceId}-owner` },
plan: 'team_25000',
customStorageLimitGB: null,
}))
mockApplyStorageUsageDeltasInTx.mockResolvedValue(100)
mockEnsureUserStatsExists.mockResolvedValue(undefined)
mockGetHighestPrioritySubscription.mockResolvedValue(null)
})
it('writes the new folder against the current workspace', async () => {
await runIgnoringReadBack(updateKnowledgeBase('kb-1', { folderId: 'f-new' }, 'req-1'))
expect(mockFindActiveFolder).toHaveBeenCalledWith('f-new', 'ws-1', 'knowledge_base')
expect(dbChainMockFns.set).toHaveBeenCalledWith(expect.objectContaining({ folderId: 'f-new' }))
})
it('moves the base to the workspace root on an explicit null', async () => {
await runIgnoringReadBack(updateKnowledgeBase('kb-1', { folderId: null }, 'req-1'))
expect(mockFindActiveFolder).not.toHaveBeenCalled()
expect(dbChainMockFns.set).toHaveBeenCalledWith(expect.objectContaining({ folderId: null }))
})
it('rejects a folder outside the knowledge base workspace', async () => {
mockFindActiveFolder.mockResolvedValue(null)
await expect(
updateKnowledgeBase('kb-1', { folderId: 'f-foreign' }, 'req-1')
).rejects.toBeInstanceOf(KnowledgeBaseFolderError)
expect(dbChainMockFns.set).not.toHaveBeenCalled()
})
it('validates a folder move that accompanies a workspace change against the destination', async () => {
await runIgnoringReadBack(
updateKnowledgeBase('kb-1', { workspaceId: 'ws-2', folderId: 'f-dest' }, 'req-1', {
actorUserId: 'u-1',
})
)
expect(mockFindActiveFolder).toHaveBeenCalledWith('f-dest', 'ws-2', 'knowledge_base')
})
it('re-roots the base when its workspace changes and no folder is named', async () => {
await runIgnoringReadBack(
updateKnowledgeBase('kb-1', { workspaceId: 'ws-2' }, 'req-1', { actorUserId: 'u-1' })
)
expect(dbChainMockFns.set).toHaveBeenCalledWith(expect.objectContaining({ folderId: null }))
})
it('leaves the folder alone when the workspace is unchanged', async () => {
await runIgnoringReadBack(
updateKnowledgeBase('kb-1', { workspaceId: 'ws-1' }, 'req-1', { actorUserId: 'u-1' })
)
expect(dbChainMockFns.set).not.toHaveBeenCalledWith(expect.objectContaining({ folderId: null }))
})
it('leaves the folder alone on a plain rename', async () => {
dbChainMockFns.limit
.mockResolvedValueOnce([{ workspaceId: 'ws-1', userId: 'u-1', folderId: 'f-old' }]) // row lock
.mockResolvedValueOnce([]) // duplicate-name check: none
await runIgnoringReadBack(updateKnowledgeBase('kb-1', { name: 'Renamed' }, 'req-1'))
expect(mockFindActiveFolder).not.toHaveBeenCalled()
expect(dbChainMockFns.set).toHaveBeenCalledWith(
expect.not.objectContaining({ folderId: expect.anything() })
)
})
})