Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

4.6 KB
Raw Blame
import type { DocumentProcessingTask, LogicalDocument } from '../models'
import {
  documentDisplayStatus,
  taskCanCancel,
  taskCanRetry,
  taskIsActive,
  taskNeedsAttention,
  taskVersionIsAfter,
} from '../model'

const document = (overrides: Partial<LogicalDocument> = {}): LogicalDocument => ({
  active: {
    contentHash: 'hash',
    createdAt: '2026-07-20T10:00:00Z',
    documentAssetId: 'asset-1',
    documentAssetVersion: 1,
    documentId: 'document-1',
    knowledgeSpaceId: 'space-1',
    mimeType: 'text/plain',
    revision: 2,
    sizeBytes: 10,
    state: 'active',
  },
  activeRevision: 2,
  createdAt: '2026-07-20T10:00:00Z',
  enabled: true,
  id: 'document-1',
  knowledgeSpaceId: 'space-1',
  rowVersion: 1,
  status: 'ready',
  title: 'Document',
  updatedAt: '2026-07-20T10:00:00Z',
  userMetadata: {},
  ...overrides,
})

const task = (overrides: Partial<DocumentProcessingTask> = {}): DocumentProcessingTask => ({
  createdAt: '2026-07-20T10:00:00Z',
  documentId: 'document-1',
  documentRevision: 3,
  id: 'task-1',
  knowledgeSpaceId: 'space-1',
  operation: 'document_processing',
  progressPercent: 10,
  stage: 'queued',
  state: 'queued',
  taskKind: 'document',
  updatedAt: '2026-07-20T10:00:00Z',
  ...overrides,
})

describe('new Knowledge document model', () => {
  it('compares task versions without losing sub-millisecond precision', () => {
    expect(taskVersionIsAfter('2026-07-20T10:00:00.123789Z', '2026-07-20T10:00:00.123456Z')).toBe(
      true,
    )
    expect(taskVersionIsAfter('2026-07-20T10:00:00.000001Z', '2026-07-20T10:00:00Z')).toBe(true)
    expect(taskVersionIsAfter('2026-07-20T10:00:00Z', '2026-07-20T10:00:00.000001Z')).toBe(false)
  })

  it.each([
    ['dispatch_pending', 'queued'],
    ['queued', 'queued'],
    ['retry_wait', 'queued'],
    ['running', 'processing'],
    ['failed', 'failed'],
  ] as const)('maps a latest %s task to %s', (state, expected) => {
    expect(documentDisplayStatus(document(), task({ state }))).toBe(expected)
  })

  it('keeps a last ready revision available after a canceled re-index', () => {
    expect(documentDisplayStatus(document(), task({ state: 'canceled' }))).toBe('ready')
    expect(
      documentDisplayStatus(
        document({ active: null, activeRevision: undefined, status: 'pending' }),
        task({ state: 'canceled' }),
      ),
    ).toBe('failed')
  })

  it('does not let an older failed task override the active revision', () => {
    expect(documentDisplayStatus(document(), task({ documentRevision: 1, state: 'failed' }))).toBe(
      'ready',
    )
  })

  it('maps only document availability and deletion to the disabled state', () => {
    expect(documentDisplayStatus(document({ enabled: false }))).toBe('disabled')
    expect(documentDisplayStatus(document({ status: 'deleting' }))).toBe('disabled')
  })

  it('keeps task badge and actions aligned with the contract states', () => {
    expect(taskIsActive(task({ state: 'running' }))).toBe(true)
    expect(taskCanCancel(task({ canCancel: false, state: 'running' }))).toBe(false)
    expect(taskCanCancel(task({ canCancel: true, state: 'queued' }))).toBe(true)
    expect(taskNeedsAttention(task({ state: 'failed' }))).toBe(true)
    expect(taskCanRetry(task({ state: 'canceled' }))).toBe(true)
    expect(taskCanRetry(task({ canRetry: false, state: 'canceled' }))).toBe(false)
    expect(taskCanRetry(task({ canRetry: true, state: 'canceled' }))).toBe(true)
    expect(taskNeedsAttention(task({ state: 'canceled' }))).toBe(true)
    expect(taskNeedsAttention(task({ state: 'succeeded' }))).toBe(false)
    expect(taskCanRetry(task({ state: 'superseded' }))).toBe(false)
    expect(
      taskCanRetry(
        task({
          canRetry: true,
          failure: {
            action: 'configure_model',
            category: 'configuration',
            code: 'MODEL_SELECTION_NOT_FOUND',
            message: 'Select another model.',
            retryPolicy: 'after_configuration',
          },
          state: 'failed',
        }),
      ),
    ).toBe(false)
  })
})

describe('published document graph repair', () => {
  it.each(['pending', 'running', 'failed'] as const)(
    'keeps text ready while graph is %s',
    (state) => {
      const repair = task({
        state: 'succeeded',
        canCancel: false,
        canRetry: state === 'failed',
        semanticEnrichment: { state, nodes_completed: 0 },
      })
      expect(documentDisplayStatus(document(), repair)).toBe('ready')
      expect(taskNeedsAttention(repair)).toBe(true)
      expect(taskIsActive(repair)).toBe(false)
      expect(taskCanCancel(repair)).toBe(false)
      expect(taskCanRetry(repair)).toBe(state === 'failed')
    },
  )
})