File size: 9,586 Bytes
f0634fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | import { describe, expect, it, vi } from 'vitest';
import { FakeRuntime } from '#/runtime/fakeRuntime';
import { RuntimeError, RuntimeRegistry } from '#/runtime/runtimeRegistry';
function runtime(generation: string, status: 'ready' | 'disconnected' = 'ready'): FakeRuntime {
return Object.assign(
new FakeRuntime(
{ workspaceId: 'workspace', runtimeId: 'local', generation },
{ status, capabilities: ['fs', 'process'] },
),
{ fs: {} as never, process: {} as never },
);
}
describe('RuntimeRegistry', () => {
it('rejects conflicts', () => {
const registry = new RuntimeRegistry('workspace');
registry.register(runtime('one'));
expect(() => registry.register(runtime('two'))).toThrow(RuntimeError);
});
it('pins leases across replacement', async () => {
const registry = new RuntimeRegistry('workspace');
const first = runtime('one');
const second = runtime('two');
const registration = registry.register(first);
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' }, ['fs']);
const replacement = registration.replace(second);
await Promise.resolve();
expect(lease.runtime).toBe(first);
expect(registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' }).runtime).toBe(second);
expect(first.disposed).toBe(false);
lease.dispose();
await replacement;
expect(first.disposed).toBe(true);
});
it('publishes status and reconnects the same generation', () => {
const registry = new RuntimeRegistry('workspace');
const current = runtime('one');
const statuses: string[] = [];
registry.onDidChange((change) => {
if (change.status !== undefined) statuses.push(change.status);
});
registry.register(current);
current.setStatus('disconnected');
expect(() => registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' })).toThrow('disconnected');
current.setStatus('ready');
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' });
expect(lease.runtime).toBe(current);
expect(lease.runtime.identity.generation).toBe('one');
lease.dispose();
expect(statuses).toEqual(['ready', 'disconnected', 'ready']);
});
it('allows degraded generations only when every required capability remains available', () => {
const registry = new RuntimeRegistry('workspace');
const current = runtime('one');
registry.register(current);
current.setStatus('degraded');
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' }, ['fs', 'process']);
expect(lease.runtime).toBe(current);
lease.dispose();
expect(() => registry.acquire(
{ workspaceId: 'workspace', runtimeId: 'local' },
['terminal'],
)).toThrowError(expect.objectContaining<Partial<RuntimeError>>({ code: 'runtime.unavailable' }));
});
it('keeps the current generation when replacement preparation fails', async () => {
const registry = new RuntimeRegistry('workspace');
const first = runtime('one');
const invalid = new FakeRuntime(
{ workspaceId: 'other', runtimeId: 'local', generation: 'two' },
{ capabilities: ['fs'] },
);
const registration = registry.register(first);
await expect(registration.replace(invalid)).rejects.toThrow('other');
expect(invalid.disposed).toBe(true);
expect(registry.current('local')).toBe(first);
});
it('keeps a published replacement current when previous generation cleanup fails', async () => {
const registry = new RuntimeRegistry('workspace');
const first = runtime('one');
const second = runtime('two');
Object.assign(first, {
dispose: vi.fn(async () => {
first.disposed = true;
throw new Error('old runtime cleanup failed');
}),
});
const registration = registry.register(first);
await expect(registration.replace(second)).rejects.toThrow('old runtime cleanup failed');
expect(registry.current('local')).toBe(second);
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' }, ['process']);
expect(lease.runtime).toBe(second);
expect(second.disposed).toBe(false);
lease.dispose();
await registration.remove();
});
it('serializes replacement and removal', async () => {
const registry = new RuntimeRegistry('workspace');
const first = runtime('one');
const second = runtime('two');
const registration = registry.register(first);
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' });
const replacement = registration.replace(second);
const removal = registration.remove();
await Promise.resolve();
expect(registry.current('local')).toBe(second);
lease.dispose();
await Promise.all([replacement, removal]);
expect(registry.current('local')).toBeUndefined();
expect(first.disposed).toBe(true);
expect(second.disposed).toBe(true);
});
it('actively closes terminal, watch, MCP, and background resources in reverse order', async () => {
const registry = new RuntimeRegistry('workspace');
const first = runtime('one');
const registration = registry.register(first);
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' });
const order: string[] = [];
for (const name of ['terminal', 'watch', 'mcp', 'background']) {
lease.track({ dispose: async () => { order.push(name); } });
}
const replacement = registration.replace(runtime('two'));
await Promise.resolve();
expect(order).toEqual(['background']);
lease.dispose();
await replacement;
expect(order).toEqual(['background', 'mcp', 'watch', 'terminal']);
});
it('forces bounded disposal exactly once when a lease remains', async () => {
const registry = new RuntimeRegistry('workspace', 1);
const first = runtime('one');
const originalDispose = first.dispose.bind(first);
const dispose = vi.fn(originalDispose);
Object.assign(first, { dispose });
const registration = registry.register(first);
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' });
await registration.replace(runtime('two'));
expect(dispose).toHaveBeenCalledTimes(1);
lease.dispose();
await registration.remove();
expect(dispose).toHaveBeenCalledTimes(1);
});
it('rejects queued replacements after registry disposal starts', async () => {
const registry = new RuntimeRegistry('workspace');
const registration = registry.register(runtime('one'));
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' });
const replacement = registration.replace(runtime('two'));
const queued = runtime('three');
const queuedReplacement = registration.replace(queued);
await Promise.resolve();
const disposal = registry.dispose();
lease.dispose();
await replacement;
await expect(queuedReplacement).rejects.toThrow('disposed');
await disposal;
expect(queued.disposed).toBe(true);
expect(registry.current('local')).toBeUndefined();
});
it('snapshots only the current generation and its live status', async () => {
const registry = new RuntimeRegistry('workspace');
const first = runtime('one');
const registration = registry.register(first);
expect(registry.snapshot()).toEqual({
workspaceId: 'workspace',
runtimes: [{
runtimeId: 'local',
generation: 'one',
status: 'ready',
capabilities: ['fs', 'process'],
}],
});
first.setStatus('disconnected');
expect(registry.snapshot().runtimes[0]?.status).toBe('disconnected');
await registration.replace(runtime('two'));
expect(registry.snapshot().runtimes[0]).toMatchObject({
generation: 'two',
status: 'ready',
});
});
it('does not fallback when a runtime is missing', () => {
const registry = new RuntimeRegistry('workspace');
registry.register(runtime('one'));
expect(() => registry.acquire({ workspaceId: 'workspace', runtimeId: 'ssh1' })).toThrow('ssh1');
});
it('untracks caller-disposed resources so drain disposes each resource exactly once, survivors in reverse order', async () => {
const registry = new RuntimeRegistry('workspace');
const registration = registry.register(runtime('one'));
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' });
const order: string[] = [];
const counts = new Map<string, number>();
const resource = (name: string) => ({
dispose: () => {
counts.set(name, (counts.get(name) ?? 0) + 1);
order.push(name);
},
});
const a = lease.track(resource('a'));
const b = lease.track(resource('b'));
const c = lease.track(resource('c'));
b.dispose();
b.dispose();
const replacement = registration.replace(runtime('two'));
lease.dispose();
await replacement;
expect(order).toEqual(['b', 'c', 'a']);
expect(counts.get('a')).toBe(1);
expect(counts.get('b')).toBe(1);
expect(counts.get('c')).toBe(1);
});
it('rejects track once the generation is draining', async () => {
const registry = new RuntimeRegistry('workspace');
const registration = registry.register(runtime('one'));
const lease = registry.acquire({ workspaceId: 'workspace', runtimeId: 'local' });
const replacement = registration.replace(runtime('two'));
await Promise.resolve();
expect(() => lease.track({ dispose: () => {} })).toThrow('draining');
lease.dispose();
await replacement;
});
});
|