The cluster never ran Theia or the IDE gateway, so every workspace stayed "未配置" forever. The feature is gone instead of half-present: - API: workspace, launch and lifecycle endpoints, the gateway identity and its internal routes, the dispatcher's workspace provisioning and the lifecycle worker. Migration 003 deletes stored workspaces and settles their pending outbox items. - Provisioner: workspace provisioning and lifecycle for both backends. - Web: the 云 IDE panel, the launch route, the start-branch picker that listed IDE branches, and the workspace count on usage. - apps/ide, infra/oct, dev:ide and the CI job; IDE parts of local k3s, network policies, RBAC, docs and the architecture diagram. A wish can still start from a pushed branch through the API (sourceBranch). Plan files and stored entitlements may still carry maxWorkspaces; it is accepted and ignored. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
193 lines
8.9 KiB
TypeScript
193 lines
8.9 KiB
TypeScript
import { afterEach, expect, it, vi } from 'vitest';
|
||
import { MemoryStore } from './helpers/memory-store.js';
|
||
import { ControlPlane, record } from '../apps/api/src/domain/control-plane.js';
|
||
import type { Run } from '@wishforge/contracts';
|
||
|
||
const temporal = vi.hoisted(() => ({ connect: vi.fn(), close: vi.fn(), cancel: vi.fn() }));
|
||
vi.mock('@temporalio/client', () => ({
|
||
Connection: { connect: temporal.connect },
|
||
Client: class {
|
||
constructor(private options: { namespace: string }) {}
|
||
workflow = { getHandle: (id: string) => ({ cancel: () => temporal.cancel(id, this.options.namespace) }) };
|
||
},
|
||
WorkflowIdReusePolicy: { ALLOW_DUPLICATE: 'allow' },
|
||
}));
|
||
import { Dispatcher } from '../apps/api/src/dispatcher.js';
|
||
|
||
afterEach(() => {
|
||
vi.restoreAllMocks();
|
||
vi.clearAllMocks();
|
||
});
|
||
it('defers an invalid tenant provisioner without reconnecting or losing either tenant cancellation', async () => {
|
||
const store = new MemoryStore();
|
||
store.seed('alpha');
|
||
store.seed('beta');
|
||
const run = { ...record('alpha'), status: 'queued', revision: 1 } as Run;
|
||
await store.transaction('alpha', async tx => {
|
||
await tx.put('run', run);
|
||
await tx.enqueue('run.start', { runId: run.id, revision: 1 }, 'start-fixture');
|
||
await tx.enqueue('run.signal', { runId: run.id, action: 'cancel' }, 'cancel-alpha');
|
||
});
|
||
await store.transaction('beta', tx =>
|
||
tx.enqueue('run.signal', { runId: 'beta-run', action: 'cancel' }, 'cancel-beta'),
|
||
);
|
||
temporal.connect.mockResolvedValue({ close: temporal.close });
|
||
const dispatcher = new Dispatcher(
|
||
new ControlPlane(store, {
|
||
WF_DISPATCH_TENANTS: 'alpha,beta',
|
||
WF_PROVISIONER_TENANT_ID: 'alpha',
|
||
WF_PROVISIONER_URL: 'https://fixture.invalid',
|
||
WF_PROVISIONER_TOKEN: 'too-short',
|
||
}),
|
||
);
|
||
temporal.cancel.mockImplementation(async (id: string) => {
|
||
if (id.endsWith(':beta-run')) await dispatcher.close();
|
||
});
|
||
const log = vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
||
await dispatcher.start();
|
||
expect(temporal.connect).toHaveBeenCalledTimes(1);
|
||
expect(temporal.close).toHaveBeenCalledTimes(1);
|
||
expect(temporal.cancel).toHaveBeenCalledWith(`wishforge:alpha:${run.id}`, 'default');
|
||
expect(temporal.cancel).toHaveBeenCalledWith('wishforge:beta:beta-run', 'default');
|
||
expect(await store.transaction('alpha', tx => tx.get<Run>('run', run.id))).toMatchObject({
|
||
status: 'waiting',
|
||
waitingReason: 'sandbox_provisioner',
|
||
waitingDetail: expect.any(String),
|
||
});
|
||
expect(await store.transaction('alpha', tx => tx.outbox())).toMatchObject([{ id: 'start-fixture', attempts: 1 }]);
|
||
expect(await store.transaction('beta', tx => tx.outbox())).toEqual([]);
|
||
expect(log).not.toHaveBeenCalledWith(expect.stringContaining('Temporal dispatcher reconnecting'));
|
||
});
|
||
|
||
it('signals each tenant’s workflows in that tenant’s own Temporal namespace', async () => {
|
||
const store = new MemoryStore();
|
||
for (const tenant of ['alpha', 'beta']) {
|
||
store.seed(tenant);
|
||
await store.transaction(tenant, tx =>
|
||
tx.enqueue('run.signal', { runId: `${tenant}-run`, action: 'cancel' }, `cancel-${tenant}`),
|
||
);
|
||
}
|
||
temporal.connect.mockResolvedValue({ close: temporal.close });
|
||
const dispatcher = new Dispatcher(
|
||
new ControlPlane(store, { WF_DISPATCH_TENANTS: 'alpha,beta', TEMPORAL_NAMESPACE_PREFIX: 'wf-' }),
|
||
);
|
||
temporal.cancel.mockImplementation(async (id: string) => {
|
||
if (id.endsWith(':beta-run')) await dispatcher.close();
|
||
});
|
||
await dispatcher.start();
|
||
expect(temporal.cancel.mock.calls).toEqual([
|
||
['wishforge:alpha:alpha-run', 'wf-alpha'],
|
||
['wishforge:beta:beta-run', 'wf-beta'],
|
||
]);
|
||
});
|
||
it('refuses to dispatch several tenants through one namespace in production', async () => {
|
||
const dispatcher = new Dispatcher(
|
||
new ControlPlane(new MemoryStore(), { WF_DEPLOYMENT_MODE: 'production', WF_DISPATCH_TENANTS: 'alpha,beta' }),
|
||
);
|
||
await expect(dispatcher.start()).rejects.toThrow('per-tenant Temporal namespaces');
|
||
expect(temporal.connect).not.toHaveBeenCalled();
|
||
});
|
||
it('drops a start that a newer one replaced while it was being dispatched, leaving the run as it is', async () => {
|
||
const store = new MemoryStore();
|
||
store.seed('alpha');
|
||
const run = { ...record('alpha'), status: 'queued', revision: 1, pendingStartId: 'start-fixture' } as Run;
|
||
await store.transaction('alpha', async tx => {
|
||
await tx.put('run', run);
|
||
await tx.enqueue('run.start', { runId: run.id, revision: 1 }, 'start-fixture');
|
||
await tx.enqueue('run.signal', { runId: 'other-run', action: 'cancel' }, 'cancel-other');
|
||
});
|
||
// Right after the dispatcher first reads the run, a pause and a continue enqueue a newer start.
|
||
const transaction = store.transaction.bind(store);
|
||
let replaced = false;
|
||
vi.spyOn(store, 'transaction').mockImplementation(async (tenant, work) => {
|
||
const result = await transaction(tenant, work);
|
||
if (!replaced && (result as Run | undefined)?.id === run.id) {
|
||
replaced = true;
|
||
await transaction(tenant, async tx => {
|
||
const current = (await tx.get<Run>('run', run.id))!;
|
||
await tx.put('run', { ...current, pendingStartId: 'newer-start' });
|
||
});
|
||
}
|
||
return result;
|
||
});
|
||
const provisioner = vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('No sandbox may be provisioned'));
|
||
temporal.connect.mockResolvedValue({ close: temporal.close });
|
||
const dispatcher = new Dispatcher(
|
||
new ControlPlane(store, {
|
||
WF_DISPATCH_TENANTS: 'alpha',
|
||
WF_PROVISIONER_TENANT_ID: 'alpha',
|
||
WF_PROVISIONER_URL: 'https://fixture.invalid',
|
||
WF_PROVISIONER_TOKEN: 'fixture-provisioner-token-only-long-enough',
|
||
WF_RESOURCE_GRANT_SECRET: 'resource-fixture-signing-secret-only-12345',
|
||
WF_INTERNAL_TOKEN_KEY: 'internal-fixture-signing-key-only-123456789',
|
||
WF_RUN_WORKER_IMAGE: `registry.invalid/runtime@sha256:${'a'.repeat(64)}`,
|
||
}),
|
||
);
|
||
temporal.cancel.mockImplementation(async () => dispatcher.close());
|
||
vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
||
await dispatcher.start();
|
||
expect(replaced).toBe(true);
|
||
expect(provisioner).not.toHaveBeenCalled();
|
||
const after = await store.transaction('alpha', tx => tx.get<Run>('run', run.id));
|
||
expect(after).toMatchObject({ status: 'queued', pendingStartId: 'newer-start' });
|
||
expect(after?.executionGeneration).toBeUndefined();
|
||
expect(after?.waitingReason).toBeUndefined();
|
||
expect((await store.transaction('alpha', tx => tx.outbox())).map(item => item.id)).not.toContain('start-fixture');
|
||
});
|
||
|
||
it('looks again soon for a start that waits for an execution slot, without calling it a failure', async () => {
|
||
const store = new MemoryStore();
|
||
store.seed('alpha');
|
||
const queued = { ...record('alpha'), status: 'queued', revision: 1, pendingStartId: 'start-fixture' } as Run;
|
||
await store.transaction('alpha', async tx => {
|
||
// Both of the tenant's execution slots are taken.
|
||
for (const generation of ['busy-1', 'busy-2'])
|
||
await tx.put('run', {
|
||
...record('alpha'),
|
||
status: 'running',
|
||
revision: 1,
|
||
resourceAllocation: { status: 'provisioned', generation },
|
||
} as unknown as Run);
|
||
await tx.put('run', queued);
|
||
await tx.enqueue('run.start', { runId: queued.id, revision: 1 }, 'start-fixture');
|
||
// It has been waiting a while: backing off had reached two minutes between looks.
|
||
for (let look = 0; look < 5; look++) await tx.defer('start-fixture', 0);
|
||
await tx.enqueue('run.signal', { runId: 'other-run', action: 'cancel' }, 'cancel-other');
|
||
});
|
||
const deferred: Array<[string, number]> = [];
|
||
const transaction = store.transaction.bind(store);
|
||
vi.spyOn(store, 'transaction').mockImplementation((tenant, work) =>
|
||
transaction(tenant, tx =>
|
||
work({
|
||
...tx,
|
||
defer: async (id: string, seconds: number) => {
|
||
deferred.push([id, seconds]);
|
||
await tx.defer(id, seconds);
|
||
},
|
||
}),
|
||
),
|
||
);
|
||
const provisioner = vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('No sandbox may be provisioned'));
|
||
temporal.connect.mockResolvedValue({ close: temporal.close });
|
||
const dispatcher = new Dispatcher(
|
||
new ControlPlane(store, {
|
||
WF_DISPATCH_TENANTS: 'alpha',
|
||
WF_PROVISIONER_TENANT_ID: 'alpha',
|
||
WF_PROVISIONER_URL: 'https://fixture.invalid',
|
||
WF_PROVISIONER_TOKEN: 'fixture-provisioner-token-only-long-enough',
|
||
WF_RESOURCE_GRANT_SECRET: 'resource-fixture-signing-secret-only-12345',
|
||
WF_INTERNAL_TOKEN_KEY: 'internal-fixture-signing-key-only-123456789',
|
||
WF_RUN_WORKER_IMAGE: `registry.invalid/runtime@sha256:${'a'.repeat(64)}`,
|
||
}),
|
||
);
|
||
temporal.cancel.mockImplementation(async () => dispatcher.close());
|
||
const log = vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
||
await dispatcher.start();
|
||
expect(deferred).toEqual([['start-fixture', 10]]);
|
||
expect(provisioner).not.toHaveBeenCalled();
|
||
const after = await transaction('alpha', tx => tx.get<Run>('run', queued.id));
|
||
expect(after).toMatchObject({ status: 'queued' });
|
||
expect(after?.waitingReason).toBeUndefined();
|
||
expect(log).not.toHaveBeenCalled();
|
||
});
|