Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
3.5 KB
Raw Blame
import { beforeEach, describe, expect, it } from 'vitest'
import { MessageQueue } from '../../queue/messageQueue'
import { TaskLifecycle } from '../taskLifecycle'

describe('Dual-write parity', () => {
  let lc: TaskLifecycle
  let mq: MessageQueue

  beforeEach(() => {
    lc = new TaskLifecycle()
    mq = new MessageQueue()
    mq.setLifecycle(lc)
  })

  it('enqueue creates matching pending record', () => {
    const task = mq.createInternalTask('test content')
    // Single-admission contract: mq.enqueue (admit) is the registration point.
    mq.enqueue(task)
    expect(lc.getStatus(task.id)).toBe('pending')
    expect(mq.getTask(task.id)?.status).toBe('pending')
  })

  it('dequeue writes processing to both', () => {
    const task = mq.createInternalTask('test')
    mq.enqueue(task)
    mq.dequeue()
    expect(lc.getStatus(task.id)).toBe('processing')
    expect(task.status).toBe('processing')
  })

  it('completeTask writes completed to both', () => {
    const task = mq.createInternalTask('test')
    mq.enqueue(task)
    mq.dequeue()
    mq.completeTask(task)
    expect(lc.getStatus(task.id)).toBe('completed')
    expect(task.status).toBe('completed')
  })

  it('failTask writes failed to both', () => {
    const task = mq.createInternalTask('test')
    mq.enqueue(task)
    mq.dequeue()
    mq.failTask(task, {
      code: 'LLM_API_CALL_FAILED',
      message: 'test error',
      retryable: false,
      provider: 'unknown',
    })
    expect(lc.getStatus(task.id)).toBe('failed')
    expect(task.status).toBe('failed')
    expect(lc.get(task.id)?.error?.code).toBe('LLM_API_CALL_FAILED')
  })

  it('transition(cancelled) writes cancelled to lifecycle record', () => {
    // B.3: cancelTask removed from queue; lifecycle is now single writer for cancel.
    // The TaskRecord is updated; task.status sync for pending tasks is handled in Phase E
    // (queue subscriber / readonly enforcement). Here we verify the lifecycle side.
    const task = mq.createInternalTask('test')
    mq.enqueue(task)
    const outcome = lc.transition(task.id, 'cancelled', 'user_requested')
    expect(outcome.kind).toBe('applied')
    expect(lc.getStatus(task.id)).toBe('cancelled')
  })
})

describe('Dual-write parity — taskExecutor cancel paths', () => {
  it('executor.abort() mirrors cancelled to TaskLifecycle', async () => {
    // Direct unit-level check using mocked executor deps
    const lc = new TaskLifecycle()
    const task = { id: 't-abort-1' } as any
    lc.register(task)
    lc.transition('t-abort-1', 'processing', 'dispatched')

    // Simulate what abort() does: set status + call lifecycle transition
    // (we're verifying the contract — the actual call is inside taskExecutor.abort)
    task.status = 'cancelled'
    lc.transition('t-abort-1', 'cancelled', 'user_requested')

    expect(lc.getStatus('t-abort-1')).toBe('cancelled')
    expect(task.status).toBe('cancelled')
  })

  it('resumeAfterApproval abort guard mirrors cancelled to TaskLifecycle', async () => {
    const lc = new TaskLifecycle()
    const task = { id: 't-resume-1' } as any
    lc.register(task)
    lc.transition('t-resume-1', 'processing', 'dispatched')
    lc.transition('t-resume-1', 'waiting_approval', 'natural')

    // Simulate resume-after-approval finding aborted signal
    task.status = 'cancelled'
    task.completedAt = new Date()
    lc.transition('t-resume-1', 'cancelled', 'user_requested')

    expect(lc.getStatus('t-resume-1')).toBe('cancelled')
    expect(task.status).toBe('cancelled')
  })
})