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
321 lines
9.7 KiB
TypeScript
321 lines
9.7 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { workspaceFiles } from '@sim/db/schema'
|
|
import { dbChainMockFns, queueTableRows, resetDbChainMock } from '@sim/testing'
|
|
import type { SQL } from 'drizzle-orm'
|
|
import { PgDialect } from 'drizzle-orm/pg-core'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import type { DbTransaction } from '@/lib/db/types'
|
|
|
|
vi.unmock('@sim/db/schema')
|
|
vi.unmock('drizzle-orm')
|
|
|
|
import {
|
|
ActiveFileMetadataKeyConflictError,
|
|
deleteFileMetadataByIdentity,
|
|
insertFileMetadata,
|
|
insertFileMetadataMany,
|
|
insertImmutableFileMetadata,
|
|
recordKnowledgeBaseFileOwnership,
|
|
} from '@/lib/uploads/server/metadata'
|
|
|
|
describe('recordKnowledgeBaseFileOwnership', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetDbChainMock()
|
|
})
|
|
|
|
it('uses the supplied transaction executor for the immutable ownership binding', async () => {
|
|
const ownership = {
|
|
key: 'kb/fork-document-1',
|
|
userId: 'user-1',
|
|
workspaceId: 'workspace-1',
|
|
originalName: 'document.pdf',
|
|
contentType: 'application/pdf',
|
|
size: 321,
|
|
}
|
|
const returning = vi.fn().mockResolvedValue([{ id: 'file-1', ...ownership }])
|
|
const select = vi.fn()
|
|
const onConflictDoNothing = vi.fn(() => ({ returning }))
|
|
const insert = vi.fn(() => ({
|
|
values: vi.fn(() => ({ onConflictDoNothing })),
|
|
}))
|
|
const executor = { select, insert } as unknown as DbTransaction
|
|
|
|
await expect(recordKnowledgeBaseFileOwnership(ownership, executor)).resolves.toBeUndefined()
|
|
|
|
expect(onConflictDoNothing).toHaveBeenCalledTimes(1)
|
|
expect(select).not.toHaveBeenCalled()
|
|
expect(insert).toHaveBeenCalledTimes(1)
|
|
expect(dbChainMockFns.select).not.toHaveBeenCalled()
|
|
expect(dbChainMockFns.insert).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('retains the existing non-transactional retry behavior when no executor is supplied', async () => {
|
|
const ownership = {
|
|
key: 'kb/manual-document.pdf',
|
|
userId: 'user-1',
|
|
workspaceId: 'workspace-1',
|
|
originalName: 'document.pdf',
|
|
contentType: 'application/pdf',
|
|
size: 321,
|
|
}
|
|
dbChainMockFns.limit.mockResolvedValueOnce([
|
|
{
|
|
id: 'file-1',
|
|
...ownership,
|
|
folderId: null,
|
|
context: 'knowledge-base',
|
|
deletedAt: null,
|
|
},
|
|
])
|
|
|
|
await expect(recordKnowledgeBaseFileOwnership(ownership)).resolves.toBeUndefined()
|
|
|
|
expect(dbChainMockFns.select).toHaveBeenCalledTimes(1)
|
|
expect(dbChainMockFns.insert).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('validates an exact active binding after a conflict without aborting the executor', async () => {
|
|
const ownership = {
|
|
key: 'kb/fork-document-1',
|
|
userId: 'user-1',
|
|
workspaceId: 'workspace-1',
|
|
originalName: 'document.pdf',
|
|
contentType: 'application/pdf',
|
|
size: 321,
|
|
}
|
|
const active = {
|
|
id: 'file-1',
|
|
...ownership,
|
|
folderId: null,
|
|
context: 'knowledge-base',
|
|
deletedAt: null,
|
|
}
|
|
const limit = vi.fn().mockResolvedValue([active])
|
|
const select = vi.fn(() => ({
|
|
from: vi.fn(() => ({ where: vi.fn(() => ({ limit })) })),
|
|
}))
|
|
const returning = vi.fn().mockResolvedValue([])
|
|
const insert = vi.fn(() => ({
|
|
values: vi.fn(() => ({ onConflictDoNothing: vi.fn(() => ({ returning })) })),
|
|
}))
|
|
const executor = { select, insert } as unknown as DbTransaction
|
|
|
|
await expect(recordKnowledgeBaseFileOwnership(ownership, executor)).resolves.toBeUndefined()
|
|
|
|
expect(insert).toHaveBeenCalledTimes(1)
|
|
expect(select).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|
|
|
|
describe('deleteFileMetadataByIdentity', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetDbChainMock()
|
|
})
|
|
|
|
it('reports whether the exact active file version was soft-deleted', async () => {
|
|
const identity = {
|
|
id: 'file-1',
|
|
key: 'kb/workspace-1/file.pdf',
|
|
context: 'knowledge-base' as const,
|
|
contentUpdatedAt: new Date('2026-08-05T00:00:00.000Z'),
|
|
}
|
|
dbChainMockFns.returning.mockResolvedValueOnce([{ id: identity.id }])
|
|
|
|
await expect(deleteFileMetadataByIdentity(identity)).resolves.toBe(true)
|
|
const predicate = dbChainMockFns.where.mock.calls[0]?.[0] as SQL
|
|
const query = new PgDialect().sqlToQuery(predicate)
|
|
expect(query.sql).toContain(
|
|
`date_trunc('milliseconds', "workspace_files"."content_updated_at")`
|
|
)
|
|
expect(query.params).toContain(identity.contentUpdatedAt)
|
|
|
|
dbChainMockFns.returning.mockResolvedValueOnce([])
|
|
await expect(deleteFileMetadataByIdentity(identity)).resolves.toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('insertFileMetadata content versions', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetDbChainMock()
|
|
})
|
|
|
|
it('advances the content version when a replacement upload restores a deleted row', async () => {
|
|
const deleted = {
|
|
id: 'file-1',
|
|
key: 'workspace/workspace-1/file.txt',
|
|
deletedAt: new Date('2026-08-03T00:00:00.000Z'),
|
|
contentUpdatedAt: new Date('2026-08-03T00:00:00.000Z'),
|
|
}
|
|
const restored = {
|
|
...deleted,
|
|
deletedAt: null,
|
|
contentUpdatedAt: new Date('2026-08-04T00:00:00.000Z'),
|
|
}
|
|
dbChainMockFns.limit.mockResolvedValueOnce([]).mockResolvedValueOnce([deleted])
|
|
dbChainMockFns.returning.mockResolvedValueOnce([restored])
|
|
|
|
await expect(
|
|
insertFileMetadata({
|
|
key: deleted.key,
|
|
userId: 'user-1',
|
|
workspaceId: 'workspace-1',
|
|
context: 'workspace',
|
|
originalName: 'file.txt',
|
|
contentType: 'text/plain',
|
|
size: 12,
|
|
})
|
|
).resolves.toEqual(restored)
|
|
|
|
expect(dbChainMockFns.set).toHaveBeenCalledWith(
|
|
expect.objectContaining({ contentUpdatedAt: expect.anything() })
|
|
)
|
|
})
|
|
|
|
it('retains legacy active-key reuse for deterministic replacement uploads', async () => {
|
|
const active = {
|
|
id: 'file-1',
|
|
key: 'workspace/workspace-1/file.txt',
|
|
userId: 'user-1',
|
|
workspaceId: 'workspace-1',
|
|
folderId: null,
|
|
context: 'workspace',
|
|
originalName: 'file.txt',
|
|
contentType: 'text/plain',
|
|
size: 12,
|
|
deletedAt: null,
|
|
}
|
|
dbChainMockFns.limit.mockResolvedValueOnce([active])
|
|
|
|
await expect(
|
|
insertFileMetadata({
|
|
key: active.key,
|
|
userId: 'user-2',
|
|
workspaceId: 'workspace-1',
|
|
context: 'workspace',
|
|
originalName: 'other.txt',
|
|
contentType: 'text/plain',
|
|
size: 12,
|
|
})
|
|
).resolves.toEqual(active)
|
|
|
|
expect(dbChainMockFns.insert).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('rejects active-key reuse for immutable upload metadata', async () => {
|
|
const active = {
|
|
id: 'file-1',
|
|
key: 'workspace/workspace-1/file.txt',
|
|
userId: 'user-1',
|
|
workspaceId: 'workspace-1',
|
|
folderId: null,
|
|
context: 'workspace',
|
|
originalName: 'file.txt',
|
|
contentType: 'text/plain',
|
|
size: 12,
|
|
deletedAt: null,
|
|
}
|
|
dbChainMockFns.limit.mockResolvedValueOnce([active])
|
|
|
|
await expect(
|
|
insertImmutableFileMetadata({
|
|
key: active.key,
|
|
userId: 'user-2',
|
|
workspaceId: 'workspace-1',
|
|
context: 'workspace',
|
|
originalName: 'other.txt',
|
|
contentType: 'text/plain',
|
|
size: 12,
|
|
})
|
|
).rejects.toBeInstanceOf(ActiveFileMetadataKeyConflictError)
|
|
|
|
expect(dbChainMockFns.insert).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('preserves exact-identity idempotence', async () => {
|
|
const active = {
|
|
id: 'file-1',
|
|
key: 'workspace/workspace-1/file.txt',
|
|
userId: 'user-1',
|
|
workspaceId: 'workspace-1',
|
|
folderId: null,
|
|
context: 'workspace',
|
|
originalName: 'file.txt',
|
|
contentType: 'text/plain',
|
|
size: 12,
|
|
deletedAt: null,
|
|
}
|
|
dbChainMockFns.limit.mockResolvedValueOnce([active])
|
|
|
|
await expect(
|
|
insertImmutableFileMetadata({
|
|
key: active.key,
|
|
userId: active.userId,
|
|
workspaceId: active.workspaceId,
|
|
context: active.context,
|
|
originalName: active.originalName,
|
|
contentType: active.contentType,
|
|
size: active.size,
|
|
})
|
|
).resolves.toEqual(active)
|
|
|
|
expect(dbChainMockFns.insert).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('insertFileMetadataMany active-key idempotence', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetDbChainMock()
|
|
})
|
|
|
|
const row = {
|
|
key: 'knowledge-base/workspace-1/document.pdf',
|
|
userId: 'user-1',
|
|
workspaceId: 'workspace-1',
|
|
folderId: null,
|
|
context: 'knowledge-base' as const,
|
|
originalName: 'document.pdf',
|
|
contentType: 'application/pdf',
|
|
size: 12,
|
|
}
|
|
|
|
it('accepts an exact retry after a concurrent insert', async () => {
|
|
dbChainMockFns.returning.mockResolvedValueOnce([])
|
|
queueTableRows(workspaceFiles, [{ id: 'file-1', ...row, deletedAt: null }])
|
|
|
|
await expect(insertFileMetadataMany([row])).resolves.toBeUndefined()
|
|
})
|
|
|
|
it('rejects a conflicting active row instead of silently adopting it', async () => {
|
|
dbChainMockFns.returning.mockResolvedValueOnce([])
|
|
queueTableRows(workspaceFiles, [
|
|
{ id: 'file-1', ...row, userId: 'different-user', deletedAt: null },
|
|
])
|
|
|
|
await expect(insertFileMetadataMany([row])).rejects.toBeInstanceOf(
|
|
ActiveFileMetadataKeyConflictError
|
|
)
|
|
})
|
|
|
|
it('deduplicates exact same-key rows before inserting', async () => {
|
|
dbChainMockFns.returning.mockResolvedValueOnce([{ id: 'file-1', ...row, deletedAt: null }])
|
|
|
|
await expect(insertFileMetadataMany([row, { ...row }])).resolves.toBeUndefined()
|
|
|
|
expect(dbChainMockFns.values).toHaveBeenCalledWith([expect.objectContaining({ key: row.key })])
|
|
})
|
|
|
|
it('rejects mismatched same-batch rows before writing either identity', async () => {
|
|
await expect(
|
|
insertFileMetadataMany([row, { ...row, userId: 'different-user' }])
|
|
).rejects.toBeInstanceOf(ActiveFileMetadataKeyConflictError)
|
|
|
|
expect(dbChainMockFns.insert).not.toHaveBeenCalled()
|
|
})
|
|
})
|