Files
waggle-os/packages/team-server/tests/daemons/scout.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

224 lines
8.9 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import type { FastifyRequest, FastifyReply } from 'fastify';
import { buildServer } from '../../src/index.js';
import { users, teams, teamMembers, teamResources, agents, scoutFindings } from '../../src/db/schema.js';
import { sql, eq } from 'drizzle-orm';
import { ScoutAgent } from '../../src/daemons/scout.js';
describe('Scout Agent (Task 3.18)', () => {
let server: Awaited<ReturnType<typeof buildServer>>;
let userId: string;
let secondUserId: string;
let teamId: string;
let scout: ScoutAgent;
beforeAll(async () => {
server = await buildServer();
// Clean up leftover test data
await server.db.execute(sql`DELETE FROM scout_findings WHERE user_id IN (SELECT id FROM users WHERE clerk_id LIKE 'scout_%')`);
await server.db.execute(sql`DELETE FROM agents WHERE user_id IN (SELECT id FROM users WHERE clerk_id LIKE 'scout_%')`);
await server.db.execute(sql`DELETE FROM team_resources WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM team_members WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_requests WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_overrides WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_policies WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM teams WHERE slug LIKE 'scouttest-%'`);
await server.db.execute(sql`DELETE FROM users WHERE clerk_id LIKE 'scout_%'`);
// Create test user
const [user] = await server.db.insert(users).values({
clerkId: 'scout_user1',
displayName: 'Scout User',
email: 'scout_user1@test.com',
}).returning();
userId = user.id;
const [secondUser] = await server.db.insert(users).values({
clerkId: 'scout_user2',
displayName: 'Scout User 2',
email: 'scout_user2@test.com',
}).returning();
secondUserId = secondUser.id;
// Create team
const [team] = await server.db.insert(teams).values({
name: 'Scout Test Team',
slug: 'scouttest-team',
ownerId: userId,
}).returning();
teamId = team.id;
// Add membership with interests
await server.db.insert(teamMembers).values({
teamId,
userId,
role: 'owner',
interests: ['python', 'data-science'],
});
// Create an agent with tools for the user
await server.db.insert(agents).values({
userId,
teamId,
name: 'TestAgent',
tools: ['web_search', 'code_review'],
});
// Create team resources
await server.db.insert(teamResources).values([
{
teamId,
resourceType: 'skill',
name: 'Python Data Analyzer',
description: 'A skill for analyzing data with Python',
config: {},
sharedBy: userId,
},
{
teamId,
resourceType: 'prompt_template',
name: 'Code Review Template',
description: 'Template for code reviews',
config: {},
sharedBy: userId,
},
]);
scout = new ScoutAgent(server.db);
// Override auth handler
server._authHandler.fn = async function (request: FastifyRequest, reply: FastifyReply) {
const testUserId = request.headers['x-test-user-id'] as string;
if (!testUserId) {
return reply.code(401).send({ error: 'Missing x-test-user-id header' });
}
request.userId = testUserId;
request.clerkId = 'test';
};
});
afterAll(async () => {
await server.db.execute(sql`DELETE FROM scout_findings WHERE user_id IN (SELECT id FROM users WHERE clerk_id LIKE 'scout_%')`);
await server.db.execute(sql`DELETE FROM agents WHERE user_id IN (SELECT id FROM users WHERE clerk_id LIKE 'scout_%')`);
await server.db.execute(sql`DELETE FROM team_resources WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM team_members WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_requests WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_overrides WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_policies WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'scouttest-%')`);
await server.db.execute(sql`DELETE FROM teams WHERE slug LIKE 'scouttest-%'`);
await server.db.execute(sql`DELETE FROM users WHERE clerk_id LIKE 'scout_%'`);
await server.close();
});
it('scan creates findings from team resources', async () => {
const findings = await scout.scan(userId, teamId);
expect(findings.length).toBeGreaterThanOrEqual(2);
expect(findings.every((f) => f.status === 'new')).toBe(true);
expect(findings.some((f) => f.title.includes('Python Data Analyzer'))).toBe(true);
expect(findings.some((f) => f.title.includes('Code Review Template'))).toBe(true);
});
it('relevance scoring boosts based on user interests', async () => {
// The Python Data Analyzer should be boosted because user has 'python' interest
const findings = await scout.listFindings(userId);
const pythonFinding = findings.find((f) => f.title.includes('Python'));
const templateFinding = findings.find((f) => f.title.includes('Code Review'));
expect(pythonFinding).toBeTruthy();
expect(templateFinding).toBeTruthy();
// Python finding should have higher score (base 0.5 + 0.3 interest boost)
expect(pythonFinding!.relevanceScore).toBeGreaterThan(templateFinding!.relevanceScore);
});
it('adopt updates status', async () => {
const findings = await scout.listFindings(userId);
const finding = findings[0];
const updated = await scout.adopt(finding.id, userId);
expect(updated).toBeTruthy();
expect(updated!.status).toBe('adopted');
});
it('dismiss updates status', async () => {
const findings = await scout.listFindings(userId);
const newFinding = findings.find((f) => f.status === 'new');
expect(newFinding).toBeTruthy();
const updated = await scout.dismiss(newFinding!.id, userId);
expect(updated).toBeTruthy();
expect(updated!.status).toBe('dismissed');
});
it('dismissed finding not resurfaced in future scans', async () => {
// Clear all non-dismissed findings
const allFindings = await scout.listFindings(userId);
const dismissed = allFindings.filter((f) => f.status === 'dismissed');
expect(dismissed.length).toBeGreaterThan(0);
// Run scan again — dismissed titles should not reappear as new
const newFindings = await scout.scan(userId, teamId);
const dismissedTitles = dismissed.map((f) => f.title);
const resurfaced = newFindings.filter((f) => dismissedTitles.includes(f.title));
expect(resurfaced.length).toBe(0);
});
it('GET /api/scout/findings returns findings for user', async () => {
const response = await server.inject({
method: 'GET',
url: '/api/scout/findings',
headers: { 'x-test-user-id': userId },
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(Array.isArray(body)).toBe(true);
expect(body.length).toBeGreaterThan(0);
});
it.each(['adopted', 'dismissed'] as const)(
'does not let one user mark another user\'s finding as %s',
async (status) => {
const [victimFinding] = await server.db.insert(scoutFindings).values({
userId: secondUserId,
teamId,
source: 'team',
category: 'practice',
title: `Victim finding ${status}`,
summary: 'belongs to another user',
relevanceScore: 0.8,
url: null,
status: 'new',
}).returning();
const response = await server.inject({
method: 'PATCH',
url: `/api/scout/findings/${victimFinding.id}`,
headers: { 'x-test-user-id': userId },
payload: { status },
});
expect(response.statusCode).toBe(404);
expect(response.json()).toEqual({ error: 'Finding not found' });
const [unchanged] = await server.db.select().from(scoutFindings)
.where(eq(scoutFindings.id, victimFinding.id));
expect(unchanged.status).toBe('new');
expect(unchanged.userId).toBe(secondUserId);
},
);
it('PATCH /api/scout/findings/:id rejects invalid status', async () => {
const findings = await scout.listFindings(userId);
const finding = findings[0];
const response = await server.inject({
method: 'PATCH',
url: `/api/scout/findings/${finding.id}`,
headers: { 'x-test-user-id': userId },
payload: { status: 'invalid' },
});
expect(response.statusCode).toBe(400);
});
});