wishforge-ide/tests/workspace-lifecycle.test.ts
eust-w fe13f6c862 🔒 Hide a workspace's internal upstream when it is requested again
Requesting a project's workspace returned the existing record as
stored, including the internal Theia and collaboration upstreams that
the list leaves out. Both now show the same public fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 19:11:12 +08:00

261 lines
11 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import type { Actor, Workspace } from '@wishforge/contracts';
import { MemoryStore } from './helpers/memory-store.js';
import { ControlPlane, record } from '../apps/api/src/domain/control-plane.js';
import { WorkspaceLifecycle, WORKSPACE_IDLE_MS } from '../apps/api/src/domain/workspace-lifecycle.js';
import { ApiController, Services } from '../apps/api/src/http.js';
const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' };
async function fixture() {
const db = new MemoryStore();
db.seed('alpha');
const control = new ControlPlane(db);
const project = await control.createProject(owner, {
name: 'Lifecycle fixture',
repositoryUrl: 'https://example.test/project.git',
});
const workspace = {
...record('alpha'),
projectId: project.id,
status: 'ready',
managedBy: 'wishforge-provisioner',
upstreamUrl: 'http://ide.internal:3000',
lastActivityAt: new Date(Date.now() - WORKSPACE_IDLE_MS - 60000).toISOString(),
};
const environment = {
...record('alpha'),
projectId: project.id,
status: 'healthy',
type: 'managed_kubernetes',
currentReleaseId: 'production-stable',
};
await db.transaction('alpha', async tx => {
await tx.put('workspace', workspace);
await tx.put('environment', environment);
});
const activity = { workspaceId: workspace.id, gatewayConnections: 0, known: true };
let state = { workspaceId: workspace.id, revision: 0, desired: 'running', status: 'ready' };
let lose = false;
let unavailable = false;
const calls: Array<{ url: string; method: string }> = [];
const fetcher: typeof fetch = async (url, options) => {
const target = new URL(String(url));
const method = options?.method ?? 'GET';
calls.push({ url: target.pathname, method });
if (target.pathname === '/internal/lifecycle-state') return Response.json(activity);
if (unavailable) throw new Error('offline');
if (method === 'PUT') {
const input = JSON.parse(String(options?.body));
state = { workspaceId: workspace.id, revision: input.revision, desired: input.desired, status: 'pending' };
if (lose) throw new Error('lost response');
}
return Response.json(state);
};
const env = {
NODE_ENV: 'production',
WF_PROVISIONER_URL: 'https://provisioner.example',
WF_PROVISIONER_TOKEN: 'p'.repeat(32),
WF_PROVISIONER_TENANT_ID: 'alpha',
WF_WORKSPACE_GATEWAY_BASE: 'https://gateway.example',
WF_WORKSPACE_GATEWAY_TOKEN: 'g'.repeat(32),
WF_WORKSPACE_GATEWAY_TENANT_ID: 'alpha',
};
const lifecycle = new WorkspaceLifecycle(db, env, fetcher);
const get = async () => (await db.transaction('alpha', tx => tx.get<Workspace>('workspace', workspace.id)))!;
const items = () => db.transaction('alpha', tx => tx.outbox());
const age = () =>
db.transaction('alpha', async tx => {
const w = (await tx.get<Workspace>('workspace', workspace.id))!;
w.lifecycle!.requestedAt = new Date(Date.now() - 6000).toISOString();
await tx.put('workspace', w);
});
return {
db,
env,
workspace,
environment,
lifecycle,
calls,
activity,
get,
items,
age,
setUnavailable: () => {
unavailable = true;
},
lose: () => {
lose = true;
},
converge: () => {
state.status = state.desired === 'running' ? 'ready' : 'paused';
},
setStatus: async (status: Workspace['status']) => {
await db.transaction('alpha', async tx => {
const w = (await tx.get<Workspace>('workspace', workspace.id))!;
w.status = status;
await tx.put('workspace', w);
});
},
};
}
describe('durable workspace lifecycle without application deployment changes', () => {
it('uses the scoped tenant provisioner resolver and never falls back to another tenant', async () => {
const f = await fixture();
f.env.WF_PROVISIONER_TENANT_ID = 'beta';
await expect(f.lifecycle.manual(owner, f.workspace.id, 'pause', 'wrong-tenant-route')).rejects.toMatchObject({
code: 'WORKSPACE_LIFECYCLE_UNCONFIGURED',
});
await f.lifecycle.scheduleIdle('alpha');
expect(f.calls).toEqual([]);
expect(await f.items()).toEqual([]);
});
it('deduplicates pause and never reports paused before observation', async () => {
const f = await fixture();
const result = await f.lifecycle.manual(owner, f.workspace.id, 'pause', 'pause-key-one');
expect(result).toMatchObject({ status: 'pausing' });
await f.lifecycle.manual(owner, f.workspace.id, 'pause', 'pause-key-one');
expect(await f.items()).toHaveLength(1);
const [item] = await f.items();
await f.lifecycle.reconcile('alpha', item!);
expect((await f.get()).status).toBe('pausing');
f.converge();
await f.lifecycle.reconcile('alpha', item!);
expect((await f.get()).status).toBe('paused');
expect(await f.db.transaction('alpha', tx => tx.get('environment', f.environment.id))).toEqual(f.environment);
expect(await f.items()).toEqual([]);
});
it('remembers a no-op resume key so a later retry cannot resume a subsequently paused workspace', async () => {
const f = await fixture();
expect(await f.lifecycle.manual(owner, f.workspace.id, 'resume', 'noop-resume-key')).toMatchObject({
status: 'ready',
});
await f.setStatus('paused');
expect(await f.lifecycle.manual(owner, f.workspace.id, 'resume', 'noop-resume-key')).toMatchObject({
status: 'ready',
});
expect((await f.get()).status).toBe('paused');
expect(await f.items()).toEqual([]);
});
it('resumes via replicas one and waits for readiness', async () => {
const f = await fixture();
await f.setStatus('paused');
await f.lifecycle.manual(owner, f.workspace.id, 'resume', 'resume-key-one');
const [item] = await f.items();
await f.lifecycle.reconcile('alpha', item!);
expect((await f.get()).status).toBe('resuming');
f.converge();
await f.lifecycle.reconcile('alpha', item!);
expect((await f.get()).status).toBe('ready');
expect(Date.parse(String((await f.get()).lastActivityAt))).toBeGreaterThan(Date.now() - 1000);
});
it('a lost scale response is unknown, then observed without a second mutation', async () => {
const f = await fixture();
await f.lifecycle.manual(owner, f.workspace.id, 'pause', 'pause-key-lost');
f.lose();
const [item] = await f.items();
await f.lifecycle.reconcile('alpha', item!);
expect((await f.get()).status).toBe('unknown');
f.converge();
await f.lifecycle.reconcile('alpha', item!);
expect((await f.get()).status).toBe('paused');
expect(f.calls.filter(c => c.method === 'PUT')).toHaveLength(1);
});
it('unknown observation cannot result in a blind mutation or ready response', async () => {
const f = await fixture();
await f.lifecycle.manual(owner, f.workspace.id, 'pause', 'pause-key-offline');
f.setUnavailable();
await f.lifecycle.reconcile('alpha', (await f.items())[0]!);
expect((await f.get()).status).toBe('unknown');
expect(f.calls.filter(c => c.method === 'PUT')).toHaveLength(0);
});
it('enforces human edit membership before queuing pause', async () => {
const f = await fixture();
for (const actor of [
{ ...owner, kind: 'connector' as const },
{ ...owner, userId: 'viewer', role: 'viewer' as const },
{ ...owner, userId: 'absent', role: 'owner' as const },
])
await expect(f.lifecycle.manual(actor, f.workspace.id, 'pause', 'pause-key-acl')).rejects.toThrow();
expect((await f.get()).status).toBe('ready');
expect(await f.items()).toEqual([]);
});
it('queues automatic pause only after 30 minutes without gateway connections', async () => {
const f = await fixture();
await f.lifecycle.scheduleIdle('alpha');
expect((await f.get()).status).toBe('pausing');
expect((await f.get()).lifecycle).toMatchObject({ automatic: true });
await f.age();
await f.lifecycle.reconcile('alpha', (await f.items())[0]!);
expect(f.calls.filter(c => c.method === 'PUT')).toHaveLength(1);
});
it.each([{ gatewayConnections: 1 }, { known: false }])(
'does not automatically stop an active or unknown workspace (%o)',
async change => {
const f = await fixture();
Object.assign(f.activity, change);
await f.lifecycle.scheduleIdle('alpha');
expect((await f.get()).status).toBe('ready');
expect(await f.items()).toEqual([]);
},
);
it('recent authorization activity and unmanaged local workspaces are excluded', async () => {
const f = await fixture();
await f.db.transaction('alpha', async tx => {
const w = (await tx.get<Workspace>('workspace', f.workspace.id))!;
w.lastActivityAt = new Date().toISOString();
await tx.put('workspace', w);
});
await f.lifecycle.scheduleIdle('alpha');
expect(f.calls).toEqual([]);
await f.db.transaction('alpha', async tx => {
const w = (await tx.get<Workspace>('workspace', f.workspace.id))!;
w.lastActivityAt = f.workspace.lastActivityAt;
delete w.managedBy;
await tx.put('workspace', w);
});
await f.lifecycle.scheduleIdle('alpha');
expect(f.calls).toEqual([]);
});
it('rechecks activity before automatic scaling and fences cancellation with a resume operation', async () => {
const f = await fixture();
await f.lifecycle.scheduleIdle('alpha');
const [pause] = await f.items();
await f.age();
f.activity.gatewayConnections = 1;
await f.lifecycle.reconcile('alpha', pause!);
expect(f.calls.filter(c => c.method === 'PUT')).toHaveLength(0);
expect((await f.get()).status).toBe('resuming');
expect((await f.get()).lifecycle).toMatchObject({ revision: 2, desired: 'running', automatic: false });
await f.lifecycle.reconcile('alpha', pause!);
expect(f.calls.filter(c => c.method === 'PUT')).toHaveLength(0);
});
});
describe('workspaces as people see them', () => {
it('never shows the internal upstream addresses, whether listed or requested again', async () => {
const db = new MemoryStore();
db.seed('alpha');
const services = new Services(new ControlPlane(db, { NODE_ENV: 'test' }));
const project = await services.control.createProject(owner, { name: 'Workspace fixture' });
const api = new ApiController(services);
const request = { actor: owner } as never;
const created = (await api.workspace(request, project.id)).data;
// The provisioner records where the workspace runs; only the gateway may reach it there.
await db.transaction('alpha', async tx => {
const workspace = (await tx.get<Workspace>('workspace', created.id))!;
await tx.put('workspace', {
...workspace,
status: 'ready',
upstreamUrl: 'http://10.0.0.5:3000/',
collaborationUpstreamUrl: 'http://10.0.0.5:8100/',
});
});
const seen = [(await api.workspace(request, project.id)).data, ...(await api.workspaces(request, project.id)).data];
expect(seen).toHaveLength(2);
for (const workspace of seen) {
expect(workspace).toMatchObject({ id: created.id, status: 'ready' });
expect(workspace).not.toHaveProperty('upstreamUrl');
expect(workspace).not.toHaveProperty('collaborationUpstreamUrl');
}
});
});