gitgram/aitimemachine/a2a/test/server/mocks/agent-executor.mock.ts
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

175 lines
4.5 KiB
TypeScript

import { vi, type Mock, type MockInstance } from 'vitest';
import { AgentExecutor } from '../../../src/server/agent_execution/agent_executor.js';
import { RequestContext, ExecutionEventBus } from '../../../src/server/index.js';
/**
* A mock implementation of AgentExecutor to control agent behavior during tests.
*/
export class MockAgentExecutor implements AgentExecutor {
// Stubs to control and inspect calls to execute and cancelTask
public execute: Mock<
(requestContext: RequestContext, eventBus: ExecutionEventBus) => Promise<void>
> = vi.fn();
public cancelTask: Mock<(taskId: string, eventBus: ExecutionEventBus) => Promise<void>> = vi.fn();
}
/**
* Fake implementation of the task execution events.
*/
export const fakeTaskExecute = async (ctx: RequestContext, bus: ExecutionEventBus) => {
const taskId = ctx.taskId;
const contextId = ctx.contextId;
// Publish task creation
bus.publish({
id: taskId,
contextId,
status: { state: 'submitted' },
kind: 'task',
});
// Publish working status
bus.publish({
taskId,
contextId,
kind: 'status-update',
status: { state: 'working' },
final: false,
});
// Publish completion
bus.publish({
taskId,
contextId,
kind: 'status-update',
status: { state: 'completed' },
final: true,
});
bus.finished();
};
/**
* A realistic mock of AgentExecutor for cancellation tests.
*/
export class CancellableMockAgentExecutor implements AgentExecutor {
private cancelledTasks = new Set<string>();
public cancelTaskSpy: MockInstance;
constructor() {
this.cancelTaskSpy = vi.spyOn(this as CancellableMockAgentExecutor, 'cancelTask');
}
public async execute(requestContext: RequestContext, eventBus: ExecutionEventBus): Promise<void> {
const taskId = requestContext.taskId;
const contextId = requestContext.contextId;
eventBus.publish({
id: taskId,
contextId,
status: { state: 'submitted' },
kind: 'task',
});
eventBus.publish({
taskId,
contextId,
kind: 'status-update',
status: { state: 'working' },
final: false,
});
// Simulate a long-running process
for (let i = 0; i < 5; i++) {
if (this.cancelledTasks.has(taskId)) {
eventBus.publish({
taskId,
contextId,
kind: 'status-update',
status: { state: 'canceled' },
final: true,
});
eventBus.finished();
return;
}
// Use fake timers to simulate work
await vi.advanceTimersByTimeAsync(100);
}
eventBus.publish({
taskId,
contextId,
kind: 'status-update',
status: { state: 'completed' },
final: true,
});
eventBus.finished();
}
public async cancelTask(taskId: string, _eventBus: ExecutionEventBus): Promise<void> {
this.cancelledTasks.add(taskId);
// The execute loop is responsible for publishing the final state
}
}
/**
* A realistic mock of AgentExecutor for failed cancellation tests.
*/
export class FailingCancellableMockAgentExecutor implements AgentExecutor {
private cancelledTasks = new Set<string>();
public cancelTaskSpy: MockInstance;
constructor() {
this.cancelTaskSpy = vi.spyOn(this as FailingCancellableMockAgentExecutor, 'cancelTask');
}
public async execute(requestContext: RequestContext, eventBus: ExecutionEventBus): Promise<void> {
const taskId = requestContext.taskId;
const contextId = requestContext.contextId;
eventBus.publish({
id: taskId,
contextId,
status: { state: 'submitted' },
kind: 'task',
});
eventBus.publish({
taskId,
contextId,
kind: 'status-update',
status: { state: 'working' },
final: false,
});
// Simulate a long-running process
for (let i = 0; i < 5; i++) {
if (this.cancelledTasks.has(taskId)) {
eventBus.publish({
taskId,
contextId,
kind: 'status-update',
status: { state: 'canceled' },
final: true,
});
eventBus.finished();
return;
}
// Use fake timers to simulate work
await vi.advanceTimersByTimeAsync(100);
}
eventBus.publish({
taskId,
contextId,
kind: 'status-update',
status: { state: 'completed' },
final: true,
});
eventBus.finished();
}
public async cancelTask(_taskId: string, _eventBus: ExecutionEventBus): Promise<void> {
// No operation: simulates the failure of task cancellation
}
}