import './setup.mjs';
import test from 'node:test';
import assert from 'node:assert/strict';
import { load } from './setup.mjs';

const { bumpVersion, cloneForNextVersion, setRingIntent } = await load('../services/intunePromotionService.js');

test('bumpVersion increments the trailing number', () => {
  assert.equal(bumpVersion('1.2.3'), '1.2.4');
  assert.equal(bumpVersion('126'), '127');
  assert.equal(bumpVersion('3.1.0-x64'), '3.1.0-x65'); // bumps the last numeric run
  assert.equal(bumpVersion(''), '1.0.0');
  assert.equal(bumpVersion('stable'), 'stable.1');
});

test('cloneForNextVersion produces an unpublished next-version plan', () => {
  const source = {
    name: 'Contoso VPN 3.1.0',
    profileId: 'p1', applicationId: 'app1', commandStyle: 'v4', installBehavior: 'system',
    restartBehavior: 'return-code', uiMode: 'native-v4', requirements: { architecture: 'x64' },
    installCommand: 'i', uninstallCommand: 'u', detectionType: 'msi-product-code', detectionRule: '{x}',
    returnCodes: [{ code: 0, type: 'success' }], assignments: [{ ring: 'Pilot', intent: 'available' }],
    relationships: [], graphAppId: 'live-app', status: 'published', visibility: 'shared'
  };
  const clone = cloneForNextVersion(source, { version: '3.2.0' });
  assert.equal(clone.name, 'Contoso VPN 3.2.0');
  assert.equal(clone.graphAppId, '');
  assert.equal(clone.status, 'draft');
  assert.equal(clone.applicationId, 'app1');
  assert.equal(clone.detectionRule, '{x}');
  assert.deepEqual(clone.assignments, source.assignments);
});

test('cloneForNextVersion auto-bumps the version from the name when not given', () => {
  const clone = cloneForNextVersion({ name: 'Firefox 120.0', visibility: 'shared' }, {});
  assert.equal(clone.name, 'Firefox 120.1');
});

test('setRingIntent promotes only the named ring and reports change', () => {
  const assignments = [
    { ring: 'Pilot', intent: 'available' },
    { ring: 'Broad', intent: 'available' }
  ];
  const result = setRingIntent(assignments, 'Broad', 'required');
  assert.equal(result.changed, true);
  assert.equal(result.assignments.find((a) => a.ring === 'Broad').intent, 'required');
  assert.equal(result.assignments.find((a) => a.ring === 'Pilot').intent, 'available');
});

test('setRingIntent is a no-op for unknown rings', () => {
  const result = setRingIntent([{ ring: 'Pilot', intent: 'available' }], 'Nope', 'required');
  assert.equal(result.changed, false);
});
