import { strict as assert } from 'node:assert';
import test from 'node:test';
import { getClientRuntimeVersion, shouldPromptForNewRuntimeVersion } from './runtime-version';

test('getClientRuntimeVersion reads web build metadata from public environment variables', () => {
  const result = getClientRuntimeVersion({
    NEXT_PUBLIC_APP_VERSION: 'commit-789',
    NEXT_PUBLIC_APP_BUILD_TIME: '2026-07-06T15:00:00.000Z',
  });

  assert.deepEqual(result, {
    service: 'web',
    version: 'commit-789',
    buildTime: '2026-07-06T15:00:00.000Z',
  });
});

test('getClientRuntimeVersion uses stable fallback values without build metadata', () => {
  const result = getClientRuntimeVersion({});

  assert.deepEqual(result, {
    service: 'web',
    version: 'unknown',
    buildTime: '',
  });
});

test('shouldPromptForNewRuntimeVersion only prompts when both versions are known and different', () => {
  assert.equal(shouldPromptForNewRuntimeVersion('commit-a', 'commit-b'), true);
  assert.equal(shouldPromptForNewRuntimeVersion('commit-a', 'commit-a'), false);
  assert.equal(shouldPromptForNewRuntimeVersion('unknown', 'commit-b'), false);
  assert.equal(shouldPromptForNewRuntimeVersion('commit-a', 'unknown'), false);
  assert.equal(shouldPromptForNewRuntimeVersion('', 'commit-b'), false);
});
