Files
waggle-os/packages/team-server/tests/routes/capability-governance.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

192 lines
7.9 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { and, eq, sql } from 'drizzle-orm';
import { buildServer } from '../../src/index.js';
import {
messages,
teamCapabilityOverrides,
teamCapabilityRequests,
teamMembers,
teams,
users,
} from '../../src/db/schema.js';
const SLUG_PREFIX = 'cap-bind-';
const MISSING_ID = '00000000-0000-4000-8000-000000000000';
describe('Capability Governance Routes', () => {
let server: Awaited<ReturnType<typeof buildServer>>;
let attackerAdminId: string;
let victimOwnerId: string;
let attackerTeamId: string;
let victimTeamId: string;
beforeAll(async () => {
server = await buildServer();
await server.db.execute(sql`DELETE FROM messages WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM team_capability_requests WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM team_capability_overrides WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM team_capability_policies WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM team_members WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'}`);
await server.db.execute(sql`DELETE FROM users WHERE clerk_id LIKE 'cap_bind_%'`);
const [attackerAdmin, victimOwner] = await server.db.insert(users).values([
{
clerkId: 'cap_bind_attacker_admin',
displayName: 'Capability Attacker Admin',
email: 'cap-bind-attacker@test.invalid',
},
{
clerkId: 'cap_bind_victim_owner',
displayName: 'Capability Victim Owner',
email: 'cap-bind-victim@test.invalid',
},
]).returning();
attackerAdminId = attackerAdmin.id;
victimOwnerId = victimOwner.id;
const [attackerTeam, victimTeam] = await server.db.insert(teams).values([
{ name: 'Capability Attacker Team', slug: `${SLUG_PREFIX}attacker`, ownerId: attackerAdminId },
{ name: 'Capability Victim Team', slug: `${SLUG_PREFIX}victim`, ownerId: victimOwnerId },
]).returning();
attackerTeamId = attackerTeam.id;
victimTeamId = victimTeam.id;
await server.db.insert(teamMembers).values([
{ teamId: attackerTeamId, userId: attackerAdminId, role: 'admin' },
{ teamId: victimTeamId, userId: victimOwnerId, role: 'owner' },
]);
server._authHandler.fn = async function (request, reply) {
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 messages WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM team_capability_requests WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM team_capability_overrides WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM team_capability_policies WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM team_members WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'})`);
await server.db.execute(sql`DELETE FROM teams WHERE slug LIKE ${SLUG_PREFIX + '%'}`);
await server.db.execute(sql`DELETE FROM users WHERE clerk_id LIKE 'cap_bind_%'`);
await server.close();
});
it('does not let an authorized admin delete another team\'s override by ID', async () => {
const [victimOverride] = await server.db.insert(teamCapabilityOverrides).values({
teamId: victimTeamId,
capabilityName: 'victim-secret-tool',
capabilityType: 'mcp',
decision: 'blocked',
decidedBy: victimOwnerId,
}).returning();
const foreign = await server.inject({
method: 'DELETE',
url: `/api/teams/${SLUG_PREFIX}attacker/capability-overrides/${victimOverride.id}`,
headers: { 'x-test-user-id': attackerAdminId },
});
const missing = await server.inject({
method: 'DELETE',
url: `/api/teams/${SLUG_PREFIX}attacker/capability-overrides/${MISSING_ID}`,
headers: { 'x-test-user-id': attackerAdminId },
});
expect({ statusCode: foreign.statusCode, body: foreign.body }).toEqual({
statusCode: missing.statusCode,
body: missing.body,
});
const [stored] = await server.db.select().from(teamCapabilityOverrides).where(and(
eq(teamCapabilityOverrides.id, victimOverride.id),
eq(teamCapabilityOverrides.teamId, victimTeamId),
));
expect(stored).toBeDefined();
});
it('still lets a same-team admin delete an override', async () => {
const [ownOverride] = await server.db.insert(teamCapabilityOverrides).values({
teamId: attackerTeamId,
capabilityName: 'attacker-team-tool',
capabilityType: 'mcp',
decision: 'blocked',
decidedBy: attackerAdminId,
}).returning();
const response = await server.inject({
method: 'DELETE',
url: `/api/teams/${SLUG_PREFIX}attacker/capability-overrides/${ownOverride.id}`,
headers: { 'x-test-user-id': attackerAdminId },
});
expect(response.statusCode).toBe(204);
const [stored] = await server.db.select().from(teamCapabilityOverrides).where(eq(teamCapabilityOverrides.id, ownOverride.id));
expect(stored).toBeUndefined();
});
it('does not let an authorized admin decide another team\'s request by ID', async () => {
const [victimRequest] = await server.db.insert(teamCapabilityRequests).values({
teamId: victimTeamId,
requestedBy: victimOwnerId,
capabilityName: 'victim-requested-tool',
capabilityType: 'mcp',
justification: 'Victim-only request',
}).returning();
const foreign = await server.inject({
method: 'PATCH',
url: `/api/teams/${SLUG_PREFIX}attacker/capability-requests/${victimRequest.id}`,
headers: { 'x-test-user-id': attackerAdminId },
payload: { status: 'rejected', reason: 'cross-team decision attempt' },
});
const missing = await server.inject({
method: 'PATCH',
url: `/api/teams/${SLUG_PREFIX}attacker/capability-requests/${MISSING_ID}`,
headers: { 'x-test-user-id': attackerAdminId },
payload: { status: 'rejected', reason: 'missing request' },
});
expect({ statusCode: foreign.statusCode, body: foreign.body }).toEqual({
statusCode: missing.statusCode,
body: missing.body,
});
const [stored] = await server.db.select().from(teamCapabilityRequests).where(and(
eq(teamCapabilityRequests.id, victimRequest.id),
eq(teamCapabilityRequests.teamId, victimTeamId),
));
expect(stored?.status).toBe('pending');
expect(stored?.decidedBy).toBeNull();
});
it('still lets a same-team admin decide a pending request', async () => {
const [ownRequest] = await server.db.insert(teamCapabilityRequests).values({
teamId: attackerTeamId,
requestedBy: attackerAdminId,
capabilityName: 'attacker-requested-tool',
capabilityType: 'mcp',
justification: 'Same-team request',
}).returning();
const response = await server.inject({
method: 'PATCH',
url: `/api/teams/${SLUG_PREFIX}attacker/capability-requests/${ownRequest.id}`,
headers: { 'x-test-user-id': attackerAdminId },
payload: { status: 'rejected', reason: 'same-team decision' },
});
expect(response.statusCode).toBe(200);
expect(response.json()).toMatchObject({
id: ownRequest.id,
teamId: attackerTeamId,
status: 'rejected',
decidedBy: attackerAdminId,
});
});
});