Skip to content

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

TypeScript
11.5 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest'
import express from 'express'
import request from 'supertest'
import type { K8sGateway } from '../src/k8s.js'
import { createAdminRouter as createAuthenticatedAdminRouter } from '../src/routes/admin/index.js'

/**
 * HTTP-level tests for the admin agents router.
 *
 * Covers PUT /admin/agents/:agentName/users|teams, the endpoints that grant
 * explicit user/team access to an agent so it is invocable via the rpc-access
 * authorization gate.
 *
 * The endpoints wrap setAgentUsers/setAgentTeams service functions and
 * delegate to bulkSetLinkedItems (destructive semantics: replace the full
 * set). These tests verify the HTTP contract at the router layer: payload
 * parsing, normalization, dedup, error propagation, and response shape.
 */

const svc = vi.hoisted(() => ({
  // Minimal surface — only what createAdminAgentsRouter + createAdminRouter call.
  acceptInvitation: vi.fn(),
  acceptInvitationForEmail: vi.fn(),
  adminDeleteTeam: vi.fn(),
  adminDeleteUser: vi.fn(),
  addMemberToTeam: vi.fn(),
  createAdminUser: vi.fn(),
  createInvitation: vi.fn(),
  createTeam: vi.fn(),
  createTeamForUser: vi.fn(),
  findMemberRole: vi.fn(),
  findMembership: vi.fn(),
  getAdminUserContext: vi.fn(),
  getCurrentTeam: vi.fn(),
  getMe: vi.fn(),
  getTeamAgents: vi.fn(),
  getTeamById: vi.fn(),
  getTeamContexts: vi.fn(),
  getUserAgents: vi.fn(),
  getUserContexts: vi.fn(),
  googleLoginData: vi.fn(),
  listAllTeams: vi.fn(),
  listMembers: vi.fn(),
  listPendingInvitations: vi.fn(),
  listTeamsByAgent: vi.fn(),
  listTeamsByContext: vi.fn(),
  listUsers: vi.fn(),
  listUsersByAgent: vi.fn(),
  listUsersByContext: vi.fn(),
  listTeams: vi.fn(),
  renameTeam: vi.fn(),
  searchDirectory: vi.fn(),
  setAgentTeams: vi.fn(),
  setAgentUsers: vi.fn(),
  setTeamAgents: vi.fn(),
  setTeamContexts: vi.fn(),
  setUserAgents: vi.fn(),
  setUserContexts: vi.fn(),
  softDeleteMember: vi.fn(),
  updateAdminUserContext: vi.fn(),
  updateMemberRole: vi.fn(),
  updateProfile: vi.fn(),
  verifyUserPassword: vi.fn(),
}))

vi.mock('../src/services/directory/index.js', () => svc)

const TEST_ADMIN_SUB = '11111111-1111-4111-8111-111111111111'

function createAdminRouter(gateway: K8sGateway) {
  const router = express.Router()
  router.use((req, _res, next) => {
    ;(req as unknown as { adminAuth: { sub: string } }).adminAuth = { sub: TEST_ADMIN_SUB }
    next()
  })
  router.use(createAuthenticatedAdminRouter(gateway))
  return router
}

function buildApp() {
  const app = express()
  app.use(express.json())
  const gatewayStub = {
    listResource: vi.fn(async () => []),
    getResource: vi.fn(async () => ({}) as never),
    createResource: vi.fn(async (body: unknown) => body),
    updateResource: vi.fn(async (body: unknown) => body),
    deleteResource: vi.fn(async () => ({ ok: true })),
    getHostOverview: vi.fn(async () => ({})),
  }
  app.use(createAdminRouter(gatewayStub as unknown as K8sGateway))
  return app
}

describe('routes/admin/agents — GET endpoints (regression coverage)', () => {
  beforeEach(() => {
    Object.values(svc).forEach(fn => {
      if (typeof fn === 'function' && 'mockReset' in fn) {
        ;(fn as ReturnType<typeof vi.fn>).mockReset()
      }
    })
  })

  it('GET /admin/agents/:agentName/users returns listUsersByAgent result wrapped in items', async () => {
    svc.listUsersByAgent.mockResolvedValue([
      { id: 'u1', email: '[email protected]', name: 'Alice', displayName: null },
      { id: 'u2', email: '[email protected]', name: null, displayName: 'Bob' },
    ])
    const app = buildApp()
    const res = await request(app).get('/admin/agents/chatllm/users').expect(200)
    expect(svc.listUsersByAgent).toHaveBeenCalledExactlyOnceWith('chatllm')
    expect(res.body).toEqual({
      items: [
        { id: 'u1', email: '[email protected]', name: 'Alice', displayName: null },
        { id: 'u2', email: '[email protected]', name: null, displayName: 'Bob' },
      ],
    })
  })

  it('GET /admin/agents/:agentName/teams returns listTeamsByAgent result wrapped in items', async () => {
    svc.listTeamsByAgent.mockResolvedValue([{ id: 't1', name: 'Engineering' }])
    const app = buildApp()
    const res = await request(app).get('/admin/agents/product/teams').expect(200)
    expect(svc.listTeamsByAgent).toHaveBeenCalledExactlyOnceWith('product')
    expect(res.body).toEqual({ items: [{ id: 't1', name: 'Engineering' }] })
  })

  it('GET /admin/agents/:agentName/users returns empty items when service returns empty array', async () => {
    svc.listUsersByAgent.mockResolvedValue([])
    const app = buildApp()
    const res = await request(app).get('/admin/agents/allinone/users').expect(200)
    expect(res.body.items).toEqual([])
  })
})

describe('routes/admin/agents — PUT /admin/agents/:agentName/users', () => {
  beforeEach(() => {
    Object.values(svc).forEach(fn => {
      if (typeof fn === 'function' && 'mockReset' in fn) {
        ;(fn as ReturnType<typeof vi.fn>).mockReset()
      }
    })
  })

  it('calls setAgentUsers with the exact userIds from the body (happy path)', async () => {
    svc.setAgentUsers.mockResolvedValue({
      agentName: 'chatllm',
      userIds: ['u1', 'u2', 'u3'],
    })
    const app = buildApp()
    const res = await request(app)
      .put('/admin/agents/chatllm/users')
      .send({ userIds: ['u1', 'u2', 'u3'] })
      .expect(200)

    expect(svc.setAgentUsers).toHaveBeenCalledExactlyOnceWith(
      'chatllm',
      ['u1', 'u2', 'u3'],
      TEST_ADMIN_SUB
    )
    expect(res.body).toEqual({
      agentName: 'chatllm',
      userIds: ['u1', 'u2', 'u3'],
    })
  })

  it('coerces non-string entries in userIds to strings via .map(String)', async () => {
    svc.setAgentUsers.mockResolvedValue({ agentName: 'x', userIds: ['42', 'null', 'true'] })
    const app = buildApp()
    await request(app)
      .put('/admin/agents/x/users')
      // Numbers and booleans should be coerced to strings before hitting the service
      .send({ userIds: [42, null, true] })
      .expect(200)

    expect(svc.setAgentUsers).toHaveBeenCalledWith('x', ['42', 'null', 'true'], TEST_ADMIN_SUB)
  })

  it('sends empty array to setAgentUsers when userIds is missing from body', async () => {
    svc.setAgentUsers.mockResolvedValue({ agentName: 'x', userIds: [] })
    const app = buildApp()
    await request(app).put('/admin/agents/x/users').send({}).expect(200)
    expect(svc.setAgentUsers).toHaveBeenCalledWith('x', [], TEST_ADMIN_SUB)
  })

  it('sends empty array to setAgentUsers when userIds is not an array', async () => {
    svc.setAgentUsers.mockResolvedValue({ agentName: 'x', userIds: [] })
    const app = buildApp()
    await request(app).put('/admin/agents/x/users').send({ userIds: 'not-an-array' }).expect(200)
    expect(svc.setAgentUsers).toHaveBeenCalledWith('x', [], TEST_ADMIN_SUB)
  })

  it('propagates service errors as 500 via next(error)', async () => {
    svc.setAgentUsers.mockRejectedValue(new Error('db connection lost'))
    const app = buildApp()
    // Default express error handler returns 500
    await request(app)
      .put('/admin/agents/chatllm/users')
      .send({ userIds: ['u1'] })
      .expect(500)
  })

  it('URL-decodes the agent name parameter (supports names with hyphens)', async () => {
    svc.setAgentUsers.mockResolvedValue({ agentName: 'multi-word-agent', userIds: ['u1'] })
    const app = buildApp()
    await request(app)
      .put('/admin/agents/multi-word-agent/users')
      .send({ userIds: ['u1'] })
      .expect(200)
    expect(svc.setAgentUsers).toHaveBeenCalledWith('multi-word-agent', ['u1'], TEST_ADMIN_SUB)
  })

  it('accepts an empty userIds array explicitly (reset-all intent)', async () => {
    svc.setAgentUsers.mockResolvedValue({ agentName: 'x', userIds: [] })
    const app = buildApp()
    await request(app).put('/admin/agents/x/users').send({ userIds: [] }).expect(200)
    expect(svc.setAgentUsers).toHaveBeenCalledWith('x', [], TEST_ADMIN_SUB)
  })
})

describe('routes/admin/agents — PUT /admin/agents/:agentName/teams', () => {
  beforeEach(() => {
    Object.values(svc).forEach(fn => {
      if (typeof fn === 'function' && 'mockReset' in fn) {
        ;(fn as ReturnType<typeof vi.fn>).mockReset()
      }
    })
  })

  it('calls setAgentTeams with the exact teamIds from the body (happy path)', async () => {
    svc.setAgentTeams.mockResolvedValue({
      agentName: 'chatllm',
      teamIds: ['t1', 't2'],
    })
    const app = buildApp()
    const res = await request(app)
      .put('/admin/agents/chatllm/teams')
      .send({ teamIds: ['t1', 't2'] })
      .expect(200)

    expect(svc.setAgentTeams).toHaveBeenCalledExactlyOnceWith(
      'chatllm',
      ['t1', 't2'],
      TEST_ADMIN_SUB
    )
    expect(res.body).toEqual({ agentName: 'chatllm', teamIds: ['t1', 't2'] })
  })

  it('is independent of PUT /users (does not call setAgentUsers)', async () => {
    svc.setAgentTeams.mockResolvedValue({ agentName: 'x', teamIds: ['t1'] })
    const app = buildApp()
    await request(app)
      .put('/admin/agents/x/teams')
      .send({ teamIds: ['t1'] })
      .expect(200)
    expect(svc.setAgentTeams).toHaveBeenCalled()
    expect(svc.setAgentUsers).not.toHaveBeenCalled()
  })

  it('propagates service errors as 500', async () => {
    svc.setAgentTeams.mockRejectedValue(new Error('deadlock'))
    const app = buildApp()
    await request(app)
      .put('/admin/agents/x/teams')
      .send({ teamIds: ['t1'] })
      .expect(500)
  })

  it('sends empty array when teamIds is missing from body', async () => {
    svc.setAgentTeams.mockResolvedValue({ agentName: 'x', teamIds: [] })
    const app = buildApp()
    await request(app).put('/admin/agents/x/teams').send({}).expect(200)
    expect(svc.setAgentTeams).toHaveBeenCalledWith('x', [], TEST_ADMIN_SUB)
  })
})

describe('routes/admin/agents — Integration invariants', () => {
  beforeEach(() => {
    Object.values(svc).forEach(fn => {
      if (typeof fn === 'function' && 'mockReset' in fn) {
        ;(fn as ReturnType<typeof vi.fn>).mockReset()
      }
    })
  })

  it('symmetric: PUT users then GET users should reflect the write (when the service layer is real)', async () => {
    // This documents the contract: after PUT, a subsequent GET should return the same set.
    // At the router layer we mock both sides — this test ensures the router calls the right services.
    svc.setAgentUsers.mockResolvedValue({ agentName: 'x', userIds: ['u1', 'u2'] })
    svc.listUsersByAgent.mockResolvedValue([
      { id: 'u1', email: '[email protected]', name: null, displayName: null },
      { id: 'u2', email: '[email protected]', name: null, displayName: null },
    ])
    const app = buildApp()
    await request(app)
      .put('/admin/agents/x/users')
      .send({ userIds: ['u1', 'u2'] })
      .expect(200)
    const getRes = await request(app).get('/admin/agents/x/users').expect(200)
    expect(getRes.body.items).toHaveLength(2)
    expect(svc.setAgentUsers).toHaveBeenCalledTimes(1)
    expect(svc.listUsersByAgent).toHaveBeenCalledTimes(1)
  })

  it('PUT /users and PUT /teams target different tables (no interference)', async () => {
    svc.setAgentUsers.mockResolvedValue({ agentName: 'x', userIds: ['u1'] })
    svc.setAgentTeams.mockResolvedValue({ agentName: 'x', teamIds: ['t1'] })
    const app = buildApp()
    await request(app)
      .put('/admin/agents/x/users')
      .send({ userIds: ['u1'] })
      .expect(200)
    await request(app)
      .put('/admin/agents/x/teams')
      .send({ teamIds: ['t1'] })
      .expect(200)
    expect(svc.setAgentUsers).toHaveBeenCalledExactlyOnceWith('x', ['u1'], TEST_ADMIN_SUB)
    expect(svc.setAgentTeams).toHaveBeenCalledExactlyOnceWith('x', ['t1'], TEST_ADMIN_SUB)
  })
})