Files
waggle-os/app/tests/e2e/workspaces.test.ts
shumeiko.it 64580b4551 test: finish moving the root suites to the community edition (P1.5, P1.3b)
- The workspace isolation scenarios store memories through the memory
  store and check that one workspace's search never returns another's.
- The Ollama manifest test covers the Linux x64 and arm64 artifacts added
  for P1.3b; an unsupported platform still resolves to nothing.
- The root run leaves out the tests that only exercise excluded packages
  (the hook packages and the memory MCP server); they go with those
  packages in P2.2.
2026-10-24 17:53:16 +02:00

251 lines
9.7 KiB
TypeScript

/**
* E2E Tests: Workspaces, Memory Isolation, and Sessions
*
* Scenarios covered:
* 3. Workspace created via API
* 5. Workspace switching changes mind context
* 6. Memory search returns results from correct mind
* 8. Session management (create, list, delete)
*/
import { describe, it, expect, afterEach } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import type { FastifyInstance } from 'fastify';
import { startService } from '@waggle/server/local/service';
import { PERSONAL_SCOPE, workspaceScope } from '@waggle/memory-contract';
import { injectWithAuth } from './test-utils.js';
/** Texts of the memories `/api/memory/search` returns for a query. */
async function searchTexts(server: FastifyInstance, q: string, scope: string, workspace?: string): Promise<string[]> {
const params = new URLSearchParams({ q, scope, ...(workspace && { workspace }) });
const res = await injectWithAuth(server, { method: 'GET', url: `/api/memory/search?${params}` });
expect(res.statusCode).toBe(200);
return (JSON.parse(res.payload) as { results: Array<{ content: string }> }).results.map((r) => r.content);
}
function makeTmpDir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-e2e-'));
}
// Port 0 lets the OS assign a free port; inject() bypasses the network anyway
const TEST_PORT = 0;
describe('Workspaces & Sessions E2E', () => {
const servers: FastifyInstance[] = [];
const tmpDirs: string[] = [];
afterEach(async () => {
for (const s of servers) {
try { await s.close(); } catch { /* ignore */ }
}
servers.length = 0;
for (const d of tmpDirs) {
try { fs.rmSync(d, { recursive: true, force: true }); } catch { /* ignore */ }
}
tmpDirs.length = 0;
});
// Scenario 3: Create workspace via POST, verify GET returns it
it('creates a workspace and retrieves it', async () => {
const dataDir = makeTmpDir();
tmpDirs.push(dataDir);
const port = TEST_PORT;
const { server } = await startService({ dataDir, port, skipLiteLLM: true });
servers.push(server);
// Create workspace
const createRes = await injectWithAuth(server, {
method: 'POST',
url: '/api/workspaces',
payload: { name: 'Test Project', group: 'Work', icon: 'briefcase' },
});
expect(createRes.statusCode).toBe(201);
const created = JSON.parse(createRes.payload);
expect(created.name).toBe('Test Project');
expect(created.group).toBe('Work');
expect(created.id).toBeDefined();
// List workspaces — should contain the new one
const listRes = await injectWithAuth(server, { method: 'GET', url: '/api/workspaces' });
expect(listRes.statusCode).toBe(200);
const list = JSON.parse(listRes.payload);
expect(list).toBeInstanceOf(Array);
// Server now auto-creates a Default Workspace on boot via
// WorkspaceManager.ensureDefault(), so the list contains both
// the default and the one this test just created.
const testWs = list.find((w: { name: string }) => w.name === 'Test Project');
expect(testWs).toBeDefined();
// Get by ID
const getRes = await injectWithAuth(server, {
method: 'GET',
url: `/api/workspaces/${created.id}`,
});
expect(getRes.statusCode).toBe(200);
const fetched = JSON.parse(getRes.payload);
expect(fetched.id).toBe(created.id);
expect(fetched.icon).toBe('briefcase');
});
// Scenario 5: Workspace switching changes mind context
it('workspace switching isolates memory context', async () => {
const dataDir = makeTmpDir();
tmpDirs.push(dataDir);
const port = TEST_PORT;
const { server } = await startService({ dataDir, port, skipLiteLLM: true });
servers.push(server);
// Create two workspaces
const res1 = await injectWithAuth(server, {
method: 'POST',
url: '/api/workspaces',
payload: { name: 'Alpha', group: 'Work', model: 'openai/gpt-4o' },
});
const ws1 = JSON.parse(res1.payload);
const res2 = await injectWithAuth(server, {
method: 'POST',
url: '/api/workspaces',
payload: { name: 'Beta', group: 'Personal', model: 'anthropic/claude-sonnet-4-20250514' },
});
const ws2 = JSON.parse(res2.payload);
// Verify they are distinct
expect(ws1.id).not.toBe(ws2.id);
expect(ws1.group).toBe('Work');
expect(ws2.group).toBe('Personal');
// Workspace memory lives in the memory store, not in per-workspace files.
expect(fs.existsSync(path.join(dataDir, 'workspaces', ws1.id, 'workspace.mind'))).toBe(false);
await server.memory.remember({ scope: workspaceScope(ws1.id), text: 'Alpha project uses Kubernetes for deployment' });
await server.memory.remember({ scope: workspaceScope(ws2.id), text: 'Beta project uses Docker Compose locally' });
// Each workspace finds its own memory and never the other's.
expect(await searchTexts(server, 'Kubernetes', 'workspace', ws1.id)).toContain('Alpha project uses Kubernetes for deployment');
expect(await searchTexts(server, 'Kubernetes', 'workspace', ws2.id)).not.toContain('Alpha project uses Kubernetes for deployment');
expect(await searchTexts(server, 'Docker', 'workspace', ws2.id)).toContain('Beta project uses Docker Compose locally');
expect(await searchTexts(server, 'Docker', 'workspace', ws1.id)).not.toContain('Beta project uses Docker Compose locally');
});
// Scenario 6: Memory search returns results from correct mind only
it('memory search returns results scoped to the correct mind', async () => {
const dataDir = makeTmpDir();
tmpDirs.push(dataDir);
const port = TEST_PORT;
const { server } = await startService({ dataDir, port, skipLiteLLM: true });
servers.push(server);
await server.memory.remember({ scope: PERSONAL_SCOPE, text: 'Waggle architecture uses Tauri with React frontend' });
const createRes = await injectWithAuth(server, {
method: 'POST',
url: '/api/workspaces',
payload: { name: 'Research', group: 'Study' },
});
const ws = JSON.parse(createRes.payload);
await server.memory.remember({ scope: workspaceScope(ws.id), text: 'GraphContext uses SHACL validation for knowledge graphs' });
expect(await searchTexts(server, 'Tauri', 'personal')).toContain('Waggle architecture uses Tauri with React frontend');
expect(await searchTexts(server, 'SHACL', 'workspace', ws.id)).toContain('GraphContext uses SHACL validation for knowledge graphs');
// Personal search never returns workspace-only memories.
expect(await searchTexts(server, 'SHACL', 'personal')).not.toContain('GraphContext uses SHACL validation for knowledge graphs');
// 'all' covers personal memory plus the named workspace.
const all = await searchTexts(server, 'architecture', 'all', ws.id);
expect(all.length).toBeGreaterThanOrEqual(1);
});
// Scenario 8: Session CRUD — create, list, rename (switch), delete
it('creates, lists, renames, and deletes sessions within a workspace', async () => {
const dataDir = makeTmpDir();
tmpDirs.push(dataDir);
const port = TEST_PORT;
const { server } = await startService({ dataDir, port, skipLiteLLM: true });
servers.push(server);
// Create workspace first
const wsRes = await injectWithAuth(server, {
method: 'POST',
url: '/api/workspaces',
payload: { name: 'Chat Workspace', group: 'Work' },
});
const ws = JSON.parse(wsRes.payload);
// Create a session
const createRes = await injectWithAuth(server, {
method: 'POST',
url: `/api/workspaces/${ws.id}/sessions`,
payload: { title: 'My First Chat' },
});
expect(createRes.statusCode).toBe(201);
const session = JSON.parse(createRes.payload);
expect(session.id).toBeDefined();
expect(session.title).toBe('My First Chat');
expect(session.messageCount).toBe(0);
// Create a second session
const createRes2 = await injectWithAuth(server, {
method: 'POST',
url: `/api/workspaces/${ws.id}/sessions`,
payload: { title: 'Debug Session' },
});
expect(createRes2.statusCode).toBe(201);
const session2 = JSON.parse(createRes2.payload);
// List sessions — should have 2
const listRes = await injectWithAuth(server, {
method: 'GET',
url: `/api/workspaces/${ws.id}/sessions`,
});
expect(listRes.statusCode).toBe(200);
const list = JSON.parse(listRes.payload);
expect(list.length).toBe(2);
// Rename (switch equivalent) — proves session is accessible and modifiable
const patchRes = await injectWithAuth(server, {
method: 'PATCH',
url: `/api/sessions/${session.id}?workspace=${ws.id}`,
payload: { title: 'Renamed Chat' },
});
expect(patchRes.statusCode).toBe(200);
const patched = JSON.parse(patchRes.payload);
expect(patched.title).toBe('Renamed Chat');
expect(patched.id).toBe(session.id);
// Verify rename persisted in list
const listRes3 = await injectWithAuth(server, {
method: 'GET',
url: `/api/workspaces/${ws.id}/sessions`,
});
const list3 = JSON.parse(listRes3.payload);
const renamed = list3.find((s: { id: string }) => s.id === session.id);
expect(renamed).toBeDefined();
expect(renamed.title).toBe('Renamed Chat');
// Delete first session
const delRes = await injectWithAuth(server, {
method: 'DELETE',
url: `/api/sessions/${session.id}?workspace=${ws.id}`,
});
expect(delRes.statusCode).toBe(200);
const delBody = JSON.parse(delRes.payload);
expect(delBody.deleted).toBe(true);
// List again — should have 1
const listRes2 = await injectWithAuth(server, {
method: 'GET',
url: `/api/workspaces/${ws.id}/sessions`,
});
const list2 = JSON.parse(listRes2.payload);
expect(list2.length).toBe(1);
expect(list2[0].id).toBe(session2.id);
});
});