| 1 | import assert from 'node:assert/strict'; |
| 2 | import { test } from 'node:test'; |
| 3 | import { compareBuilds, helloAction, mayAutoReload, retryDelay } from '../src/lib/engine-build.ts'; |
| 4 | |
| 5 | test('builds order by timestamp then tag', () => { |
| 6 | assert.ok((compareBuilds('200-aaa', '100-zzz') ?? 0) > 0); |
| 7 | assert.ok((compareBuilds('100-aaa', '200-aaa') ?? 0) < 0); |
| 8 | assert.ok((compareBuilds('100-abc', '100-abd') ?? 0) < 0); |
| 9 | assert.equal(compareBuilds('100-abc', '100-abc'), 0); |
| 10 | }); |
| 11 | |
| 12 | test('timestamps compare numerically, not as strings', () => { |
| 13 | assert.ok((compareBuilds('1000-a', '999-a') ?? 0) > 0); |
| 14 | }); |
| 15 | |
| 16 | test('malformed build ids never order', () => { |
| 17 | assert.equal(compareBuilds('dev', '100-a'), null); |
| 18 | assert.equal(compareBuilds('100-a', ''), null); |
| 19 | assert.equal(helloAction('100-a', 'garbage'), 'ignore'); |
| 20 | }); |
| 21 | |
| 22 | test('a worker yields to a newer peer', () => { |
| 23 | assert.equal(helloAction('100-a', '200-b'), 'yield'); |
| 24 | }); |
| 25 | |
| 26 | test('a worker announces itself to an older peer', () => { |
| 27 | assert.equal(helloAction('200-b', '100-a'), 'announce'); |
| 28 | }); |
| 29 | |
| 30 | test('the same build coexists', () => { |
| 31 | assert.equal(helloAction('100-a', '100-a'), 'ignore'); |
| 32 | }); |
| 33 | |
| 34 | test('two workers never both yield', () => { |
| 35 | const pairs: [string, string][] = [['100-a', '200-b'], ['100-b', '100-a'], ['5-x', '50-x']]; |
| 36 | for (const [one, two] of pairs) { |
| 37 | const actions = [helloAction(one, two), helloAction(two, one)]; |
| 38 | assert.equal(actions.filter((a) => a === 'yield').length, 1); |
| 39 | } |
| 40 | }); |
| 41 | |
| 42 | test('auto reload is limited to once per guard window', () => { |
| 43 | assert.equal(mayAutoReload(1_000_000, null), true); |
| 44 | assert.equal(mayAutoReload(1_030_000, 1_000_000), false); |
| 45 | assert.equal(mayAutoReload(1_060_000, 1_000_000), true); |
| 46 | }); |
| 47 | |
| 48 | test('retry delay backs off and caps', () => { |
| 49 | assert.deepEqual([0, 1, 2, 3, 4, 10].map(retryDelay), [1000, 2000, 4000, 8000, 8000, 8000]); |
| 50 | }); |