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

211 lines
7.8 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { buildServer } from '../../src/index.js';
import { users, teams, teamMembers, tasks } from '../../src/db/schema.js';
import { sql } from 'drizzle-orm';
describe('Task Board API', () => {
let server: Awaited<ReturnType<typeof buildServer>>;
let ownerId: string;
let memberId: string;
let outsiderId: string;
let teamSlug: string;
let teamId: string;
let createdTaskId: string;
beforeAll(async () => {
server = await buildServer();
// Clean up any leftover test data
await server.db.execute(sql`DELETE FROM tasks WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM team_members WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_requests WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_overrides WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_policies WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM teams WHERE slug LIKE 'tstest-%'`);
await server.db.execute(sql`DELETE FROM users WHERE clerk_id LIKE 'tstest_%'`);
// Create test users
const [owner] = await server.db.insert(users).values({
clerkId: 'tstest_owner',
displayName: 'Task Owner',
email: 'tstest_owner@test.com',
}).returning();
ownerId = owner.id;
const [member] = await server.db.insert(users).values({
clerkId: 'tstest_member',
displayName: 'Task Member',
email: 'tstest_member@test.com',
}).returning();
memberId = member.id;
const [outsider] = await server.db.insert(users).values({
clerkId: 'tstest_outsider',
displayName: 'Task Outsider',
email: 'tstest_outsider@test.com',
}).returning();
outsiderId = outsider.id;
// Create a team with owner + member
const [team] = await server.db.insert(teams).values({
name: 'Task Test Team',
slug: 'tstest-tasks',
ownerId,
}).returning();
teamId = team.id;
teamSlug = team.slug;
await server.db.insert(teamMembers).values([
{ teamId, userId: ownerId, role: 'owner' },
{ teamId, userId: memberId, role: 'member' },
]);
// Override auth handler
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 tasks WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM team_members WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_requests WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_overrides WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_policies WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'tstest-%')`);
await server.db.execute(sql`DELETE FROM teams WHERE slug LIKE 'tstest-%'`);
await server.db.execute(sql`DELETE FROM users WHERE clerk_id LIKE 'tstest_%'`);
await server.close();
});
it('creates a task on team board', async () => {
const response = await server.inject({
method: 'POST',
url: `/api/teams/${teamSlug}/tasks`,
headers: { 'x-test-user-id': ownerId },
payload: { title: 'Research competitors', description: 'Analyze top 5 competitors', priority: 'high' },
});
expect(response.statusCode).toBe(201);
const body = JSON.parse(response.body);
expect(body.title).toBe('Research competitors');
expect(body.description).toBe('Analyze top 5 competitors');
expect(body.priority).toBe('high');
expect(body.status).toBe('open');
expect(body.createdBy).toBe(ownerId);
expect(body.teamId).toBe(teamId);
createdTaskId = body.id;
});
it('lists tasks on team board', async () => {
const response = await server.inject({
method: 'GET',
url: `/api/teams/${teamSlug}/tasks`,
headers: { 'x-test-user-id': memberId },
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(Array.isArray(body)).toBe(true);
expect(body.length).toBeGreaterThanOrEqual(1);
expect(body.some((t: { id: string }) => t.id === createdTaskId)).toBe(true);
});
it('lists tasks with status filter', async () => {
// Create a second task and mark it done
const createRes = await server.inject({
method: 'POST',
url: `/api/teams/${teamSlug}/tasks`,
headers: { 'x-test-user-id': ownerId },
payload: { title: 'Done task' },
});
const doneTask = JSON.parse(createRes.body);
await server.inject({
method: 'PATCH',
url: `/api/teams/${teamSlug}/tasks/${doneTask.id}`,
headers: { 'x-test-user-id': ownerId },
payload: { status: 'done' },
});
// Filter by status=open
const response = await server.inject({
method: 'GET',
url: `/api/teams/${teamSlug}/tasks?status=open`,
headers: { 'x-test-user-id': memberId },
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(body.every((t: { status: string }) => t.status === 'open')).toBe(true);
expect(body.some((t: { id: string }) => t.id === doneTask.id)).toBe(false);
});
it('claims a task — sets assignedTo and status to claimed', async () => {
const response = await server.inject({
method: 'POST',
url: `/api/teams/${teamSlug}/tasks/${createdTaskId}/claim`,
headers: { 'x-test-user-id': memberId },
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(body.assignedTo).toBe(memberId);
expect(body.status).toBe('claimed');
});
it('completes a task — status changes to done', async () => {
const response = await server.inject({
method: 'PATCH',
url: `/api/teams/${teamSlug}/tasks/${createdTaskId}`,
headers: { 'x-test-user-id': memberId },
payload: { status: 'done' },
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(body.status).toBe('done');
});
it('creates a subtask with parentTaskId', async () => {
const response = await server.inject({
method: 'POST',
url: `/api/teams/${teamSlug}/tasks`,
headers: { 'x-test-user-id': ownerId },
payload: { title: 'Sub-research item', parentTaskId: createdTaskId },
});
expect(response.statusCode).toBe(201);
const body = JSON.parse(response.body);
expect(body.parentTaskId).toBe(createdTaskId);
expect(body.priority).toBe('normal');
});
it('non-team-member gets 403', async () => {
const response = await server.inject({
method: 'GET',
url: `/api/teams/${teamSlug}/tasks`,
headers: { 'x-test-user-id': outsiderId },
});
expect(response.statusCode).toBe(403);
});
it('gets a single task by id', async () => {
const response = await server.inject({
method: 'GET',
url: `/api/teams/${teamSlug}/tasks/${createdTaskId}`,
headers: { 'x-test-user-id': ownerId },
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(body.id).toBe(createdTaskId);
expect(body.title).toBe('Research competitors');
});
});