// Author: Guoliang Chen, a Senior Digital Design Engineer // Checks for `restart.js`: the serialized restart chain that makes a // `lucisv.verbose` toggle land on the server, whenever it arrives. // // node restart_test.js // // Exits 0 on success, 1 on any failure. Plain node, no dependencies and no // test runner — the same style as `smoke.js` next to it, and run the same way // (`tools/release.sh` runs `smoke.js`; this one is a developer check). // // The fake client below scripts only what `restart.js` is allowed to depend // on: `start()` and `stop()` return promises, and this file decides when they // settle. It deliberately does **not** re-implement `vscode-languageclient`'s // state machine — QA's throwaway harness did, and a copy of somebody else's // state machine is a test of the copy. What is pinned here is *our* rule: one // chain, no overlapping transitions, and the last toggle always wins. // See docs/features/verbose-timing/README.md. const { createRestarter, sameArgs } = require('./restart'); let checks = 0; function ok(condition, what) { checks += 1; if (!condition) fail(what); } function equal(got, want, what) { checks += 1; const a = JSON.stringify(got); const b = JSON.stringify(want); if (a !== b) fail(`${what}: got ${a}, want ${b}`); } function fail(message) { console.error(`restart: FAIL — ${message}`); process.exit(1); } /** Let every already-resolved promise run. */ const settle = () => new Promise((resolve) => setImmediate(resolve)); /** * A client whose `start()`/`stop()` this test settles by hand, and a * `serverOptions` for it to read arguments from. * * `spawns` records the arguments of every start *as the client would have read * them* — which is the whole question a lost toggle turns on. */ function fake({ pendingStarts = 0 } = {}) { const serverOptions = { command: 'lucisv-server', args: [], transport: 'stdio' }; const state = { serverOptions, spawns: [], stops: 0, started: 0, failures: [], /** Resolves the start that is waiting, while there are pending ones left. */ release: null, pendingStarts, failNextStart: false, }; const client = { start() { state.spawns.push(serverOptions.args.slice()); if (state.failNextStart) { state.failNextStart = false; return Promise.reject(new Error('spawn ENOENT')); } // Only the first `pendingStarts` spawns hang — the ones a test is about. // A restart that follows resolves at once, like a spawn that works. if (state.pendingStarts <= 0) return Promise.resolve(); state.pendingStarts -= 1; return new Promise((resolve, reject) => { state.release = { resolve, reject }; }); }, stop() { state.stops += 1; return Promise.resolve(); }, }; state.restarter = createRestarter({ client, serverOptions, onStarted: () => { state.started += 1; }, onStartFailed: (error) => state.failures.push(error.message), }); state.client = client; return state; } async function activationSpawnsOnceWithTheSettingsArguments() { const it = fake(); it.serverOptions.args = ['--verbose']; const restarter = createRestarter({ client: it.client, serverOptions: it.serverOptions, onStarted: () => {}, onStartFailed: fail, }); await restarter.start(); equal(it.spawns, [['--verbose']], 'activation spawns once, with what the settings said'); equal(restarter.spawnedWith(), ['--verbose'], 'and remembers what it spawned with'); } async function aToggleDuringTheActivationStartIsNotLost() { // QA finding 1, 2026-08-29, and the reason this file exists. The activation // start is still in flight — a large workspace's first scan holds it open — // when the user turns `lucisv.verbose` on. Before the chain, `stop()` failed, // `start()` rejoined the in-flight start with the OLD arguments, and the // extension repainted as if the toggle had worked. const it = fake({ pendingStarts: 1 }); it.restarter.start(); await settle(); equal(it.spawns, [[]], 'the activation spawn is in flight, with no arguments'); const toggled = it.restarter.want(['--verbose']); await settle(); equal(it.spawns, [[]], 'and nothing else may spawn while it is: no overlapping transitions'); equal(it.stops, 0, 'and nothing is stopped mid-start'); it.release.resolve(); await toggled; equal(it.spawns, [[], ['--verbose']], 'the toggle is honoured once the start it queued behind is done'); equal(it.stops, 1, 'by stopping the server that had the old arguments'); equal(it.serverOptions.args, ['--verbose'], 'the client reads the new arguments at that start'); equal(it.restarter.spawnedWith(), ['--verbose'], 'and the running server is the one the user asked for'); equal(it.started, 2, 'both starts repaint'); } async function aBurstOfTogglesCollapsesToOneRestartOnTheLastOne() { // on -> off -> on while a start is in flight: three toggles, one restart, // and it lands on the last thing anybody asked for. const it = fake({ pendingStarts: 1 }); it.restarter.start(); await settle(); it.restarter.want(['--verbose']); it.restarter.want([]); const last = it.restarter.want(['--verbose']); await settle(); equal(it.spawns, [[]], 'still nothing extra while the first start is in flight'); it.release.resolve(); await last; equal(it.spawns, [[], ['--verbose']], 'three toggles cost one restart'); equal(it.restarter.spawns(), 2, 'and the restarter says so'); equal(it.restarter.spawnedWith(), ['--verbose'], 'landing on the last toggle'); } async function aToggleBackToWhatIsRunningRestartsNothing() { const it = fake(); await it.restarter.start(); await it.restarter.want(['--verbose']); equal(it.spawns.length, 2, 'the real change restarts'); await it.restarter.want(['--verbose']); equal(it.spawns.length, 2, 'asking for what is already running does nothing'); equal(it.stops, 1, 'and stops nothing'); } async function aFailedStartIsReportedAndTheChainStaysUsable() { const it = fake(); await it.restarter.start(); it.failNextStart = true; await it.restarter.want(['--verbose']); equal(it.failures, ['spawn ENOENT'], 'the failure is reported, not swallowed'); equal(it.restarter.spawnedWith(), null, 'and nothing is recorded as running'); // The next toggle must *ask* again, including for the arguments that just // failed. Whether the ask can succeed is not this module's call: the real // `vscode-languageclient` never clears `_onStart` after a failed start, so it // hands back the same rejection without spawning anything (the known // limitation in the feature README). What is pinned here is that `restart.js` // keeps asking rather than going quiet — the fake below is free to succeed. await it.restarter.want(['--verbose']); equal(it.spawns, [[], ['--verbose'], ['--verbose']], 'the chain is still usable and retries'); equal(it.failures.length, 1, 'and the retry that worked reports nothing'); } async function aStopThatRejectsDoesNotStopTheRestart() { // A client that will not stop — not running, or already stopping — must not // cost the user the start they actually asked for. const it = fake(); await it.restarter.start(); it.client.stop = () => Promise.reject(new Error('Client is not running')); await it.restarter.want(['--verbose']); equal(it.spawns, [[], ['--verbose']], 'the start happens anyway'); equal(it.failures, [], 'and a refused stop is not a start failure'); } async function aToggleInTheSameTickAsDisposeSpawnsNothing() { // QA finding 3, 2026-08-29, as they reproduced it: with `deactivate()` // stopping the client itself — attached to a *snapshot* of the chain — a // `lucisv.verbose` event arriving in the same tick queued a full reconcile // beside it and the trace read `stop, stop, start(--verbose)`. That last // spawn had nothing left to await or stop it: an orphan, whenever the // extension host outlives the module (a disable, or a host-only restart). const it = fake(); await it.restarter.start(); equal(it.spawns, [[]], 'the server is up'); const gone = it.restarter.dispose(); it.restarter.want(['--verbose']); // the racing toggle, same tick await gone; await settle(); equal(it.spawns, [[]], 'nothing is ever spawned after teardown begins'); equal(it.stops, 1, 'and the one stop is the teardown one'); equal(it.restarter.spawnedWith(), null, 'nothing is left running'); // And the door stays shut: a later toggle is a silent no-op, not a spawn. const after = it.restarter.want([]); await after; equal(it.spawns, [[]], 'a toggle after teardown does nothing at all'); // Nothing is even *queued* after teardown — `want` hands back the teardown // promise itself rather than adding a reconcile for the flag inside // `reconcile` to no-op. Both guards are wanted: this one keeps the chain // from growing, and the one in `reconcile` catches what got in first. ok(after === gone, 'a toggle after teardown adds no link to the chain'); } async function aReconcileQueuedBeforeDisposeBecomesANoOp() { // The other order, and the reason the flag is checked inside `reconcile` and // not only in `want`: the toggle got into the queue first, and `dispose` // shut the door behind it. What was queued must do nothing rather than // spawn — the chain guarantees it runs *after* dispose was called, not that // it was never queued. const it = fake({ pendingStarts: 1 }); it.restarter.start(); await settle(); equal(it.spawns, [[]], 'the activation start is in flight'); it.restarter.want(['--verbose']); // queued behind the in-flight start const gone = it.restarter.dispose(); // ...and then the window closes it.release.resolve(); await gone; equal(it.spawns, [[]], 'the queued toggle spawns nothing once the door is shut'); equal(it.stops, 1, 'only the teardown stop happens'); equal(it.restarter.spawnedWith(), null, 'and nothing is left running'); } async function disposeResolvesAfterTheFinalStop() { // What `deactivate()` returns is what VS Code waits on, so it has to mean // "the server is stopped", not "the stop was queued". const it = fake(); await it.restarter.start(); let stopped = false; it.client.stop = () => new Promise((resolve) => setTimeout(() => { stopped = true; it.stops += 1; resolve(); }, 10), ); await it.restarter.dispose(); ok(stopped, 'dispose() resolves only once the final stop has finished'); equal(it.stops, 1, 'and it really stopped the client'); // Idempotent: disposing twice is one stop, and still resolves. await it.restarter.dispose(); equal(it.stops, 1, 'a second dispose stops nothing again'); // And two disposes in the *same tick* both mean "teardown is finished" — // which is only true if the final stop went onto the chain rather than // beside it, since the second caller gets the chain as it stands. const again = fake(); await again.restarter.start(); let down = false; again.client.stop = () => new Promise((resolve) => setTimeout(() => { down = true; resolve(); }, 10), ); await Promise.all([again.restarter.dispose(), again.restarter.dispose()]); ok(down, 'both callers wait for the one stop that actually happens'); } async function disposingBeforeAnythingSpawnedStopsNothing() { // An activation that failed before a server existed must not stop a client // that was never started — the real one rejects that, loudly. const it = fake(); await it.restarter.dispose(); equal(it.stops, 0, 'nothing was running, so nothing is stopped'); equal(it.spawns, [], 'and nothing was ever spawned'); } function argumentListsCompareByValueAndNullIsNothing() { ok(sameArgs([], []), 'no arguments is no arguments'); ok(sameArgs(['--verbose'], ['--verbose']), 'the same flag is the same flag'); ok(!sameArgs([], ['--verbose']), 'and a different one is not'); ok(!sameArgs(null, []), 'nothing running is never what is wanted'); ok(!sameArgs(null, null), 'not even against another nothing — a failed start must retry'); } async function main() { // A test that awaits a promise nothing will settle would otherwise let node // run out of work and exit 0 in silence — a hang that reads as a pass. This // timer both fails it and keeps the event loop alive to say so. const watchdog = setTimeout( () => fail('timed out after 5s — a promise this test waits on never settled'), 5000, ); argumentListsCompareByValueAndNullIsNothing(); await activationSpawnsOnceWithTheSettingsArguments(); await aToggleDuringTheActivationStartIsNotLost(); await aBurstOfTogglesCollapsesToOneRestartOnTheLastOne(); await aToggleBackToWhatIsRunningRestartsNothing(); await aFailedStartIsReportedAndTheChainStaysUsable(); await aStopThatRejectsDoesNotStopTheRestart(); await aToggleInTheSameTickAsDisposeSpawnsNothing(); await aReconcileQueuedBeforeDisposeBecomesANoOp(); await disposeResolvesAfterTheFinalStop(); await disposingBeforeAnythingSpawnedStopsNothing(); clearTimeout(watchdog); console.log(`restart: OK — ${checks} checks over the restart chain`); } main().catch((error) => fail(error.stack || String(error))); // Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com