/ src / lib / server / agents / agent-assignment.test.ts
agent-assignment.test.ts
  1  import { describe, it } from 'node:test'
  2  import assert from 'node:assert/strict'
  3  import type { Agent } from '@/types'
  4  import {
  5    isDelegationTaskPayload,
  6    resolveDelegatorAgentId,
  7    resolveManagedAgentAssignment,
  8    validateManagedAgentAssignment,
  9  } from '@/lib/server/agents/agent-assignment'
 10  
 11  const now = Date.now()
 12  const agents: Record<string, Agent> = {
 13    molly: {
 14      id: 'molly',
 15      name: 'Molly',
 16      description: '',
 17      systemPrompt: '',
 18      provider: 'openai',
 19      model: 'gpt-4o',
 20      createdAt: now,
 21      updatedAt: now,
 22    },
 23    writer: {
 24      id: 'writer',
 25      name: 'Writer',
 26      description: '',
 27      systemPrompt: '',
 28      provider: 'openai',
 29      model: 'gpt-4o',
 30      createdAt: now,
 31      updatedAt: now,
 32    },
 33  }
 34  
 35  describe('resolveManagedAgentAssignment', () => {
 36    it('resolves explicit aliases to concrete agent ids', () => {
 37      const resolved = resolveManagedAgentAssignment({ assignee: 'Writer' }, agents, 'molly')
 38      assert.equal(resolved.agentId, 'writer')
 39      assert.equal(resolved.source, 'explicit')
 40    })
 41  
 42    it('resolves description-based delegation before scope checks', () => {
 43      const resolved = resolveManagedAgentAssignment(
 44        { description: 'Please delegate this to @Writer and let them handle the draft.' },
 45        agents,
 46        'molly',
 47      )
 48      assert.equal(resolved.agentId, 'writer')
 49      assert.equal(resolved.source, 'description')
 50    })
 51  })
 52  
 53  describe('validateManagedAgentAssignment', () => {
 54    it('blocks assigning another agent when scope is self', () => {
 55      const resolved = resolveManagedAgentAssignment({ assignee: 'writer' }, agents, 'molly')
 56      const error = validateManagedAgentAssignment({
 57        resourceLabel: 'tasks',
 58        agents,
 59        assignScope: 'self',
 60        currentAgentId: 'molly',
 61        targetAgentId: resolved.agentId,
 62        unresolvedReference: resolved.unresolvedReference,
 63      })
 64      assert.match(error || '', /only assign tasks to yourself/i)
 65    })
 66  
 67    it('allows self-assignment in self scope', () => {
 68      const resolved = resolveManagedAgentAssignment({ agentId: 'molly' }, agents, 'molly')
 69      const error = validateManagedAgentAssignment({
 70        resourceLabel: 'tasks',
 71        agents,
 72        assignScope: 'self',
 73        currentAgentId: 'molly',
 74        targetAgentId: resolved.agentId,
 75        unresolvedReference: resolved.unresolvedReference,
 76      })
 77      assert.equal(error, null)
 78    })
 79  
 80    it('rejects unknown explicit agent references', () => {
 81      const resolved = resolveManagedAgentAssignment({ agentId: 'missing-agent' }, agents, 'molly')
 82      const error = validateManagedAgentAssignment({
 83        resourceLabel: 'tasks',
 84        agents,
 85        assignScope: 'all',
 86        currentAgentId: 'molly',
 87        targetAgentId: resolved.agentId,
 88        unresolvedReference: resolved.unresolvedReference,
 89      })
 90      assert.match(error || '', /unknown agent "missing-agent"/i)
 91    })
 92  
 93    it('rejects self-delegation using resolved agent ids', () => {
 94      const payload = {
 95        agentId: 'molly',
 96        sourceType: 'delegation',
 97        delegatedByAgentId: 'Molly',
 98      }
 99      const resolved = resolveManagedAgentAssignment(payload, agents, 'molly')
100      const error = validateManagedAgentAssignment({
101        resourceLabel: 'tasks',
102        agents,
103        assignScope: 'all',
104        currentAgentId: 'molly',
105        targetAgentId: resolved.agentId,
106        unresolvedReference: resolved.unresolvedReference,
107        isDelegation: isDelegationTaskPayload(payload),
108        delegatorAgentId: resolveDelegatorAgentId(payload, agents, 'molly'),
109      })
110      assert.match(error || '', /different agent id/i)
111    })
112  })