import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test' import { mockConfigModule } from './helpers/mock-config' mock.module('../config', () => mockConfigModule({ pollTimeout: 1 })) import { storeCreateEnvironment, storeGetWorkItem, storeReset, storeUpdateEnvironment, } from '../store' import { pollWork } from '../services/work-dispatch' import { EnvironmentRpcError, requestDirectoryListing, resetEnvironmentRpcsForTests, settleEnvironmentRpc, supportsDirectoryBrowse, } from '../services/environment-rpc' describe('environment RPC', () => { beforeEach(() => { storeReset() resetEnvironmentRpcsForTests() }) afterEach(() => { resetEnvironmentRpcsForTests() }) test('queues list_dir on the work channel and resolves on receipt', async () => { const env = storeCreateEnvironment({ secret: 's', directory: '/tmp', capabilities: { fs_browse: true }, }) const pending = requestDirectoryListing(env.id, '/tmp') const work = await pollWork(env.id, 0.5) expect(work?.data.type).toBe('env_rpc') if (work?.data.type !== 'env_rpc') throw new Error('unexpected work type') expect(work.data.method).toBe('list_dir') expect(work.data.params).toEqual({ path: '/tmp' }) expect(work.cwd).toBeUndefined() expect(work.spawn_mode).toBeUndefined() const settled = settleEnvironmentRpc(env.id, work.data.id, { path: '/tmp', parent: '/', entries: [ { name: 'b', path: '/tmp/b' }, { name: 'malformed' }, { name: 'a', path: '/tmp/a' }, ], truncated: false, }) expect(settled).toBe(true) const listing = await pending expect(listing.path).toBe('/tmp') expect(listing.parent).toBe('/') expect(listing.entries).toEqual([ { name: 'b', path: '/tmp/b' }, { name: 'a', path: '/tmp/a' }, ]) expect(storeGetWorkItem(work.id)?.state).toBe('completed') }) test('rejects when the machine did not advertise fs_browse', async () => { const env = storeCreateEnvironment({ secret: 's', capabilities: {} }) const err = await requestDirectoryListing(env.id, '/tmp').catch(e => e) expect(err).toBeInstanceOf(EnvironmentRpcError) expect(err.code).toBe('unsupported') }) test('rejects when the machine is offline', async () => { const env = storeCreateEnvironment({ secret: 's', capabilities: { fs_browse: true }, }) storeUpdateEnvironment(env.id, { status: 'disconnected' }) const err = await requestDirectoryListing(env.id, '/tmp').catch(e => e) expect(err).toBeInstanceOf(EnvironmentRpcError) expect(err.code).toBe('offline') }) test('times out and drops the queued work item', async () => { const env = storeCreateEnvironment({ secret: 's', capabilities: { fs_browse: true }, }) const pending = requestDirectoryListing(env.id, '/tmp', 20) const work = await pollWork(env.id, 0.5) expect(work?.data.type).toBe('env_rpc') const err = await pending.catch(e => e) expect(err).toBeInstanceOf(EnvironmentRpcError) expect(err.code).toBe('timeout') expect(storeGetWorkItem(work!.id)?.state).toBe('completed') }) test('drops receipts that belong to another environment', async () => { const env = storeCreateEnvironment({ secret: 's', capabilities: { fs_browse: true }, }) const other = storeCreateEnvironment({ secret: 's2', capabilities: { fs_browse: true }, }) const pending = requestDirectoryListing(env.id, '/tmp') const work = await pollWork(env.id, 0.5) if (work?.data.type !== 'env_rpc') throw new Error('unexpected work type') expect( settleEnvironmentRpc(other.id, work.data.id, { path: '/tmp', parent: '/', entries: [], truncated: false, }), ).toBe(false) expect( settleEnvironmentRpc(env.id, work.data.id, { path: '/tmp', parent: '/', entries: [], truncated: false, }), ).toBe(true) await pending }) test('supportsDirectoryBrowse reads the capability flag', () => { expect(supportsDirectoryBrowse({ fs_browse: true })).toBe(true) expect(supportsDirectoryBrowse({ fs_browse: false })).toBe(false) expect(supportsDirectoryBrowse(null)).toBe(false) expect(supportsDirectoryBrowse(undefined)).toBe(false) }) })