Files
waggle-os/packages/worker/tests/handlers/chat-handler.test.ts
shumeiko.it c25efe6c44 refactor(server): move the team runtime into a postponed package (P1.1)
Team mode stays in the repository but is not built or shipped (ADR-0003).

- The team server (index.ts, config, db, daemons, plugins, proactive,
  routes, scheduler, services, ws, drizzle migrations) and its tests move
  from @waggle/server to packages/team-server (@waggle/team-server,
  private). It keeps its own copies of the logger and the agent-group
  executor, so it does not import @waggle/server. @clerk/fastify, bullmq,
  ioredis, postgres, drizzle-orm, drizzle-kit, cron-parser and
  @waggle/waggle-dance leave @waggle/server; the launcher drops the same
  team-only dependencies.
- build:packages no longer builds waggle-dance or the worker: nothing in
  the community edition imports them, and the worker's project reference
  would otherwise build the team server. The worker now imports from
  team-server; the team Dockerfile builds both explicitly.
- The sidecar no longer starts the team server when DATABASE_URL is set,
  and loses the team routes (/api/team/*, /api/teams), the WaggleDance
  routes and bridge (/api/waggle-dance/*), the Team server binding and
  egress guard, the WebSocket team client, the TeamSync cache and pushes,
  team audit forwarding, team workspace registration and the team
  capability-policy lookup in chat. The in-process room signal bus stays:
  agent groups, fleet runs and external tool runs use it.
  middleware/audit.ts was dead and is deleted.
- @waggle/core loses TeamSync and the teamServer part of WaggleConfig. A
  teamServer key in config.json is ignored and kept on save, as P0.1 does
  for the tier keys. Workspaces lose teamServerUrl and teamUserId; teamId
  and teamRole stay because workspaces from a shared data directory still
  carry them and the viewer write guards read them. POST /api/workspaces
  ignores team fields.
- The sidecar bundle no longer marks the team packages as external.

The agent still references the TeamSync type in orchestrator.ts and
pattern-write-back.ts; those files are being rewritten in P1.5 and drop
it there.

Logs UF-030 and UF-031 (team sync and team policy defects found while
removing the code).
2026-10-24 12:17:41 +02:00

176 lines
5.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import type { Job } from 'bullmq';
import type { Db } from '../../../team-server/src/db/connection.js';
import type { JobData } from '../../src/job-processor.js';
/** Build a minimal Job<JobData> mock — only `.data` is exercised by handlers. */
function makeJob(data: JobData): Job<JobData> {
return { data } as unknown as Job<JobData>;
}
vi.mock('@waggle/agent', async (importOriginal) => {
const actual = await importOriginal<typeof import('@waggle/agent')>();
return {
...actual,
runAgentLoop: vi.fn(async () => ({
content: 'Agent response here',
toolsUsed: ['read_file'],
usage: { inputTokens: 100, outputTokens: 50 },
})),
};
});
import { chatHandler } from '../../src/handlers/chat-handler.js';
import { runAgentLoop } from '@waggle/agent';
describe('Chat Handler', () => {
const mockDb = {} as Db;
let dataDir: string;
beforeEach(() => {
vi.clearAllMocks();
dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-worker-chat-'));
vi.stubEnv('WAGGLE_DATA_DIR', dataDir);
});
afterEach(() => {
vi.unstubAllEnvs();
fs.rmSync(dataDir, { recursive: true, force: true });
});
it('calls runAgentLoop and returns response with correct shape', async () => {
const mockJob = makeJob({
jobId: 'j1',
teamId: 't1',
userId: 'u1',
jobType: 'chat',
input: { message: 'hello world' },
});
const result = await chatHandler(mockJob, mockDb);
expect(result.response).toBe('Agent response here');
expect(result.userId).toBe('u1');
expect(result.model).toBe('claude-sonnet');
expect(result.toolsUsed).toEqual(['read_file']);
expect(result.tokensUsed).toBe(150);
});
it('passes correct config to runAgentLoop', async () => {
const mockJob = makeJob({
jobId: 'j2',
teamId: 't1',
userId: 'u1',
jobType: 'chat',
input: { message: 'test message', model: 'gpt-4o' },
});
await chatHandler(mockJob, mockDb);
expect(runAgentLoop).toHaveBeenCalledOnce();
const callArgs = vi.mocked(runAgentLoop).mock.calls[0][0];
expect(callArgs.model).toBe('gpt-4o');
expect(callArgs.messages).toEqual([{ role: 'user', content: 'test message' }]);
expect(callArgs.systemPrompt).toContain('Waggle AI agent');
expect(callArgs.systemPrompt).toContain('read-only');
expect(callArgs.systemPrompt).toContain('cannot run shell commands, execute code, or write, edit, or delete files');
expect(callArgs.tools.map(tool => tool.name)).toEqual([
'read_file',
'search_files',
'search_content',
'web_search',
'web_fetch',
]);
});
it('ignores caller workspaceDir and roots tools in the team data directory', async () => {
const callerWorkspace = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-attacker-root-'));
const tenantRoot = path.join(dataDir, 'teams', 'team-1', 'files');
fs.mkdirSync(tenantRoot, { recursive: true });
fs.writeFileSync(path.join(tenantRoot, 'tenant.txt'), 'tenant-root-content');
fs.writeFileSync(path.join(callerWorkspace, 'tenant.txt'), 'caller-root-content');
const mockJob = makeJob({
jobId: 'j3',
teamId: 'team-1',
userId: 'u1',
jobType: 'chat',
input: { message: 'test', workspaceDir: callerWorkspace },
});
try {
await chatHandler(mockJob, mockDb);
const callArgs = vi.mocked(runAgentLoop).mock.calls[0][0];
const readFile = callArgs.tools.find(tool => tool.name === 'read_file');
expect(readFile).toBeDefined();
await expect(readFile!.execute({ path: 'tenant.txt' })).resolves.toContain('tenant-root-content');
} finally {
fs.rmSync(callerWorkspace, { recursive: true, force: true });
}
});
it('fails closed when WAGGLE_DATA_DIR is unavailable', async () => {
vi.stubEnv('WAGGLE_DATA_DIR', '');
const mockJob = makeJob({
jobId: 'j-no-root',
teamId: 'team-1',
userId: 'u1',
jobType: 'chat',
input: { message: 'test' },
});
await expect(chatHandler(mockJob, mockDb)).rejects.toThrow('WAGGLE_DATA_DIR');
expect(runAgentLoop).not.toHaveBeenCalled();
});
it('uses default model when not specified in input', async () => {
const mockJob = makeJob({
jobId: 'j4',
teamId: 't1',
userId: 'u1',
jobType: 'chat',
input: { message: 'hi' },
});
await chatHandler(mockJob, mockDb);
const callArgs = vi.mocked(runAgentLoop).mock.calls[0][0];
expect(callArgs.model).toBe('claude-sonnet');
});
it('handles missing message gracefully', async () => {
const mockJob = makeJob({
jobId: 'j5',
teamId: 't1',
userId: 'u1',
jobType: 'chat',
input: {},
});
const result = await chatHandler(mockJob, mockDb);
expect(result.response).toBe('Agent response here');
expect(result.userId).toBe('u1');
const callArgs = vi.mocked(runAgentLoop).mock.calls[0][0];
expect(callArgs.messages).toEqual([{ role: 'user', content: '' }]);
});
it('includes userId in response', async () => {
const mockJob = makeJob({
jobId: 'j6',
teamId: 't1',
userId: 'user-abc',
jobType: 'chat',
input: { message: 'test' },
});
const result = await chatHandler(mockJob, mockDb);
expect(result.userId).toBe('user-abc');
});
});