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.

2.9 KB
Raw Blame
import type { ReactElement } from 'react'
import { act, create } from 'react-test-renderer'
import { afterEach, describe, expect, it } from 'vitest'
import { MediaHandleRegistry } from './media-handle-registry'
import { useMediaHandleRegistry } from './use-media-handle-registry'

const STAGED = { uri: 'file:///cache/a.png', mime: 'image/png', byteLength: 12 }

const mountedTrees: ReturnType<typeof create>[] = []

function createTracked(element: Parameters<typeof create>[0]): ReturnType<typeof create> {
  const tree = create(element)
  mountedTrees.push(tree)
  return tree
}

afterEach(() => {
  act(() => {
    for (const tree of mountedTrees.splice(0)) {
      tree.unmount()
    }
  })
})

function harness(): {
  render: (sessionId: string | null) => void
  unmount: () => void
  readonly discarded: string[]
  readonly seen: MediaHandleRegistry[]
} {
  const discarded: string[] = []
  const seen: MediaHandleRegistry[] = []

  function Probe({ sessionId }: { sessionId: string | null }): ReactElement | null {
    const registry = useMediaHandleRegistry({
      sessionId,
      discard: (uri) => discarded.push(uri)
    })
    if (seen.at(-1) !== registry) {
      seen.push(registry)
    }
    return null
  }

  let tree: ReturnType<typeof create> | null = null
  return {
    render(sessionId) {
      act(() => {
        if (tree === null) {
          tree = createTracked(<Probe sessionId={sessionId} />)
        } else {
          tree.update(<Probe sessionId={sessionId} />)
        }
      })
    },
    unmount() {
      act(() => {
        tree?.unmount()
        tree = null
      })
    },
    discarded,
    seen
  }
}

describe('the registry a mounted page session holds', () => {
  it('hands the same registry back across renders of one session', () => {
    const probe = harness()
    probe.render('session-a')
    probe.render('session-a')
    expect(probe.seen).toHaveLength(1)
  })

  it('releases every staged file when the page unmounts', () => {
    const probe = harness()
    probe.render('session-a')
    probe.seen[0]?.mint([STAGED, { ...STAGED, uri: 'file:///cache/b.png' }])
    expect(probe.discarded).toEqual([])
    probe.unmount()
    expect(probe.discarded).toEqual(['file:///cache/a.png', 'file:///cache/b.png'])
  })

  it('releases the old session’s files when a new session takes the mount', () => {
    // The retired document can no longer release its staged files; changing session must sweep them.
    const probe = harness()
    probe.render('session-a')
    probe.seen[0]?.mint([STAGED])
    probe.render('session-b')
    expect(probe.discarded).toEqual(['file:///cache/a.png'])
    expect(probe.seen).toHaveLength(2)
    expect(probe.seen[1]?.liveCount()).toBe(0)
  })

  it('gives a mount with no session a registry too, so a caller never holds null', () => {
    const probe = harness()
    probe.render(null)
    expect(probe.seen[0]).toBeInstanceOf(MediaHandleRegistry)
    probe.unmount()
    expect(probe.discarded).toEqual([])
  })
})