Adds prettier 3.9.8 (exact) with a shared config (single quotes, 120 columns, trailing commas), .prettierignore, .editorconfig and the `format` / `format:check` scripts, then formats all TypeScript, JavaScript, CSS, JSON and YAML sources. Purely mechanical: no behaviour change. Typecheck passes, the vitest suite and the 74 IDE node tests produce the same results as before (socket-binding tests still cannot run inside this sandbox). Previously the longest source line was ~3000 characters; reviewing the following semantic changes now produces readable diffs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
127 lines
5.2 KiB
JavaScript
127 lines
5.2 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { renderWorkspaceResources } from '../workspaces.mjs';
|
|
import { observeWorkspaceLifecycle, reconcileWorkspaceLifecycle } from '../workspace-lifecycle.mjs';
|
|
const config = {
|
|
tenantId: 'alpha',
|
|
namespace: 'wf-alpha',
|
|
context: 'fixture',
|
|
controlNamespace: 'wf-control',
|
|
runtimeClass: 'kata',
|
|
storageClass: 'fixture',
|
|
storageSize: '10Gi',
|
|
};
|
|
const input = { tenantId: 'alpha', projectId: 'project', workspaceId: 'workspace' };
|
|
function fixture() {
|
|
const rendered = renderWorkspaceResources(config, input, `registry.example/theia@sha256:${'a'.repeat(64)}`);
|
|
const d = rendered.resources.find(r => r.kind === 'Deployment');
|
|
d.metadata.uid = 'immutable-uid';
|
|
d.metadata.resourceVersion = '1';
|
|
d.metadata.generation = 1;
|
|
d.status = { observedGeneration: 1, availableReplicas: 1, updatedReplicas: 1, replicas: 1 };
|
|
let pods = [];
|
|
let lose = false;
|
|
const calls = [];
|
|
const execute = async (_config, args) => {
|
|
calls.push(args);
|
|
if (args[0] === 'get' && args[1] === 'pods') return { items: pods };
|
|
if (args[0] === 'get') return structuredClone(d);
|
|
assert.equal(args[0], 'patch');
|
|
assert.equal(args[1], 'deployment');
|
|
assert.equal(args[2], rendered.name);
|
|
const patch = JSON.parse(args[args.indexOf('-p') + 1]);
|
|
assert.equal(patch[0].value, d.metadata.resourceVersion);
|
|
assert.equal(patch[1].value, d.metadata.uid);
|
|
d.metadata.annotations = patch[2].value;
|
|
d.spec.replicas = patch[3].value;
|
|
d.metadata.resourceVersion = String(Number(d.metadata.resourceVersion) + 1);
|
|
d.metadata.generation++;
|
|
if (lose) throw new Error('KUBERNETES_LIFECYCLE_UNKNOWN');
|
|
return structuredClone(d);
|
|
};
|
|
return {
|
|
d,
|
|
calls,
|
|
execute,
|
|
setPods: value => {
|
|
pods = value;
|
|
},
|
|
lose: () => {
|
|
lose = true;
|
|
},
|
|
converge: () => {
|
|
d.status = {
|
|
observedGeneration: d.metadata.generation,
|
|
replicas: d.spec.replicas,
|
|
availableReplicas: d.spec.replicas,
|
|
updatedReplicas: d.spec.replicas,
|
|
};
|
|
},
|
|
};
|
|
}
|
|
test('pause only patches the owned IDE deployment, retains all PVCs, and waits for observed termination', async () => {
|
|
const f = fixture();
|
|
const request = { ...input, revision: 1, desired: 'paused' };
|
|
assert.equal((await reconcileWorkspaceLifecycle(config, request, { execute: f.execute })).status, 'pending');
|
|
f.converge();
|
|
f.setPods([{ status: { phase: 'Running' }, metadata: { deletionTimestamp: new Date().toISOString() } }]);
|
|
assert.equal((await observeWorkspaceLifecycle(config, input, { execute: f.execute })).status, 'pending');
|
|
f.setPods([]);
|
|
assert.equal((await observeWorkspaceLifecycle(config, input, { execute: f.execute })).status, 'paused');
|
|
assert.ok(f.calls.every(args => !args.includes('pvc') && !args.includes('delete') && !args.includes('service')));
|
|
});
|
|
test('resume does not claim ready until updated and available replica is observed', async () => {
|
|
const f = fixture();
|
|
await reconcileWorkspaceLifecycle(config, { ...input, revision: 1, desired: 'paused' }, { execute: f.execute });
|
|
f.converge();
|
|
assert.equal(
|
|
(await reconcileWorkspaceLifecycle(config, { ...input, revision: 2, desired: 'running' }, { execute: f.execute }))
|
|
.status,
|
|
'pending',
|
|
);
|
|
f.converge();
|
|
assert.equal((await observeWorkspaceLifecycle(config, input, { execute: f.execute })).status, 'ready');
|
|
});
|
|
test('lost mutation response reconciles by observation and does not send a duplicate patch', async () => {
|
|
const f = fixture();
|
|
f.lose();
|
|
const request = { ...input, revision: 1, desired: 'paused' };
|
|
await assert.rejects(reconcileWorkspaceLifecycle(config, request, { execute: f.execute }), /UNKNOWN/);
|
|
f.converge();
|
|
assert.equal((await reconcileWorkspaceLifecycle(config, request, { execute: f.execute })).status, 'paused');
|
|
assert.equal(f.calls.filter(a => a[0] === 'patch').length, 1);
|
|
});
|
|
test('tenant, namespace, managed labels and revision fencing reject without mutations', async () => {
|
|
const f = fixture();
|
|
await assert.rejects(
|
|
reconcileWorkspaceLifecycle(
|
|
config,
|
|
{ ...input, tenantId: 'other', revision: 1, desired: 'paused' },
|
|
{ execute: f.execute },
|
|
),
|
|
/SCOPE/,
|
|
);
|
|
f.d.metadata.labels['wishforge.io/component'] = 'production-app';
|
|
await assert.rejects(
|
|
reconcileWorkspaceLifecycle(config, { ...input, revision: 1, desired: 'paused' }, { execute: f.execute }),
|
|
/OWNERSHIP/,
|
|
);
|
|
f.d.metadata.labels['wishforge.io/component'] = 'ide';
|
|
f.d.metadata.namespace = 'production';
|
|
await assert.rejects(
|
|
reconcileWorkspaceLifecycle(config, { ...input, revision: 1, desired: 'paused' }, { execute: f.execute }),
|
|
/OWNERSHIP/,
|
|
);
|
|
f.d.metadata.namespace = config.namespace;
|
|
f.d.metadata.annotations = { 'wishforge.io/lifecycle-revision': '3', 'wishforge.io/lifecycle-desired': 'running' };
|
|
await assert.rejects(
|
|
reconcileWorkspaceLifecycle(config, { ...input, revision: 2, desired: 'paused' }, { execute: f.execute }),
|
|
/STALE/,
|
|
);
|
|
await assert.rejects(
|
|
reconcileWorkspaceLifecycle(config, { ...input, revision: 3, desired: 'paused' }, { execute: f.execute }),
|
|
/CONFLICT/,
|
|
);
|
|
assert.equal(f.calls.filter(a => a[0] === 'patch').length, 0);
|
|
});
|