Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

3.6 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createConnectionLogStore } from './connection-log-buffer'
import type { ConnectionLogPersistence } from './connection-log-buffer'
import type { ConnectionLogEntry } from './types'

const entry = (id: number): ConnectionLogEntry => ({
  id: `${id}`,
  ts: id,
  level: 'info',
  message: `event ${id}`
})
const drain = () => vi.advanceTimersByTimeAsync(0)

beforeEach(() => {
  vi.useFakeTimers()
})

afterEach(() => {
  vi.clearAllTimers()
  vi.useRealTimers()
})

describe('connection log persistence revisions', () => {
  it('writes a synchronous burst once per host after hydration', async () => {
    const save = vi.fn<ConnectionLogPersistence['save']>(async () => {})
    const store = createConnectionLogStore(200, { load: async () => [], save })
    await Promise.all(['a', 'b'].map((host) => store.hydrate(host)))
    await drain()
    save.mockClear()
    for (let i = 0; i < 1000; i++) {
      store.append('a', entry(i))
      store.append('b', entry(i + 2000))
    }
    await drain()
    expect(save).toHaveBeenCalledTimes(2)
    expect(save).toHaveBeenCalledWith(
      'a',
      Array.from({ length: 200 }, (_, i) => entry(800 + i))
    )
    expect(save).toHaveBeenCalledWith(
      'b',
      Array.from({ length: 200 }, (_, i) => entry(2800 + i))
    )
  })

  it('shares one snapshot across startup appends waiting on hydration', async () => {
    let loaded!: (entries: ConnectionLogEntry[]) => void
    const save = vi.fn<ConnectionLogPersistence['save']>(async () => {})
    const store = createConnectionLogStore(200, {
      load: () =>
        new Promise((resolve) => {
          loaded = resolve
        }),
      save
    })
    for (let i = 1; i <= 100; i++) {
      store.append('a', entry(i))
    }
    loaded([entry(0)])
    await store.hydrate('a')
    await drain()
    expect(save).toHaveBeenCalledTimes(1)
    expect(save).toHaveBeenLastCalledWith(
      'a',
      Array.from({ length: 101 }, (_, i) => entry(i))
    )
  })

  it('replaces obsolete queued revisions while a save is delayed', async () => {
    const saved: string[][] = []
    let release!: () => void
    let delay = false
    const store = createConnectionLogStore(2, {
      load: async () => [],
      save: async (_host, entries) => {
        saved.push(entries.map((item) => item.id))
        if (delay) {
          delay = false
          await new Promise<void>((resolve) => {
            release = resolve
          })
        }
      }
    })
    await store.hydrate('a')
    await drain()
    saved.length = 0
    delay = true
    store.append('a', entry(1))
    await drain()
    store.append('a', entry(2))
    await drain()
    store.append('a', entry(3))
    await drain()
    expect(saved).toEqual([['1']])
    release()
    await drain()
    expect(saved).toEqual([['1'], ['2', '3']])
  })

  it('attempts a failed snapshot once and carries the entries into the next write', async () => {
    const save = vi.fn<ConnectionLogPersistence['save']>(async () => {})
    const store = createConnectionLogStore(200, { load: async () => [], save })
    await store.hydrate('a')
    await drain()
    save.mockClear()
    save.mockRejectedValueOnce(new Error('first'))
    store.append('a', entry(1))
    store.append('a', entry(2))
    store.append('a', entry(3))
    await drain()
    expect(save).toHaveBeenCalledTimes(1)
    expect(save.mock.calls[0]?.[1]).toEqual([entry(1), entry(2), entry(3)])
    store.append('a', entry(4))
    await drain()
    expect(save).toHaveBeenCalledTimes(2)
    expect(save).toHaveBeenLastCalledWith('a', store.get('a'))
  })
})