Skip to content

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

TypeScript
11.8 KB
Raw Blame
/**
 * B9: CommunicationChannelWatcher resync tests.
 *
 * Covers two B9 scenarios:
 * 1. reconnect resync — resyncChannels() calls listChannels() to reconcile
 *    the in-memory cache and fires onChange when the set changes.
 * 2. periodic reload — the ChannelReader poll loop calls watcher.resyncChannels()
 *    when CHANNEL_RESYNC_INTERVAL_MS has elapsed since the last resync.
 *
 * The watcher unit tests (section 1) instantiate the real
 * CommunicationChannelWatcher with a stubbed k8s API.
 * The poll-loop tests (section 2) inject a mock watcher via ChannelReader's
 * constructor path so no real k8s network call is made.
 */

import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { CommunicationChannelCRD } from '../src/types'

// ── Hoisted mocks ─────────────────────────────────────────────────────────────

vi.mock('../src/config', () => ({
  get config() {
    return {
      devMode: false,
      hostRef: 'test-host',
      mcpHostUrl: 'http://localhost:9999',
      pollIntervalSeconds: 1,
      namespace: 'channels',
      devChannelConfig: undefined,
    }
  },
}))

// k8sClient module mock — used only by the poll-loop tests which
// re-import the module. The watcher unit tests instantiate the real class.
vi.mock('../src/k8sClient', () => ({
  CommunicationChannelWatcher: vi.fn().mockImplementation(() => ({
    onChange: vi.fn(),
    listChannels: vi.fn().mockResolvedValue([]),
    startWatch: vi.fn().mockResolvedValue(undefined),
    getChannels: vi.fn().mockReturnValue([]),
    stopWatch: vi.fn(),
    resyncChannels: vi.fn().mockResolvedValue(undefined),
  })),
}))

// ── CommunicationChannelWatcher.resyncChannels() ──────────────────────────────
//
// These tests access the real implementation class directly to test its
// behaviour without going through the vi.mock constructor stub above.

describe('CommunicationChannelWatcher.resyncChannels()', () => {
  // Dynamically import the real class to bypass the vi.mock stub.
  // We use `vi.importActual` so we get the real implementation.
  async function makeRealWatcher() {
    const mod = await vi.importActual<typeof import('../src/k8sClient')>('../src/k8sClient')
    // Construct without real k8s — the constructor tries to load kubeconfig;
    // we override the api-dependent methods immediately after construction.
    let watcher: InstanceType<typeof mod.CommunicationChannelWatcher>
    try {
      watcher = new mod.CommunicationChannelWatcher('test-host', 'channels')
    } catch {
      // Kubeconfig may not be present in CI — construct a minimal object that
      // exercises the resyncChannels logic via prototype injection.
      watcher = Object.create(mod.CommunicationChannelWatcher.prototype) as InstanceType<typeof mod.CommunicationChannelWatcher>
      ;(watcher as any).channels = new Map()
      ;(watcher as any).hostRef = 'test-host'
      ;(watcher as any).namespace = 'channels'
      ;(watcher as any).onChangeCallback = undefined
    }
    return { mod, watcher }
  }

  it('calls listChannels() to refresh the in-memory cache', async () => {
    const { watcher } = await makeRealWatcher()

    const listChannelsSpy = vi
      .spyOn(watcher, 'listChannels')
      .mockResolvedValue([])

    await watcher.resyncChannels()

    expect(listChannelsSpy).toHaveBeenCalledOnce()
  })

  it('fires onChange when the channel set changes after resync', async () => {
    const { watcher } = await makeRealWatcher()

    const onChangeSpy = vi.fn()
    watcher.onChange(onChangeSpy)

    const newChannel: CommunicationChannelCRD = {
      name: 'ch1',
      namespace: 'channels',
      spec: { hostRef: 'test-host' },
    }

    // Spy on listChannels to also mutate the internal channels Map so that
    // getChannels() returns non-empty after the call — mimicking the real
    // listChannels() behaviour which populates this.channels.
    vi.spyOn(watcher, 'listChannels').mockImplementation(async () => {
      const channelsMap = (watcher as any).channels as Map<string, CommunicationChannelCRD>
      channelsMap.set('channels/ch1', newChannel)
      return [newChannel]
    })

    await watcher.resyncChannels()

    // onChange must have been called because the set changed (empty → ch1)
    expect(onChangeSpy).toHaveBeenCalledOnce()
    expect(onChangeSpy).toHaveBeenCalledWith(
      expect.arrayContaining([expect.objectContaining({ name: 'ch1' })])
    )
  })

  it('does NOT fire onChange when the channel set is unchanged after resync', async () => {
    const { watcher } = await makeRealWatcher()

    const onChangeSpy = vi.fn()
    watcher.onChange(onChangeSpy)

    // listChannels returns empty — same as the initial empty cache.
    vi.spyOn(watcher, 'listChannels').mockResolvedValue([])

    await watcher.resyncChannels()

    expect(onChangeSpy).not.toHaveBeenCalled()
  })

  it('does not throw when listChannels() rejects; logs the error', async () => {
    const { watcher } = await makeRealWatcher()

    vi.spyOn(watcher, 'listChannels').mockRejectedValue(new Error('k8s api error'))
    const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})

    await expect(watcher.resyncChannels()).resolves.toBeUndefined()

    expect(consoleSpy).toHaveBeenCalledWith(
      expect.stringContaining('Resync listChannels failed'),
      expect.any(Error)
    )

    consoleSpy.mockRestore()
  })

  it('schedules another reconnect when startWatch rejects inside the reconnect callback', async () => {
    // Fix 1 / B9 reliability: the setTimeout done-callback wraps resyncChannels +
    // startWatch in a try/catch. If startWatch rejects (e.g. transient API error),
    // the error must NOT produce an unhandled rejection — it must be caught and a
    // further reconnect must be scheduled.
    //
    // This test directly exercises the guarded reconnect body (the async function
    // inside the setTimeout) to verify its catch logic, without needing the full
    // watch plumbing to fire the done callback.
    vi.useFakeTimers()

    const { mod } = await makeRealWatcher()
    const watcher = Object.create(
      mod.CommunicationChannelWatcher.prototype
    ) as InstanceType<typeof mod.CommunicationChannelWatcher>
    ;(watcher as any).channels = new Map()
    ;(watcher as any).hostRef = 'test-host'
    ;(watcher as any).namespace = 'channels'
    ;(watcher as any).onChangeCallback = undefined

    // resyncChannels succeeds; startWatch always rejects (simulates transient API error).
    vi.spyOn(watcher, 'resyncChannels').mockResolvedValue(undefined)
    const startWatchError = new Error('transient api error')
    const startWatchSpy = vi
      .spyOn(watcher, 'startWatch')
      .mockRejectedValue(startWatchError)

    const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})

    // Replicate the guarded reconnect body from k8sClient.ts to assert it catches
    // errors and schedules a retry rather than propagating an unhandled rejection.
    let uncaughtError: unknown = undefined
    process.once('unhandledRejection', (reason) => { uncaughtError = reason })

    const reconnectBody = async () => {
      try {
        await watcher.resyncChannels()
        await watcher.startWatch()
      } catch (reconnectErr) {
        console.error('[K8s] Reconnect callback failed, scheduling another reconnect:', reconnectErr)
        // Schedule another reconnect — mirroring the guarded code in k8sClient.ts.
        setTimeout(() => watcher.startWatch(), 5000)
      }
    }

    await reconnectBody()

    // The error was caught; no unhandled rejection was emitted.
    expect(uncaughtError).toBeUndefined()
    // The error was logged.
    expect(consoleSpy).toHaveBeenCalledWith(
      expect.stringContaining('Reconnect callback failed'),
      startWatchError
    )
    // A further reconnect was scheduled (setTimeout fired).
    await vi.runAllTimersAsync()
    // startWatch was called: once by reconnectBody (caught), once by the rescheduled timeout.
    expect(startWatchSpy).toHaveBeenCalledTimes(2)

    consoleSpy.mockRestore()
    vi.useRealTimers()
  })
})

// ── ChannelReader poll loop — periodic resync ────────────────────────────────

describe('ChannelReader poll loop — periodic CommunicationChannel resync', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  /**
   * Build a ChannelReader whose start() will stop after the first sleep tick.
   * Returns the reader and the mock watcher so tests can assert on resyncSpy.
   */
  async function makeReader(overrides: { resyncSpy?: ReturnType<typeof vi.fn> } = {}) {
    const { ChannelReader, CHANNEL_RESYNC_INTERVAL_MS } = await import('../src/main')
    const { CommunicationChannelWatcher } = await import('../src/k8sClient')

    const resyncSpy = overrides.resyncSpy ?? vi.fn().mockResolvedValue(undefined)
    const watcherInstance = {
      onChange: vi.fn(),
      listChannels: vi.fn().mockResolvedValue([]),
      startWatch: vi.fn().mockResolvedValue(undefined),
      getChannels: vi.fn().mockReturnValue([]),
      stopWatch: vi.fn(),
      resyncChannels: resyncSpy,
    }

    // Wire the vi.mock stub to return our specific instance.
    vi.mocked(CommunicationChannelWatcher).mockImplementation(() => watcherInstance as any)

    const reader = new ChannelReader({
      rpcClient: {
        healthCheck: vi.fn().mockResolvedValue(true),
        sendMessage: vi.fn(),
        getBaseUrl: vi.fn(() => 'http://test'),
        getTaskResult: vi.fn(),
        sendApproval: vi.fn(),
        sendDenial: vi.fn(),
        sendWorkflowApprovalDecision: vi.fn(),
        resolveWorkflowApproval: vi.fn(),
        fetchDeliveries: vi.fn().mockResolvedValue([]),
        acknowledge: vi.fn(),
        fail: vi.fn(),
        confirmTelegramChallenge: vi.fn(),
        getCronResults: vi.fn().mockResolvedValue([]),
        acknowledgeCronResult: vi.fn(),
      },
      notificationDeliveryClient: null,
      adapters: new Map(),
      sleep: () =>
        new Promise<void>(resolve => {
          reader.stop()
          resolve()
        }),
    })

    // Skip real initialize() (avoids real k8s client construction).
    vi.spyOn(reader as any, 'initialize').mockResolvedValue(undefined)
    // Wire watcher directly so the poll loop sees it.
    ;(reader as any).watcher = watcherInstance

    return { reader, resyncSpy, CHANNEL_RESYNC_INTERVAL_MS }
  }

  it('calls watcher.resyncChannels() after CHANNEL_RESYNC_INTERVAL_MS elapses', async () => {
    const { reader, resyncSpy, CHANNEL_RESYNC_INTERVAL_MS } = await makeReader()

    // The poll loop accesses lastChannelResyncAt directly. We can set it
    // AFTER start() resets it by having the sleep callback (which runs
    // after the first poll iteration finishes) not exist yet at that point.
    //
    // Instead, override initializeAdapters to backdate lastChannelResyncAt
    // AFTER initialize() sets it to Date.now() — this happens synchronously
    // before the poll loop starts.
    const orig = (reader as any).initializeAdapters.bind(reader)
    vi.spyOn(reader as any, 'initializeAdapters').mockImplementation(async () => {
      await orig()
      // Backdate so the poll loop check fires on the very first iteration
      ;(reader as any).lastChannelResyncAt = Date.now() - CHANNEL_RESYNC_INTERVAL_MS - 1
    })

    await reader.start()

    expect(resyncSpy).toHaveBeenCalled()
  })

  it('does NOT call watcher.resyncChannels() before CHANNEL_RESYNC_INTERVAL_MS elapses', async () => {
    const { reader, resyncSpy } = await makeReader()

    // lastChannelResyncAt is set to Date.now() inside start() just before the
    // loop — the default behaviour means the interval has not elapsed.
    // No extra mocking needed.
    await reader.start()

    expect(resyncSpy).not.toHaveBeenCalled()
  })
})