@bongos/core 1.19.923 → 1.19.924

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,7 +15,7 @@
15
15
 
16
16
  import { strict as assert } from 'node:assert';
17
17
  import { createRequire } from 'node:module';
18
- import { readFileSync } from 'node:fs';
18
+ import { readFileSync, readdirSync } from 'node:fs';
19
19
  import { fileURLToPath } from 'node:url';
20
20
  import { dirname, join } from 'node:path';
21
21
 
@@ -52,10 +52,24 @@ await t('the shape and the action both reach the DB constraints', () => {
52
52
  // The JS list and the CHECK are two spellings of one vocabulary; a widening that
53
53
  // lands in only one of them fails at insert time, in production, on the first
54
54
  // real use. (The bug class task 1003913 shipped a fix for, in another file.)
55
- const mig = read('modules', 'provisioning', 'migrations', 'provisioning_018_core_upgrade_intent.sql')
55
+ // Read the NEWEST migration that widens the action CHECK, not a fixed filename: the
56
+ // vocabulary grows, and a test pinned to one file passes forever while the constraint
57
+ // it is meant to guard falls behind (task 1004121 added an action 018 does not carry).
58
+ const migDir = join(__dirname, '..', 'modules', 'provisioning', 'migrations');
59
+ const widenings = readdirSync(migDir).filter((f) => f.endsWith('.sql'))
60
+ .filter((f) => /ADD CONSTRAINT provisioning_intents_action_check/.test(readFileSync(join(migDir, f), 'utf8')))
61
+ .sort();
62
+ assert.ok(widenings.length, 'some migration defines the action CHECK');
63
+ const mig = readFileSync(join(migDir, widenings[widenings.length - 1]), 'utf8')
64
+ .replace(/^\s*--.*$/gm, ' ').replace(/\s+/g, ' ');
65
+ const shapes = read('modules', 'provisioning', 'migrations', 'provisioning_018_core_upgrade_intent.sql')
56
66
  .replace(/^\s*--.*$/gm, ' ').replace(/\s+/g, ' ');
57
- assert.match(mig, /CHECK \(action IN \([^)]*'core-upgrade'\)\)/, 'the intents CHECK admits the new action');
58
- assert.match(mig, /CHECK \(hosting_shape IN \([^)]*'control-plane'\)\)/, 'the instances CHECK admits the new shape');
67
+ // Membership, not POSITION. The old spelling required 'core-upgrade' to be the last
68
+ // item before the closing paren, so the next action appended after it failed a test
69
+ // that had nothing to say about the next action.
70
+ assert.match(mig, /CHECK \(action IN \(/, 'the intents CHECK is an action allow-list');
71
+ assert.ok(mig.includes("'core-upgrade'"), 'and it still admits the move itself');
72
+ assert.match(shapes, /CHECK \(hosting_shape IN \([^)]*'control-plane'\)\)/, 'the instances CHECK admits the new shape');
59
73
  for (const a of P.INTENT_ACTIONS) {
60
74
  assert.ok(mig.includes(`'${a}'`), `the widened CHECK must still carry '${a}' — a rewrite drops what it forgets`);
61
75
  }
@@ -404,5 +418,43 @@ await t('THE DEPLOY PAGE NEVER COMPUTES A VERSION COMPARISON — every number on
404
418
  assert.match(js, /p\.behind/, 'and asks the server whether there is anything to do');
405
419
  });
406
420
 
421
+ console.log('\nthe pre-flight verdict, and when it may be shown (task 1004121):');
422
+
423
+ const snapOf = (over = {}) => ({
424
+ served_version: '1.19.917', available: ['1.19.917', '1.19.921'], recommended: '1.19.921',
425
+ observed_at: new Date().toISOString(), preflight_at: new Date().toISOString(), ...over,
426
+ });
427
+ const box = { id: '1', slug: 'cloudbongos', hosting_shape: 'control-plane' };
428
+
429
+ await t('a verdict taken against the version ON OFFER is shown', () => {
430
+ const p = CU.previewFromSnapshot(box, snapOf({
431
+ preflight: { target: '1.19.921', ok: true, lines: ['database reachable'], error: null } }), null);
432
+ assert.ok(p.preflight, 'it reaches the control');
433
+ assert.equal(p.preflight.target, '1.19.921');
434
+ assert.ok(p.preflight_at, 'with its own age, not the probe’s');
435
+ });
436
+
437
+ await t('a verdict taken against a SUPERSEDED version is withheld, not shown as a pass', () => {
438
+ // The whole point of storing `target` with the verdict. A pass checked against
439
+ // 1.19.920 says nothing true about a button labelled 1.19.921 — and a stale pass is
440
+ // worse than no answer, because it reads as a guarantee.
441
+ const p = CU.previewFromSnapshot(box, snapOf({
442
+ preflight: { target: '1.19.920', ok: true, lines: ['database reachable'], error: null } }), null);
443
+ assert.equal(p.preflight, null, 'withheld');
444
+ assert.equal(p.preflight_at, null, 'and so is its timestamp — no age for an answer not given');
445
+ });
446
+
447
+ await t('a stale REFUSAL is withheld too — it must not veto a move it never checked', () => {
448
+ const p = CU.previewFromSnapshot(box, snapOf({
449
+ preflight: { target: '1.19.900', ok: false, lines: [], error: 'working tree not clean' } }), null);
450
+ assert.equal(p.preflight, null, 'the guard is symmetric: it withholds fails as well as passes');
451
+ });
452
+
453
+ await t('no verdict at all is null — never an implied pass', () => {
454
+ const p = CU.previewFromSnapshot(box, snapOf({ preflight: null, preflight_at: null }), null);
455
+ assert.equal(p.preflight, null);
456
+ assert.equal(p.behind, true, 'and the rest of the reading is unaffected');
457
+ });
458
+
407
459
  console.log(`\ncore_upgrade_door: ${passed} passed, ${failed} failed`);
408
460
  if (failed) process.exit(1);
@@ -471,5 +471,87 @@ await t('an UNPRIVILEGED runner still adds no sudo and no env at all', async ()
471
471
  assert.match(cmd, /^node \S*scripts\/gds\/upgrade\.js /, 'it still invokes the CLI directly');
472
472
  });
473
473
 
474
+ console.log('\nthe dry run behind "see what would change" (task 1004121):');
475
+
476
+ await t('a passing dry run is parsed into its pre-flight lines, with no error', () => {
477
+ const { checks, error } = R.parsePreflight([
478
+ 'bongos upgrade — @bongos/core: 1.19.917 → 1.19.921',
479
+ ' ✓ pre-flight: no modules/ — nothing to pre-check',
480
+ ' ✓ pre-flight: database "cloudbongos" reachable as the current user',
481
+ ].join('\n'));
482
+ assert.equal(error, null);
483
+ assert.equal(checks.length, 2);
484
+ });
485
+
486
+ await t('a REFUSAL is carried verbatim — its wording is the instruction', () => {
487
+ const { error } = R.parsePreflight(
488
+ ' ✖ cannot determine which database to migrate.\n Set PGDATABASE, or give this instance a db.database in config/branding.json.');
489
+ assert.match(error, /Set PGDATABASE/, 'the remedy rides along, not just the complaint');
490
+ });
491
+
492
+ await t('ONE builder serves both legs — the preview and the move cannot drift apart', () => {
493
+ // Two hand-copied builders are how "what you were shown" stops matching "what runs".
494
+ // Everything deciding whether the upgrade is ALLOWED must be identical; only the tail
495
+ // that describes what happens AFTER the pin moves may differ, because a dry run stops
496
+ // before it.
497
+ const box = { id: 7, slug: 'test4', port: 3006, hosting_shape: 'standalone', db_name: 'test4' };
498
+ const move = R.upgradeInvocation(box, '1.19.921', { privileged: true, requestedBy: '42' });
499
+ const peek = R.upgradeInvocation(box, '1.19.921', { privileged: true, dryRun: true });
500
+ for (const shared of ['sudo -u bongos-test4 ', 'env PGDATABASE=test4 ', '--to 1.19.921', '--registry']) {
501
+ assert.ok(move.cmd.includes(shared), `move carries ${shared}`);
502
+ assert.ok(peek.cmd.includes(shared), `preview carries the same ${shared}`);
503
+ }
504
+ assert.equal(move.instanceDir, peek.instanceDir, 'same target directory');
505
+ assert.equal(move.runAs, peek.runAs, 'same account');
506
+ // And the tail genuinely differs — asserted rather than assumed, so this test cannot
507
+ // quietly pass a preview that would restart the box.
508
+ assert.match(peek.cmd, /--dry-run/);
509
+ for (const post of ['--service', '--health-url', '--version-url', '--commit-pin', '--requested-by']) {
510
+ assert.ok(move.cmd.includes(post), `the move carries ${post}`);
511
+ assert.ok(!peek.cmd.includes(post), `the preview must NOT carry ${post} — it never gets that far`);
512
+ }
513
+ });
514
+
515
+ await t('an invalid slug is refused at the BUILDER, not left to a helper to throw', () => {
516
+ // The slug reaches a shell through instanceUser()/dbName(). Depending on another
517
+ // function's internals for that is one refactor away from an injection path.
518
+ assert.throws(() => R.upgradeInvocation({ slug: 'a; rm -rf /', port: 1, hosting_shape: 'standalone' }, '1.19.921', {}),
519
+ /invalid slug/);
520
+ });
521
+
522
+ await t('a dry run that fails WITHOUT a recognisable marker is still a refusal, never a pass', () => {
523
+ const deps = { privileged: false, exec: () => ({ ok: false, stdout: 'something went sideways' }) };
524
+ const r = R.preflightUpgrade({ id: 7, slug: 'test4', port: 3006, hosting_shape: 'standalone' }, '1.19.921', deps);
525
+ assert.equal(r.ok, false, 'a non-zero exit is a refusal whatever it printed');
526
+ assert.ok(r.error);
527
+ });
528
+
529
+ await t('the TICK never dry-runs — the verdict is asked for, not produced on a schedule', async () => {
530
+ // Re-running a check every tick for a box behind for hours spends a subprocess to
531
+ // re-learn the same answer, and is still missing in the window that matters.
532
+ const d = makeDeps({ served: '1.19.762' });
533
+ let spawned = 0;
534
+ d.preflightUpgrade = () => { spawned++; return { ok: true }; };
535
+ await R.snapshotCoreVersions(inst(), d);
536
+ assert.equal(spawned, 0, 'the version probe is per-tick; the dry run is not');
537
+ });
538
+
539
+ await t('the on-demand leg caches the verdict, and a REFUSAL is still a successful preview', async () => {
540
+ const d = makeDeps({ served: '1.19.762' });
541
+ d.preflightUpgrade = () => ({ target: '1.19.921', ok: false, lines: [], error: 'working tree not clean' });
542
+ const r = await R.preflightCoreUpgrade(inst(), d, intent({ target_version: '1.19.921' }));
543
+ assert.equal(r.ok, true, 'the CHECK ran — a refusal is what it was for, not a failure of it');
544
+ const write = d.queries.find((q) => /INSERT INTO provisioning_core_version_snapshot/.test(q.sql));
545
+ assert.ok(write, 'the verdict is cached');
546
+ assert.match(String(write.params[1]), /working tree not clean/);
547
+ });
548
+
549
+ await t('a preview of nothing is refused rather than cached as a pass', async () => {
550
+ const d = makeDeps({ served: '1.19.762' });
551
+ const r = await R.preflightCoreUpgrade(inst(), d, intent({ target_version: null }));
552
+ assert.equal(r.ok, false);
553
+ assert.equal(d.queries.filter((q) => /INSERT INTO provisioning_core_version_snapshot/.test(q.sql)).length, 0);
554
+ });
555
+
474
556
  console.log(`\ncore_upgrade_runner: ${passed} passed, ${failed} failed`);
475
557
  if (failed) process.exit(1);
@@ -75,12 +75,18 @@ function boot({ controlPlane = preview('1', 'cloudbongos'), controlPlaneStatus =
75
75
  OTB: { escapeHtml: (s) => String(s).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])), toast() {} },
76
76
  location: { assign() {} },
77
77
  };
78
+ // The page's poll interval, driven rather than slept through. Injecting the REAL
79
+ // setTimeout made every case whose preview never converges sit out the page's full
80
+ // ~30s wait in wall-clock, still repainting after the test had finished — slow, and
81
+ // flaky against a torn-down sandbox.
82
+ windowObj.__deployPollMs = 0;
83
+ windowObj.__deployPollTries = 3;
78
84
  const sandbox = { window: windowObj, document: documentObj, console, setTimeout, clearTimeout, queueMicrotask, Date, Set, Map, Promise };
79
85
  sandbox.globalThis = sandbox;
80
86
  vm.createContext(sandbox);
81
87
  vm.runInContext(SRC, sandbox, { filename: 'deploy.js' });
82
88
  documentObj._ready();
83
- const settle = async () => { for (let i = 0; i < 12; i++) await new Promise((r) => setTimeout(r, 0)); };
89
+ const settle = async () => { for (let i = 0; i < 40; i++) await new Promise((r) => setTimeout(r, 0)); };
84
90
  return { els, posts, gets, settle, panel: () => els.get('dp-panel').innerHTML, sub: () => els.get('dp-sub').textContent };
85
91
  }
86
92
 
@@ -126,13 +132,20 @@ test('confirming sends THAT project’s id and the version it was shown', async
126
132
  b.els.get('dp-preview-32')._on.click();
127
133
  b.els.get('dp-confirm-32')._on.click();
128
134
  await b.settle();
129
- assert.equal(b.posts.length, 1);
130
- assert.equal(b.posts[0].path, '/provisioning/instances/32/core-upgrade');
135
+ // Arming also asks for a dry run now (task 1004121), so the MOVE is the post that is
136
+ // not one — separated by its payload rather than by its position, because "the second
137
+ // post" would silently start asserting about the wrong request the next time the order
138
+ // changes.
139
+ const moves = b.posts.filter((x) => !(x.body && x.body.dry_run));
140
+ assert.equal(moves.length, 1, 'exactly one move is sent');
141
+ assert.equal(moves[0].path, '/provisioning/instances/32/core-upgrade');
131
142
  // Field-wise, not deepEqual: the body is built inside the vm realm, so its
132
143
  // prototype is not this realm's Object and deepStrictEqual rejects it on identity
133
144
  // alone — a harness artifact that says nothing about the payload.
134
- assert.equal(b.posts[0].body.to, '1.19.871');
135
- assert.deepEqual(Object.keys(b.posts[0].body), ['to'], 'and nothing else is sent');
145
+ assert.equal(moves[0].body.to, '1.19.871');
146
+ assert.deepEqual(Object.keys(moves[0].body), ['to'], 'and nothing else is sent');
147
+ // And the move must never carry the flag that would turn it into a no-op.
148
+ assert.equal(moves[0].body.dry_run, undefined, 'a move is not a dry run');
136
149
  });
137
150
 
138
151
  test('a box with nothing newer offers no button', async () => {
@@ -191,3 +204,89 @@ test('a move on the platform row re-reads the control plane, not a tenant previe
191
204
  assert.ok(b.gets.includes('/provisioning/control-plane'), 'the self row is read from its own endpoint');
192
205
  assert.ok(!b.gets.some((g) => /instances\/3\d\/core-upgrade/.test(g)), 'and no tenant is re-probed');
193
206
  });
207
+
208
+ // ---- the pre-flight verdict on step two (task 1004121) ---------------------------
209
+ // The defect: step one was called "see what would change" and changed nothing but a
210
+ // local flag. These read what step two actually PAINTS, because the bug was invisible
211
+ // to every assertion about what the page fetched — it fetched nothing.
212
+
213
+ test('step two shows what the dry run found, not just a restatement of the action', async () => {
214
+ const b = boot({ controlPlane: preview('1', 'cloudbongos', {
215
+ preflight: { target: '1.19.871', ok: true, lines: ['database "cloudbongos" reachable as the current user'], error: null },
216
+ preflight_at: new Date().toISOString(),
217
+ }) });
218
+ await b.settle();
219
+ b.els.get('dp-preview-1')._on.click();
220
+ const html = b.panel();
221
+ assert.match(html, /nothing blocks this move/i, 'it states a verdict');
222
+ assert.match(html, /database &quot;cloudbongos&quot; reachable|database "cloudbongos" reachable/, 'and shows what was checked');
223
+ });
224
+
225
+ test('a BLOCKED move says so, in the upgrade’s own words, and still offers the button', async () => {
226
+ const b = boot({ controlPlane: preview('1', 'cloudbongos', {
227
+ preflight: { target: '1.19.871', ok: false, lines: [], error: 'working tree not clean — commit/stash first, or pass --force.' },
228
+ preflight_at: new Date().toISOString(),
229
+ }) });
230
+ await b.settle();
231
+ b.els.get('dp-preview-1')._on.click();
232
+ const html = b.panel();
233
+ assert.match(html, /This would fail/, 'the owner is told before pressing');
234
+ assert.match(html, /working tree not clean/, 'verbatim — the wording names the fix');
235
+ // Deliberate: the runner refuses on its own, and this reading can be minutes old.
236
+ assert.match(html, /id="dp-confirm-1"/, 'the button is still there — a stale ✖ must not lock the owner out');
237
+ });
238
+
239
+ test('with no verdict yet, arming ASKS for one and says so — never an implied pass', async () => {
240
+ // The whole failure mode in one test: silence where a check should be must not look
241
+ // like a clean bill of health, and the page must actually go and get the answer rather
242
+ // than wait for a background tick that may be minutes away.
243
+ const b = boot({ controlPlane: preview('1', 'cloudbongos', { preflight: null, preflight_at: null }) });
244
+ await b.settle();
245
+ b.els.get('dp-preview-1')._on.click();
246
+ // While it is in flight the row says so — asserted BEFORE settling, because the whole
247
+ // point is what the owner sees during the wait.
248
+ assert.match(b.panel(), /Checking what this would do/, 'it states that a check is under way');
249
+ await b.settle();
250
+ const asks = b.posts.filter((x) => x.body && x.body.dry_run === true);
251
+ assert.equal(asks.length, 1, 'exactly one check is requested');
252
+ assert.equal(asks[0].body.to, '1.19.871', 'against the version on offer');
253
+ // And when the answer never arrives (this harness never converges), it falls back to
254
+ // the honest state rather than to a pass — which is the failure mode being guarded.
255
+ const settled = b.panel();
256
+ assert.match(settled, /Not checked yet/, 'an answer that never came is stated as such');
257
+ assert.doesNotMatch(settled, /nothing blocks this move/i, 'and never dressed up as a pass');
258
+ });
259
+
260
+ test('the poll does not close the panel the owner is reading', async () => {
261
+ // refreshOne() returns a row to step one — right after a MOVE, wrong during the wait.
262
+ // Polling with it un-armed the row under the owner every interval, so the confirm
263
+ // button vanished mid-check.
264
+ const b = boot({ controlPlane: preview('1', 'cloudbongos', { preflight: null, preflight_at: null }) });
265
+ await b.settle();
266
+ b.els.get('dp-preview-1')._on.click();
267
+ await b.settle();
268
+ assert.match(b.panel(), /id="dp-confirm-1"/, 'still armed after the poll has run and given up');
269
+ });
270
+
271
+ test('a poll whose row is closed stops instead of repainting a step nobody is on', async () => {
272
+ const b = boot({ controlPlane: preview('1', 'cloudbongos', { preflight: null, preflight_at: null }) });
273
+ await b.settle();
274
+ b.els.get('dp-preview-1')._on.click();
275
+ b.els.get('dp-cancel-1')._on.click();
276
+ await b.settle();
277
+ assert.match(b.panel(), /id="dp-preview-1"/, 'back at step one');
278
+ assert.doesNotMatch(b.panel(), /Checking what this would do/, 'and not still claiming to be checking');
279
+ });
280
+
281
+ test('a verdict already in hand is NOT re-requested', async () => {
282
+ // Re-asking would spend a subprocess on the box to reprint an answer already shown.
283
+ const b = boot({ controlPlane: preview('1', 'cloudbongos', {
284
+ preflight: { target: '1.19.871', ok: true, lines: ['database reachable'], error: null },
285
+ preflight_at: new Date().toISOString(),
286
+ }) });
287
+ await b.settle();
288
+ b.els.get('dp-preview-1')._on.click();
289
+ await b.settle();
290
+ assert.equal(b.posts.filter((x) => x.body && x.body.dry_run).length, 0, 'no check requested');
291
+ assert.match(b.panel(), /nothing blocks this move/i, 'the held verdict is shown');
292
+ });