const assert = require('assert');
const {
  findValidatorTarget,
  matchesValidatorTarget,
  nodeOwner
} = require('../services/validatorTargetBindingService');

const topology = {
  validators: [
    { nodeName: 'validator-4', pcName: 'WIN-I3J2KPSOOLB', owner: 'ellis' }
  ]
};
const correct = {
  pcName: 'WIN-I3J2KPSOOLB',
  userId: 'ellis',
  metadata: { hostname: 'WIN-I3J2KPSOOLB', ownerUserId: 'ellis' }
};
const wrongAccount = {
  pcName: 'WIN-I3J2KPSOOLB',
  userId: 'ellia',
  metadata: { hostname: 'WIN-I3J2KPSOOLB', ownerUserId: 'ellia' }
};
const conflictingOwnerFields = {
  pcName: 'WIN-I3J2KPSOOLB',
  userId: 'ellis',
  metadata: { hostname: 'WIN-I3J2KPSOOLB', ownerUserId: 'ellia' }
};

assert.equal(findValidatorTarget(topology, correct)?.nodeName, 'validator-4');
assert.equal(findValidatorTarget(topology, wrongAccount), null);
assert.equal(findValidatorTarget(topology, conflictingOwnerFields), null);
assert.equal(nodeOwner(conflictingOwnerFields), '');
assert.equal(matchesValidatorTarget(correct, { pcName: 'WIN-I3J2KPSOOLB' }), true);

console.log(JSON.stringify({
  ok: true,
  schemaVersion: 'plais-validator-target-binding-self-test/v1',
  correctOwnerAccepted: true,
  hostnameOnlyWrongOwnerRejected: true,
  conflictingOwnerFieldsRejected: true,
  legacyOwnerlessTargetAccepted: true
}, null, 2));
